ID:139275
 
Code:
Attack Proc:
        proc/enemyattack()
var/obj/monster/O
if(src.wild==1)
while(src)
if(O in view(src))
if(O.name in src.killlist)
walk_to(src,O,1,5)
if(O in oview(1))
step_towards(src,O)
else
sleep(10)
walk_rand(src,15)
break
else
for(O in view(src))
break
sleep(5)
spawn(2) enemyattack()

Damage proc:
        proc/damage(obj/O)
var/damage = (rand(src.minatk,src.maxatk)) - O.def
O.hp -= damage
O.killlist += src.name

Problem description:
First-this is based from an AI Demo

The first code is the Enemyattack proc, which so far only makes the enemy walk randomly, even though they have been attacked. They're supposed to walk around when there isn't any objs near them that are listed in their kill list, but they just continue to walk. Im thinking the reason to this is because the O.name is not being added to the src.killlist or something like that, but thats what I don't get because the name is being added to the killlist in the damage proc.

Help would be appreciated.
What I suggest rather than in a list, is the simply hold a variable with the player's name whom attacked the Enemy.

Enemy
parent_type = /obj
Mob1
var/attacked = "" //Where the Players Key goes
proc
Attacked(mob/M)
if(M.key == src.attacked) //If the key's match
src.Attack(M) //Attack it!
else //If not
return //Return
Attack(mob/M in oview(src)) //Attacks mob "M"
//Attack Stuff
mob
Player
proc
Attacking(Enemy/E) //When you attack something
E.attacked = "[src.key]" //To make sure the Enemy attacks THAT player, and not another one with the same "Character Name"
E.Attacked(src) //Activates the Enemy's Attacked Proc
verb
Hit_crap(Enemy/E in view(1))
//Attacking stuff
src.Attacking()


You can see where the loop starts with the Enemy being attacked first, and the progression from there. The only way this will work for all attacks is if you add the "<font face=courier new>src.Attacking()</font>" proc is placed somewhere within EVERY ability that(For lack of a better phrase) "Pulls Aggro" on the Enemy. This is helpful too because you can have certain "debuffs" that you can place on an Enemy, but they wont attack you for it.

If you have any questions, just post them here, I'll try my best to answer them. and I'm sorry if this little Demo doesn't work, it's just to provide proof-of-concept. If you want, i can try to develop something for you. I have to make one of my own in my game anyways, so I'll do my best to help.

Danke Schoen, und Auf Weidersehen!

-Danbriggs

PS: If it doesn't work, and you know why, please tell me so that i may clean up this little snippet. It was done off the top of my head and without being tested.

PSS: If you're wanting to know how to make an "Aggro" system like in WoW, that simply attacks the player with the highest threat against the Enemy, please tell me. I'm developing one at the moment, and i plan to make it a Library. I'll make sure that it remains as simple as possible so newer coders can understand it and implement it into their projects. Thanks.
In response to Danbriggs
I get where you're coming from. I split the proc into 2 separate procs, one for walking(works) and one for attacking(doesn't). Also, I switched O.name into O.Owner(key) which I should have done earlier incase any two were to name their monsters the same name.

Also, I should I said in the original post that I am using a Bump Attack system here, so all I really need is the Enemy to follow whoever hit them.

EDIT:
        proc/damage(obj/O)
var/damage = (rand(src.minatk,src.maxatk)) - O.def
O.killlist="[src.Owner]"
O.hp -= damage
O.isattacked = 1
s_damage(O,damage,"white")
if(O.hp <= 0)
del(O)
src.attacking=0
sleep(src.atkspd)
src.attacking=0
proc/enemyattack()
if(wild&&isattacked)
for(var/obj/O in world)if(src.killlist=="[O.Owner]"&&O in view(src))
walk_to(src,O,5)
if(O in oview(1))
step_towards(src,O)
spawn(2)
enemyattack()


This is what I have at the moment. The vars "wild" means if its controlled by a player or not and isattacked changes when the obj is attacked by a player.

Basically, you attack another monster with yours and change the isattack of that monster to 1, then the proc should catch it and search the world for objs that have belong to the Owner in it's kill list. But it just doesn't seem to work.
In response to FallenZ
I'm not really sure the code itself is important right now. One of the best things you can do before coding anything is to just brainstorm the concept behind the code. Here's mine:

"Player(P) attacks Enemy(E). When P attacks, it activates a proc within itself that turns E's aggression to P. E will now continue attacking P until one of the two is either killed or removed from combat.
We know that we will need at least use three variables to make this work: E's Target, Death, and weather the two are in Combat or not. We also know that we have to contain the "initiative" proc into the Player, since he will be the one triggering the aggression. And perhaps an "initiative" proc within the Enemy as well, in case you wish to aggro the Played when he gets so close to him.
Next, We'll need the proc for the Enemy, that once he has his target, can begin attacking it."

This is basically the bear bones of what you need to know. From here, you can fit together the various procs, vars, and other things to essentially make it run as smoothly as you can. Using this concepts technique will also help you become a more productive and efficient programmer. As long as you know WHAT you need to make it work, HOW to do it just becomes arbitrary. I hope this helps at least a little.

-Danbriggs
In response to Danbriggs
Your concept really helped. I just made it so that the Enemy would follow whoever attacked them during the player's damage proc. Then in the Bump step, if the src is an Enemy and not a player, it will be redirected in to the enemyattack proc, which is the enemy version of the damage step.

Thanks for the help, now I can finally move on with my game. :D
In response to FallenZ
No problem. Just use that trick whenever you're stumped on how to make something work, That's how I always solve my coding conundrums. Good luck with your project, I hope it works.