forked from jordanmchavez/kdm-tts
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathArchive.ttslua
323 lines (279 loc) · 12.3 KB
/
Archive.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
local Check = require("Kdm/Util/Check")
local Container = require("Kdm/Util/Container")
local Expansion = require("Kdm/Expansion")
local Location = require("Kdm/Location")
local log = require("Kdm/Log").ForModule("Archive")
local NamedObject = require("Kdm/NamedObject")
local Util = require("Kdm/Util/Util")
---------------------------------------------------------------------------------------------------
local Archive = {}
Archive.data = {
{ "Abilities", "Abilities", "Abilities Archive" },
{ "Fighting Arts", "Fighting Arts", "Fighting Arts Archive" },
{ "Secret Fighting Arts", "Secret Fighting Arts", "Secret Fighting Arts Archive" },
{ "Disorders", "Disorders", "Disorders Archive" },
{ "Severe Injuries", "Severe Injuries", "Severe Injuries Archive" },
{ "Tactics", "Tactics", "Tactics Archive" },
{ "Weapon Proficiencies", "Weapon Proficiencies", "Weapon Proficiencies Archive" },
{ "Armor Sets", "Armor Sets", "Armor Sets Archive" },
{ "Vermin", "Vermin", "Vermin Archive" },
{ "Strange Resources", "Strange Resources", "Strange Resources Archive" },
{ "Basic Resources", "Basic Resources", "Basic Resources Archive" },
{ "Terrain", "Terrain", "Terrain Archive" },
{ "Terrain Tiles", "Terrain Tiles", "Terrain Tiles Archive" },
{ "Hunt Events", "Hunt Events", "Hunt Events Archive" },
{ "Settlement Events", "Settlement Events", "Settlement Events Archive" },
{ "Rare Gear", "Gear", "Rare Gear Archive" },
{ "All Gear", "Gear", "All Gear Archive" },
{ "Monster Resources", "Monster Resources", "Monster Resources Archive" },
{ "Survivor Sheet", "Survivor Sheet", "Survivor Sheets Archive" },
{ "Survivor Box", "Survivor Box", "Survivor Boxes Archive" },
{ "Dodge Tokens", "Survival Tokens", "Dodge Tokens" },
{ "Encourage Tokens", "Survival Tokens", "Encourage Tokens" },
{ "Embolden Tokens", "Survival Tokens", "Embolden Tokens" },
{ "Dash Tokens", "Survival Tokens", "Dash Tokens" },
{ "Surge Tokens", "Survival Tokens", "Surge Tokens" },
{ "Overcharge Tokens", "Survival Tokens", "Overcharge Tokens" },
{ "Endure Tokens", "Survival Tokens", "Endure Tokens" },
{ "Allister", "Player Figurine", "Allister Archive" },
{ "Ezra", "Player Figurine", "Ezra Archive" },
{ "Lucy", "Player Figurine", "Lucy Archive" },
{ "Zachary", "Player Figurine", "Zachary Archive" },
{ "Patterns", "Patterns", "Patterns Archive"},
}
---------------------------------------------------------------------------------------------------
function Archive.Init()
Archive.index = {}
Archive.direct = {}
-- "Direct" entries are the items directly contained by the infinite container archive, such as the "Disorders" deck in the "Disorders Archive".
-- For such items we just a flag telling us to take the item directly, no need to search inside it.
for _, entry in ipairs(Archive.data) do
local name, type, archive = entry[1], entry[2], entry[3]
local key = Archive.Key(name, type)
Archive.index[key] = archive
Archive.direct[key] = true
end
Archive.containers = {}
Archive.containerX = -150
Archive.containerZ = 120
for _, expansion in ipairs(Expansion.All()) do
Archive.RegisterEntries(expansion.archiveEntries)
end
end
---------------------------------------------------------------------------------------------------
function Archive.Key(name, type)
return type.."."..name
end
---------------------------------------------------------------------------------------------------
function Archive.RegisterEntries(params)
if not params then
return
end
archive = params.archive
assert(archive)
for _, entry in ipairs(params.entries) do
local name, type = entry[1], entry[2]
assert(name)
assert(type)
local key = Archive.Key(name, type)
if not params.allowOverrides then
assert(Check(Archive.index[key] == nil, "Archive entry %s/%s for %s is already registered for %s", name, type, archive, Archive.index[key]))
end
Archive.index[key] = archive
end
end
---------------------------------------------------------------------------------------------------
function Archive.NextContainerPosition()
local position = { x = Archive.containerX, y = 15, z = Archive.containerZ }
Archive.containerX = Archive.containerX + 10
if Archive.containerX > 150 then
Archive.containerX = -150
Archive.containerZ = Archive.containerZ - 10
if Archive.containerZ < 70 then
Archive.containerZ = 120
end
end
return position
end
---------------------------------------------------------------------------------------------------
function Archive.Take(params)
local archive = params.archive
local name = params.name
local type = params.type
local height = params.height or 2
local location = params.location
local position = params.position
if location then
location = Location.Get(location)
position = location:Center()
position.y = position.y + (height or 2)
else
assert(Check.Vec3(position, "Either location or position is required"))
end
local rotation = params.rotation or { x = 0, y = 180, z = 0 }
local spawnFunc = params.spawnFunc
assert(Check.StrOrNil(archive))
assert(Check.Str(name))
assert(Check.Str(type))
assert(Check.NumOrNil(height))
assert(Check.Vec3OrNil(rotation))
assert(Check.FuncOrNil(spawnFunc))
if not archive then
local key = Archive.Key(name, type)
archive = Archive.index[key]
if not archive then
log:Errorf("%s (%s) is not in the archive", name, type)
return nil
end
if Archive.direct[key] then
-- for single item archives no need to pull intermediate container
log:Debugf("Using direct from archive %s for %s (%s)", archive, name, type)
return NamedObject.Get(archive).takeObject({
position = position,
rotation = rotation,
smooth = false,
callback_function = spawnFunc,
})
end
end
if location then
log:Debugf("Taking item %s (%s) in archive %s to %s +%f", name, type, archive, location:Name(), height)
else
log:Debugf("Taking item %s (%s) in archive %s to %s", name, type, archive, position)
end
local container = Archive.containers[archive]
if container then
log:Debugf("Re-using already-spawned archive container %s for %s (%s)", container:Guid(), name, type)
else
-- spawn new container and cache
local archiveObject = NamedObject.Get(archive)
local containerObject = archiveObject.takeObject({
position = Archive.NextContainerPosition(),
smooth = false,
})
container = Container(containerObject)
if not container then
log:Errorf("Couldn't take from archive [%s] %s. If you accidentally reset/cleared it, then you may need to restore an earlier save. If it's one of the deck archives (Disorders, Fighting Arts, etc), you can try reconstructing the deck then dropping it onto the archive box to restore it.", archiveObject.getGUID(), archiveObject.getName())
Util.Highlight(archiveObject)
return nil
end
--log:Debugf("Spawned archive container %s at (%f, %f) for %s (%s)", container:Guid(), Archive.containerX, Archive.containerZ, name, type)
log:Debugf("Spawned archive container %s at (%f, %f) for %s (%s)", "", Archive.containerX, Archive.containerZ, name, type)
container:Lock(true)
Archive.containers[archive] = container
end
local object = container:Take({
name = name,
type = type,
position = position,
rotation = rotation,
spawnFunc = spawnFunc,
})
if not object then
log:Errorf("Couldn't find %s (%s) in archive %s. If you accidentally reset/cleared it, then you may need to restore an earlier save. If it's one of the deck archives (Disorders, Fighting Arts, etc), you can try reconstructing the deck then dropping it onto the archive box to restore it.", name, type, archive)
Util.Highlight(NamedObject.Get(archive))
return nil
end
log:Debugf("Spawned object %s (%s)", object.getName(), object.getGUID())
return object
end
---------------------------------------------------------------------------------------------------
function Archive.ArchiveSource(name, type)
return {
source = "Archive",
name = name,
type = type,
}
end
---------------------------------------------------------------------------------------------------
function Archive.ContainerSource(container, name, type)
return {
source = "Container",
container = container,
name = name,
type = type,
}
end
---------------------------------------------------------------------------------------------------
function Archive.CreateDeckFromSources(params)
local sources, location, rotation = params.sources, Location.Get(params.location), params.rotation or { x = 0, y = 180, z = 0 }
local objects = {}
local missing = {}
for i, source in ipairs(sources) do
local takeParams = { name = source.name, type = source.type, location = location, height = (i - 1) * 0.5, rotation = rotation }
local object = nil
if source.source == "Archive" then
object = Archive.Take(takeParams)
if not object then
log:Debugf("Couldn't find '%s' in archive", source.name)
table.insert(missing, source)
end
elseif source.source == "Container" then
object = source.container:Take(takeParams)
if not object then
log:Debugf("Couldn't find '%s' in container [%s] %s", source.name, source.container:Guid(), source.container:Name())
table.insert(missing, source)
end
else
assert(Check.Fail("Unrecognized source: %s", source))
end
if object then
table.insert(objects, object)
end
end
local deckObject
if #objects > 1 then
deckObject = group(objects)[1]
assert(Check(deckObject))
else
log:Debugf("CreateDeck only asked for one object [%s] %s, so not grouping", objects[1].getGUID(), objects[1].getName())
deckObject = objects[1]
end
deckObject.setName(params.name)
deckObject.setGMNotes(params.type)
deckObject.setPositionSmooth(location:Center(), false, true)
deckObject.setRotation(rotation)
log:Debugf("Created deck [%s] %s at %s", deckObject.getGUID(), deckObject.getName(), location)
return Container(deckObject), missing
end
---------------------------------------------------------------------------------------------------
function Archive.Clean()
for _, container in pairs(Archive.containers) do
log:Debugf("Destroying archive container %s", container:Guid())
container:Destruct()
end
-- scan and clean orphaned containers from a previous error
local hits = Physics.cast({
origin = { x = 0, y = 100, z = 95 },
direction = { x = 0, y = -1, z = 0 },
type = 3,
size = { x = 350, y = 50, z = 100 },
})
for _, hit in ipairs(hits) do
local obj = hit.hit_object
if obj.tag == "Bag" and obj.getGMNotes() == "Archive Bag" then
log:Debugf("Destroying leftover archive bag %s (%s)", obj.getName(), obj.getGUID())
obj.destruct()
end
if obj.tag == "Deck" then
local key = Archive.Key(obj.getName(), obj.getGMNotes())
if Archive.direct[key] then
log:Debugf("Destroying leftover archive deck %s (%s)", obj.getName(), obj.getGUID())
obj.destruct()
end
end
end
Archive.containers = {}
Archive.containerX = -150
Archive.containerZ = 120
end
---------------------------------------------------------------------------------------------------
return {
Init = Archive.Init,
RegisterEntries = Archive.RegisterEntries,
Take = Archive.Take,
ArchiveSource = Archive.ArchiveSource,
ContainerSource = Archive.ContainerSource,
CreateDeckFromSources = Archive.CreateDeckFromSources,
Clean = Archive.Clean,
}