ID:2424373
 
Code:
var
const

ANGRY = "angry"
HAPPY = "happy"

FIGHT = "fight"
RUN_AWAY = "run away"
mob
var
respawnX; respawnY; respawnZ
respawnSpeed = 50; respawnLoc


mob

NPC/AI
//step_size = 32
var/state = HAPPY
var/wander_distance
var/attack_distance

New()
..()
loc = respawnLoc
AI()

proc
Wander(range = 5000) //5
if(!loc) return
if(!x) return
if(!initial(x)) return

//if(!src) //path
var/turf/t = loc

while(!t || t.density || t == loc)
t = locate(initial(x) + rand(-range, range), initial(y) + rand(-range, range), z)

if(t)
walk_to(src,t,1,0,6)
AI()
if(Dead) return
if(state == HAPPY)

Get_Target()

if(Target)
if(Health < Max_Health/4)
if(prob(70))
world << "[name] decides to stay and fight."
state = FIGHT
else
world << "[name] decides to run away."
state = RUN_AWAY
return AI()
//call_for_help()

if(get_dist(src, Target) > 1)
walk_to(src, Target,0,0,4)
sleep(5)
else
Attack()
sleep(5)
else
Wander()
sleep(5)

else if(state == FIGHT)
Get_Target()

if(Target)
//call_for_help()

if(get_dist(src, Target) > 1)
walk_to(src, Target,1,2,8)
sleep(5)
else
Attack()
sleep(10)
else
state = HAPPY
sleep(5)

else if(state == RUN_AWAY)
Get_Target()

if(Target)
//call_for_help()
walk_away(src, Target,3,1,8)
sleep(3)
else
state = HAPPY
sleep(5)

spawn() AI()

Get_Target()
var/mob/T = Target
if(T && T.Dead)
Target = null

if(state == ANGRY)
for(var/mob/M in oview(2, src))
Change_Target(M)

Attack()
Execute_Attack_AI()

Execute_Attack_AI(Charge)
set instant = 1
if(Attacking || Attack_One_Delay > world.time || !Universal_Decider()) return
if(Last_Attack < world.time-10)
Attack_String = 0
Combo_String = ""

Attack_String++
Attack_One_Delay = world.time+1
Last_Attack = world.time
Attacking = 1

src.ComboCheck()

if(Attack_Type == "Sword")
Combo_String = "[Combo_String],S1"
var/Attack_Range_Type = ""
if(Target) src.Track_Target()
Sword_Combat_Hit(1,Attack_Type,Attack_Range_Type)
if(Attack_Type == "Hand")
flick(pick("Punch Left","Punch Right","Kick Left","Kick Right"),src)
Combo_String = "[Combo_String],H1"
if(Target) src.Track_Target()
Basic_Combat_Hit(1,Charge)
spawn() Attacking = 0


Problem description:

This code used to work perfectly fine, but now when the mob attacks the player, the game flips out and its as if he's attacking a million times in one second, but after it stops the mob dies or knockouts and gets back up and then its working perfectly fine again. Just wanted someone to look over the code
I don't see state ever getting set to ANGRY in this code. There might be important code missing here. Get_Target is only being called when state isn't ANGRY, so it's never being used in this code to get a new target.

If something is happening multiple times in an instant, that's clearly recursion going out of control. Try to follow the flow of the code in the situation where the AI attacks a player and see if anything is being called when it shouldn't be.