ID:1292233
 
(See the best response by Pirion.)
Code:
mob/Enemy
icon = 'Graphics/NormalBase.dmi'
var/const/ConstantValue = 32
New()
src.MaxHealth = round(((src.Level ** 2 * 115) / 100) + ((src.Level * ConstantValue) + 100))
src.MaxMana = round(((src.Level ** 2 * 105) / 100) + ((src.Level * ConstantValue) + 100))
src.Strength = round(((src.Level ** 3 / 100) / ConstantValue) + ((src.Level * 2) + ConstantValue))
src.Defense = round(((src.Level ** 3 / 100) / ConstantValue) + ((src.Level * 2) + ConstantValue))
src.Agility = round(((src.Level ** 3 / 100) / ConstantValue) + ((src.Level * 2) + ConstantValue))
src.Magic = round(((src.Level ** 3 / 100) / ConstantValue) + ((src.Level * 2) + ConstantValue))
src.Health = src.GetMaxHP()
src.Mana = src.GetMaxMana()
spawn(-1)
src.Wander()
return..()

proc
Wander()
while(src)
while(PausedMove) // PausedMove is a tmp variable
sleep(10)
for(var/mob/Player/P in oview(src))
if(get_dist(src, P) <= 3)
PausedMove = 0
walk_rand(src, 5, 1)
else
P = null
PausedMove = 1
src.loc = locate(initial(src.x), initial(src.y), initial(src.z))
break
sleep(10)


Problem description:
I tho about 2 ways to my AI don't use high CPU resources. 1 - I am showing it, I am trying to make my AI only to wander whenever a play is within the req. distance. If I don't use PausedMove, whenever I go out of his range, it constantly tries to move and respawn at same time. But if I am using PausedMove, it doesn't move at all, just stays there. What I want to accomplish is when player gets near AI, it will move but when it goes out of AI distance, AI should stop do anything.
2 - If I wanted to make my NPCs only to spawn whenever a user goes within the monster area, do I need to map them ? Or I can do it programmatically ?

One more question, why if I do this
for(var/client/C in oview(src))

// Instead

for(var/mob/Player/P in oview(src))


It doesn't even do anything.. ?

Thanks in advance.
Best response
In your while(PausedMove) you need to set it to false if there is a player in view.

For the second question - No, you can use pick on area.contents to return a random turf in the area, and spawn them to that location.

For the third, I believe it would be because client does not have a location so it never gets in the oview. You may need to do some special check to see if client.eye is in the range.
Have you tried it yourself ? I tried before posting and it doesn't seems to work. Any way I could stop their actions if my player goes off their sight ?

And thanks for the replies, specially 2nd one. Not sure if it's a good way to use less CPU resources, but I'll try.

EDIT:

Alright, I managed to remake the AI system and I would like someone to see if I am doing naything bad.

mob/Enemy
New()
spawn(-1)
src.FindTarget()
return..()

proc
FindTarget()
while(!src.Target)
for(var/mob/Player/P in oview(5, src))
if(P.client)
src.Target = P
world << output("Target: [src.Target.key]", "ChatOutput")
spawn()
src.Wander()
break
sleep(5)

proc
Wander()
while(src.Target)
if(get_dist(src, src.Target) <= 3)
walk_rand(src, 5, 1)
else
src.Target = null
sleep(10)
if(!src.Target)
var/turf/InitialLoc = locate(initial(src.x), initial(src.y), initial(src.z))
walk_to(src, InitialLoc, 0, 6, 6)
src.FindTarget()
sleep(30)


Thanks in advance!
No real problems I see. In the FindTarget() proc, you don't need to break as it will not continue anyways. And in Wander() under the if(!src.Target) statement, the sleep(30) is unnecessary because no actions follow it.

Hope I helped, good luck!
Thank you very much. I fixed it now and it seems like everything is going smooth. I appreciate everyone's help.