map.lua

main 241 lines · 7.1 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1local context = G.botContext
2
3context.getMapView = function() return modules.game_interface.getMapPanel() end
4context.getMapPanel = context.getMapView
5context.zoomIn = function() modules.game_interface.getMapPanel():zoomIn() end
6context.zoomOut = function() modules.game_interface.getMapPanel():zoomOut() end
7
8context.getSpectators = function(param1, param2)
9--[[
10 if param1 is table (position) then it's used for central position, then param2 is used as param1
11 if param1 is creature, then creature position and direction of creature is used, then param2 is used as param1
12 if param1 is true/false then it's used for multifloor, example: getSpectators(true)
13 if param1 is string then it's used for getSpectatorsByPattern
14]]--
15 local pos = context.player:getPosition()
16 local direction = context.player:getDirection()
17 if type(param1) == 'table' then
18 pos = param1
19 direction = 8 -- invalid direction
20 param1 = param2
21 end
22 if type(param1) == 'userdata' then
23 pos = param1:getPosition()
24 direction = param1:getDirection()
25 param1 = param2
26 end
27
28 if type(param1) == 'string' then
29 return g_map.getSpectatorsByPattern(pos, param1, direction)
30 end
31
32 local multifloor = false
33 if type(param1) == 'boolean' and param1 == true then
34 multifloor = true
35 end
36 return g_map.getSpectators(pos, multifloor)
37end
38
39context.getCreatureById = function(id, multifloor)
40 if type(id) ~= 'number' then return nil end
41 if multifloor ~= true then
42 multifloor = false
43 end
44 for i, spec in ipairs(g_map.getSpectators(context.player:getPosition(), multifloor)) do
45 if spec:getId() == id then
46 return spec
47 end
48 end
49 return nil
50end
51
52context.getCreatureByName = function(name, multifloor)
53 if not name then return nil end
54 name = name:lower()
55 if multifloor ~= true then
56 multifloor = false
57 end
58 for i, spec in ipairs(g_map.getSpectators(context.player:getPosition(), multifloor)) do
59 if spec:getName():lower() == name then
60 return spec
61 end
62 end
63 return nil
64end
65
66context.getPlayerByName = function(name, multifloor)
67 if not name then return nil end
68 name = name:lower()
69 if multifloor ~= true then
70 multifloor = false
71 end
72 for i, spec in ipairs(g_map.getSpectators(context.player:getPosition(), multifloor)) do
73 if spec:isPlayer() and spec:getName():lower() == name then
74 return spec
75 end
76 end
77 return nil
78end
79
80context.findAllPaths = function(start, maxDist, params)
81 --[[
82 Available params:
83 ignoreLastCreature
84 ignoreCreatures
85 ignoreNonPathable
86 ignoreNonWalkable
87 ignoreStairs
88 ignoreCost
89 allowUnseen
90 allowOnlyVisibleTiles
91 ]]--
92 if type(params) ~= 'table' then
93 params = {}
94 end
95 for key, value in pairs(params) do
96 if value == nil or value == false then
97 params[key] = 0
98 elseif value == true then
99 params[key] = 1
100 end
101 end
102 return g_map.findEveryPath(start, maxDist, params)
103end
104context.findEveryPath = context.findAllPaths
105
106context.translateAllPathsToPath = function(paths, destPos)
107 local predirections = {}
108 local directions = {}
109 local destPosStr = destPos
110 if type(destPos) ~= 'string' then
111 destPosStr = destPos.x .. "," .. destPos.y .. "," .. destPos.z
112 end
113
114 while destPosStr:len() > 0 do
115 local node = paths[destPosStr]
116 if not node then
117 break
118 end
119 if node[3] < 0 then
120 break
121 end
122 table.insert(predirections, node[3])
123 destPosStr = node[4]
124 end
125 -- reverse
126 for i=#predirections,1,-1 do
127 table.insert(directions, predirections[i])
128 end
129 return directions
130end
131context.translateEveryPathToPath = context.translateAllPathsToPath
132
133
134context.findPath = function(startPos, destPos, maxDist, params)
135 --[[
136 Available params:
137 ignoreLastCreature
138 ignoreCreatures
139 ignoreNonPathable
140 ignoreNonWalkable
141 ignoreStairs
142 ignoreCost
143 allowUnseen
144 allowOnlyVisibleTiles
145 precision
146 marginMin
147 marginMax
148 ]]--
149 if not destPos or startPos.z ~= destPos.z then
150 return
151 end
152 if type(maxDist) ~= 'number' then
153 maxDist = 100
154 end
155 if type(params) ~= 'table' then
156 params = {}
157 end
158 local destPosStr = destPos.x .. "," .. destPos.y .. "," .. destPos.z
159 params["destination"] = destPosStr
160 local paths = context.findAllPaths(startPos, maxDist, params)
161 local marginMin = params.marginMin or params.minMargin
162 local marginMax = params.marginMax or params.maxMargin
163 if type(marginMin) == 'number' and type(marginMax) == 'number' then
164 local bestCandidate = nil
165 local bestCandidatePos = nil
166 for x = -marginMax, marginMax do
167 for y = -marginMax, marginMax do
168 if math.abs(x) >= marginMin or math.abs(y) >= marginMin then
169 local dest = (destPos.x + x) .. "," .. (destPos.y + y) .. "," .. destPos.z
170 local node = paths[dest]
171 if node and (not bestCandidate or bestCandidate[1] > node[1]) then
172 bestCandidate = node
173 bestCandidatePos = dest
174 end
175 end
176 end
177 end
178 if bestCandidate then
179 return context.translateAllPathsToPath(paths, bestCandidatePos)
180 end
181 return
182 end
183
184 if not paths[destPosStr] then
185 local precision = params.precision
186 if type(precision) == 'number' then
187 for p = 1, precision do
188 local bestCandidate = nil
189 local bestCandidatePos = nil
190 for x = -p, p do
191 for y = -p, p do
192 local dest = (destPos.x + x) .. "," .. (destPos.y + y) .. "," .. destPos.z
193 local node = paths[dest]
194 if node and (not bestCandidate or bestCandidate[1] > node[1]) then
195 bestCandidate = node
196 bestCandidatePos = dest
197 end
198 end
199 end
200 if bestCandidate then
201 return context.translateAllPathsToPath(paths, bestCandidatePos)
202 end
203 end
204 end
205 return nil
206 end
207
208 return context.translateAllPathsToPath(paths, destPos)
209end
210context.getPath = context.findPath
211
212-- also works as autoWalk(dirs) where dirs is a list eg.: {1,2,3,0,1,1,2,}
213context.autoWalk = function(destination, maxDist, params)
214 if type(destination) == "table" and table.isList(destination) and not maxDist and not params then
215 g_game.autoWalk(destination, {x=0,y=0,z=0})
216 return true
217 end
218
219 -- Available params same as for findPath
220 local path = context.findPath(context.player:getPosition(), destination, maxDist, params)
221 if not path then
222 return false
223 end
224 -- autowalk without prewalk animation
225 g_game.autoWalk(path, {x=0,y=0,z=0})
226 return true
227end
228
229context.getTileUnderCursor = function()
230 if not modules.game_interface.gameMapPanel.mousePos then return end
231 return modules.game_interface.gameMapPanel:getTile(modules.game_interface.gameMapPanel.mousePos)
232end
233
234context.canShoot = function(pos, distance)
235 if not distance then distance = 5 end
236 local tile = g_map.getTile(pos, distance)
237 if tile then
238 return tile:canShoot(distance)
239 end
240 return false
241end
Top