ID:1637907
 
(See the best response by Ter13.)
Code:
mob
NPC
Yusuke
icon = 'NPC.dmi'
icon_state = "Yusuke NPC"

New()
AI()


proc
AI()
while(src)
if(attacker)
walk_to(src,attacker)
if(get_dist(src,attacker) == 1)
Attack(src,attacker,"Str","Injuries",100,0,100)

sleep(5)


Problem description:

Game runs slow when i run it
Best response
It's because in the case that there's no attacker, there's no sleep instruction.

You need an else case and a sleep().
            proc
AI()
while(src)
if(attacker)
walk_to(src,attacker)
if(get_dist(src,attacker) == 1)
Attack(src,attacker,"Str","Injuries",100,0,100)

sleep(5)
 //mob ai 
mob
NPC
Yusuke
icon = 'NPC.dmi'
icon_state = "Yusuke NPC"
New()
AI()
//part of attack proc that changes the attacked mobs attacker var into the attacker
proc
Attack(mob/User, mob/Target, Type = "Str", Stat="injuries", Chance=100, Cure, Extra)
if(prob(Chance))
switch(Type)
if("Str")
dmg = User.str - (Target.defense)
Target.attacker = User
if("Reiki")
dmg = User.reiki - (Target.defense)
Target.attacker = User
//AI
proc
AI()
while(src)
if(attacker)
walk_to(src,attacker)
if(get_dist(attacker,src) == 1)
Attack(src,attacker,"Str","Injuries",100,0,100)

sleep(5)
else
sleep(5)
return
now the mob wont walk to the attacker
In response to YoungJR1232
Prob not the best by far but

#define IDLE 0
#define ENGAGED 1

//above macros are just neater way to check values or return values

mob/proc
AI(){
set waitfor=FALSE

var
target = null // Your target
mode = IDLE // This mode determines whether it is walking to a target or not

while(src){
if(!target){ // if no target
target = locate(/mob) in oview(world.view,src) // locate a target
}else if(!(locate(target) in oview(world.view,src))){ // if target exists and not in view then stop looking for target
walk(src,0) // stop walking
target = null // reset target
mode = IDLE // reset ai mode
}

if(target && mode == ENGAGED){ // if target and you are engaged
if(get_dist(src,target)<=1){ //if target is 1 tile or less away to attack
Attack(src,target,"Str","Injuries",100,0,100) // attack target
}
}else if(target && mode == IDLE){ //if target found and not yet following after
walk_to(src,target) //begin following target
mode = ENGAGED // so we know we are following, and hostile against a target
}
sleep(5)
}
}


also i was kinda lazy on this but you shouldn't have all your enemies running this loop constantly you should delete the AI when its not needed just wasting CPU.

Also you shouldn't keep recalling walk_to its already a loop in itself pretty much as it will keep going after being called once.
Edited first post.
ok i understand the concept of that but im ttrying to think how to make it so when the mob is attacked then it starts following the attacker any suggestions?
In response to YoungJR1232
YoungJR1232 wrote:
ok i understand the concept of that but im ttrying to think how to make it so when the mob is attacked then it starts following the attacker any suggestions?

yes instead of calling AI() when a mob is created call it when they are attacked.
oh smart thinking and is there a way to call something for a specific mob so like after a verb that relates to combat it runs AI for the certain mob
In response to Gokussj99
It's been a while since I've seen any braced DM.
            proc
AI()
while(src)
if(attacker)
walk_to(src,attacker)
if(get_dist(src,attacker) == 1)
Attack(src,attacker,"Str","Injuries",100,0,100)
else
step_rand(src)
sleep(5)

That should do it