Using a huge 3D array

This is a discussion on Using a huge 3D array within the C++ Programming forums, part of the General Programming Boards category; I wasn't sure whether to post this in the game programming forum or this one, but because it's more of ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    16

    Question 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. #2
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176

    3D Array

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

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

    fletch
    "Logic is the art of going wrong with confidence."
    Morris Kline

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > 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. #4
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    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
    "Logic is the art of going wrong with confidence."
    Morris Kline

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3D array problem
    By rainman39393 in forum C++ Programming
    Replies: 1
    Last Post: 03-15-2008, 06:09 PM
  2. Dynamically allocating 3D array
    By ssharish2005 in forum C Programming
    Replies: 8
    Last Post: 02-26-2008, 03:07 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Huge array
    By Shadow12345 in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2002, 03:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21