Version 1.0

This commit is contained in:
OTCv8
2019-11-03 00:47:50 +01:00
parent 6dd9a54749
commit e65bdb534a
11 changed files with 324 additions and 131 deletions

View File

@@ -15,6 +15,7 @@ local battleTab = addTab("Battle")
local caveTab = addTab("Cave")
local toolsTab = addTab("Tools")
Panels.Eating(battleTab)
Panels.Health(battleTab)
Panels.HealthItem(battleTab)
Panels.ManaItem(battleTab)
@@ -40,6 +41,30 @@ macro(1000, "this macro does nothing", "f7", function()
end, toolsTab)
macro(100, "debug pathfinding", nil, function()
for i, tile in ipairs(g_map.getTiles(posz())) do
tile:setText("")
end
local path = findEveryPath(pos(), 20, {
ignoreNonPathable = false
})
local total = 0
for i, p in pairs(path) do
local s = i:split(",")
local pos = {x=tonumber(s[1]), y=tonumber(s[2]), z=tonumber(s[3])}
local tile = g_map.getTile(pos)
if tile then
tile:setText(p[2])
end
total = total + 1
end
end, toolsTab)
macro(1000, "speed hack", nil, function()
player:setSpeed(1000)
end, toolsTab)
--#hotkeys
hotkey("f5", "example hotkey", function()
@@ -48,6 +73,7 @@ end)
singlehotkey("ctrl+f6", "singlehotkey", function()
info("Wow, you clicked f6 singlehotkey")
usewith(268, player)
end)
--#callbacks
@@ -58,15 +84,6 @@ onPlayerPositionChange(function()
end)
--#other
HTTP.getJSON("https://api.ipify.org/?format=json", function(data, err)
if err then
warn("Whoops! Error occured: " .. err)
return
end
local myIp = data['ip']
end)
]=]},

View File

@@ -39,6 +39,7 @@ function executeBot(config, storage, tabs, msgCallback, saveConfigCallback)
context.math = math
context.table = table
context.string = string
context.tonumber = tonumber
context.tr = tr
context.json = json
context.regexMatch = regexMatch

View File

@@ -51,42 +51,145 @@ context.getPlayerByName = function(name, multifloor)
return nil
end
context.findPath = function(startPos, destPos, maxDist, ignoreFields, ignoreCreatures)
context.findAllPaths = function(start, maxDist, params)
--[[
Available params:
ignoreLastCreature
ignoreCreatures
ignoreNonPathable
ignoreNonWalkable
ignoreStairs
ignoreCost
allowUnseen
]]--
if type(params) ~= 'table' then
params = {}
end
for key, value in pairs(params) do
if value == nil or value == false then
params[key] = 0
elseif value == true then
params[key] = 1
end
end
return g_map.findEveryPath(start, maxDist, params)
end
context.findEveryPath = context.findAllPaths
context.translateAllPathsToPath = function(paths, destPos)
local predirections = {}
local directions = {}
local destPosStr = destPos
if type(destPos) ~= 'string' then
destPosStr = destPos.x .. "," .. destPos.y .. "," .. destPos.z
end
while destPosStr:len() > 0 do
local node = paths[destPosStr]
if not node then
break
end
if node[3] < 0 then
break
end
table.insert(predirections, node[3])
destPosStr = node[4]
end
-- reverse
for i=#predirections,1,-1 do
table.insert(directions, predirections[i])
end
return directions
end
context.translateEveryPathToPath = context.translateAllPathsToPath
context.findPath = function(startPos, destPos, maxDist, params)
--[[
Available params:
ignoreLastCreature
ignoreCreatures
ignoreNonPathable
ignoreNonWalkable
ignoreStairs
ignoreCost
allowUnseen
precision
marginMin
marginMax
]]--
if startPos.z ~= destPos.z then
return
end
if type(maxDist) ~= 'number' then
maxDist = 100
end
local complexity = math.min(10000, maxDist * maxDist)
local flags = 0
if ignoreFields then
flags = flags + 4
if type(params) ~= 'table' then
params = {}
end
if ignoreCreatures then
flags = flags + 16
local destPosStr = destPos.x .. "," .. destPos.y .. "," .. destPos.z
params["destination"] = destPosStr
local paths = context.findAllPaths(startPos, maxDist, params)
local marginMin = params.marginMin or params.minMargin
local marginMax = params.marginMax or params.maxMargin
if type(marginMin) == 'number' and type(marginMax) == 'number' then
local bestCandidate = nil
local bestCandidatePos = nil
for x = -marginMax, marginMax do
for y = -marginMax, marginMax do
if math.abs(x) >= marginMin or math.abs(y) >= marginMin then
local dest = (destPos.x + x) .. "," .. (destPos.y + y) .. "," .. destPos.z
local node = paths[dest]
if node and (not bestCandidate or bestCandidate[1] > node[1]) then
bestCandidate = node
bestCandidatePos = dest
end
end
end
end
if bestCandidate then
return context.translateAllPathsToPath(paths, bestCandidatePos)
end
return
end
return g_map.findPath(startPos, destPos, complexity, flags)
if not paths[destPosStr] then
local precision = params.precision
if type(precision) == 'number' then
for p = 1, precision do
local bestCandidate = nil
local bestCandidatePos = nil
for x = -p, p do
for y = -p, p do
local dest = (destPos.x + x) .. "," .. (destPos.y + y) .. "," .. destPos.z
local node = paths[dest]
if node and (not bestCandidate or bestCandidate[1] > node[1]) then
bestCandidate = node
bestCandidatePos = dest
end
end
end
if bestCandidate then
return context.translateAllPathsToPath(paths, bestCandidatePos)
end
end
end
return nil
end
return context.translateAllPathsToPath(paths, destPos)
end
context.getPath = context.findPath
context.autoWalk = function(destination, maxDist, params)
-- Available params same as for findPath
local path = context.findPath(context.player:getPosition(), destination, maxDist, params)
if not path then
return false
end
-- autowalk without prewalk animation
g_game.autoWalk(path, {x=0,y=0,z=0})
return true
end
context.autoWalk = function(destination, maxDist, ignoreFields, ignoreCreatures)
if maxDist == nil then
maxDist = 100
end
if ignoreFields == nil then
ignoreFields = false
end
if ignoreCreatures == nil then
ignoreCreatures = false
end
if context.player:getPosition().z ~= destination.z then
return false
end
local path = context.findPath(context.player:getPosition(), destination, maxDist, ignoreFields, ignoreCreatures)
if #path < 1 then
return false
end
if g_game.getFeature(GameNewWalking) then
g_game.autoWalk(path, context.player:getPosition())
else
g_game.autoWalk(path, {x=0,y=0,z=0})
end
return true
end

