Download Search Discord Facebook Instagram LinkedIn Meetup Pinterest Tumblr Twitch Twitter Vimeo YouTube

Simulation Engine: Production Rules

Welcome to the first post from the programming side of things. 🙂 I’d like to start with a few words about the game’s underlying simulation.

The core of the game is written as a lightweight production system. It’s an abstract engine for running rules, each of which has some preconditions, and if those are satisfied, produces some actions that change the state of the world (which causes next set of rules to activate and run, which changes the world, which activates more rules, etc).

An example rule looks like this:

frequency: every 7 days
checks: unit workers > 0
inputs: unit 5 leather
outputs: unit 3 shoes

This rule means: every 7 days, check if you have any workers and enough leather, and if so, consume the leather, produce shoes, and store them back in the building. This is a very basic resource conversion rule.

Another example:

frequency: every 10 days
checks: [ map crime > 0.8, check-random below 0.2 ]
success: [ start-crime, feedback-message key crime-effect ]

This means: if crime in the area exceeds 0.8, roll a random number generator looking for a value less than 20%, and on success, start a crime effect and let the player know.

So in our case, each unit (building, NPC, and so on) has its own “state”, and its own set of rules that it runs. Rules get checked every now and then, and whenever any of them triggers based on the state of the unit, the players, or the game board, its actions get run. These actions are typically very simple, e.g. “consume 5 leather and produce 3 shoes”, but can get pretty complex, such as “find the depot and walk over there”, or “start a fire with probability 20%”.

Rob changing production rulesRob changing production rules

Rules were designed to be moddable, and easy to change and test. All units and their rules are defined in a JSON file, which gets loaded into the game at the beginning. Most of the game’s simulation is built on these rules, defined in plain text, which means the game doesn’t have to be recompiled to change the logic – you can just change the JSON file and restart (the same goes for other non-rules based parts of the game, such as the upcoming terrain generator).

At the point of writing, the game has slightly over a hundred rules defined, for now just the ones that apply to buildings. The more complex buildings run dozens of rules at a time. This is not a huge number – although it’s going to get larger, once we add NPCs and the rules governing their behavior.

Production rule systems are a very, very old approach in AI, going back to the 1970s, when Newell and Simon and others were trying to create artificial systems that mimic the power of human thinking and dealing with the world. There are some pretty complex production systems out there. For our purposes, we’ve built a trivially easy variant, which doesn’t really do anything “smart”, just does what it’s told – but does it well, and quite efficiently.