How to make a AI choose?

Started by
17 comments, last by vvv2 9 years, 6 months ago

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?

Advertisement

Enter utility-based AI.

Dave Mark - President and Lead Designer of Intrinsic Algorithm LLC
Professional consultant on game AI, mathematical modeling, simulation modeling
Co-founder and 10 year advisor of the GDC AI Summit
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!"

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?

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).

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php


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.


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.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php


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?


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.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

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.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

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-founder and 10 year advisor of the GDC AI Summit
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!"

This topic is closed to new replies.

Advertisement