My name is Daniel DiCicco and I am the developer of StarDrive, a 4X space game.
This article is an overview of the general approach we have taken with StarDrive’s AI and is meant to provide some insight into the process of designing an AI for games of this genre. Primarily we will discuss how “hierarchical” artificial intelligence can be organized into tiers, with boss AIs delegating tasks to sub-AIs to keep things clear and flexible for the developer. Furthermore, we will discuss the concept of “Goals” which act in a dual role as scripts and memories for AI behavior.
A few game design basics
Games of the 4X genre present quite a challenge to the AI designer because these games tend to be very complex with many nuanced systems. Generally speaking, every 4x game involves growing a civilization from its infancy through its various stages of maturity and ultimately to dominance over the game world. Along the way you will need to explore this world, decide how to allocate your resources to best exploit what you find in that world, and you will need to compete with the AI players for control of these resources using diplomacy and combat.
To understand some of the higher level concepts of AI design, you should consider a few game design basics. First, in every game there is a bunch of data stored in memory and this data is known as the Game State. In a 4x game this might be information such as where the planets are, the location of all the ships, their health, their velocity, and so on and so forth. In a 4x game the Game State is typically quite large with many thousands of items stored in memory.
The second game design basic to understand is that this Game State is updated at a fixed interval – typically sixty times per second. That is, sixty times every second, the game has some logic that it follows to bring the game state into the next moment. It starts at the top of a list of instructions and works its way to the bottom. Even in turn-based games like Endless Space this is happening – the nebulas swirl, a little light blinks on and off — whatever. It’s updating fast and frequently and typically redrawing everything you see on screen every frame. Thus, frames per second really means how many times the game is able to loop itself per second.
The Artificial Intelligence of most games lives in that little loop, notwithstanding games that utilize multi-threaded AIs which just have their own loops. Every loop, the AI has the duty of analyzing the game state, drawing some conclusions from that game state, and implementing some sort of reaction to the game state (even if the reaction is no reaction). And herein lies the rub and the hard work. What data should the AI analyze? What should it do with that data?
AI in StarDrive and the hierarchical AI concept
In StarDrive I have a big boss AI that I call the GSAI – the Grand Strategic Artificial Intelligence. The Computer Player. It has full access to all of the data about its own empire, just like a player would. It knows where all of its ships are and all of its colonies. It knows what ships are inside its sensor range, what technology it is researching, how much money it has, and so on. And just like a Human player, the AI has a few imperatives programmed into it.
First, the AI wants to be constantly expanding. But rather than bother itself with figuring out which planet exactly to colonize and what to build there, the GSAI passes that task on to a subordinate – the Expansion Manager. The Expansion Manager is a limited little fellow. He can’t see everything the GSAI can see and he can only handle a few orders at a time. But he sure does know how to colonize a planet.
And so every update, the GSAI will tell the Expansion Manager – “hey, I want to have 2 (or however many) plans underway to colonize new planets. Take care of it.” The Expansion Manager then creates “Goals” and executes the subroutines needed to carry out the goal. It figures out which planet is best based on quality and distance. It figures out if it has any colony ships available and if it doesn’t, it orders one to be built. It remembers where it is building that ship so that each update it can check if that ship is ready yet. Once the ship is ready, it orders that ship to go on and colonize the planet. The GSAI meanwhile hasn’t gotten its hands dirty with the details.
Second, an AI player has the imperative of survival. It needs to be able to defend itself from external threats and to do that it is going to need ships and troops. But again, the GSAI is really worried more about what it needs rather than how it needs to get done. So it creates a few new manager positions – a Force Pool Manager and a Defensive Coordinator – and it tells them what it wants. For example, it may say that we need 50 ships and of those ships 25 should be on defense and the other 25 we should leave available for offensive operations. And then off the Managers go. They analyze what they have available, compare it against what the boss wants, and if we’re missing some ships then they’ll create a few private goals to get those ships made.
The Defensive Coordinator highlights a third level of the AI hierarchy: the Unit AI. The Unit AI is essentially the Captain of the ship! While the Defensive Coordinator is responsible for deciding what the ship should guard, the Unit AI is responsible for actually driving the ship and deciding how to execute its orders. It never has to think about bigger picture concepts, instead needing only to know where to fly and what to shoot. Another example of a third-level AI is a planetary governor, who knows only how to manage his planet according to the role assigned it by his boss AI.
But there are even more managers to be had. StarDrive also utilizes a Diplomatic Manager whose job it is to create and respond to diplomatic events. There is an Economic Manager that sets tax rates based on need. A Research Manager that picks technologies to research based on the GSAI’s personality. A War Planner to make decisions about which planets to attack in a time of war and to create fleets for those tasks. A Fleet Admiral AI whose job is to execute the orders given to it by the War Planner. That sure is a lot of managers!
At the core of the AI are Goals
Yet for all of the complexity of an AI like this, the manager system keep things flexible and easy to both enhance and debug. If a developer is unhappy with the logic used for exploration, then he or she knows exactly where to go to retool that Manager’s thinking. If we want some new behaviors out of a ship’s captain in combat, then we go edit his Unit AI. By enforcing the hierarchy and compartmentalizing these AIs we can get reliable and predictable AI performance.
At the core of all of these manager systems is the concept of “Goals”. The GSAI tells the Managers what to do and then the Manager creates “Goals”, which are basically like scripts. The Manager knows what it needs to do to execute the goal and tracks the goal’s progress in steps. For instance, the Expansion Manager might follow logic like this when evaluating its assigned goal of colonizing a planet:
- Find a planet to colonize
- Look for a colony ship that is awaiting orders. If none exists, go to step 3. If we have a ship, go to step 4.
- Check if we have requested a new colony ship. If not, create a colony ship goal. Next update, Go to step 2.
- Now that we have a ship, order the unit AI to go colonize that planet.
What would the human player do?
Ultimately, when designing a behavior for an AI, I like to think of what a human player would do. I break that down into discrete steps that the AI can execute using the same rules a human player must follow. I must be careful to consider all of the possible steps. What if the colony ship is destroyed while en route? The Expansion Manager needs to know that its goal failed and start over. But with careful planning and plenty of testing, it is possible to create complex finite state machines for these managers that are extremely effective at doing their jobs.
The downside of a system like this is that it is highly deterministic. Given a set of inputs, the AI is likely to respond in the same way every time because it is performing the same analysis. To combat this, the developer can input elements of randomness into AI decision making. So for instance, if the GSAI says “Give me 5 ships!” then the Force Pool Manager, rather than picking the same five every time, may do a few dice rolls to figure out which out of an array of acceptable ships it might build. Or which planet it will colonize, or what technology it will demand of a player, and so on and so forth.
Memories also play a crucial role in human decision making. If an AI has been a huge jerk to us then we’re going to remember it. We’re going to remember that they took our planet and we’re going to want it back. And, likewise, the AI has memories like this to. Deciding what to remember and how those memories will affect a Manager’s analysis, this is the creative work that the developer has to undertake.
At the end of the day we have to keep our goal in mind as developers. Our job is to create a fun and challenging experience for the player. Even if we had all of the money and time in the world to create an AI for our game, it could always be better. There are always more human things it can do. But the goal here is not to pass the Turing test. It’s to make a fun game.
What’s fun, well that’s a whole different can of philosophical worms. I believe that would be an excellent topic for conversation here — what do you find fun in AIs from 4x games? What don’t you like? What do you want to see that you haven’t seen before? Who knows, maybe some developers out there are listening…
Until next time,
Daniel DiCicco is the developer of the space 4X game StarDrive, due to release in Q1 2013. You can know more about Dan and find more tips on game development – especially concerning space 4X games – by having a look at our interview, where Dan talks about StarDrive, Kickstarter and indie games development.Subscribe RSS
- StarDrive Has Reached Its Funding Target!
- Starship Corporation Funding Campaign Complete
- StarDrive: Beta Pre-Orders Pushed to Beginning of 2013
- StarDrive Alpha Funding Starts at Desura
- StarDrive 2 Officially Announced