proc/Walk_Towards(var/mob/ref,var/atom/trg,var/lag=0,var/speed=16)
set waitfor=0
if(trg==0){ref.Target=0;ref.L.Remove(ref.L)}
else if(ref.Target==0)
ref.Target=trg
FixTarget(get_turf(ref),get_turf(ref.Target),ref)
var/atom/start=get_turf(ref); var/turf/end=get_turf(ref.Target);
while(trg<>null&&ref.Target<>0 && start<>end)
if(ref.L.len==0){ref.L=AStarSearch(start,end,ref)}
var/diagpenalty=1;
if(get_dir(ref,ref.L[1])==NORTHEAST || get_dir(ref,ref.L[1])==NORTHWEST|| get_dir(ref,ref.L[1])==SOUTHEAST|| get_dir(ref,ref.L[1])==SOUTHWEST){diagpenalty=sqrt(2)}
ref.glide_size=(16)*world.tick_lag/lag
if(get_dist(ref,ref.L[1])>1){step_towards(ref,ref.L[1],16)}else{step(ref,get_dir(ref,ref.L[1]),16)}
if(ref.loc==ref.L[1]){ref.L.Remove(ref.L[1])}
sleep(lag*diagpenalty)
start=get_turf(ref);end=get_turf(ref.Target)
ref.Target=0
else //this just changes the target
ref.Target=trg
ref.L.Remove(ref.L)
FixTarget(get_turf(ref),get_turf(ref.Target),ref)
Problem description:
I have a problem with my Walk_Toward proc, for a reason it works normally at first but after some time average real-time increases really fast until the whole game crashes.(walking proc is used with a formation of mobs so there are nearly 40 mobs moving together)
so here is the code. also I've noticed that when I am near average realtime increases but when I go far it decreases...
also FixTarget() is used to change the target in the event that the target is dense so it changes the target to the nearest available. I use no density in the map so it never does anything in this test.
Indents aside, a few things that aren't right in this code:
- if(trg==0) is likely to never be true, because if anything you'll pass a null and not a number. That should be if(!trg) instead. Same with the ref.Target check. Never use ==0 when checking for falsehood; use the ! operator.
- Calling get_dir() repeatedly is no bueno. Call it once and store it in a var.
- You don't need to compare the direction to each of the four diagonals. All you need is if(thedir & (thedir-1)), which will check to see if it has more than one bit set.
- get_dist() doesn't distinguish between targets on the same z level, or different; it will give you the max of the x, y, or z distance. You should test for different-z cases if you use get_dist().
Also on a more general note, having a mob var named "T" isn't likely to be very helpful. Use short names like that for local vars and arguments in procs, and descriptive ones for your mob vars.