forked from jordanmchavez/kdm-tts
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Survivor.ttslua
1747 lines (1459 loc) · 72 KB
/
Survivor.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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local Archive = require("Kdm/Archive")
local Check = require("Kdm/Util/Check")
local EventManager = require("Kdm/Util/EventManager")
local Location = require("Kdm/Location")
local log = require("Kdm/Log").ForModule("Survivor")
local MessageBox = require("Kdm/MessageBox")
local NamedObject = require("Kdm/NamedObject")
local Ui = require("Kdm/Ui")
local Util = require("Kdm/Util/Util")
-------------------------------------------------------------------------------------------------
local Survivor = {}
local SurvivorClass = {}
SurvivorClass.__index = SurvivorClass
function SurvivorClass.Is(x) return getmetatable(x) == SurvivorClass end
local SurvivorSheetClass = {}
SurvivorSheetClass.__index = SurvivorSheetClass
function SurvivorSheetClass.Is(x) return getmetatable(x) == SurvivorSheetClass end
local SurvivorBoxClass = {}
SurvivorBoxClass.__index = SurvivorBoxClass
function SurvivorBoxClass.Is(x) return getmetatable(x) == SurvivorBoxClass end
-------------------------------------------------------------------------------------------------
Survivor.MODIFIED_STATS = {
movement = "modifiedMovement",
speed = "modifiedSpeed",
accuracy = "modifiedAccuracy",
strength = "modifiedStrength",
evasion = "modifiedEvasion",
luck = "modifiedLuck",
}
Survivor.CHECKBOX_SEQUENCE_MAXES = {
["huntXp"] = 16,
["courage"] = 9,
["understanding"] = 9,
["weaponProficiencyRank"] = 8,
}
Survivor.ALLOWED_CARD_TYPES = {
["Secret Fighting Arts"] = true,
["Fighting Arts"] = true,
["Disorders"] = true,
["Abilities"] = true,
["Severe Injuries"] = true,
["Weapon Proficiencies"] = true,
["Player Figurine"] = true,
}
Survivor.MARKER_EMPTY = "MarkerEmpty"
Survivor.MARKER_YELLOW_STAR = "MarkerYellowStar"
Survivor.MARKER_GREEN_TRIANGLE = "MarkerGreenTriangle"
Survivor.MARKER_BLUE_SQUARE = "MarkerBlueSquare"
Survivor.MARKER_RED_CIRCLE = "MarkerRedCircle"
Survivor.MARKER_PURPLE_DIAMOND = "MarkerPurpleDiamond"
Survivor.NEXT_MARKER = {
[Survivor.MARKER_EMPTY] = Survivor.MARKER_YELLOW_STAR,
[Survivor.MARKER_YELLOW_STAR] = Survivor.MARKER_GREEN_TRIANGLE,
[Survivor.MARKER_GREEN_TRIANGLE] = Survivor.MARKER_BLUE_SQUARE,
[Survivor.MARKER_BLUE_SQUARE] = Survivor.MARKER_RED_CIRCLE,
[Survivor.MARKER_RED_CIRCLE] = Survivor.MARKER_PURPLE_DIAMOND,
[Survivor.MARKER_PURPLE_DIAMOND] = Survivor.MARKER_EMPTY,
}
Survivor.PREV_MARKER = {
[Survivor.MARKER_EMPTY] = Survivor.MARKER_PURPLE_DIAMOND,
[Survivor.MARKER_PURPLE_DIAMOND] = Survivor.MARKER_RED_CIRCLE,
[Survivor.MARKER_RED_CIRCLE] = Survivor.MARKER_BLUE_SQUARE,
[Survivor.MARKER_BLUE_SQUARE] = Survivor.MARKER_GREEN_TRIANGLE,
[Survivor.MARKER_GREEN_TRIANGLE] = Survivor.MARKER_YELLOW_STAR,
[Survivor.MARKER_YELLOW_STAR] = Survivor.MARKER_EMPTY,
}
Survivor.CUSTOM_ASSETS = {
{
name = "CheckBoxFilled",
url = "http://cloud-3.steamusercontent.com/ugc/1189461526463396506/7C8173ABCCC39B1FB8EB5FF188FAC0B5CF5A3F69/",
},
{
name = Survivor.MARKER_EMPTY,
url = "http://cloud-3.steamusercontent.com/ugc/1790722535857515064/9835893D92CE5B6B4C12FB4345E11184756578EE/",
},
{
name = Survivor.MARKER_YELLOW_STAR,
url = "http://cloud-3.steamusercontent.com/ugc/1790722535857515118/ADEC771A4BE98497CA7D73E8DAC54579ACF931C1/",
},
{
name = Survivor.MARKER_GREEN_TRIANGLE,
url = "http://cloud-3.steamusercontent.com/ugc/1790722535857515167/28CAFE231AB570185C9258E070DE650707EA6B79/",
},
{
name = Survivor.MARKER_BLUE_SQUARE,
url = "http://cloud-3.steamusercontent.com/ugc/1790722535857515209/10679F6E9244587F251864B6EED84FC9802FF93D/",
},
{
name = Survivor.MARKER_RED_CIRCLE,
url = "http://cloud-3.steamusercontent.com/ugc/1790722535857515252/2812E4FA09333A5D4DE768301D8F71291F0AD9FC/",
},
{
name = Survivor.MARKER_PURPLE_DIAMOND,
url = "http://cloud-3.steamusercontent.com/ugc/1790722535857515296/0EC80660E45B8AAFB6B66F7CFD189DE0A39EC8D1/",
},
{
name = "IconFightingArt",
url = "http://cloud-3.steamusercontent.com/ugc/1790722535857515347/18624B79B3DEB547755AE3EECFC219A4713F53B4/",
},
{
name = "IconDisorder",
url = "http://cloud-3.steamusercontent.com/ugc/1790722535857515397/427B848EBAC81BC10C1610E90DC02F9654EEE994/",
},
{
name = "IconAbility",
url = "http://cloud-3.steamusercontent.com/ugc/1790722535857515440/7E68C46CB68D455D29347172389E8F3BB0085BC0/",
},
{
name = "IconSevereInjury",
url = "http://cloud-3.steamusercontent.com/ugc/1790722535857515501/5DDEF532D75FEB02FC1DF5D924313E7A13ECE90A/",
},
}
-------------------------------------------------------------------------------------------------
function Survivor.Init(saveState)
Survivor.InitSaveState(saveState)
Survivor.InitUi()
Survivor.InitEvents()
end
---------------------------------------------------------------------------------------------------
function Survivor.InitSaveState(saveState)
Survivor.nextSurvivorId = 1
Survivor.survivors = {}
Survivor.survivorsById = {}
for _, survivorSaveState in ipairs(saveState.survivors or {}) do
local survivor = SurvivorClass.Create(survivorSaveState)
table.insert(Survivor.survivors, survivor)
Survivor.survivorsById[survivor.id] = survivor
Survivor.nextSurvivorId = Util.Max(Survivor.nextSurvivorId, survivor.id + 1)
end
Survivor.survivorBoxesBySurvivor = {}
Survivor.survivorBoxesByObject = {}
for _, survivorBoxSaveState in ipairs(saveState.survivorBoxes or {}) do
local survivor = Survivor.survivorsById[survivorBoxSaveState.survivorId]
if not survivor then
return log:Errorf("Survivor box %s was saved with non-existant survivor %d", survivorBoxSaveState.objectGuid, survivorBoxSaveState.survivorId)
end
local object = getObjectFromGUID(survivorBoxSaveState.objectGuid)
if not object then
return log:Errorf("Survivor %s had saved survivor box GUID %s, but that object doesn't exist. Try re-creating this survivor's sheet from the Survivor Board.", survivor:NameOrUnnamed(), survivorBoxSaveState.objectGuid)
end
local survivorBox = SurvivorBoxClass.Create(survivor, object)
Survivor.survivorBoxesBySurvivor[survivor] = survivorBox
Survivor.survivorBoxesByObject[object] = survivorBox
end
Survivor.survivorSheetsBySurvivor = {}
Survivor.survivorSheetsByObject = {}
for _, survivorSheetSaveState in ipairs(saveState.survivorSheets or {}) do
local survivor = Survivor.survivorsById[survivorSheetSaveState.survivorId]
if not survivor then
return log:Errorf("Survivor sheet %s was saved with non-existant survivor %d", survivorSheetSaveState.survivorSheetGuid, survivorSheetSaveState.survivorId)
end
local object = getObjectFromGUID(survivorSheetSaveState.objectGuid)
if not object then
return log:Errorf("Survivor %d had saved survivor sheet GUID %s, but that object doesn't exist. Try re-spawning this survivor's box from the survivor board.", survivor.id, survivorSheetSaveState.objectGuid)
end
local survivorSheet = SurvivorSheetClass.Create(survivor, object)
Survivor.survivorSheetsBySurvivor[survivor] = survivorSheet
Survivor.survivorSheetsByObject[object] = survivorSheet
end
Survivor.newSurvivorBonuses = saveState.newSurvivorBonuses
Survivor.page = saveState.page or 1
Survivor.hideLiving = saveState.hideLiving
Survivor.hideDead = saveState.hideDead
Survivor.hideSkipNextHunt = saveState.hideSkipNextHunt
Survivor.hideRetired = saveState.hideRetired
Survivor.hideYellowStar = saveState.hideYellowStar
Survivor.hideGreenTriangle = saveState.hideGreenTriangle
Survivor.hideBlueSquare = saveState.hideBlueSquare
Survivor.hideRedCircle = saveState.hideRedCircle
Survivor.hidePurpleDiamond = saveState.hidePurpleDiamond
end
---------------------------------------------------------------------------------------------------
function Survivor.InitUi()
Survivor.boardObject = NamedObject.Get("Survivor Board")
local ui = Ui.Create3d("survivorBoard", Survivor.boardObject, 0.6)
Survivor.ui = ui
Survivor.livingSurvivorsText = ui:Text({ id = "livingSurvivors", topLeft = { x = 5.284441, y = -7.640587 }, bottomRight = { x = 5.023291, y = -7.052870 }, fontSize = 220, alignment = "MiddleCenter" })
Survivor.deadSurvivorsText = ui:Text({ id = "deadSurvivors", topLeft = { x = 4.319638, y = -7.642143 }, bottomRight = { x = 4.065874, y = -7.059141 }, fontSize = 220, alignment = "MiddleCenter" })
Survivor.newSurvivorBonusesInput = ui:Input({ id = "newSurvivorBonuses", topLeft = { x = 3.778204, y = -7.33247 }, bottomRight = { x = 1.031039, y = -7.164494 }, fontSize = 80, alignment = "LowerLeft", text = Survivor.newSurvivorBonuses, onValueChanged = function(value)
Survivor.newSurvivorBonuses = value
Survivor.newSurvivorBonusesInput:SetText(value)
end })
Survivor.newSurvivorButton = ui:Button({ id = "newSurvivor", topLeft = { x = 0.756925, y = -7.658921 }, bottomRight = { x = -0.617333,y = -7.045891 }, onClick = function()
local id = Survivor.nextSurvivorId
Survivor.nextSurvivorId = Survivor.nextSurvivorId + 1
local survivor = SurvivorClass.Create({ id = id })
table.insert(Survivor.survivors, survivor)
Survivor.survivorsById[survivor.id] = survivor
Survivor.UpdateLivingDeadCounts()
Survivor.SetPageAndRefresh(Survivor.MaxPage())
end })
Survivor.clearSkipNextHuntButton = ui:Button({ id = "clearSkipNextHunt", topLeft = { x = -0.897427, y = -7.661951 }, bottomRight = { x = -2.819457, y = -7.049488 }, onClick = function()
for _, survivor in ipairs(Survivor.survivors) do
if survivor:SkipNextHunt() then
survivor:SetSkipNextHunt(false)
log:Printf("Cleared 'Skip Next Hunt' on %s", survivor:NameOrUnnamed())
end
end
end })
Survivor.previousPageButton = ui:Button({ id = "previousPage", topLeft = { x = -6.576738, y = -7.658603 }, bottomRight = { x = -6.846550, y = -7.060284 }, onClick = function()
Survivor.SetPageAndRefresh(Survivor.page - 1)
end })
Survivor.pageText = ui:Text({ id = "page", topLeft = { x = -6.846550, y = -7.658603 }, bottomRight = { x = -7.783247, y = -7.040886 }, fontSize = 160, alignment = "MiddleCenter" })
Survivor.nextPageButton = ui:Button({ id = "nextPage", topLeft = { x = -7.783247, y = -7.662317 }, bottomRight = { x = -8.049563, y = -7.040886 }, onClick = function()
Survivor.SetPageAndRefresh(Survivor.page + 1)
end })
local width = 7.051640 - 7.260710
local top = 7.231073
local height = 7.696268 - top
for suffix, left in pairs({
Living = 7.260710,
Dead = 6.436522,
SkipNextHunt = 5.644810,
Retired = 4.296477,
YellowStar = 3.390988,
GreenTriangle = 2.677092,
BlueSquare = 1.925451,
RedCircle = 1.205827,
PurpleDiamond = 0.489192,
}) do
local attr = "hide"..suffix
local cb = attr.."CheckBox"
Survivor[cb] = ui:CheckBox({ id = attr, topLeft = { x = left, y = top }, bottomRight = { x = left + width, y = top + height }, checked = Survivor[attr], onClick = function()
Survivor[attr] = not Survivor[attr]
Survivor[cb]:Check(Survivor[attr])
Survivor.SetPageAndRefresh(1)
end })
end
Survivor.cells = {}
for i = 1, 20 do
local cell = {
object = NamedObject.Get("Survivor Board Cell "..i),
ordinal = i,
}
Survivor.InitSurvivorCellUi(cell)
Survivor.cells[i] = cell
end
-- total hack because the board is so large and text get weird
Survivor.livingSurvivorsText.attributes.scale = "0.1 0.2246487"
Survivor.deadSurvivorsText.attributes.scale = "0.1 0.2246487"
Survivor.newSurvivorBonusesInput.attributes.scale = "0.1 0.2246487"
Survivor.pageText.attributes.scale = "0.1 0.2246487"
Survivor.boardObject.UI.setCustomAssets(Survivor.CUSTOM_ASSETS)
Survivor.ui:ApplyToObject()
end
---------------------------------------------------------------------------------------------------
function Survivor.InitSurvivorCellUi(cell)
local ui = Ui.Create3d("survivorCell"..cell.ordinal, cell.object, 0.11)
cell.ui = ui
cell.survivalCounter = ui:Counter({ id = "survival", topLeft = { x = 0.488679, y = -0.960464 }, bottomRight = { x = 0.294088, y = -0.766285 }, fontSize = 140, text = 0, onValueChanged = function(delta)
cell.survivor:SetSurvival(cell.survivor:Survival() + delta)
end })
cell.insanityCounter = ui:Counter({ id = "insanity", topLeft = { x = -0.293847, y = -0.964120 }, bottomRight = { x = -0.489293, y = -0.764670 }, fontSize = 140, text = 0, onValueChanged = function(delta)
cell.survivor:SetInsanity(cell.survivor:Insanity() + delta)
end })
for stat, data in pairs({
["movement"] = { topLeft = { x = 0.237461, y = -0.963491 }, bottomRight = { x = 0.098091, y = -0.824870 } },
["speed"] = { topLeft = { x = 0.070339, y = -0.964019 }, bottomRight = { x = -0.069972, y = -0.823512 } },
["accuracy"] = { topLeft = { x = -0.095285, y = -0.960736 }, bottomRight = { x = -0.235804, y = -0.823606 } },
["strength"] = { topLeft = { x = 0.238673, y = -0.745258 }, bottomRight = { x = 0.097487, y = -0.610387 } },
["evasion"] = { topLeft = { x = 0.068523, y = -0.746108 }, bottomRight = { x = -0.068570, y = -0.609721 } },
["luck"] = { topLeft = { x = -0.097451, y = -0.747023 }, bottomRight = { x = -0.236673, y = -0.608649 } },
}) do
cell[stat.."Counter"] = ui:Counter({ id = stat, topLeft = data.topLeft, bottomRight = data.bottomRight, fontSize = 90, onValueChanged = function(delta)
cell.survivor:SetModified(stat, cell.survivor[stat] + delta)
end })
end
cell.markerButton = ui:Button({ id = "marker", topLeft = { x = 0.490351, y = -0.667075 }, bottomRight = { x = 0.295514, y = -0.556129 }, colors = Ui.IMAGE_COLORS, onClick = function(button)
local value = cell.survivor:Marker() or Survivor.MARKER_EMPTY
if button == Ui.LEFT_MOUSE_BUTTON then
value = Survivor.NEXT_MARKER[value]
else
value = Survivor.PREV_MARKER[value]
end
cell.survivor:SetMarker(value)
end })
cell.nameInput = ui:Input({ id = "name", topLeft = { x = 0.374686, y = -0.555475 }, bottomRight = { x = -0.492332, y = -0.471773 }, fontSize = 60, placeholder = "Unnamed Survivor", onValueChanged = function(value)
cell.survivor:SetName(value)
end })
local smallCheckBoxSize = 0.0352955
local bigCheckBoxSize = 0.04039
for stat, topLeft in pairs({
["male"] = { x = 0.488708, y = -0.450130 },
["female"] = { x = 0.391058, y = -0.451183 },
["reroll"] = { x = 0.301735, y = -0.448962 },
["skipNextHunt"] = { x = 0.097530, y = -0.451439 },
["retired"] = { x = -0.156637, y = -0.451290 },
["dead"] = { x = -0.334745, y = -0.450177 },
}) do
cell[stat.."CheckBox"] = ui:CheckBox({ id = stat, topLeft = topLeft, bottomRight = { x = topLeft.x - smallCheckBoxSize, y = topLeft.y + smallCheckBoxSize }, onClick = function()
cell.survivor:SetBool(stat, not cell.survivor[stat])
end })
end
for stat, seq in pairs({
["huntXp"] = {
{ topLeft = { x = 0.312961, y = -0.358888 }, big = false },
{ topLeft = { x = 0.260530, y = -0.362141 }, big = true },
{ topLeft = { x = 0.205214, y = -0.358888 }, big = false },
{ topLeft = { x = 0.158189, y = -0.358888 }, big = false },
{ topLeft = { x = 0.111091, y = -0.358888 }, big = false },
{ topLeft = { x = 0.060947, y = -0.362141 }, big = true },
{ topLeft = { x = 0.004465, y = -0.358888 }, big = false },
{ topLeft = { x = -0.043838, y = -0.358888 }, big = false },
{ topLeft = { x = -0.089868, y = -0.358888 }, big = false },
{ topLeft = { x = -0.138367, y = -0.362141 }, big = true },
{ topLeft = { x = -0.195562, y = -0.358888 }, big = false },
{ topLeft = { x = -0.242767, y = -0.358888 }, big = false },
{ topLeft = { x = -0.290438, y = -0.358888 }, big = false },
{ topLeft = { x = -0.336773, y = -0.358888 }, big = false },
{ topLeft = { x = -0.386495, y = -0.362141 }, big = true },
{ topLeft = { x = -0.447584, y = -0.362141 }, big = true },
},
["courage"] = {
{ topLeft = { x = 0.489029, y = -0.207650 }, big = false },
{ topLeft = { x = 0.440906, y = -0.207650 }, big = false },
{ topLeft = { x = 0.391424, y = -0.209982 }, big = true },
{ topLeft = { x = 0.335180, y = -0.207650 }, big = false },
{ topLeft = { x = 0.287784, y = -0.207650 }, big = false },
{ topLeft = { x = 0.240546, y = -0.207650 }, big = false },
{ topLeft = { x = 0.193479, y = -0.207650 }, big = false },
{ topLeft = { x = 0.146031, y = -0.207650 }, big = false },
{ topLeft = { x = 0.096573, y = -0.209982 }, big = true },
},
["understanding"] = {
{ topLeft = { x = 0.489029 + -0.542864, y = -0.207650 }, big = false },
{ topLeft = { x = 0.440906 + -0.542864, y = -0.207650 }, big = false },
{ topLeft = { x = 0.391424 + -0.542864, y = -0.209982 }, big = true },
{ topLeft = { x = 0.335180 + -0.542864, y = -0.207650 }, big = false },
{ topLeft = { x = 0.287784 + -0.542864, y = -0.207650 }, big = false },
{ topLeft = { x = 0.240546 + -0.542864, y = -0.207650 }, big = false },
{ topLeft = { x = 0.193479 + -0.542864, y = -0.207650 }, big = false },
{ topLeft = { x = 0.146031 + -0.542864, y = -0.207650 }, big = false },
{ topLeft = { x = 0.096573 + -0.542864, y = -0.209982 }, big = true },
},
["weaponProficiencyRank"] = {
{ topLeft = { x = 0.488380, y = -0.056321 }, big = false },
{ topLeft = { x = 0.441384, y = -0.056321 }, big = false },
{ topLeft = { x = 0.391489, y = -0.060015 }, big = true },
{ topLeft = { x = 0.335152, y = -0.056321 }, big = false },
{ topLeft = { x = 0.288189, y = -0.056321 }, big = false },
{ topLeft = { x = 0.240844, y = -0.056321 }, big = false },
{ topLeft = { x = 0.194179, y = -0.056321 }, big = false },
{ topLeft = { x = 0.143038, y = -0.060015 }, big = true },
}
}) do
for i, data in ipairs(seq) do
local bottomRight = { x = data.topLeft.x, y = data.topLeft.y }
if data.big then
bottomRight.x = bottomRight.x - bigCheckBoxSize
bottomRight.y = bottomRight.y + bigCheckBoxSize
else
bottomRight.x = bottomRight.x - smallCheckBoxSize
bottomRight.y = bottomRight.y + smallCheckBoxSize
end
local stati = stat..i
cell[stati.."CheckBox"] = ui:CheckBox({ id = stati, topLeft = data.topLeft, bottomRight = bottomRight, onClick = function()
local value = i
if cell.survivor[stat] >= i then
value = value - 1
end
cell.survivor:SetNum(stat, value)
end })
end
end
cell.weaponProficiencyTypeInput = ui:Input({ id = "weaponProficiencyType", topLeft = { x = -0.054834, y = -0.064679 }, bottomRight = { x = -0.490963, y = -0.017972 }, fontSize = 40, onValueChanged = function(value)
cell.survivor:SetWeaponProficiencyType(value)
end })
local image1TopLeft = { x = 0.492351, y = 0.091029 }
local image1BottomRight = { x = 0.446708, y = 0.136740 }
local image14TopLeft = { x = 0.001845, y = 0.426968 }
local imageWidth = image1BottomRight.x - image1TopLeft.x
local imageHeight = image1BottomRight.y - image1TopLeft.y
local imageColDelta = image14TopLeft.x - image1TopLeft.x
local imageRowDelta = (image14TopLeft.y - image1TopLeft.y) / 6
local textOffset = -0.01
for col = 1, 2 do
local x = image1TopLeft.x + ((col - 1) * imageColDelta)
for row = 1, 7 do
local y = image1TopLeft.y + ((row - 1) * imageRowDelta)
local i = ((col - 1) * 7) + row
cell["card"..i.."Image"] = ui:Image({
id = "card"..i,
topLeft = { x = x , y = y },
bottomRight = { x = x + imageWidth, y = y + imageHeight },
})
cell["card"..i.."Text"] = ui:Text({
id = "card"..i,
topLeft = { x = x + imageWidth + textOffset, y = y },
bottomRight = { x = x + imageColDelta, y = y + imageHeight },
fontSize = 40,
alignment = "MiddleLeft",
})
end
end
for i, data in pairs({
{ topLeft = { x = 0.494516, y = 0.550233 }, bottomRight = { x = -0.491173, y = 0.594320 } },
{ topLeft = { x = 0.494516, y = 0.609200 }, bottomRight = { x = -0.491173, y = 0.654265 } },
{ topLeft = { x = 0.494516, y = 0.668215 }, bottomRight = { x = -0.491173, y = 0.713223 } },
{ topLeft = { x = 0.494516, y = 0.727703 }, bottomRight = { x = -0.491173, y = 0.773722 } },
{ topLeft = { x = 0.494516, y = 0.787172 }, bottomRight = { x = -0.491173, y = 0.833688 } },
}) do
cell["notes"..i.."Input"] = ui:Input({ id = "notes"..i, topLeft = data.topLeft, bottomRight = data.bottomRight, fontSize = 30, onValueChanged = function(value)
cell.survivor["SetNotes"..i](cell.survivor, value)
end })
end
cell.moveToBeginningButton = ui:Button({ id = "moveToBeginning", topLeft = { x = 0.490387, y = 0.862018 }, bottomRight = { x = 0.392043, y = 0.960771 }, onClick = function()
Survivor.MoveBefore(cell.survivor, Survivor.SurvivorsMatchingCurrentFilters()[1])
Survivor.SetPageAndRefresh(Survivor.page)
end })
cell.moveBackButton = ui:Button({ id = "moveBack", topLeft = { x = 0.368695, y = 0.863741 }, bottomRight = { x = 0.271971, y = 0.959538 }, onClick = function()
local index = ((Survivor.page - 1) * 20) + cell.ordinal
Survivor.MoveBefore(cell.survivor, Survivor.SurvivorsMatchingCurrentFilters()[index - 1])
Survivor.SetPageAndRefresh(math.ceil((index - 1) / 20))
end })
cell.spawnButton = ui:Button({ id = "spawn", topLeft = { x = 0.246005, y = 0.862302 }, bottomRight = { x = 0.011954, y = 0.962470 }, onClick = function()
Survivor.SpawnSurvivorBox(cell.survivor, "Survivor Box Spawn "..cell.ordinal)
end })
cell.deleteButton = ui:Button({ id = "delete", topLeft = { x = -0.013713, y = 0.860059 }, bottomRight = { x = -0.252386, y = 0.958966 }, onClick = function()
Survivor.DestroySurvivor(cell.survivor)
end })
cell.moveForwardButton = ui:Button({ id = "moveForward", topLeft = { x = -0.274279, y = 0.857828 }, bottomRight = { x = -0.369863, y = 0.959786 }, onClick = function()
local index = ((Survivor.page - 1) * 20) + cell.ordinal
Survivor.MoveAfter(cell.survivor, Survivor.SurvivorsMatchingCurrentFilters()[index + 1])
Survivor.SetPageAndRefresh(math.ceil((index + 1) / 20))
end })
cell.moveToEndButton = ui:Button({ id = "moveToEnd", topLeft = { x = -0.392412, y = 0.860326 }, bottomRight = { x = -0.492170, y = 0.961336 }, onClick = function()
local survivors = Survivor.SurvivorsMatchingCurrentFilters()
Survivor.MoveAfter(cell.survivor, survivors[#survivors])
Survivor.SetPageAndRefresh(Survivor.page)
end })
cell.object.UI.setCustomAssets(Survivor.CUSTOM_ASSETS)
cell.ui:ApplyToObject()
end
---------------------------------------------------------------------------------------------------
function Survivor.InitEvents()
EventManager.AddHandler("onObjectDestroy", function(object)
local survivorBox = Survivor.survivorBoxesByObject[object]
if survivorBox then
log:Debugf("%s was destroyed", survivorBox)
Survivor.survivorBoxesBySurvivor[survivorBox.survivor] = nil
Survivor.survivorBoxesByObject[object] = nil
return
end
local survivorSheet = Survivor.survivorSheetsByObject[object]
if survivorSheet then
log:Debugf("%s was destroyed", survivorSheet)
Survivor.survivorSheetsBySurvivor[survivorSheet.survivor] = nil
Survivor.survivorSheetsByObject[object] = nil
return
end
end)
EventManager.AddHandler("onObjectEnterContainer", function(container, object)
local survivorBox = Survivor.survivorBoxesByObject[container]
if survivorBox then
survivorBox:UpdateCards()
end
end)
EventManager.AddHandler("onObjectLeaveContainer", function(container, object)
local survivorBox = Survivor.survivorBoxesByObject[container]
if survivorBox then
survivorBox:UpdateCards()
end
end)
EventManager.AddHandler("tryObjectEnterContainer", function(container, object, previousReturnValue)
if previousReturnValue == false then
return false
end
if Survivor.survivorBoxesByObject[object] then
log:Printf("Survivor boxes cannot be put into containers.")
return false
end
if Survivor.survivorBoxesByObject[container] and not Survivor.ALLOWED_CARD_TYPES[object.getGMNotes()] then
log:Printf("Only Fighting Arts, Disorders, Abilities, and Impairments can go into survivor boxes.")
return false
end
return true
end)
EventManager.AddHandler(EventManager.ON_SURVIVOR_STAT_CHANGED, function(survivor, stat, value)
if stat == "name" then
local survivorBox = Survivor.survivorBoxesBySurvivor[survivor]
if survivorBox then
survivorBox:SetName(value)
end
end
if stat == "dead" then
Survivor.UpdateLivingDeadCounts()
end
if stat == "dead" or stat == "retired" or stat == "skipNextHunt" or stat == "marker" then
Survivor.SetPageAndRefresh(Util.Min(Survivor.MaxPage(), Survivor.page))
end
local survivorSheet = Survivor.survivorSheetsBySurvivor[survivor]
if survivorSheet then
local updater = Survivor.statUpdaters[stat]
if updater then
log:Debugf("%s %s changed to %s", survivor, stat, value)
updater(survivorSheet, stat, value)
end
end
for _, cell in ipairs(Survivor.cells) do
if cell.survivor == survivor then
local updater = Survivor.statUpdaters[stat]
if updater then -- some stats don't have UI
updater(cell, stat, value)
break
end
end
end
end)
EventManager.AddHandler(EventManager.ON_SURVIVOR_CARDS_CHANGED, function(survivor)
for _, cell in ipairs(Survivor.cells) do
if cell.survivor == survivor then
Survivor.UpdateCellCards(cell)
end
end
end)
end
-------------------------------------------------------------------------------------------------
function Survivor.PostInit()
Survivor.UpdateLivingDeadCounts()
Survivor.SetPageAndRefresh(Survivor.page)
end
---------------------------------------------------------------------------------------------------
function Survivor.Save()
local survivors = {}
for _, survivor in ipairs(Survivor.survivors) do
table.insert(survivors, survivor:Save())
end
local survivorBoxes = {}
for _, survivorBox in pairs(Survivor.survivorBoxesBySurvivor) do
table.insert(survivorBoxes, survivorBox:Save())
end
local survivorSheets = {}
for _, survivorSheet in pairs(Survivor.survivorSheetsBySurvivor) do
table.insert(survivorSheets, survivorSheet:Save())
end
return {
survivors = survivors,
survivorBoxes = survivorBoxes,
survivorSheets = survivorSheets,
newSurvivorBonuses = Survivor.newSurvivorBonuses,
page = Survivor.page,
hideLiving = Survivor.hideLiving,
hideDead = Survivor.hideDead,
hideSkipNextHunt = Survivor.hideSkipNextHunt,
hideRetired = Survivor.hideRetired,
hideYellowStar = Survivor.hideYellowStar,
hideGreenTriangle = Survivor.hideGreenTriangle,
hideBlueSquare = Survivor.hideBlueSquare,
hideRedCircle = Survivor.hideRedCircle,
hidePurpleDiamond = Survivor.hidePurpleDiamond,
}
end
---------------------------------------------------------------------------------------------------
function Survivor.Import(data)
for object, _ in pairs(Survivor.survivorBoxesByObject) do
object.destruct()
end
Survivor.survivorBoxesBySurvivor = {}
Survivor.survivorBoxesByObject = {}
for object, survivorSheet in pairs(Survivor.survivorSheetsByObject) do
object.destruct()
end
Survivor.survivorSheetsBySurvivor = {}
Survivor.survivorSheetsByObject = {}
Survivor.InitSaveState(data)
Survivor.UpdateLivingDeadCounts()
Survivor.SetPageAndRefresh(Survivor.page)
end
-------------------------------------------------------------------------------------------------
-- same as save except we don't include physical objects: sheets and boxes
function Survivor.Export()
local survivors = {}
for _, survivor in ipairs(Survivor.survivors) do
table.insert(survivors, survivor:Save())
end
return {
survivors = survivors,
newSurvivorBonuses = Survivor.newSurvivorBonuses,
page = Survivor.page,
hideLiving = Survivor.hideLiving,
hideDead = Survivor.hideDead,
hideSkipNextHunt = Survivor.hideSkipNextHunt,
hideRetired = Survivor.hideRetired,
hideYellowStar = Survivor.hideYellowStar,
hideGreenTriangle = Survivor.hideGreenTriangle,
hideBlueSquare = Survivor.hideBlueSquare,
hideRedCircle = Survivor.hideRedCircle,
hidePurpleDiamond = Survivor.hidePurpleDiamond,
}
end
-------------------------------------------------------------------------------------------------
function Survivor.UpdateLivingDeadCounts()
local numLiving = 0
local numDead = 0
for _, survivor in ipairs(Survivor.survivors) do
if survivor:Dead() then
numDead = numDead + 1
else
numLiving = numLiving + 1
end
end
Survivor.livingSurvivorsText:SetText(numLiving)
Survivor.deadSurvivorsText:SetText(numDead)
end
---------------------------------------------------------------------------------------------------
function Survivor.SurvivorsMatchingCurrentFilters()
local survivors = {}
for i, survivor in ipairs(Survivor.survivors) do
local hideSurvivor = (
(Survivor.hideLiving and not survivor:Dead()) or
(Survivor.hideDead and survivor:Dead()) or
(Survivor.hideSkipNextHunt and survivor:SkipNextHunt()) or
(Survivor.hideRetired and survivor:Retired()) or
(Survivor.hideYellowStar and survivor:Marker() == Survivor.MARKER_YELLOW_STAR) or
(Survivor.hideGreenTriangle and survivor:Marker() == Survivor.MARKER_GREEN_TRIANGLE) or
(Survivor.hideBlueSquare and survivor:Marker() == Survivor.MARKER_BLUE_SQUARE) or
(Survivor.hideRedCircle and survivor:Marker() == Survivor.MARKER_RED_CIRCLE) or
(Survivor.hidePurpleDiamond and survivor:Marker() == Survivor.MARKER_PURPLE_DIAMOND)
)
if not hideSurvivor then
table.insert(survivors, survivor)
end
end
return survivors
end
---------------------------------------------------------------------------------------------------
function Survivor.MaxPage()
return Util.Max(1, math.ceil(#Survivor.SurvivorsMatchingCurrentFilters() / 20))
end
---------------------------------------------------------------------------------------------------
function Survivor.SetPageAndRefresh(page)
local survivors = Survivor.SurvivorsMatchingCurrentFilters()
local maxPage = Util.Max(1, math.ceil(#survivors / 20))
if page < 1 then
log:Debugf("Capping page to 1")
page = 1
elseif page > maxPage then
log:Debugf("Capping page to maxPage=%d", maxPage)
page = maxPage
end
Survivor.page = page
local baseIndex = (page - 1) * 20
local numSurvivorsOnPage = Util.Min(baseIndex + 20, #survivors) - baseIndex
log:Debugf("Found %d survivors on page %d", numSurvivorsOnPage, page)
for i = 1, numSurvivorsOnPage do
Survivor.ShowCell(Survivor.cells[i], survivors[baseIndex + i])
end
for i = numSurvivorsOnPage + 1, 20 do
Survivor.HideCell(Survivor.cells[i])
end
Survivor.pageText:SetText("Page "..page.."/"..maxPage)
end
---------------------------------------------------------------------------------------------------
function Survivor.MoveBefore(survivor, beforeSurvivor)
if not beforeSurvivor or survivor == beforeSurvivor then
return
end
local survivorIndex = nil
local beforeSurvivorIndex = nil
for i, s in ipairs(Survivor.survivors) do
if s == survivor then
survivorIndex = i
elseif s == beforeSurvivor then
beforeSurvivorIndex = i
end
end
assert(Check(survivorIndex != nil))
assert(Check(beforeSurvivorIndex != nil))
table.insert(Survivor.survivors, beforeSurvivorIndex, Survivor.survivors[survivorIndex])
if survivorIndex < beforeSurvivorIndex then
table.remove(Survivor.survivors, survivorIndex)
else
table.remove(Survivor.survivors, survivorIndex + 1)
end
end
---------------------------------------------------------------------------------------------------
function Survivor.MoveAfter(survivor, afterSurvivor)
if not afterSurvivor or survivor == afterSurvivor then
return
end
local survivorIndex = nil
local afterSurvivorIndex = nil
for i, s in ipairs(Survivor.survivors) do
if s == survivor then
survivorIndex = i
elseif s == afterSurvivor then
afterSurvivorIndex = i
end
end
assert(Check(survivorIndex != nil))
assert(Check(afterSurvivorIndex != nil))
table.insert(Survivor.survivors, afterSurvivorIndex + 1, Survivor.survivors[survivorIndex])
if survivorIndex < afterSurvivorIndex then
table.remove(Survivor.survivors, survivorIndex)
else
table.remove(Survivor.survivors, survivorIndex + 1)
end
end
---------------------------------------------------------------------------------------------------
function Survivor.CanDestroySurvivor(survivor)
local survivorBox = Survivor.survivorBoxesBySurvivor[survivor]
if survivorBox then
log:Broadcastf("Please delete survivor box first.")
Util.Highlight(survivorBox.object)
return
end
local survivorSheet = Survivor.survivorSheetsBySurvivor[survivor]
if survivorSheet then
log:Broadcastf("Please return survivor to settlement first.")
Util.Highlight(survivorSheet.object)
return
end
return true
end
---------------------------------------------------------------------------------------------------
function Survivor.DestroySurvivor(survivor)
if not Survivor.CanDestroySurvivor(survivor) then
return
end
MessageBox.Show(Util.SafeFormat("Are you sure you want to delete '%s'", survivor:NameOrUnnamed()), function()
-- We have to check again for the pathological case of spawning a box/sheet while the message box is up
if not Survivor.CanDestroySurvivor(survivor) then
return
end
for i, s in ipairs(Survivor.survivors) do
if s == survivor then
table.remove(Survivor.survivors, i)
Survivor.survivorsById[survivor.id] = nil
log:Printf("Deleted survivor %s", survivor:NameOrUnnamed())
Survivor.UpdateLivingDeadCounts()
Survivor.SetPageAndRefresh(Util.Min(Survivor.MaxPage(), Survivor.page))
EventManager.FireEvent(EventManager.ON_SURVIVOR_DESTROYED, survivor)
end
end
log:Debugf("Survivor %s not found, it must have already been deleted", survivor)
end)
end
---------------------------------------------------------------------------------------------------
-- to generate cell positions:
-- local notes = ""
-- for i = 1, 20 do
-- local pos = Survivor.cells[i].object.getPosition()
-- notes = notes..string.format("{ x = %f, y = %f, z = %f },\n", pos.x, pos.y, pos.z)
-- end
-- setNotes(notes)
Survivor.CELL_POSITIONS = {
{ x = -44.897339, y = 3.424418, z = -87.435417 },
{ x = -34.916607, y = 3.424421, z = -87.436218 },
{ x = -24.935480, y = 3.424424, z = -87.436760 },
{ x = -14.954360, y = 3.424428, z = -87.437302 },
{ x = -4.973239, y = 3.424431, z = -87.437851 },
{ x = 5.007882, y = 3.424434, z = -87.438400 },
{ x = 14.989003, y = 3.424437, z = -87.438942 },
{ x = 24.970661, y = 3.424440, z = -87.439453 },
{ x = 34.951500, y = 3.424444, z = -87.440254 },
{ x = 44.932381, y = 3.424447, z = -87.440941 },
{ x = -44.898739, y = 3.424458, z = -105.875778 },
{ x = -34.917610, y = 3.424461, z = -105.876328 },
{ x = -24.936489, y = 3.424464, z = -105.876877 },
{ x = -14.955368, y = 3.424467, z = -105.877419 },
{ x = -4.974248, y = 3.424470, z = -105.877968 },
{ x = 5.006873, y = 3.424474, z = -105.878510 },
{ x = 14.987995, y = 3.424477, z = -105.879059 },
{ x = 24.969114, y = 3.424480, z = -105.879601 },
{ x = 34.950699, y = 3.424483, z = -105.880150 },
{ x = 44.931355, y = 3.424486, z = -105.880692 },
}
function Survivor.ShowCell(cell, survivor)
log:Debugf("Showing cell %d with survivor %s", cell.ordinal, survivor)
if cell.survivor == survivor then
log:Debugf("Cell %d already has survivor %s", cell.ordinal, survivor)
return
end
cell.survivor = survivor
cell.markerButton:SetImage(survivor:Marker() or Survivor.MARKER_EMPTY)
-- counters
cell.survivalCounter:SetText(survivor:Survival())
cell.insanityCounter:SetText(survivor:Insanity())
cell.movementCounter:SetText(survivor:Movement())
cell.speedCounter:SetText(survivor:Speed())
cell.accuracyCounter:SetText(survivor:Accuracy())
cell.strengthCounter:SetText(survivor:Strength())
cell.evasionCounter:SetText(survivor:Evasion())
cell.luckCounter:SetText(survivor:Luck())
-- inputs
cell.nameInput:SetText(survivor:Name())
cell.weaponProficiencyTypeInput:SetText(survivor:WeaponProficiencyType())
cell.notes1Input:SetText(survivor:Notes1())
cell.notes2Input:SetText(survivor:Notes2())
cell.notes3Input:SetText(survivor:Notes3())
cell.notes4Input:SetText(survivor:Notes4())
cell.notes5Input:SetText(survivor:Notes5())
-- checkboxes
cell.maleCheckBox:Check(survivor:Male())
cell.femaleCheckBox:Check(survivor:Female())
cell.rerollCheckBox:Check(survivor:Reroll())
cell.skipNextHuntCheckBox:Check(survivor:SkipNextHunt())
cell.retiredCheckBox:Check(survivor:Retired())
cell.deadCheckBox:Check(survivor:Dead())
-- checkbox sequences
for stat, max in pairs(Survivor.CHECKBOX_SEQUENCE_MAXES) do
local value = Util.Min(survivor[stat], max)
for i = 1, value do
cell[stat..i.."CheckBox"]:Check(true)
end
for i = value + 1, max do
cell[stat..i.."CheckBox"]:Check(false)
end
end
Survivor.UpdateCellCards(cell)
cell.object.setScale({ x = 9.01, y = 1, z = 9.01 })
cell.object.setRotation({ x = 0, y = 180, z = 0 })
cell.object.setPosition(Survivor.CELL_POSITIONS[cell.ordinal])
end
---------------------------------------------------------------------------------------------------
Survivor.ICONS_BY_TYPE = {
["Secret Fighting Arts"] = "IconFightingArt",
["Fighting Arts"] = "IconFightingArt",
["Disorders"] = "IconDisorder",
["Abilities"] = "IconAbility",
["Severe Injuries"] = "IconSevereInjury",
}
function Survivor.UpdateCellCards(cell)
local cardsByType = {
["Abilities"] = {},
["Disorders"] = {},
["Fighting Arts"] = {},
["Secret Fighting Arts"] = {},
["Severe Injuries"] = {},
}
for _, card in ipairs(cell.survivor:Cards()) do
if cardsByType[card.type] then
table.insert(cardsByType[card.type], card)
end
end
for i = 1, 14 do
cell["card"..i.."Text"]:Hide()
cell["card"..i.."Image"]:Hide()
end
local function PickIndex(list1, list2)
local i = nil
if #list1 > 0 then
i = list1[#list1]
table.remove(list1)
elseif #list2 > 0 then
i = list2[#list2]
table.remove(list2)
end
return i