Version 2.3 - cooldowns in action bar, more advanced bot, new cavebot, bug fixes

This commit is contained in:
OTCv8
2020-04-15 01:22:06 +02:00
parent ed8162a9d5
commit 9a4ab2ae3b
68 changed files with 3261 additions and 428 deletions

View File

@@ -29,6 +29,20 @@ context.callback = function(callbackType, callback)
context._currentExecution = prevExecution
end
end)
local cb = context._callbacks[callbackType]
return {
remove = function()
local index = nil
for i, cb2 in ipairs(context._callbacks[callbackType]) do
if cb == cb2 then
index = i
end
end
if index then
table.remove(context._callbacks[callbackType], index)
end
end
}
end
-- onKeyDown(callback) -- callback = function(keys)
@@ -158,7 +172,7 @@ end
context.listen = function(name, callback)
if not name then return context.error("listen: invalid name") end
name = name:lower()
context.onTalk(function(name2, level, mode, text, channelId, pos)
return context.onTalk(function(name2, level, mode, text, channelId, pos)
if name == name2:lower() then
callback(text, channelId, pos)
end
@@ -167,7 +181,7 @@ end
-- onPlayerPositionChange(callback) -- callback = function(newPos, oldPos)
context.onPlayerPositionChange = function(callback)
context.onCreaturePositionChange(function(creature, newPos, oldPos)
return context.onCreaturePositionChange(function(creature, newPos, oldPos)
if creature == context.player then
callback(newPos, oldPos)
end
@@ -176,7 +190,7 @@ end
-- onPlayerHealthChange(callback) -- callback = function(healthPercent)
context.onPlayerHealthChange = function(callback)
context.onCreatureHealthPercentChange(function(creature, healthPercent)
return context.onCreatureHealthPercentChange(function(creature, healthPercent)
if creature == context.player then
callback(healthPercent)
end

View File

@@ -33,9 +33,10 @@ Config.list = function(dir)
return correctList
end
-- load config from string insteaf of dile
-- load config from string insteaf of file
Config.parse = function(data)
local status, result = pcall(function()
if data:len() < 2 then return {} end
return json.decode(data)
end)
if status and type(result) == 'table' then
@@ -51,13 +52,29 @@ Config.parse = function(data)
end
Config.load = function(dir, name)
local file = context.configDir .. "/" .. dir .. "/" .. name .. ".json"
local file = context.configDir .. "/" .. dir .. "/" .. name .. ".json"
if g_resources.fileExists(file) then -- load json
return json.decode(g_resources.readFileContents(file))
local status, result = pcall(function()
local data = g_resources.readFileContents(file)
if data:len() < 2 then return {} end
return json.decode(data)
end)
if not status then
context.error("Invalid json config (" .. name .. "): " .. result)
return {}
end
return result
end
file = context.configDir .. "/" .. dir .. "/" .. name .. ".cfg"
if g_resources.fileExists(file) then -- load cfg
return table.decodeStringPairList(g_resources.readFileContents(file))
local status, result = pcall(function()
return table.decodeStringPairList(g_resources.readFileContents(file))
end)
if not status then
context.error("Invalid cfg config (" .. name .. "): " .. result)
return {}
end
return result
end
return context.error("Config " .. file .. " doesn't exist")
end
@@ -87,7 +104,7 @@ Config.save = function(dir, name, value, forcedExtension)
if (table.isStringPairList(value) and forcedExtension ~= "json") or forcedExtension == "cfg" then -- cfg
g_resources.writeFileContents(file .. ".cfg", table.encodeStringPairList(value))
else
g_resources.writeFileContents(file .. ".json", json.encode(value))
g_resources.writeFileContents(file .. ".json", json.encode(value, 2))
end
return true
end
@@ -175,7 +192,11 @@ Config.setup = function(dir, widget, configExtension, callback)
if g_resources.fileExists(file) then
return context.error("Config " .. name .. " already exist")
end
g_resources.writeFileContents(file, "")
if configExtension == "json" then
g_resources.writeFileContents(file, json.encode({}))
else
g_resources.writeFileContents(file, "")
end
context.storage._configs[dir].selected = name
widget.switch:setOn(false)
refresh()

View File

@@ -1,6 +1,6 @@
local context = G.botContext
context.encode = function(data) return json.encode(data) end
context.encode = function(data, indent) return json.encode(data, indent or 2) end
context.decode = function(text) local status, result = pcall(function() return json.decode(text) end) if status then return result end return {} end
context.displayGeneralBox = function(title, message, buttons, onEnterCallback, onEscapeCallback)

View File

@@ -8,5 +8,17 @@ UI.createWidget = function(name, parent)
if parent == nil then
parent = context.panel
end
return g_ui.createWidget(name, parent)
local widget = g_ui.createWidget(name, parent)
widget.botWidget = true
return widget
end
UI.createWindow = function(name)
local widget = g_ui.createWidget(name, g_ui.getRootWidget())
widget.botWidget = true
widget:show()
widget:raise()
widget:focus()
return widget
end

View File

@@ -4,6 +4,14 @@ if type(context.UI) ~= "table" then
end
local UI = context.UI
UI.Button = function(text, callback, parent)
local widget = UI.createWidget("BotButton", parent)
widget:setText(text)
widget.onClick = callback
return widget
end
UI.Config = function(parent)
return UI.createWidget("BotConfig", parent)
end
@@ -11,8 +19,11 @@ end
-- call :setItems(table) to set items, call :getItems() to get them
-- unique if true, won't allow duplicates
-- callback (can be nil) gets table with new item list, eg: {{id=2160, count=1}, {id=268, count=100}, {id=269, count=20}}
UI.Container = function(callback, unique, parent)
local widget = UI.createWidget("BotContainer", parent)
UI.Container = function(callback, unique, parent, widget)
if not widget then
widget = UI.createWidget("BotContainer", parent)
end
local oldItems = {}
local updateItems = function()
@@ -33,7 +44,7 @@ UI.Container = function(callback, unique, parent)
if somethingNew then
oldItems = items
callback(items)
callback(widget, items)
end
widget:setItems(items)
@@ -77,6 +88,183 @@ UI.Container = function(callback, unique, parent)
return items
end
widget:setItems({})
return widget
end
UI.DualScrollPanel = function(params, callback, parent) -- callback = function(widget, newParams)
--[[ params:
on - bool,
text - string,
title - string,
min - number,
max - number,
]]
params.title = params.title or "title"
params.text = params.text or ""
params.min = params.min or 20
params.max = params.max or 80
local widget = UI.createWidget('DualScrollPanel', parent)
widget.title:setOn(params.on)
widget.title.onClick = function()
params.on = not params.on
widget.title:setOn(params.on)
if callback then
callback(widget, params)
end
end
widget.text:setText(params.text or "")
widget.text.onTextChange = function(widget, text)
params.text = text
if callback then
callback(widget, params)
end
end
local update = function(dontSignal)
widget.title:setText("" .. params.min .. "% <= " .. params.title .. " <= " .. params.max .. "%")
if callback and not dontSignal then
callback(widget, params)
end
end
widget.scroll1:setValue(params.min)
widget.scroll2:setValue(params.max)
widget.scroll1.onValueChange = function(scroll, value)
params.min = value
update()
end
widget.scroll2.onValueChange = function(scroll, value)
params.max = value
update()
end
update(true)
end
UI.DualScrollItemPanel = function(params, callback, parent) -- callback = function(widget, newParams)
--[[ params:
on - bool,
item - number,
title - string,
min - number,
max - number,
]]
params.title = params.title or "title"
params.item = params.item or 0
params.min = params.min or 20
params.max = params.max or 80
local widget = UI.createWidget('DualScrollItemPanel', parent)
widget.title:setOn(params.on)
widget.title.onClick = function()
params.on = not params.on
widget.title:setOn(params.on)
if callback then
callback(widget, params)
end
end
widget.item:setItemId(params.item)
widget.item.onItemChange = function()
params.item = widget.item:getItemId()
if callback then
callback(widget, params)
end
end
local update = function(dontSignal)
widget.title:setText("" .. params.min .. "% <= " .. params.title .. " <= " .. params.max .. "%")
if callback and not dontSignal then
callback(widget, params)
end
end
widget.scroll1:setValue(params.min)
widget.scroll2:setValue(params.max)
widget.scroll1.onValueChange = function(scroll, value)
params.min = value
update()
end
widget.scroll2.onValueChange = function(scroll, value)
params.max = value
update()
end
update(true)
end
UI.Label = function(text, parent)
local label = UI.createWidget('BotLabel', parent)
label:setText(text)
return label
end
UI.Separator = function(parent)
local separator = UI.createWidget('BotSeparator', parent)
return separator
end
UI.TextEdit = function(text, callback, parent)
local widget = UI.createWidget('BotTextEdit', parent)
widget.onTextChange = callback
widget:setText(text)
return widget
end
UI.TwoItemsAndSlotPanel = function(params, callback, parent)
--[[ params:
on - bool,
title - string,
item1 - number,
item2 - number,
slot - number,
]]
params.title = params.title or "title"
params.item1 = params.item1 or 0
params.item2 = params.item2 or 0
params.slot = params.slot or 1
local widget = UI.createWidget("TwoItemsAndSlotPanel", parent)
widget.title:setText(params.title)
widget.title:setOn(params.on)
widget.title.onClick = function()
params.on = not params.on
widget.title:setOn(params.on)
if callback then
callback(widget, params)
end
end
widget.slot:setCurrentIndex(params.slot)
widget.slot.onOptionChange = function()
params.slot = widget.slot.currentIndex
if callback then
callback(widget, params)
end
end
widget.item1:setItemId(params.item1)
widget.item1.onItemChange = function()
params.item1 = widget.item1:getItemId()
if callback then
callback(widget, params)
end
end
widget.item2:setItemId(params.item2)
widget.item2.onItemChange = function()
params.item2 = widget.item2:getItemId()
if callback then
callback(widget, params)
end
end
return widget
end

View File

@@ -1,5 +1,8 @@
local context = G.botContext
-- DO NOT USE THIS CODE.
-- IT'S ONLY HERE FOR BACKWARD COMPATIBILITY, MAY BE REMOVED IN THE FUTURE
context.createWidget = function(name, parent)
if parent == nil then
parent = context.panel
@@ -32,11 +35,11 @@ context.addTab = function(name)
return tab.tabPanel.content
end
context.tabs:setOn(true)
local smallTabs = #(context.tabs.tabs) >= 5
local newTab = context.tabs:addTab(name, g_ui.createWidget('BotPanel')).tabPanel.content
if #(context.tabs.tabs) > 5 then
context.tabs:setOn(true)
if smallTabs then
for k,tab in pairs(context.tabs.tabs) do
tab:setPadding(3)
tab:setFont('small-9px')
end
end