ID:144788
 
Code:
mob/proc/AIWood()
if(src.AIWoodOn == 1)
src.AIMetalOn = 0
src.AIFoodOn = 0
src.AIArbiniteOn = 0
src.AICropOn = 0
var/obj/GotoWood/closest
var/mob/Trees/closest2
var/dist
var/dist2
for(var/obj/GotoWood/GW in world)
if(!closest)
closest = GW
dist = get_dist(src,GW)

if(closest.Owner == src.Owner)

if(src.CarryWood >= 99)

src.WoodTarget = null
src.Chop = 0
src.destination = closest




else
if(get_dist(src,GW)<dist)
closest = GW
dist = get_dist(src,GW)

if(closest.Owner == src.Owner)

if(src.CarryWood >= 99)

src.WoodTarget = null
src.Chop = 0
src.destination = closest




for(var/mob/Trees/T in world)
if(src.CarryWood <= 99)
if(!closest2)
closest2 = T
dist2 = get_dist(src,T)



else
if(get_dist(src,T)<dist2)
closest2 = T
dist2 = get_dist(src,T)
if(src.WoodTarget == null)
src.WoodTarget = closest2
if(src.WoodTarget)
src.destination = closest2
src.Chop = 1
else
src.WoodTarget = null
src.Chop = 0

else
return

spawn(30) AIWood()


Problem description:

This code is basically an AI proc that runs for each specific unit created in my game, it tells it to collect Wood, the problem is that the proc is creating massive ammounts of lag, were talking 5 second delays. Is there any way I could maybe slow down the proc of even fix it so there is less lag?
Oh ye slimey horrors!

First things first:

- You don't need src. in front of anything. If no source is specified, src is assumed
- Use if(something) and if(!something), not if(something==0) and if(something==1)

But your real problem is quite simple:

You're looping through EVERY OBJECT WITH A PARTICULAR TYPE IN THE WORLD AND FINDING OUT HOW FAR AWAY THEY ARE. This will be slow.

Simple solution: Loop only through things in view(src). If you want the thing to be able to see to the ends of the earth, then you'll have to do some funky data-structure stuff if you don't want it to be incredibly slow.
In response to Jp (#1)
You should also try using bit operators for those boolean vars, no point in keeping such a mass. D;