File manipulation

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 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 K.
[email protected] wrote:

An example of how it might work is: Ruby has a table of ruby phrases and

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

Sorry: I should have said, “Hi Tim”.

On Thu, Jul 31, 2008 at 9:48 PM, Shadowfirebird
[email protected] wrote:

swords = []; swords << sword.new()

equivalent LPC code.
Depends how many phrases you got there. If there are just a few of them and
phrases[m] or raise “Phrase not found: #{m.inspect}”

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

On 31.07.2008 20:22, Tim M. wrote:

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

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! :slight_smile: Oh, and Shadowfirebird, my email is:
[email protected]

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

On 8/1/08, Martin DeMello [email protected] wrote:

On Thu, Jul 31, 2008 at 1:48 PM, Shadowfirebird
[email protected] 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

On Thu, Jul 31, 2008 at 1:48 PM, Shadowfirebird
[email protected] wrote:

Event.new(Sword.new.strike(:martin))

hey!!!

martin

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 L.'s TeensyMud:
http://sourcery.dyndns.org/wiki.cgi?TeensyMud

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

If you’re listening, Jon, thanks.

Shadowfirebird.

On 7/31/08, Tim M. [email protected] 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! :slight_smile: Oh, and Shadowfirebird, my email is:
[email protected]


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


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 M. wrote:

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

Event.new(Sword.new.strike(:martin))

hey!!!

martin

No worries. It’s just a symbol.