ID:1603940
 
(See the best response by Lugia319.)
Code:
mob
proc
NPCAI() //name of proc
var/mob/player/M //variable M has to be mob/usr
while(src) //while src is stil kickin
if(M in oview(5)) //if M is in oview(5)
if(M.name in src.killlist) //now if M.name is in src.killlist, this has it only attack if attacked!
walk_to(src,M,1,4) //src walks to M until 1 block away, moving 4/10ths of a second
if(M in oview(1)) //if M is in oview(1)
step_towards(src,M) //src steps toward M
else //if usr.name isnt in src.killlist
sleep(15)//pauses for 1 and 1/2 seconds
step_rand(src) //step randomly on the field
break //breaks out of the while loop
else //if M isnt in oview(5)
for(M in view(src)) //for all Ms in view(src)
break //breaks out of the while loop
sleep(5) //pauses 1/2 second before redoing loop
spawn(2) // pauses 2/10 of second before redoing proc
NPCAI()


Problem description:
I'm trying to make the npcs/AI's cause less lag to my game. It's making the game go to like..extremely low server efficiency rates. I tried testing game w/o npcs and it was 100%,however,with npcs it was way higher. I deleted a bunch of them and it went higher, but I can't really afford to delete any more..it'd just be awkward.

Any1 know how to optimize the NPCs/AIs to cause less lag
Best response
I usually have it so if there aren't any players inside a certain range that they go idle. As a player enters view range (via turf/Entered) I have them activate again. AI has 4 states

1. AIFindTarget()
2. AIMove2Target()
3. AIAttackTarget()
4. AISleep() <-- This one makes the AI go inactive as it ends.
What if you want them to roam even when no one is around.
LILMESSI18 wrote:
What if you want them to roam even when no one is around.

Rather than simulating reality maybe there's a compromise with that question. What you're basically talking about is a case where maybe the developer doesn't want NPCs to be found in the same place that players last saw them.

An easy solution, let an NPC roam from say 20 seconds after they're out of sight so at least they seem to have been doing something when a player comes back.

Considering a lot of games bound NPCs to only be able to walk in a certain square of space the NPC could have theoretically been moving the whole time. They're no subjective way to tell but objectively we know that they stopped moving after 20 seconds.
In response to Zecronious
Zecronious wrote:

Considering a lot of games bound NPCs to only be able to walk in a certain square of space the NPC could have theoretically been moving the whole time. They're no subjective way to tell but objectively we know that they stopped moving after 20 seconds.

Schrodinger's NPC

If you want them to move a bit when no one's around, tie it to the AISleep part but you really can't get around this issue. The NPC has to go idle, because if it's constantly roaming it WILL cause lag.
In response to Lugia319
Lugia319 wrote:
Zecronious wrote:

Considering a lot of games bound NPCs to only be able to walk in a certain square of space the NPC could have theoretically been moving the whole time. They're no subjective way to tell but objectively we know that they stopped moving after 20 seconds.

Schrodinger's NPC

If you want them to move a bit when no one's around, tie it to the AISleep part but you really can't get around this issue. The NPC has to go idle, because if it's constantly roaming it WILL cause lag.

Schrodinger's NPC, dam now I'm gonna use that. You're right, it has to go idle some time. Preferably sooner rather than later.
I'm going to take a minute here to explain why the NPCs have to go idle to make the game not lag.

When you begin a procedure, a bit of memory in your computer is allocated to that procedure. Kind of a "hands off" flag is set to it. No other process running in your computer can touch that piece of memory. That memory will continue to be "hands off" as long as your procedure is running. When other procedures are called from a procedure, they are added to "the stack" (actually I think if you use spawn() they're not added to the stack but don't quote me on that). This is basic flow of control. Suppose proc2 is called within proc1 (without using spawn). proc1 cannot continue until proc2 "resolves" or ends. So proc1's memory cannot be freed up until it ends and proc2 has to end first. Clear on that? Good.

Now suppose your NPCs are immortal. The AI code will hog up memory as long as while(src) is true and depending on how many NPCs you have, that can be a lot of memory. There's no way to get that memory back for your machine because it's flagged as "hands off" so your game can run, and because your proc never resolves, your machine can't get the memory back. THIS IS THE PROBLEM WITH INFINITE LOOPS. When it comes to infinite loops, the issue is that you're only going to get so much memory allocated for your procs and when you need more memory than you're given, the game crashes. while(src) is a line that begs to be an infinite loop, because it's always true as far as the NPC is concerned, because the only way for it to not be true is if the NPC doesn't exist (which is why your spawn(2) NPCAI() is a waste of lines).

This is why I suggest that NPCs "go idle" when not in view. If the NPC has no reason to interact with the world, why is it moving? There isn't a player there to "make sure" it's moving, so why not save some memory and have it go idle when no one's around. Granted, it could become an issue if EVERY NPC was active because SOMEONE was nearby, which would mean you'd have to be careful about your NPC numbers. But you can cross that bridge when you get to it.
The memory side of it is fairly minimal, to be fair.

It's more just the fact you've got 200+ things moving about (which takes CPU time) that no human eyes will ever see. Meanwhile said human eyes are staring at other things your game are meant to be doing, which is being slowed down by 200+ NPCs wandering about in a forest no-one cares about. Essentially as Zecronious has explained very cleanly.

http://en.wikipedia.org/wiki/If_a_tree_falls_in_a_forest -> http://en.wikipedia.org/wiki/Qualia Basically.
http://www.byond.com/members/ PhatT?command=view_post&post=1447399#comment7868507

This was my attempt at the AI problems a few months back.
In response to Ter13
Ter13 wrote:
http://www.byond.com/members/ PhatT?command=view_post&post=1447399#comment7868507

This was my attempt at the AI problems a few months back.

Ter you should turn that into a lib or something and add more stuff to it :X
You are probably right. Though, I have a new state-machine based AI solution that I made recently, that I'm much more fond of. It's just in need of a proper pathfinding solution and it's finished.
ur stuff are always awesome cant wait to check em out!
I if i increase the spawn before the NPCAI proc is repeated, would it lower the amount of lag?
Those last two lines shouldn't even be there, plain and simple.
Really?
In response to Yusuke11
Yusuke11 wrote:
I if i increase the spawn before the NPCAI proc is repeated, would it lower the amount of lag?

No.
Oh..
I think Ik why the last two should not be there ty
Like mentioned above having your NPCs inactive when no clients are around is generally going to be better in the long run for your average project.

Disabling your NPCs 20 seconds after leaving their range is good as well for simulating walking around while away, as Zecronious stated.

You can also enable them when their just out of screen range too. So that the NPCs are technically active before you even see them. So when you do move into view they were already doing things.