It’s not going to be productive to spend lots of time optimizing code at this stage, because things are still pretty fluid. I like to save detail work until later in the project. But today was devoted to some quick optimizations of the AI, pathing, and collision check routines.
Over the IndieCade weekend, I tried a few of the pathing exhibition levels on my laptop on battery power, and it went down to about 1 FPS once all 50 rats were on the screen. I was expecting this, since I didn’t put any effort into performance yet–I just wanted to get it working.
Visual Studio 2012 impressed me with its performance and memory profiler. I was able to quickly see what the slowdown was being caused by. It’s mostly collision detection, as usual, but the traffic field system was also grinding things down. The vision checks are pretty much the biggest culprit, because they do multiple collision tests per frame to check if one creature can see another.
There are some more robust solutions I can do later, but I only wanted to spend one day on it, so:
- Traffic field builds itself for entire map, rather than dynamically as creatures move.
- Vision field keeps separate lists of monsters and heroes so it doesn’t need to do checks or filters in the “detect foe” vision checks.
- Position comparison checks reduced by bailing out earlier for most cases (early returns from functions when it doesn’t make sense to do further tests on the actor).
Just the above cut the time spent in the update routine by about half. Meaning… on battery power, the torture test levels run upwards of 2 FPS! Okay, so there’s more to do (most likely a bucketing system to split the room into sectors, like Escape Goat). But this will do for now–I mainly wanted to make sure none of the systems I developed were going to pose such a performance threat that they would need to be removed later.