We had a set list of things that needed to work in GC2 by the end of November, pretty much the very basic elements of Gal Civ that would let you play and win or lose by military means. We finished that, and so yesterday Brad gave us his feedback. One of the things that he brought up was that the end of the turn was taking too long. Now, we really haven't changed all that much of the end turn process, so I wasn't really sure what was causing the problem.
I went into the code and put in some debug statements and started stepping through the code. It didn't take me long to figure out that the code that changed the turn from player to AI to player was recursive. Recursive means that something is defined in terms of itself. So, for a common math example, the Fibonacci sequence is recursive. The way it works is like this:
f0 = 0;
f1 = 1;
f2 = f0 + f1 = 1;
f3 = f1 + f2 = 2;
fn = fn-1 + fn-2;
where n is as high as you care to go towards infinity. To get the nth number in the sequence, you add the two previous numbers.
Now, a computer uses what is called a stack to handle the instructions that programs give it. Imagine a peg that you put colored rings on, each ring being an instruction. Before you can take one off the bottom, you have to take all the ones on top first. This is called a last in, first out data structure (LIFO). In programming, code is usually organized into sections/modules called functions or proceedures (depending on the language and what the code inside it is doing) . Every time a function is called by the program to do its instructions, that function gets added to the stack along with the data passed to it. So if the function calls itself, you can see how it can quickly get big, and when the stack runs out of RAM it has to start using the hard drive. So now the end turn performance is much improved since I've made it non-recursive.