Tuesday, July 26, 2011

An Autoplace & Route Tool for Civilization Style Tech Trees

A particular statement in this piece by Derek "Kael" Paxton about technology in Stardock's Elemental: War of Magic stood out:
5. Autolayout- The tech tree draws itself and its lines automatically.  This was needed because the tech tree will be different every time, so it must be able to lay itself out programmatically.  The good news for modders is that they can add techs to their hearts content and they will be automatically added to the tree right where they belong.
I thought that was really cool, and decided to look into how to do such things.

After some literature research, I found this paper out of AT&T Bell Labs By Emden R. Gansner, Eleftherios Koutsofios, Stephen C. North, and Kiem-Phong Vo.

The algorithm looked fairly straightforward so I decided to see if I could implement it first in Excel 2007 (VBA) and then in Civilization IV (Python or C++ depending upon which level I ran the algorithm).

Here is how the tool works:

  1. Reads in the Technology Data from a spreadsheet
  2. Processes the Technology Data to add missing Parent-Children links (Data originally contains only Children-Parent links)
  3. Does a DFS search from every root node and calculates an affinity between each root based on how many nodes they share.
  4. Orders the Root nodes based on their affinities, so that roots which share many nodes are placed together
  5. Runs most of the Vertex Ordering part of Gansner's Algorithm (Heuristic Based)
  6. Determines X and Y coordinates for each technology based on its rank and vertex order.
  • I have not yet finished generalizing the code.  I think that if you try it right now on a tree that is deeper than 9 ranks, you'll get an out of bounds error.
  • It's not as fast as it could be, there are some inefficiencies I already have in my mind to target.  For now it takes about 2 seconds to generate and draw the placement.
  • It requires Excel 2007 and Macros enabled, you trust me right?
The Final Result

Download AutoPlace and Route Tool