• Create Account

## How to make a AI choose?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

18 replies to this topic

### #1Scouting Ninja  Members

2913
Like
2Likes
Like

Posted 13 August 2014 - 04:25 PM

I have very little experience in AI programing and was wondering how I would make the AI choose the best option for it's self.

example:

Small Sword     = 1   dmg   /cost: 1 point

Medium Sword = 5   dmg    /cost: 5 points

Large Sword    = 15 dmg    /cost: 10 points

Total points = 12

So any player seeing this will know that the large sword is the better option, but I want the AI to know this.

I can't just tell it to use the highest costing attack because the values will be random.

I don't only want it to compare attacks it should also be able to decide what enemy to attack if there is more than one?

3523
Like
4Likes
Like

Posted 13 August 2014 - 04:43 PM

Enter utility-based AI.

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC

Professional consultant on game AI, mathematical modeling, simulation modeling
Co-advisor of the GDC AI Summit
Co-founder of the AI Game Programmers Guild
Author of the book, Behavioral Mathematics for Game AI

Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

### #3Scouting Ninja  Members

2913
Like
1Likes
Like

Posted 13 August 2014 - 04:54 PM

Enter utility-based AI.

Thank's this does look like what I want, but it seems like a lot to learn.

Will I really need to know this for a gaming AI?

### #4Norman Barrows  Members

6334
Like
6Likes
Like

Posted 13 August 2014 - 05:22 PM

POPULAR

you want the AI to select the weapon with the best value, where value = dmg/cost.

small and med swords: value = 1.

lg sword: value=1 and 1/3.

select the long sword!

just like a search for closest target, where you loop through the targets calculating distance and storing the best distance and best target and then return the best target,

but you loop through the weapons, calculating the values, and storing best value and best weapon, and return best weapon as the result.

bestwpn=none

bestvalue=0

for a = 0 to maxwpns

if wpnvalue of wpn a > bestvalue

bestwpn=a

bestvalue=wpnvalue of wpn a

return bestwpn

this generic "best search" algo is a _TRUE_  game design pattern.

its used in a billion places, in a billion ways, in a billion games (so to speak).

Edited by Norman Barrows, 13 August 2014 - 05:28 PM.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

### #5Scouting Ninja  Members

2913
Like
0Likes
Like

Posted 13 August 2014 - 05:33 PM

you want the AI to select the weapon with the best value, where value = dmg/cost.

This does seam a lot simpler and I guess for healing items mixed in I could check -1*dmg.

Thanks for all the help, it seams that I can work something out from here.

### #6Norman Barrows  Members

6334
Like
1Likes
Like

Posted 13 August 2014 - 05:56 PM

This does seam a lot simpler and I guess for healing items mixed in I could check -1*dmg.

are you selecting items to use, or buy?

for use, you first want to check your state:

1. i need to retreat and heal, or

2. i'm still ok, press on with the attack.

in case 1, you select the best healing item.

in case 2 you select the best weapon.

but you want a layered approach:

1. decide what in general you need to do  ( attack vs heal).

2. decide the particulars of how to do it  ( use best search algo to select best wpn or best healing item).

so your AI might first decide on attack vs heal, then invoke one of two sub-AI's: one that specializes in attack, the other in healing.

divide and conquer.

Edited by Norman Barrows, 13 August 2014 - 05:59 PM.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

### #7Scouting Ninja  Members

2913
Like
3Likes
Like

Posted 13 August 2014 - 06:08 PM

but you want a layered approach:

So It should be similar to a card battle game phases?

First phase:

Check to heal or attack.

Check target.

Second phase:

Check for most effective attack or ability.

Last phase:

Check to repeat First phase or pass.

With each phase based on the "best search" code.

Maybe even a sub step to decide what effect like poison or stun is best?

### #8Norman Barrows  Members

6334
Like
4Likes
Like

Posted 14 August 2014 - 07:34 AM

So It should be similar to a card battle game phases?

not too familiar with those, but i get the idea, similar to multi-phase combat turns in tabletop wargames.

phases or layers, whatever you want to call it, its a MULTI-STEP process.

you basically follow the line of logic you yourself would use in combat if you had the luxury of time to contemplate your move.

first, you'd see what condition you were in, whether you had to heal or not.

if you don't have to heal, you might then check for insurmountable odds, calling for a retreat.

if you don't have to heal or retreat, then attack.

the result of this first "layer" or "phase" of decision making results in one of three outputs:  heal, retreat, or attack.

you then call the appropriate specialty AI subroutine: heal, retreat, or attack. these would be your second layer or phase of AI.

lets say you called the attack subroutine as an example.

