Author Topic: good way to implement an array of stacks/linked lists (for 2D tile-based game)?  (Read 4372 times)

0 Members and 1 Guest are viewing this topic.

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
I suppose the thing to be thinking about is if there's going to be less tiles to process for objects, or less objects to process than tiles. 
The answer to that question might affect the efficiency of your data structure significantly.

Very true. If the map is small but there are lots of objects, performance will be different than a large map with not many objects.

I think using a single (1D) array to store multiple stacks of UDTs will work for me.
(See latest code above, method #5.)

Offline madscijr

  • Seasoned Forum Regular
  • Posts: 295
    • View Profile
knowing nothing about just what kind of game your working on all I could do is give you an example from my current project Phantasy Star.
...
I know this isn't technically Bill's "Linked Lists" thing  but its my view as where you use a value in one array to look at another (to look at another and so on if needed) where what is in that second array can change without affecting the first.

Thanks for taking the time to give such a detailed answer.
I'll definitely give this a look when I have more time (out of time today).

Currently I think I have a workable solution, using one 2-D array for a map, where each tile contains the index of the "top" of a stack, where all stacks are stored in a single 1-D array.
(See above post, method #5.)

This implementation uses a PUSH function to add items dynamically, with a REDIM _PRESERVE.
It has logic to check to see if there is a formerly deleted stack node to reuse, in which case we don't have to REDIM.

I think deletion would be slow if we were to REDIM _PRESERVE every time we wanted to remove an item,
so instead I implemented a logical delete, where arrStack(0) always points to the next available (deleted) item.
And the deleted item's NextIndex points to the next deleted item, and so on.
For my game, I don't foresee the list ever getting so huge and having so many deleted items that we need to worry about memory,
but a "cleanup" routine could always be added where we track the # of deleted items, and if it passes some threshold,
we physically delete all the outstanding items.
However, reusing previously deleted items has a benefit of speeding up an insert (faster than REDIM _PRESERVE),
and I think inserts will occur about as frequently as deletes.

For both the stack items and deleted items chains, if we find .NextIndex = 0, we know we're at the bottom of that particular stack.

Anyway that's what I came up with so far...

Thanks again for your and everyone's replies and suggestions!
« Last Edit: June 30, 2021, 05:54:34 pm by madscijr »