• Advertisement
  • entries
  • comments
  • views

GC In-Depth Part 1: The AI Behind GC

Sign in to follow this  



So for two hours this morning I was reading through the new TGB docs and getting GC up and running with Beta1.1. It gave me a little trouble at first - it would load the game but would keep displaying the TGB level builder instead of the main GC menu. Finally fixed it and now GC is running perfectly - I don't even have to use a .bat file to launch the game anymore. Awesomeness. They also added two awesome new docs, one on input handling and another on Torque Script. The reference doc was also updated - they have a pathing object! And a trigger object! I love this engine...

So anyways I still have a few errors and warnings that pop up in the console that don't actually affect the game at all it seems but I'd still like to know how to get rid of em - however I'm still waiting for a response to my thread on the GG forums.

Now onto the meat of this entry, Part 1 of 3 in which I'll be discussing in-depth the future development of GC, from the game's AI, to its tutorial to its first public demo.

The AI

The AI for the game is basically broken down into three categories:

  • Tactics - This is the AI's ability to think about the decisions it makes in terms of placing planets, using special actions, and it's overall ability to read the opposing player(s) strategies.

  • Stance - This defines the AI's overall strategy. There are three stances that the AI can adopt at any given time

    • Defensive - The AI tries to limit contact with other players and build up its current holdings, only letting itself be drawn out if someone repeatedly attacks it. In this mode the AI's strategic ability is at its greatest

    • Passive - The AI works to expand its presence on the map but does so indescriminately, defending itself when neccessary. In this mode the AI's strategic ability is balanced.

    • Agressive - The AI tends to relentlessly attack a single opponent but switches focus easily if threatened by another player. In this mode the AI doesn't think very strategically.

  • Difficulty - This defines how the AI ultimately makes decisions after tactics and stance are calculated and taken into account. The higher the difficulty the more likely the AI is to make the right choice


Stance is the underlying behavorial factor for the AI, so we'll discuss it first. The first thing to note is that the stance of an AI is fuzzy. There are three stances but you could consider them each a third of the AI's total "stance value", so to say. So a completely defensive AI would have a stance of 0, while a completely offensive AI would have a stance of 10. In between you can have a slightly defensive stance of 3.72 a passive stance of 5 or a slightly aggresive stance of 6.76. I'm not sure if these will be the actual numbers but I just want to get across the idea of stance as a scale from whatever to whatever.

Stance is assigned randomly to the AI at the start of the game, and from there it is influenced by the other player(s), both human and AI alike. The response is simple - the more an AI is attacked the more aggressive it gets, the less an AI is attacked the more passive it gets, and the more the AI loses the more defensive it gets. An AI will get aggressive faster if attacked by a single player than if attacked by multiple players. It will also get defensive faster if attacked by multiple players than if attacked by a single player.

Certain "triggers" will also have an instant effect on stance. For example if the AI is bordering on defeat, it may switch to full-on aggression in the hopes of maybe making a comeback. A sudden loss of a lot of planets could force the AI straight into a defensive stance. The defeat of another player could switch an AI's stance to straight-up passive. Once a trigger is activated the stance once again starts to become influenced by the environment.


This is the real meat of the AI, although its effectiveness is ultimately dependent upon the current stance the AI holds. The more defensive an AI is, the more it will take time to think things all the way through while the more aggressive it is, the less it will think of the consequences of its actions. The main goal of the game is to take over all other player's planets, and so this is the AI's primary focus. The only way stance affects this goal is that a more agressive AI will usually focus on a certain player's planets, a passive AI won't care what type of planet it takes over as long as it takes one, and a more defensive AI will choose a player that's less likely to be able to strike back quickly.

So since taking over a planet is the main goal no matter what, the first thing the AI has to do is determine which of its planets on the board it can use to take over another planet. Step One is to determine which planets belong to it. It tracks this data in an array

1 3 2 2
1 0 2 -1
1 0 0 -1
1 3 1 1

So in the above small-sized 4x4 map, all the 0's are systems that are empty or unoccupiable, and all the -1's are systems that were removed to create an irregular map (regular being square). The systems that belong to the player are identified by matching up to the player's number (1-4). We'll say that this AI is Player 1.

The next array it tracks contains values that show how close a system is to exploding

1 2 2 2
3 5 2 -1
1 -1 5 -1
2 1 1 1