the attack subroutine would select a target and a weapon, steer and move the unit, and trigger the attack action at the appropriate moment.

the attack subroutine might in turn call a lower level AI subroutines:  one for select target, one for select weapon, one for steering, one for movement, and one for triggering the attack. these would be your third  layer of AI. some of these types of routines, such as select weapon, might use the best search algo.

the same idea would hold for the healing AI. it would have a lower level "select healing item" subroutine, the same way the attack AI has a "select weapon" subroutine.

so the top later would be the "what should i do?" AI: attack, heal, or retreat.

the second layer would be the attack, heal, and retreat subroutines.

the third layer would be the low level routines like select weapon, target, healing item, etc.

a more concrete example might help. i'll post something from Caveman.

Edited by Norman Barrows, 14 August 2014 - 07:44 AM.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

### #9Norman Barrows  Members

6334
Like
5Likes
Like

Posted 14 August 2014 - 08:55 AM

POPULAR

this is psuedo code of the AI for a friendly NPC in Caveman. i've posted it as pseudo code for clarity, and omitted irrelevant stuff like "are they active?", "are they alive?", "are they an avian and use their own special AI?", etc:

at the very top level it loops through the units and calls the AI routines for each unit:

for a=0 to maxunits

setstate(a)

runstate(a)

setstate figures out whats going on, and what the unit should do, similar to the top level attack, heal, or retreat AI in the previous example.

runstate does the work of what needs doing.

setstate:

if they're resting, continue resting.

else if they're fatigued, rest.

else

select best weapon

if taking fire

select target

if they have a target

if half dead, flee. else attack.

else (no target) flock

else if cornered, respond to being cornered.

else if in collision recovery, continue collision recovery.

select target

if has target, attack.

else graze (wander, flock, or stand, at random).

this results in one of the following states as output:

rest, flee, attack, cornered, collision recovery, wander, flock, or stand.

runstate:

if in collision recovery, do collision recovery AI.

else if stand, do nothing (just recover some fatigue from not moving).

else if wander, steer unit in wander direction and run.

else if flee, steer away from target and run.

else if flock, steer unit towards leader and run.

else if cornered, do cornered AI.

else if attack:

if half dead, steer unit away from target, else steer unit towards target.

move towards/away from target (speed based on range when moving towards target).

attack when in range and target is in arc of fire.

else if rest, decrease fatigue.

collision recovery AI:

simple "bang and turn" behavior. the unit has collided with something. recovery consists of turning back to the left or right at random, and moving off for a short distance before resuming normal movement.

cornered AI:

the unit is cornered between hostile units and terrain obstacles. select closest target and attack. note that surrender might be another option you could add to your game if the unit is cornered. In Caveman, you can attack to subdue, causing the hostile to flee, but you can't capture prisoners. And hostiles never surrender, they always try to flee.

setstate is the top layer

runstate is the second layer

things like select target, steer unit, move unit, and select weapon are the third layer

any subroutines called by select target, steer unit, move unit, select weapon, etc would be a fourth layer of AI.

the best search algo is used for select target and select weapon. its also used to automatically calculate best armor.

the end result is a layered hierarchy of expert systems.

this is just one of over a dozen types of AI in Caveman, each with its own setstate routine.

runstate is used by all the different types of AI, and actually supports 17 different states, out of which seven are used by the NPC AI.

Edited by Norman Barrows, 14 August 2014 - 09:09 AM.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

3523
Like
2Likes
Like

Posted 14 August 2014 - 03:59 PM

Enter utility-based AI.

Thank's this does look like what I want, but it seems like a lot to learn.

Will I really need to know this for a gaming AI?

It is the fastest-growing AI architecture out there. Between behavior trees and utility systems, you can handle most of the AI needs out there.

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC

Professional consultant on game AI, mathematical modeling, simulation modeling
Co-advisor of the GDC AI Summit
Co-founder of the AI Game Programmers Guild
Author of the book, Behavioral Mathematics for Game AI

Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

### #11LAURENT*  Members

656
Like
0Likes
Like

Posted 14 August 2014 - 04:44 PM

Alright so AI is basically logic. Unless you have some syntax or impanation problem you need to set priorities and think about how you want the AI to think.

Here is something I would try.

First I would read the health of the player to see what is best suited for killing them.If the player has really high health and a single big sword swing won't give the AI an advantage I would make it choose second best to save points If the player is on it's last leg about to die I would have the AI go all out to ensure the player will die If the player has a chance to be killed by a big sword swing but the AI doesn't have enough points I would make it choose the light attack to build up quickly

AI is just logic and you should think about what's the most effective way to get the result you want which mainly involve killing the player.

### #12Scouting Ninja  Members

2913
Like
0Likes
Like

