Fog of War is an essential part of many strategy games;It's the black or darkened parts of the map that the player has either not explored or currently possess vision over.
It makes sense in concept and it's fairly simple in implementation (although often resource-heavy and in need of optimizations). It decides whether units and buildings at a location can be seen or not depending on the vision of your own counterparts.
Discovered buildings will always be seen after discovering them, because.. well, buildings don't move. Whereas units, which almost constantly are ordered around the map are only visible if the player possess vision over them at that moment.
It does not forget
However, that's not all there is to it! Fog of War also remembers things that once was, irrelevant of their status in the now, thus it can deceive. Previously discovered and initial map objects (buildings, resources, etc) will not disappear from your view even though they are destroyed and removed the game.
If this small detail is forgotten, buildings that were deleted and trees that were cut down in distant lands where the player had no vision would be seen disappearing. Opss...
Fake it till they make it (there)
So, whenever any such object is destroyed, we can simply create a "fake" object on all clients which currently have no vision over it at the exact same position and rotation to represent the memory of it. However, initially.. if a player would be looking at a forest being cut down he might have noticed trees flickering, moving behind and in front of each other in terms of which is drawn above and under the other. This is due to the layer depth, which decides in what order sprites are dawn and also has to be remembered and displayed correctly. For this I had to make some adjustments in the engine because of how objects are assigned their depth, but was easily done.
Then the final step will be to remove this fake object once the player gets vision over it, perhaps with a nice fading effect. Sweet!
Maphacking, are we?
Finally, it's done! Or.. so we thought!
Remember how, when you are placing a building in strategy games you cannot place it on any other object, regardless of current visibility yeah?
You cannot place a building on a object that you can currently see (regardless if it actually exists or is since long gone), because that'd be strange.
Players should also be able to place buildings on other objects that they currently cannot see, and let the placement cancel out when the players vision finds the blocking object.
The player has placed a building close to the resource, only to find out someone else already had done so, cancelling the building.
A game that did not take this into consideration was Empire Earth, this resulted in players being able to scout terrain from anywhere by simply moving the mouse over the map while placing the (preferable) largest buildings in the game, the farm which was the equivalent of nine barracks in size (!). Instantly given the feedback of red (for occupied) or green (for available) tile borders players could easily see what tiles were occupied and which were not. Quite the hack, huh..
So if you thought it'd be enough to let each player have their own vision, you were wrong, they also have to have their own version of what tiles are walkable(empty) and not.
This was one example of many where, when implementing something seemingly simple you might forget parts that are subtle or obvious and finally end up with a lot more than you originally planned for.
Until next time!