These numbers represent the number of planets left to place in the system before it explodes (this includes solar bodies, which can have remainder count as high as 6), so the closer to 0 the number is the closer the system is to exploding. In this data, -1 refers not only to removed systems, but systems that can't hold planets, like black holes and asteroid fields.

What the second array does is allow the AI to perform simple simulations based on the first array. So for example the AI knows from the first array that it owns the top-left system. So it drops a planet there (in the simulation). This subtracts 1 from the spot in the second array.

0 2 2 2
3 5 2 -1
1 -1 5 -1
2 1 1 1

Since the result is 0, the AI determines (upon querying the map) that this was a planet, not a solar body, resets the system (since it's a corner system it only needs three planets to explode) and subtracts 1 from the adjacent systems

3 1 2 2
2 5 2 -1
1 -1 5 -1
2 1 1 1

The AI then updates the first array, placing a 1 (or whatever the player's number is) in the systems it just expanded into

1 1 2 2
1 0 2 -1
1 0 0 -1
1 3 1 1

Now the AI rechecks the second array. Since no zeros are detected, the simulation is over and the AI analyzes the result.

The analyzation determines 3 things:

  • Number of systems controlled - this is calculated simply by adding up the number of systems from the first array that match the AI's player number

  • Number of players taken over - this is tracked throughout the simulation and records how many different player's planets were taken over. i.e 2

  • Number of planets taken over per player - also a tracked value, it records how many planets per each player were taken over in a 2D array - [player number, # of planets]

Now that the AI has determined the outcome of the action it has to review the position it finds itself in and score itself. This is determined by the following:

  1. The more systems controlled the better

  2. The more special systems controlled the better

  3. The more systems controlled with fewer adjacent systems the better

  4. The more enemy systems adjacent to friendly planets that are less-likely to explode the better

  5. The more friendly systems ready to explode that are adjacent to enemy systems the better

  6. The more players it's managed to take over at once the better

  7. The more friendly planets adjacent to each other the better

  8. The more planets taken over the better

Each of these categories adds up to the final "grade" of the move, and each category is weighed differently depending on stance. For example an aggressive AI would care less than a defensive AI about category 4, whereas a defensive AI would care less about category 1 than an aggressive AI.

So this check is done for every system on the map the AI can place a planet in.


Now the difficulty finally factors in. As I mentioned earlier, an AI of higher-difficulty will choose the best choice more than an AI of lower-difficulty. What happens is that after the tactics are considered, the AI has a grade for every system on the map. Systems with a higher grade are better choices for the AI to consider. So the AI simply looks at values based on difficulty. So say a map has a range of values from -25 to 34. Only the lower 20% of these values would be considered by the lowest-difficulty AI, with a 1 in 10 random chance of the AI picking from a higher range of values. The middle-difficulty AI would choose from the range of 20% - 50% of values with a 1 in 5 chance of picking from a higher range of values and a 1 in 5 chance of picking from a lower range of values. The higher-difficulty AI would choose from 50% to 85% of values with a 1 in 7 chance of picking from a lower range and a 1 in 10 chance of picking from a higher range. The highest-level difficulty would choose from 85% - 100% and have no chance of ever picking a lower ranged value.

Again these numbers are not the actual numbers, but they are close to what I have in mind and are there to give you an idea of what I mean.

The Final Move

If an AI has the ability to perform special actions at the end of its turn, it will take advantage of it, although how much depends again on its stance. An aggressive AI is more likely to use up as many actions as possible (up to 3 allowed per turn) each time it gets the chance while a more defensive AI would use them only if it sees enough benefit, like preventing an attack with a missile launch or planetary shield or destroying a lot of enemies with a supernova bomb. Each special action is given its evaluation based on the stance of the AI and the current status of the game board. Special actions do not play a direct role in the AI's decision to place a planet because they are not directly linked anyways to the planet-placing takeover gameplay, they are merely there as a means to further take out action against other players after you have placed a planet.


This was more a stream of consciousness entry than anything else - a lot of what I wrote above could change or not come to be as the AI is developed, but it's the first time I've put my thoughts down so it's a start. I'll be following the development of the AI closely as I work on it, so rest assured more on this is to come. But for now I need a break and time to ponder tomorrow's entry on the game's tutorial.

Feedback welcome [grin]
Sign in to follow this  

1 Comment

Recommended Comments

Yes, thats what I'm talking about! I've been thinking of ways to do the AI for our game, and what I really needed was another person's ideas to listen to. I'll definitely be tuning into you journal[grin]

Share this comment

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement