ID:722086
 
(See the best response by .screw.)
Code:
mob/monsters
Enemy
New()
..()
spawn(-1) src.CombatAI()
return ..()

mob/monsters/proc/CombatAI()
while(src)
for(var/mob/Player/M in oview())
if(get_dist(src,M)<=3)
src.dir=get_dir(src,M)
else
step_to(src,M)
break
sleep(rand(4,8))


Problem description:

When I am using this code my cpu is higher by 4% so can someone please tell me what am i doing wrong here.Ty

It's all correct.
Best response
LaNuiit wrote:
Code:
> mob/monsters
> Enemy
> New()
> ..()
> spawn(-1) src.CombatAI()
> return ..()
>
> mob/monsters/proc/CombatAI()
> while(src)
> for(var/mob/Player/M in oview())
> if(get_dist(src,M)<=3)
> src.dir=get_dir(src,M)
> else
> step_to(src,M)
> break
> sleep(rand(4,8))
>
>

Problem description:

When I am using this code my cpu is higher by 4% so can someone please tell me what am i doing wrong here.Ty


Every time an enemy is created, a new CombatAI() proc is initiated. Therefore, you have multiple infinite loops going on at once.
So whats the best thing to do to fix this
Try changing step_to to step_toward. Feed had this problem with it's AI and when changing to switching to step_toward it fixed itself and now Feed can handle 800+ AI running at only ~8 CPU.
sry to bother you guys again.
mob/monsters/proc/CombatAI()
while(src)
for(var/mob/Player/M in oview())
if(src.dir==NORTH && M.y == src.y +3 &&M.x == src.x)
else if(src.dir==SOUTH && M.y==src.y -3 &&M.x == src.x)
else if(src.dir==WEST && M.x == src.x -3 &&M.y == src.y)
else if(src.dir==EAST && M.x == src.x +3 &&M.y == src.y)
src.do_projectile_stuff()
else
step_towards(src,M)
break
sleep(rand(4,8))


Thats what I coded for projectile part. What I want to create is that when enemy is facing player it wont be diagonaly. But is it there a better way to do that? Also i want to make it that is betwen 3 and 7 oview when src.do_projectile_stuff() should be triggered.

    Move(var/turf/NewTurf,var/StepDir)
switch(StepDir)
if(NORTHWEST) pick(step(src,NORTH),step(src,WEST))
if(NORTHEAST) pick(step(src,NORTH),step(src,EAST))
if(SOUTHWEST) pick(step(src,SOUTH),step(src,WEST))
if(SOUTHEAST) pick(step(src,SOUTH),step(src,EAST))
else return ..(NewTurf,StepDir)

and thats also my walking part