GameDev 51: Chain LightningProcedural Lightning Texture, Chain Lightning Attack
by Christian - 19.05.2020
GameDev 51: Chain LightningAs I was playing a few old Warcraft III custom maps with some friends, I was reminded of one of the more iconic attacks in the game: a chain lightning attack. So obviously I wanted to add one to my game as well. But how to create an animated lightning texture? I might be able to create one in Photoshop or Blender, but it would take me a really long time and a lot of manual work to get a decent result. And then what if I wanted to change the color or another detail, it would take a lot of work again to change it. But luckily there is another solution for me: write a small C++ program that procedurally generates an almost infinite amount of different lightning textures. Sure, it might take a bit longer to create the program than it would take to create a texture in photoshop, but once it is finished I can create new textures in a matter of minutes of even seconds.
Procedural Lightning Textures
Starting the project the first thing I worked on was to draw a simple straight line in a lightning style. After trying a few different versions I ended up with three lines of varying size, opacity and hardness on top of each other. First there is a soft and almost transparent blue shine, then comes a smaller and more opaque cyan light and the center is completely white, because even colored light sources appear white when they are very bright and only the shine indicates their color - see lightsabers. Then I added an option to randomly modify the width of the line to make it a bit more interesting than an even thickness everywhere. But a single line doesn't really look like lightning, so it was time to not only add kinks to the main line, but also work on spawning child lightnings. Children have a small chance to spawn anywhere and a higher chance to spawn at the kinks in the main lightning. Also children can have children themselves, which ends up in a tree data structure with the depth of the child trees being a controllable option. The last major feature I implemented were micro lightnings. Micro lightnings are very similar to normal children, however they are generally smaller and spawned a lot more frequently all over the main lightning and its children. And they are what basically saved this project, they added a lot of interest, giving the bigger lightning lines rough and fuzzy edges and were able to produce a lot of different and cool looking results.
Working on procedural generation algorithms there is always a lot of trial and error. While you may have an idea of what could look interesting and give you the effect you are going for, there is no way of knowing unless you actually implement it and try it out. Therefore the code is going to be very messy with a lot of hardcoded numbers, very few comments and leftover tries that didn't work out, but you didn't want to delete in case you want to try something similar in the future. So once I was getting close to my desired results, I started to create a seperate settings struct, where I moved all the different options and numbers to that control the procedural generation. With the settings struct it is possible to treat the actual generation class as a black box and try a lot of different options very easily and quickly.
My main focus was to create a lightning beam texture, because that's what I needed, but it's very easy to change the algorithm to also create more classical lightning strikes.
A selection of different textures produced by the algorithm:
The settings struct, these are the most important settings to control the algorithm:
Chain Lightning Attack
With an easy way to generate a lightning beam texture it was time to add a chain lightning attack to my tower defense game. To facilitate the chain lightning attack and probably other similar attacks in the future I created a new line projectile attack type. Line projectiles connect to an enemy, deal damage instantly and remain for a short period of time so their animation can play. However, as the name suggests, chain lightning has to be able to chain and hit multiple enemies. Therefore I added two new stats to the tower blueprints: bounces and bounce range, which define how often and how far an attack can bounce - or chain in this case. They only apply to the line projectiles at the moment, but I am going to add them to normal projectiles too. Anyway, after trying a few different generated lightning textures and adding a simple sound effect, this is what the final chain lightning attack looks like:
- finished the stork model
- more game name searching
- I tried a few layouts for the main menu
- made a pumpkin and jack o'lantern model, which are probably going to be towers, e.g. the jack o'lantern is an upgrade for the pumpkin
- changed the range calculation from 3D to 2D, now only the x and z coordinates are considered, it's only a minor change from 3D, but there would be small inconsistencies of a towers range between flying and non flying waves when using 3D
- Offtopic: added referrers to Magpie
- Offtopic: a lot of research into access control headers for Magpie, they made it a little bit more secure, but it's still not perfect, but at the moment I don't think it can be completely tamper proof
- added an option to my sound library to slightly alter the pitch of a sound effect every time it is played
by Christian - 19.05.2020 - 23:41