Freespace 2 and Prime Factorization

Written 2010-11-30

Tags:Star Wars FreeSpace 2 Fate of the Galaxy FotG 

Some time ago, I wrote a patch for the open-source continuation of the Freespace 2 project (FS2_open). Previously, each ship could only select between firing one set of primary weapons, or all sets of primary weapons. Firing individual weapons in a set was decided by the ship's data files.

However, games with more complex ships needed a different way to control weapon firing sequences. In particular, Fate of the Galaxy, a fan-made StarWars space shooter, needed a way for pilots to pick how many versus how quickly each bank should fire. As situations change, it may become necessary for pilots to reconfigure primary weapons to fire in parallel, for more of a shotgun effect instead of sequentially firing each weapon in the bank, which leads to a much more even power draw. The X-Wing is a perfect example of this - when attacking faster craft like interceptors, quad lasers are much more useful. But when attacking swarms of small asteroids, waiting for all four lasers to recharge is a detriment.

Because FS2_open is a game engine instead of a game, the same code needs to control the firing sequence of each ship. But how does a ship decide what firing sequences to expose to the pilot? The simplest answer is factorization. For example, a ship with four primary weapons in a bank may fire arrangements of 1x4, 2x2, or 4x1. A ship with six can fire 1x6, 2x3, 3x2, or 6x1. Over a long enough time period, the power draw should be consistent, so for each arrangement, delay the firing time by the number of shots, and it will average out evenly.

Finally, there is the question of how to factor these numbers in flight. The simplest solution would be a linear search from 1 to int(sqrt(n)). This could be done at mission load time for each model of ship... or it could be done at compile time with a look-up table and an assumption about the maximum number of firepoints per bank, which is the solution I implemented.