Lead Developer, Stardock Entertainment
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.


Comments
on Dec 03, 2004
The rings puzzle is a classic LIFO problem, nice to see that I am not the only guy who has overlooked it's modern applications...
on Dec 04, 2004
Well explained CariElf
on Dec 05, 2004
you have a functional programmer amongst you (the horror =p), or you have some incredibly degenerate trees. Either way, I'm pleased that hear that you guys are performance testing. I like my turn-based games to be nice and responsive.
on Dec 06, 2004
n code was causing part of its own slowdown, not just all the ships and planets updating.   

on Dec 07, 2004
I don't know - the best definition I've seen went:-

Recursion : see recursion

Alex
on Dec 07, 2004
*sigh* dat carielf. i iz not understandin' a word but i still lurv readin' it
on Dec 08, 2004
I appreciate posts such as this. I enjoy reading them and knowing what actually goes into making the games I play (even the small things). Please keep them coming!