Posted 15 August 2014 - 04:54 AM

Thank's everyone for all the help.

I now have a npcs that walk around and can engage in combat, the layered approach produced a great AI with out too much of a struggle.

It is the fastest-growing AI architecture out there. Between behavior trees and utility systems, you can handle most of the AI needs out there.

I have been reading up whenever I can, it's been really interesting.

I hope to implement it into my game at some time, just need to learn more.

First I would read the health of the player to see what is best suited for killing them.If the player has really high health and a single big sword swing won't give the AI an advantage I would make it choose second best to save points If the player is on it's last leg about to die I would have the AI go all out to ensure the player will die If the player has a chance to be killed by a big sword swing but the AI doesn't have enough points I would make it choose the light attack to build up quickly

I will consider this for a hard mode.

At the moment my AI always makes the best attack choice, making it really hard to win when you accidentally selected the wrong option or when you are out numbered by even one enemy.

To solve the problem I weakened the attack of regular enemy's, it's still the same for boss battles or important battles, on easy the opponent has a one out of five chance of choosing the worst possible attack.

### #13wodinoneeye  Members

1669
Like
0Likes
Like

Posted 20 August 2014 - 08:19 AM

Game mechanics are also often more complicated

Such as : the large swords swings slower - taking more time for the complete cycle, thus it is not  damage-per-swing but damage-over-time.

This also may tie into other game mechanics like having to do another action quickly and that longer swing now delays doing some other needed operation quickly.

Another might be BIG swords take 2 hands when with a small the other hand could hold a shield or second weapon.

The bigger sword may have more reach than a smaller one or it may deliver more power to a strike that isnt soaked off as much by certain kinds of armor (or be more useful to block strikes from a equivalent large sword because of its mass)

DIfferent swords often have slash moves or thrusting moves

A smaller weapon can be handier in a close fight.

Consider similarly for other weapon types available.

So the decision can be more complex and be effected by the situation (and thus the logic growing quite a bit more)

--------------------------------------------Ratings are Opinion, not Fact

### #14Tutorial Doctor  Members

2564
Like
3Likes
Like

Posted 20 August 2014 - 08:43 AM

I have made it rather obvious that I am a major fan of fuzzy logic, hehe.

The term "better" is a fuzzy term. What is better in one case, might not be better in another case. Perhaps in CQC the short sword is better, and at a longer distance, the larger sword is better.

So you have two sets. A long distance set, and a short distance set. In the set of "better at long distance", the short sword will have the mebership value of 0, and the long sword will have the membership value of 1. Any sword with a length in-between will have varying "belongingness" to the set (they will be more or less better for long distance attacks.)

Then you have the short distance set where the short sword has a membership value of 1, and the long sword has a mebership value of 0.

So any new sword will be both better and worse for any given situation, depending on the situation.

They call me the Tutorial Doctor.

3523
Like
1Likes
Like

Posted 20 August 2014 - 05:18 PM

I have made it rather obvious that I am a major fan of fuzzy logic, hehe.

"Utility" == "fuzzy"

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC

Professional consultant on game AI, mathematical modeling, simulation modeling
Co-advisor of the GDC AI Summit
Co-founder of the AI Game Programmers Guild
Author of the book, Behavioral Mathematics for Game AI

Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

### #16Álvaro  Members

20266
Like
2Likes
Like

Posted 20 August 2014 - 05:27 PM

"Utility" > "fuzzy"

;)

3523
Like
2Likes
Like

Posted 21 August 2014 - 10:32 AM

"Utility" > "fuzzy"

;)

I approve of this.

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC

Professional consultant on game AI, mathematical modeling, simulation modeling
Co-advisor of the GDC AI Summit
Co-founder of the AI Game Programmers Guild
Author of the book, Behavioral Mathematics for Game AI

Blogs I write:
IA News - What's happening at IA | IA on AI - AI news and notes | Post-Play'em - Observations on AI of games I play

"Reducing the world to mathematical equations!"

### #18ddyer  Members

259
Like
0Likes
Like

Posted 06 October 2014 - 09:33 PM

Just try them all

In complex games with many arbitrary factors, using a MCTS (Google it) search can be very effective.

You don't personally have to have any idea what strategy is best, and you won't have to rejigger

your whole strategy every time something changes in the game.

---visit my game site http://www.boardspace.net - free online strategy games

### #19vvv2  Members

285
Like
0Likes
Like

Posted 07 October 2014 - 02:41 AM

Just try them all

In complex games with many arbitrary factors...

Hi,

I usually use a different goal functions and different goal value for different situations and different strategies. This function is usually selected from a possible goal value in the knowledge base table.

Thanks.