ID:147068
 
i want the NPC's to run towards each other, Wander() does that, but they don't. all the NPC's do is just stand still. im not sure if they do that because they can't handle having an ally and an enemy in the same oview, or the charge() proc is screwing up the code. if your wandering what the WalkAround() proc is, its like step_rand, except the mobs don't make cheesy movements.


mob/NPC
var/mob/NPC/m
proc/charge()
while(src)
if(m.colorofwar == src.colorofwar)
WalkAround(src)
else
Wander(src)
proc/Wander()
while(src)
if(m in oview(5))
step_towards(src,m)
Wander()
Bump(mob/n)
if(istype(n,/mob/NPC))
attack(n)
proc/attack()
sleep(2)
var/damage = Strength - m.Defence
m.Life -= damage
view(src) << "[src] attacks [m]!"
view(src) << "[damage] damage!"
if(m.Life < 0)
del(src)
Ok. First, you're passing src on as an arguement without any need to.
When you wrote Wander(src) you're actually writing src.Wander(src) because no matter what you've always got to specify what atom (or datum) is calling the proc, verb or modifing the var. You can get away with leaving it blank most of the time because it defualts to src.
Anyway, back to the point. You're passing an arguement to Wander() when Wander() takes no arguements. So cut that out.

Now, onto the actual problem. You're never actually setting m in any of this. You've checked to see if it's in oview(5) but that's it.
I'm not sure how you want your NPCs to target players, but if you give me a brief description of how you want them to and the name of your /mob/player type I'll walk you through making them target players.



Also, it's not that important but you should give better names to your atom vars. Something like targetNPC would be a much more fitting name then 'm'.

One last thing, when posting code in the forum put
</b> at the start and <b>
at the end (without the spaces). That will make it appear in a box and format it like Dream Maker does. It makes it a lot easier to read code snippets.
On top of what DarkView said, I'll give you another tip. This is bad:
proc/Wander()
while(src)
if(m in oview(5))
step_towards(src,m)
Wander()

You should never have a proc call itself this way to loop. It will eventually give you a stack overflow error. Instead, use spawn(). However here you're already using while(src) to loop, so as long as you make sure to add a sleep(delay) after the if(), you're all right.

You should definitely add that sleep(), though, or you'll have an infinite loop.

Lummox JR
In response to DarkView
okay, the NPC's are Blue, Yellow, Red, Green, White, and Black. PC's are not targets at all (the mob is blank and his density is 0, making him a perfect overview camera). NPC's with the same colors are allies, when they spot an enemy within a range of 5, they run to the enemy and attack by bumping him, and bumping triggers the attack proc. I should put in some code where NPC's will run to the enemy that is closest to him, I saw Nadrew's skills demo and i know how to do it, getting the NPC's to attack is a different story.
In response to Mardok
Ok. Well use your charge() proc for this. A word of warning however, don't copy and paste my code snippets otherwise this wont work. You can copy the lines of code, but you'll have to do all the indentation (spacing) yourself because I'm not using tabs I'm using spaces.
    proc/charge()
while(src)
//This is where we'll put our new stuff.
if(m.colorofwar == src.colorofwar)
WalkAround(src)
else
Wander(src)



Now for starters, we have to loop through all the /mob/NPCs in oview(5).
For this we'll use a for() loop. Something like this:
for(var/mob/NPC/pTarget in oview(5,src))    //We'll call the mob pTarget, which stands for 'potential Target'.

We also have to take extra special care when using procs like oview(), range() and view() because it uses usr by defualt. Which if left will result in it checking the /mob/NPCs within range of the players mob, not the NPC.
All you really need to know is that in range(), orange(), view() and oview() you have to write view(range,center) where center will usually be src.


Ok, now we've got that done, we need to check to see if these potential targets are friendly or not.
for(var/mob/NPC/pTarget in oview(5,src))
if(src.colorofwar == pTarget.colorofwar) //If they're on the same team.
continue //This tells for() or while() loops to move on to the next loop without doing the rest of the stuff. So basically it'll move on to the next thing in oview().


