944,066 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5865
  • C RSS
May 17th, 2005
0

Creating a Coordiantion (Grid Based) System for a text-based game.

Expand Post »
Hey everyone.

I am not sure of any of you are familiar with the term MUD (which is Multi User Dungeon, a text-based game). If not feel free to go over to www.mudconnect.com to check it out.

Anyway, back to my post. What I am currently planning on doing is changing the way the world works from a room-based world to a location based world, which uses x, y and z coordinations.

The current room-based world would load up each room into a certain number (called vnum) and within each room there the posibility of having 6 exits, which takes the player to another room with the number specified on the exit. In practice this works great. Also a collection of rooms is called an area.

Now my idea is to remove the whole concept of an area existing out of rooms, so instead of a town being an area with a x amount of rooms it is a location, consisting of a x amount of spaces and y amount of spaces (x,y grid system). The z axis would be used for height as to how high a certain place on the location is or how high a player is above the ground (seeing that certain players can fly).

So basically I have taken up thought of this alot more recently and was wondering what would be the best way to go about.

What I thought of was using a file (let's call it locations) and within that file it has names (or numbers) of the locations which needs to be loaded. Each location which is loaded would be loaded from another file (x.loc). Now in each file for each location there would have to be a x, y and z to know the size of the grid, seeing that the inside of a house would be a location just as a whole town would be a location, and they both do differ in size. Also each location would have a main description, but each x, y and z position should have sub-description, seeing that the location could be "The town of Belgaard" and if you are in the northern parts of the town infornt of a store it would be "The bakery".

So basically I would need a structure to load the list of all the locations, and also a structure to load each of the locations specified in the locations file, as well as keep in mind that certain positions in a location may have obsticles etc.

Now what would be the best way to go about, also would the better way be to load a location into the players structure as the player enters a location, or load all the locations into memory. I know loading it into memory once of consumes more memory, but loading it everytime needed would cause alot of lag seeing that your average mud has about 10-25 players on at the same time.

Any advice on this would be appreciated.

Thank you.


P.S If this is very confusing but you do feel that you would like to help I will be more than happy to explain things in much more detail, as well as show the code of how it currently works, etc.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Unverified User
Auron is offline Offline
11 posts
since May 2005
May 17th, 2005
0

Re: Creating a Coordiantion (Grid Based) System for a text-based game.

You're basically just describing the "area of rooms" concept with different terminology. Both can be easily written using multidimensional arrays, or sparse matrices. In the file you could have the x/y/x coordinates that relate to the array indexes followed by location information:
  1. 0 0 0 Information about the location
  2. 0 0 1 Information about the location
  3. 0 0 2 Information about the location
  4. 0 0 3 Nothing there
Then in your program you can have an array of structures that represent location information:
  1. struct Location {
  2. /* Location information */
  3. } loc[Z][X][Y];
And in your program, you can load the file into the array easily assuming you have a way to parse the location information into a Location object:
  1. int inrange(int x, int low, int high)
  2. {
  3. return x >= low && x < high;
  4. }
  5.  
  6. void LoadFile(struct Location loc[Z][X][Y], FILE *fp)
  7. {
  8. int x, y, z;
  9.  
  10. while (fscanf("%d%d%d", &z, &x, &y) == 3) {
  11. assert(inrange(z, 0, Z) && inrange(x, 0, X) && inrange(y, 0, Y));
  12. LoadLocation(loc[z][x][y], fp);
  13. }
  14.  
  15. if (!feof(fp)) {
  16. fprintf(stderr, "An error occurred while reading the file\n");
  17. cleanup(); /* Do any application-specific cleanup */
  18. exit(EXIT_FAILURE);
  19. }
  20. }
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
May 17th, 2005
0

Re: Creating a Coordiantion (Grid Based) System for a text-based game.

Ok so far I've got it planned out as follow:
  1. #define MAX_GRID_X 100 // 100 x 100 grid size per locations would
  2. #define MAX_GRID_Y 100 // be more than enough, actually overdoing it
  3.  
  4. typedef struct location LOCATION;
  5. struct location
  6. {
  7. char *description; // the description of the current sector
  8.  
  9. int sector_numb; // unique number for the sector
  10. int sector_type; // the type of sector it is
  11.  
  12. int object; // point to an object, 0 means no object
  13. int mobile; // point to a npc, 0 means no npc
  14.  
  15. int is_exit; // point to another location, 0 means no exit
  16. int z_coord; // how high can something go on that point
  17. };
Ok the above structure would hold the data for a single location from 0 to 99 on both the x and y axis. I don't need a z-axis here seeing that each location will be flat, z is used to see how high something can go up, used for when inside a building, etc.

Ok so now I would like to load all the locations into a structure. So I presume to do that I would create a new structre which loads every location into the above structure.

  1. typedef struct world WORLD;
  2. struct world
  3. {
  4. LOCATION *grid[MAX_GRID_X - 1][MAX_GRID_Y - 1];
  5. char *description;
  6.  
  7. int location_number;
  8. };
  9.  
  10. extern struct world *world_list;
I think I should get the loading correct this way, but I am not sure if it is the right way to go.

P.S Forgot to mention that it is written in C and compiled under linux.
Reputation Points: 10
Solved Threads: 1
Unverified User
Auron is offline Offline
11 posts
since May 2005
May 17th, 2005
0

Re: Creating a Coordiantion (Grid Based) System for a text-based game.

If you want to have multiple "worlds" then yea, that's probably the best way to go about it.

>>LOCATION *grid[MAX_GRID_X - 1][MAX_GRID_Y - 1];
Why are you taking 1 away from each of the sizes? It would be safer in the long run to just change MAX_GRID_X and MAX_GRID_Y to 99 rather than figure out what kind of integer arithmetic to do to avoid out of bounds accesses.
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005
May 17th, 2005
0

Re: Creating a Coordiantion (Grid Based) System for a text-based game.

May I suggest linked list, or multi-branched trees, instead of matrices. I used to write some of these, a long long time ago -- like on Apple ][...


There are two ways you can create "worlds". One is as described, in matrices, and the other is links.

In the case of links, there is no real direction, or coordinate system. Points of interests are linked together by "parent-child" links, and can have many to many relationships.

In a matrix environment, you can only do, presuming three axis and that you can move in an angle, 26 directions. (Stacked cubes with 3/3/3 sides.)

The problem here is that you must define all of the matrix, and reserve memory. This becomes very memory and storage intensive as you start adding details to the various locations. With links, or pointers, you can preset the number of links to be say 256 possible exit points, yet not use the memory, only if the actual exit exists.

With linked lists, and still presenting it as an XYZ to the user, you can define the first, say 128 links to descriptions, so you would have a record for the ceiling - '>look ceiling' - 'the ceiling has cobwebs hanging, with 2" spiders staring back at you hungrily'... or if the link wasn't defined use a default answer like 'you see nothing interesting as you stare at the ceiling'.

So the links can lead to new rooms, or path, or can be containers for other information, like descriptions, or an object, or a rule that might trigger an other event.

A perfect example of this is a spreadsheet. Most people think spreadsheets store data in a two dimensional array, but in reality it is just a linked list.

You can also find some very fine example of the implementation of such systems by searching for BBS door games like Trade Wars, or Star Trek.

There was such a link list network where the link took the user to other BBS systems. I have been thinking of such network to write for the web, yet never got the time or chance to deploy it. In the old times, users had to disconnect, and the the "door" would dial the "new" world where the link pointed to.

Ahhh.. The good old times..
Reputation Points: 18
Solved Threads: 0
Junior Poster
Libertate is offline Offline
120 posts
since Jan 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Can't find the error
Next Thread in C Forum Timeline: Classes and header files





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC