forked from jordanmchavez/kdm-tts
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Rules.ttslua
338 lines (275 loc) · 11.8 KB
/
Rules.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
local Archive = require("Kdm/Archive")
local Check = require("Kdm/Util/Check")
local CircularArray = require("Kdm/Util/CircularArray")
local Expansion = require("Kdm/Expansion")
local Location = require("Kdm/Location")
local log = require("Kdm/Log").ForModule("Rules")
local NamedObject = require("Kdm/NamedObject")
local Trie = require("Kdm/Util/Trie")
local Ui = require("Kdm/Ui")
local Util = require("Kdm/Util/Util")
---------------------------------------------------------------------------------------------------
local Rules = {}
---------------------------------------------------------------------------------------------------
function Rules.Init()
Rules.trie = Trie()
Rules.history = CircularArray(100)
Rules.index = {}
Rules.searchResultButtons = {}
for _, expansion in ipairs(Expansion.All()) do
Rules.RegisterExpansion(expansion)
end
local rules = Rules.GetCurrentRules()
if rules != nil then
local currentState = rules.getStateId()
local rulebookName = nil
if currentState == 1 then
rulebookName = rules.getName()
else
for _, state in ipairs(rules.getStates()) do
if state.id == 1 then
rulebookName = state.name
end
end
end
assert(Check(rulebookName, "No rules page with state 1 in %s (%s)", rules.getName(), rules.getGUID()))
Rules.history:Push({ rulebookName, currentState})
end
-----------------------------------------------------------------------------------------------
local xStart = 70+100+10+30+10
local yStart = -10
local ui = Ui.Get2d()
local colors = "#ffffff|#dddddd|#dddddd|#ffffff"
Rules.searchInput = ui:Input({ id = "RulesSearch", x = xStart, y = yStart, width = 240, height = 30, placeholder = "search rules/events...", workaroundNilColors = true, textColor = "#000000", colors = colors, textOffset = "5 5 0 0", fontSize = 16, onValueChanged = function(value)
Rules.SetSearchResults(Rules.Search(value))
end })
ui:Button({ id = "RulesCancelSearch", x = xStart+240+5, y = yStart, width = 30, height = 30, text = "X", textAlignment = "MiddleCenter", textColor = "#000000", colors = colors, fontSize = 20, onClick = function()
Rules.SetSearchResults({})
Rules.searchInput:SetText(nil)
end })
Rules.searchResultsPanel = ui:Panel({ id = "RulesSearchResults", x = xStart, y = yStart - 40, width = 300, height = 160, color = "#00000088", active = false })
Rules.searchResultButtons = {}
for i = 1, 5 do
Rules.searchResultButtons[i] = Rules.searchResultsPanel:Button({ id = "Result"..i, x = 5, y = -5 + (i - 1) * -30, width = 280, height = 30, fontSize = 16, colors = Ui.INVISIBLE_COLORS, textColor = "#dddddd" })
end
-----------------------------------------------------------------------------------------------
local ui = Ui.Create3d("RulesNav", NamedObject.Get("Rules Navigation Board"), 0.11)
local x1 = 7.762758
local x1End = 6.705129
local width = x1End - x1
local x13 = -6.702875
local dx = (x13 - x1) / 12
local y1 = -0.401185
local y1End = 0.047924
local height = y1End - y1
local y2 = 0.392804
local dy = y2 - y1
local createButton = function(name, col, row, onClick)
local topLeft = { x = x1 + ((col - 1) * dx), y = y1 + ((row - 1) * dy) }
local bottomRight = { x = topLeft.x + width, y = topLeft.y + height }
ui:Button({ id = name, topLeft = topLeft, bottomRight = bottomRight, onClick = onClick })
end
local buttons = {
{ "Core Rules", 1, 1 },
{ "Dragon King Rules", 2, 1 },
{ "Dung Beetle Knight Rules", 3, 1 },
{ "Flower Knight Rules", 4, 1 },
{ "Gorm Rules", 5, 1 },
{ "Lion God Rules", 6, 1 },
{ "Lion Knight Rules", 7, 1 },
{ "Lonely Tree Rules", 8, 1 },
{ "Manhunter Rules", 9, 1 },
{ "Slenderman Rules", 10, 1 },
{ "Spidicules Rules", 11, 1 },
{ "Sunstalker Rules", 12, 1 },
{ "White Gigalion Rules", 13, 1 },
}
for _, button in ipairs(buttons) do
createButton(button[1], button[2], button[3], function() Rules.SpawnRules(button[1], 1) end)
end
createButton("Back", 6, 2, Rules.Back)
createButton("Previous Page", 7, 2, function() Rules.ChangePage(-1) end )
createButton("Next Page", 8, 2, function() Rules.ChangePage(1) end)
ui:ApplyToObject()
end
---------------------------------------------------------------------------------------------------
function Rules.RegisterExpansion(expansion)
for rulebookIndex, rulebook in ipairs(expansion.rulebooks or {}) do
local rulebookName = rulebook.name
assert(Check.Str(rulebookName, "rulebook.name"))
local pages = rulebook.pages
assert(Check.Table(rulebook.pages))
for pageName, pageData in pairs(pages) do
assert(Check.Str(pageName, "rulebooks[%s].pages[pageName]", rulebookName))
local state = pageData.state
assert(Check.Num(state, "rulebooks[%s].pages[%s].state", rulebookName, pageName))
assert(Check(Rules.index[pageName] == nil, "Rulebook page %s is already registered to %s", pageName, Rules.index[pageName]))
Rules.index[pageName] = { rulebook = rulebookName, state = state }
local keywords = pageData.keywords
assert(Check.Table(keywords, "rulebooks[%s].pages[%s].keywords", rulebookName, pageName))
local entry = {
rulebook = rulebookName,
name = pageName,
state = state,
timelineEvent = pageData.timelineEvent,
showdown = pageData.showdown,
}
for keywordIndex, keyword in ipairs(keywords) do
assert(Check.Str(keyword, "rulebooks[%s].pages[%s].keywords[%d]", rulebookName, pageName, keywordIndex))
Rules.trie:Set(keyword, entry)
end
if pageData.huntEventStart then
local huntEventStart = pageData.huntEventStart
assert(Check.Num(huntEventStart, "rulebooks[%s].pages[%s].huntEventStart", rulebookName, pageName, keywordIndex))
local huntEventEnd = pageData.huntEventEnd
assert(Check.Num(huntEventEnd, "rulebooks[%s].pages[%s].huntEventEnd", rulebookName, pageName, keywordIndex))
for huntEventNumber = huntEventStart, huntEventEnd do
Rules.trie:Set(tostring(huntEventNumber), entry)
end
end
end
end
end
---------------------------------------------------------------------------------------------------
function Rules.RulebookAndState(name)
local data = Rules.index[name]
assert(Check(data, "Unknown rule: %s", name))
return data.rulebook, data.state
end
---------------------------------------------------------------------------------------------------
function Rules.Search(searchTerms)
if searchTerms == nil or searchTerms == "" then
return {}
end
-- only accept results that match all terms
local matchIndex = {}
local i = 1
for term in string.gmatch(searchTerms, "%S+") do
local termMatches = Rules.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)
return sortedMatches
end
---------------------------------------------------------------------------------------------------
function Rules.SetSearchResults(results)
if #results == 0 then
Rules.searchResultsPanel:Hide()
return
end
Rules.searchResultsPanel:Show()
local numResults = Util.Min(5, #results)
Rules.searchResultsPanel:SetHeight(10 + (numResults * 30))
for i = 1, numResults do
local result = results[i]
local button = Rules.searchResultButtons[i]
button:Show()
button:SetOnClick(function()
Rules.searchInput:SetText(nil)
Rules.SetSearchResults({})
Rules.SpawnRules(result.rulebook, result.state)
--Location.Get("Rules Board"):LookAt({ pitch = 90 })
end)
button:SetText(result.name)
end
for i = numResults + 1, 5 do
local button = Rules.searchResultButtons[i]
button:Hide()
button:SetOnClick(nil)
button:SetText(nil)
end
end
---------------------------------------------------------------------------------------------------
function Rules.SpawnRules(rules, state, dontRecordHistory)
assert(Check.Str(rules))
assert(Check.Num(state))
assert(Check.BooleanOrNil(dontRecordHistory))
log:Debugf("Spawning rules %s[%d]", rules, state)
local location = Location.Get("Rules Board")
local blocking = location:BoxClean({ types = { "Rulebook" } })
if #blocking > 0 then
log:Broadcastf("Something is blocking the rules board. Please move the highlighted objects out of the way and try again.")
Util.HighlightAll(blocking)
return
end
if not Archive.Take({
name = rules,
type = "Rulebook",
location = location,
spawnFunc = function(obj)
if state > 1 then
obj = obj.setState(state)
end
Wait.frames(function() obj.setLock(true) end, 3)
end,
}) then
log:Errorf("Unrecognized rules: %s[%d]", rules, state)
end
Archive.Clean()
if not dontRecordHistory then
Rules.history:Push({ rules, state })
end
end
---------------------------------------------------------------------------------------------------
function Rules.GetCurrentRules()
return Location.Get("Rules Board"):FirstObject({ types = { "Rulebook" } })
end
---------------------------------------------------------------------------------------------------
function Rules.Back()
local rules = Rules.GetCurrentRules()
if not rules then
return
end
if Rules.history:Size() <= 1 then
log:Debugf("<= 1 history element, can't go back")
return
end
Rules.history:Pop()
local last = Rules.history:Top()
log:Debugf("Going back to %s[%d]", last[1], last[2])
Rules.SpawnRules(last[1], last[2], true) -- dontRecordHistory
end
---------------------------------------------------------------------------------------------------
function Rules.ChangePage(dir)
local rules = Rules.GetCurrentRules()
if not rules then
log:Debugf("No rules to change")
return
end
local currentState = rules.getStateId()
local nextState = currentState + dir
log:Debugf("Found current rules %s (%s) at state %d, looking for state %d", rules.getName(), rules.getGUID(), currentState, nextState)
local rulebookName = (currentState == 1) and rules.getName() or nil
local hasNext = false
for _, state in ipairs(rules.getStates()) do
if state.id == 1 then
rulebookName = state.name
end
if state.id == nextState then
log:Debugf("Found next state %d: %s", state.id, state.name)
hasNext = true
end
end
if not hasNext then
log:Debugf("No state %d", nextState)
return
end
assert(Check(rulebookName, "No rulebook name (page with state 1) in %s (%s)", rules.getName(), rules.getGUID()))
Rules.history:Push({ rulebookName, nextState })
local finalObj = rules.setState(nextState)
Wait.frames(function() finalObj.setLock(true) end, 3)
end
---------------------------------------------------------------------------------------------------
return {
Init = Rules.Init,
RulebookAndState = Rules.RulebookAndState,
Search = Rules.Search,
SpawnRules = Rules.SpawnRules,
}