If i don't have AI designer tools (FSM,BT,Goap) , i code AI behaviour in a way inspired by Goap AI system
(It's inspired, but it's not Goap , there is no weights , sequences and some other things)
This is a work in progress example you can get inspiration, the script is not complete and working.
Compared to standard linear code, this way of coding decreases a lot the complexity and make it lot more
adaptable and expandable for any other AI, and lot more easy and clear to read.
Inspired by Goap,the code is separated in modules :
- Event modules
- Detection modules
- Condition modules
- Action modules
Event modules :
They can be external events, timers or triggers altering AI variables, so they can directly influence what actions should trigger.
For example the player doing damage to AI character, this would set a targetDetected flag to true, even if the character AI would be far from player.
Detection modules :
This is the AI detection sensors.
This can be distances checking, field of view, triggers or anything else.
Condition modules :
This is checking conditions for each actions available, the result is one action is activated.
Action modules :
They are actions the AI can perform.
For example wander, chase, attack or anything else.
- Code: Select all
ox,oy,oz = 0;
obj = 0;
dccID = 0;
playerKo;
health = 300;
prevHealth = 300;
ko = false ;
targetDetected = false;
meleeRange=false;
inRangeDistance = 10;
meleeRangeDistance = 3 ;
state_actual ;
state_wandering = "state_wandering";
state_attacking = "state_attacking";
state_chasing = "state_chasing";
wanderDestination ;
function onInit(objID)
obj = objID;
wx,wy,wz = entity.getPosition(obj);
CURRENT_ANIM = IDLE_1;
end
function onUpdate( td )
isKo();
if(! isKo && !playerKo ) then
Detect() ;
conditionsCheck();
Actions();
end
if(! isKo && playerKo ) then
wander();
end
end
-- Detections
function Detect()
isInRange();
inMeleeRange();
end
--Conditions
function conditionsCheck()
conditions1();
end
--Actions
function Actions()
wander();
chase();
attack();
end
----------------------------------------------
-- Events modules
----------------------------------------------
--called by player or other objects events
function damagedByPlayer()
if( health <= prevHealth) then
targetDetected = true;
prevHealth = health;
if(health <=0 ) then
isKo = true;
end
end
end
----------------------------------------------
-- Detection modules
----------------------------------------------
function isInRange()
if(distanceToPlayer <= inRangeDistance) then
targetDetected = true;
end
end
function inMeleeRange()
meleeRange = (distanceToPlayer <= meleeRangeDistance) ;
end
----------------------------------------------
-- Conditions check modules
----------------------------------------------
function conditions1()
if(!detected) then
wander();
end
if(detected && !meleeRange)
chase();
end
if(detected && meleeRange ) then
attack();
end
end
----------------------------------------------
--Actrion modules
----------------------------------------------
function wander()
changeState(state_wandering, animation_walk);
getNewWanderPosition();
moveToWanderPosition();
end
function chase()
changeState(state_chasing, animation_run);
gotoplayer();
end
function attack()
changeState(state_attacking, animation_attack);
--timer
--doDamage()
end
----------------------------------------------
-- Other functions
----------------------------------------------
function changeState(newState, newAnimation)
if( state_actual != newState) then
state_actual = newState;
playAnimation(newAnimation);
end
end
function getNewWanderPosition()
if( distance(position, positionGoal) <= 1 ) then
positionGoal = getNewRandomPosition(positionOrigin,radiusMin,radiusMax);
end
end
I am making a zombie AI based on that system, so i'll make it available and could be included in Skyline scripts as a template.