Frogboy Frogboy

AI Wars

AI Wars

To best understand the AI of v1.1 one must first understand the nature of threads.

In a normal game, the players all function in a single thread. There is a while loop in which various components of the game are given the opportunity to to do their part – draw something to the screen, look for player input, update the game state and of course, move the computer AI.

In most games, including v1.0, the AI does all their work at the end of a turn.  This is why in most games you hit the turn and there is a delay, the AI is having to think about and plan what they do.

The challenge, of course, is making sure players aren’t waiting too long between turns, especially if the game locks them out of the UI (which is the norm).

But there is another way: Threads.  Threads allow you to have different things computing concurrently. Even on a single-core machine, threads can make a huge difference. Threads are the reason why Stardock games have historically had such a strong reputation for good AI.  The AI can be calculating strategies while the player moves. There can even be background threads that do nothing but look at the game state and set various flags for the AI to act on when the time is right.

Threads are the reason why Galactic Civilizations II was able to have some of its most infamous diplomatic moments such as “So, I see you’re building up quite a fleet there, so let me guess, you’re going to mass up for an attack, take my planet and then sue for peace? Well, sorry but in the meantime, I’ve been putting ships around your ships..”

In the OS/2 version of GalCiv (back in the 90s) that had cloaked ships, players were greeted with the horrific realization that the AI had surrounded them with cloaked ships and preemptively wiped out their massed fleet.  And this was back when the game was made with 486’s in mind.

Threads rule.

So why not threads?

Why do games (including Elemental v1.0 and other major strategy games of 2010) not use multithreaded AI? Debugging. If multiple things are touching bits of memory, modifying variables, creating or deleting objects it can be a real pain to coordinate.  The bigger the team, ironically, the harder it is to do. 

Plus, truth be told, most people just aren’t that comfortable juggling many threads. It requires a different way of solving a problem. It’s a bit like recursion, they can be hard to envision if you’re not used to it. 

On Elemental, I didn’t have the opportunity to go into that.  On our other games, the only coding part I was responsible for was the AI (except our earliest games).  On Elemental, things went somewhat..differently. By the end of the project I had added lead developer, producer, and design implementer to my hats so the AI had to be done in the more traditional way.

Now that we have Kael to take charge of production, Toby as design implementer and CariElf back as lead developer, I can do for Elemental what I do best: Multithreaded AI development.

Threads: Breaking a problem up into components

When you play a game, what are the things you do? What are the mistakes typical in computerized opponents?

The most obvious is that you’re looking and thinking about your strategy all the time.  So let’s look at the threads we should have in Elemental:

1. Global Strategic AI thread. This is little more than one big while loop that sits in a thread. It’s very low priority and all it does is look at the map and see what sorts of pre-designed situations have been triggered.  Think of it as pattern recognition. If it recognizes a pattern, it sets a flag and the AI in question then begins to deal with it.  This is where players can have the most impact by telling me what sorts of cheese they do to thwart the AI.

If I were writing the Starcraft AI, for instance, this thread would be the one seeing if I’m about to get baneling rushed or if photon canon rushed and deal with it. 

It also sets flags on known items as to whether they should be targeted for control, imbued, recruited, killed, etc.

This is NOT like an expert system or a neural net because the AI is not learning. It is adapting to player strategies but its choice of adaptations is limited to what we (the Elemental community and I) come up with.  Eventually I hope to export this as a C++ DLL for others to play with or better yet make it available in Python but that’s aways off.

2. Player Strategic AI thread. This is the thread that handles deciding what should be built, what strategies it should employ, where it should send its soldiers, what spells it should cast, what type of unit it should design, what technology it should research, what city improvement should be built.

3. Tactical Strategic AI.  The tactical battles are the one part of the game I’m not involved in yet. We have two capable developers handling that.  I won’t be taking that over until the expansion pack.  But that will become another AI thread down the line as a background thread looks at every army group and sets what types of strategies they should employ generally as well as potentially against any enemies that are near by.

