Building a room system for an adventure game
yoda54
created: 2006-08-05 00:42:46
Greetings Monks, I'm currently building a adventure game and I'm bit stuck on how to build a room system. How would I generate a series of connected rooms? Here is what my package looks like so far. Each object has four exit attributes which I store references to other rooms.

Thanks for any advice!


package main:

use Room;

# this is how I generate rooms and add exits

my $townsquare = Room->new(name => "Town Square");
my $training = Room->new(name => "store");
$townsquare->addeast($store);
$store->addwest($townsquare);



package Room;

use strict;
use warnings;

sub new {
    my ($class, %arg) = @_;
    my $objref = {
        _name                   => $arg{name}               || "Town Square",
        _collection             => $arg{collection}         || [],
        _enemies                => $arg{enemies}            || "off",
        _exiteast               => $arg{exiteast}           || "none",
        _exitwest               => $arg{exitwest}           || "none",
        _exitsouth              => $arg{exitsouth}          || "none",
        _exitnorth              => $arg{exitnorth}          || "none",

        _engine                 => $arg{engine}             || 0
    };

    bless $objref, $class;
    return $objref;
}

sub enter {

    #add person to collection
    my $self  = shift;
    my $ref   = shift;
    my $aref  = $self->{_collection};
    push @$aref, $ref;
}


sub leave {
    my $self  = shift;
    my $ref   = shift;
    my $aref  = $self->{_collection};

    my $count = 0;

    #remove person from collection
    foreach(@$aref) {
        if ($ref == $_) {
            splice @$aref, $count, 1;
        }
        $count++;
    }
}
sub addeast {
    my $self  = shift;
    my $exit  = shift;
    $self->{_exiteast} = $exit;
}

sub addwest {
    my $self  = shift;
    my $exit  = shift;
    $self->{_exitwest} = $exit;
}

sub addsouth {
    my $self  = shift;
    my $exit  = shift;
    $self->{_exitsouth} = $exit;
}

sub addnorth {
    my $self  = shift;
    my $exit  = shift;
    $self->{_exitnorth } = $exit;
}

sub name { $_[0]->{_name}; }



sub exitwest  { $_[0]->{_exitwest}; }
sub exiteast  { $_[0]->{_exiteast}; }
sub exitsouth { $_[0]->{_exitsouth}; }
sub exitnorth { $_[0]->{_exitnorth}; }

1;
Re: Building a room system for an adventure game
created: 2006-08-05 05:56:40
This looks alright, but it might get a bit messy when you're creating circular references. I think it might be easier to use some kind of "table" to initialize the objects from:

my %rooms = (
   townsquare => { 
      name => 'Town Square'
      east => 'store',
   },
   store => {
      name => 'Town Store',
      west => 'townsquare',
   }
);
my %room = ();
# create rooms without connections
while (my ($id,$roominfo) = each %rooms) {
   $room{$id} = Room->new($roominfo->{name}); 
}

# since we now already have all room objects,
# it's easier to add connections to them
while (my ($id, $roominfo) = each %rooms) {
   for (qw(east west north south)) {
     $room{$id}->"add$_"($room{$roominfo->{$_}});
  }
}

Ofcourse, you could also store just the roomid for connections in the room objects and look them up in the hash when the exitX() methods are called.

[id://149675|"What should it profit a man, if he should win a flame war, yet lose his cool?"]
Re: Building a room system for an adventure game
created: 2006-08-05 08:32:01

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

  • where to find the map file
  • where to find the 'key' file
  • where to find a file that tracked by x/y coordinates
  • what file each of the rooms inherited from

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.)

Re: Building a room system for an adventure game
created: 2006-08-07 05:48:15

Originally I came up with the same basic solution as [Joost] thinking that the forward references would be a problem, however I still thought it would be nice to have direct access to the objects without an intervening global hash. So I came up the following madness that creates a new singleton class for each room on the fly in the same manner as [id://553767] (and thus comes with all the same caveats). The advantage of using a singleton class is that the problem with the forward references goes away, you just call the constructor whenever you need to and you will either get a new object or get the same one if the constructor had already been called. Obviously creating individual classes for the rooms might not be considered the best design choice by OO purists but that is necessary here for the singleton thing to work.

use strict;
use warnings;
 
my $world = World->new();
my $current = $world->start();
print $current->describe_room();
 
$current = $current->exit("north");
print $current->describe_room();
 
package World;
 
sub new
{
   my ($class) = @_;
 
   my $self = bless {}, $class;
 
 
   while()
   {
     chomp;
     my ($room_name,$start,$north,$south,$east,$west) = split /,/;
 
     my $room = $room_name->instance();
     $room->add_exit("north",$north->instance()) if $north;
     $room->add_exit("south",$south->instance()) if $south;
     $room->add_exit("east",$east->instance()) if $east;
     $room->add_exit("west",$west->instance()) if $west;
     $room->description($room_name);
     $self->start($room) if $start;
   }
 
   return $self;
}
 
 
sub start
{
    my ($self, $start ) = @_;
 
    if ( defined $start )
    {
       $self->{_start_room} = $start;
    }
 
    return $self->{_start_room};
}
 
package Room;
 
use base qw(Class::Singleton);
 
my @exits = qw(north south east west);
 
sub add_exit
{
   my ( $self,$direction, $room ) = @_;
 
   $self->{_exits}->{$direction} = $room;
}
 
sub exit
{
   my ($self, $direction ) = @_;
 
   return exists $self->{_exits}->{$direction} ?
                 $self->{_exits}->{$direction} : undef;
}
 
sub exits
{
   my ($self) = @_;
 
   return keys %{$self->{_exits}};
}
 
sub describe_room
{
    my ($self) = @_;
 
    my $description = "You are in the " . $self->description() ."\n";
    $description .= "There are exits:\n";
    foreach my $exit ( $self->exits() )
    {
      $description .= "To the $exit going to the "
                      . $self->exit($exit)->description() . "\n";
    }
 
    return $description;
}
 
sub description
{
   my ( $self, $description ) = @_;
 
   if (defined $description)
   {
      $self->{_description} = $description;
   }
   return $self->{_description};
}
 
sub DESTROY {}
 
package UNIVERSAL;
 
sub DESTROY {}
 
sub AUTOLOAD
{
   my ($class, @args) = @_;
 
   our $AUTOLOAD;
 
   (my $method = $AUTOLOAD) =~ s/.*:://;
 
 
 
   no strict 'refs';
 
   push @{"${class}::ISA"},'Room' ;
 
   $class->$method( @args);
}
 
package main;
 
__END__
Hall,1,Store,,,
Store,0,,Hall,Cupboard,Corridor
Cupboard,0,,,,Store
Corridor,0,,,Store,
Obviously that is the bare bones to demonstrate that the scheme works, and quite honestly I wouldn't recommend it to anyone of faint heart, but it's another way of doing it.

/J\

perlmonks.org content © perlmonks.org and gellyfish, jhourcle, Joost, yoda54

prlmnks.org © 2006 edmund von der burg (eccles & toad)

v 0.03