forked from Kreolix/CovenantMissionHelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.lua
535 lines (467 loc) · 20 KB
/
Board.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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
local CovenantMissionHelper, CMH = ...
local L = MissionHelper.L
local SIMULATE_ITERATIONS = 100
local MAX_ROUNDS = 100
local MAX_RANDOM_ROUNDS = 50
local LVL_UP_ICON = "|TInterface\\petbattles\\battlebar-abilitybadge-strong-small:0|t"
local SKULL_ICON = "|TInterface\\TargetingFrame\\UI-RaidTargetingIcon_8:0|t"
---@class Board
local Board = {Errors = {}, CombatLog = {}, HiddenCombatLog = {}, CombatLogEvents = {}}
local TargetTypeEnum, EffectTypeEnum = CMH.DataTables.TargetTypeEnum, CMH.DataTables.EffectTypeEnum
local function arrayForPrint(array)
if not array then
for _, text in ipairs(CMH.Board.CombatLog) do print(text) end
return 'EMPTY ARRAY'
end
local result = ''
for _, e in pairs(array) do
result = result .. tostring(e) .. ', '
end
return result
end
local function copy(obj, seen)
if type(obj) ~= 'table' then return obj end
if seen and seen[obj] then return seen[obj] end
local s = seen or {}
local res = setmetatable({}, getmetatable(obj))
s[obj] = res
for k, v in pairs(obj) do res[copy(k, s)] = copy(v, s) end
return res
end
local function isAura(effectType)
return effectType >= EffectTypeEnum.DoT and effectType <= EffectTypeEnum.AdditionalTakenDamage
end
function Board:new(missionPage, isCalcRandom)
local newObj = {
units = {},
hasRandomSpells = false,
probability = 100,
isMissionOver = false,
isEmpty = true,
initialAlliesHP = 0,
initialEnemiesHP = 0,
isCalcRandom = isCalcRandom,
max_rounds = MAX_ROUNDS,
baseXP = 0,
winXP = 0,
--missionPage = missionPage,
}
local isCompletedMission = (missionPage.missionInfo == nil)
local missionInfo = isCompletedMission and _G["CovenantMissionFrame"].MissionComplete.currentMission or missionPage.missionInfo
newObj.missionID = missionInfo.missionID
newObj.baseXP = missionInfo.xp
newObj.winXP = newObj.baseXP
for _, reward in pairs (missionInfo.rewards) do
if reward.followerXP then
newObj.winXP = newObj.winXP + reward.followerXP
end
end
-- set enemy's units
local enemies = C_Garrison.GetMissionCompleteEncounters(newObj.missionID)
for i = 1, #enemies do
local enemyUnit = CMH.Unit:new(enemies[i])
--SELECTED_CHAT_FRAME:AddMessage("enemyUnitName = " .. enemyUnit.name)
newObj.units[enemyUnit.boardIndex] = enemyUnit
newObj.initialEnemiesHP = newObj.initialEnemiesHP + enemyUnit.currentHealth
end
--set my team
-- If completed mission have < 5 followers, "empty" frames isn't empty actually.
-- It saved from last completed mission.
local framesByBoardIndex, boardIndexes = {}, {}
if isCompletedMission then
-- completed mission
for _, follower in pairs(_G["CovenantMissionFrame"].MissionComplete.followerGUIDToInfo) do
table.insert(boardIndexes, follower.boardIndex)
end
framesByBoardIndex = _G["CovenantMissionFrame"].MissionComplete.Board.framesByBoardIndex
else
boardIndexes = {0, 1, 2, 3, 4}
framesByBoardIndex = missionPage.Board.framesByBoardIndex
end
for _, boardIndex in pairs(boardIndexes) do
local follower = framesByBoardIndex[boardIndex]
local info = follower.info
if info then
info.boardIndex = follower.boardIndex
info.maxHealth = info.autoCombatantStats.maxHealth
info.health = info.autoCombatantStats.currentHealth
info.attack = info.autoCombatantStats.attack
info.isAutoTroop = info.isAutoTroop ~= nil and info.isAutoTroop or (info.quality == 0)
info.followerGUID = follower:GetFollowerGUID()
local XPToLvlUp = 0
if info.isAutoTroop or info.level == 60 then
info.isLoseLvlUp = false
info.isWinLvlUp = false
else
XPToLvlUp = isCompletedMission and info.maxXP - info.currentXP or info.levelXP - info.xp
info.isLoseLvlUp = XPToLvlUp <= newObj.baseXP
info.isWinLvlUp = XPToLvlUp <= newObj.winXP
end
if info.autoCombatSpells == nil then info.autoCombatSpells = follower.autoCombatSpells end
local myUnit = CMH.Unit:new(info)
--SELECTED_CHAT_FRAME:AddMessage("myUnitName = " .. myUnit.name)
newObj.units[follower.boardIndex] = myUnit
newObj.isEmpty = false
if myUnit.isAutoTroop == false then newObj.initialAlliesHP = newObj.initialAlliesHP + myUnit.currentHealth end
end
end
self.__index = self
setmetatable(newObj, self)
newObj:setHasRandomSpells()
if self.hasRandomSpells then self.max_rounds = MAX_RANDOM_ROUNDS end
return newObj
end
local boardForSimulate = {}
function Board:simulate()
if self.isEmpty then return end
if self.hasRandomSpells and self.isCalcRandom then
local win_count = 0
for i = 1, SIMULATE_ITERATIONS do
boardForSimulate = copy(self)
boardForSimulate:fight()
if boardForSimulate:isWin() then win_count = win_count + 1 end
wipe(CMH.Board.CombatLog)
wipe(CMH.Board.HiddenCombatLog)
wipe(CMH.Board.CombatLogEvents)
end
self.probability = math.floor(100 * win_count/SIMULATE_ITERATIONS)
elseif self.hasRandomSpells then
return
end
self:fight()
end
function Board:fight(round)
round = round or 1
if round == 1 then self:applyUnitsPassiveSkills() end
while self.isMissionOver == false and round < self.max_rounds do
CMH:log('\n')
CMH:log(GREEN_FONT_COLOR:WrapTextInColorCode(L["Round"] .. ' ' .. round))
MissionHelper:addRound()
local turnOrder = self:getTurnOrder()
local removed_effects = self:manageBuffsFromDeadUnits()
local enemy_turn = false
for _, boardIndex in pairs(turnOrder) do
CMH:debug_log('turn for index ' .. boardIndex)
if boardIndex > 4 and enemy_turn == false then
enemy_turn = true
CMH:log('\n')
end
self:makeUnitAction(round, boardIndex)
end
-- unit can die by DoT, but I don't check it inside makeUnitAction
self.isMissionOver = self:checkMissionOver()
round = round + 1
end
end
function Board:setHasRandomSpells()
for _, unit in pairs(self.units) do
for _, spell in pairs(unit.spells) do
for _, effect in pairs(spell.effects) do
if effect.TargetType == TargetTypeEnum.randomEnemy or effect.TargetType == TargetTypeEnum.randomEnemy_2
or effect.TargetType == TargetTypeEnum.randomAlly then
self.hasRandomSpells = true
return
end
end
end
end
self.hasRandomSpells = false
end
function Board:applyUnitsPassiveSkills()
for _, unit in pairs(self.units) do
if unit.passive_spell ~= nil then
for _, effect in pairs(unit.passive_spell.effects) do
local targetIndexes = self:getTargetIndexes(unit, effect.TargetType, -1)
CMH:debug_log("Effect: " .. effect.Effect .. ', TargetType: ' .. effect.TargetType)
CMH:debug_log("targetIndexes -> " .. arrayForPrint(targetIndexes))
local targetInfo = {}
for _, targetIndex in pairs(targetIndexes) do
local eventTargetInfo = unit:castSpellEffect(self.units[targetIndex], effect, unit.passive_spell, false)
table.insert(targetInfo, eventTargetInfo)
end
MissionHelper:addEvent(unit.passive_spell.ID, isAura(effect.Effect) and EffectTypeEnum.ApplyAura or effect.Effect, unit.boardIndex, targetInfo)
end
end
end
end
--- If one team dead, mission over
function Board:checkMissionOver()
local isMyTeamAlive = false
for i = 0, 4 do
if self:isUnitAlive(i) then
isMyTeamAlive = true
break
end
end
local isEnemyTeamAlive = false
for i = 5, 12 do
if self:isUnitAlive(i) then
isEnemyTeamAlive = true
break
end
end
return not (isMyTeamAlive and isEnemyTeamAlive)
end
function Board:isUnitAlive(boardIndex)
local unit = self.units[boardIndex]
if unit then
return unit:isAlive()
end
return false
end
local function isFriendlyUnit(sourceIndex, targetIndex)
return (sourceIndex <= 4 and targetIndex <= 4) or (sourceIndex > 4 and targetIndex > 4)
end
function Board:isTargetableUnit(sourceIndex, targetIndex)
return self:isUnitAlive(targetIndex) and (not self.units[targetIndex].untargetable or isFriendlyUnit(sourceIndex, targetIndex))
end
function Board:getTargetableUnits(sourceIndex)
local result = {}
for i = 0, 12 do
table.insert(result, i, self:isTargetableUnit(sourceIndex, i))
end
CMH:debug_log("targetableUnits -> " .. arrayForPrint(result))
return result
end
function Board:getTurnOrder()
local order = {}
local sort_table = {}
for i = 0, 4 do
if self:isUnitAlive(i) then table.insert(sort_table, self.units[i]) end
end
table.sort(sort_table, function (a, b) return (b.currentHealth < a.currentHealth) end)
for _, unit in pairs(sort_table) do
table.insert(order, unit.boardIndex)
end
sort_table = {}
for i = 5, 12 do
if self:isUnitAlive(i) then table.insert(sort_table, self.units[i]) end
end
table.sort(sort_table, function (a, b) return (b.currentHealth < a.currentHealth) end)
for _, unit in pairs(sort_table) do
table.insert(order, unit.boardIndex)
end
CMH:debug_log("turn order -> " .. arrayForPrint(order))
return order
end
function Board:makeUnitAction(round, boardIndex)
if self.isMissionOver then return end
local unit = self.units[boardIndex]
if not unit:isAlive() then return end
local targetIndexes, aliveUnits, lastTargetType
unit:decreaseSpellsCooldown()
self:manageAppliedBuffs(unit)
for _, spell in pairs(unit:getAvailableSpells()) do
if self.isMissionOver then break end
CMH:debug_log("Spell: " .. spell.name .. ' (' .. #spell.effects .. ')')
lastTargetType = -1
for _, effect in pairs(spell.effects) do
targetIndexes = self:getTargetIndexes(unit, effect.TargetType, lastTargetType, targetIndexes)
CMH:debug_log("Effect: " .. effect.Effect .. ', TargetType: ' .. effect.TargetType)
CMH:debug_log("targetIndexes -> " .. arrayForPrint(targetIndexes))
local targetInfo = {}
for _, targetIndex in pairs(targetIndexes) do
local eventTargetInfo = unit:castSpellEffect(self.units[targetIndex], effect, spell, false)
table.insert(targetInfo, eventTargetInfo)
end
MissionHelper:addEvent(spell.ID, isAura(effect.Effect) and EffectTypeEnum.ApplyAura or effect.Effect, boardIndex, targetInfo)
for _, info in pairs(targetInfo) do
self:onUnitTakeDamage(spell.ID, boardIndex, info, effect)
end
if effect.TargetType ~= TargetTypeEnum.lastTarget then lastTargetType = effect.TargetType end
end
unit:startSpellCooldown(spell.ID)
end
end
function Board:manageAppliedBuffs(sourceUnit)
local removed_buffs = {}
for _, unit in pairs(self.units) do
if unit:isAlive() then
local unit_removed_buffs = unit:manageBuffs(sourceUnit)
for _, buff in pairs(unit_removed_buffs) do
table.insert(removed_buffs, buff)
end
end
end
return removed_buffs
end
function Board:manageBuffsFromDeadUnits()
local removed_buffs = {}
for _, unit in pairs(self.units) do
if not self:isUnitAlive(unit.boardIndex) then
local unit_removed_buffs = self:manageAppliedBuffs(unit)
for _, buff in pairs(unit_removed_buffs) do
table.insert(removed_buffs, buff)
end
end
end
end
function Board:onUnitTakeDamage(spellID, casterBoardIndex, eventTargetInfo, effect)
-- check reflect
if effect.Effect == EffectTypeEnum.Damage or effect.Effect == EffectTypeEnum.Damage_2 then
--CMH:debug_log(string.format('casterIndex = %s, targetIndex = %s, spellID = %s, effect = %s, targetReflect = %s',
-- casterBoardIndex, eventTargetInfo.boardIndex, spellID, effect.Effect, self.units[eventTargetInfo.boardIndex].reflect))
local targetUnit = self.units[eventTargetInfo.boardIndex]
if targetUnit.reflect > 0 then
local reflectEventTargetInfo = targetUnit:castSpellEffect(self.units[casterBoardIndex], {Effect = EffectTypeEnum.Reflect, ID = -1}, {}, true)
MissionHelper:addEvent(spellID, CMH.DataTables.EffectTypeEnum.Reflect, targetUnit.boardIndex, {reflectEventTargetInfo})
self:onUnitTakeDamage(spellID, targetUnit.boardIndex, reflectEventTargetInfo, {Effect = EffectTypeEnum.Reflect})
end
end
-- unit died
if eventTargetInfo.newHealth == 0 then
MissionHelper:addEvent(spellID, CMH.DataTables.EffectTypeEnum.Died, casterBoardIndex, {eventTargetInfo})
CMH:log(ORANGE_FONT_COLOR:WrapTextInColorCode(string.format('%s %s %s ',
self.units[casterBoardIndex].name, L['kill'], self.units[eventTargetInfo.boardIndex].name)))
self.isMissionOver = self:checkMissionOver()
end
end
function Board:getTotalLostHP(isWin)
local restHP = 0
local _start, _end, startHP = 0, 4, self.initialAlliesHP
if not isWin then _start, _end, startHP = 5, 12, self.initialEnemiesHP end
for i = _start, _end do
if self.units[i] and (self.units[i].isAutoTroop == false or not isWin) then
if self.units[i].isWinLvlUp then
restHP = restHP + self.units[i].maxHealth
elseif self:isUnitAlive(i) then
restHP = restHP + self.units[i].currentHealth
end
end
end
return startHP - restHP
end
function Board:getTotalRemainingPercentHP(isWin)
local restHP = 0
local _start, _end, startHP = 0, 4, self.initialAlliesHP
local followerCount = 0
for i = _start, _end do
if self.units[i] and self.units[i].isAutoTroop == false then followerCount = followerCount + 1 end
end
if not isWin then _start, _end, startHP = 5, 12, self.initialEnemiesHP end
for i = _start, _end do
if self.units[i] and (self.units[i].isAutoTroop == false or not isWin) then
if self.units[i].isWinLvlUp then
restHP = restHP + 1
elseif self:isUnitAlive(i) then
restHP = restHP + self.units[i].currentHealth/self.units[i].maxHealth
end
end
end
return followerCount - restHP
end
function Board:getMinLostHPPercent(isWin)
-- 1 and 2 is 100% and 200% HP. avoid excess calculation
local minLostHPPercent = 2
if isWin then
local _start, _end = 0, 4
for i = _start, _end do
if self.units[i] and (self.units[i].isAutoTroop == false) and not self.units[i].isWinLvlUp then
if self.units[i].currentHealth == 0 then return 1 end
minLostHPPercent = min(minLostHPPercent, self.units[i].currentHealth/self.units[i].maxHealth)
end
end
return 1 - minLostHPPercent
else
local _start, _end = 5, 12
for i = _start, _end do
if self.units[i] and self:isUnitAlive(i) then
minLostHPPercent = min(minLostHPPercent, self.units[i].currentHealth/self.units[i].maxHealth)
end
end
return 1 + minLostHPPercent
end
end
local function constructString(unit, isWin)
local percentHP = 100
if not unit.isWinLvlUp then percentHP = 100 * unit.currentHealth/unit.maxHealth end
local result = unit.name .. L['.'] .. ' ' .. L['HP'] .. ' = ' .. unit.currentHealth .. '/' .. unit.maxHealth .. ' (' .. math.floor(percentHP) .. '%)' .. '\n'
--result = unit.isWinLvlUp and result .. ' (Level Up)\n' or result .. '\n'
if (isWin and unit.isWinLvlUp) or (not isWin and unit.isLoseLvlUp) then result = LVL_UP_ICON .. result end
if unit.currentHealth == 0 then result = SKULL_ICON .. result end
return ' ' .. result
end
function Board:getResultInfo()
if self.isEmpty then return '' end
if self.hasRandomSpells and self.isCalcRandom == false then
return L["Units have random abilities. The mission isn't simulate automatically.\nClick on the button to check the result."]
end
local isWin = self:isWin()
local lostHP = self:getTotalLostHP(true)
local loseOrGain = lostHP >= 0 and L['LOST'] or L['RECEIVED']
local warningText = self.hasRandomSpells and RED_FONT_COLOR:WrapTextInColorCode(
L["Units have random abilities. Actual rest HP may not be the same as predicted"]) or ''
local text = ''
local avgPercentHP = 0
local minPercentHP = 1
local followerCount = 0
local currentHealth = 0
for i = 0, 4 do
if self.units[i] then
text = text .. constructString(self.units[i], isWin)
if (self:isWin() and self.units[i].isWinLvlUp) or (not self:isWin() and self.units[i].isLoseLvlUp) then
currentHealth = self.units[i].maxHealth
else
currentHealth = self.units[i].currentHealth
end
if not self.units[i].isAutoTroop then
avgPercentHP = avgPercentHP + currentHealth/self.units[i].maxHealth
minPercentHP = min(minPercentHP, currentHealth/self.units[i].maxHealth)
followerCount = followerCount + 1
end
end
end
text = string.format("%s\n%s:\n%s \n\n%s %s %s = %s",
warningText, L['My units'], text, L['TOTAL'], loseOrGain, L['HP'], math.abs(lostHP))
text = text .. '\n' .. L['Average HP'].. ' = ' .. math.floor(100 * avgPercentHP/followerCount) .. '%\n'
.. L['Minimal HP'].. ' = ' .. math.floor(100 * minPercentHP) .. '%'
if isWin == false then
local enemyInfo = ''
for i = 5, 12 do
if self.units[i] then enemyInfo = enemyInfo .. constructString(self.units[i], isWin) end
end
local remainingHP = self.initialEnemiesHP - self:getTotalLostHP(false)
enemyInfo = string.format('%s:\n%s', L['Enemy units'], enemyInfo)
local total = RED_FONT_COLOR:WrapTextInColorCode(
string.format('%s = %s/%s (%s%%)',
L['TOTAL REMAINING HP'], remainingHP, self.initialEnemiesHP, math.floor(100*remainingHP/self.initialEnemiesHP))
)
text = text .. '\n\n\n\n' ..enemyInfo .. '\n\n' .. total
end
return text
end
function Board:constructResultString()
if self.isEmpty then
return L['Add units on board']
elseif self.hasRandomSpells and self.isCalcRandom == false then
return ''
elseif not self.isMissionOver then
return RED_FONT_COLOR:WrapTextInColorCode(string.format(L['More than %s rounds. Winner is undefined'], self.max_rounds))
end
local result = self:isWin()
if self.probability == 100 and result then
return GREEN_FONT_COLOR:WrapTextInColorCode(L['WIN'])
elseif self.probability == 0 or (result == false and self.probability == 100) then
return RED_FONT_COLOR:WrapTextInColorCode(L['LOSE'])
else
return ORANGE_FONT_COLOR:WrapTextInColorCode(string.format(L['WIN'] .. ' (~%s%%)', self.probability))
end
end
function Board:isWin()
for _, unit in pairs(self.units) do
if unit:isAlive() then
if unit.boardIndex > 4 then return false else return true
end
end
end
end
function Board:getTargetIndexes(unit, targetType, lastTargetType, lastTargetIndexes)
-- update targets if skill has different effects target type
if lastTargetType ~= targetType and targetType ~= TargetTypeEnum.lastTarget then
local aliveUnits = self:getTargetableUnits(unit.boardIndex)
return CMH.TargetManager:getTargetIndexes(unit.boardIndex, targetType, aliveUnits, unit.tauntedBy)
else
return lastTargetIndexes
end
end
CMH.Board = Board