forked from jordanmchavez/kdm-tts
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Showdown.ttslua
843 lines (709 loc) · 34.7 KB
/
Showdown.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
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
local Archive = require("Kdm/Archive")
local Check = require("Kdm/Util/Check")
local Container = require("Kdm/Util/Container")
local EventManager = require("Kdm/Util/EventManager")
local Expansion = require("Kdm/Expansion")
local Grid = require("Kdm/Util/Grid")
local log = require("Kdm/Log").ForModule("Showdown")
local Location = require("Kdm/Location")
local Monster = require("Kdm/Monster")
local NamedObject = require("Kdm/NamedObject")
local Overlay = require("Kdm/Util/Overlay")
local Player = require("Kdm/Player")
local Rules = require("Kdm/Rules")
local Terrain = require("Kdm/Terrain")
local Ui = require("Kdm/Ui")
local Util = require("Kdm/Util/Util")
---------------------------------------------------------------------------------------------------
local Showdown = {}
Showdown.MAX_TRAITS_MOODS = 14
Showdown.MAX_TERRAIN = 10
---------------------------------------------------------------------------------------------------
function Showdown.Init()
Showdown.settingUp = false
Showdown.InitExpansions()
Showdown.InitUi()
Showdown.InitOverlay()
EventManager.AddHandler("onObjectPickUp", Showdown.OnObjectPickUp)
EventManager.AddHandler("onObjectDrop", Showdown.OnObjectDrop)
end
---------------------------------------------------------------------------------------------------
function Showdown.InitExpansions()
Showdown.monsters = {}
Showdown.monstersByName = {}
Showdown.expansionsByMonsterName = {}
Showdown.sizesByMonsterName = {}
Showdown.levelsByMonsterLevelName = {}
Showdown.minionsByName = {}
for _, expansion in ipairs(Expansion.All()) do
for _, monster in ipairs(expansion.monsters or {}) do
if monster.resourcesDeck == nil then monster.resourcesDeck = monster.name.." Resources" end
if monster.info == nil then monster.info = monster.name.." Info" end
if monster.basicAction == nil then monster.basicAction = monster.name.." Basic Action" end
if monster.basicAiDeck == nil then monster.basicAiDeck = monster.name.." Basic AI" end
if monster.basicAiDeck2 == nil then monster.basicAiDeck2 = monster.name.." Basic AI Two" end --Second AI Deck
if monster.advancedAiDeck == nil then monster.advancedAiDeck = monster.name.." Advanced AI" end
if monster.advancedAiDeck2 == nil then monster.advancedAiDeck2 = monster.name.." Advanced AI Two" end --Second AI Deck
if monster.legendaryAiDeck == nil then monster.legendaryAiDeck = monster.name.." Legendary AI" end
if monster.legendaryAiDeck2 == nil then monster.legendaryAiDeck2 = monster.name.." Legendary AI Two" end --Second AI Deck
if monster.specialAiDeck == nil then monster.specialAiDeck = monster.name.." Special AI" end
if monster.hitLocationsDeck == nil then monster.hitLocationsDeck = monster.name.." Hit Locations" end
monster.expansion = expansion
table.insert(Showdown.monsters, monster)
Showdown.monstersByName[monster.name] = monster
Showdown.expansionsByMonsterName[monster.name] = expansion
Showdown.sizesByMonsterName[monster.name] = { x = monster.size.x, y = monster.size.y }
for _, level in ipairs(monster.levels) do
Showdown.levelsByMonsterLevelName[monster.name..level.name] = level
end
end
for _, minion in ipairs(expansion.minions or {}) do
Showdown.minionsByName[minion.name] = minion
end
end
table.sort(Showdown.monsters, function(x, y) return x.name < y.name end)
end
---------------------------------------------------------------------------------------------------
function Showdown.InitUi()
Showdown.uiOpen = false
local ui = Ui.Get2d()
local panel = ui:Panel({ id = "Showdown", rectAlignment = "MiddleCenter", x = 0, y = 0, width = 667, height = 632, active = false })
Showdown.panel = panel
panel:Image({ id = "Showdown", image = "Showdown", x = 0, y = 0, width = 667, height = 632 })
panel:Button({ id = "Close", x = 627, y = -10, width = 30, height = 30, onClick = Showdown.HideUi })
local monsterScroll = panel:VerticalScroll({ id = "V", x = 20+3, y = -(109+3), width = 306-6, height = 216-6 })
local monsterPanel = monsterScroll:Panel({ id = "Monster", x = 0, y = 0, width = 280, height = #Showdown.monsters * 30 })
Showdown.monsterButtonGroup = monsterPanel:OptionButtonGroup({ id = "Monster", textAlignment = "MiddleLeft", fontSize = 16, onClick = Showdown.SelectMonster })
Showdown.monsterOptionButtons = {}
local y = 0
for _, monster in ipairs(Showdown.monsters) do
Showdown.monsterOptionButtons[monster.name] = Showdown.monsterButtonGroup:OptionButton({x = 0, y = y, width = 280, height = 30, text = monster.name, optionValue = monster })
y = y - 30
end
local levelPanel = panel:Panel({ id = "Level", x = 341+3, y = -(109+3), width = 306-6, height = 216-6 })
Showdown.levelButtonGroup = levelPanel:OptionButtonGroup({ id = "Level", textAlignment = "MiddleLeft", fontSize = 16, onClick = function(option)
local level = option:OptionValue()
log:Debugf("Selected %s, %s", Showdown.monster.name, level.name)
Showdown.level = level
option:Select()
end })
Showdown.levelOptionButtons = {}
local y = 0
for i = 1, 7 do
Showdown.levelOptionButtons[i] = Showdown.levelButtonGroup:OptionButton({ x = 0, y = y, width = 300, height = 30, active = false })
y = y - 30
end
panel:Button({ id = "Begin", x = 183, y = -345, width = 301, height = 60, onClick = function()
if not Showdown.monster or not Showdown.level then
return log:Broadcastf("Please select a monster and level")
end
local expansion = Showdown.expansionsByMonsterName[Showdown.monster.name]
if not Expansion.IsEnabled(expansion.name) then
return log:Broadcastf("Requires %s expansion.", expansion.name)
end
log:Debugf("Begin showdown clicked for monster %s, level %s", Showdown.monster.name, Showdown.level.name)
Showdown.HideUi()
Showdown.Setup(Showdown.monster, Showdown.level)
end })
panel:Button({ id = "Clean", x = 183, y = -552, width = 301, height = 60, onClick = function()
log:Debugf("Clean showdown clicked")
Showdown.Clean()
Showdown.HideUi()
end })
end
---------------------------------------------------------------------------------------------------
function Showdown.InitOverlay()
local overlayObject = NamedObject.Get("Overlay")
local left = 27.011892
local top = -15.859633
local right = 76.526237
local bottom = -51.838516
-- local left = 6.363780
-- local top = -3.159184
-- local right = -6.015290
-- local bottom = 5.837886
local width = right - left
local cellWidth = width / 21
local height = bottom - top
local cellHeight = height / 15
Showdown.grid = Grid.Create({
object = overlayObject,
left = left + cellWidth / 2,
right = right - cellWidth / 2,
top = top + cellHeight / 2,
bottom = bottom - cellHeight / 2,
-- left = 6.0777295,
-- top = -2.86238375,
-- right = -5.73516825,
-- bottom = 5.527284,
cols = 22,
rows = 16,
})
Showdown.overlay = Overlay.Create({
object = overlayObject,
grid = Showdown.grid,
--height = 10.75,
height = 11.750850,
})
end
---------------------------------------------------------------------------------------------------
function Showdown.ShowUi()
log:Debugf("Showing showdown UI")
Showdown.panel:Show()
Showdown.uiOpen = true
end
function Showdown.HideUi()
log:Debugf("Hiding showdown UI")
Showdown.panel:Hide()
Showdown.uiOpen = false
end
function Showdown.IsUiOpen()
return Showdown.uiOpen
end
---------------------------------------------------------------------------------------------------
function Showdown.SelectMonster(option)
local monster = option:OptionValue()
log:Debugf("Selected %s", monster.name)
Showdown.monster = monster
for i, level in ipairs(monster.levels) do
local levelOptionButton = Showdown.levelOptionButtons[i]
levelOptionButton:Show()
levelOptionButton:SetOptionValue(level)
levelOptionButton:SetText(level.name)
end
for i = #monster.levels + 1, 7 do
local levelOptionButton = Showdown.levelOptionButtons[i]
levelOptionButton:SetText(nil)
levelOptionButton:SetOptionValue(nil)
levelOptionButton:Hide()
end
option:Select()
Showdown.level = Showdown.levelOptionButtons[1]:OptionValue()
Showdown.levelOptionButtons[1]:Select()
end
-------------------------------------------------------------------------------------------------
function Showdown.Setup(monsterOrName, levelOrName)
if Showdown.settingUp then
log:Errorf("Showdown setup currently in progress, please wait until finished before setting up another hunt. If the showdown appears to be stuck, click 'Cleanup Showdown' from the 'Cleanup' menu to reset.")
return
end
local monster = nil
if type(monsterOrName) == "string" then
monster = Showdown.monstersByName[monsterOrName]
assert(Check(monster, "Unknown monster %s", monsterOrName))
else
monster = monsterOrName
end
--[[
Monster.SetMoods(monster.moods or {})
Monster.SetInjuries(monster.injuries or {})
]]--
local level = nil
if type(levelOrName) == "string" then
level = Showdown.levelsByMonsterLevelName[monster.name..levelOrName]
assert(Check(level, "Unknown level %s for monster %s", levelOrName, monster.name))
else
level = levelOrName
end
--Assign monster's stat-altering Moods & Persistent Injuries
-- local replacedMoods = Util.ReplaceStarWithLevel(monster.moods, level) or {}
Monster.SetMoods(Showdown.ReplaceStarWithLevel(monster.moods or {}, level.level or 0))
-- local replacedInjuries = Util.ReplaceStarWithLevel(monster.injuries, level) or {}
Monster.SetInjuries(Showdown.ReplaceStarWithLevel(monster.injuries or {}, level.level or 0))
local showdownSetup = level.showdown
assert(Check(showdownSetup, "No showdown setup for %s, %s", monster.name, level.name))
local blocking = Showdown.CleanInternal()
if #blocking > 0 then
log:Broadcastf("Something is blocking showdown setup. Please move the highlighted objects out of the way and try again.")
Util.HighlightAll(blocking)
return
end
log:Printf("Setting up showdown against %s %s...", monster.name, level.name)
Showdown.settingUp = true
-----------------------------------------------------------------------------------------------
-- Rules
local rules = showdownSetup.rules or monster.rules
Rules.SpawnRules(rules[1], rules[2])
-----------------------------------------------------------------------------------------------
-- Resources
local monsterResourcesLocation = Location.Get("Monster Resources")
if not monster.resourcesDeck then
monsterResourcesLocation:BoxClean({ types = { "Monster Resources" } })
else
local monsterResourcesAlreadyExist = false
local hits = monsterResourcesLocation:RayCast()
for _, hit in ipairs(hits) do
local obj = hit.hit_object
if obj.getGMNotes() == "Monster Resources" then
if obj.getName() == monster.resourcesDeck then
log:Debugf("Found monster resources: %s (%s), skipping spawn", obj.getName(), obj.getGUID())
monsterResourcesAlreadyExist = true
break
else
log:Debugf("Deleting old monster resources deck %s (%s)", obj.getName(), obj.getGUID())
obj.destruct()
end
end
end
if not monsterResourcesAlreadyExist then
local monsterResourcesDeck = Archive.Take({ name = monster.resourcesDeck, type = "Monster Resources", location = monsterResourcesLocation, rotation = { x = 0, y = 180, z = 180 }})
monsterResourcesDeck.shuffle()
end
end
-----------------------------------------------------------------------------------------------
-- AI
Archive.Take({ name = monster.info, type = "AI", location = "Monster Info" })
Archive.Take({ name = monster.basicAction, type = "AI", location = "Basic Action" })
local basicAiDeck = Container(Archive.Take({ name = monster.basicAiDeck, type = "AI", location = "Unused Basic AI", rotation = { x = 0, y = 180, z = 180 } }))
basicAiDeck:Shuffle()
local advancedAiDeck
if monster.advancedAiDeck then
advancedAiDeck = Container(Archive.Take({ name = monster.advancedAiDeck, type = "AI", location = "Unused Advanced AI", rotation = { x = 0, y = 180, z = 180 } }))
advancedAiDeck:Shuffle()
end
local legendaryAiDeck
if monster.legendaryAiDeck then
legendaryAiDeck = Container(Archive.Take({ name = monster.legendaryAiDeck, type = "AI", location = "Unused Legendary AI", rotation = { x = 0, y = 180, z = 180 } }))
legendaryAiDeck:Shuffle()
end
local specialAiDeck = Container(Archive.Take({ name = monster.specialAiDeck, type = "AI", location = "Unused Special AI", rotation = { x = 0, y = 180, z = 180 } }))
local miscAiDeck = Container(Archive.Take({ name = "Misc AI", type = "AI", location = "Unused Misc AI", rotation = { x = 0, y = 180, z = 180 } }))
local basicAiDeck2
local advancedAiDeck2
local legendaryAiDeck2
if monster.twoAiDecks then
basicAiDeck2 = Container(Archive.Take({ name = monster.basicAiDeck2, type = "AI", location = "Unused Basic AI 2", rotation = { x = 0, y = 180, z = 180 } })) --Second AI Deck
basicAiDeck2:Shuffle()
if monster.advancedAiDeck2 then
advancedAiDeck2 = Container(Archive.Take({ name = monster.advancedAiDeck2, type = "AI", location = "Unused Advanced AI 2", rotation = { x = 0, y = 180, z = 180 } })) --Second Ai Deck
advancedAiDeck2:Shuffle()
end
if monster.legendaryAiDeck2 then
legendaryAiDeck2 = Container(Archive.Take({ name = monster.legendaryAiDeck2, type = "AI", location = "Unused Legendary AI 2", rotation = { x = 0, y = 180, z = 180 } })) --Second Ai Deck
legendaryAiDeck2:Shuffle()
end
end
local nextStartingLocation = 1
local function NextStartingLocation()
if nextStartingLocation <= Showdown.MAX_TRAITS_MOODS then
local location = "Trait/Mood "..nextStartingLocation
nextStartingLocation = nextStartingLocation + 1
return location
end
end
local function AddStartingCard(card, deck)
local location = NextStartingLocation()
if not location then
return log:Errorf("Not enough trait/mood slots for starting card %s from deck %s; please place this card manually", card, deck)
end
return deck:Take({ name = card, type = "AI", location = location })
end
-- starting cards
if showdownSetup.starting then
for _, card in ipairs(showdownSetup.starting.basic or {}) do
AddStartingCard(card, basicAiDeck)
end
for _, card in ipairs(showdownSetup.starting.advanced or {}) do
AddStartingCard(card, advancedAiDeck)
end
for _, card in ipairs(showdownSetup.starting.legendary or {}) do
AddStartingCard(card, legendaryAiDeck)
end
for _, card in ipairs(showdownSetup.starting.special or {}) do
AddStartingCard(card, specialAiDeck)
end
for _, card in ipairs(showdownSetup.starting.misc or {}) do
AddStartingCard(card, miscAiDeck)
end
-- special case life to the wound stack
if showdownSetup.life then
miscAiDeck:Take({ name = "Life", type = "AI", location = "Wound Stack" })
end
if monster.twoAiDecks then
for _, card in ipairs(showdownSetup.starting.basic2 or {}) do
AddStartingCard(card, basicAiDeck2)
end
for _, card in ipairs(showdownSetup.starting.advanced2 or {}) do
AddStartingCard(card, advancedAiDeck2)
end
for _, card in ipairs(showdownSetup.starting.legendary2 or {}) do
AddStartingCard(card, legendaryAiDeck2)
end
end
end
-- survivor statuses
if showdownSetup.survivorStatus then
for i = 1, 4 do
log:Debugf("Spawning survivor %d status %s", i, showdownSetup.survivorStatus[i])
specialAiDeck:Take({ name = showdownSetup.survivorStatus[i], type = "AI", location = "Player "..i.." Status" })
end
end
if showdownSetup.survivorStatus2 then --Added because of Regal Worm
for i = 1, 4 do
log:Debugf("Spawning survivor %d status %s", i, showdownSetup.survivorStatus2[i])
specialAiDeck:Take({ name = showdownSetup.survivorStatus2[i], type = "AI", location = "Player "..i.." Status" })
end
end
-- build AI deck
local sources = {}
local sources2 = {}
for _, card in ipairs(showdownSetup.customBasic or {}) do
table.insert(sources, Archive.ContainerSource(basicAiDeck, card, "AI" ))
end
for _, card in ipairs(showdownSetup.customAdvanced or {}) do
table.insert(sources, Archive.ContainerSource(advancedAiDeck, card, "AI" ))
end
for _, card in ipairs(showdownSetup.customLegendary or {}) do
table.insert(sources, Archive.ContainerSource(legendaryAiDeck, card, "AI" ))
end
for _, card in ipairs(showdownSetup.customSpecial or {}) do
table.insert(sources, Archive.ContainerSource(specialAiDeck, card, "AI" ))
end
for _, card in ipairs(showdownSetup.miscAI or {}) do
table.insert(sources, Archive.ContainerSource(miscAiDeck, card, "AI"))
end
for i = 1, (showdownSetup.basic or 0) do
table.insert(sources, Archive.ContainerSource(basicAiDeck))
end
for i = 1, (showdownSetup.advanced or 0) do
table.insert(sources, Archive.ContainerSource(advancedAiDeck))
end
for i = 1, (showdownSetup.legendary or 0) do
table.insert(sources, Archive.ContainerSource(legendaryAiDeck))
end
local aiDeck2
if monster.twoAiDecks then
for _, card in ipairs(showdownSetup.customBasic2 or {}) do --Second AI Deck
table.insert(sources2, Archive.ContainerSource(basicAiDeck2, card, "AI" ))
end
for _, card in ipairs(showdownSetup.customAdvanced2 or {}) do --Second AI Deck
table.insert(sources2, Archive.ContainerSource(advancedAiDeck2, card, "AI" ))
end
for _, card in ipairs(showdownSetup.customLegendary2 or {}) do --Second AI Deck
table.insert(sources2, Archive.ContainerSource(legendaryAiDeck2, card, "AI" ))
end
for i = 1, (showdownSetup.basic2 or 0) do --Second AI Deck
table.insert(sources2, Archive.ContainerSource(basicAiDeck2))
end
for i = 1, (showdownSetup.advanced2 or 0) do --Second AI Deck
table.insert(sources2, Archive.ContainerSource(advancedAiDeck2))
end
for i = 1, (showdownSetup.legendary2 or 0) do --Second AI Deck
table.insert(sources2, Archive.ContainerSource(legendaryAiDeck2))
end
aiDeck2 = Archive.CreateDeckFromSources({ sources = sources2, location = "AI 2", name = monster.name.." AI Two", type = "AI", rotation = { x = 0, y = 180, z = 180 } }) --Second AI Deck
aiDeck2:Shuffle()
end
local aiDeck = Archive.CreateDeckFromSources({ sources = sources, location = "AI", name = monster.name.." AI", type = "AI", rotation = { x = 0, y = 180, z = 180 } })
aiDeck:Shuffle()
if showdownSetup.topAiCards or showdownSetup.bottomAiCards then
aiDeck:OrderDeck(showdownSetup.topAiCards, showdownSetup.bottomAiCards)
-- TODO: hack to work around broken order deck...instead spawn the cards in a specific order
aiDeck.object.setPositionSmooth(Location.Get("AI"):Center(), false, true)
aiDeck.object.setRotation({ x = 0, y = 180, z = 180 })
end
-----------------------------------------------------------------------------------------------
-- Hit Locations
local sources = {
Archive.ArchiveSource(monster.hitLocationsDeck, "Hit Locations")
}
for _, card in ipairs(showdownSetup.miscHitLocations or {}) do
table.insert(sources, Archive.ContainerSource(miscAiDeck, card, "Hit Locations"))
end
local hitLocationsDeck = Archive.CreateDeckFromSources({ sources = sources, location = "HL", name = monster.hitLocationsDeck, type = "Hit Locations", rotation = { x = 0, y = 180, z = 180 } })
hitLocationsDeck:Shuffle()
if showdownSetup.removeHitLocations then
log:Debugf("Taking %s from hit location deck and moving to Unused HL", showdownSetup.removeHitLocations)
local sources = {}
for _, hl in ipairs(showdownSetup.removeHitLocations) do
table.insert(sources, Archive.ContainerSource(hitLocationsDeck, hl, "Hit Locations"))
end
Archive.CreateDeckFromSources({ sources = sources, location = "Unused HL", name = "Unused Hit Locations", type = "Hit Locations" })
end
if showdownSetup.topHitLocationCards then
log:Debugf("Putting %s on top of hit location deck", showdownSetup.topHitLocationCards)
hitLocationsDeck:OrderDeck(showdownSetup.topHitLocationCards)
end
-----------------------------------------------------------------------------------------------
-- Terrain
local nextTerrainLocation = 1
local terrainOverflowCol = 2
local terrainOverflowRow = 2
local NextTerrainLocation = function()
if nextTerrainLocation <= Showdown.MAX_TERRAIN then
terrainLocation = "Terrain "..nextTerrainLocation
miscLocation = "Misc "..nextTerrainLocation
nextTerrainLocation = nextTerrainLocation + 1
return terrainLocation, miscLocation
else
log:Errorf("Not enough terrain spaces!")
location = string.format("(%d, %d)", terrainOverflowCol, terrainOverflowRow)
terrainOverflowRow = terrainOverflowRow + 3
if terrainOverflowRow >= 16 then
terrainOverflowRow = 2
terrainOverflowCol = terrainOverflowCol + 2
end
return location, location
end
end
local terrainDeck = Container(Archive.Take({ name = "Terrain", type = "Terrain", location = "Terrain", rotation = { x = 0, y = 180, z = 180 } }))
local terrainTilesBag = Container(Archive.Take({ name = "Terrain Tiles", type = "Terrain Tiles", location = "Terrain Tiles" }))
terrainDeck:Shuffle()
local terrainTileObjects = {}
local SpawnTerrainTile = function(terrainTile, location, rotation)
if not terrainTileObjects[terrainTile] then
log:Debugf("Spawning new terrain tile %s to %s", terrainTile, location)
terrainTileObjects[terrainTile] = terrainTilesBag:Take({ name = terrainTile, type = "Terrain Tiles", location = location, rotation = rotation })
else
log:Debugf("Cloning terrain tile %s to %s", terrainTile, location)
local clonePosition = Location.Get(location):Center()
clonePosition.y = clonePosition.y + 2
local clonedObject = terrainTileObjects[terrainTile].clone({ position = clonePosition })
if rotation then
Wait.frames(function() clonedObject.setRotation(rotation) end, 2)
end
end
end
local SpawnTerrain = function(terrain, locations, rotations)
local terrainLocation, miscLocation = NextTerrainLocation()
local terrainCard = terrainDeck:Take({ name = terrain, type = "Terrain", location = terrainLocation })
if not terrainCard then
return log:Errorf("Couldn't find terrain '%s'", terrain)
end
local descriptor
if terrain then
descriptor = locations and "fixed" or "unfixed"
else
descriptor = "random"
end
log:Debugf("Spawned %s %s terrain at %s", descriptor, terrainCard.getName(), location)
local data = Terrain.Data(terrainCard.getName())
if locations then
for i, location in ipairs(locations) do
SpawnTerrainTile(data.terrainTile, location, rotations[i])
end
else
local count = data.count
if count == "*" then
count = level.level
end
for i = 1, count do
SpawnTerrainTile(data.terrainTile, terrainLocation)
end
end
if data.miscObject then
log:Debugf("Spawning misc object %s/%s at %s", data.miscObject.name, data.miscObject.type, miscLocation)
Archive.Take({ name = data.miscObject.name, type = data.miscObject.type, location = miscLocation })
end
for _, startingCard in ipairs(data.starting or {}) do
local startingLocation = NextStartingLocation()
log:Debugf("Spawning starting card %s at %s", startingCard, startingLocation)
Archive.Take({ name = startingCard, type = "AI", location = startingLocation })
end
end
-- special terrain
for _, terrainSetup in ipairs(showdownSetup.specialTerrain or monster.specialTerrain or {}) do
if terrainSetup.terrain then
terrainLocation, miscLocation = NextTerrainLocation()
log:Debugf("Spawning special terrain card %s at %s", terrainSetup.terrain, terrainLocation)
Archive.Take({ name = terrainSetup.terrain, type = "Terrain", location = terrainLocation })
end
if terrainSetup.terrainTile then
log:Debugf("Spawning special terrain tile %s at %s", terrainSetup.terrainTile, terrainSetup.position)
Archive.Take({ name = terrainSetup.terrainTile, type = "Terrain Tiles", location = terrainSetup.position, rotation = terrainSetup.rotation })
end
if terrainSetup.miscObject then
log:Debugf("Spawning special misc object %s/%s at %s", terrainSetup.miscObject.name, terrainSetup.miscObject.type, miscLocation)
Archive.Take({ name = terrainSetup.miscObject.name, type = terrainSetup.miscObject.type, location = miscLocation })
end
end
-- fixed terrain
for _, terrainSetup in ipairs(showdownSetup.fixedTerrain or monster.fixedTerrain or {}) do
SpawnTerrain(terrainSetup.terrain, terrainSetup.positions, terrainSetup.rotations)
end
-- unfixed terrain
for _, terrain in ipairs(showdownSetup.unfixedTerrain or monster.unfixedTerrain or {}) do
SpawnTerrain(terrain, nil, nil)
end
-- random terrain
for i = 1, (showdownSetup.randomTerrain or monster.randomTerrain or 0) do
SpawnTerrain(nil, nil, nil)
end
-----------------------------------------------------------------------------------------------
-- misc object
local miscObject = showdownSetup.miscObject or monster.miscObject
if miscObject then
log:Debugf("Spawning misc object %s", miscObject.name)
Archive.Take({ name = miscObject.name, type = miscObject.type, location = "Misc" })
end
-----------------------------------------------------------------------------------------------
-- monster
if showdownSetup.figurines then
for _, figurine in ipairs(showdownSetup.figurines) do
Archive.Take({
archive = monster.name.." Archive",
name = figurine.name,
type = "Monster Figurine",
location = figurine.position, --or monster.position,
rotation = figurine.rotation, --or monster.rotation,
})
end
else
Archive.Take({
name = monster.name,
type = "Monster Figurine",
location = showdownSetup.position or monster.position,
rotation = showdownSetup.rotation or monster.rotation,
})
end
-- stats
for _, stat in ipairs({ "movement", "toughness", "speed", "damage", "accuracy", "evasion", "luck" }) do
Monster.SetStat(stat, showdownSetup[stat] or 0)
end
-----------------------------------------------------------------------------------------------
-- players
local playerLocations = showdownSetup.playerPositions or monster.playerPositions
local playerRotations = showdownSetup.playerRotations or monster.playerRotations or {{ x = 0, y = 0, z = 0 }, { x = 0, y = 0, z = 0 }, { x = 0, y = 0, z = 0 }, { x = 0, y = 0, z = 0 } }
for i, player in ipairs(Player.Players()) do
local figurine = player:FigurineObject()
if figurine then
figurine.setPositionSmooth(Location.Get(playerLocations[i]):Center(), false, true)
figurine.setRotation(playerRotations[i])
end
end
-----------------------------------------------------------------------------------------------
Archive.Clean()
Showdown.settingUp = false
Wait.frames(function() log:Printf("Showdown ready. Adjust your survivor/terrain placement and prepare for death.") end, 120)
--Location.Get("(11.5, 8.5)"):LookAt({ distance = 90 })
EventManager.FireEvent(EventManager.ON_SHOWDOWN_STARTED)
end
---------------------------------------------------------------------------------------------------
function Showdown.CleanInternal()
Archive.Clean()
local types = {
"AI",
"Hit Locations",
"Terrain",
"Terrain Tiles",
"Monster Figurine",
"Minion Figurine",
"Tokens",
"Archive Bag",
}
local debug = false
local blocking = {}
blocking = Util.ConcatArrays(blocking, Location.Get("Terrain Board"):BoxClean({ types = types, debug = debug }))
blocking = Util.ConcatArrays(blocking, Location.Get("Monster Board"):BoxClean({ tags = { "Deck" }, types = types, debug = debug }))
-- don't block for the huge showdown board clean
Location.Get("Showdown Board"):BoxClean({ types = types, debug = debug })
return blocking
end
---------------------------------------------------------------------------------------------------
function Showdown.Clean()
Showdown.settingUp = false
Showdown.CleanInternal()
Monster.SetMoods({})
Monster.SetInjuries({})
for _, stat in ipairs({ "movement", "toughness", "speed", "damage", "accuracy", "evasion", "luck" }) do
Monster.SetStat(stat, 0)
end
log:Printf("Cleaned up showdown.")
EventManager.FireEvent(EventManager.ON_SHOWDOWN_ENDED)
end
-------------------------------------------------------------------------------------------------
function Showdown.OnObjectPickUp(_, object)
local type = object.getGMNotes()
if type == "Player Figurine" then
log:Debugf("Player figurine [%s] %s picked up", object.getGUID(), object.getName())
local player = Player.ForFigurine(object)
if not player or not player:SurvivorSheet() then
return
end
local color = object.getColorTint()
color[4] = 0.7
Showdown.overlay:Show(object.getPosition(), 1, player:SurvivorSheet():Survivor():ModifiedMovement(), color)
log(player:SurvivorSheet():Survivor():ModifiedMovement())
elseif type == "Minion Figurine" then
log:Debugf("Minion figurine [%s] %s picked up", object.getGUID(), object.getName())
local minion = Showdown.minionsByName[object.getName()]
if not minion then
return log:Debugf("Unknown minion %s", object.getName())
end
Showdown.overlay:Show(object.getPosition(), 1, minion.movement, { r = 0.0, g = 0.0, b = 0.0, a = 0.9 })
elseif type == "Monster Figurine" then
log:Debugf("Monster figurine [%s] %s picked up", object.getGUID(), object.getName())
local size = Showdown.sizesByMonsterName[object.getName()]
if not size then
log:Debugf("Monster %s has no registered size", object.getName())
return
end
if Monster.Movement() == 0 then
log:Debugf("Movement zero")
return
end
Showdown.overlay:Show(object.getPosition(), size.x, Monster.Movement(), { r = 0.0, g = 0.0, b = 0.0, a = 0.9 })
end
end
---------------------------------------------------------------------------------------------------
function Showdown.OnObjectDrop(_, object)
local type = object.getGMNotes()
if type == "Player Figurine" then
log:Debugf("Player figurine [%s] %s picked up", object.getGUID(), object.getName())
local player = Player.ForFigurine(object)
if player then
Showdown.overlay:Hide()
end
object.setPositionSmooth(Showdown.grid:Snap(object, { x = 1, y = 1 }), false, true)
elseif type == "Monster Figurine" then
log:Debugf("Monster figurine [%s] %s dropped", object.getGUID(), object.getName())
local size = Showdown.sizesByMonsterName[object.getName()]
if not size then
size = { x = 1, y = 1 }
--return log:Debugf("Monster %s has no registered size", object.getName())
end
object.setPositionSmooth(Showdown.grid:Snap(object, size), false, true)
Showdown.overlay:Hide()
elseif type == "Minion Figurine" then
log:Debugf("Minion figurine [%s] %s dropped", object.getGUID(), object.getName())
local minion = Showdown.minionsByName[object.getName()]
if not minion then
return log:Debugf("Unrecognized minion: %s", object.getName())
end
object.setPositionSmooth(Showdown.grid:Snap(object, minion.size), false, true)
Showdown.overlay:Hide()
elseif type == "Terrain Tiles" then
log:Debugf("Terrain tile [%s] %s dropped", object.getGUID(), object.getName())
local size = Terrain.TileSize(object.getName())
if not size then
return log:Debugf("Terrain tile %s has no registered size", object.getName())
end
object.setPositionSmooth(Showdown.grid:Snap(object, size), false, true)
end
end
---------------------------------------------------------------------------------------------------
function Showdown.ReplaceStarWithLevel(cards, level)
assert(Check.Num(level))
local copy = {}
for k, v in pairs(cards) do --Error Message here
local innerCopy = {}
for ik, iv in pairs(v) do
if (not Check.Num(iv)) then
innerCopy[ik] = level or 0
else
innerCopy[ik] = iv or 0
end
end
copy[k] = innerCopy
end
return copy
end
---------------------------------------------------------------------------------------------------
return {
Init = Showdown.Init,
Setup = Showdown.Setup,
ShowUi = Showdown.ShowUi,
HideUi = Showdown.HideUi,
IsUiOpen = Showdown.IsUiOpen,
Clean = Showdown.Clean,
}