ID:162296
 
Yes, that's wAndering not wOndering. What's the best way of making AI's wander around? I used to be able to do it but now it just creates lag for some reason. thanks
Best thing to do is to find an AI demo and see what the fine points are of the code, but all is basically is is "walk_rand(src,delay)"
In response to Jholder84
Thanks, that took a while to work into my code because i'd done something stupid with the move_allowed var, lol.
Can I make the delay a random number between, say, 5 and 600 each time? I tried
walk_rand(src,rand(5,600))

but of course that sets one random number, and it never changes. Is there a way?

(When I tested this example, one of my shopkeepers got stuck on something less than 10! He was rand_walking like a headless chicken! Its hilarious, try it sometime.)
In response to Adam753
So that command doesn't re randomize apparently. I did this:
var/randmove = rand(5,60)
walk_rand(src,randmove)

600 seemed WAY too long of a pause so I changed it to 60. Worked pretty good. Of course you could go crazy with this:
var/pickrandmove = rand(1,100)
var/randmove
if(pickrandmove <= 10) randmove = rand(1,5)
else if(pickrandmove > 10 && pickrandmove <= 50) randmove = rand(5,60)
else if(pickrandmove > 50 && pickrandmove <= 90) randmove = rand(60,200)
else if(pickrandmove > 90) randmove = rand(200,600)
else randmove = 20 //just in case something goes wrong 20 is a good number
walk_rand(src,randmove)


Like I said, you can go crazy with this and have fun.
In response to Adam753
or you just make a recursive proc

mob
NPC
wanderer
proc
wander()
step_rand(src) // Step our wanderer randomly.
spawn(rand(5,60)) wander() //spawn src.wander() sometime between 1/2 second and 6 seconds. Current proc will end and processing will continue with the new wander(), which does the same thing.


Notice that with this heirarchy the only mob with the wander() will be of type /mob/NPC/wanderer and anything that is a child of that type (/mob/NPC/wanderer/chicken has wander() ). Of course you'll have to figure out where you want to call wander().

You may also want to edit it so:
That it is more robust--so you don't get null.wander() runtime errors.
That it can be stopped because this one goes on forever.
In response to Jholder84
Well, I don't want them to move TOO often as i'm going to be trying to fight them and talk to them! thanks, i'm sure I can do a lot with that.
In response to Adam753
It's easy enough to add in a test to have them stand still if you're within a certain distance from them.

stop_move()
if(src.client)
return 0
else
for(var/mob/M in oview(3,src))
if(M.client)
if(get_dist(src,M) <= 3)
break
else
continue
else
continue

Something like that and add src.stop_move() after your walk_rand line.
In response to Jholder84
I know that, but all my verbs set src in oview(1) so cn you imagine trying to catch and ose a verb on something moving once every 2 seconds? I can do the rest myself, thanks for your help.
In response to Adam753
I'd like to point out that Jholder84 has been wrong in every single one of his posts here. Moving on, this is what you want:

mob/proc/NPCwander(var/mindelay = 10, var/maxdelay = 10)
if(mindelay > maxdelay || maxdelay < 1 || mindelay < 1) return //ensure proper arguments
while(!client)
//if there's no player nearby
if(!locate(/mob/player) in oview(3,src))
step_rand(src)
var/timetosleep = rand(mindelay, maxdelay)
sleep(timetosleep)
In response to Garthor
It may not be the way that YOU do it or even the way you SHOULD do it, but that does not change the fact that the code that I gave works. But this is not the place for this so I will now leave it alone.
In response to Jholder84
No, none of your code works. It works in the sense that it would compile, but it doesn't work in the sense that it doesn't do what you want it to do. The first thing you posted just does precisely what he said he DOESN'T want, and the second thing you posted just does precisely nothing, except waste CPU cycles.