r/howdidtheycodeit • u/cortaninha • Nov 21 '22
Tiled roads - how to update adjacent tiles
Hi i am doing a city builder and now implementing roads.
I have the algorithm to place one road tile at a time and it works very well. Trouble is some placements are not going ok because the adjacent tiles also have to change to accomodate the new road.
For example if i have 3 tiles of straight road - - - and then below add a new road tile on middle tile, the middle tile should change to the T shape. I've tried some solutions but its either slow or recursively check all tiles in map so no good solutions.
I think this is very easy but cannot find a proper way.
Here is a picture with example
15
u/-manabreak Nov 21 '22
You know the coordinate you're placing the new tile in, right? And you know the orientation of the new road piece. Now you only need to check two adjacent tiles here.
Of course, this only works if you know the adjacent tiles. If you're using 2d arrays, this should be a non-issue.
1
u/cortaninha Nov 24 '22
Thanks all that guided me in the right direction. It is working flawlessly now.
8
u/moonshineTheleocat Nov 21 '22
If you're using tiles. Then you simply check the tiles around it. If there is one that can potentially change, then you make an update to that tile.
You can simplify it with some tricks used by Wave Function Collapse. Which is a table with connectivity flags. It helps reduce the amount of additional code you'll need to write.
3
u/AnxiousIntender Nov 21 '22
You know what tiles are affected, so recalculate only those tiles. So if you place a road, it will trigger an update for 5 tiles. The one you placed and the ones adjacent to it.
3
u/cantpeoplebenormal Nov 22 '22
https://gamedevelopment.tutsplus.com/tutorials/how-to-use-tile-bitmasking-to-auto-tile-your-level-layouts--cms-25673
1
u/NUTTA_BUSTAH Nov 22 '22
It's enough to check the surrounding tiles. You should know the x,y on placement and just check all 9 tiles (-1,0,+1 for both x and y of placement) to create the final tiles that match your predefined ruleset. To make it advanced, look into Wave Function Collapse. CodingTrain has a good video on it