Log in

View Full Version : AI implementation



Tamur
10-07-2004, 19:18
hi all,

Having tried to digest all the different issues that folks are having with the AI, and seeing a number of suggestion, I thought it might be good to give the basics of how game AI is implemented, so that suggestions can be put in the same language that the dev's are speaking.

The idea that sometimes floats around is that AI can be defined by the programmer. An example is:



ourUnit = this unit
otherUnit = SeenUnit

if IsEnemy(otherUnit) && Strength(ourUnit) > Strength(otherUnit)
{
Attack(otherUnit)
}
else
{
ourUnit.RunAwayFrom(otherUnit)
}


This sort of code tells the computer what to do in a given situation. While straightforward, this is not artificial intelligence. This is simply a behaviour rule that the computer lives by, time and again. Every time it sees a weaker enemy unit, it will attack. Otherwise, it will retreat.

The basis of true AI, on the other hand, is that the computer can both 1) use a base set of "facts", which are the seed of its intelligence, to generate higher-order facts based on the answers to the base facts, and 2) act according to all the facts it has been able to generate.

An example, to take this out of the abstract. We'll stick with the above example for clarity.

Say the programmer has defined these base facts defined (along with the actions Combine(x,y) and Attack(x,y):



strength(x) = calculated strength of army x
isNear(x,y) = if y is in the current turn's movement path of x,
then x is near y
isEnemy(x) = if x is from an enemy faction, then this is true for x
combinedStrength(x,y) = Strength(x) + Strength(y)


These are all hard, discernable facts about the situation, and all the computer needs to be able to make a decision.

The computer then uses rules of combination to make higher-order facts out of these base facts. For example, it could have a rule that makes a fact which defines whether or not it can carry out all possible actions. This would result in:



canCombine(x,y) = if isNear(x,y), then this is true for x and y
canAttack(x,y) = if isEnemy(y), and isNear(x,y), then this is true for x
canRetreat(x) = if x can move somewhere without running into
an enemy army, then this is true for x


Further, the computer may have a rule of combination that weighs the relative ease with which an action can be taken. This would yield:



canSuccessfullyAttack(x,y) = if canAttack(x,y) is true and
strength(x) >= strength(y)


So on and so forth...

The programmer does not define the reactions of the computer. The programmer simply defines the parameters by which the computer can make its own decision.

This just the basic, basic side of AI. There are many different ways to implement AI, many of which take significant computing power. For the campaign map, this is not an issue. But for the battle map, something like a neural network would be a big CPU hog.

Err... so that's about it. I'm sure there are those around here would could explain it far better, and get into the details. But hopefully this helps folks understand what's involved when you're requesting changes to the AI.