ID:161119
 
im trying to make a fighting game with stick figures thats single player, but i dont know how to make NPCs that attack the player back, does anyone know how to do this?
It's called AI, or Artificial Intelligence. There might be libraries or demos on it, but I don't think they would help since all AI systems are different.

I'll give you an example of a basic AI system.
mob/AI
Monster //Your basic "intelligent" monster.
var/hp=10 //Health variable.
New() //When the monster is created...
..() //default action...
spawn Wander() //Call the Wander() proc in the background.
proc/Wander() //This is the Wander() proc.
while(src) //infinite loop
sleep(10) //Every second,
step_rand(src) //Step a random direction,
for(var/mob/M in view(1,src))attack(M) //and attack any mobs around it.
mob/proc/attack(mob/M) //Basic attack proc.
view()<<"[src] attacks [M]!"
M.hp-=1
if(M.hp<=0)
world<<"[M] was killed by [src]!"
del M


At the moment, all they do is walk around aimlessly and attack anyone who comes near. It doesn't chase anything, retreat, or anything. That's for you to make yourself.
In response to Kaiochao
ya, i tried that for my own game, walk over to the mob, it was attacking itself....
In response to Lureman
nvm, problem is, its view() not oview(), so it can attack itself...

remembered i had that same problem with my attack verb at first....
In response to Kaiochao
Ok The Previous code done by kaiochao was correct for the most part but unfortunately he forgot to put it so it only attacks clients not other monsters so i will show it the correct way (basically the same just one more bar of code)<code>
mob/AI
Monster
var/hp=10
New()
..()
spawn Wander()
proc/Wander()
while(src)
sleep(10)
step_rand(src)
for(var/mob/M in view(1,src))
if(M.Client)//if mob(M) is a client(user)
attack(M)
mob/proc/attack(mob/M)
view()<<"[src] attacks [M]!"
M.hp-=1
if(M.hp<=0)
world<<"[M] was killed by [src]!"
del(M)
and that one little bar of code makes it so it doesn't attack its fellow monsters only a mob that is a client(sorry copied and pasted kaiochao too lazy to write it all)
In response to Lunar_Reaper
It was simply a basic example, he knew it wasn't perfect. There was no need to bump this up, your addition was also very simple and easy to do anyway, and wasn't worth it either.
In response to Kaioken
I was doing more of a just in case sort of thing just in case he didn't add that very simple addition. and if he hadn't there would be monsters attacking each other instead of the player i was just making sure that he didn't add it and wonder why it was attacking the other monsters.
In response to Kaioken
... :-)