View File

@@ -621,6 +621,8 @@ Panel
end
refreshConfig()
-- processing
local getMonsterConfig = function(monster)
if monsters[monster:getName():lower()] then
@@ -639,21 +641,28 @@ Panel
return -1
end
local distance = context.getDistanceBetween(context.player:getPosition(), monster:getPosition())
if distance > 10 then
local pos = context.player:getPosition()
local mpos = monster:getPosition()
local hp = monster:getHealthPercent()
if config.minHealth > hp or config.maxHealth < hp then
return -1
end
local maxDistance = 5
if config.chase and hp < 20 then
maxDistance = 7
end
local distance = math.max(math.abs(pos.x-mpos.x), math.abs(pos.y-mpos.y))
if distance > maxDistance then
return -1
end
local mpos = monster:getPosition()
local hasPath = false
for x=-1,1 do
for y=-1,1 do
local pathTo = context.findPath(context.player:getPosition(), {x=mpos.x-x, y=mpos.y-y, z=mpos.z}, 100, true, false)
if #pathTo > 0 then
hasPath = true
break
end
end
local pathTo = context.findPath(context.player:getPosition(), {x=mpos.x, y=mpos.y, z=mpos.z}, 10, { ignoreNonPathable = true, precision=1 })
if pathTo then
hasPath = true
end
if distance > 2 and not hasPath then
return -1
@@ -667,19 +676,26 @@ Panel
priority = priority + 10
end
if distance <= 2 then
priority = priority + 20
priority = priority + 10
end
if distance <= 1 then
priority = priority + 10
end
if monster:getHealthPercent() <= 10 then
if hp <= 20 and config.chase then
priority = priority + 30
end
if hp <= 10 then
priority = priority + 10
end
if monster:getHealthPercent() <= 25 then
if hp <= 25 then
priority = priority + 10
end
if monster:getHealthPercent() <= 50 then
if hp <= 50 then
priority = priority + 10
end
if monster:getHealthPercent() <= 75 then
if hp <= 75 then
priority = priority + 10
end
@@ -720,13 +736,13 @@ Panel
table.remove(lootContainers, 1)
return true
end
if lootTries >= 5 then
lootTries = 0
table.remove(lootContainers, 1)
return true
end
local dist = math.max(math.abs(pos.x-cpos.x), math.abs(pos.y-cpos.y))
local dist = math.max(math.abs(pos.x-cpos.x), math.abs(pos.y-cpos.y))
if dist <= 5 then
local tile = g_map.getTile(cpos)
if not tile then
@@ -739,7 +755,8 @@ Panel
table.remove(lootContainers, 1)
return true
end
topItem:setMarked('orange')
if dist <= 1 then
lootTries = lootTries + 1
openContainerRequest = context.now
@@ -755,30 +772,19 @@ Panel
end
lootTries = lootTries + 1
if context.autoWalk(cpos, 100 + dist * 2) then
if context.autoWalk(cpos, 20, { precision = 1}) then
return true
end
if context.autoWalk(cpos, 100 + dist * 2, true) then
if context.autoWalk(cpos, 20, { ignoreNonPathable = true, precision = 1}) then
return true
end
for i=1,5 do
local cpos2 = {x=cpos.x + math.random(-1, 1),y = cpos.y + math.random(-1, 1), z = cpos.z}
if context.autoWalk(cpos2, 100 + dist * 2) then
return true
end
end
-- try again, ignore field
for i=1,5 do
local cpos2 = {x=cpos.x + math.random(-1, 1),y = cpos.y + math.random(-1, 1), z = cpos.z}
if context.autoWalk(cpos2, 100 + dist * 2, true) then
return true
end
if context.autoWalk(cpos, 20, { ignoreNonPathable = true, precision = 2}) then
return true
end
-- ignore fields and monsters
if context.autoWalk(cpos, 100 + dist * 2, true, true) then
if context.autoWalk(cpos, 20, { ignoreNonPathable = true, ignoreCreatures = true, precision = 2}) then
return true
end
else
@@ -817,6 +823,7 @@ Panel
return
end
topItem:setMarked('blue')
table.insert(lootContainers, tpos)
end)
@@ -916,40 +923,23 @@ Panel
local distance = math.max(math.abs(offsetX), math.abs(offsetY))
if config.keepDistance then
if (distance == config.distance or distance == config.distance + 1) then
local minDistance = config.distance
if target:getHealthPercent() < 20 and config.chase and danger < 10 then
minDistance = 1
end
if (distance == minDistance or distance == minDistance + 1) then
return
else
local bestDist = 10
local bestPos = pos
for i=1,5 do
local testPos = {x=pos.x + math.random(-3,3), y=pos.y + math.random(-3,3), z=pos.z}
local dist = math.abs(config.distance - math.max(math.abs(tpos.x - testPos.x), math.abs(tpos.y - testPos.y)))
if dist < bestDist then
local path = context.findPath(pos, testPos, 100, false, false)
if #path > 0 then
bestPos = testPos
bestDist = dist
if not context.autoWalk(tpos, 8, { minMargin=minDistance, maxMargin=minDistance + 1}) then
if not context.autoWalk(tpos, 8, { ignoreNonPathable = true, minMargin=minDistance, maxMargin=minDistance + 1}) then
if not context.autoWalk(tpos, 8, { ignoreNonPathable = true, ignoreCreatures = true, minMargin=minDistance, maxMargin=minDistance + 2}) then
return
end
end
end
if bestDist > 1 then
for i=1,10 do
local testPos = {x=pos.x + math.random(-4,4), y=pos.y + math.random(-4,4), z=pos.z}
local dist = math.abs(config.distance - math.max(math.abs(tpos.x - testPos.x), math.abs(tpos.y - testPos.y)))
if dist < bestDist then
local path = context.findPath(pos, testPos, 100, true, false)
if #path > 0 then
bestPos = testPos
bestDist = dist
end
end
end
end
if bestDist < 10 then
context.autoWalk(bestPos, 100, true, false)
context.delay(300)
end
context.delay(300)
end
return
end
@@ -971,16 +961,14 @@ Panel
end
if distance > 1 then
for x=-1,1 do
for y=-1,1 do
if context.autoWalk({x=tpos.x-x, y=tpos.y-y, z=tpos.z}, 100, true, false) then
if not context.autoWalk(tpos, 8, { precision = 1}) then
if not context.autoWalk(tpos, 8, { ignoreNonPathable = true, precision = 1}) then
if not context.autoWalk(tpos, 8, { ignoreNonPathable = true, precision = 2}) then
return
end
end
end
if not context.autoWalk(tpos, 100, false, true) then
context.autoWalk(tpos, 100, true, true)
end
context.delay(300)
end
end)
end

