TPW FIREFLIES - glowing animated ambient fireflies
 
Author: tpw
Thanks: Valken, lordprimate
Requires: Community Base Addons for Arma 3
Optional: TPW FOG, TPW SOAP
Compatibility: SP, MP client (maybe)
 
Description
-----------
Fireflies (glowworms / lightning bugs if you prefer) are a widely appreciated feature during summer evenings in many parts of the world (but not mine unfortunately). Interestingly, Arma3 actually does spawn ambient fireflies at night, but they're not only unlit, they don't even have a visible model! To remedy this situation TPW FIREFLIES spawns animated insects with lightpoints around the player which simulate the appearance and behaviour of swarming fireflies, greatly adding to the visual ambience of playing on maps after sunset. 
 
Features
--------
By default TPW FIREFLIES spawns fireflies for four hours after sunset, if it's not too windy or raining, and the temperature is greater than 18degC (if using TPW FOG). Fireflies will only spawn if there is sufficient vegetation and no nearby houses, and they won't spawn on roads or water. Fireflies will gradually spawn in, usually in small groups of 1 - 5 but occasionally in larger swarms of up to 25, up to a maximum of 50 fireflies within a 25 m radius of the player. The default firefly is green/yellow, briefly blinks every second or so, and is bright enough to gently illuminate its surroundings within 0.5 m. Fireflies will stop glowing if it starts to rain, becomes too windy, or if there is nearby gunfire (if using TPW SOAP).
 
If you don't like these sensible defaults, TPW FIREFLIES is highly configurable: you can change the colour, size, speed and brightness (which depends on the map and its night lighting config) of the fireflies; whether they blink or glow steadily; their number, density and swarm size; whether they spawn everywhere or only in dense forest; the temperature threshold for spawning; the time window after sunset; and the distance from the player they will spawn/despawn. You can globally enable/disable TPW FIREFLIES using tpw_firefly_active at any time.  
 
// USER VARIABLES
tpw_firefly_active = true; // Fireflies active. false = inactive
 
//Environment
tpw_firefly_window = 4; // How many hours after sunset will fireflies appear
tpw_firefly_temp = 18; // Minimum temperature for fireflies to be active, if running TPW FOG
tpw_firefly_trees = 5 ; // Number of trees needed within tpw_firefly_radius of player in order to spawn fireflies. 0 = fireflies everywhere 
 
//Spawning 
tpw_firefly_radius = 25; // Firefies will be spawned within this distance (m) of player
tpw_firefly_time = 5; // Max seconds between spawning each firefly/swarm
tpw_firefly_maxflies = 50; // Maximum number of fireflies around player
tpw_firefly_swarmsize = 5; // Maximum number of fireflies per swarm (must be less than tpw_firefly_maxflies)
tpw_firefly_largeswarm = 0.1; // Chance of a large swarm (5 X tpw_firefly_swarmsize) spawning near player. 0 = never, 1 = always
 
// Firefly attributes
tpw_firefly_colour = [250,220,100]; // Firefly glow colour
tpw_firefly_brightness = 0.4; // Brightness of each firefly (0 - 1)
tpw_firefly_size = 0.05; //  Apparent size of each firefly (0.01 - 0.2)
tpw_firefly_blink = 1; // Flashing enabled. 0 = no flashing, 1 = brief flash with ~1 sec between flashes
tpw_firefly_speed = 0.075; // Max time (sec) between firefly animation "frames". Larger = slower moving fireflies  
 
All of these variables may be changed on the fly in the debug shell or a trigger. For instance if you'd like red fireflies, enter tpw_firefly_colour = [250,150,50] in the debug shell and the effect will be immediate. Or if you'd like a specific area of a map to have a higher density of fireflies you can place a trigger with tpw_firefly_maxflies = 200* in its On Activation field.
 
How it works
------------
In TPW FIREFLIES each firefly is represented by a lightpoint and a mosquito model (basically just the 2D texture). Animating a lightpoint in a CPU friendly way proved challenging, the eventual solution was to record 5 seconds of flight of an ambient mosquito into an array of 3D relative co-ordinates, and for each firefly to simply step through the array, setting the position of the lightpoint/model to each co-ordinate. By varying the step speed and scaling the co-ordinates it's possible to achieve randomly flying fireflies that orbit a given point with constantly changing speed and radius in a very natural way. Because all flies use the same animation co-ordinates they occasionally sync up and give every appearance of interacting in the swarm. The process uses simple fast vector functions and consumes very little CPU, with dozens or even hundreds of fireflies present.  
 
Issues
------
Sigh, yet another bloody mod from TPW that does nothing to improve the core combat experience of Arma 3....

TPW FIREFLIES came about from a request from Valken to enliven the nightime in jungle maps such as Tanoa. My initial attempts attaching/moving lightpoints to existing ambient insects worked OK but after a bit of feedback and interweb research I realised that a more customisable solution was required to achieve a more believable firefly experience. It's been an enjoyable challenge to come up with the current implementation of TPW FIREFLIES, as well as learning more about these interesting creatures. And I've gotten to spend many hours walking around beautiful A3 maps at night looking at fireflies which are just as mesmerising in-game as they presumably are in real life. 

I should stress that it's not within the scope of this mod to fully simulate the biology and complex behaviour of a diverse group of insects. However you should be able to customise the experience to suit your own expectations of plausibility. If you want giant bright blue fireflies blanketing the area for 100 m around the player on your sci-fi map, then knock yourself out. It's up to you to decide whether  there should be fireflies on a given map. For the record fireflies are pretty much globally distributed, though not in every region. There are fireflies in Greece and most of the Eurasian landmass where it's temperate and moist enough, so they could believably be seen on Altis/Stratis and many Arma2 / CUP European and Middle Eastern / Central Asian maps. There are no fireflies on Fiji, but they are common in Southeast Asia so are not totally out of place in a Tanoan jungle setting - there are no Australian kookaburras on Fiji either, but they've somehow made it onto Tanoa too!.
 
Although TPW FIREFLIES uses very little CPU* to do its thing, the fireflies are true dynamic lights and having lots of them in an area can cause the engine to flip out a bit. You might occasionally notice flickering house or street lights when numerous fireflies are about, or fireflies with a visible light flare but no illumination, or fireflies with no visible light flare still illuminating the surroundings. Leaving tpw_firefly_blink = 1 helps alleviate the demands on the lighting system, since at any given time less than 20% of flies should be illuminated on average. 


* I've experimented with 500 simultaneous fireflies with no noticeable FPS impact. 
