![]() |
| | #1 |
| Registered User Join Date: May 2005
Posts: 7
| Setting Up a sector based grid system 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];
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];
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. |
| Auron is offline | |
| | #2 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| 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 ] ... Code: SECTOR_DATA sector[ MAX_X_GRID -1 ] ... 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 Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #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
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 ) );
So my problem is to get the structure layouts right (world[a].sector[b][c].sector_type). Hope this is a better explanation. Code: .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... .......... |
| Auron is offline | |
| | #4 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| Are your locations all a fixed size, or a variable size? that is to say: Code: .................... .................... .................... .................... .................... .................... .................... .................... .................... .................... 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...
};
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;
}
Or, if everything is a fixed size, get rid of the pointers and just use a 2D array in its place. Quzah. }
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #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 ) %%....***.....@@ %%..........@@ Code: /---==---\ |........| This is a 10x5 sector |........| -, |, / & \ indicates walls |........| = indicates a door \--------/ 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];
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. |
| Auron is offline | |
| | #6 |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,262
| 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.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? |
| quzah is offline | |
| | #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. |
| Auron is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Using mscorlib & system namespace in an MFC? | Robert_Sitter | C++ Programming | 3 | 11-13-2005 06:47 PM |
| Opinions on custom system build | lightatdawn | Tech Board | 2 | 10-18-2005 04:15 AM |
| system(); | GanglyLamb | C Programming | 5 | 10-30-2002 03:57 AM |
| Your favourite fantasy game setting? | fry | Game Programming | 4 | 10-16-2002 06:26 AM |
| Setting the System Time | Unregistered | C Programming | 1 | 11-01-2001 11:18 AM |