Sign in to follow this  
Trox

Tile-Based Games

Recommended Posts

Trox    134
I've been thinking about this for a bit now and would like to know what others think. What is the best data type to use for storing tile information? I was thinking a 2D array. Also, should a tile know its surrounding tiles or should each tile be independent of each other?

Share this post


Link to post
Share on other sites
Oberon_Command    6081
The best, IMHO, is an std::vector, for a couple of reasons:

1) It is dynamic, which means you get more flexibility, and you can resize your maps if needed.
2) It is just about as fast as a normal array.

I wouldn't use a 2D array, as it makes some things unnecessarily complex; when drawing the tiles, you need to use two loops instead of one.

I would make tiles independent of each other, if I were you, as it greatly simplifies things.

Share this post


Link to post
Share on other sites
speedie    140
a 2D array is my prefered, as yes I do like my tiles to know their emediate nieghbors. If your going to incorporate fringe you'll want to know what the tile next to it is.

Share this post


Link to post
Share on other sites
Trox    134
Hmm differing opinions...

I can see why tiles should be independant of each other. It causes alot of overhang trying to keep up with the quad-link list. And I would perfer to not have the overhang. However, how do you tell your characters what tile to move to if you do not keep the surrounding tile data?

Share this post


Link to post
Share on other sites
Anatol    178
You could also use a std::map<int, std::map< int, Tile> > for 2d, though of course it is somewhat complicated but I guess it would "feel" much more like an array.

edit: Doh xD

Share this post


Link to post
Share on other sites
jyk    2094
Quote:
Original post by Anatol
You could also use a std::map<int, std::map< int, Tile> > for 2d, though of course it is somewhat complicated but I guess it would "feel" much more like an array.
If it's just the [][] syntax you're after, that's probably not the best way to get it; access will be slower and the data may not be stored contiguously.

If you want that syntax, use boost::multi_array instead.

Share this post


Link to post
Share on other sites
Trox    134
I'm not after any syntax, just methodology. Infact, I couldn't even tell you what the syntax shown does exactly. I'm programming in C#, not C++.

Share this post


Link to post
Share on other sites
Mushu    1396
In any language, I think any multidimensional array is overkill for a rectangular NxM array, which can be simulated with your 1D array of choice (whether it be a raw array or a dynamic array) -

// the NxM dimensions of our array
int SOME_MAX_X_SIZE;
int SOME_MAX_Y_SIZE;

// an arbitrary array with enough room for our dimensions -
// can be a raw C-style array or a std::vector or C# equivalent.
ArrayValueType array[ SOME_MAX_X_SIZE * SOME_MAX_Y_SIZE ];

// a function to get an individual value from an the array
// using a 2D coordinate -
ArrayValueType getIndex( int x, int y ) {
return array[ x + y * SOME_MAX_X_SIZE ];
}

Using that little chunk, (x+y*MAX_X_SIZE) you can simulate an Nd array with a single dimensional array, given that its rectangular. If you want a non-rectangular array (ie, each row doesn't have the same number of elements), look into boost::multi_array as suggested, or the C# equivalent.

Share this post


Link to post
Share on other sites
pulpfist    528
If you want to use shortest path algorithms later on, you will need to keep the relations between the neighbours stored somewhere.
Personally I think I would prefer to have them in a separate array/vector so that I could choose how many nodes to use for the path finding separately from how many tiles there is.

Share this post


Link to post
Share on other sites
Mushu    1396
Quote:
Original post by pulpfist
If you want to use shortest path algorithms later on, you will need to keep the relations between the neighbours stored somewhere.
Personally I think I would prefer to have them in a separate array/vector so that I could choose how many nodes to use for the path finding separately from how many tiles there is.

Umm, why a completely separate vector? Traversal information is part of each tile - why not actually make it part of each tile:

struct CTile {
/* whatever render information */

/* relations to neighbors; 1 for each cardinal direction */
CPathfindingInformation _terrain[4];

/* maybe a reference/pointer to master array, but this should be a
POD-ish class and have all the operations done on it by the
controlling (CMap?) class. */
};

Share this post


Link to post
Share on other sites
egwenejs    125
Quote:

I can see why tiles should be independant of each other. It causes alot of overhang trying to keep up with the quad-link list. And I would perfer to not have the overhang. However, how do you tell your characters what tile to move to if you do not keep the surrounding tile data?


If you use a rectangular array and load your tiles into your map deterministically you can find neighboring tiles mathematically no matter what method you use. I personally like std::vector <std::vector <TILE>> for my maps

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this