Re: Linked Lists and Arrays

From: Daniel A. Koepke (dkoepke@california.com)
Date: 05/02/00


On Tue, 2 May 2000, Zachary Tellman wrote:

> Couldn't you also define multiple 2-d arrays, one for each elevation.

Yes, of course.  Depending on how you want to store it.  You could quite
easily make it an actual array of variable sized single dimension arrays:

    int i;
    struct map_data **layers;
    CREATE(layers, struct map_data **, NumLayers);
    for (i = 0; i < NumLayers; i++)
        CREATE(*layers, struct map_data *, layerSize[i]);

Where 'layerSize[i]' holds the size of layer[i] (e.g.., for a 320x200
layer, it would be 64000).

> However, passing a player from array to array could make things
> somewhat more complicated.

Not really.  You would store which layer of the map the player is on in
character_data (or elsewhere in your structures) along with the (x, y)
value.  Moving the player to another layer should be as simple as:

    char_from_room(ch);
    char_to_room(ch, MAP(layer[i], x, y, z));

where char_from_room() and char_to_room() take care to update the
character's 'z' value as well as his 'x' and 'y'.  Handling players on
different layers should be pretty simple.  Let's say you want 'where' to
show every player on the current layer for Bob.  Ignoring some obvious
optimizations (such as having the layer maintain a linked list of everyone
on it), we can do this as such:

    struct descriptor_data *d;
    struct char_data *targ;

    *buf = '\0';

    for (d = descriptor_list; d; d = d->next) {
        targ = d->character;
        if (d->playing == CON_PLAYING && targ && targ->z == ch->z) {
            sprintf(buf, "%-20s - %s\r\n", GET_NAME(targ),
                    get_location_name(targ->x, targ->y, targ->z));
        }
    }

Note that we could also use the single dimensional array with each layer
being variable sized, but it becomes a bit of headache to keep track of
the boundaries at that point, and easier to just make an array of the
arrays.

That last sentence, BTW, answers your question: is it possible to make an
array of arrays?  Of course.  In fact,

    int a[2][5];

is just that: it's an array of size 5 of an array of size 2.  Which is why
we fill it in as such:

    int a[2][5] = {
        { 0, 1 },
        { 2, 3 },
        { 4, 5 },
        { 6, 7 },
        { 8, 9 }
    };

There's 5 entries in the array 'a'.  Each entry, however, is an array of
size 2.  Thus, 'a' holds 10 integer elements.

-dak


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html  |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 04/10/01 PDT