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 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.
if they're resting, continue resting.
else if they're fatigued, rest.
select best weapon
if taking fire
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.
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.
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.
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.