Sometimes, this kind of problem can be solved easily when turned upside down. I understand that you want to keep a representation of your world in memory. The method you expose has important drawbacks from a memory usage standpoint. Probably, Perl6's lazy lists might come handy, but we do not have that yet, do we?
A more conservative approach, would be to have each object track its own position within the world, as a set of three coordinates which is what your array suggests. You could have something like...
package BaseItem;
sub moveto ($$$$) {
my $self = shift;
my $new_x = shift;
my $new_y = shift;
my $new_z = shift;
...
}
sub position ($) {
my $self = shift;
return ($self->{x}, $self->{y}, $self->{z});
}
...
This would be a basic item in your game, providing some basic properties such as the ability to move and the ability to report its current position.
Regular items might be something like:
package Item;
our @ISA = qw(BaseItem);
...
So they inherit the properties of BaseItem such as the position and possibly things like mass, volume or standard modifiers.
I would then build a class Container which could be used to hold a (number of) objects, moving them all together. This class could also be used in the Player class, as players can carry things with them.
Since normaly you tend to have a smaller number of objects than rooms in your world, it makes more sense to scan the list of objects in order to find which are in a given room, specially since you do not need to figure this out every time for every room.
If you want to go multiplayer or even massively multiplayer, a database backend might be used to store the object and player data. But I digress...
Grouping base properties of the items in classes like these, would also allow you to build caches to reduce the work associated with drawing a room. A cache might very well hold in a hash or similar structure, where are the objects since this would only need to hold a part of the world at a time.
As you might guess from this post, I have had a bit of work in this area before and would be willing to devote some spare time to something like what you're building.
Good luck...
|