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


In reply to Re: RPG Engine Location Tracking (Large Three Dimentional Array?) by fokat
in thread RPG Engine Location Tracking (Large Three Dimentional Array?) by Flame

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.