Now we've made it so the loop will skip anything on the same team as src, we just need to make it so that it sets m to anything else.

for(var/mob/NPC/pTarget in oview(5,src))
if(src.colorofwar == pTarget.colorofwar)
continue
else //If they aren't on the same team.
m = pTarget //Set m to pTarget, making it the current target.
break //This is like 'continue' but instead of moving on to the next object it stops the loop completely. We'll use this because after we've found a target there isn't any point in looping through the other stuff.



Ok. Now hopefully everything should work.
In response to DarkView
mob/NPC
var/mob/NPC/m
proc/charge()
while(src)
for(var/mob/NPC/ptarget in oview(5,src))
if(src.colorofwar == ptarget.colorofwar)
continue
else
m = ptarget
break
if(m.colorofwar == src.colorofwar)
WalkAround(src)
else
Wander(src)
proc/Wander()
while(src)
if(m in oview(5))
step_towards(src,m)
Bump(mob,n)
if(istype(n,/mob/NPC))
attack(n)
proc/attack()
sleep(2)
var/damage = Attack - m.Defence
m.Life -= damage
view(src) << "[src] attacks [m]!"
view(src) << "[damage] damage."
if(m.Life < 0)
del(src)
proc/WalkAround() // Simple AI for moving the mob to make things more interesting!
while(src)
step(src, pick(NORTH,NORTHEAST,NORTHWEST,SOUTH,SOUTHEAST,SOUTHWEST,EAST,WEST))
spawn(rand(5,15))
WalkAround()

thanks for the help, but the NPC's are still not moving, i dont think it has to deal with any other code in the game, because i scrolled through it, looking for mistakes. all the code required for this procedure is here.
In response to Mardok
Doh. I missed a huge mistake. You've got while(src) loops in your WalkAround() and Wander() procs.
What this is doing is making your AI get stuck in there because as long as the mob exists src is going to be true. Meaning that when it enters Wander() it's going to just keep on checking if m in oview() and if it is step towards it.
I also think you've got Wander() and WalkAround() mixed up. It would seem that you want it to walk towards it's enemies and walk randomly otherwise, but you've got it wandering around randomly if it isn't on the same side, and walking towards it if it is.
In response to DarkView
Doh, it still doesn't work, the mobs are still not moving. GAAAA, the frustration.
mob/NPC
var/mob/NPC/m
proc/charge()
while(src)
for(var/mob/NPC/ptarget in oview(5,src))
if(src.colorofwar == ptarget.colorofwar)
continue
else
m = ptarget
break
if(m.colorofwar == src.colorofwar)
WalkAround(src)
else
Wander(src)
proc/Wander()
if(m in oview(5))
step_towards(src,m)
Bump(mob,n)
if(istype(n,/mob/NPC))
attack(n)
proc/attack()
sleep(2)
var/damage = Attack - m.Defence
m.Life -= damage
view(src) << "[src] attacks [m]!"
view(src) << "[damage] damage."
if(m.Life < 0)
del(src)
proc/WalkAround() // Simple AI for moving the mob to make things more interesting!
step(src, pick(NORTH,NORTHEAST,NORTHWEST,SOUTH,SOUTHEAST,SOUTHWEST,EAST,WEST))
spawn(rand(5,15))
WalkAround()

I can't think of anything that'll give us a clue of what's wrong.
In response to Mardok
Putting src into oview() like I said may fix it. It may not but it's definantly going to cause you problems if you don't fix it.

How is charge() being called?

Also, to locate the problem we'll use a technique where we output messages to the world when ever parts of these procs are called.
First, make sure you've only got one of these NPCs in the world.
Now add stuff like world << "charge() starting" to parts of the procs. The idea is when you run it you'll be able to see what's happening/not happening easier.

For instance, if we put a world << "restart main charge loop" just after the while(src) in charge() we'll be able to see how often the main loop is looping.


One last thing, remove the last two lines of WalkAround() (the spawn and call to WalkAround()). Otherwise everytime charge() calls WalkAround() it's going to start up a new infinate loop which you don't want (it'll cause the mob to WalkAround() while doing other things).