The most recent feature I’ve added to the game is (are?) lit areas. They were a planned feature that got cut pretty early on, since it wasn’t necessary, and I didn’t want to waste time on it. But then I came up with an ability that required it, so I went ahead and put it in. Luckily, it didn’t end up taking very long to do.
Basically, everything that can go in a level (features, effects and even creatures themselves) can potentially “cast light.”
Any time anything that casts light is added to the map, it also gets added to a list of “light sources.” The map now also has a “lightmap,” which holds a true/false value for each tile that says whether it’s lit or not. Every turn, every tile on the lightmap is set to unlit. The game then loops through all the light sources and checks every tile within each light source’s “light distance” to see if it can draw a straight line to the tile without hitting any sight-blocking obstacles like walls (using Bresenham’s line algorithm), and if it can, it sets it as lit.
Now, in terms of determining what creatures can see, every creature has a set sight radius. Anything that’s within that range of them, they can see as long as there’s nothing blocking the way. Now that I’ve added the lights, all I had to do is check to see if the tile is in range OR it’s set as lit on the lightmap (it still also checks to see if anything is blocking the creature’s line of sight, of course).
Here’s an example. The player (currently possessing the zombie), can see a small radius around themselves. The torch on the left also illuminates a small area around itself, but some of the light is blocked on by the trees.
For the player, every turn I check every tile in the map to see if they can see it. Not very efficient, and if I did this for every single creature it’d probably be pretty slow. The good news is, creatures generally don’t care that much about the stuff in the levels. They just need to know about walls and floors, and (for the more intelligent ones), hazards. All this information is actually stored in a preset pathfinding map that all creatures have access to without actually having to have seen the tile (more info on how that works in a previous post). I guess we can assume that they live in the area so they know where everything is. The only thing they actually need to worry if they can “see” or not is other creatures.
So, all in all, it really was a lot less complicated to add than I thought it would be, which is really nice because usually when you add a new feature it turns out to be MORE complicated and take twice as long as you expected to code. And now I can have things that light up the levels like torches or candles, and you can see fire even if it’s far away, and even glowing creatures that you can see in the dark.