MUD in 15 Lines of Ruby #
This is great. In response to a post about how writing a MUD in Ruby would be “a horrifically complex and lengthy project,” Jon Lambert dropped a 15-line MUD in the response! Now that’s a comeback.
 require 'socket';require 'yaml';def q x;$m.find{|o|o.t==:p&&x==o.n};end
 def a r,t;$m.find_all{|o|t==o.t&&(!r||r==o.l)};end;def g z;a(nil,:p).each{|p|
 p.p z};end;class O;attr_accessor :i,:n,:l,:e,:s,:t;def initialize n,l=nil,t=:r
 @n,@l,@i,@t=n,l,$d+=1,t;@e={};end;def p s;@s.puts(s)if @s;end;def y m
 v=$m.find{|o|@l==o.i};t=v.e.keys;case m;when/^q/;@s.close;@s=nil;
 File.open('d','w'){|f|YAML::dump $m,f};when/^h/;p "i,l,d,g,c,h,q,<exit>,O,R" 
 when/^i/;a(@i,:o).each{|o|p o.n};when/^c.* (.*)/;g "#{@n}:#{$1}" 
 when/^g/;a(@l,:o).each{|q|q.l=@i};when/^d/;a(@i,:o).each{|q|q.l=@l}
 when/^O (.*)/;$m<<O.new($1,@l,:o);when/^R (.*) (.*) (.*)/;$m<<d=O.new($1)
 v.e[$2]=d.i;d.e[$3]=v.i;when/^l/;p v.n;(a(@l,:p)+a(@l,:o)).each{|x|
 p x.n if x.s||x.t==:o};p t.join '|';when/(^#{t.empty? ? "\1" : t.join('|^')})/
 @l=v.e[$1];else;p "?";end;end;end;test ?e,'d'||begin;$d=0;$m=[O.new("Home")]
 end;$m=YAML::load_file 'd';$d=$m.size;z=TCPServer.new 0,4000;while k=z.accept
 Thread.new(k){|s|s.puts "Name";s.gets;l=$_.chomp;d=q l;$m<<d=O.new(l,1,:p)if !d
 d.s=s;while s.gets;d.y $_.chomp;end;};end
	A bit of sleuthing on the matter and I’ve discovered that this is a derivative of Jon’s TeensyMud. He has nice, readable versions in his repository, if you like.
If you want to play with this one:
- Save the above to teensymud.rb.
- Create a YAML file named “d” (for dungeon) with a blank array in it.  (Likeso: echo "[]" > d.)
- Run teensymud.rb.
- Telnet to localhostport 4000.
- At Nameprompt, enter your name then<Enter>.
| Play commands | |
| i | displays player inventory | 
| l | displays the contents of a room | 
| dr | drops all objects in your inventory into the room | 
| g | gets all objects in the room into your inventory | 
| k (name) | attempts to kill player (e.g. k bubba) | 
| s (message) | sends (message) to all players in the room | 
| c (message) | sends (message) to all players in the game | 
| q | quits the game (saves player) | 
| (exit name) | moves player through exit named (ex. south) | 
| OLC | |
| O (object name) | creates a new object (ex. O rose) | 
| R (room name) (exit name to) (exit name back) | creates a new room and autolinks the exits using the exit names provided. | 
Please, tell us more, Jon. If you’re feeling nostalgic and want to egg things on, come join ruby-talk this week. The Ruby Quiz will be a MUD client. (extracted from ruby-talk:154208.)


 

michael
Freakin’ amazing. I love Ruby!
odanuki
ack! ack! my long dormant mud addiction is in danger of being rekindled
greasygreasy
_why, you forgot to escape the < > tags in that text. You should see what it’s doing to Safari.
The page just ENDS before < object name >, same with the main page.
why
My thanks to you, emphatically greased.
Dave Burt
The instructions are longer than the entire code for the MUD .
...and I see you’ve included my bug fix :)
why
I really want to know how you discovered that.
mrchucho
I’ve been reading MUD Game Programming and implementing its “SimpleMUD” in Ruby rather than C++. Not only is it possible, it’s awesome.
Stuff like Teensy MUD ’s YAML databases and being able to put regexps in a case statement streamlines any MUD . Ruby is well-suited for MUD development.
I’ve even been able to use irb in lieu of OLC !
_why the cult follower
I learned the C-syntax using LPC .
Dave Burt
I discovered it while de-obfuscating the script. I still haven’t executed the thing.
The script is actually remarkably simple – well done, Jon.
Long version: look at the immediate context (with the missing ”=”):
.each{|x| p x.n if x.s||x.t=:o}Now, that is going to
pallx.n, and overwritex.twith a literal:o. That’s pretty sus already; it makes more sense for that expression just to be a condition. Then,.nis for name,.tis for type, and:ois for object (the kind you can pick up). Finally, this iswhen/^l/—in response to the look command.Sy
Geeze, what have I unleashed?
Dave Burt
Sy: And the funny thing is, you only asked for a client!
J. Lambert
Oh there’s no need to create a ‘d’ file just run it. When you quit the world will be saved. A fixed version of the above with Dave’s “medusa” bug fix is at ftp://sourcery.dyndns.org/pub/mud/teensymud/t14k.tgz
I’m up to release 2.4 now around 32K of code. If you don’t have a juvenile sense of humor you may want to steer clear from the larger version.
Bob Maguire
I don’t get it. Why is this a good thing? I agree it’s not lengthy, but how is that not “horrifically complex” in its readability?
Scatman Dan
Just plain beautiful.
J. Lambert
I created a wiki/forum site for TeensyMud. Not much there, but there is a list of other ruby muds there that I know about.
I call it TeensyWeb (of course). I’m planning to release the site code as well. Since this is my first rails app, I expect there’s a lot of bugs in it.
Comments are closed for this entry.