Algorithm to configure tile transitions

This topic is 3636 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Howdy, I recently started working on a game to pass my free time. I've never created games before so it sounded like a nice project. I figured a basic RTS game would be a good start, so searched for some free tiles to use. Having random terrain generation in mind, is started creating an engine that would handle the management of the map and drawing the tiles. My problem is that i can't seem to think of a good algorithm to configure the surrounding tiles of a solid terrain: Lets say the whole terrain is stone, and i have a terrain resource 'Stone->Grass'. In this resource i have 9+4 tiles. For instance, imagine the following tiles: 1 2 3 4 5 6 7 8 9 11 12 14 16 1) top left stone->grass tile 2) top stone->grass tile 3) top right stone-> grass tile 4) left stone->grass tile 5) solid stone->grass tile 6) right stone->grass tile etc. The last 4 tiles are the inverted corners. Imagine i place a water tile in a fully stone map, then i'd need to loop around all 8 neighbours to set the correct corner tile. The problem is that i can't seem to create a elegant algorithm that does this. My current solution works about 95% of all situation and consists of having each neighbour scan its neighbours when a tile changes. Using that scan, i can invert the location of the neighbour and translate to the tile location. (So basically when tile 2,2 is set to 'water', tile 1,1 scans its neighbours and sees 2,2 (bottomright) as a water tile, inverting the scan result (bottomright->topleft) which results in the correct tile being drawn) But it gets very complex when i place more water tiles on the map that connect each other. Is this a common problem? Any thoughts on a better way to handle this?

Share on other sites
I'm not sure that's enough tiles. How do you handle a case like this?

+-----+-----+|Stone|Grass|+-----+-----+|Grass|Stone|+-----+-----+

One way to do this is to assign types to the corners of tiles instead of the tiles themselves. If you have two types, then you need 16 tiles to cover all transitions (without rotation), including the 2 single type tiles. Then when you generate your map, you just set types at the corners of tiles and selecting the proper tile becomes trivial (if you encode the corner types as a bit field and select your tile ids carefully, the corners themselves encode the tile).

This approach tends to lead to having the terrain boundaries in the middle of tiles. If you want to keep the terrain boundaries near the edges of tiles you can give each tile a main type, with each corner holding a secondary type. This requires 16 tiles for each type in a 2 type transition for a total of 32 tiles.

In this case when you change the main type of a tile, you change the corner types of adjacent tiles. Selecting a tile is still trivial. You can use the corner types as a bitfield to encode which tile in a set to use, and the main type to select the tileset.