-
Notifications
You must be signed in to change notification settings - Fork 26
/
BattleUi.ttslua
407 lines (348 loc) · 17.6 KB
/
BattleUi.ttslua
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
local Check = require("Kdm/Util/Check")
local EventManager = require("Kdm/Util/EventManager")
local Location = require("Kdm/Location")
local log = require("Kdm/Log").ForModule("BattleUi")
local Monster = require("Kdm/Monster")
local NamedObject = require("Kdm/NamedObject")
local Player = require("Kdm/Player")
local Survivor = require("Kdm/Survivor")
local Ui = require("Kdm/Ui")
local Util = require("Kdm/Util/Util")
---------------------------------------------------------------------------------------------------
local BattleUi = {}
BattleUi.MAX_WEAPONS = 3
BattleUi.PLAYER_SPACING = 0
BattleUi.TEXT_COLOR = "#ffffffdc"
BattleUi.DISABLED_TEXT_COLOR = "#ffffff60"
BattleUi.DARK_COLORS = "#00000000|#ffffff10|#ffffff20|#ffffffff"
BattleUi.DARK_SELECTED_COLORS = "#ffffff20|#ffffff30|#ffffff40|#ffffffff"
---------------------------------------------------------------------------------------------------
function BattleUi.Init(saveState)
BattleUi.hiddenWeaponsBySurvivor = {}
for survivorId, hiddenWeapons in pairs(saveState or {}) do
local survivor = Survivor.SurvivorForId(survivorId)
if survivor == nil then
log:Debugf("No survivor with ID %d doesn't exist", survivorId)
else
BattleUi.hiddenWeaponsBySurvivor[survivor] = hiddenWeapons
end
end
BattleUi.InitUi(ui)
end
function BattleUi.InitUi(ui)
local ui = Ui.Get2d()
BattleUi.ui = ui
ui:Image({ id = "BattleUi", rectAlignment = "MiddleLeft", x = 10, y = -20, width = 50, height = 50, image = "BattleUi" })
ui:Button({ id = "BattleUi", rectAlignment = "MiddleLeft", x = 10-1, y = -20+1, width = 50+2, height = 50+2, colors = BattleUi.DARK_COLORS, onClick = function()
if BattleUi.uiOpen then
BattleUi.Hide()
else
BattleUi.Show()
end
end })
ui:Image({ id = "NextTurn", rectAlignment = "MiddleLeft", x = 10, y = -80, width = 50, height = 50, image = "NextTurn" })
ui:Button({ id = "NextTurn", rectAlignment = "MiddleLeft", x = 10-1, y = -80+1, width = 50+2, height = 50+2, colors = BattleUi.DARK_COLORS, onClick = BattleUi.NextTurn })
BattleUi.uiOpen = false
BattleUi.panel = ui:Panel({ id = "BattleUi", x = 70, y = -210, width = 436, height = 608, color = "#00000000", active = BattleUi.uiOpen })
BattleUi.panel.attributes.allowDragging = true
BattleUi.panel.attributes.returnToOriginalPositionWhenReleased = false
BattleUi.playerUis = {}
for _, player in ipairs(Player.Players()) do
local playerUi = { height = 140 }
local ordinal = player:Ordinal()
BattleUi.playerUis[ordinal] = playerUi
playerUi.open = true
local panel = BattleUi.panel:Panel({ id = "Player"..ordinal, x = 0, y = -(playerUi.height + BattleUi.PLAYER_SPACING) * (ordinal - 1), width = 436, height = playerUi.height, color = "#121212ff" })
playerUi.panel = panel
playerUi.playerCircleImage = panel:Image({ id = "PlayerCircle", x = 8, y = -8, width = 25, height = 25, image = "WhiteCircle" })
playerUi.nameText = panel:Text({ id = "Name", x = 38, y = -8, width = 280, height = 25, fontSize = 16, color = BattleUi.TEXT_COLOR, text = "Survivor with a very long name" })
playerUi.turnEnded = false
panel:Image({ id = "EndTurn", x = 324, y = -8, width = 48, height = 25, image = "BR_Sleep" })
playerUi.endTurnButton = panel:Button({ id = "EndTurn", x = 324-1, y = -8+1, width = 48+2, height = 25+2, colors = BattleUi.DARK_COLORS, onClick = function()
playerUi.turnEnded = not playerUi.turnEnded
if playerUi.turnEnded then
log:Printf("Player %d: %s turn ended", ordinal, player:SurvivorSheet():Survivor():NameOrUnnamed())
playerUi.playerCircleImage:SetImage("GreyCircle")
playerUi.nameText:SetColor(BattleUi.DISABLED_TEXT_COLOR)
playerUi.nameText:SetFontStyle("Italic")
playerUi.endTurnButton:SetColors(BattleUi.DARK_SELECTED_COLORS)
else
log:Printf("Player %d: %s next turn", ordinal, player:SurvivorSheet():Survivor():NameOrUnnamed())
playerUi.playerCircleImage:SetImage("WhiteCircle")
playerUi.nameText:SetColor(BattleUi.TEXT_COLOR)
playerUi.nameText:SetFontStyle("Normal")
playerUi.endTurnButton:SetColors(BattleUi.DARK_COLORS)
end
end})
playerUi.showWeaponsImage = panel:Image({ id = "ShowWeapons", x = 380, y = -8, width = 48, height = 25, image = "BR_Show" })
playerUi.showWeaponsButton = panel:Button({ id = "ShowWeapons", x = 380-1, y = -8+1, width = 48+2, height = 25+2, colors = BattleUi.DARK_COLORS, onClick = function()
local survivor = player:SurvivorSheet():Survivor()
log:Printf("Showing all weapons for %s", survivor:NameOrUnnamed())
BattleUi.hiddenWeaponsBySurvivor[survivor] = nil
BattleUi.UpdatePlayer(player)
end})
playerUi.weaponImages = {}
playerUi.weaponHideWeaponsButtons = {}
playerUi.weaponNameTexts = {}
playerUi.weaponSpeedTexts = {}
playerUi.weaponHitTexts = {}
playerUi.weaponWoundTexts = {}
playerUi.weaponCritTexts = {}
local y = -41
for i = 1, BattleUi.MAX_WEAPONS do
playerUi.weaponImages[i] = panel:Image({ id = "Weapon"..i, x = 8, y = y, width = 420, height = 25, image = "BR_Weapon" })
playerUi.weaponHideWeaponsButtons[i] = panel:Button({ id = "WeaponName"..i, x = 8-1, y = y+1, width = 8+19+8+2, height = 25+2, colors = BattleUi.DARK_COLORS, onClick = function()
local text = playerUi.weaponNameTexts[i]
local weaponName = text.object.UI.getAttribute(text.attributes.id, "text")
local survivor = player:SurvivorSheet():Survivor()
log:Printf("Hiding %s for %s", weaponName, survivor:NameOrUnnamed())
if not BattleUi.hiddenWeaponsBySurvivor[survivor] then
BattleUi.hiddenWeaponsBySurvivor[survivor] = {}
end
BattleUi.hiddenWeaponsBySurvivor[survivor][weaponName] = true
BattleUi.UpdatePlayer(player)
end })
playerUi.weaponNameTexts[i] = panel:Text({ id = "WeaponName"..i, x = 43, y = y, width = 172, height = 25, color = BattleUi.TEXT_COLOR, fontSize = 14, text = "Calcified Juggernaut Blade" })
playerUi.weaponSpeedTexts[i] = panel:Text({ id = "WeaponSpeed"..i, x = 243, y = y, width = 20, height = 25, color = BattleUi.TEXT_COLOR, fontSize = 16, text = "44" })
playerUi.weaponHitTexts[i] = panel:Text({ id = "WeaponHit"..i, x = 288, y = y, width = 20, height = 25, color = BattleUi.TEXT_COLOR, fontSize = 16, text = "44" })
playerUi.weaponWoundTexts[i] = panel:Text({ id = "WeaponWound"..i, x = 334, y = y, width = 55, height = 25, color = BattleUi.TEXT_COLOR, fontSize = 16, text = "44 (+44)" })
playerUi.weaponCritTexts[i] = panel:Text({ id = "WeaponCrit"..i, x = 407, y = y, width = 20, height = 25, color = BattleUi.TEXT_COLOR, fontSize = 16, text = "44" })
y = y - 33
end
end
end
---------------------------------------------------------------------------------------------------
BattleUi.RELEVANT_SURVIVOR_STATS = {
["name"] = true,
["modifiedSpeed"] = true,
["modifiedAccuracy"] = true,
["modifiedStrength"] = true,
["modifiedEvasion"] = true,
["modifiedLuck"] = true,
}
BattleUi.RELEVANT_MONSTER_STATS = {
["toughness"] = true,
["evasion"] = true,
["luck"] = true,
}
function BattleUi.PostInit()
-- We specifically delay registering event handlers until PostInit() to avoid redundant UpdatePlayer() calls from
-- the ON_PLAYER_SURVIVOR_LINKED events emitted from Player.Init()
EventManager.AddHandler(EventManager.ON_SHOWDOWN_STARTED, BattleUi.Show)
EventManager.AddHandler(EventManager.ON_SHOWDOWN_ENDED, BattleUi.Hide)
EventManager.AddHandler(EventManager.ON_PLAYER_SURVIVOR_LINKED, function(player) BattleUi.UpdatePlayer(player) end)
EventManager.AddHandler(EventManager.ON_PLAYER_SURVIVOR_UNLINKED, function(player) BattleUi.UpdatePlayer(player) end)
EventManager.AddHandler(EventManager.ON_SURVIVOR_STAT_CHANGED, function(survivor, stat, value)
if BattleUi.RELEVANT_SURVIVOR_STATS[stat] then
BattleUi.Update()
end
end)
EventManager.AddHandler(EventManager.ON_MONSTER_STAT_CHANGED, function(stat)
if BattleUi.RELEVANT_MONSTER_STATS[stat] then
BattleUi.Update()
end
end)
EventManager.AddHandler(EventManager.ON_PLAYER_WEAPONS_CHANGED, function(player) BattleUi.UpdatePlayer(player) end)
EventManager.AddHandler(EventManager.ON_SURVIVOR_DESTROYED, function(survivor) BattleUi.hiddenWeaponsBySurvivor[survivor] = nil end)
EventManager.AddHandler(EventManager.ON_PLAYER_COLOR_CHANGED, function(player, colorTable, colorStr)
BattleUi.playerUis[player:Ordinal()].playerCircleImage:SetColor(colorStr)
end)
BattleUi.Update()
end
---------------------------------------------------------------------------------------------------
function BattleUi.Save()
local hiddenWeaponsBySurvivorId = {}
for survivor, hiddenWeapons in pairs(BattleUi.hiddenWeaponsBySurvivor) do
hiddenWeaponsBySurvivorId[survivor:Id()] = hiddenWeapons
end
return hiddenWeaponsBySurvivorId
end
---------------------------------------------------------------------------------------------------
function BattleUi.CalcWeapons(player, survivor)
-- dedup and consolidate weapons
local hiddenWeapons = BattleUi.hiddenWeaponsBySurvivor[survivor] or {}
local weaponsAndModifiers = {}
local weaponCounts = {}
for _, weaponAndModifiers in pairs(player:WeaponsAndModifiers()) do
local weapon = weaponAndModifiers.weapon
if not hiddenWeapons[weapon.name] then
local count = weaponCounts[weapon.name] or 0
weaponCounts[weapon.name] = count + 1
if count == 0 then
table.insert(weaponsAndModifiers, weaponAndModifiers)
end
end
end
table.sort(weaponsAndModifiers, function(wm1, wm2) return wm1.weapon.name < wm2.weapon.name end)
local survivorSpeed = survivor:ModifiedSpeed()
local survivorAccuracy = survivor:ModifiedAccuracy()
local survivorStrength = survivor:ModifiedStrength()
local survivorLuck = survivor:ModifiedLuck()
local monsterEvasion = Monster.Evasion()
local monsterToughness = Monster.Toughness()
local monsterLuck = Monster.Luck()
local results = {}
for _, weaponAndModifiers in ipairs(weaponsAndModifiers) do
local weapon = weaponAndModifiers.weapon
local speed = Util.Max(1, weapon.speed + survivorSpeed + (weaponAndModifiers.modifiers["speed"] or 0))
-- accuracy goes the opposite way
local accuracy = weapon.accuracy - survivorAccuracy - (weaponAndModifiers.modifiers["accuracy"] or 0) -- accuracy goes the opposite way
local strength = weapon.strength + survivorStrength + (weaponAndModifiers.modifiers["strength"] or 0)
local luck = (weapon.deadly or 0) + survivorLuck + (weaponAndModifiers.modifiers["luck"] or 0)
local hit = Util.Clamp(accuracy + monsterEvasion, 2, 10)
local wound = Util.Max(monsterToughness - strength, 2) -- sharp weapons means we need to show the target roll if it's past 10
local crit = Util.Clamp(10 - luck + monsterLuck, 2, 10)
if weapon.paired and weaponCounts[weapon.name] > 1 then
speed = speed + weapon.speed
end
if weapon.slow then
speed = 1
end
table.insert(results, {
name = weapon.name,
speed = speed,
hit = hit,
wound = wound,
strength = strength,
crit = crit,
})
end
return results
end
---------------------------------------------------------------------------------------------------
function BattleUi.UpdatePositions()
local y = 0
for ordinal, _ in ipairs(Player.Players()) do
local playerUi = BattleUi.playerUis[ordinal]
playerUi.panel:SetOffsetXY("0 "..y)
if playerUi.open then
y = y - BattleUi.playerUis[ordinal].height - BattleUi.PLAYER_SPACING
end
end
BattleUi.panel:SetHeight(-y - 16)
end
---------------------------------------------------------------------------------------------------
function BattleUi.UpdatePlayerInternal(player)
local playerUi = BattleUi.playerUis[player:Ordinal()]
if not player:SurvivorSheet() then
log:Debugf("Hiding %s", player)
playerUi.open = false
playerUi.height = 0
playerUi.panel:Hide()
return
end
local survivor = player:SurvivorSheet():Survivor()
log:Debugf("Updating %s", player)
playerUi.open = true
playerUi.panel:Show()
playerUi.nameText:SetText(survivor:NameOrUnnamed())
playerUi.showWeaponsButton:Show()
local weapons = BattleUi.CalcWeapons(player, survivor)
local numShownWeapons = Util.Min(#weapons, BattleUi.MAX_WEAPONS)
for i = 1, numShownWeapons do
local weapon = weapons[i]
playerUi.weaponImages[i]:Show()
playerUi.weaponHideWeaponsButtons[i]:Show()
playerUi.weaponNameTexts[i]:Show()
playerUi.weaponNameTexts[i]:SetText(weapon.name)
playerUi.weaponSpeedTexts[i]:Show()
playerUi.weaponSpeedTexts[i]:SetText(weapon.speed)
playerUi.weaponHitTexts[i]:Show()
playerUi.weaponHitTexts[i]:SetText(weapon.hit)
playerUi.weaponWoundTexts[i]:Show()
playerUi.weaponWoundTexts[i]:SetText(string.format("%d (%s%d)", weapon.wound, weapon.strength > 0 and "+" or "", weapon.strength))
playerUi.weaponCritTexts[i]:Show()
playerUi.weaponCritTexts[i]:SetText(weapon.crit)
end
for i = numShownWeapons + 1, BattleUi.MAX_WEAPONS do
playerUi.weaponImages[i]:Hide()
playerUi.weaponHideWeaponsButtons[i]:Hide()
playerUi.weaponNameTexts[i]:Hide()
playerUi.weaponSpeedTexts[i]:Hide()
playerUi.weaponHitTexts[i]:Hide()
playerUi.weaponWoundTexts[i]:Hide()
playerUi.weaponCritTexts[i]:Hide()
end
playerUi.height = 8 + (numShownWeapons + 1) * 33
playerUi.panel:SetHeight(playerUi.height)
end
---------------------------------------------------------------------------------------------------
function BattleUi.UpdatePlayer(player)
BattleUi.UpdatePlayerInternal(player)
BattleUi.UpdatePositions()
end
---------------------------------------------------------------------------------------------------
function BattleUi.Update()
for _, player in ipairs(Player.Players()) do
BattleUi.UpdatePlayerInternal(player)
end
BattleUi.UpdatePositions()
end
---------------------------------------------------------------------------------------------------
function BattleUi.NextTurn()
log:Printf("Starting next turn")
local monsterControllerObject = nil
local monsterControllerPlayer = nil
local players = Player.Players()
for i, _ in ipairs(players) do
local playerPrefix = "Player "..i
local survivalTokens = Location.Get(playerPrefix.." Survival Tokens"):AllObjects("Survival Tokens")
for _, token in ipairs(survivalTokens) do
rot = token.getRotation()
if rot.z >= 15 and rot.z <= 345 then
token.flip()
end
end
local playerUi = BattleUi.playerUis[i]
playerUi.turnEnded = false
playerUi.playerCircleImage:SetImage("WhiteCircle")
local color = NamedObject.Get(playerPrefix.." Marker").getColorTint()
playerUi.playerCircleImage:SetColor("#"..Color(color):toHex())
playerUi.nameText:SetColor(BattleUi.TEXT_COLOR)
playerUi.nameText:SetFontStyle("Normal")
playerUi.endTurnButton:SetColors(BattleUi.DARK_COLORS)
if not monsterControllerObject then
for _, hit in ipairs(Location.Get(playerPrefix.." Upper"):BoxCast()) do
local object = hit.hit_object
if object.getGMNotes() == "Monster Controller" then
log:Debugf("Found monster controller %s at player %d", object.getGUID(), i)
monsterControllerObject = object
monsterControllerPlayer = i
break
end
end
end
end
if monsterControllerObject then
monsterControllerPlayer = monsterControllerPlayer + 1
if monsterControllerPlayer > #players then
monsterControllerPlayer = 1
end
local survivorSheet = players[monsterControllerPlayer]:SurvivorSheet()
if survivorSheet then
log:Printf("%s is now the monster controller.", survivorSheet:Survivor():NameOrUnnamed())
else
log:Printf("Player %d is now the monster controller.", monsterControllerPlayer)
end
local newPosition = Location.Get("Player "..monsterControllerPlayer.." Monster Controller"):Center()
monsterControllerObject.setPositionSmooth(newPosition, false, false)
else
log:Debugf("No monster controller found")
end
end
---------------------------------------------------------------------------------------------------
function BattleUi.Show()
BattleUi.panel:Show()
BattleUi.uiOpen = true
end
---------------------------------------------------------------------------------------------------
function BattleUi.Hide()
BattleUi.panel:Hide()
BattleUi.uiOpen = false
end
---------------------------------------------------------------------------------------------------
return {
Init = BattleUi.Init,
PostInit = BattleUi.PostInit,
Save = BattleUi.Save,
}