in reply to Building a room system for an adventure game
Okay, I've never done this in perl. All of my mud coding was ~10 years ago on various LPC / MudOS varients.
However, what we did have was a more abstracted exit system -- after all, who's to say you're not going to way 'northwest' and the like? Or 'up' 'down', etc. (or any other direction 'in', 'out', etc...
sub addexit { my ($self, $exit, $direction) = @_; $self->{'_exits'}->{$direction} = $exit; } sub addhiddenexit { my ($self, $exit, $direction) = @_; $self->{'_hiddenexists'}->{$direction} = $exit; } sub showexits { my ($self) = @_; my $message = join "\n", 'The following exits are available:', map { ' $_\n' } sort keys %{ $self->{'_exits'} }; return $message; } sub exit { my ($self, $direction) = @_; if ( exists($self->{'_hiddenexits'}->{$direction}) ) { return $self->{'_hiddenexits'}->{$direction}; } if ( exists($self->{'_exits'}->{$direction}) ) { return $self->{'_exits'}->{$direction}; } return undef; }
Now we get to the difficult part ... I didn't like writing rooms the way you're doing it (all in code). The lib we were used filenames for the most part to load objects, but if you specified something of the format 'filename:arg', it would pass the argument to the init?new? function in the file. So I wrote an engine to generate the rooms from various configuration files (partially inspired by one on another mud I had done some coding on). For example, in your case:
# map file A-B # key file KEY : A NAME : Town Square KEY : B NAME : store
There was then a 'main' file, which inherited from the engine, and contained the following
The key file could handle naming, items to look at in the room, objects cloned in the room (monsters, removable objects, etc.), and the x/y file could also add extra exits / hidden exits.
The map file use a series of symbols to add exits
- : east / west | : north/south \ : nw/se / : ne/sw X : ne/sw/nw/se ^ : north v : south > : east < : west . : hidden (n/s or e/w, depending on placement)
Oh -- and a room should probably inherit from 'container' or something similar -- no reason to duplicate the same code for keeping track of people in a room, as for keeping track of items in a bag, items in a person's inventory, etc.
And the exit code is simplified -- you might want to add support for common abbreviations (n:north, nw:northwest, etc.)
|
|---|