forked from jordanmchavez/kdm-tts
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Location.ttslua
463 lines (369 loc) · 15.7 KB
/
Location.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
local Check = require("Kdm/Util/Check")
local Console = require("Kdm/Console")
local EventManager = require("Kdm/Util/EventManager")
local Expansion = require("Kdm/Expansion")
local LocationData = require("Kdm/LocationData")
local log = require("Kdm/Log").ForModule("Location")
local NamedObject = require("Kdm/NamedObject")
local Util = require("Kdm/Util/Util")
---------------------------------------------------------------------------------------------------
local Location = {}
Location.__index = Location
function Location.Is(x) return getmetatable(x) == Location end
Location.CELL_SIZE = 1
Location.DEFAULT_CAST_HEIGHT = 20
Location.CLEAN_IGNORE_TAGS = { "Board", "Table" }
---------------------------------------------------------------------------------------------------
function Location.Init()
Location.locationsByName = {}
Location.locationsByCell = {}
Location.locationsByObject = {}
for name, data in pairs(LocationData) do
local location = nil
if data.fromObject then
location = Location.CreateFromObject(NamedObject.Get(data.fromObject))
else
data.name = name
if data.board then
data.object = NamedObject.Get(data.board)
end
location = Location.Create(data)
end
Location.Add(location)
end
for _, expansion in ipairs(Expansion.All()) do
for name, locationParams in pairs(expansion.locations or {}) do
locationParams.object = NamedObject.Get(locationParams.board)
Location.Add(Location.Create(locationParams))
end
end
-- Initialize starting objects to their particular locations
for _, object in ipairs(getAllObjects()) do
if object.interactable then
local locations = Location.LocationsAtPosition(object.getPosition())
Location.locationsByObject[object] = locations
for _, location in ipairs(locations) do
log:Debugf("[%s] %s started in %s", object.getGUID(), object.getName(), location)
location.objects[object] = object
-- We specifically don't call dropHandlers here
-- Users are responsible for performing initial location:AllObjects() queries
end
end
end
EventManager.AddHandler("onObjectDrop", function(_, object) Location.OnEnter(object) end)
EventManager.AddHandler("onObjectSpawn", function(object) Location.OnEnter(object) end)
EventManager.AddHandler("onObjectPickUp", function(_, object) Location.OnLeave(object) end)
EventManager.AddHandler("onObjectEnterContainer", function(_, object) Location.OnLeave(object) end)
EventManager.AddHandler("onObjectDestroy", function(object) Location.OnLeave(object) end)
Console.AddCommand("showloc", function(args)
if #args != 2 then
return Console.Printf("Usage: showloc <location>")
end
local location = Location.locationsByName[args[2]:lower()]
if not location then
return Console.Printf("No such location: %s", args[2])
end
location:BoxCast({ debug = true })
end, "Highlights a location")
Console.AddCommand("locobjects", function(args)
if #args != 2 then
return Console.Printf("Usage: locobjects <location>")
end
local location = Location.locationsByName[args[2]:lower()]
if not location then
return Console.Printf("No such location: %s", args[2])
end
local objects = location:AllObjects()
Console.Printf("%d objects at %s:", #objects, location:Name())
for _, object in ipairs(objects) do
Console.Printf(" [%s] %s", object.getGUID(), object.getName())
end
end, "Lists objects at a location")
end
---------------------------------------------------------------------------------------------------
function Location.CellIndex(col, row)
return row.." "..col
end
---------------------------------------------------------------------------------------------------
function Location.Add(location)
local name = location:Name()
assert(Check(not Location.locationsByName[name], "Location %s already registered: %s", name, Location.locationsByName[name]))
Location.locationsByName[name:lower()] = location
local left, top, right, bottom = Location.CellRect(location)
for row = top, bottom do
for col = left, right do
local index = Location.CellIndex(col, row)
if not Location.locationsByCell[index] then
Location.locationsByCell[index] = { location }
else
table.insert(Location.locationsByCell[index], location)
end
end
end
end
---------------------------------------------------------------------------------------------------
function Location.Get(locationOrName)
if type(locationOrName) == "table" then
assert(Location.Is(locationOrName))
return locationOrName
end
assert(Check.Str(locationOrName))
local location = Location.locationsByName[locationOrName:lower()]
if not location then
log:Debugf("Unknown location: %s", locationOrName)
end
return location
end
---------------------------------------------------------------------------------------------------
function Location.LocationsAtPosition(position)
local results = {}
local x, z = position.x, position.z
local col, row = math.floor(x / Location.CELL_SIZE), math.floor(z / Location.CELL_SIZE)
local locations = Location.locationsByCell[Location.CellIndex(col, row)] or {}
for _, location in ipairs(locations) do
local left, top, right, bottom = location:Rect()
if x >= left and x <= right and z >= top and z <= bottom then
table.insert(results, location)
end
end
return results
end
---------------------------------------------------------------------------------------------------
function Location.ObjectLocations(object)
assert(Check.Object(object))
return Location.locationsByObject[object]
end
---------------------------------------------------------------------------------------------------
function Location.OnEnter(object)
if object.held_by_color then
log:Debugf("[%s] %s held by a player, ignoring", object.getGUID(), object.getName())
return
end
local locations = Location.LocationsAtPosition(object.getPosition())
-- Link the object with all locations *before* calling handlers, in case the handlers do their own queries
Location.locationsByObject[object] = locations
for _, location in ipairs(locations) do
log:Debugf("[%s] %s entered %s", object.getGUID(), object.getName(), location)
location.objects[object] = true
end
for _, location in ipairs(locations) do
for _, dropHandler in ipairs(location.dropHandlers) do
dropHandler(object)
end
end
end
---------------------------------------------------------------------------------------------------
function Location.OnLeave(object)
local locations = Location.locationsByObject[object] or {}
-- Unlink the object with all locations *before* calling handlers, in case the handlers do their own queries
for _, location in ipairs(locations) do
log:Debugf("[%s] %s left %s", object.getGUID(), object.getName(), location)
location.objects[object] = nil
end
for _, location in ipairs(locations) do
for _, pickUpHandler in ipairs(location.pickUpHandlers) do
pickUpHandler(object)
end
end
Location.locationsByObject[object] = nil
end
---------------------------------------------------------------------------------------------------
function Location.Create(params)
assert(Check.Table(params))
assert(Check.ObjectOrNil(params.object))
assert(Check.Str(params.name))
local location = {
name = params.name,
objects = {},
dropHandlers = {},
pickUpHandlers = {},
}
setmetatable(location, Location)
if params.center and params.size then
if params.object then
location.center = params.object.positionToWorld(params.center)
else
location.center = params.center
end
location.topLeft = {
x = location.center.x - (params.size.x / 2),
y = location.center.y - (params.size.y / 2),
z = location.center.z - (params.size.z / 2),
}
location.bottomRight = {
x = location.center.x + (params.size.x / 2),
y = location.center.y + (params.size.y / 2),
z = location.center.z + (params.size.z / 2),
}
elseif params.p1 and params.p2 then
local p1 = params.p1
local p2 = params.p2
if params.object then
p1 = params.object.positionToWorld(p1)
p2 = params.object.positionToWorld(p2)
end
location.center = {
x = (p1.x + p2.x) / 2,
y = (p1.y + p2.y) / 2,
z = (p1.z + p2.z) / 2,
}
location.topLeft = {
x = Util.Min(p1.x, p2.x),
y = Util.Min(p1.y, p2.y),
z = Util.Min(p1.z, p2.z),
}
location.bottomRight = {
x = Util.Max(p1.x, p2.x),
y = Util.Max(p1.y, p2.y),
z = Util.Max(p1.z, p2.z),
}
else
assert(Check.Fail("Invalid params for Location.Create. Requires either center+size or p1+p2: %s", params))
end
return location
end
---------------------------------------------------------------------------------------------------
function Location.CreateFromObject(object)
local bounds = object.getBounds()
return Location.Create({ name = object.getName(), center = bounds.center, size = bounds.size })
end
---------------------------------------------------------------------------------------------------
function Location:__tostring()
return Util.SafeFormat("Location{name=%s}", self.name)
end
---------------------------------------------------------------------------------------------------
function Location:CellRect()
local left, top, bottom, right = self:Rect()
return math.floor(left / Location.CELL_SIZE), math.floor(top / Location.CELL_SIZE), math.floor(bottom / Location.CELL_SIZE), math.floor(right / Location.CELL_SIZE)
end
---------------------------------------------------------------------------------------------------
function Location:Name()
return self.name
end
---------------------------------------------------------------------------------------------------
function Location:Center()
local c = self.center
-- always return a copy
return { x = c.x, y = c.y, z = c.z }
end
---------------------------------------------------------------------------------------------------
function Location:Size()
local tl, br = self.topLeft, self.bottomRight
return {
x = math.abs(br.x - tl.x),
y = math.abs(br.y - tl.y),
z = math.abs(br.z - tl.z),
}
end
---------------------------------------------------------------------------------------------------
function Location:Rect()
local topLeft, bottomRight = self.topLeft, self.bottomRight
local left = Util.Min(topLeft.x, bottomRight.x)
local right = Util.Max(topLeft.x, bottomRight.x)
local top = Util.Min(topLeft.z, bottomRight.z)
local bottom = Util.Max(topLeft.z, bottomRight.z)
return left, top, right, bottom
end
---------------------------------------------------------------------------------------------------
function Location:BoxCast(params)
params = params or {}
local center = self:Center()
center.y = center.y + (params.height or Location.DEFAULT_CAST_HEIGHT)
local size = nil
if params.size then
size = params.size
else
size = self:Size()
size.y = size.x -- doesn't matter, just needs to be > epsilon for a top-down cast
end
return Physics.cast({
origin = center,
direction = { x = 0, y = -1, z = 0 },
type = 3,
size = size,
debug = params.debug,
})
end
---------------------------------------------------------------------------------------------------
function Location:RayCast(params)
params = params or {}
local center = self:Center()
center.y = center.y + (params.height or Location.DEFAULT_CAST_HEIGHT)
return Physics.cast({
origin = center,
direction = { x = 0, y = -1, z = 0 },
type = 1,
debug = params.debug,
})
end
---------------------------------------------------------------------------------------------------
function Location:FirstObject(params)
for object, _ in pairs(self.objects) do
if not object.held_by_color and object.interactable and Location.Matches(object, params.tags, params.types) then
return object
end
end
return nil
end
---------------------------------------------------------------------------------------------------
function Location:AllObjects(tag)
local results = {}
for object, _ in pairs(self.objects) do
if not object.held_by_color and object.interactable and (tag == nil or object.getGMNotes() == tag) then
table.insert(results, object)
end
end
return results
end
---------------------------------------------------------------------------------------------------
function Location.Matches(obj, tags, types)
local matchesTags = tags and Util.ArrayContains(tags, obj.tag)
local matchesTypes = types and Util.ArrayContains(types, obj.getGMNotes())
return matchesTags or matchesTypes
end
---------------------------------------------------------------------------------------------------
function Location:Clean(params, castFunc)
tags, types, debug = params.tags, params.types, params.debug
log:Debugf("Cleaning %s", self)
local hits = castFunc(self, { size = params.size, debug = params.debug })
local blocking = {}
for _, hit in ipairs(hits) do
local obj = hit.hit_object
log:Debugf("Hit object [%s]%s with type %s and tag %s", obj.getGUID(), obj.getName(), obj.getGMNotes(), obj.tag)
if Location.Matches(obj, Location.CLEAN_IGNORE_TAGS, params.ignoreTypes) or not obj.interactable then
-- ignore
elseif Location.Matches(obj, tags, types) then
log:Debugf("Destroying hit object [%s] %s with matching type %s/tag %s", obj.getGUID(), obj.getName(), obj.getGMNotes(), obj.tag)
obj.destruct()
else
log:Debugf("Hit blocking object %s/%s (%s)", obj.getName(), obj.tag, obj.getGUID())
table.insert(blocking, obj)
end
end
return blocking
end
function Location:BoxClean(params) return self:Clean(params, Location.BoxCast) end
function Location:RayClean(params) return self:Clean(params, Location.RayCast) end
---------------------------------------------------------------------------------------------------
function Location:LookAt(params)
params = params or {}
Util.LookAt({ position = self:Center(), pitch = params.pitch, distance = params.distance })
end
---------------------------------------------------------------------------------------------------
function Location:AddDropHandler(handler)
assert(Check.Func(handler))
table.insert(self.dropHandlers, handler)
end
---------------------------------------------------------------------------------------------------
function Location:AddPickUpHandler(handler)
assert(Check.Func(handler))
table.insert(self.pickUpHandlers, handler)
end
---------------------------------------------------------------------------------------------------
return {
Is = Location.Is,
Init = Location.Init,
Get = Location.Get,
LocationsAtPosition = Location.LocationsAtPosition,
ObjectLocations = Location.ObjectLocations,
}