forked from Farbod678/kdm-tts
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTimeline.ttslua
874 lines (744 loc) · 33 KB
/
Timeline.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
local Archive = require("Kdm/Archive")
local Check = require("Kdm/Util/Check")
local Container = require("Kdm/Util/Container")
local Expansion = require("Kdm/Expansion")
local Hunt = require("Kdm/Hunt")
local Location = require("Kdm/Location")
local log = require("Kdm/Log").ForModule("Timeline")
local MessageBox = require("Kdm/MessageBox")
local Rules = require("Kdm/Rules")
local Showdown = require("Kdm/Showdown")
local Trie = require("Kdm/Util/Trie")
local Ui = require("Kdm/Ui")
local Util = require("Kdm/Util/Util")
-------------------------------------------------------------------------------------------------
local Timeline = {}
Timeline.MAX_YEARS = 30
Timeline.MAX_YEAR_EVENTS = 6
Timeline.MAX_SURVIVAL_ACTIONS = 6
Timeline.MAX_MILESTONES = 4
Timeline.MAX_QUARRIES = 10
Timeline.MAX_NOTES = 6
Timeline.MAX_TIMELINE_EVENT_POPUP_RESULTS = 5
Timeline.RIGHT_MOUSE_BUTTON = "-2"
Timeline.EVENT_TYPE_ICONS = {
["RulebookEvent"] = "RulebookEventIcon",
["HuntEvent"] = "HuntEventIcon",
["ShowdownEvent"] = "ShowdownEventIcon",
["SettlementEvent"] = "SettlementEventIcon",
}
Timeline.EVENT_TYPE_IMAGES = {
["RulebookEvent"] = "RulebookEventButton",
["HuntEvent"] = "HuntEventButton",
["ShowdownEvent"] = "ShowdownEventButton",
["SettlementEvent"] = "SettlementEventButton",
}
---------------------------------------------------------------------------------------------------
function Timeline.Init(saveState)
Timeline.InitDefaultEvents()
Timeline.survivalActions = {}
for i = 1, Timeline.MAX_SURVIVAL_ACTIONS do
Timeline.survivalActions[i] = {}
end
Timeline.milestones = {}
for i = 1, Timeline.MAX_MILESTONES do
Timeline.milestones[i] = {}
end
Timeline.quarries = {}
for i = 1, Timeline.MAX_QUARRIES do
Timeline.quarries[i] = {}
end
Timeline.notes = {}
for i = 1, Timeline.MAX_NOTES do
Timeline.notes[i] = {}
end
Timeline.years = {}
for i = 1, Timeline.MAX_YEARS do
Timeline.years[i] = {
events = {}
}
for j = 1, Timeline.MAX_YEAR_EVENTS do
Timeline.years[i].events[j] = {}
end
end
Timeline.InitState(saveState)
Timeline.InitExpansions()
Timeline.InitUi(ui)
end
---------------------------------------------------------------------------------------------------
function Timeline.InitDefaultEvents()
Timeline.trie = Trie()
Timeline.defaultEvents = {
{
keywords = { "Unspecified", "Nemesis"},
event = {
type = "ShowdownEvent",
name = "Unspecified Nemesis",
nemesis = true,
},
},
}
for level = 1, 3 do
table.insert(Timeline.defaultEvents, {
keywords = { "Unspecified", "Nemesis", "Level", tostring(level) },
event = {
type = "ShowdownEvent",
name = "Unspecified Nemesis - Level "..level,
nemesis = true,
},
})
end
for _, event in ipairs(Timeline.defaultEvents) do
for _, keyword in ipairs(event.keywords) do
Timeline.trie:Set(keyword, event.event)
end
end
end
---------------------------------------------------------------------------------------------------
function Timeline.InitState(saveState)
Timeline.settlementName = saveState.name
Timeline.survivalLimit = saveState.survivalLimit or 1
Timeline.departingBonuses = saveState.departingBonuses
Timeline.arrivalBonuses = saveState.arrivalBonuses
local survivalActions = saveState.survivalActions or {}
for i = 1, Timeline.MAX_SURVIVAL_ACTIONS do
if survivalActions[i] then
Timeline.survivalActions[i].name = survivalActions[i].name
Timeline.survivalActions[i].checked = survivalActions[i].checked
else
Timeline.survivalActions[i].name = nil
Timeline.survivalActions[i].checked = nil
end
end
local milestones = saveState.milestones or {}
for i = 1, Timeline.MAX_MILESTONES do
if milestones[i] then
Timeline.milestones[i].name = milestones[i].name
Timeline.milestones[i].event = milestones[i].event
Timeline.milestones[i].checked = milestones[i].checked
else
Timeline.milestones[i].name = nil
Timeline.milestones[i].event = nil
Timeline.milestones[i].checked = nil
end
end
local quarries = saveState.quarries or {}
for i = 1, Timeline.MAX_QUARRIES do
Timeline.quarries[i].quarry = quarries[i]
end
local notes = saveState.notes or {}
for i = 1, Timeline.MAX_NOTES do
Timeline.notes[i].note = notes[i]
end
local years = saveState.years or {}
for i = 1, Timeline.MAX_YEARS do
local year = years[i] or {}
Timeline.years[i].checked = year.checked
local events = year.events or {}
for j = 1, Timeline.MAX_YEAR_EVENTS do
if events[j] then
Timeline.years[i].events[j].event = events[j]
else
Timeline.years[i].events[j].event = nil
end
end
end
end
---------------------------------------------------------------------------------------------------
function Timeline.InitExpansions()
for _, expansion in ipairs(Expansion.All()) do
for _, rulebook in ipairs(expansion.rulebooks or {}) do
for name, page in pairs(rulebook.pages) do
if page.timelineEvent then
local event = {
name = name,
type = "RulebookEvent",
rulebook = rulebook.name,
state = page.state,
expansion = expansion.name,
}
for _, keyword in ipairs(page.keywords) do
Timeline.trie:Set(keyword, event)
end
end
end
end
for _, monster in ipairs(expansion.monsters or {}) do
local monsterKeywords = Util.Split(monster.name, " ")
if #monster.levels == 1 then
local event = {
type = monster.nemesis and "ShowdownEvent" or "HuntEvent",
name = Timeline.ShowdownEventTimelineName(monster.name, nil, monster.nemesis),
monster = monster.name,
expansion = expansion.name,
}
for _, keyword in ipairs(monsterKeywords) do
Timeline.trie:Set(keyword, event)
end
else
for _, level in ipairs(monster.levels) do
local levelKeywords = Util.Split(level.name, " ")
local event = {
type = monster.nemesis and "ShowdownEvent" or "HuntEvent",
name = Timeline.ShowdownEventTimelineName(monster.name, level.name, monster.nemesis),
monster = monster.name,
level = level.name,
expansion = expansion.name,
}
for _, keyword in ipairs(Util.ConcatArrays(monsterKeywords, levelKeywords)) do
Timeline.trie:Set(keyword, event)
end
end
end
end
for _, settlementEvent in ipairs(expansion.settlementEvents or {}) do
for _, keyword in ipairs(Util.Split(settlementEvent, " ")) do
Timeline.trie:Set(keyword, {
name = settlementEvent,
type = "SettlementEvent",
expansion = expansion.name,
})
end
end
end
end
---------------------------------------------------------------------------------------------------
function Timeline.InitUi(ui)
Timeline.uiOpen = false
local ui = Ui.Get2d()
local panel = ui:Panel({ id = "Timeline", rectAlignment = "MiddleCenter", x = 0, y = 0, width = 1600, height = 935, active = Timeline.uiOpen })
Timeline.panel = panel
panel:Image({ id = "Timeline", image = "Settlement", x = 0, y = 0, width = 1600, height = 935 })
panel:Button({ id = "Close", x = 1570, y = 0, width = 30, height = 30, onClick = function(mouseButton, player)
Timeline.HideUiForAll()
end })
Timeline.settlementNameInput = panel:Input({ id = "SettlementName", x = 198, y = -45, width = 482, height = 40, fontSize = 24, onValueChanged = function(value)
log:Debugf("Setting settlement name to %s", value)
Timeline.settlementName = value
end })
Timeline.survivalLimitCounter = panel:Counter({ id = "SurvivalLimit", x = 793, y = -55, width = 50, height = 50, fontSize = 36, color = Ui.DARK_BROWN, onValueChanged = function(delta)
local value = Timeline.survivalLimit + delta
Timeline.survivalLimit = value
log:Debugf("Setting survival limit to %d", value)
Timeline.survivalLimitCounter:SetText(value)
end })
Timeline.departingBonusesInput = panel:Input({ id = "DepartingBonus", x = 980, y = -45, width = 288, height = 25, onValueChanged = function(value)
log:Debugf("Setting departing bonuses to %s", value)
Timeline.departingBonuses = value
Timeline.departingBonusesInput:SetText(value)
end })
Timeline.arrivalBonusesInput = panel:Input({ id = "ArrivalBonus", x = 980, y = -79, width = 288, height = 25, onValueChanged = function(value)
log:Debugf("Setting arrival bonuses to %s", value)
Timeline.arrivalBonuses = value
Timeline.arrivalBonusesInput:SetText(value)
end })
local coords = {
{ x = 1301, y = -112 },
{ x = 1301, y = -142 },
{ x = 1301, y = -173 },
{ x = 1435, y = -112 },
{ x = 1435, y = -142 },
{ x = 1435, y = -172 },
}
for i, survivalAction in ipairs(Timeline.survivalActions) do
local x, y = coords[i].x, coords[i].y
survivalAction.checkBox = panel:CheckBox({ id = "SurvivalAction"..i, x = x, y = y, width = 20, height = 20, uncheckedImage = "CheckBoxEmpty", active = false, onClick = function()
local name = Timeline.survivalActions[i].name
if name then
local checked = not Timeline.survivalActions[i].checked
log:Debugf("%s survival action %s", checked and "Checking" or "Unchecking", name)
Timeline.survivalActions[i].checked = checked
Timeline.survivalActions[i].checkBox:Check(checked)
end
end })
survivalAction.text = panel:Text({ id = "SurvivalAction"..i, x = x+26, y = y + 5, width = 100, height = 30, fontSize = 12, fontStyle = "Bold", text = "Embolden", active = false })
end
local checkBoxY = -277
local textY = checkBoxY + 2
local buttonY = -272
local dy = -40
for i, milestone in ipairs(Timeline.milestones) do
milestone.checkBox = panel:CheckBox( { id = "Milestone"..i, x = 1301, y = checkBoxY, width = 20, height = 20, uncheckedImage = "CheckBoxEmpty", active = false, onClick = function()
local milestone = Timeline.milestones[i]
assert(milestone)
local value = not milestone.checked
log:Debugf("%s milestone %s", value and "Checking" or "Unchecking", milestone.name)
milestone.checked = value
Timeline.milestones[i].checkBox:Check(value)
end })
milestone.text = panel:Text({ id = "Milestone"..i, x = 1326, y = textY, width = 86, height = 25, fontSize = 12, fontStyle = "Bold", active = false })
milestone.eventButton = panel:Button({ id = "MilestoneEvent"..i, x = 1417, y = buttonY, width = 145, height = 30, image = "MilestoneButton", active = false, onClick = function(mouseButton, player)
local milestone = Timeline.milestones[i]
assert(milestone)
Timeline.HideUiForAll()
local rulebook, state = Rules.RulebookAndState(milestone.event)
Rules.SpawnRules(rulebook, state)
--Location.Get("Rules Board"):LookAt({ pitch = 90 })
end })
milestone.eventText = panel:Text({ id = "MilestoneEvent"..i, x = 1447, y = buttonY, width = 108, height = 30, fontSize = 12, color = Ui.LIGHT_BROWN, active = false })
checkBoxY = checkBoxY + dy
textY = textY + dy
buttonY = buttonY + dy
end
local dy = 27
for i, quarry in ipairs(Timeline.quarries) do
local x = (i <= 5) and 1301 or 1437
local row = (i - 1) % 5
quarry.input = panel:Input({ id = "Quarries"..i, x = x, y = -499 - (row * dy), width = 125, height = 25, fontSize = 12, fontStyle = "Bold", onValueChanged = function(value)
log:Debugf("Setting quarries[%d] to %s", i, value)
Timeline.quarries[i].quarry = value
Timeline.quarries[i].input:SetText(value)
end })
end
local dy = 27
for i, note in ipairs(Timeline.notes) do
note.input = panel:Input({ id = "Notes"..i, x = 1301, y = -719 - (i - 1) * dy, width = 261, height = 25, onValueChanged = function(value)
log:Debugf("Setting settlement notes %d to %s", i, value)
Timeline.notes[i].note = value
Timeline.notes[i].input:SetText(value)
end })
end
-----------------------------------------------------------------------------------------------
local scroll = panel:VerticalScroll({ id = "V", x = 23, y = -158, width = 1242, height = 760 - 6 })
local timelinePanel = scroll:Panel({ id = "Timeline", x = 0, y = 0, width = 1242, height = 1563 })
timelinePanel:Image({ id = "Timeline", image = "Timeline30", x = 0, y = 0, width = 1242, height = 1563 })
local x1 = 78
local width = 172
local dx = 192
local dy = -52
local offset = 30
for i, year in ipairs(Timeline.years) do
local y = -15 + ((i - 1) * dy)
year.checkBox = timelinePanel:CheckBox({ id = "Year"..i, x = 10, y = y, width = 20, height = 20, uncheckedImage = "CheckBoxEmpty", onClick = function()
local checked = not Timeline.years[i].checked
log:Debugf("%s year %d", checked and "Checking" or "Unchecking", i)
Timeline.years[i].checked = checked
Timeline.years[i].checkBox:Check(checked)
end })
local y = -10 + ((i - 1) * dy)
for j, event in ipairs(year.events) do
local id = "Year"..i.."Event"..j
local x = x1 + ((j - 1) * dx)
year.events[j].button = timelinePanel:Button({ id = id, x = x, y = y, width = width, height = 30, onClick = function(mouseButton)
Timeline.ShowTimelineEventPopup(i, j)
end })
year.events[j].text = timelinePanel:Text({ id = id, x = x + offset, y = y, width = width - offset, height = 30, fontSize = 12, horizontalOverflow = "Wrap", color = Ui.LIGHT_BROWN })
end
end
-----------------------------------------------------------------------------------------------
Timeline.popupOpen = false
local popupPanel = ui:Panel({ id = "AddTimelineEvent", rectAlignment = "MiddleCenter", x = 0, y = 0, width = 376, height = 493, active = false })
Timeline.popupPanel = popupPanel
popupPanel:Image({ id = "AddTimelineEvent", image = "AddTimelineEvent", x = 0, y = 0, width = 376, height = 493 })
popupPanel:Button({ id = "Close", x = 336, y = -10, width = 30, height = 30, onClick = function()
Timeline.HideTimelineEventPopup()
end })
Timeline.popupSearchInput = popupPanel:Input({ id = "Search", x = 13+5, y = -52, width = 350-6, height = 30, fontSize = 14, textColor = Ui.LIGHT_BROWN, placeholder = "search events...", onValueChanged = Timeline.SearchEvents })
Timeline.popupResults = {}
Timeline.popupResultButtons = {}
Timeline.popupResultIcons = {}
Timeline.popupResultTexts = {}
for i = 1, Timeline.MAX_TIMELINE_EVENT_POPUP_RESULTS do
local y = -67 - (i * 30)
local x = 13+3
Timeline.popupResultButtons[i] = popupPanel:Button({ id = "SearchResult"..i, x = x, y = y, width = 350-6, height = 30, active = false, onClick = function()
Timeline.AddTimelineEvent(Timeline.popupYear, Timeline.popupEvent, Timeline.popupResults[i])
Timeline.HideTimelineEventPopup()
end })
x = x + 2 -- 2 padding
Timeline.popupResultIcons[i] = popupPanel:Image({ id = "SearchResult"..i, x = x, y = y-5, width = 24, height = 20, active = false })
x = x + 24 + 3 -- 3 padding
Timeline.popupResultTexts[i] = popupPanel:Text({ id = "SearchResult"..i, x = x, y = y, width = 350-6-24-6, height = 30, fontSize = 14, color = Ui.LIGHT_BROWN, active = false })
end
Timeline.popupCustomNameInput = popupPanel:Input({ id = "CustomName", x = 13+3, y = -355, width = 350-6, height = 30, fontSize = 14, textColor = Ui.LIGHT_BROWN, placeholder = "enter event name...", onValueChanged = function(value)
log:Debugf("Setting custom name to %s", value)
Timeline.popupCustomName = value
end })
Timeline.popupType = "RulebookEvent"
Timeline.popupTypeOptionCheckBoxGroup = popupPanel:OptionCheckBoxGroup({ id = "Event", uncheckedImage = "CheckBoxEmpty", uncheckedColors = Ui.IMAGE_COLORS, onClick = function(option)
log:Debugf("Selected event type %s", option:OptionValue())
Timeline.popupType = option:OptionValue()
option:Select()
end })
Timeline.popupRulebookEventCheckBox = Timeline.popupTypeOptionCheckBoxGroup:OptionCheckBox({ x = 58, y = -405, width = 20, height = 20, checked = true, optionValue = "RulebookEvent" })
Timeline.popupShowdownEventCheckBox = Timeline.popupTypeOptionCheckBoxGroup:OptionCheckBox({ x = 166, y = -405, width = 20, height = 20, optionValue = "ShowdownEvent" })
Timeline.popupSettlementEventCheckBox = Timeline.popupTypeOptionCheckBoxGroup:OptionCheckBox({ x = 270, y = -405, width = 20, height = 20, optionValue = "SettlementEvent" })
popupPanel:Button({ id = "Add", x = 65, y = -450, width = 100, height = 30, onClick = function()
if Timeline.popupCustomName == nil or Timeline.popupCustomName == "" then
return log:Broadcastf("Please enter the event's name.")
end
Timeline.AddTimelineEvent(Timeline.popupYear, Timeline.popupEvent, { name = Timeline.popupCustomName, type = Timeline.popupType, custom = true })
Timeline.HideTimelineEventPopup()
end })
popupPanel:Button({ id = "Cancel", x = 211, y = -450, width = 100, height = 30, onClick = Timeline.HideTimelineEventPopup })
end
---------------------------------------------------------------------------------------------------
function Timeline.PostInit()
Timeline.SetUiFromState()
end
---------------------------------------------------------------------------------------------------
function Timeline.SetUiFromState()
Timeline.settlementNameInput:SetText(Timeline.settlementName)
Timeline.survivalLimitCounter:SetText(Timeline.survivalLimit)
Timeline.departingBonusesInput:SetText(Timeline.departingBonuses)
Timeline.arrivalBonusesInput:SetText(Timeline.arrivalBonuses)
for _, survivalAction in ipairs(Timeline.survivalActions) do
if survivalAction.name then
survivalAction.checkBox:Show()
survivalAction.checkBox:Check(survivalAction.checked)
survivalAction.text:Show()
survivalAction.text:SetText(survivalAction.name)
else
survivalAction.checkBox:Hide()
survivalAction.text:Hide()
end
end
for _, milestone in ipairs(Timeline.milestones) do
if milestone.name then
milestone.checkBox:Show()
milestone.checkBox:Check(milestone.checked)
milestone.text:Show()
milestone.text:SetText(milestone.name)
milestone.eventButton:Show()
milestone.eventText:Show()
milestone.eventText:SetText(milestone.event)
else
milestone.checkBox:Hide()
milestone.text:Hide()
milestone.eventButton:Hide()
milestone.eventText:Hide()
end
end
for _, quarry in ipairs(Timeline.quarries) do
quarry.input:SetText(quarry.quarry)
end
for _, note in ipairs(Timeline.notes) do
note.input:SetText(note.note)
end
for i, year in ipairs(Timeline.years) do
year.checkBox:Check(year.checked)
for j, event in ipairs(year.events) do
if event.event then
Timeline.AddTimelineEvent(i, j, event.event)
else
Timeline.RemoveTimelineEvent(i, j)
end
end
end
end
-----------------------------------------------------------------------------------------------
function Timeline.Save()
local survivalActions = {}
for i, survivalAction in pairs(Timeline.survivalActions) do
if survivalAction and survivalAction.name then
survivalActions[i] = { name = survivalAction.name, checked = survivalAction.checked }
end
end
local milestones = {}
for i, milestone in ipairs(Timeline.milestones) do
if milestone.name then
milestones[i] = { name = milestone.name, checked = milestone.checked, event = milestone.event }
end
end
local quarries = {}
for i, quarry in ipairs(Timeline.quarries) do
quarries[i] = quarry.quarry
end
local notes = {}
for i, note in ipairs(Timeline.notes) do
notes[i] = note.note
end
local years = {}
for i, year in ipairs(Timeline.years) do
local events = {}
for j, event in ipairs(year.events) do
events[j] = event.event
end
years[i] = { checked = year.checked, events = events }
end
return {
name = Timeline.settlementName,
survivalLimit = Timeline.survivalLimit,
departingBonuses = Timeline.departingBonuses,
arrivalBonuses = Timeline.arrivalBonuses,
survivalActions = survivalActions,
milestones = milestones,
quarries = quarries,
notes = notes,
years = years,
}
end
---------------------------------------------------------------------------------------------------
function Timeline.Import(importState)
Timeline.InitState(importState)
Timeline.SetUiFromState()
end
---------------------------------------------------------------------------------------------------
function Timeline.Export()
return Timeline.Save() -- no difference for us
end
---------------------------------------------------------------------------------------------------
function Timeline.ShowUi(player)
log:Debugf("Showing settlement UI for ".. player.steam_name)
str = Timeline.panel:ShowForPlayer(player.color)
if str == player.color then
Timeline.uiOpen = true
else
log:Errorf(str .. " is already looking at the Settlement UI", player.steam_name, player.color)
end
end
---------------------------------------------------------------------------------------------------
function Timeline.HideUi(player)
log:Debugf("Hiding settlement UI for " .. player.steam_name)
str = Timeline.panel:HideForPlayer(player.color)
if str == "None" or str == player.color then
Timeline.uiOpen = false
else
log:Errorf(str .. " is already looking at the Settlement UI", player.steam_name, player.color)
end
end
---------------------------------------------------------------------------------------------------
function Timeline.ShowUiForAll()
log:Debugf("Showing Timeline UI for all")
Timeline.panel:Show()
Timeline.uiOpen = true
end
function Timeline.HideUiForAll()
log:Debugf("Hiding Timeline UI for all")
Timeline.panel:Hide()
Timeline.uiOpen = false
end
---------------------------------------------------------------------------------------------------
function Timeline.IsUiOpen()
return Timeline.uiOpen
end
---------------------------------------------------------------------------------------------------
function Timeline.ShowTimelineEventPopup(year, event)
if Timeline.popupOpen then
log:Debugf("Timeline event panel already open")
return
end
log:Debugf("Showing timeline event panel for year %d, event %d", year, event)
Timeline.popupPanel:Show()
Timeline.popupOpen = true
Timeline.popupYear = year
Timeline.popupEvent = event
end
---------------------------------------------------------------------------------------------------
function Timeline.HideTimelineEventPopup()
log:Debugf("Hiding Timeline event popup")
Timeline.popupPanel:Hide()
Timeline.popupOpen = nil
Timeline.popupYear = nil
Timeline.popupEvent = nil
Timeline.popupSearchInput:SetText(nil)
Timeline.SetEventSearchResults({})
Timeline.popupCustomName = nil
Timeline.popupCustomNameInput:SetText(nil)
Timeline.popupType = Timeline.popupRulebookEventCheckBox:OptionValue()
Timeline.popupRulebookEventCheckBox:Select()
end
---------------------------------------------------------------------------------------------------
function Timeline.SearchEvents(searchTerms)
if searchTerms == nil or searchTerms == "" then
Timeline.SetEventSearchResults({})
end
-- only accept results that match all terms
local matchIndex = {}
local i = 1
for term in string.gmatch(searchTerms, "%S+") do
local termMatches = Timeline.trie:Get(term)
local termIndex = Util.Index(termMatches, "name")
matchIndex = (i == 1) and termIndex or Util.Intersect(matchIndex, termIndex)
i = i + 1
end
-- sort matches
local sortedMatches = {}
for _, m in pairs(matchIndex) do
table.insert(sortedMatches, m)
end
table.sort(sortedMatches, function(x, y) return x.name < y.name end)
log:Debugf("Search results for %s: %s", searchTerms, Util.Map(sortedMatches, function(m) return m.name end))
Timeline.SetEventSearchResults(sortedMatches)
end
---------------------------------------------------------------------------------------------------
function Timeline.SetEventSearchResults(results)
Timeline.popupResults = results
for i = 1, Util.Min(Timeline.MAX_TIMELINE_EVENT_POPUP_RESULTS, #results) do
local result = results[i]
log:Debugf("Setting result %d to %s", i, result.name)
local button = Timeline.popupResultButtons[i]
button:Show()
local icon = Timeline.popupResultIcons[i]
icon:Show()
icon:SetImage(Timeline.EVENT_TYPE_ICONS[result.type])
local text = Timeline.popupResultTexts[i]
text:Show()
text:SetText(result.name)
end
for i = #results + 1, 5 do
local button = Timeline.popupResultButtons[i]
button:Hide()
local icon = Timeline.popupResultIcons[i]
icon:Hide()
icon:SetImage(nil)
local text = Timeline.popupResultTexts[i]
text:Hide()
text:SetText(nil)
end
end
---------------------------------------------------------------------------------------------------
function Timeline.AddTimelineEvent(yearIndex, eventIndex, params)
assert(Check.Num(yearIndex))
assert(Check.Num(eventIndex))
local name, type = params.name, params.type
if type == "ShowdownEvent" or type == "HuntEvent" then
name = params.custom and params.name or Timeline.ShowdownEventTimelineName(params.monster, params.level, params.nemesis)
end
assert(Check.Str(name))
log:Debugf("Adding %s event %s to year %d, event %d", type, name, yearIndex, eventIndex)
local event = Timeline.years[yearIndex].events[eventIndex]
event.button:SetImage(Timeline.EVENT_TYPE_IMAGES[type])
event.button:SetColors(Ui.IMAGE_COLORS)
event.button:SetOnClick(function(mouseButton, player) Timeline.DoEvent(yearIndex, eventIndex, mouseButton, player) end)
event.text:Show()
event.text:SetText(name)
Timeline.years[yearIndex].events[eventIndex].event = {
name = name,
type = type,
custom = params.custom,
monster = params.monster,
nemesis = params.nemesis,
level = params.level,
}
end
---------------------------------------------------------------------------------------------------
function Timeline.RemoveTimelineEvent(yearIndex, eventIndex)
assert(Check.Num(yearIndex))
assert(Check.Num(eventIndex))
local event = Timeline.years[yearIndex].events[eventIndex]
event.button:SetImage(nil)
event.button:SetColors(Ui.INVISIBLE_COLORS)
event.button:SetOnClick(function() Timeline.ShowTimelineEventPopup(yearIndex, eventIndex) end)
event.text:Hide()
event.text:SetText(nil)
Timeline.years[yearIndex].events[eventIndex].event = nil
end
---------------------------------------------------------------------------------------------------
Timeline.levelAbbr ={
["Level 1"] = "Lvl 1",
["Level 2"] = "Lvl 2",
["Level 3"] = "Lvl 3",
["Level 4"] = "Lvl 4",
}
function Timeline.ShowdownEventTimelineName(monster, level, nemesis)
level = Timeline.levelAbbr[level] or level
local name = (monster and monster:len() > 0) and monster or nil
if monster and level then
if nemesis then
return "Nemesis - "..monster.." "..level
else
return monster.." "..level
end
elseif monster then
if nemesis then
return "Nemesis - "..monster
else
return monster
end
elseif level then
return "Nemesis - "..level
else
return "Nemesis"
end
end
---------------------------------------------------------------------------------------------------
function Timeline.DoEvent(yearIndex, eventIndex, mouseButton, player)
local event = Timeline.years[yearIndex].events[eventIndex].event
assert(Check(event, "No event data for year %d, event %d", yearIndex, eventIndex))
log:Debugf("Processing year %d, event %d: %s", yearIndex, eventIndex, event)
if mouseButton == Timeline.RIGHT_MOUSE_BUTTON then
log:Debugf("RMB clicked, removing year %d, event %d", yearIndex, eventIndex)
Timeline.RemoveTimelineEvent(yearIndex, eventIndex)
else
local handler = Timeline.eventHandlersByType[event.type]
assert(Check(handler, "Unrecognized event type: %s", event.type))
handler(event, player)
end
end
---------------------------------------------------------------------------------------------------
Timeline.eventHandlersByType = {
["RulebookEvent"] = function(event, player)
if event.custom then
return log:Debugf("Custom timeline event button, no effect")
end
Timeline.HideUiForAll()
rulebook, state = Rules.RulebookAndState(event.name)
Rules.SpawnRules(rulebook, state)
--Location.Get("Rules Board"):LookAt({ pitch = 90 })
end,
["ShowdownEvent"] = function(event, player)
if event.custom then
Timeline.HideUiForAll()
Showdown.ShowUi(player)
return
end
local msg = string.format("Ready to start showdown for %s", event.monster)
if event.level then
msg = msg.." ("..event.level..")"
end
msg = msg.."?"
MessageBox.Show(msg, function()
Timeline.HideUiForAll()
Showdown.Setup(event.monster, event.level)
end)
end,
["HuntEvent"] = function(event, player)
if event.custom then
Timeline.HideUiForAll()
Hunt.ShowUi(player)
return
end
local msg = string.format("Ready to start hunt for %s", event.monster)
if event.level then
msg = msg.." ("..event.level..")"
end
msg = msg.."?"
MessageBox.Show(msg, function()
Timeline.HideUiForAll()
Hunt.Setup(event.monster, event.level)
end)
end,
["SettlementEvent"] = function(event, player)
Timeline.HideUiForAll()
local settlementEventsLocation = Location.Get("Settlement Events")
local deck = settlementEventsLocation:FirstObject({ types = { "Settlement Events" } })
if deck == nil then
log:Broadcastf("Couldn't find Settlement Event deck in the highlighted location, so created a new one.")
deck = Archive.Take({ name = "Settlement Events", type = "Settlement Events", location = settlementEventsLocation, rotation = { x = 0, y = 180, z = 180 } })
Util.Highlight(deck)
end
local card = Container(deck):Take({ name = event.name, type = "Settlement Events", location = Location.Get("Drawn Settlement Events") })
if not card then
log:Broadcastf("Couldn't find the '%s' card in the Settlement Events Deck", event.name)
Util.Highlight(deck)
return
end
--Util.LookAt({ object = card, pitch = 75, distance = 10 })
end
}
---------------------------------------------------------------------------------------------------
return {
Init = Timeline.Init,
PostInit = Timeline.PostInit,
Save = Timeline.Save,
Import = Timeline.Import,
Export = Timeline.Export,
ShowUi = Timeline.ShowUi,
HideUi = Timeline.HideUi,
ShowUiForAll = Timeline.ShowUiForAll,
HideUiForAll = Timeline.HideUiForAll,
IsUiOpen = Timeline.IsUiOpen,
MAX_YEARS = Timeline.MAX_YEARS,
MAX_YEAR_EVENTS = Timeline.MAX_YEAR_EVENTS,
}