ID:177989
 
I read everything, have everything ready, but now I need help making a monster that walks around and attacks people. Variables I will be using are HP, Strength, and Expup. In case that helps.
Drafonis wrote:
I read everything, have everything ready, but now I need help making a monster that walks around and attacks people. Variables I will be using are HP, Strength, and Expup. In case that helps.

A common system for this is to make a loop that wanders at random, approaches any player in view, and attacks once it gets up to them. I think I like this variation a little better:
mob
var/atom/home // used for monsters
var/atom/target // used for monsters
var/target_reset // time to reset target
var/AIdelay=4
var/sleeping=1

New()
..()
home=loc

proc/Wake()
if(sleeping) AI()

proc/AI()
if(key) return
sleeping=0
var/mob/M
var/list/L=list()
for(M in oview(1,src))
if(M.key) L+=M // attack all player mobs (logged in or not)
if(L.len)
StartBattle(pick(L))
return // AI() will be called again at end of battle if monster lives
var/mindist=20
for(M in oview(src))
if(!M.key) continue // skip non-players
var/d=get_dist(src,M)
if(d>mindist) continue
if(d<mindist) L=list()
L+=M
if(L.len)
M=pick(L)
target=M
target_reset=world.time+AIdelay*20
if(!target)
for(M in oview(100,src)) // this will clip from 100 to world.view*2
if(M.key) break
if(!M) // no mobs in the vicinity
// This line is here if you want to do it this way
// spawn(rand(AIdelay*5,AIdelay*10)) AI()
if(!home || loc==home)
sleeping=1 // call AI() to wake this mob up
return
else target=home
else if(home && get_dist(src,home)<3) // patrol
target=pick(oview(10,src)) // move to some random point
else target=pick(view(home)) // go "home"
target_reset=world.time+AIdelay*20
var/turf/T
if(target && target_reset<world.time)
T=get_step_to(src,target)
if(!T)
step_rand(src)
spawn(AIdelay) AI()

This will make a monster patrol around until it finds a player, and attack the nearest one. My version of this code would use a "sleep mode" to conserve CPU cycles--useful if you have a lot of monsters--so that you can simply wake monsters when a player enters their vicininty (perhaps through turf.Entered() signaling them).

Lummox JR