Open Game Source: Freeciv

by Dennis Payne

Name: Freeciv
Version: 1.9.0
Administrators: Jeff Mallatt and Tony Stuckey
License: GPL
Operating System: Primarily X under Unix


Freeciv is a turn based strategy game designed after the Civilization games from Microprose. Originally started in 1995, Freeciv can currently play a clone of Civilization or Civilization II. The current stable version includes "an AI which gives most people a run for their money." Extensive network support allows players to negotiate peace with long time friends or internet strangers. Additionally players can easily design their own customized settings.

Hacking Guide

Open source generally starts as a solution to a developer's problems or wants. The reasoning behind structures tend to exists only in the creator's mind or the code comments. As a project gains critical mass, explaining such matters becomes more and more time consuming. Freeciv includes a hacking guide to help new developers.

Additionally there are several other files of interest to hackers. The README.AI describes some of the problems and ideas for the computer players. The design of Freeciv allows for extensive game modification through the use of rulesets, described in README.rulesets. For artists, the explains some of the aspects of creating new tilesets. Looking for something to do? The TODO list provides an excellent starting point or perhaps check the Freeciv web site for the roadmap of the next release.

An active mailing list describes current Freeciv activity. Snapshots of the CVS archive are available which show works in progress. The Freeciv organization is an excellent model of how to run open source game development.

Building Improvements

Having viewed the source to Civilization: Call to Power, the Freeciv source is much nicer. In just a few seconds of looking around, I could easily guess where important functions and structures could be found. The code is done in an object-oriented style that improves readability.

One area in particular caught my eye, city improvements. Improvements, such as a bank and the Pyramids, modify the production and other statistics of a city. To build an improvement the civilization must have sufficient technology. So what if we remove technology from the game?

Master of Magic

Microprose published Master of Magic which did just that. Although they could have simply swapped technology for magic, the designers were much more inventive. For the time being lets just look at the city improvements.

Under Master of Magic certain units and buildings can only be built after another improvement. For example the fighter's guild and halberdiers can only be made after the armory is constructed. Some improvements require that two buildings be constructed.

Allowing this kind of advancement under Freeciv is fairly easy to implement. Two improvement requirement fields were added to city improvements. Rather than change all the existing data files I made the requirements default to none. Units gained an additional improvement requirement as well. To construct the patch I didn't even need to learn much about the internals of Freeciv. (Note: The patch modifies some of the packet structure and therefore is not compatible with unmodified clients.)

The modification currently doesn't fix the rules for selling buildings. So you can currently sell a building that is required for another improvement which I believe doesn't fit with Master of Magic. The "Change All" option from the client's city menu is another area that could use enhancement. Currently it displays all the buildings even if no cities have the prerequisites. Many other modifications would be needed to implement Master of Magic fully as well.

More Improvements

The game effects of improvements are still hard coded for the most part in Freeciv. Each building has a variant field but that only determines if the improvement follows the Civilization or Civilization II effects. The readme describing the rulesets lists scrapping the variant system for something more general as a future enhancement.

Apparently units were originally designed in a similar manner. Over time the hard coded types were replaced by the flags and roles in the data files. Ideally this should be done for improvements as well. However, it isn't quite as clear how to perform such a task for buildings.

The best explanation is to take a look at the could_build_improvement function which determines what improvement can be built. The university, for example, isn't constructed before the library. The Master of Magic patch allows for such a requirement. But how do you express that the city should build only one electrical plant which comes in three types. You could add a flag that the improvement is an electrical plant but checking if a plant has previously been constructed becomes time consuming. Caching the information with the city data is simple enough although you will still have problems when you sell the plant. So what about the spaceship pieces? They require the Apollo wonder before even starting. The ship must still have space for the selected part and, of course, the ship must not be on route to Alpha Centauri. Checking for the Apollo wonder encounters similar problems as finding an electrical plant. Without a more general system, adding a Master of Magic option would be difficult.

Coding Standards

Although Freeciv's object-oriented style improves readability, it is sometimes used to an extreme. For example, get_improvement_name gets the name of an improvement based on the improvement id. Instead of getting the improvement entry by improvement_type[id], they called another function get_improvement_type. Most of the other functions to get an attribute of the improvement structure don't call get_improvement_type so I'm not sure why it is done here.

The game has extensive international support. Freeciv uses the GNU gettext library for localization support. The text in the game is English as if no international support exists. Before outputting to the user interface the program calls gettext which looks up what the text translates to in the .po files. Normally the extensive calls to gettext would reduce readability, but the Freeciv coders hid the call in an unobtrusive macro _. Here's a code fragment to highlight the technique:

/* Instead of:*/
cmd_reply(CMD_EXPLAIN, caller, C_FAIL, gettext("No explanation for that yet."));
/* Freeciv uses: */
cmd_reply(CMD_EXPLAIN, caller, C_FAIL, _("No explanation for that yet."));
According to the Jeff Mallatt this is the "non-official quasi-recommendation straight from the GNU gettext folks." Civilation: Call to Power used a similar but not nearly as unobtrusive technique. Using gettext still requires a string comparison or hash which is less than ideal.

Another similarity to the commercial Civilization is the use of a common data file format. Freeciv uses simple text files similar to the old Windows .ini files. The files usually have several sections. Each section can define any number of name and value pairs. For a more detailed explanation of the file format see registry.c. In fact the format is even used for the save files. This one feature alone is probably worth a look at by the various open source developers.

Back to OGS

Copyright (c) 2000 Dennis Payne / Identical Software