ID:139642
 
Code:
mob
monsters
New()
MonsterAI()
src.safezone = 0
src.last_x = src.x
src.last_y = src.y
src.last_z = src.z

mob
var
target
AITimer = 5
proc
MonsterAI()
if (client)
return 1
spawn(AITimer)
MonsterAI()


mob
MonsterAI()
if (client)
return
if(!src.move)
return
var/action_taken
if(src.target)
if(src.target in oview(1,src))
AIAttack(src.target)
var/mob/M = src.target
if(!target)
goto lol
if(M.powerlevel <= 0)
src.target = null
lol
action_taken = 1
if(src.target in oview(6,src))
walk_to(src,src.target,1,6)
action_taken = 1
else
var/mob/M = src.target
if(src.target in oview(15,src))
view(src,60) << "*<font color = yellow><tt>[src] looks at [M] as he flees and begins to teleport.</tt><font color = white>*"
sleep(2)
src.x=M.x
src.y=M.y-1
src.z=M.z
view(src,6) << "<b><font color=blue><font size = 2>[src]:</b><font color=white><font size = 2> <tt>Muhahaha, you won't get away that easy!</tt></font color></font color>"
action_taken = 1
else
src.x = src.last_x
src.y = src.last_y
src.z = src.last_z
src.target = null
else
for (var/mob/other_mob in oview(6,src))
if(istype(other_mob, /mob/Player/))
src.target = other_mob
action_taken = 1
break
if(!src.target&&!followblock&&!follow)
walk_to(src,src.owner,2,2)
if(action_taken)
spawn(AITimer)
MonsterAI()
return
..()
mob
proc
AIAttack(mob/M)
if(!M.KO)
if(istype(M, /mob/monsters))
src.target = null
return
var/damage = powerlevel / 1.5
M.AITakeDamage(src, damage)
else return
AITakeDamage(mob/attacker, damage)
if (attacker.client)
return
else
if(src)
src.powerlevel -= damage
if(src.GettingHitAttackText == 1)
src << "[attacker] attacked you for [damage] damage"
if(src.powerlevel <= 0&&src.npp==0)
src.DEATH(attacker)
attacker.x = attacker.last_x
attacker.y = attacker.last_y
attacker.z = attacker.last_z


Problem description:

When I use this for the Monsters ingame, The more monsters I spawn the more it eats CPU if I have a normal amount of Monsters ( around 50) it takes up about 10% of CPU

I was wondering if there was a way to make it use less CPU.
One thing is that you aren't letting New() finish with what it normally does (have a ..() at the end).
In response to OrangeWeapons (#1)
You certainly have a knack for contributing irrelevant information.

For starters, he could stop using goto. He could also not call his "MonsterLoop" recursively (having the loop call itself at the end). Then he can give his entire AI an overhaul to be more elegant, robust, and efficient.

However, the CPU usage could be coming from the fact that when a monster "flees", the message saying so is being displayed to everything within 60 tiles of the center object...