It’s been over three years since I started working on Tribe Of Pok. Over that time I’ve grown as a developer and found better ways of doing things. One example is water flow mechanics. It’s one of the first things I worked on and has undergone several iterations.

When I was a newbie programmer with barely any experience, I was creating a new object for every unit of rain that fell and then destroying it when it evaporated or soaked into the ground. If you have any experience as a programmer, you’ll know how disastrously bad of an idea it is to create/destroy hundreds of objects every second. That implementation was quickly scrapped when my frame rate dropped to 10 FPS every time it rained.

20150316Riverfill

Next, I tried storing the amount of water in each tile and performing water calculations only on the tiles. Performance was much better. All I had to do was +1 for every unit of rain that fell and -1 for every evaporation/saturation event. Simply calculate the height of each tile to determine if water can flow into it from an adjacent tile.

However when it rained, the ground would become blanketed in a layer of water. Hills would be just as saturated as valleys and there was nowhere for Pokians to hide. As a side effect, dry ponds would never completely refill since much of the water was stuck on higher ground. I fixed this by introducing movement to saturation. In effect, the water in the soil was “flowing” downhill just as the water above ground did.

Now water was flowing somewhat realistically and wasn’t hampering gameplay.

20150330 WaterDownhill

A year or so later I added rivers. Rivers form long, narrow paths that move in one direction. However with the simulation as it was, water moved in a random direction with each update. Sometimes water would flow into an adjacent hex. Then on the next update it would randomly decide to flow back into its starting hex. The result was water taking too long to go downriver. For particularly long rivers, the water evaporated before it even reached the other end of the map!

To fix this, I’m cheating by teleporting water from the high points to the low points along the river. Eventually I’ll replace teleporting water with a better method. In the meantime, I’m iterating and improving other aspects of the game.

20150319 Dam river