In science fiction, space bases usually have some combination of circular and straight components to make it look “futuristic”. I wanted an algorithm to generate a layout for a space base or space station. Initially I had sketched out something that combined the circular and straight components in various ways but I decided to start with just the circular ones, because they’d look cooler.
The basic algorithm is:
- Generate locations for “nodes”. In the straight layout algorithm, the nodes would be along a cartesian x,y grid. In the circular layout algorithm, the nodes are along a radial r,φ grid.
- Choose which locations are “active”. At each active location we place a node object.
- Fill in edges to make sure all the node objects are connected. In the radial layout, there are arc edges (along the circular levels) and radial edges (from level to level).
- Each node has a radial edge to the previous level.
- Each node has an arc extending from it, of random length.
- Some arc edges will be expanded to ensure that the radial edges to the next level are connected to something.
After sketching out the idea and algorithm on paper, I implemented it in Flash, then added a simple Flash MX UI to tweak the parameters:
For a busier layout, try decreasing the “radial spacing”, increasing the “levels”, increasing “node density”, and decreasing “node radius”. In general, try experimenting with “levels” and “node density”.
I’m a programmer and I’ve needed art for my games. I prefer to have procedural art (most of the graphics for Blob City were procedurally generated). I look for small, simple code that can generate something decent looking. Having lots of variety is nice; that’s one of the advantages of procedural art. It’s even better if the art can match the game objects and the player’s actions. The space base demo’s layout and drawing code compiles to under 5k (probably under 3k in a real game, because you won’t need as much setup code and generality), but when you add the MX UI, it ends up being around 180k. (Ugh, I’d use MX more if it wasn’t so big!) It’s only a few hundred lines of Actionscript, and I’m sure much of that could be simplified for use in a real game.
Initially I was thinking this would be used in a 2D space battle game to generate random space stations. Your ship would have to destroy each of the energy nodes in order to destroy the station. However, it’s somewhat generic, and it could also be used for lunar factories on the surface of a moon, or for a security pad a door panel.
Some ideas of how to extend this:
- The base could grow over time, both adding nodes to the interior and adding tendrils to reach out farther. The “node density shrink” parameter lets you lower the density of the outer layers.
- The nodes could be different shapes, colors, textures, and sizes to match the kinds of game objects they represent. For example, if it’s a lunar factory, they might be colored for storage, energy production, raw materials, final goods, habitat, etc. The “node radius shrink” parameter lets you make outer nodes smaller than inner ones.
- The nodes could be recursive/fractal, so when you zoom in, you see another radial layout.
- The nodes could revolve around the center. Each layer might revolve at a different speed. You might recompute the edges as the nodes move.
- The edges might themselves be game objects, so you need to destroy some edges to disconnect power to the shields before you can blow up the node.
- The edges might be “energy” instead of physical. With some animation, the edges could be balls of energy moving back and forth between nodes.
- spacestation.as has the layout algorithm and the drawing code.
- spacestationdemo.mxml has the UI for manipulating the parameters.
- AngularRange.as is a helper class for representing angular ranges.
The code is released under the MIT license, so you can use it for free and commercial games.
I’d love to hear what you think. Leave comments on my blog.