View File

@@ -316,6 +316,87 @@ Panel
end
Panels.ManaItem = Panels.Mana
Panels.Eating = function(parent)
if not parent then
parent = context.panel
end
local panelName = "autoEatingPanel"
local panelId = 1
while parent:getChildById(panelName .. panelId) do
panelId = panelId + 1
end
panelName = panelName .. panelId
local ui = context.setupUI([[
Panel
height: 55
margin-top: 2
Label
id: info
anchors.left: parent.left
anchors.right: parent.right
anchors.top: parent.top
text: Auto Eating
text-align: center
BotItem
id: item1
anchors.left: parent.left
anchors.top: prev.bottom
margin-top: 3
margin-left: 10
BotItem
id: item2
anchors.horizontalCenter: parent.horizontalCenter
anchors.top: prev.top
BotItem
id: item3
anchors.right: parent.right
anchors.top: prev.top
margin-right: 10
]], parent)
ui:setId(panelName)
if not context.storage["autoEating" .. panelId] then
context.storage["autoEating" .. panelId] = {}
end
ui.item1.onItemChange = function(widget)
context.storage["autoEating" .. panelId][1] = widget:getItemId()
end
ui.item1:setItemId(context.storage["autoEating" .. panelId][1] or 3725)
ui.item2.onItemChange = function(widget)
context.storage["autoEating" .. panelId][2] = widget:getItemId()
end
ui.item2:setItemId(context.storage["autoEating" .. panelId][2] or 0)
ui.item3.onItemChange = function(widget)
context.storage["autoEating" .. panelId][3] = widget:getItemId()
end
ui.item3:setItemId(context.storage["autoEating" .. panelId][3] or 0)
context.macro(15000, function()
local candidates = {}
for i, item in pairs(context.storage["autoEating" .. panelId]) do
if item >= 100 then
table.insert(candidates, item)
end
end
if #candidates == 0 then
return
end
context.usewith(candidates[math.random(1, #candidates)], context.player)
end)
end
Panels.ManaItem = Panels.Mana
Panels.Turning = function(parent)
context.macro(1000, "Turning / AntiIdle", nil, function()
context.turn(math.random(1, 4))

View File

@@ -488,7 +488,7 @@ Panel
context.onContainerOpen(function(container)
if container:getItemsCount() > 0 then
lastOpenedContainer = context.now
lastOpenedContainer = context.now + container:getItemsCount() * 100
end
end)
@@ -547,32 +547,35 @@ Panel
if distance > 100 or position.z ~= context.player:getPosition().z then
lastGotoSuccesful = false
elseif distance > 0 then
commandExecutionNo = commandExecutionNo + 1
lastGotoSuccesful = false
if commandExecutionNo <= 3 then -- try max 3 times
if not context.autoWalk(position, 100 + distance * 2, commandExecutionNo > 1, false) then
if commandExecutionNo > 1 then
context.autoWalk(position, 100 + distance * 2, true, true) -- ignore creatures
end
context.delay(500)
return
end
return
elseif commandExecutionNo == 4 then -- try last time, location close to destination
for i=1,3 do
position.x = position.x + math.random(-1, 1)
position.y = position.y + math.random(-1, 1)
if context.autoWalk(position, 100 + distance * 2, true) then
if not context.findPath(context.player:getPosition(), position, 100, { ignoreNonPathable = true, precision = 1, ignoreCreatures = true }) then
lastGotoSuccesful = false
executeNextMacroCall = true
else
commandExecutionNo = commandExecutionNo + 1
lastGotoSuccesful = false
if commandExecutionNo <= 3 then -- try max 3 times
if not context.autoWalk(position, distance * 2, { ignoreNonPathable = false }) then
if commandExecutionNo > 1 then
if context.autoWalk(position, distance * 2, { ignoreNonPathable = true, precision = 1 }) then
context.delay(500)
end
end
return
end
return
elseif commandExecutionNo == 4 then -- try last time, location close to destination
if context.autoWalk(position, distance * 2, { ignoreNonPathable = true, ignoreLastCreature = true, precision = 2, allowUnseen = true }) then
context.delay(500)
return
end
elseif distance <= 2 then
lastGotoSuccesful = true
executeNextMacroCall = true
end
elseif distance <= 2 then
lastGotoSuccesful = true
executeNextMacroCall = lastGotoSuccesful
end
else
lastGotoSuccesful = true
executeNextMacroCall = lastGotoSuccesful
executeNextMacroCall = true
end
else
context.error("Waypoints: invalid use of goto function")