-
Notifications
You must be signed in to change notification settings - Fork 39
/
Change target if can face creature.lua
160 lines (151 loc) · 5.61 KB
/
Change target if can face creature.lua
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
--[[
Script Name: Change target if can face creature
Description: Step and turn face to target for exori vis shooting example.
Author: Ascer - example
]]
local STEP_DELAY = 500 -- step every x ms to avoid overdash.
local ALLOW_WALK_IDS = {123} -- enter here id such as parcels, boxes etc we check for it.
local MONSTERS = {"Wild warrior", "valkyrie", "war wolf", "skeleton", "rat"} -- monsters to active
-- DON'T EDIT BELOW THIS LINE
local stepTime = 0
-- lower case table with monsters names
MONSTERS = table.lower(MONSTERS)
----------------------------------------------------------------------------------------------------------------------------------------------------------
--> Function: tileIsWalkable(x, y, z)
--> Description: Check is tile is walkable
--> Params:
--> @x coordinate in the map on the x-axis
--> @y coordinate in the map on the y-axis
--> @z coordinate in the map on the z-axis
-->
--> Return: boolean true or false
----------------------------------------------------------------------------------------------------------------------------------------------------------
function tileIsWalkable(x, y, z)
local path = 0
local items = Map.GetItems(x, y, z)
for i, item in ipairs(items) do
if item.id == 99 then return false end
if Item.hasAttribute(item.id, ITEM_FLAG_NOT_WALKABLE) then return false end
if Item.hasAttribute(item.id, ITEM_FLAG_NOT_PATHABLE) then
if table.find(ALLOW_WALK_IDS, item.id) then
path = path + 1
if path >= 2 then return false end
else
return false
end
end
end
return true
end
----------------------------------------------------------------------------------------------------------------------------------------------------------
--> Function: getAttackedCreature()
--> Description: Get creature you already attack
--> Params: None
-->
--> Return: -1 if no creature else table with creature
----------------------------------------------------------------------------------------------------------------------------------------------------------
function getAttackedCreature()
local t = Self.TargetID()
if t <= 0 then return -1 end
local c = Creature.getCreatures(t)
if table.count(c) < 2 then return -1 end
if c.hpperc <= 0 then return - 1 end
return c
end
----------------------------------------------------------------------------------------------------------------------------------------------------------
--> Function: delayedStep(dir)
--> Description: Get creature you already attack
--> Params:
-->
--> @dir number direction to step
--> Return: none
----------------------------------------------------------------------------------------------------------------------------------------------------------
function delayedStep(dir)
if os.clock() - stepTime > (STEP_DELAY / 1000) then
Self.Step(dir)
stepTime = os.clock()
end
end
----------------------------------------------------------------------------------------------------------------------------------------------------------
--> Function: getNewTarget(list, range)
--> Description: Get table with creature to attack.
--> Class: None
--> Params:
--> @list table with creature names
--> @range number max distance between you and creature
--> Return: table with monster info or -1 if not found.
----------------------------------------------------------------------------------------------------------------------------------------------------------
function getNewTarget(list, range)
local target = Self.TargetID()
if target > 0 then
local creatures = Creature.iCreatures(range, false)
for i = 1, #creatures do
local creature = creatures[i]
if creature.id ~= target then
return creature
end
end
else
local creatures = Creature.iCreatures(range, false)
for i = 1, #creatures do
local creature = creatures[i]
if table.find(list, creature.name) and creature.hpperc > HPPERC then
return creature
end
end
end
return -1
end
function changeTarget()
local new = getNewTarget(MONSTERS, 5)
if table.count(new) > 1 then
Creature.Attack(new.id)
else
Self.Stop()
end
end --> Attack new creature from list or stop attack current
-- module run function in loop 200ms
Module.New("Change target if can face creature", function()
local s = Self.Position()
if Self.isConnected() then
local t = getAttackedCreature()
if t ~= -1 then
if table.find(MONSTERS, string.lower(t.name)) then
local s = Self.Position()
if t.x == (s.x - 1) and t.y == (s.y - 1) then -- north west
if tileIsWalkable(s.x, s.y - 1, s.z) then
delayedStep(0)
elseif tileIsWalkable(s.x - 1, s.y, s.z) then
delayedStep(3)
else
changeTarget()
end
elseif t.x == (s.x + 1) and t.y == (s.y - 1) then
if tileIsWalkable(s.x, s.y - 1, s.z) then
delayedStep(0)
elseif tileIsWalkable(s.x + 1, s.y, s.z) then
delayedStep(1)
else
changeTarget()
end
elseif t.x == (s.x + 1) and t.y == (s.y + 1) then
if tileIsWalkable(s.x, s.y + 1, s.z) then
delayedStep(2)
elseif tileIsWalkable(s.x + 1, s.y, s.z) then
delayedStep(1)
else
changeTarget()
end
elseif t.x == (s.x - 1) and t.y == (s.y + 1) then
if tileIsWalkable(s.x, s.y + 1, s.z) then
delayedStep(2)
elseif tileIsWalkable(s.x - 1, s.y, s.z) then
delayedStep(3)
else
changeTarget()
end
end
end
end
end
end)