Originally Posted by Anitarf
Why does it take so long to generate the terrain?
The two longest parts are cellular automata iterations and critter population.
I have to TriggerSleepAction every so often to avoid going over the op limit and crashing out, which will be the cause of most of the waiting time (I do a wait 0.0 but I know that's not actually 0.0). I am fairly lazy in how I approach this; every iteration of some complex operation, I increment a "speed limit" counter, and once that reaches a threshold I reset it and call for a wait. The thresholds are based on anecdotal guesswork.
Cellular automata is expensive because you have to iterate over every tile (3600 iterations in this case), and for each tile query all of its neighbours, then perform some action -- doing this at least twice over the entire grid. Unfortunately the algorithm relies on advancing the current state of the grid, so I can't e.g. partition it and run several instances in parallel.
A lot of my other processes consume a lot of operations, too though -- enemy population relies on iterating over every tile again, checking if it's clear, deciding if there should be an enemy on it, then deciding which enemy to place. For critter/clutter placement, this relies on another smaller loop to decide which clutter to drop based on percentage ranges (so e.g. shrubs and flowers are more likely to be dropped than critter animals).
Though I measured it last night and it felt longer than it actually took; usually 22-24 seconds. As long as I can keep generation under 25 seconds, a few lines of cinematic intro dialogue should be able to fill the space.