Testing the AI

This is my AI map for v1.1 Beta 1. The goal with this map is to teach the AI how to play the game effectively using the v1.1 game mechanics. 

 

image

This battle will be the subject of my  next journal entry.

73,116 views 30 replies
Reply #26 Top

Quoting DKL, reply 24
Why do games (including Elemental v1.0 and other major strategy games of 2010) not use multithreaded AI? Debugging. If multiple things are touching bits of memory, modifying variables, creating or deleting objects it can be a real pain to coordinate. The bigger the team, ironically, the harder it is to do.
That just screams 'Haskell!' and transactional memory
End of DKL's quote

No, it is not that complicated. Note that the threads are only accessing the map as read-only, they are doing background analysis. This is pretty easy to thread. The problem is coming up with, or using such as pattern in the first place. Also note that the global strategic thread is only setting flags to the player strategic thread, this means you again can coordinate the threads without using locking. So, he has designed a threading system that is easy to integrate with existing systems, and doesn't require tons of locks, and which doesn't communicate back-and-forth, thus also eliminating any risk of deadlocks.

Reply #27 Top

How indepth is the AI likely to be getting? Can it change the roles of created units, to adapt them to circumstances?

For example the Roles:

Scout: If the computers field of reference is limited to what they can currently see with active units and bases, they like players will need dedicated scout class forces that will investigate another player unit slipping around  the edge of their view, to follow possible threats out of response range to keep an eye on them without being easily killed.

Explorer: Check out the world and find out where all the cool stuff is.

Capture: Unit allocated to collect a resource, can also be tied to scout or explorer. 

Defensive Reaction Force: Unit or force designated to cover an area and respond to threats for their area.

Aggressive Reaction Force: Unit or force designated to clear an area threaten away enemies on an open approach, chase them down without leaving critiical areas undefended.

Aggressor Forces: Lets go kill still and take over what we can.

Bait: A unit sent out to lure enemys into thinking they can get rid of this pesky thing with no trouble, then lead them into an ambush, where the enemy has no knowledge of where their other forces are becuase they cannot see them.

So if a scout assigned unit realises that the enemy in close proximity, is vunerable to it, it can simple change its role and attempt to kill it, then adapt again as circumstances permit. Or a Capture unit sent out to setup a new town , runs from the enemy by taking up the Bait role and leads them into an ambush.

These ideas can make more room for development idea like camoflage that can hide scouts unless the unit gets very close, and ofcourse things that allow for detecting camofalged units, thus an idea scout would have both, but any unit can be used to scout. This will also allow the computer AI to uutalize its processing and reaction speeds to trick players. Also allowing for semi omnescient map overview without the normal just allow them to know where everything is and respond to it.

Players currently just tend to anticipate that the computer knows where everything is  and assume they will take the fastest possible route to it and ambush them with ease, nothing quite like having the computer bumbling around like a player to make the game harder to anticipate.

 

Reply #28 Top

Note that the threads are only accessing the map as read-only, they are doing background analysis
End of quote

Yeah, generally its not complicated if you omit the fact that map actually changes during analysis, and technically, it can cause errors. While haskell part was obviously a joke, using STM would still let you not care about player's actions while processing map.

Reply #29 Top

Quoting jmarke, reply 1
Spectacular!  The open conversations about development...and implementation of user feedback is exactly why I love Stardock.  Buying a Stardock game, you always feel like you're part of a cool process...and I, for one, thoroughly enjoy knowing my purchase has no ceiling.  Amazing things will continue to be considered and integrated into patches and add-ons.  

 

Thanks for involving us!

 

 
End of jmarke's quote

 

I agree 100%!  I'll always buys SD strategy games, no matter the initial state, because of the respect Brad gives the players.

Reply #30 Top

Very nice info.

What about an AI contest?! Like a competition or something like that.

Cya