{"id":8347,"date":"2012-12-28T14:03:40","date_gmt":"2012-12-28T19:03:40","guid":{"rendered":"https:\/\/www.spacesector.com\/blog\/?p=8347"},"modified":"2012-12-29T13:48:51","modified_gmt":"2012-12-29T18:48:51","slug":"ai-design-in-4x-games-an-overview","status":"publish","type":"post","link":"https:\/\/www.spacesector.com\/blog\/2012\/12\/ai-design-in-4x-games-an-overview\/","title":{"rendered":"AI Design in 4X Games &#8211; An Approach"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8348\" title=\"AI Design in 4X Games - An Overview\" src=\"https:\/\/www.spacesector.com\/blog\/wp-content\/uploads\/ai_4X_games_overview_2.jpg\" alt=\"AI Design in 4X Games - An Overview\" width=\"600\" height=\"338\" \/><\/p>\n<p>My name is Daniel DiCicco and I am the developer of <a  href=\"http:\/\/www.stardrivegame.com\/\" target=\"_blank\">StarDrive<\/a>, a 4X space game.<\/p>\n<p>This article is an overview of the general approach we have taken with StarDrive\u2019s 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 \u201chierarchical\u201d 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 \u201cGoals\u201d which act in a dual role as scripts and memories for AI behavior.<\/p>\n<h3>A few game design basics<\/h3>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>The second game design basic to understand is that this Game State is updated at a fixed interval &#8211; 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 &#8211; the nebulas swirl, a little light blinks on and off &#8212; whatever. It\u2019s 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.<\/p>\n<p>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?<\/p>\n<h3>AI in StarDrive and the hierarchical\u00a0AI concept<\/h3>\n<p>In StarDrive I have a big boss AI that I call the GSAI &#8211; 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.<\/p>\n<p>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 &#8211; the Expansion Manager. The Expansion Manager is a limited little fellow. He can\u2019t 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.<\/p>\n<p>And so every update, the GSAI will tell the Expansion Manager &#8211; \u201chey, I want to have 2 (or however many) plans underway to colonize new planets. Take care of it.\u201d The Expansion Manager then creates \u201cGoals\u201d 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\u2019t, 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\u2019t gotten its hands dirty with the details.<\/p>\n<p>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 &#8211; a Force Pool Manager and a Defensive Coordinator &#8211; 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\u2019re missing some ships then they\u2019ll create a few private goals to get those ships made.<\/p>\n<p>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.<\/p>\n<p>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\u2019s 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!<\/p>\n<h3>At the core of the AI are Goals<\/h3>\n<p>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\u2019s thinking. If we want some new behaviors out of a ship\u2019s 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.<\/p>\n<p>At the core of all of these manager systems is the concept of \u201cGoals\u201d. The GSAI tells the Managers what to do and then the Manager creates \u201cGoals\u201d, which are basically like scripts. The Manager knows what it needs to do to execute the goal and tracks the goal\u2019s progress in steps. For instance, the Expansion Manager might follow logic like this when evaluating its assigned goal of colonizing a planet:<\/p>\n<p>Steps:<\/p>\n<ol>\n<li>Find a planet to colonize<\/li>\n<li>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.<\/li>\n<li>Check if we have requested a new colony ship. If not, create a colony ship goal. Next update, Go to step 2.<\/li>\n<li>Now that we have a ship, order the unit AI to go colonize that planet.<\/li>\n<\/ol>\n<h3>What would the human player do?<\/h3>\n<p>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.<\/p>\n<p>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 \u201cGive me 5 ships!\u201d 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.<\/p>\n<p>Memories also play a crucial role in human decision making. If an AI has been a huge jerk to us then we\u2019re going to remember it. We\u2019re going to remember that they took our planet and we\u2019re 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\u2019s analysis, this is the creative work that the developer has to undertake.<\/p>\n<h3>Final thoughts<\/h3>\n<p>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\u2019s to make a fun game.<\/p>\n<p>What\u2019s fun, well that\u2019s a whole different can of philosophical worms. I believe that would be an excellent topic for conversation here &#8212; what do you find fun in AIs from 4x games? What don\u2019t you like? What do you want to see that you haven\u2019t seen before? Who knows, maybe some developers out there are listening&#8230;<\/p>\n<p>Until next time,<\/p>\n<p>Dan.<\/p>\n<p><em>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 &#8211; especially concerning space 4X games &#8211; by having a look at our\u00a0<a  href=\"https:\/\/www.spacesector.com\/blog\/2012\/11\/interview-with-daniel-dicicco-on-stardrive-kickstarter-and-indie-game-development\/\" target=\"_blank\">interview<\/a>, where Dan talks about StarDrive, Kickstarter and indie games development.\u00a0<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This article is an overview of the general approach we have taken with StarDrive\u2019s AI and is meant to provide some insight into the process of designing an AI for games of this genre.<\/p>\n","protected":false},"author":1074,"featured_media":0,"comment_status":"registered_only","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[177,1],"tags":[1185,1502,1174,636],"class_list":["post-8347","post","type-post","status-publish","format-standard","hentry","category-game-design","category-space-strategy-games","tag-artificial-intelligence","tag-game-design","tag-ideas-concepts","tag-stardrive"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.spacesector.com\/blog\/wp-json\/wp\/v2\/posts\/8347","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.spacesector.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.spacesector.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.spacesector.com\/blog\/wp-json\/wp\/v2\/users\/1074"}],"replies":[{"embeddable":true,"href":"https:\/\/www.spacesector.com\/blog\/wp-json\/wp\/v2\/comments?post=8347"}],"version-history":[{"count":0,"href":"https:\/\/www.spacesector.com\/blog\/wp-json\/wp\/v2\/posts\/8347\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.spacesector.com\/blog\/wp-json\/wp\/v2\/media?parent=8347"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.spacesector.com\/blog\/wp-json\/wp\/v2\/categories?post=8347"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.spacesector.com\/blog\/wp-json\/wp\/v2\/tags?post=8347"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}