-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathluniabot.lua
More file actions
447 lines (398 loc) · 13.7 KB
/
luniabot.lua
File metadata and controls
447 lines (398 loc) · 13.7 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
local defaultOptions = {
enableAudio = false,
}
local walkEvent = nil
local stop = false
local isAttacking = false
local isFollowing = false
local currentTargetPositionId = 1
local waypoints = {}
local autowalkTargetPosition = waypoints[currentTargetPositionId]
local atkLoopId = nil
local atkSpellLoopId = nil
local itemHealingLoopId = nil
local spellHealingLoopId = nil
local manaLoopId = nil
local hasteLoopId = nil
local buffLoopId = nil
local hasLured = false
local shieldLoopId = nil
local player = nil
local healingItem
local manaItem
function init()
luniaBotWindow = g_ui.displayUI('luniabot')
player = g_game.getLocalPlayer()
waypointList = luniaBotWindow.waypoints
luniaBotWindow:hide()
luniaBotButton = modules.client_topmenu.addLeftGameButton('luniaBotButton', tr('LuniaBot'), '/otcbot-master/luniabot', toggle)
atkButton = luniaBotWindow.autoAttack
walkButton = luniaBotWindow.walking
healthSpellButton = luniaBotWindow.AutoHealSpell
healthItemButton = luniaBotWindow.AutoHealItem
manaRestoreButton = luniaBotWindow.AutoMana
atkSpellButton = luniaBotWindow.AtkSpell
manaTrainButton = luniaBotWindow.ManaTrain
hasteButton = luniaBotWindow.AutoHaste
buffButton = luniaBotWindow.AutoBuff
lureButton = luniaBotWindow.LureMonsters
manaShieldButton = luniaBotWindow.AutoManaShield
healthItemButton.onCheckChange = autoHealPotion
manaRestoreButton.onCheckChange = autoManaPotion
luniaBotWindow.AtkSpellText.onTextChange = saveBotText
luniaBotWindow.HealSpellText.onTextChange = saveBotText
luniaBotWindow.HealthSpellPercent.onTextChange = saveBotText
luniaBotWindow.HealItem.onTextChange = saveBotText
luniaBotWindow.HealItemPercent.onTextChange = saveBotText
luniaBotWindow.ManaItem.onTextChange = saveBotText
luniaBotWindow.ManaPercent.onTextChange = saveBotText
luniaBotWindow.WptName.onTextChange = saveBotText
luniaBotWindow.ManaSpellText.onTextChange = saveBotText
luniaBotWindow.ManaTrainPercent.onTextChange = saveBotText
luniaBotWindow.HasteText.onTextChange = saveBotText
luniaBotWindow.BuffText.onTextChange = saveBotText
luniaBotWindow.LureMinimum.onTextChange = saveBotText
luniaBotWindow.LureMaximum.onTextChange = saveBotText
connect(g_game, { onGameStart = logIn})
end
function saveBotText()
g_settings.set(player:getName() .. " " .. luniaBotWindow:getFocusedChild():getId(), luniaBotWindow:getFocusedChild():getText())
end
function logIn()
player = g_game.getLocalPlayer()
--Fixes default values
if(luniaBotWindow.HealItem:getText()) == ",266" then
luniaBotWindow.HealItem:setText('266')
end
if(luniaBotWindow.ManaItem:getText()) == ",268" then
luniaBotWindow.ManaItem:setText('268')
end
local checkButtons = {atkButton, healthSpellButton, walkButton, healthItemButton, manaRestoreButton, atkSpellButton, manaTrainButton, hasteButton, manaShieldButton, buffButton, lureButton}
for _,checkButton in ipairs(checkButtons) do
checkButton:setChecked(g_settings.getBoolean(player:getName() .. " " .. checkButton:getId()))
end
local textBoxes = {luniaBotWindow.ManaSpellText, luniaBotWindow.HasteText, luniaBotWindow.AtkSpellText, luniaBotWindow.HealSpellText, luniaBotWindow.HealthSpellPercent, luniaBotWindow.HealItem, luniaBotWindow.HealItemPercent, luniaBotWindow.ManaItem, luniaBotWindow.ManaPercent, luniaBotWindow.WptName, luniaBotWindow.BuffText, luniaBotWindow.LureMinimum, luniaBotWindow.LureMaximum}
for _,textBox in ipairs(textBoxes) do
local storedText = g_settings.get(player:getName() .. " " .. textBox:getId())
if (string.len(storedText) >= 1) then
textBox:setText(g_settings.get(player:getName() .. " " .. textBox:getId()))
end
end
end
function terminate()
luniaBotWindow:destroy()
luniaBotButton:destroy()
end
function disable()
luniaBotButton:hide()
end
function hide()
luniaBotWindow:hide()
end
function show()
luniaBotWindow:show()
luniaBotWindow:raise()
luniaBotWindow:focus()
end
function toggleLoop(key)
--maybe remove some looops, for example healing could be done through events
local bts = {
autoAttack = {atkLoop, atkLoopId},
walking = {walkToTarget, walkEvent},
AutoHealSpell = {healingSpellLoop, spellHealingLoopId},
AtkSpell = {atkSpellLoop, atkSpellLoopId},
ManaTrain = {manaTrainLoop, manaLoopId},
AutoHaste = {hasteLoop, hasteLoopId},
AutoManaShield = {shieldLoop, shieldLoopId},
AutoBuff = {buffLoop, buffLoopId},
}
local btn = luniaBotWindow:getChildById(key)
local bt = bts[btn:getId()]
if (btn:isChecked()) then
g_settings.set(player:getName() .. " " .. btn:getId(), true)
if (bt) then
bt[1]()
end
else
g_settings.set(player:getName() .. " " .. btn:getId(), false)
if (bt) then
removeEvent(bt[2])
end
end
end
function autoHealPotion()
healingItem = healthItemButton:isChecked()
g_settings.set(player:getName() .. " " .. healthItemButton:getId(), healthItemButton:isChecked())
if (healingItem and itemHealingLoopId == nil) then
itemHealingLoop()
end
if (not manaItem and not healingItem) then
removeEvent(itemHealingLoopId)
itemHealingLoopId = nil
end
end
function autoManaPotion()
manaItem = manaRestoreButton:isChecked()
g_settings.set(player:getName() .. " " .. manaRestoreButton:getId(), manaRestoreButton:isChecked())
if (manaItem and itemHealingLoopId == nil) then
itemHealingLoop()
end
if (not manaItem and not healingItem) then
removeEvent(itemHealingLoopId)
itemHealingLoopId = nil
end
end
function toggle()
if luniaBotWindow:isVisible() then
hide()
else
show()
end
end
local function getDistanceBetween(p1, p2)
return math.max(math.abs(p1.x - p2.x), math.abs(p1.y - p2.y))
end
function Player.canAttack(self)
return not self:hasState(16384) and not g_game.isAttacking()
end
function Creature:canReach(creature)
--function from candybot
if not creature then
return false
end
local myPos = self:getPosition()
local otherPos = creature:getPosition()
local neighbours = {
{x = 0, y = -1, z = 0},
{x = -1, y = -1, z = 0},
{x = -1, y = 0, z = 0},
{x = -1, y = 1, z = 0},
{x = 0, y = 1, z = 0},
{x = 1, y = 1, z = 0},
{x = 1, y = 0, z = 0},
{x = 1, y = -1, z = 0}
}
for k,v in pairs(neighbours) do
local checkPos = {x = myPos.x + v.x, y = myPos.y + v.y, z = myPos.z + v.z}
if postostring(otherPos) == postostring(checkPos) then
return true
end
local steps, result = g_map.findPath(otherPos, checkPos, 40000, 0)
if result == PathFindResults.Ok then
return true
end
end
return false
end
function atkLoop()
if(player:canAttack()) then
local pPos = player:getPosition()
local luredMob = {}
local lureAmount = tonumber(luniaBotWindow.LureMaximum:getText())
local lureMinimum = tonumber(luniaBotWindow.LureMinimum:getText())
local luring = lureButton:isChecked()
if pPos then --solves some weird bug, in the first login, the players position is nil in the start for some reason
local creatures = g_map.getSpectators(pPos, false)
if (luring) then
for _, mob in ipairs(creatures) do
cPos = mob:getPosition()
if getDistanceBetween(pPos, cPos) <= 5 and mob:isMonster() and player:canReach(mob) then
table.insert(luredMob, mob)
end
end
end
if (luring and #luredMob >= lureAmount) then
hasLured = true
end
if (luring and #luredMob <= lureMinimum) then
hasLured = false
end
for _, creature in ipairs(creatures) do
cPos = creature:getPosition()
if getDistanceBetween(pPos, cPos) <= 5 and creature:isMonster() and player:canReach(creature) then
if (not luring or hasLured) then
g_game.attack(creature)
atkLoopId = scheduleEvent(atkLoop, 200)
return
end
end
end
end
end
atkLoopId = scheduleEvent(atkLoop, 200)
end
function fag()
local label = g_ui.createWidget('Waypoint', waypointList)
local pos = player:getPosition()
label:setText(pos.x .. "," .. pos.y .. "," .. pos.z)
table.insert(waypoints, pos)
end
function walkToTarget()
--found this function made by gesior, i edited it abit, maybe there's better ways to walk?
autowalkTargetPosition = waypoints[currentTargetPositionId]
if not g_game.isOnline() then
walkEvent = scheduleEvent(walkToTarget, 500)
return
end
local playerPos = player:getPosition()
if (playerPos and autowalkTargetPosition) then
if (getDistanceBetween(playerPos, autowalkTargetPosition) >= 150) then
currentTargetPositionId = currentTargetPositionId + 1
if (currentTargetPositionId > #waypoints) then
currentTargetPositionId = 1
end
walkEvent = scheduleEvent(walkToTarget, 1500)
return
end
end
-- if g_game.getLocalPlayer():getStepTicksLeft() > 0 then
-- walkEvent = scheduleEvent(walkToTarget, g_game.getLocalPlayer():getStepTicksLeft())
-- return
-- end
if g_game.isAttacking() or isFollowing then
walkEvent = scheduleEvent(walkToTarget, 100)
return
end
if not autowalkTargetPosition then
currentTargetPositionId = currentTargetPositionId + 1
if (currentTargetPositionId > #waypoints) then
currentTargetPositionId = 1
end
walkEvent = scheduleEvent(walkToTarget, 100)
return
end
-- fast search path on minimap (known tiles)
steps, result = g_map.findPath(g_game.getLocalPlayer():getPosition(), autowalkTargetPosition, 5000, 0)
if result == PathFindResults.Ok then
g_game.walk(steps[1], true)
elseif result == PathFindResults.Position then
currentTargetPositionId = currentTargetPositionId + 1
if (currentTargetPositionId > #waypoints) then
currentTargetPositionId = 1
end
else
-- slow search path on minimap, if not found, start 'scanning' map
steps, result = g_map.findPath(g_game.getLocalPlayer():getPosition(), autowalkTargetPosition, 25000, 1)
if result == PathFindResults.Ok then
g_game.walk(steps[1], true)
else
-- can't reach? so skip this waypoint. improve this somehow
currentTargetPositionId = currentTargetPositionId + 1
end
end
-- limit steps to 10 per second (100 ms between steps)
walkEvent = scheduleEvent(walkToTarget, math.max(100, g_game.getLocalPlayer():getStepTicksLeft()))
end
function saveWaypoints()
local saveText = '{\n'
for _,v in pairs(waypoints) do
saveText = saveText .. '{x = '.. v.x ..', y = ' .. v.y .. ', z = ' .. v.z .. '},\n'
end
saveText = saveText .. '}'
local file = io.open('modules/otcbot-master/wpts/'.. luniaBotWindow.WptName:getText() ..'.lua', 'w')
file:write(saveText)
file:close()
end
function loadWaypoints()
local f = io.open('modules/otcbot-master/wpts/'.. luniaBotWindow.WptName:getText() ..'.lua', "rb")
local content = f:read("*all")
f:close()
clearWaypoints()
waypoints = loadstring("return "..content)()
for _,v in ipairs(waypoints) do
local labelt = g_ui.createWidget('Waypoint', waypointList)
labelt:setText(v.x .. "," .. v.y .. "," .. v.z)
end
end
function clearWaypoints()
waypoints = {}
autowalkTargetPosition = currentTargetPositionId
autowalkTargetPosition = waypoints[currentTargetPositionId]
clearLabels()
walkButton:setChecked(false)
end
function clearLabels()
while waypointList:getChildCount() > 0 do
local child = waypointList:getLastChild()
waypointList:destroyChildren(child)
end
end
function itemHealingLoop()
-- Prioritize healing item instead of mana
if healingItem then
local hpItemPercentage = tonumber(luniaBotWindow.HealItemPercent:getText())
local hpItemId = tonumber(luniaBotWindow.HealItem:getText())
if (player:getHealth() <= (player:getMaxHealth() * (hpItemPercentage/100))) then
g_game.useInventoryItemWith(hpItemId, player)
-- maybe don't try using mana after healing item?
end
end
if manaItem then
local manaItemPercentage = tonumber(luniaBotWindow.ManaPercent:getText())
local manaItemId = tonumber(luniaBotWindow.ManaItem:getText())
if (player:getMana() <= (player:getMaxMana() * (manaItemPercentage/100))) then
g_game.useInventoryItemWith(manaItemId, player)
end
end
itemHealingLoopId = scheduleEvent(itemHealingLoop, 250)
end
function healingSpellLoop()
local healingSpellPercentage = tonumber(luniaBotWindow.HealthSpellPercent:getText())
local healSpell = luniaBotWindow.HealSpellText:getText()
if (not player) then
spellHealingLoopId = scheduleEvent(healingSpellLoop, 502)
end
if (player:getHealth() <= (player:getMaxHealth() * (healingSpellPercentage/100))) then
g_game.talk(healSpell)
end
spellHealingLoopId = scheduleEvent(healingSpellLoop, 502)
end
function manaTrainLoop()
local manaTrainPercentage = tonumber(luniaBotWindow.ManaTrainPercent:getText())
local manaSpell = luniaBotWindow.ManaSpellText:getText()
if (not player) then
manaLoopId = scheduleEvent(manaTrainLoop, 1000)
end
if (player:getMana() >= (player:getMaxMana() * (manaTrainPercentage/100))) then
g_game.talk(manaSpell)
end
manaLoopId = scheduleEvent(manaTrainLoop, 1000)
end
function hasteLoop()
local hasteSpell = luniaBotWindow.HasteText:getText()
if (not player) then
hasteLoopId = scheduleEvent(hasteLoop, 1000)
end
if (player:getHealth() >= (player:getMaxHealth() * (70/100))) then -- only cast when healthy
if (not player:hasState(PlayerStates.Haste, player:getStates())) then
g_game.talk(hasteSpell)
end
end
hasteLoopId = scheduleEvent(hasteLoop, 1000)
end
function buffLoop()
local buffSpell = luniaBotWindow.BuffText:getText()
if (not player) then
buffLoopId = scheduleEvent(buffLoop, 1000)
end
if (not player:hasState(PlayerStates.PartyBuff, player:getStates())) then
g_game.talk(buffSpell)
end
buffLoopId = scheduleEvent(buffLoop, 1000)
end
function shieldLoop()
if (not player) then
shieldLoopId = scheduleEvent(shieldLoop, 1000)
end
if (not player:hasState(PlayerStates.ManaShield, player:getStates())) then
g_game.talk('utamo vita')
end
shieldLoopId = scheduleEvent(shieldLoop, 1000)
end
function atkSpellLoop()
local atkSpell = luniaBotWindow.AtkSpellText:getText()
if (g_game.isAttacking()) then
g_game.talk(atkSpell)
end
atkSpellLoopId = scheduleEvent(atkSpellLoop, 502)
end