mirror of
https://github.com/OTCv8/otclientv8.git
synced 2025-10-20 06:33:26 +02:00
Version 1.8
This commit is contained in:
@@ -8,12 +8,24 @@ context.callback = function(callbackType, callback)
|
||||
if callbackType == "onAddThing" or callbackType == "onRemoveThing" then
|
||||
g_game.enableTileThingLuaCallback(true)
|
||||
end
|
||||
|
||||
local desc = "lua"
|
||||
local info = debug.getinfo(2, "Sl")
|
||||
if info then
|
||||
desc = info.short_src .. ":" .. info.currentline
|
||||
end
|
||||
|
||||
local callbackData = {}
|
||||
table.insert(context._callbacks[callbackType], function(...)
|
||||
if not callbackData.delay or callbackData.delay < context.now then
|
||||
local prevExecution = context._currentExecution
|
||||
context._currentExecution = callbackData
|
||||
local start = g_clock.realMillis()
|
||||
callback(...)
|
||||
local executionTime = g_clock.realMillis() - start
|
||||
if executionTime > 100 then
|
||||
context.warning("Slow " .. callbackType .. " (" .. executionTime .. "ms): " .. desc)
|
||||
end
|
||||
context._currentExecution = prevExecution
|
||||
end
|
||||
end)
|
||||
|
174
modules/game_bot/functions/icon.lua
Normal file
174
modules/game_bot/functions/icon.lua
Normal file
@@ -0,0 +1,174 @@
|
||||
local context = G.botContext
|
||||
|
||||
local iconsWithoutPosition = 0
|
||||
|
||||
context.addIcon = function(id, options, callback)
|
||||
--[[
|
||||
Available options:
|
||||
item: {id=2160, count=100}
|
||||
outfit: outfit table ({})
|
||||
text: string
|
||||
x: float (0.0 - 1.0)
|
||||
y: float (0.0 - 1.0)
|
||||
width: number
|
||||
height: number
|
||||
hotkey: string
|
||||
switchable: true / false [default: true]
|
||||
movable: true / false [default: true]
|
||||
phantom: true / false [defaule: false]
|
||||
]]--
|
||||
local panel = modules.game_interface.gameMapPanel
|
||||
if type(id) ~= "string" or id:len() < 1 then
|
||||
return context.error("Invalid id for addIcon")
|
||||
end
|
||||
if options.switchable == false and type(callback) ~= 'function' then
|
||||
return context.error("Invalid callback for addIcon")
|
||||
end
|
||||
if type(context.storage._icons) ~= "table" then
|
||||
context.storage._icons = {}
|
||||
end
|
||||
if type(context.storage._icons[id]) ~= "table" then
|
||||
context.storage._icons[id] = {}
|
||||
end
|
||||
local config = context.storage._icons[id]
|
||||
local widget = g_ui.createWidget("BotIcon", panel)
|
||||
widget.botWidget = true
|
||||
|
||||
if type(config.x) ~= 'number' and type(config.y) ~= 'number' then
|
||||
if type(options.x) == 'number' and type(options.y) == 'number' then
|
||||
config.x = math.min(1.0, math.max(0.0, options.x))
|
||||
config.y = math.min(1.0, math.max(0.0, options.y))
|
||||
else
|
||||
config.x = 0.01 + math.floor(iconsWithoutPosition / 5) / 10
|
||||
config.y = 0.05 + (iconsWithoutPosition % 5) / 5
|
||||
iconsWithoutPosition = iconsWithoutPosition + 1
|
||||
end
|
||||
end
|
||||
|
||||
if options.item then
|
||||
if type(options.item) == 'number' then
|
||||
widget.item:setItemId(options.item)
|
||||
else
|
||||
widget.item:setItemId(options.item.id)
|
||||
widget.item:setItemCount(options.item.count or 1)
|
||||
widget.item:setShowCount(false)
|
||||
end
|
||||
end
|
||||
|
||||
if options.outfit then
|
||||
widget.creature:setOutfit(options.outfit)
|
||||
end
|
||||
|
||||
if options.switchable == false then
|
||||
widget.status:hide()
|
||||
widget.status:setOn(true)
|
||||
else
|
||||
widget.status:setOn(config.enabled)
|
||||
end
|
||||
|
||||
if options.text then
|
||||
if options.switchable ~= false then
|
||||
widget.status:hide()
|
||||
if widget.status:isOn() then
|
||||
widget.text:setColor('green')
|
||||
else
|
||||
widget.text:setColor('red')
|
||||
end
|
||||
end
|
||||
widget.text:setText(options.text)
|
||||
end
|
||||
|
||||
widget.setOn = function(val)
|
||||
widget.status:setOn(val)
|
||||
if widget.status:isOn() then
|
||||
widget.text:setColor('green')
|
||||
else
|
||||
widget.text:setColor('red')
|
||||
end
|
||||
config.enabled = widget.status:isOn()
|
||||
end
|
||||
|
||||
widget.onClick = function(widget)
|
||||
if options.switchable ~= false then
|
||||
widget.setOn(not widget.status:isOn())
|
||||
if type(callback) == 'table' then
|
||||
callback.setOn(config.enabled)
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
callback(widget, widget.status:isOn())
|
||||
end
|
||||
|
||||
if options.hotkey then
|
||||
widget.hotkey:setText(options.hotkey)
|
||||
context.hotkey(options.hotkey, "", function()
|
||||
widget:onClick()
|
||||
end, nil, options.switchable ~= false)
|
||||
else
|
||||
widget.hotkey:hide()
|
||||
end
|
||||
|
||||
if options.movable ~= false then
|
||||
widget.onDragEnter = function(widget, mousePos)
|
||||
if not g_keyboard.isCtrlPressed() then
|
||||
return false
|
||||
end
|
||||
widget:breakAnchors()
|
||||
widget.movingReference = { x = mousePos.x - widget:getX(), y = mousePos.y - widget:getY() }
|
||||
return true
|
||||
end
|
||||
|
||||
widget.onDragMove = function(widget, mousePos, moved)
|
||||
local parentRect = widget:getParent():getRect()
|
||||
local x = math.min(math.max(parentRect.x, mousePos.x - widget.movingReference.x), parentRect.x + parentRect.width - widget:getWidth())
|
||||
local y = math.min(math.max(parentRect.y, mousePos.y - widget.movingReference.y), parentRect.y + parentRect.height - widget:getHeight())
|
||||
widget:move(x, y)
|
||||
return true
|
||||
end
|
||||
|
||||
widget.onDragLeave = function(widget, pos)
|
||||
local parent = widget:getParent()
|
||||
local parentRect = parent:getRect()
|
||||
local x = widget:getX() - parentRect.x
|
||||
local y = widget:getY() - parentRect.y
|
||||
local width = parentRect.width - widget:getWidth()
|
||||
local height = parentRect.height - widget:getHeight()
|
||||
|
||||
config.x = math.min(1, math.max(0, x / width))
|
||||
config.y = math.min(1, math.max(0, y / height))
|
||||
widget:addAnchor(AnchorHorizontalCenter, 'parent', AnchorHorizontalCenter)
|
||||
widget:addAnchor(AnchorVerticalCenter, 'parent', AnchorVerticalCenter)
|
||||
|
||||
widget:setMarginTop(height * (-0.5 + config.x))
|
||||
widget:setMarginLeft(width * (-0.5 + config.y))
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
widget.onGeometryChange = function(widget, oldRect, newRect)
|
||||
if widget:isDragging() then return end
|
||||
local parent = widget:getParent()
|
||||
local parentRect = parent:getRect()
|
||||
local width = parentRect.width - widget:getWidth()
|
||||
local height = parentRect.height - widget:getHeight()
|
||||
widget:setMarginTop(height * (-0.5 + config.y))
|
||||
widget:setMarginLeft(width * (-0.5 + config.x))
|
||||
end
|
||||
|
||||
if options.phantom ~= true then
|
||||
widget.onMouseRelease = function()
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
if options.switchable ~= false then
|
||||
if type(callback) == 'table' then
|
||||
callback.setOn(config.enabled)
|
||||
callback.icon = widget
|
||||
else
|
||||
callback(widget, widget.status:isOn())
|
||||
end
|
||||
end
|
||||
return widget
|
||||
end
|
@@ -34,33 +34,89 @@ context.macro = function(timeout, name, hotkey, callback, parent)
|
||||
hotkey = retranslateKeyComboDesc(hotkey)
|
||||
end
|
||||
|
||||
local switch = nil
|
||||
if name:len() > 0 then
|
||||
if context.storage._macros[name] == nil then
|
||||
context.storage._macros[name] = false
|
||||
table.insert(context._macros, {
|
||||
enabled = false,
|
||||
name = name,
|
||||
timeout = timeout,
|
||||
lastExecution = context.now,
|
||||
hotkey = hotkey,
|
||||
})
|
||||
local macro = context._macros[#context._macros]
|
||||
|
||||
macro.isOn = function()
|
||||
return macro.enabled
|
||||
end
|
||||
macro.isOff = function()
|
||||
return not macro.enabled
|
||||
end
|
||||
macro.toggle = function(widget)
|
||||
if macro.isOn() then
|
||||
macro.setOff()
|
||||
else
|
||||
macro.setOn()
|
||||
end
|
||||
switch = context._addMacroSwitch(name, hotkey, parent)
|
||||
end
|
||||
macro.setOn = function(val)
|
||||
if val == false then
|
||||
return macro.setOff()
|
||||
end
|
||||
macro.enabled = true
|
||||
context.storage._macros[name] = true
|
||||
if macro.switch then
|
||||
macro.switch:setOn(true)
|
||||
end
|
||||
if macro.icon then
|
||||
macro.icon.setOn(true)
|
||||
end
|
||||
end
|
||||
macro.setOff = function(val)
|
||||
if val == false then
|
||||
return macro.setOn()
|
||||
end
|
||||
macro.enabled = false
|
||||
context.storage._macros[name] = false
|
||||
if macro.switch then
|
||||
macro.switch:setOn(false)
|
||||
end
|
||||
if macro.icon then
|
||||
macro.icon.setOn(false)
|
||||
end
|
||||
end
|
||||
|
||||
if name:len() > 0 then
|
||||
-- creature switch
|
||||
local text = name
|
||||
if hotkey:len() > 0 then
|
||||
text = name .. " [" .. hotkey .. "]"
|
||||
end
|
||||
macro.switch = context.addSwitch("macro_" .. (#context._macros + 1), text, macro.toggle, parent)
|
||||
|
||||
-- load state
|
||||
if context.storage._macros[name] == true then
|
||||
macro.setOn()
|
||||
end
|
||||
end
|
||||
|
||||
local desc = "lua"
|
||||
local info = debug.getinfo(2, "Sl")
|
||||
if info then
|
||||
desc = info.short_src .. ":" .. info.currentline
|
||||
end
|
||||
|
||||
table.insert(context._macros, {
|
||||
timeout = timeout,
|
||||
name = name,
|
||||
lastExecution = context.now,
|
||||
hotkey = hotkey,
|
||||
switch = switch
|
||||
})
|
||||
|
||||
local macroData = context._macros[#context._macros]
|
||||
macroData.callback = function()
|
||||
if not macroData.delay or macroData.delay < context.now then
|
||||
context._currentExecution = macroData
|
||||
callback()
|
||||
macro.callback = function(macro)
|
||||
if not macro.delay or macro.delay < context.now then
|
||||
context._currentExecution = macro
|
||||
local start = g_clock.realMillis()
|
||||
callback(macro)
|
||||
local executionTime = g_clock.realMillis() - start
|
||||
if executionTime > 100 then
|
||||
context.warning("Slow macro (" .. executionTime .. "ms): " .. macro.name .. " - " .. desc)
|
||||
end
|
||||
context._currentExecution = nil
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
return macroData
|
||||
return macro
|
||||
end
|
||||
|
||||
-- hotkey(keys, callback)
|
||||
@@ -94,11 +150,22 @@ context.hotkey = function(keys, name, callback, parent, single)
|
||||
single = single
|
||||
}
|
||||
|
||||
local desc = "lua"
|
||||
local info = debug.getinfo(2, "Sl")
|
||||
if info then
|
||||
desc = info.short_src .. ":" .. info.currentline
|
||||
end
|
||||
|
||||
local hotkeyData = context._hotkeys[keys]
|
||||
hotkeyData.callback = function()
|
||||
if not hotkeyData.delay or hotkeyData.delay < context.now then
|
||||
context._currentExecution = hotkeyData
|
||||
local start = g_clock.realMillis()
|
||||
callback()
|
||||
local executionTime = g_clock.realMillis() - start
|
||||
if executionTime > 100 then
|
||||
context.warning("Slow hotkey (" .. executionTime .. "ms): " .. hotkeyData.name .. " - " .. desc)
|
||||
end
|
||||
context._currentExecution = nil
|
||||
return true
|
||||
end
|
||||
|
@@ -132,7 +132,6 @@ context.findPath = function(startPos, destPos, maxDist, params)
|
||||
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
|
||||
@@ -143,6 +142,7 @@ context.findPath = function(startPos, destPos, maxDist, params)
|
||||
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]
|
||||
print(node)
|
||||
if node and (not bestCandidate or bestCandidate[1] > node[1]) then
|
||||
bestCandidate = node
|
||||
bestCandidatePos = dest
|
||||
@@ -195,3 +195,7 @@ context.autoWalk = function(destination, maxDist, params)
|
||||
return true
|
||||
end
|
||||
|
||||
context.getTileUnderCursor = function()
|
||||
if not modules.game_interface.gameMapPanel.mousePos then return end
|
||||
return modules.game_interface.gameMapPanel:getTile(modules.game_interface.gameMapPanel.mousePos)
|
||||
end
|
@@ -3,6 +3,7 @@ local context = G.botContext
|
||||
context.BotServer = {}
|
||||
context.BotServer.url = "ws://bot.otclient.ovh:8000/"
|
||||
context.BotServer.timeout = 3
|
||||
context.BotServer.ping = 0
|
||||
context.BotServer._callbacks = {}
|
||||
context.BotServer._lastMessageId = 0
|
||||
context.BotServer._wasConnected = true -- show first warning
|
||||
@@ -24,6 +25,7 @@ context.BotServer.init = function(name, channel)
|
||||
end
|
||||
context.BotServer._wasConnected = true
|
||||
if message["type"] == "ping" then
|
||||
context.BotServer.ping = message["ping"]
|
||||
return context.BotServer._websocket.send({type="ping"})
|
||||
end
|
||||
if message["type"] == "message" then
|
||||
|
Reference in New Issue
Block a user