-
Notifications
You must be signed in to change notification settings - Fork 5
/
decision.lua
328 lines (259 loc) · 9.91 KB
/
decision.lua
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
-------------------------------------------------------------------------------
--- AUTHOR: Nostrademous
--- GITHUB REPO: https://github.com/Nostrademous/Dota2-WebAI
-------------------------------------------------------------------------------
--- LOAD OUR GLOBAL CONSTANTS
require( GetScriptDirectory().."/constants/tables" )
require( GetScriptDirectory().."/constants/generic" )
require( GetScriptDirectory().."/constants/roles" )
require( GetScriptDirectory().."/constants/runes" )
require( GetScriptDirectory().."/constants/shops" )
require( GetScriptDirectory().."/constants/fountains" )
require( GetScriptDirectory().."/constants/jungle" )
local item_map = require( GetScriptDirectory().."/constants/item_mappings" )
--- LOAD OUR HELPERS
require( GetScriptDirectory().."/helper/global_helper" )
local dp = require( GetScriptDirectory().."/data_packet" )
local InvHelp = require( GetScriptDirectory().."/helper/inventory_helper" )
--- LOAD OUR DEBUG SYSTEM
dbg = require( GetScriptDirectory().."/debug" )
local think = require( GetScriptDirectory().."/think" )
local server = require( GetScriptDirectory().."/webserver_out" )
noneMode = dofile( GetScriptDirectory().."/modes/none" )
-------------------------------------------------------------------------------
-- BASE CLASS - DO NOT MODIFY THIS SECTION
-------------------------------------------------------------------------------
local X = { init = false, currentMode = noneMode, currentModeValue = BOT_MODE_DESIRE_NONE, prevMode = noneMode }
function X:new(myBotInfo)
local mybot = {}
setmetatable(mybot, self)
self.__index = self
mybot.botInfo = myBotInfo
-- TODO - other stats we want to track?
GetBot().mybot = mybot -- make mybot accessible anywhere after calling GetBot()
GetBot().botInfo = mybot.botInfo -- make botInfo accessible
return mybot
end
function X:getCurrentMode()
return self.currentMode
end
function X:getCurrentModeValue()
return self.currentModeValue
end
function X:getPrevMode()
return self.prevMode
end
function X:ExecuteMode()
if self.currentMode ~= self.prevMode then
if self.currentMode:GetName() == nil then
dbg.pause("Unimplemented Mode: ", self.currentMode)
end
dbg.myPrint("Mode Transition: "..self.prevMode:GetName():upper().." --> "..self.currentMode:GetName():upper())
-- call OnEnd() of previous mode
self.prevMode:OnEnd()
-- call OnStart() of new mode
self.currentMode:OnStart()
self.prevMode = self.currentMode
end
-- do Mode
self.currentMode:Think()
end
function X:BeginMode(mode, value)
if mode == nil then
self.currentMode = noneMode
self.currentModeValue = BOT_MODE_DESIRE_NONE
return
end
if mode == self.currentMode then return end
self.currentMode = mode
self.currentModeValue = value
end
function X:ClearMode()
self.currentMode = noneMode
self.currentModeValue = BOT_MODE_DESIRE_NONE
self:ExecuteMode()
end
-------------------------------------------------------------------------------
-- BASE INITIALIZATION & RESET FUNCTIONS - DO NOT OVER-LOAD
-------------------------------------------------------------------------------
function X:DoInit(hBot)
if not globalInit then
InitializeGlobalVars()
end
self.Init = true
hBot.mybot = self
self.sNextAbility = {}
self.sNextItem = {}
self.lastModeThink = -1000.0
self.moving_location = nil
self.ability_location = nil
self.ability_completed = -1000.0
self.attack_target = nil
self.attack_target_id = -1
self.attack_completed = -1000.0
local fullName = hBot:GetUnitName()
self.Name = string.sub(fullName, 15, string.len(fullName))
end
function X:ResetTempVars()
self.moving_location = nil
if self.ability_location ~= nil then
if GameTime() >= self.ability_completed then
self.ability_location = nil
self.ability_completed = -1000.0
end
end
if self.attack_target ~= nil then
if GameTime() >= self.attack_completed then
self.attack_completed = -1000.0
end
end
end
-------------------------------------------------------------------------------
-- BASE THINK - DO NOT OVER-LOAD
-------------------------------------------------------------------------------
local lastReply = nil
local function ServerUpdate()
local hBot = GetBot()
server.SendData(hBot)
local worldReply = server.GetLastReply(dp.TYPE_WORLD)
if worldReply ~= nil then
dbg.myPrint("Need to Process new World Reply")
dbg.myPrint("Packet RTT: ", RealTime() - worldReply.Time)
end
local enemiesReply = server.GetLastReply(dp.TYPE_ENEMIES)
if enemiesReply ~= nil then
dbg.myPrint("Need to Process new Enemies Reply")
dbg.myPrint("Packet RTT: ", RealTime() - enemiesReply.Time)
end
local botReply = server.GetLastReply(dp.TYPE_PLAYER..tostring(hBot:GetPlayerID()))
if botReply == nil then
return nil, BOT_MODE_DESIRE_NONE
else
dbg.myPrint("Need to Process new Player Reply")
dbg.myPrint("Packet RTT: ", RealTime() - botReply.Time)
if botReply.Data then
if botReply.Data.StartItems then
if #hBot.mybot.sNextItem == 0 then
hBot.mybot.sNextItem = botReply.Data.StartItems
end
end
if botReply.Data.LevelAbs then
if #hBot.mybot.sNextAbility == 0 then
hBot.mybot.sNextAbility = botReply.Data.LevelAbs
end
end
end
end
return nil, BOT_MODE_DESIRE_NONE
end
function X:Think(hBot)
-- if we are a human player, don't bother
if not hBot:IsBot() then return end
if not self.Init then
self:DoInit(hBot)
return
end
if GetGameState() ~= GAME_STATE_GAME_IN_PROGRESS and GetGameState() ~= GAME_STATE_PRE_GAME then return end
-- draw debug info to Game UI
dbg.draw()
-- do out Thinking and set our Mode
if GameTime() - self.lastModeThink >= 0.1 then
-- reset any frame-temporary variables
self:ResetTempVars()
-- try to get directives from the web-server
local highestDesiredMode, highestDesiredValue = ServerUpdate()
-- if we got "nil" then do local thinking
if highestDesiredMode == nil then
highestDesiredMode, highestDesiredValue = think.MainThink()
end
-- execute any atomic operations
self:ExecuteAtomicOperations(hBot)
-- execute our current directives
self:BeginMode(highestDesiredMode, highestDesiredValue)
self:ExecuteMode()
self.lastModeThink = GameTime()
end
end
-------------------------------------------------------------------------------
-- ATOMIC OPERATIONS
-------------------------------------------------------------------------------
function X:ExecuteAtomicOperations(hBot)
self:Atomic_LearnAbilities(hBot)
self:Atomic_BuyItems(hBot)
self:Atomic_SwapItems(hBot)
end
function X:Atomic_LearnAbilities(hBot)
if #hBot.mybot.sNextAbility == 0 then return end
local nAbilityPoints = hBot:GetAbilityPoints()
if nAbilityPoints > 0 then
for i = 1, #hBot.mybot.sNextAbility do
local sNextAb = hBot.mybot.sNextAbility[1]
local hAbility = hBot:GetAbilityByName(sNextAb)
if hAbility and hAbility:CanAbilityBeUpgraded() then
hBot:ActionImmediate_LevelAbility(sNextAb)
table.remove(hBot.mybot.sNextAbility, 1)
break
else
dbg.pause("Trying to level an ability I cannot", hBot.mybot.sNextAbility)
end
end
end
end
function X:Atomic_BuyItems(hBot)
if #hBot.mybot.sNextItem == 0 then return end
local sComps = {}
for i = 1, #hBot.mybot.sNextItem do
TableConcat(sComps, InvHelp:GetComponents(item_map[hBot.mybot.sNextItem[i]]))
end
hBot.mybot.sNextItem = sComps
for k,v in pairs(sComps) do
print(k,v)
end
while #hBot.mybot.sNextItem > 0 do
local sNextItem = hBot.mybot.sNextItem[1]
if hBot:GetGold() >= GetItemCost(sNextItem) then
local secret = IsItemPurchasedFromSecretShop(sNextItem)
local side = IsItemPurchasedFromSideShop(sNextItem)
local fountain = (not secret)
local shops = {}
-- Determine valid shops that sell the item
if (secret and side) then
shops = {SHOP_SECRET_RADIANT, SHOP_SECRET_DIRE, SHOP_SIDE_BOT, SHOP_SIDE_TOP}
elseif (secret) then
shops = {SHOP_SECRET_RADIANT, SHOP_SECRET_DIRE}
elseif (side and fountain) then
shops = {SHOP_SIDE_BOT, SHOP_SIDE_TOP, SHOP_RADIANT, SHOP_DIRE}
elseif (fountain) then
shops = {SHOP_RADIANT, SHOP_DIRE}
end
for i = 1, #shops do
local shop = shops[i]
if ShopDistance(hBot, shop) <= SHOP_USE_DISTANCE then
local buyRet = InvHelp:BuyItem(hBot, sNextItem)
if buyRet == 0 then
table.remove(hBot.mybot.sNextItem, 1)
end
break
end
end
else
break
end
end
end
function X:Atomic_SwapItems(hBot)
local index1 = nil
local index2 = nil
if not InvHelp:IsBackpackEmpty(hBot) then
local i1 = InvHelp:MostValuableItemSlot(hBot, 6, 8)
local i2 = InvHelp:LeastValuableItemSlot(hBot, 0, 5)
if i1.value > i2.value then
index1 = i1.slot
index2 = i2.slot
end
end
if index1 and index2 then
hBot:ActionImmediate_SwapItems(index1, index2)
end
end
return X