1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
for _, m in ipairs(MONSTERS) do
if t % m.atkspd == 0 then
m.target = nil
local closest
for _, p in ipairs(table.shuffle(player(0, 'table'))) do
if player(p, 'health') > 0 and
not gettile(PLAYERS[p].x, PLAYERS[p].y).SAFE and
not gettile(PLAYERS[p].x, PLAYERS[p].y).NOMONSTERS then
local dist = math.sqrt((player(p, 'x')-m.x)^2 + (player(p, 'y')-m.y)^2)
if dist < 400 then
if not closest or dist < closest[2] then
closest = {p, dist}
end
end
end
end
if closest then
local dist = closest[2]
if dist < 400 then
m.target = closest[1]
if m.spc and math.random(10000) <= m.spc[1] then
m.spc[2](m, m.target, dist)
elseif dist <= (m.range or 32) then
m:hit(m.target, 10)
end
end
end
end
m.imgang = math.sin(t/2.5*math.pi) * 15
if m.target and player(m.target, 'exists') and player(m.target, 'health') > 0 and
not gettile(PLAYERS[m.target].x, PLAYERS[m.target].y).SAFE and
not gettile(PLAYERS[m.target].x, PLAYERS[m.target].y).NOMONSTERS then
xdist, ydist = player(m.target, 'x') - m.x, player(m.target, 'y') - m.y
local dist = math.sqrt(xdist^2 + ydist^2)
if dist < 400 then
m.ang = math.atan2(ydist, xdist)-math.pi/2+math.random(-1, 1)/2
		 	 m.imgang = math.deg(math.atan2(ydist, xdist))+90
else
m.target = nil
end
end
if not m.target then
m:rot(math.random(-1, 1)/2)
end
if not m:move(m:rot(), m.health > m.runat and 1 or -1) then
repeat until m:move(math.rad(math.random(360)), 1)
end
end