[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

File manipulation

Tim Mcd

7/31/2008 6:22:00 PM

So, I am currently working on a MUD (Online multiplayer text based game,
like an MMO). The MUD's scripting language is LPC. I am trying to create
a ruby script, that reads another ruby file, and changes all instances
of 'Sword-of-might = Sword.new(...)' to something like 'static void
create() {...}'. Nothing too fancy, just me being able to go through a
file finding different bits of ruby that I can then replace with the
equivalent LPC code.

An example of how it might work is: Ruby has a table of ruby phrases and
what to turn them into if encountered. Ruby then reads the given file,
and if it finds any of the given phrases, turns them into the
corresponding LPC that I have defined.

So, the problem is, I have no idea how to go about doing something
like this in Ruby. Any thoughts/help or places to look? (tutorials,
etc.)
--
Posted via http://www.ruby-....

9 Answers

Robert Klemme

7/31/2008 6:46:00 PM

0

On 31.07.2008 20:22, Tim Mcd wrote:
> So, I am currently working on a MUD (Online multiplayer text based game,
> like an MMO). The MUD's scripting language is LPC. I am trying to create
> a ruby script, that reads another ruby file, and changes all instances
> of 'Sword-of-might = Sword.new(...)' to something like 'static void
> create() {...}'. Nothing too fancy, just me being able to go through a
> file finding different bits of ruby that I can then replace with the
> equivalent LPC code.
>
> An example of how it might work is: Ruby has a table of ruby phrases and
> what to turn them into if encountered. Ruby then reads the given file,
> and if it finds any of the given phrases, turns them into the
> corresponding LPC that I have defined.
>
> So, the problem is, I have no idea how to go about doing something
> like this in Ruby. Any thoughts/help or places to look? (tutorials,
> etc.)

Depends how many phrases you got there. If there are just a few of them
and they are fixed, you can do something like this:

phrases = {
"foo" => "bar",
"baz" => "boo!",
}

dat = File.read input_file

dat.gsub! Regexp.union(phrases.keys) do |m|
phrases[m] or raise "Phrase not found: #{m.inspect}"
end

File.open out, "w" do |io|
io.write dat
end

Kind regards

robert

Shadowfirebird

7/31/2008 8:48:00 PM

0

Hi Robert,

It seems to me that that's a hell of a difficult thing to do.

Take your Sword.new() call. There are a lot of different ways I
might code that (especially in Ruby!). A simple search and replace
will probably not be very reliable.

s = Sword.new()
OR
swords = []; swords << sword.new()
OR
Event.new(Sword.new.strike(:martin))

...or whatever. And that's just one statement -- it says nothing of
the multitude of different ways that a given problem can be solved
using Ruby code.

Is there any particular reason why you need to translate Ruby to LPC?
Why not just use Ruby as the in-game language?


(Confession time: I'm writing a MUSH myself, although mine is really
just a learning exercise, and, I suspect, a great deal less ambitious
than your project. For various reasons I'd dismissed the idea of
users writing code within the game, one of them being that I couldn't
see of any easy way to protect the game from malicious code. I'd be
interested on your thoughts on this, either inside or outside the
list.)

Shadowfirebird.


On Thu, Jul 31, 2008 at 7:49 PM, Robert Klemme
<shortcutter@googlemail.com> wrote:
> On 31.07.2008 20:22, Tim Mcd wrote:
>>
>> So, I am currently working on a MUD (Online multiplayer text based game,
>> like an MMO). The MUD's scripting language is LPC. I am trying to create
>> a ruby script, that reads another ruby file, and changes all instances
>> of 'Sword-of-might = Sword.new(...)' to something like 'static void
>> create() {...}'. Nothing too fancy, just me being able to go through a
>> file finding different bits of ruby that I can then replace with the
>> equivalent LPC code.
>>
>> An example of how it might work is: Ruby has a table of ruby phrases and
>> what to turn them into if encountered. Ruby then reads the given file,
>> and if it finds any of the given phrases, turns them into the
>> corresponding LPC that I have defined.
>>
>> So, the problem is, I have no idea how to go about doing something
>> like this in Ruby. Any thoughts/help or places to look? (tutorials,
>> etc.)
>
> Depends how many phrases you got there. If there are just a few of them and
> they are fixed, you can do something like this:
>
> phrases = {
> "foo" => "bar",
> "baz" => "boo!",
> }
>
> dat = File.read input_file
>
> dat.gsub! Regexp.union(phrases.keys) do |m|
> phrases[m] or raise "Phrase not found: #{m.inspect}"
> end
>
> File.open out, "w" do |io|
> io.write dat
> end
>
> Kind regards
>
> robert
>
>



--
Me, I imagine places that I have never seen / The colored lights in
fountains, blue and green / And I imagine places that I will never go
/ Behind these clouds that hang here dark and low
But it's there when I'm holding you / There when I'm sleeping too /
There when there's nothing left of me / Hanging out behind the
burned-out factories / Out of reach but leading me / Into the
beautiful sea

Shadowfirebird

7/31/2008 8:53:00 PM

0

Sorry: I should have said, "Hi Tim".

On Thu, Jul 31, 2008 at 9:48 PM, Shadowfirebird
<shadowfirebird@gmail.com> wrote:
> Hi Robert,
>
> It seems to me that that's a hell of a difficult thing to do.
>
> Take your Sword.new() call. There are a lot of different ways I
> might code that (especially in Ruby!). A simple search and replace
> will probably not be very reliable.
>
> s = Sword.new()
> OR
> swords = []; swords << sword.new()
> OR
> Event.new(Sword.new.strike(:martin))
>
> ...or whatever. And that's just one statement -- it says nothing of
> the multitude of different ways that a given problem can be solved
> using Ruby code.
>
> Is there any particular reason why you need to translate Ruby to LPC?
> Why not just use Ruby as the in-game language?
>
>
> (Confession time: I'm writing a MUSH myself, although mine is really
> just a learning exercise, and, I suspect, a great deal less ambitious
> than your project. For various reasons I'd dismissed the idea of
> users writing code within the game, one of them being that I couldn't
> see of any easy way to protect the game from malicious code. I'd be
> interested on your thoughts on this, either inside or outside the
> list.)
>
> Shadowfirebird.
>
>
> On Thu, Jul 31, 2008 at 7:49 PM, Robert Klemme
> <shortcutter@googlemail.com> wrote:
>> On 31.07.2008 20:22, Tim Mcd wrote:
>>>
>>> So, I am currently working on a MUD (Online multiplayer text based game,
>>> like an MMO). The MUD's scripting language is LPC. I am trying to create
>>> a ruby script, that reads another ruby file, and changes all instances
>>> of 'Sword-of-might = Sword.new(...)' to something like 'static void
>>> create() {...}'. Nothing too fancy, just me being able to go through a
>>> file finding different bits of ruby that I can then replace with the
>>> equivalent LPC code.
>>>
>>> An example of how it might work is: Ruby has a table of ruby phrases and
>>> what to turn them into if encountered. Ruby then reads the given file,
>>> and if it finds any of the given phrases, turns them into the
>>> corresponding LPC that I have defined.
>>>
>>> So, the problem is, I have no idea how to go about doing something
>>> like this in Ruby. Any thoughts/help or places to look? (tutorials,
>>> etc.)
>>
>> Depends how many phrases you got there. If there are just a few of them and
>> they are fixed, you can do something like this:
>>
>> phrases = {
>> "foo" => "bar",
>> "baz" => "boo!",
>> }
>>
>> dat = File.read input_file
>>
>> dat.gsub! Regexp.union(phrases.keys) do |m|
>> phrases[m] or raise "Phrase not found: #{m.inspect}"
>> end
>>
>> File.open out, "w" do |io|
>> io.write dat
>> end
>>
>> Kind regards
>>
>> robert
>>
>>
>
>
>
> --
> Me, I imagine places that I have never seen / The colored lights in
> fountains, blue and green / And I imagine places that I will never go
> / Behind these clouds that hang here dark and low
> But it's there when I'm holding you / There when I'm sleeping too /
> There when there's nothing left of me / Hanging out behind the
> burned-out factories / Out of reach but leading me / Into the
> beautiful sea
>
>



--
Me, I imagine places that I have never seen / The colored lights in
fountains, blue and green / And I imagine places that I will never go
/ Behind these clouds that hang here dark and low
But it's there when I'm holding you / There when I'm sleeping too /
There when there's nothing left of me / Hanging out behind the
burned-out factories / Out of reach but leading me / Into the
beautiful sea

Tim Mcd

7/31/2008 9:38:00 PM

0

Hmm... could I possibly see your MUSH code? I have pretty much stuck to
Interactive Fiction with ruby (afraid of networking!) I was wanting to
port Ruby to LPC just for the fun of it. Being able to create rooms on
my MUD with Ruby would be fun! ^_^ Oh, and Shadowfirebird, my email is:
tmcdowell@gmail.com

--
Posted via http://www.ruby-....

Martin DeMello

7/31/2008 11:05:00 PM

0

On Thu, Jul 31, 2008 at 1:48 PM, Shadowfirebird
<shadowfirebird@gmail.com> wrote:
> Event.new(Sword.new.strike(:martin))

hey!!!!

martin

Shadowfirebird

8/1/2008 9:21:00 AM

0

Event.new(shadowfirebird.cast(:heal, :martin))

On 8/1/08, Martin DeMello <martindemello@gmail.com> wrote:
> On Thu, Jul 31, 2008 at 1:48 PM, Shadowfirebird
> <shadowfirebird@gmail.com> wrote:
>> Event.new(Sword.new.strike(:martin))
>
> hey!!!!
>
> martin
>
>


--
Me, I imagine places that I have never seen / The colored lights in
fountains, blue and green / And I imagine places that I will never go
/ Behind these clouds that hang here dark and low
But it's there when I'm holding you / There when I'm sleeping too /
There when there's nothing left of me / Hanging out behind the
burned-out factories / Out of reach but leading me / Into the
beautiful sea

Shadowfirebird

8/1/2008 9:36:00 AM

0

Well, my current working code is only a proof-of-concept thing and
extremely icky; one big file, with a bunch of stuff hardcoded. But
you can walk around, pick up and drop stuff, dig rooms, and create
items. For better or worse, I'll forward it to you.

My own inspiration came from Jon Lambert's TeensyMud:
http://sourcery.dyndns.org/wiki.cgi...

And, even better, this post, again by Jon, that does a MUD in 15 lines
of Ruby(!): http://redhanded.hobix.com/bits/mudIn15LinesO...

If you're listening, Jon, thanks.

Shadowfirebird.

On 7/31/08, Tim Mcd <tmcdowell@gmail.com> wrote:
> Hmm... could I possibly see your MUSH code? I have pretty much stuck to
> Interactive Fiction with ruby (afraid of networking!) I was wanting to
> port Ruby to LPC just for the fun of it. Being able to create rooms on
> my MUD with Ruby would be fun! ^_^ Oh, and Shadowfirebird, my email is:
> tmcdowell@gmail.com
>
> --
> Posted via http://www.ruby-....
>
>


--
Me, I imagine places that I have never seen / The colored lights in
fountains, blue and green / And I imagine places that I will never go
/ Behind these clouds that hang here dark and low
But it's there when I'm holding you / There when I'm sleeping too /
There when there's nothing left of me / Hanging out behind the
burned-out factories / Out of reach but leading me / Into the
beautiful sea

Ian Hobson

8/1/2008 11:33:00 AM

0

Tim Mcd wrote:
> So, I am currently working on a MUD (Online multiplayer text based game,
> like an MMO). The MUD's scripting language is LPC. I am trying to create
> a ruby script, that reads another ruby file, and changes all instances
> of 'Sword-of-might = Sword.new(...)' to something like 'static void
> create() {...}'. Nothing too fancy, just me being able to go through a
> file finding different bits of ruby that I can then replace with the
> equivalent LPC code.
>
> An example of how it might work is: Ruby has a table of ruby phrases and
> what to turn them into if encountered. Ruby then reads the given file,
> and if it finds any of the given phrases, turns them into the
> corresponding LPC that I have defined.
>
> So, the problem is, I have no idea how to go about doing something
> like this in Ruby. Any thoughts/help or places to look? (tutorials,
> etc.)
>
Hi Tim,

I suspect your phrase replacement scheme will not cover all your needs.
You need a more powerfull technique.

May I suggest you structure it like a compiler. Use a tree structure for
the first intermediate form, generated (with dictionary of names or
symbol table) from the first pass. The second pass would walk this tree,
calling first level semantic routines for each node. These would, in
their turn call routines (methods in an object) that knows about LPC.
The interface between the first and second level back end is important,
but need never actually go to disk.

Not knowing the structure of the files, I can't say how much of the
structure of Ruby you will have to include. Until that is established,
you can't decide if you are going to go top-down or bottom-up - see any
book on compilers. I find top-down easeir to undertand, generates more
meaningsul error messages, and has better error recovery than bottom-up,
but that may be my brain.

By replacing one set of LPC routines with another set, you get an
interpreter of the file. This is the same as replacing the LPC object
with an interpreting object. Note, if you call the semantic routines
earlier, instead of creating the tree form, then compiling may be
quicker, but interpretation becomes impossible. You can't recompile each
time through a loop, becasue you get problems with the state being
changed as well as slow performance.

You might also look at what the LPC compiler actually produces and
generate the same. This will give you a way to write your scripts in (a
subset of) Ruby.

Regards

Ian

Phlip

8/1/2008 1:57:00 PM

0

>> Event.new(Sword.new.strike(:martin))
>
> hey!!!!
>
> martin

No worries. It's just a symbol.