Building A City - Part I

imageWell. My last post was a little, ummm... popular? Slashdot, Boing Boing, BluesNews, and pretty much the rest of the planet have all linked to it. Glad everyone is happy about the source release.

Anyways, there's been a lot of buzz about the code and the main questions that come up are "how do I get it to compile?". Let's go through this.

This is the first part of a series. In this post we'll go through the tools you need to install and the steps to get the Python and Windows code compiled. In part II, we'll hook up what we've built with a UI. Beyond that I'll dig into the various engines and ways to manipulate and change them.

Tools of the Trade

The Micropolis code isn't just C code. There is that (bascially the original code slightly retrofitted and updated for OLPC). There's also the python and TCL code. Python is an OO programming language that Don Hopkins chose to work with. Python itself is a pretty good language to work with but there's additional power using a tool called SWIG. SWIG let's you take your existing C/C++/etc. code and "pythonify" it. No, we don't mean to teach it how to lop off the Black Knight's arms and legs in battle. We mean expose your already existing property (in this case the SimCity code) to python. This will allow us to plug in scripting to Micropolis and make it do our bidding.

Anywho, here's the rundown of the tools you'll need to get started.

SWIG

SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages. It's run by the projects to generate Python files from the C++ ones. Grab the latest version of SWIG for Windows from here and install to C:\SWIG.

GTK

GTK is a multi-platform graphical windowing library. Download and install the gtk++win32-devel package here. Use the default C:\GTK folder for installing. The GTK install includes Cairo, a 2D graphics library used by Micropolis so you don't have to install it separately.

Pycairo

While you get the Cairo libraries for free with the GTK install, you'll need the python bindings for Cairo. These are available through the pycairo project here. Install to C:\pycairo. This will enable you to build the TileEngine project.

Python 2.5

Python is an OO scripting language that has a lot of flexibility and power. Micropolis exposes its internals using SWIG to python so you can plug it in as an engine to drive the core functionality. Download Python 2.5 from here. You can install 2.5 just to get going although the site recommends 2.5.1. Install it to the default location (C:\Python25) and you're all set.

Compiling

The source code comes in two packages. The first one, micropolis-activity-source.tgz, is the TCL/Tk version for Linux with the (mostly) original C code. The second package, MicropolisCore.tgz, (the one we're going to build with Visual Studio) is a C++ version which has the following features:

  • A Micropolis project containing cleaned up C++ code from the original C code for the main game engine
  • The cellular automata engine pulled into a separate project of its own called CellEngine
  • A TileEngine project (using Cairo by default) which displays Micropolis tiles and CellEngine cells.

Once you have all the pre-requisite tools listed above installed you're ready to go. Note that if you installed the tools in anywhere but the path mentioned, you'll have to modify the projects in Visual Studio so it can find them accordingly.

First, right click on the Micropolis project and build it. You should get output similar to this:

1>------ Build started: Project: Micropolis, Configuration: Release Win32 ------
1>Executing tool...
1>..\micropolis.h(1240): Warning(462): Unable to set dimensionless array variable
1>..\micropolis.h(1242): Warning(462): Unable to set dimensionless array variable
1>..\micropolis.h(2093): Warning(462): Unable to set dimensionless array variable
1>..\micropolis.h(2095): Warning(462): Unable to set dimensionless array variable
1>..\micropolis.h(2097): Warning(462): Unable to set dimensionless array variable
1>..\micropolis.h(2099): Warning(462): Unable to set dimensionless array variable
1>Compiling...
1>micropolis_wrap.cpp
1>Linking...
1>   Creating library d:\Development\Projects\MicropolisCore\src\Micropolis\python\\Release\_micropolis.lib and object d:\Development\Projects\MicropolisCore\src\Micropolis\python\\Release\_micropolis.exp
1>Generating code
1>Finished generating code
1>Embedding manifest...
1>Build log was saved at "file://d:\Development\Projects\MicropolisCore\src\Micropolis\python\Release\BuildLog.htm"
1>Micropolis - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Once this is built in your Micropolis project you'll see that the SWIG folder is now populated. This contains all the python wrappers that talk to the C++ Micropolis code (generated by the SWIG tool).

Now build the CellEngine project. You should get output similar to this:

1>------ Build started: Project: CellEngine, Configuration: Release Win32 ------
1>Executing tool...
1>Build log was saved at "file://d:\Development\Projects\MicropolisCore\src\CellEngine\python\Release\BuildLog.htm"
1>CellEngine - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Finally build TileEngine project. You should get output similar to this:

1>------ Build started: Project: TileEngine, Configuration: Release Win32 ------
1>Executing tool...
1>..\tileengine.h(125): Warning(454): Setting a pointer/reference variable may leak memory.
1>Build log was saved at "file://d:\Development\Projects\MicropolisCore\src\TileEngine\python\Release\BuildLog.htm"
1>TileEngine - 0 error(s), 0 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Now that everything is built you've got yourself 3 import libraries that can be used by python. At this point you could drive the system purely from python. The Linux port uses Tcl/Tk as it's windowing system and we'll get into using this to interact with the python output we created here in the next blog post.

This is a series of posts exploring and extending the Micropolis code. You can view the full list of posts here.

Published Sunday, January 13, 2008 4:01 PM by Bil Simser

Comments

# re: Building A City - Part I

Tuesday, January 15, 2008 8:11 AM by Jonas

A Visual Studio independed version would be appreciated...

# re: Building A City - Part I

Tuesday, January 15, 2008 11:29 AM by Bil Simser

@Jonas: Not sure what you mean by independent? The project is for VS2005, although it could be compiled for VS2008 and I assume you could create a VS2003 version if you really wanted to. It's just C++ code so nothing special going on here.

# re: Building A City - Part I

Sunday, January 20, 2008 9:55 AM by A Edmonds

Hello,

I'm trying to compile the Micropolis project in Visual Studio 2005 and a get the following error message:

error PRJ0017: A tool return an error code from "Executing tool..."  

It seems if I try to compile and of the project in the solution file, I get this message.  

I have went through the list of files/programs need and I have installed them all at least twice each.  

If anyone has any ideas I would greatly appreciate them.  

Thank you,

Adam

# re: Building A City - Part I

Tuesday, January 22, 2008 12:17 AM by Clay

Regarding this error: "error PRJ0017: A tool return an error code from "Executing tool..."  "

I got this when I had the swig files installed at a location other than c:\swig. If that's not the problem, take a look at the build log that visual studio makes, it will have more information.

# re: Building A City - Part I

Tuesday, January 22, 2008 12:27 AM by Bil Simser

@A Edmonds: SWIG needs to be installed to C:\SWIG. Otherwise go into the properties for each project and change the path to your path.

# re: Building A City - Part I

Saturday, January 26, 2008 1:52 AM by P-Dawg

I don't think installing pycairo to c:\pycairo is required.

In the second article there's another installation step for pycairo that does work for me and DOES seem to be required.

I deleted my c:\pycairo and it works fine.

# re: Building A City - Part I

Friday, February 1, 2008 5:25 PM by Thaddeus

a minor point, but you might want to make your "link" pycario an actual link :)

# re: Building A City - Part I

Thursday, March 13, 2008 4:02 AM by Antonello

Hi guys,

Can you explain me, step-by-step, how to compile MicropolisCore in VS2005? I have never used VS.

Thank you

# re: Building A City - Part I

Tuesday, August 7, 2012 9:46 AM by Javier Ortiz

pycairo link is not working.