# Thread: Using a huge 3D array

1. ## Using a huge 3D array

I wasn't sure whether to post this in the game programming forum or this one, but because it's more of a technical thing, I'm posting it here.

Here's my problem. I'm making a text-based RPG, and I'm using a three-dimensional array to store information about the player's surroundings. I made it so that each slot in the array is given one of three numbers, corresponding to what's in that spot in my RPG:

-1 = impassable
0 = passable, no one is there
1+ = passable, number tells how many people are there

This method worked until I tried making the array larger after finishing the general mechanics so I could start work on the actual game. This is where the problem comes in: I want to make this quite a sizable game, and to do that will require me to make a huge landscape. An arbitrary magnitude I picked for the size was 1000 x 1000 x 30, and I've narrowed it down to that creating an array that size crashes my program, even when I'm using the smallest variable type. I doubt my RPG's landscape could have smaller dimensions than that.

So, is there any way around that that would still enable me to use that method of storing the terrain info, or am I screwed and will have to opt for other methods of figuring out whether a person can move somewhere and whether talking is an option? I know there would be other ways, but they're much more lengthy than this.

2. ## 3D Array

Have you tried placing the array on the heap instead of the stack?

int * Map = new int [x][y][z];

fletch

3. > 1000 x 1000 x 30
30Mbytes of storage (at least).

But that's just the tip of the iceberg, you've got to initialise all those locations with meaningful data, unless you have a lot of

N
There is ocean here
N
There is ocean here
N
There is ocean here
N
There is ocean here
N
There is ocean here

Not only that, no user will ever visit every location in your RPG, it would simply take too long (1 room per second for almost a year, non-stop)

Your RPG will have at most a few hundred locations (that are interesting in some way). I suggest you store the connectivity of the locations within the data, then you only need to store the locations which interest you

Perhaps something like this
Code:
```enum { NONE, KITCHEN, HALL, STAIRS };
enum { NORTH, SOUTH, WEST, EAST };

struct room {
char    *name;
int      next[4];   // N,S,W,E from this location
} rooms[] = {
{ "None" },
{ "Kitchen",    { NONE,    HALL,   NONE, NONE } },
{ "Hall",       { KITCHEN, STAIRS, NONE, NONE } },
{ "Stairs",     { HALL,    NONE,   NONE, NONE } },
};```
If you have
int room_num = KITCHEN;

Moving south is just
room_num = rooms[room_num].next[SOUTH];

4. Good point, that would end up being a lot of memory. On the other hand, I can see Gabu wanting his player to wander across the ocean killing giant squid or something (there is ocean here, there is ocean here, there is ocean here...). Plus, he wants a large game and that's going to take memory.

I don't know much about using files with C++, but how about saving your map to a file and then loading portions of the map as required? This would allow you to have a large map (at the expense of el-cheapo disk space) without consuming large chunks of memory. And if you saved it in a text file, you and your players could edit the map with a text editor instead of coding changes and recompiling the game. Or you could always write your own map editor...

Regardless, you'd want to optimize the map in order to save memory or speed disk access.

fletch