-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
ObjectUtils.ttslua
executable file
·297 lines (245 loc) · 10.4 KB
/
ObjectUtils.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
require('ge_tts.License')
local Base64 = require('ge_tts.Base64')
local Json = require('ge_tts.Json')
local Object = require('ge_tts.Object')
local SaveManager = require('ge_tts.SaveManager')
local Vector3 = require('ge_tts.Vector3')
---There's a random component to our GUIDs designed to mitigate collisions is a user wants to copy objects between mods.
local GUID_PREFIX_RANDOM_BYTE_LENGTH = 3
---@type string
local guidPrefix
---@type number
local guidIndex = 0
---@class ge_tts__ObjectUtils
local ObjectUtils = {}
---@param obj tts__Object
---@return ge_tts__Vector3
function ObjectUtils.getTransformScale(obj)
local rotation = obj.getRotation()
local onesVector = Vector3(1, 1, 1).rotateZ(rotation.z).rotateX(rotation.x).rotateY(rotation.y)
local scale = Vector3(obj.positionToLocal(onesVector.add(obj.getPosition())))
return scale
end
---@param tag string
---@return boolean
function ObjectUtils.isContainerTag(tag)
return tag == Object.Tag.Deck or tag == Object.Tag.Bag
end
---@return number
function ObjectUtils.previousGuidIndex()
return guidIndex
end
---@return string
function ObjectUtils.guidPrefix()
return guidPrefix
end
---@return string
function ObjectUtils.nextGuid()
guidIndex = guidIndex + 1
return guidPrefix .. tostring(guidIndex)
end
---@param objectState tts__ObjectState
---@param guid string
---@param callback_function nil | tts__ObjectCallbackFunction
local function safeSpawnObject(objectState, guid, callback_function)
objectState.GUID = guid
local spawningObject = spawnObjectData({
data = objectState,
callback_function = callback_function,
})
return spawningObject
end
---
---A wrapper around TTS' spawnObjectData() which assigns GUIDs in a fashion that mitigates collisions with objects
---in containers.
---
---@param objectState tts__ObjectState @Will be JSON encoded after we generate and assign a GUID.
---@param callback_function? nil | tts__ObjectCallbackFunction @Callback that will be called when the object has finished spawning.
---@return tts__Object
function ObjectUtils.safeSpawnObject(objectState, callback_function)
return safeSpawnObject(objectState, ObjectUtils.nextGuid(), callback_function)
end
---
---Same as ObjectUtils.safeSpawnObject(...), except that instead of generating a unique GUID, it is your responsibility
---to provide one. If you fail to provide a unique GUID, all safety guarantees are lost.
---
---In practice, you should only call this method if you're respawning an object that was destroyed.
---
---@overload fun(objectState: tts__ObjectState, guid: string): tts__Object
---@param objectState tts__ObjectState @Will be JSON encoded after we generate and assign a GUID.
---@param guid string
---@param callback_function nil | tts__ObjectCallbackFunction @Callback that will be called when the object has finished spawning.
---@return tts__Object
function ObjectUtils.safeRespawnObject(objectState, guid, callback_function)
return safeSpawnObject(objectState, guid, callback_function)
end
--- This is only useful in very specific circumstances. Generally ObjectUtils is automatically setup appropriately when
--- Tabletop Simulator calls onLoad.
---@param guidPrefix string
---@param guidIndex number
function ObjectUtils.setup(prefix, index)
guidPrefix = prefix
guidIndex = index
end
---@overload fun(position: nil | tts__VectorShape): tts__ObjectState_Transform
---@overload fun(position: nil | tts__VectorShape, rotation: nil | tts__VectorShape): tts__ObjectState_Transform
---@overload fun(position: nil | tts__VectorShape, rotation: nil | tts__VectorShape, scale: nil | tts__VectorShape): tts__ObjectState_Transform
---@overload fun(transform: {position: nil | tts__VectorShape, rotation: nil | tts__VectorShape, scale: nil | tts__VectorShape}): tts__ObjectState_Transform
---@vararg ge_tts__Vector3
---@return tts__ObjectState_Transform
function ObjectUtils.transformState(...)
---@type tts__ObjectState_Transform
local state = {}
---@type nil | tts__VectorShape
local position
---@type nil | tts__VectorShape
local rotation = nil
---@type nil | tts__VectorShape
local scale = nil
if select('#', ...) == 1 then
local args = --[[---@type table]] ...
if args[1] then
position = --[[---@type tts__VectorShape]] args
else
local transform = --[[---@type {position: nil | tts__VectorShape, rotation: nil | tts__VectorShape, scale: nil | tts__VectorShape}]] args
position = transform.position
rotation = transform.rotation
scale = transform.scale
end
else
position, rotation, scale = ...
end
if position then
state.posX = (--[[---@type tts__CharVectorShape]] position).x or (--[[---@type tts__NumVectorShape]] position)[1]
state.posY = (--[[---@type tts__CharVectorShape]] position).y or (--[[---@type tts__NumVectorShape]] position)[2]
state.posZ = (--[[---@type tts__CharVectorShape]] position).z or (--[[---@type tts__NumVectorShape]] position)[3]
end
if rotation then
state.rotX = (--[[---@type tts__CharVectorShape]] rotation).x or (--[[---@type tts__NumVectorShape]] rotation)[1]
state.rotY = (--[[---@type tts__CharVectorShape]] rotation).y or (--[[---@type tts__NumVectorShape]] rotation)[2]
state.rotZ = (--[[---@type tts__CharVectorShape]] rotation).z or (--[[---@type tts__NumVectorShape]] rotation)[3]
end
if scale then
state.scaleX = (--[[---@type tts__CharVectorShape]] scale).x or (--[[---@type tts__NumVectorShape]] scale)[1]
state.scaleY = (--[[---@type tts__CharVectorShape]] scale).y or (--[[---@type tts__NumVectorShape]] scale)[2]
state.scaleZ = (--[[---@type tts__CharVectorShape]] scale).z or (--[[---@type tts__NumVectorShape]] scale)[3]
end
return state
end
---@param transformState tts__ObjectState_Transform
---@return ge_tts__Vector3
function ObjectUtils.getTransformStatePosition(transformState)
return Vector3(
transformState.posX or 0,
transformState.posY or 0,
transformState.posZ or 0
)
end
---@param transformState tts__ObjectState_Transform
---@return ge_tts__Vector3
function ObjectUtils.getTransformStateRotation(transformState)
return Vector3(
transformState.rotX or 0,
transformState.rotY or 0,
transformState.rotZ or 0
)
end
---@param transformState tts__ObjectState_Transform
---@return ge_tts__Vector3
function ObjectUtils.getTransformStateScale(transformState)
return Vector3(
transformState.scaleX or 1,
transformState.scaleY or 1,
transformState.scaleZ or 1
)
end
---
---Same as ObjectUtils.safeSpawnObject except that each entry in containerState.ContainedObjects will also be assigned a
---unique GUID.
---
---@overload fun(containerState: tts__ContainerState): tts__Container
---@param containerState tts__ContainerState @Will be JSON encoded after we generate and assign a GUID.
---@param callback_function nil | tts__Callback<fun(container: tts__Container): void> @Callback that will be called when the object has finished spawning.
---@return tts__Container
function ObjectUtils.safeSpawnContainer(containerState, callback_function)
for _, objectState in ipairs(containerState.ContainedObjects) do
objectState.GUID = ObjectUtils.nextGuid()
end
return --[[---@type tts__Container]] ObjectUtils.safeSpawnObject(containerState, --[[---@type nil | tts__ObjectCallbackFunction]] callback_function)
end
local POSITION_NEAR_THRESHOLD = 0.025001
local POSITION_NEAR_THRESHOLD_SQUARED = POSITION_NEAR_THRESHOLD * POSITION_NEAR_THRESHOLD
--- Works around a bug in TTS' setPositionSmooth that occurs if you setPositionSmooth to a position then immediately
--- back to the current position with setPositionSmooth, then the second call is incorrectly ignored.
---@param object tts__Object
---@param position tts__VectorShape
---@param collide? nil | boolean
---@param fast? nil | boolean
function ObjectUtils.setPositionSmooth(object, position, collide, fast)
if object.getPositionSmooth() then
local objectPosition = object.getPosition()
if Vector3.distanceSquared(position, objectPosition) <= POSITION_NEAR_THRESHOLD_SQUARED then
object.setPosition(position)
return
end
end
object.setPositionSmooth(position, collide, fast)
end
local ROTATION_NEAR_THRESHOLD = 1.000001
--- Works around a bug in TTS' setRotationSmooth that occurs if you setRotationSmooth to a rotation then immediately
--- back to the current rotation with setRotationSmooth, then the second call is incorrectly ignored.
---@param object tts__Object
---@param rotation tts__VectorShape
---@param collide? nil | boolean
---@param fast? nil | boolean
function ObjectUtils.setRotationSmooth(object, rotation, collide, fast)
if object.getRotationSmooth() then
local objectRotation = object.getRotation()
local objectRotationDirection = Vector3(1, 0, 0)
.rotateZ(objectRotation.z)
.rotateX(objectRotation.x)
.rotateY(objectRotation.y)
local rotationDirection = Vector3(1, 0, 0)
.rotateZ(Vector3.z(rotation))
.rotateX(Vector3.x(rotation))
.rotateY(Vector3.y(rotation))
if Vector3.angle(rotationDirection, objectRotationDirection) <= ROTATION_NEAR_THRESHOLD then
object.setRotation(rotation)
return
end
end
object.setRotationSmooth(rotation, collide, fast)
end
---@shape __ge_tts__ObjectUtils_SavedStateData
---@field guidIndex number
---@field guidPrefix string
---@return string
local function onSave()
---@type __ge_tts__ObjectUtils_SavedStateData
local data = {
guidIndex = guidIndex,
guidPrefix = guidPrefix,
}
return Json.encode(data)
end
local function onFirstLoad()
local guidRandomBytes = {}
for _ = 1, GUID_PREFIX_RANDOM_BYTE_LENGTH do
table.insert(guidRandomBytes, math.random(1, 255))
end
guidPrefix = Base64.encode(guidRandomBytes, false) .. ':'
end
---@param savedState string
local function onLoad(savedState)
if savedState == '' then
onFirstLoad()
return
end
local data = --[[---@type __ge_tts__ObjectUtils_SavedStateData]] Json.decode(savedState)
guidPrefix = data.guidPrefix
guidIndex = data.guidIndex
end
local MODULE_NAME = 'ge_tts.ObjectUtils'
SaveManager.registerOnSave(MODULE_NAME, onSave)
SaveManager.registerOnLoad(MODULE_NAME, onLoad)
return ObjectUtils