I've been working on some simple AI for the last few days, it's supposed to be for zombies. Basically, the object always moves towards the player unless a ray cast between the two is blocked by the map's walls, then the enemy moves towards a waypoint. These waypooints are are places where there are turns in the hallway. It runs at 45 FPS with five enemies without any sliding collision but under 35 FPS when I check collision for more than one enemy. I can include a full copy of the game if people want to run the code, but attached is the function I use for the pathfinding.
This is my first real attempt at waypoint pathfinding and good AI in general. Any feedback on my method would be cool too. I really want the game to have 20 or so of these zombies walking around at once, so performance is a must.
NOTE- the array ai_info(x,y) keeps information for each enemy (the first number of the array for each enemy, the second are listed below...
ai_info(1,0)- object number
ai_info(1,1)- active trigger
ai_info(1,2)- number for destination waypoint
ai_info(1,3)- reaction timer, so that the enemies don't reaction instantly to new player positions
`Enemey AI
Function BioFPS_pathfind(ai_waypoint_amount,ai_amount)
for x=1 to ai_amount
dsqr = sqrdist(object position x(2),object position y(2),object position z(2),object position x(ai_info(x,0)),object position y(ai_info(x,0)),object position z(ai_info(x,0)))
`if you're nearby the enemy, they move towards you
if ai_info(x,1)=0 and SC_rayCast(0,object position x(ai_info(x,0)),object position y(ai_info(x,0)),object position z(ai_info(x,0)),object position x(2),object position y(2),object position z(2),0)=0
ai_info(x,1)=1
endif
`checks to see if they lose sight of you
if ai_info(x,1)>0 and dsqr>3000
ai_info(x,1)=0
temp_point=0
ai_info(x,2)=0
ENDIF
`When active, moves towards player
if ai_info(x,1)=1
if SC_rayCast(0,object position x(ai_info(x,0)),object position y(ai_info(x,0)),object position z(ai_info(x,0)),object position x(2),object position y(2),object position z(2),0)=0
if ai_info(x,3)=0
point object ai_info(x,0),object position x(2),object position y(ai_info(x,0)),object position z(2)
`sets how long it takes for the AI to reset its angle towards player
ai_info(x,3)=100
endif
ai_info(x,3)=ai_info(x,3)-1
if dsqr>250
tempx=object position x(ai_info(x,0))
tempz=object position z(ai_info(x,0))
move object ai_info(x,0),3
SlidingCollision(ai_info(x,0),tempx,object position y(ai_info(x,0)),tempz,object position x(ai_info(x,0)),object position y(ai_info(x,0)),object position z(ai_info(x,0)),70,1,1,0)
if object playing(ai_info(x,0))=0 then play object ai_info(x,0),5
else
`ATTACKING GOES HERE
endif
else
`This is where the AI chooses the waypoint to use for direcetion correction
for a=1 to ai_waypoint_amount
dsqr = sqrdist(object position x(2),object position y(2),object position z(2),ai(a,1),object position y(2),ai(a,2))
if dsqr<temp_point
temp_point=dsqr
ai_info(x,2)=a
endif
if temp_point=0
temp_point=dsqr
ai_info(x,2)=a
endif
NEXT a
ai_info(x,1)=2
endif
ENDIF
`This is where the AI moves towards correcting waypoint
if ai_info(x,1)=2
point object ai_info(x,0),ai(ai_info(x,2),1),object position y(ai_info(x,0)),ai(ai_info(x,2),2)
tempx=object position x(ai_info(x,0))
tempz=object position z(ai_info(x,0))
move object ai_info(x,0),3
SlidingCollision(ai_info(x,0),tempx,object position y(ai_info(x,0)),tempz,object position x(ai_info(x,0)),object position y(ai_info(x,0)),object position z(ai_info(x,0)),70,1,1,0)
if object playing(ai_info(x,0))=0 then play object ai_info(x,0),5
dsqr = sqrdist(object position x(ai_info(x,0)),object position y(ai_info(x,0)),object position z(ai_info(x,0)),ai(ai_info(x,2),1),object position y(ai_info(x,0)),ai(ai_info(x,2),2))
if SC_rayCast(0,object position x(ai_info(x,0)),300,object position z(ai_info(x,0)),object position x(2),300,object position z(2),0)=0
ai_info(x,1)=1
ai_info(x,3)=10
endif
ENDIF
temp_point=0
next x
ENDFUNCTION
BioFox Games