One of the cool things I am trying to do with this game is to build an abstract simulation of what we have all witnessed all over the world. The Collapse of a government into civil war. This is the main “villain” in the game in the sense that the primary goal of the 4x game is to avoid your faction’s descent into civil war.

In order to abstract anything you conceptually have to define it first so I started with modeling three things, 1st, the leadership cycles that governments go through. 2nd the phases that a civil war could experience, and 3rd an event-driven system that can trigger the first two.

Ok so that last paragraph was a mouth full so let’s break it down even more. Start with a big chart



Gameplay Cycles – these are pretty simple as a concept, any country’s leadership turns over this change in leadership is called a gameplay cycle, in the case of countries that are democracies, we simply make the gameplay cycle a time period of 8 years or 2920 days (n Days). For the games, two other cycle types, Dictatorships, and Monarchies those time periods are just longer (giving the player more time to make things worst for themselves). At the end of the Leadership cycle, the game will run a rules engine that will determine if the StorySequence should change moving the player into a new part of the overall narrative.


The Story Sequence is the master game state that determines what the other factions’ AI agents will do and can do against the player and ultimately what event types of “things” will happen to the player. The story sequence is heavily influenced by the concept of an AI Director in games like Left 4 Dead. The StorySquence is critical to the flow of the game and ultimately affects both gameplay and visuals. Just think of its like how a film is structured with three acts, only a civil war actually has 10 acts or Story Sequences.

In the game, the Story Sequence can take the player to four different narrative outcomes. The optimal is the Sustained Peace and Well-Being phase aka “Utopia”. The other three outcomes are not great, civil war, nuclear war, or natural disaster. These outcomes are determined by the player’s behavior during the game session. If the player is too aggressive they might end up in a outcome that leads to nuclear war, game over. So a story sequence is just defined as a set amount of time (n Days) from when the player starts to when the next story sequence can happen. In this case, it’s every 8 in-game years. In the game, we break this down into the “training wheels period”. This is done during the first gameplay cycle called the “First 100 Days“. This is a time period where other gameplay cycles aren’t running and the player can get used to the basic mechanics of the game without any major disruptions or plotting to overthrow you.

The Rules Engine evaluates Events generated during the StorySequence based on a time interval, the player’s behavior, and the other AI faction’s agents’ actions to determine what actions it should take next. In the example, the leadership cycle ends after 8 game years, or when 2920 days have passed this event trigger is applied. Now the important thing to remember here is that other events can and still do happen in between these story sequences and that those events can also shift the story sequence. Intel Events, Terrorist Events, and even Cultural events all can shift the story sequence.

Now for this to work we need to bring all these parts together into a country manager which will run the country for the player. The manager itself will have a queue of events that have to happen. We use a queue because we will need some determinism in the game to add and remove events from the list before they are processed potentially and ensure that they are processed in a fixed order. We also queue and dequeue items from the master list because we don’t want to maintain a long-running log list of all the events that ever happen in memory as this will increase the game’s memory save file, the longer the player plays the game(which I want). We can pump these events into an offline historical database for other purposes, but we want to keep this outside of the save file for a number of reasons (this is also beyond the scope of this post). This keeps the memory profile from becoming unstable in later game stages, think about playing this for 20+ real-time hours how many virtual events might have happened?

When the country manager starts up it creates the event queue and starts to fill it with events based on the player stats against the rules engine and the game’s overall master play state.

Now a leadership event is just one of many game events, but in this example, it’s used to show how the events are triggered.

An event takes place in a particular location, on a particular date, and most importantly it has a set of things it changes, in this case, we have a generic event that can change any of the player’s perks/flaws/infrastructure/resources/bias so it can make an event that takes a players power grid from online to offline.

Now the clever part of this is how these events are chained together and triggered. Instead of having to continuously check the rules which would happen inside of an Update() and is CPU intensive. We call it inside the event itself and chain it to the next event.

In order to trigger the event we avoid traversing the list of events looking for something to process and instead we wait until the currentSpawnInterval is equal to the timer we set up (n Days). This is just a delay for how long we need to wait, we evaluate the state of the world and use that to determine what should happen next, instead of continuously polling the game, we make sure that the currentSpawnInterval remembers it should wait 2920 days is true. When this code runs in the update it is a simple != this vs a find and eval this.

When that is true then it will fire the trigger event, which will then remove the event from the list and call the function which will set/change the player’s stats, in this case, a change in leadership evals if we should change the Story Sequence to Political and Economic Grievances. That will only happen if the player government support drops before a certain number. When that happens the game will generate a different set of events that will force the player to either increase their support levels or further lower them. After that Gameplay cycle ends once again the player will be at a narrative cliff where the game will start to form rebel groups that will fight against the player and allow the AI agents to undermine the player. Currently, there is no plan to have different AI agents work together but in the future this is possible.

The AI Agent will be discussed in a future post, but very quickly the AI agent works like a decision tree that can remember previous events and weights those previous events’ outcomes against its understood decisions and is capable of adding new decisions based on a pool of decisions that govern a number of play strategies, think of RTS play styles (rusher vs turtle).


When is random not really random?

Now when we create a world event we don’t want it to be totally random it should happen based on two things, the Story Sequence and the Player’s stats. But for events to have meaning they have to change the game world, but not break it. Think of real life, some random events will just happen, but other events have things that proceed them. If an earthquake happens it will impact the country’s ability to produce stuff, but if the game generated earthquakes every five days that would be unrealistic.

In order to prevent the game from randomly generating events like back-to-back earthquakes. The game will create events along with a bunch of other possible events. Those past events live in a list called eventPrefabs. The Event Priority is the threshold to wait between this event and the next repeated event. So an earthquake event happens back to back it will then wait to exceed the threshold of “the other earthquake” events that might be randomly generated right after it. The downside of this approach is you will generate more events than you will actually use and that will never actually happen, but this can be reduced by limiting the length of the “event” memory can be. But you also don’t have things happen back to back that don’t make sense.

Now the big question why does this exist in the game? Well, it is there so that when you play in a single player you will have a villain to play against, an underlying force that is nudging you in one direction and forcing you to make decisions that will have known-unknown outcomes. In this way, it creates an open narrative system for the game that I can’t completely define but can set the narrative boundary. One of those four outcomes will happen, It’s just up to the player to get to them.



Categories: Uncategorized