Dr Nic

Turn-based game DSL

Late in the night, whilst the baby feeds, I continue to develop Dr Nic’s Civilizations game. This led me to develop a DSL for the specification of game rules. You need to see this.

Here’s an example definition of some terrain:

  class Desert < Terrain
    title           "Desert"
    letter          "d"
    graphic         "desert"
    movement_cost   1
    defense_bonus   10
    food            0
    shield          1
    trade           0
  class Oasis < Desert
    title           "Oasis"
    special_code    1
    food            3
    shield          1
    trade           0
    graphic         "oasis"

An Oasis is a special version of the Desert terrain, so Ruby subclasses offer a compatible relationship.

This is seriously cool. All in Ruby.

Why write a DSL for something trite like game rules?

The descriptions of terrain (plains, hills, ocean), the improvements (roads, irrigation, mines), etc. all need defining somewhere. Here are the standard options:

  1. Rows in a database, imported into ActiveRecords at startup
  2. Files that are loaded at startup, parsed and converted into an internal data model
  3. Ruby DSL

The sort of configuration we're dealing with is static: its a part of the game design to a certain extent. Yes, I could put the configuration in a database and access/modify it via an admin console, though version control becomes an additional administrative hassle.

Storing game rule configuration in external files makes version control trivial, though there is a one-time cost of implementing special purpose syntax, parsers, internal game data models and testing.

A Ruby DSL is exactly the same as implementing option 2 - the configuration is stored in files, though as the syntax is pure Ruby, there is no need to implement a parser and internal data model.

There was a one time cost for supporting the syntax. But why the lucky stiff paid this cost for me with his traits definition for slaying dragons [1]. So I was free and clear of any actual effort.

"Yes, smart arse, but you still had to write up all those Ruby classes"

An irrelevant argument, you'd have to enter the configuration for any of the 3 options, but...

I wrote a generator to build the Ruby class definitions for me from the Freeciv's GPL rule sets. Sweet.

[1] The definition of the Terrain class, using the traits mechanism, is simple:

  class Terrain
    traits :title,

Related posts:

  1. Magic Multi-Connections: A “facility in Rails to talk to more than one database at a time” At this point in time there’s no facility in Rails...
  2. Coming home to Brisbane I left Australia on the 3rd of July last year...
  3. Extending _why’s Creature class Many Rubist’s first explanation of metaprogramming is by why the...
  4. So, cattr_accessor doesn’t work like it should? Rails’ active_support library adds some wonderful functions into standard Ruby...
  5. [BTS] Dr Nic’s Civilizations 2.0 I’m not promising there ever will be a Dr Nic’s...

2 Responses to “Turn-based game DSL”

  1. [...] A FreeCiv meta language A brilliant domain specific language for turn based games. [...]

  2. [...] Dr Nic Turn based game DSL Posted by root 1 day 15 hours ago (http://drnicwilliams.com) Turn based game dsl posted by dr nic on september 07 2006 and blessed with 1 comment powered by wordpress styled with scribbishwp Discuss  |  Bury |  News | Dr Nic Turn based game DSL [...]