This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Is there a better way to index a mostly empty array?

Is there a better way to index a mostly empty array?
by on (#175561)
Usually, when I load a level/room, I either...
1.load the room # to x or y, and index to an array of that item...1 byte per room, per array.

Or,
2.load the address of the start the room's data to a pointer, and indirect index (y) to the data.

But, I was going to...let's say, double the maximum possible enemies per room, but only use the extra enemies rarely... Like 10% or less. I feel like this is a huge waste of ROM space, to have an array of 90% zeroes.

What would be a better approach?

If each enemy uses 16 bytes of data (ROM), per 100 rooms, that could be 1500 wasted bytes of ROM space.
Re: Is there a better way to index a mostly empty array?
by on (#175563)
That'd be a dynamic setup, instead of a single array.

Code:
const struct enemy things[num_rooms][max_enemies];

->

static const struct enemy room0[3];
static const struct enemy room1[32];
const struct enemy * const things[num_rooms] = {
  room0,
  room1,
};
Re: Is there a better way to index a mostly empty array?
by on (#175578)
I think I'm just going to have a pointer to each room data set, and terminate each set with FF, so that it doesn't try to load extra enemies in each room.

Any other suggestions?

(Yes, I changed my avatar).
Re: Is there a better way to index a mostly empty array?
by on (#175586)
dougeff wrote:
pointer to each room data set, and terminate each set with FF

Super Mario Bros, Super Mario Bros 3 and Super Mario World do this.

Seems like the best method to me, really.