Linux DevCenter    
 Published on Linux DevCenter (http://www.linuxdevcenter.com/)
 See this if you're having trouble printing code examples


Building Freeciv: An Open Source Strategy Game

by Howard Wen
11/21/2001

If imitation is the sincerest form of flattery, then Freeciv is clearly a labor of love among its many volunteers. Since the code for this open source version of Civilization was released in 1995, hundreds of volunteers have added to it and improved it, even though the original developers haven't been heavily involved in years.

Freeciv runs on Linux, Windows, OS/2, and Amiga, and features online and network multiplayer capability. Initially, fixing the networking and multiplay proved to be the biggest challenges for the Freeciv team. Nowadays, the toughest jobs remaining include sprucing up the game's graphics and re-doing the artificial intelligence (AI). Continued efforts like these have become goals not unlike the strategy behind Freeciv's gameplay -- building and improving upon things over time.

Freeciv's Roots in Openciv

As it turns out, Freeciv is not the first open source version of Civilization. The idea for Freeciv came about one day when three Danish students were in their university's computer science lab, playing Openciv, a free multiplayer Civilization clone written in Python using Tk/Tcl. "We soon gave up on the game, as it performed terribly. At that point, the idea was born to write our own version in C," says Peter Unold, now a 30-year-old graduate student in Aarhus, Denmark. Along with two other fellow students, Claus Leth Gregersen and Allan Ove Kjeldbjerg, he created Freeciv while studying computer science at Aarhus University.

Making their own knock-off of Civilization, which would work better than Openciv, was not necessarily their sole motivation. The project was seen as a good way for all three to gain some skills in X11 programming and client/server game development. Because their goal was to learn these things as quickly as possible, a basic working version of Freeciv was finished in only three weeks.

While Freeciv was more stable than Openciv, it was not an entirely complete game. It was playable, just not very fun because there were a couple of technical problems that needed to be addressed. But Unold and the other two authors of the game moved on to other projects and responsibilities. Fortunately, with its source code made available to the public, Freeciv quickly gained a following, and a succession of administrators and volunteers picked up where Unold and his colleagues left off.

Client-Server Issues

One thing that proved especially challenging to fix up was the game's client and server system. The problem was deciding what should be done in the client and what in the server. "If the client had to query the server for information, you would incur extra latency from the network that would annoy the user," says Trent Piepho, a 25-year-old computer scientist from Seattle, Washington, who contributed lots of fixes and improvements to the Freeciv code in 1998. "But if you did something in the client, you had to worry about people modifying the clients to cheat."

"I also remember how we tried to limit network traffic by having the clients do as much [of the] calculation as possible -- thick server/thick client," Unold says. "We later gave up and went with a thick server/thin client solution."

"Some don't agree, but I think the strict client-server separation in Freeciv is elegant," says Thue Kristensen, a 22-year-old university student from Copenhagen, Denmark, who was once one of the administrators maintaining Freeciv. "It is my feeling that in all the commercial titles with multiplayer, each client has a complete copy of the game world. Having the strict separation in Freeciv means that cheating is impossible [because it] eliminates unintentional cases of revealing too much information."

Related Reading

Physics for Game DevelopersPhysics for Game Developers
By David M. Bourg
Table of Contents
Index
Sample Chapter
Full Description

The client cannot make changes on its own; all it can do is ask the server to make changes. An example during gameplay would be the way a diplomatic action against a city is executed. The player does this by moving a diplomat into the city and then choosing an action from a pop-up menu. This actually works by sending the move request from the client to the server, which then sees it is a diplomat moving into a city and tells the client to pop up the menu. When the player chooses from this menu, the choice is sent to the server, where it is executed, and the results manifest themselves in the client.

"Letting the server tell the client to make the pop-up (and not letting the client act on the order itself) is smart because the server, when executing a goto, will move the unit without interaction," says Kristensen.

Introducing Artificial Players

Networking matters aside, there was also work to do on the gameplay itself. While designing the multiplayer feature, Unold came away with the opinion that strategy games like Civilization do not fare well under this format: "Imagine five people playing a game of Freeciv. After 30 minutes, one of the players is crushed. The game is probably going to continue for another 3-4 hours, with no way for the unlucky player to re-join."

Unold and his fellow Freeciv creators tried to find a way to keep players in the game by designing artificial players that would start the game along with the real players, but whose control could be taken over by a real player who had been crushed. "It worked, but wasn't really quite satisfying for anyone," Unold said.

Screen shot.
Figure 1. A wide view of players' positions in Freeciv.

The result of this fix, instead, became a test of one's patience. The crushed players who take over AI players are expected to continue putting up as good a fight as possible, and it takes much longer for the winning player to finish the game. This is an issue which was not fully resolved to Unold's satisfaction before he left the project. "We all hate to lose, especially if losing means a long painful process," he says. "In general, multiplayer takes too long to play."

Simultaneous Moves

Another multiplayer issue stems from the fact that players makes their moves simultaneously. A battle unit in Freeciv has both an attack strength and a defense strength. During a multiplayer game, when two opposing units are placed close to each other at the end of a round, the game turns into an action game at the beginning of the next round, since each player wants to either attack or run away as quickly as possible. The only good solution to prevent this would be to disallow simultaneous movements. This, however, says Unold, "would turn the game into a boring waiting game."

Luckily, this quirk turned out to be not necessarily a bad thing. Gregersen, who is now a 30-year-old programmer in Aarhus, Denmark, points out: "The design of our multiplayer model (everyone can move simultanously) created a new variant of Civilization. Even though it has basically the same rules as Civilization II, hardcore Freeciv players play it as if it's a real-time action game like Age of Empires."

Wish List: Better AI, More Art

Under its present administrators, improving Freeciv's AI is the top priority, particularly the client-side AI and server AI. This may entail writing completely new AI code. The issue with the AI is not that it does not work; many good Freeciv players find that the AI is fairly challenging. The problem is being able to modify it.

"The AI code is badly documented, both overall and in the individual functions," says Kristensen. "It makes it hard to make improvements to it. It also makes it hard to make changes to the codebase as a whole if those changes touch the AI."

Other areas in need of improvement are artistic. Jeff Mallatt, a 42-year-old engineer from Arlington, Massachusetts, who formerly maintained Freeciv, points out that the project's greatest weakness is that, while it attracts plenty of programmers, hardly any artists, musicians or technical writers have contributed.

He gives an example which illustrates this problem, and explains the notable absence of sound in the game: "A functioning sound system has been implemented -- in fact, at least two have been implemented -- but not ever finished. The reason: no sounds! The system is worthless without sounds. And no matter how much we beg for sound (or artwork or user documentation) contributions, we rarely get anything. I don't mean to make it sound like Freeciv is in desperate need of more contributors. Contributors of any resource are welcome. It's just that some areas are under-represented."

Screen shot.
Figure 2. A closer view of players' positions. More sound and better art are on the wish list.

It being one of the more successful projects in the open source community, one would think after being in such active development for so long that Freeciv might rival its commercially-sold counterparts in quality and features. It does not, and similar strategy titles like Civilization II and Alpha Centauri clearly have slicker user interfaces, smarter AI, and generally better gameplay overall.

So what then has been Freeciv's enduring appeal among the hundreds who have contributed to its development over the course of six years? Maybe, as Unold suggests, it goes back to the idea of building elaborate stuff, a theme which code-tweaking and the premise of games like Civilization share:

"Playing Civilization is like playing with Lego bricks. Build a civilization from the ground up, taking care of little issues like building roads, irrigating, city planning, etc. It's so geeky and we love it."

Howard Wen is a freelance writer who has contributed frequently to O'Reilly Network and written for Salon.com, Playboy.com, and Wired, among others.


Return to the Linux DevCenter.

Copyright © 2009 O'Reilly Media, Inc.