Thread: Setting Up a sector based grid system

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    7

    Setting Up a sector based grid system

    Hey everyone.

    I am trying to set up a grid-based world system in a text-based game (MUD). The whole world would consist out of several locations, each location stored in it's own file.

    What I want to have is that every location consist out of sector's using a 2 dimensional array, but the whole world consist of the collection of locations. Certain sector's in a location may be of different type and/or not accessable by players. I was able to create a single location so far without any problems, but as soon as I try to make a collection of locations, although it compiled fine, the game crashed when loading.

    What I used was:
    Code:
    typedef struct	sector_data		SECTOR_DATA;
    
    #define MAX_X_GRID		100
    #define MAX_Y_GRID		100
    
    struct sector_data {
      char	*description;
      int		 sector_type;
    } sector[MAX_X_GRID - 1][MAX_X_GRID - 1];
    This worked fine when I used sector[x][y].sector_type, etc. but that would only allow me to create one location. So after that I decided to use a different way:
    Code:
    typedef struct	sector_data		SECTOR_DATA;
    typedef struct	world_data		WORLD_DATA;
    
    #define MAX_X_GRID		100
    #define MAX_Y_GRID		100
    #define MAX_LOCATIONS		 10
    
    struct sector_data {
      int		 sector_type;
    };
    
    struct world_data {
      char		*description;	/* rather than having each sector use it */
      SECTOR_DATA	*sector[MAX_X_GRID - 1][MAX_Y_GRID - 1];
    } world[MAX_LOCATIONS - 1];
    Using this method compiled fine but everytime the game loaded the world it crashed. Is normally used world[x].description or world[i].sector[x][y].sector_type.

    I am currently unsure what I have done wrong, yet even if there would be a better way. Any suggestions or help would be much appreciated.

    Thank you.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well for starters, your size is wrong. Given an array of SIZE, you have accessable elements of 0 to SIZE -1. Thus:
    Code:
    SECTOR_DATA sector[ MAX_X_GRID ] ...
    Gives you accessable elements of zero through MAX_X_GRID - 1. So when you do this:
    Code:
    SECTOR_DATA sector[ MAX_X_GRID -1 ] ...
    You actually get accessable elements of zero through MAX_X_GRID -2. I doubt that's what you want.

    The same goes for your world size array.

    Anyway, there's not really enough information here to figure out what it is you're actually trying to do. You've got N world pieces, that you're trying to load. Each of these pieces go in the list of world parts. That I've got. What does one of these pieces contain? Is it just a series of sector types, like so?
    Code:
    wwwwww^^^^tttttt^^^^wwwww
    wwwww^^^^^ttttttt^^^^^www
    wwwww^^^^tttttt^^^^^^^www
    wwwww^tttttttt^^^^^^wwwww
    wwwww^^ttttt^^^^^^wwwwwww
    wwwww^^^ttttt^^^^^^wwwwww
    wwwwwww^^tttttt^^^^^^wwww
    Or what?

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    7
    Sorry if I explained poorly.

    Ok within the mud there is a "virtual world", this world exist out of locations where each location is devided into sectors, such as [0][0] or [9][9] (x and y coordinations).

    Now what I am trying to accomplish is have a file called world.lst, in this file are a list of all the locations which is within the world. Now for each location within world.lst there will be a file <location>.loc (location being the name of the location) which will have all the sectors, information etc. for the location.

    Now When loading up the world I would like to have each location stored on a seperate grid, so:
    Code:
    world.lst
     |- location1.loc
         |- grid layout
     |- location2.loc
         |- grid layout
     |- location3.loc
         |- grid layout
    In actual if I want to draw the grid for location1 I would have something similar to:
    Code:
    int  loc = 1;
    static char grid[MAX_BUFFER];
    
    grid[0] = '\0';
    
    for ( xLoop = 0; xLoop < MAX_GRID_X; xLoop++ )
      for ( yLoop = 0; yLoop < MAX_GRID_Y; yLoop++ )
        strcat( grid, get_sector_type( world[loc].sector[xLoop][yLoop].sector_type ) );
    Where get_sector_type would be an external function called to return the appropriate ascii character for the type of sector.

    So my problem is to get the structure layouts right (world[a].sector[b][c].sector_type). Hope this is a better explanation.
    Code:
      ..........    ..........    ..........
      ..........    ..........    ..........
      ..........    ..........    ..........
      ..........    ..........    ..........
      ..........    ..........    ..........
    Looking at the above is three locations where each '.' is a sector on a x,y grid and all three locations would make up the world.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are your locations all a fixed size, or a variable size? that is to say:
    Code:
    ....................
    ....................
    ....................
    ....................
    ....................
    ....................
    ....................
    ....................
    ....................
    ....................
    Where you have three areas which all fit together some how. Are gaps allowed? Is every area the same size, with the same number of sectors? There still isn't really enough information.


    Well let's assume an arbitrary of world pieces, with an arbitrary number of sectors, of arbitrary sizes.
    Code:
    struct sector
    {
        ...sector info...
    };
    
    struct location
    {
        struct sector **grid;
        size_t xsize, ysize;
        ...whatever...
    };
    Now you do something like so:
    Code:
    struct location *loadlocation( FILE *fp )
    {
        ...
    
        newloc = malloc( sizeof *newloc );
    
        ...
    
        newloc->xsize = read xsize from file
        newloc->ysize = read ysize from file
    
        ...
    
        newloc->grid = malloc( sizeof( struct sector * ) * newloc->ysize );
        for( y = 0; y < ysize; y++ )
            newloc->grid[ y ] = malloc( sizeof( struct sector ) * newloc->xsize );
    
        for( y = 0; y < ysize; y++ )
            for( x = 0; x < xsize; x++ )
                newloc->grid[ y ][ x ] = read sector info from file
    
        ...
    
        return newloc;
    }
    Be sure you free everything correctly when you're done with it.

    Or, if everything is a fixed size, get rid of the pointers and just use a 2D array in its place.

    Quzah.


    }
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    7
    Code:
       %%..........@@
      %%............@@
     %%%.............@@ 
    %%%....~~~~~.....@@@  This is a 20x10 sector
    %%%...~~~~~~~....@@@    ~ indicates water
    %%%....~~~~~.....@@@    % indicates trees
    %%%..............@@@    @ indicates rocks
     %%%....*=*.....@@@     *, = indicates a house ( below )
      %%....***.....@@
       %%..........@@
    Looking at the above although it is a 20x10 grid 0,0 & 0,1 & 0,2 etc. isn't used so it is displayed as a blank spaced ' '. Where as depending on the sector type it would display the character appointed in the appropriate colour.


    Code:
    /---==---\
    |........|   This is a 10x5 sector
    |........|   -, |, / & \ indicates walls
    |........|   = indicates a door
    \--------/
    Looking at the above it is a 10x5 grid where all the sectors are used.

    So basically some of the locations would differ in size and certain sectors isn't used. Although those sectors isn't used it is still part of the location.

    So taking the above information. I will need to load each location in a 2 dimensional array.
    Code:
    typedef struct sector_data {
      ...
      information
      ...
    } sector[X][Y];
    Where X and Y would be define as the maximum any locations can go. (No locations would go above 100x100).

    Now what I would like to do is load up a collection of locations into a new variable (let's call it world). So in the end i would use world[loc].sector[x][y].type, etc. (loc would be the current location in, x and y pointing to the current sector).

    All the locations would be loaded into memory seeing that it would be pointless to load/unload the location whenever someone enters, due to the fact that it is a online multiplayer game, and also if it were to load/unload then all the objects/mobs would reset the whole time which I don't want.

    Thank you.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Since your sector sizes are not all the same, using an array is a waste of space. If however you want to use an array, you'll have to hard code the maximum size for X and Y, and then just load into that. However, the method I've shown will do what you want.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    7
    Thx quzah - i will look into it.

    Do you know of any tutorials or articles which could help me with this sort of thing.

    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using mscorlib & system namespace in an MFC?
    By Robert_Sitter in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2005, 06:47 PM
  2. Opinions on custom system build
    By lightatdawn in forum Tech Board
    Replies: 2
    Last Post: 10-18-2005, 04:15 AM
  3. system();
    By GanglyLamb in forum C Programming
    Replies: 5
    Last Post: 10-30-2002, 03:57 AM
  4. Your favourite fantasy game setting?
    By fry in forum Game Programming
    Replies: 4
    Last Post: 10-16-2002, 06:26 AM
  5. Setting the System Time
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-01-2001, 11:18 AM