forked from BenDol/otclient-candybot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcandybot.lua
More file actions
227 lines (177 loc) · 4.6 KB
/
Copy pathcandybot.lua
File metadata and controls
227 lines (177 loc) · 4.6 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
--[[
@Authors: Ben Dol (BeniS)
@Details: Otclient module entry point. This handles
main bot controls and functionality.
]]
CandyBot = extends(UIWidget)
CandyBot.window = nil
CandyBot.options = {}
CandyBot.defaultOptions = {}
dofile('consts.lua')
dofile('helper.lua')
dofile('logger.lua')
dofile('classes/target.lua')
dofile('classes/attack.lua')
local botButton
local botTabBar
local enabled
local function setupDefaultOptions()
for _, module in pairs(Modules.getOptions()) do
for k, option in pairs(module) do
CandyBot.defaultOptions[k] = option
end
end
end
local function loadModules()
dofile('modules.lua')
Modules.init()
-- setup the default options
setupDefaultOptions()
end
local function loadExtensions()
dofiles('extensions')
end
function CandyBot.init()
CandyBot.window = g_ui.displayUI('candybot.otui')
CandyBot.window:setVisible(false)
botButton = modules.client_topmenu.addRightGameToggleButton(
'botButton', 'Bot (Ctrl+Shift+B)', 'candybot', CandyBot.toggle)
botButton:setOn(false)
botTabBar = CandyBot.window:getChildById('botTabBar')
botTabBar:setContentWidget(CandyBot.window:getChildById('botContent'))
botTabBar:setTabSpacing(-1)
-- bind keys
g_keyboard.bindKeyDown('Ctrl+Shift+B', CandyBot.toggle)
g_keyboard.bindKeyPress('Tab', function() botTabBar:selectNextTab() end, CandyBot.window)
g_keyboard.bindKeyPress('Shift+Tab', function() botTabBar:selectPrevTab() end, CandyBot.window)
-- load extensions
loadExtensions()
-- load modules
loadModules()
-- init bot logger
BotLogger.init()
-- hook functions
connect(g_game, {
onGameStart = CandyBot.online,
onGameEnd = CandyBot.offline
})
-- get bot settings
CandyBot.options = g_settings.getNode('Bot') or {}
if g_game.isOnline() then
CandyBot.online()
end
end
function CandyBot.terminate()
CandyBot.hide()
disconnect(g_game, {
onGameStart = CandyBot.online,
onGameEnd = CandyBot.offline
})
if g_game.isOnline() then
CandyBot.offline()
end
Modules.terminate()
if botButton then
botButton:destroy()
botButton = nil
end
g_settings.setNode('Bot', CandyBot.options)
CandyBot.window:destroy()
end
function CandyBot.online()
addEvent(CandyBot.loadOptions)
end
function CandyBot.offline()
Modules.stop()
CandyBot.hide()
g_keyboard.unbindKeyDown('Ctrl+Shift+B')
end
function CandyBot.toggle()
if CandyBot.window:isVisible() then
CandyBot.hide()
else
CandyBot.show()
CandyBot.window:focus()
end
end
function CandyBot.show()
if g_game.isOnline() then
CandyBot.window:show()
botButton:setOn(true)
end
end
function CandyBot.hide()
CandyBot.window:hide()
botButton:setOn(false)
end
function CandyBot.enable(state)
enabled = state
if not state then Modules.stop() end
end
function CandyBot.isEnabled()
return enabled
end
function CandyBot.getIcon()
return botIcon
end
function CandyBot.getUI()
return CandyBot.window
end
function CandyBot.getParent()
return CandyBot.window:getParent() -- main window
end
function CandyBot.loadOptions()
local char = g_game.getCharacterName()
if CandyBot.options[char] ~= nil then
for i, v in pairs(CandyBot.options[char]) do
addEvent(function() CandyBot.changeOption(i, v, true) end)
end
else
for i, v in pairs(CandyBot.defaultOptions) do
addEvent(function() CandyBot.changeOption(i, v, true) end)
end
end
end
function CandyBot.changeOption(key, state, loading)
local loading = loading or false
if state == nil then
return
end
if CandyBot.defaultOptions[key] == nil then
CandyBot.options[key] = nil
return
end
if g_game.isOnline() then
Modules.notifyChange(key, state)
local panel = CandyBot.window
if loading then
for k, p in pairs(Modules.getPanels()) do
if p:getChildById(key) ~= nil then
panel = p
end
end
local widget = panel:getChildById(key)
if not widget then
return
end
local style = widget:getStyle().__class
if style == 'UITextEdit' then
widget:setText(state)
elseif style == 'UIComboBox' then
widget:setCurrentOption(state)
elseif style == 'UICheckBox' then
widget:setChecked(state)
elseif style == 'UIItem' then
widget:setItemId(state)
elseif style == 'UIScrollBar' then
local value = tonumber(state)
if value then widget:setValue(value) end
end
end
local char = g_game.getCharacterName()
if CandyBot.options[char] == nil then
CandyBot.options[char] = {}
end
CandyBot.options[char][key] = state
end
end