ID:1629066
 
Keywords: aicpu
(See the best response by Ter13.)
Code:
ai_combat()
while(!src.ko || !src.move || active)
set background = 1

if(!src.target)
var/mob/player/m = locate() in oviewers(src , 10)
if(m) src.target = m

world << output("Target Found: [src.target]","playerOutput")

//else if(prob(10))
// var/mob/player/m = locate() in oviewers()
// src.target = m

else

if(locate(src.target) in oviewers(src , 10))

if(!target.ko)
if(get_dist(src , src.target) == 1)

src.dir = get_dir(src , src.target)
src.Attack()

if(prob(tacRate))
/*if(prob(70)) */step_away(src , src.target , 3, src.rundelay)
//else src.loc = locate(pick(src.target.x+1, src.target.x, src.target.x-1) , pick(src.target.y-1, src.target.y+1, \
src.target.y) , src.target.z)



else
if(health.current < health.maximum / 10)
step_away(src , src.target)

else
step_to(src , src.target)


else
src.ai_home()
break
// for(var/obj/projectile/o in oview(1)) if(prob(50))step_away(src , o)

sleep(world.tick_lag * 10 + rand(-2 , -4))

ai_home()

if(src.loc != initial(loc) || src.loc != locate(0,0,0))

spawn(10)
//if(locate(src.target) in oviewers(src , 10)) src.ai_combat()

//else
src.loc = initial(src.loc)
src.ai_heal()
src.active = 0


Problem description: Hello, how you guys doing? I am attempting my own Ai process but I can't seem to reduce how much CPU it takes. CPU sky rockets when multiple(around 5-9) ai are going after their target. Another problem, when ai_home() is called, both the player and the Ai are teleported back to their last location before ai_home() is called.

Best response
Here's an AI system I wrote for Phat T. You might learn something from it:

http://www.byond.com/forum/?post=1447399#comment7868507
In response to Ter13
Thank you so much that was very helpful. A few questions though.

-When the ai is going back home and the player appears back in it's range, I want the ai stop and go after the target.

-I want to use get_dir() during the ai attacking phase but with only cardinal directions. I don't want the ai to face southeast, northeast, etc.

-There are also an instance where the ai stops moving completely even though the ai.foundTarget() is active. this is the Error I get:

/*
runtime error: Maximum recursion level reached (perhaps there is an infinite loop)
To avoid this safety check, set world.loop_checks=0.
proc name: chaseState (/mob/Enemies/proc/chaseState)
usr: Luchasi (/mob/player)
src: Bully (/mob/Enemies/Humans/Bully)
call stack:
Bully (/mob/Enemies/Humans/Bully): chaseState()
Bully (/mob/Enemies/Humans/Bully): lostTarget()
Bully (/mob/Enemies/Humans/Bully): attackState()
*/



-Regards