-
Notifications
You must be signed in to change notification settings - Fork 6
/
sandbox.lua
344 lines (300 loc) · 11.3 KB
/
sandbox.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
lua_sandbox = require("lib/lua-sandbox/sandbox")
if Sandbox == nil then
Sandbox = class({})
end
local function copy_method(obj, method)
return function(self, ...)
return obj[method](obj, ...)
end
end
function Sandbox:Init()
self.game_info = {}
self.public_api = self:SandboxPublicAPI()
self.default_hero = "npc_dota_hero_axe"
self.init = true
end
function Sandbox:SetupGameInfo(game_info)
self.game_info = game_info
end
function Sandbox:LoadScript(user_script, quota, env, candidate_name)
for k, v in pairs(self.public_api) do
env[k] = v
end
env["print"] = function (...)
return print("[Sandbox.".. candidate_name .."]", ...)
end
for k, v in pairs(self.game_info) do
env[k] = v
end
local options = {
quota = quota,
env = env,
}
local results = {pcall(lua_sandbox.protect, user_script, options)}
if not results[1] then
print("[Sandbox.".. candidate_name ..".script.load]", results[2])
return nil
end
return results[2]
end
function Sandbox:RunFunctionWrap(func, candidate_name, ...)
if not func then
return nil
end
local results = {pcall(func, ...)}
if not results[1] then
print("[Sandbox.".. candidate_name ..".script.run]", results[2])
return nil
end
return results[2]
end
function Sandbox:LoadChooseHeroScript(user_script, candidate_name)
return self:LoadScript(user_script, 100000, {}, candidate_name)
end
function Sandbox:LoadActionScript(user_script, candidate_name)
return self:LoadScript(user_script, 500000, {}, candidate_name)
end
function Sandbox:RunChooseHero(choose_func, candidate_name)
local hero_name = self:RunFunctionWrap(choose_func, candidate_name)
if type(hero_name) ~= "string" then
hero_name = self.default_hero
end
return hero_name
end
function Sandbox:RunAction(act_func, entity, ctx, candidate_name)
local sandboxed_entity = self:SandboxHero(entity, false)
local new_ctx = self:RunFunctionWrap(act_func, candidate_name, sandboxed_entity, ctx)
return new_ctx
end
function GetGameTime()
return GameRules:GetGameTime()
end
function Sandbox:SandboxPublicAPI()
local api = {
Vector = Vector,
QAngle = QAngle,
GetGameTime = GetGameTime,
}
return api
end
function Sandbox:SandboxHero(hero, readonly)
if hero == nil then
return nil
end
local sandboxed = self:SandboxBaseNPC(hero, readonly)
sandboxed.GetAgility = copy_method(hero, "GetAgility")
sandboxed.GetIntellect = copy_method(hero, "GetIntellect")
sandboxed.GetStrength = copy_method(hero, "GetStrength")
sandboxed.GetGold = copy_method(hero, "GetGold")
sandboxed.candidate_id = hero.candidate_id
-- TODO: level up
return sandboxed
end
function Sandbox:SandboxBaseNPC(npc, readonly)
if npc == nil then
return nil
end
local sandboxed = {
GetEntityIndex = copy_method(npc, "GetEntityIndex"),
GetAbsOrigin = copy_method(npc, "GetAbsOrigin"),
GetTeam = copy_method(npc, "GetTeam"),
GetAttackSpeed = copy_method(npc, "GetAttackSpeed"),
GetHealth = copy_method(npc, "GetHealth"),
GetHealthRegen = copy_method(npc, "GetHealthRegen"),
GetMaxHealth = copy_method(npc, "GetMaxHealth"),
GetLevel = copy_method(npc, "GetLevel"),
GetMana = copy_method(npc, "GetMana"),
GetMaxMana = copy_method(npc, "GetMaxMana"),
GetManaRegen = copy_method(npc, "GetManaRegen"),
GetUnitName = copy_method(npc, "GetUnitName"),
GetAttackRange = copy_method(npc, "Script_GetAttackRange"),
GetAbilityCount = copy_method(npc, "GetAbilityCount"),
IsAttacking = copy_method(npc, "IsAttacking"),
IsConsideredHero = copy_method(npc, "IsConsideredHero"),
}
local order_whitelist = {
[DOTA_UNIT_ORDER_NONE] = true,
[DOTA_UNIT_ORDER_MOVE_TO_POSITION] = true,
[DOTA_UNIT_ORDER_MOVE_TO_TARGET] = true,
[DOTA_UNIT_ORDER_ATTACK_MOVE] = true,
[DOTA_UNIT_ORDER_ATTACK_TARGET] = true,
[DOTA_UNIT_ORDER_CAST_POSITION] = true,
[DOTA_UNIT_ORDER_CAST_TARGET] = true,
[DOTA_UNIT_ORDER_CAST_TARGET_TREE] = true,
[DOTA_UNIT_ORDER_CAST_NO_TARGET] = true,
[DOTA_UNIT_ORDER_CAST_TOGGLE] = true,
[DOTA_UNIT_ORDER_HOLD_POSITION] = true,
[DOTA_UNIT_ORDER_TRAIN_ABILITY] = true,
[DOTA_UNIT_ORDER_DROP_ITEM] = true,
[DOTA_UNIT_ORDER_GIVE_ITEM] = true,
[DOTA_UNIT_ORDER_PICKUP_ITEM] = true,
[DOTA_UNIT_ORDER_PURCHASE_ITEM] = true,
[DOTA_UNIT_ORDER_SELL_ITEM] = true,
[DOTA_UNIT_ORDER_MOVE_ITEM] = true,
[DOTA_UNIT_ORDER_CAST_TOGGLE_AUTO] = true,
[DOTA_UNIT_ORDER_STOP] = true,
[DOTA_UNIT_ORDER_MOVE_TO_DIRECTION] = true,
[DOTA_UNIT_ORDER_PATROL] = true,
[DOTA_UNIT_ORDER_CONTINUE] = true,
[DOTA_UNIT_ORDER_MOVE_RELATIVE] = true,
}
function sandboxed:FindUnitsInRadius(
location,
radius,
team_filter,
type_filter,
flag_filter,
find_order
)
local units = FindUnitsInRadius(
npc:GetTeam(),
location,
nil, -- cacheUnit
radius,
team_filter,
type_filter,
flag_filter,
find_order,
false -- canGrowCache
)
local sandboxed_units = {}
for _, unit in ipairs(units) do
local sandboxed_unit = Sandbox:SandboxUnit(unit, true)
table.insert(sandboxed_units, sandboxed_unit)
end
return sandboxed_units
end
function sandboxed:GetAbilityByIndex(index)
local ability = npc:GetAbilityByIndex(index)
return Sandbox:SandboxAbility(ability)
end
function sandboxed:GetItemInSlot(slot)
local item = npc:GetItemInSlot(slot)
return Sandbox:SandboxItem(item)
end
if readonly then
return sandboxed
end
function sandboxed:ExecuteOrder(
order_type,
target_index,
ability_index,
position,
queue
)
if not order_whitelist[order_type] then
return
end
ExecuteOrderFromTable{
UnitIndex = npc:GetEntityIndex(),
OrderType = order_type,
TargetIndex = target_index,
AbilityIndex = ability_index,
Position = position,
Queue = queue
}
end
function sandboxed:PurchaseItem(item_name)
local item = CreateItem(item_name, npc, npc)
if item == nil then
return false
end
if not item:IsPurchasable() or not npc:IsAlive() then
item:RemoveSelf()
return false
end
local gold_own = npc:GetGold()
local gold_cost = item:GetCost()
local gold_left = gold_own - gold_cost
if gold_left < 0 then
item:RemoveSelf()
return false
end
-- we only use unreliable gold
npc:SetGold(gold_left, false)
npc:AddItem(item)
return true
end
return sandboxed
end
function Sandbox:SandboxUnit(unit, readonly)
if unit == nil then
return nil
elseif unit:IsConsideredHero() then
return self:SandboxHero(unit, readonly)
else -- TODO: support more NPC types
return nil
end
end
function Sandbox:SandboxAbility(ability)
if ability == nil then
return nil
end
local sandboxed = {
GetAbilityName = copy_method(ability, "GetAbilityName"),
GetAOERadius = copy_method(ability, "GetAOERadius"),
GetBehavior = copy_method(ability, "GetBehavior"),
GetChannelledManaCostPerSecond
= copy_method(ability, "GetChannelledManaCostPerSecond"),
GetChannelTime = copy_method(ability, "GetChannelTime"),
GetCooldownTimeRemaining = copy_method(ability, "GetCooldownTimeRemaining"),
GetCurrentAbilityCharges = copy_method(ability, "GetCurrentAbilityCharges"),
GetEffectiveCastRange = copy_method(ability, "GetEffectiveCastRange"), -- TODO: fix args
GetEffectiveCooldown = copy_method(ability, "GetEffectiveCooldown"),
GetLevel = copy_method(ability, "GetLevel"),
GetLevelSpecialValueNoOverride
= copy_method(ability, "GetLevelSpecialValueNoOverride"),
GetManaCost = copy_method(ability, "GetManaCost"),
GetSpecialValueFor = copy_method(ability, "GetSpecialValueFor"),
GetToggleState = copy_method(ability, "GetToggleState"),
GetDuration = copy_method(ability, "GetDuration"),
IsItem = copy_method(ability, "IsItem"),
GetEntityIndex = copy_method(ability, "GetEntityIndex"),
}
return sandboxed
end
function Sandbox:SandboxItem(item)
if item == nil then
return nil
end
local sandboxed = {
CanBeUsedOutOfInventory = copy_method(item, "CanBeUsedOutOfInventory"),
CanOnlyPlayerHeroPickup = copy_method(item, "CanOnlyPlayerHeroPickup"),
GetCost = copy_method(item, "GetCost"),
GetCurrentCharges = copy_method(item, "GetCurrentCharges"),
GetInitialCharges = copy_method(item, "GetInitialCharges"),
GetItemSlot = copy_method(item, "GetItemSlot"),
GetItemState = copy_method(item, "GetItemState"),
GetPurchaseTime = copy_method(item, "GetPurchaseTime"),
GetSecondaryCharges = copy_method(item, "GetSecondaryCharges"),
GetValuelessCharges = copy_method(item, "GetValuelessCharges"),
IsAlertableItem = copy_method(item, "IsAlertableItem"),
IsCastOnPickup = copy_method(item, "IsCastOnPickup"),
IsCombinable = copy_method(item, "IsCombinable"),
IsCombineLocked = copy_method(item, "IsCombineLocked"),
IsDisassemblable = copy_method(item, "IsDisassemblable"),
IsDroppable = copy_method(item, "IsDroppable"),
IsInBackpack = copy_method(item, "IsInBackpack"),
IsItem = copy_method(item, "IsItem"),
IsKillable = copy_method(item, "IsKillable"),
IsMuted = copy_method(item, "IsMuted"),
IsNeutralDrop = copy_method(item, "IsNeutralDrop"),
IsPermanent = copy_method(item, "IsPermanent"),
IsPurchasable = copy_method(item, "IsPurchasable"),
IsRecipe = copy_method(item, "IsRecipe"),
IsRecipeGenerated = copy_method(item, "IsRecipeGenerated"),
IsSellable = copy_method(item, "IsSellable"),
IsStackable = copy_method(item, "IsStackable"),
GetName = copy_method(item, "GetAbilityName"), -- CDOTA_Item extends CDOTABaseAbility
GetEntityIndex = copy_method(item, "GetEntityIndex"),
}
return sandboxed
end
function Sandbox:CleanUpItems()
local total = GameRules:NumDroppedItems()
for _ = 1, total do
GameRules:GetDroppedItem(0):RemoveSelf()
end
end
if not Sandbox.init then Sandbox:Init() end
GameRules.Sandbox = Sandbox