ID:154981
 
proc/
WalkTo(mob/c, mob/m)
while(c && m && bounds_dist(c,m)>c.step_size)
step_towards(c,m)
sleep(c.step_size/world.fps)
world<<"debugging."


So.. I tried writing my own walk_to() proc using bounds_dist to get pixel perfect accuracy yet I came up with a problem. I walk towards an enemy then attack, then back to my initial position. Then it's the enemies turn and they begin walking towards me and the code gets stuck here, constantly sending the world my debugging message. My assumption is that my formula for while() is off and when the enemy or I get to a certain bounds_dist it either keeps on running the above code, or it skips it completely.

I've tried all sorts of formulas, some work longer than others but in the end, always reach a breaking point. I'm not an expert with math, anyone think they know a formula that'll work this correctly?
Have the debugging message output the current value of bounds_dist(c,m), or output the result of step_towards().

Also, to simplify the condition in the while() statement you can reorganize it like this:

while(1)
if(!c) break
if(!m) break
if(bounds_dist(c, m) <= c.step_size) break

step_towards(c, m)
sleep(c.step_size / world.fps)


By writing out the checks in separate if() statements they can be less confusing.
In response to Forum_account (#1)
Hmm thanks! I never thought of using while with breaks. It's a lot cleaner. I'm gonna start using it like that. Also, I've thought of a temporary fix.

Any critique on using this method?

    WalkTo(mob/c, mob/m)
var/laststep=bounds_dist(c, m)+1
while(1)
var/distance=bounds_dist(c, m)
if(!c) break
if(!m) break
if(distance <= c.step_size) break
if(distance==laststep) break
laststep=distance
step_towards(c, m)
sleep(c.step_size / world.fps)


Doing this... I'm checking if I haven't moved since my last step, and if I haven't, it breaks the proc. I have a feeling it might cause conflicts later on though. What do you think?
In response to AbdelJN (#2)
I think step_towards returns a value you can use to determine if it succeeded or not, so you might just be able to do this:

if(!step_towards(c, m)) break


Another way to simplify complex conditions in if or while statements is to use extra variables, like this:

while(1)
var/close_to_target = bounds_dist(c,m) < c.step_size
var/some_other_condition = a + complex * (and - messy) >= calculation
if(close_to_target || some_other_condition) break
In response to Forum_account (#3)
Oh damn. Thanks a lot:) This is really, really helpful.