-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.lua
More file actions
executable file
·582 lines (488 loc) · 14.7 KB
/
Copy pathutil.lua
File metadata and controls
executable file
·582 lines (488 loc) · 14.7 KB
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
---@type Plugin
local mode = ...
local util = {}
local ipairs = ipairs
local tostring = tostring
local isActive = isActive
local osRealClock = os.realClock
local bondsGetAll = bonds.getAll
local playersGetAll = players.getAll
local humansGetAll = humans.getAll
local vehiclesGetAll = vehicles.getAll
local itemsGetAll = items.getAll
local physicsLineIntersectLevel = physics.lineIntersectLevel
local physicsLineIntersectLevelQuick = physics.lineIntersectLevelQuick
local physicsLineIntersectHuman = physics.lineIntersectHuman
local physicsLineIntersectHumanQuick = physics.lineIntersectHumanQuick
local physicsLineIntersectVehicle = physics.lineIntersectVehicle
local physicsLineIntersectVehicleQuick = physics.lineIntersectVehicleQuick
local physicsLineIntersectTriangle = physics.lineIntersectTriangle
local json = require 'main.json'
local PROFILES_FILE = 'sandbox-profiles.json'
local profiles = {}
local profilesDirty = false
function util.saveProfiles ()
local f = io.open(PROFILES_FILE, 'w')
if f then
f:write(json.encode(profiles))
f:close()
mode:print('Profiles saved')
end
end
function util.setProfilesDirty ()
profilesDirty = true
end
function util.saveProfilesIfDirty ()
if profilesDirty then
util.saveProfiles()
profilesDirty = false
end
end
function util.loadProfiles ()
local f = io.open(PROFILES_FILE, 'r')
if f then
local data = json.decode(f:read('*all'))
profiles = data
f:close()
mode:print('Profiles loaded')
end
end
---@param phone integer
---@param friendPhone integer
---@return boolean hasFriendAdded
function util.hasFriendAdded (phone, friendPhone)
local key = tostring(phone)
local profile = profiles[key]
if not profile then return false end
local friends = profile.friends
if not friends then return false end
return friends[tostring(friendPhone)] and true or false
end
---@param aPhone integer
---@param bPhone integer
---@return boolean areMutualFriends
function util.areMutualFriends (aPhone, bPhone)
return util.hasFriendAdded(aPhone, bPhone) and util.hasFriendAdded(bPhone, aPhone)
end
---@param ply Player
---@return table? profile
function util.getProfile (ply)
if ply.isBot then
return nil
end
local key = tostring(ply.phoneNumber)
if not profiles[key] then
profiles[key] = {
createdAt = os.time()
}
end
profiles[key].name = ply.name
return profiles[key]
end
---@param kind string
---@param pos Vector
---@param pitchScale? number
---@param volumeScale? number
function util.sound (kind, pos, pitchScale, volumeScale)
pitchScale = pitchScale or 1.0
volumeScale = volumeScale or 1.0
if kind == 'snap' then
-- Magazine
events.createSound(39, pos, 0.75 * volumeScale, 2.0 * pitchScale)
elseif kind == 'general' then
-- Bullet casing
events.createSound(40, pos, 0.75 * volumeScale, 2.0 * pitchScale)
elseif kind == 'error' then
-- Gear shift
events.createSound(41, pos, 0.75 * volumeScale, 2.0 * pitchScale)
end
end
---@param ply Player
---@param action table
function util.addUndoAction (ply, action)
local data = ply.data
if not data.sandboxUndoQueue then
data.sandboxUndoQueue = {}
end
local queue = data.sandboxUndoQueue
action.time = osRealClock()
table.insert(queue, action)
while #queue > 255 do
table.remove(queue, 1)
end
end
---Erase all undos where key = value.
---@param key string
---@param value? any
function util.eraseUndosWhere (key, value)
for _, ply in ipairs(playersGetAll()) do
local queue = ply.data.sandboxUndoQueue
if queue and #queue > 0 then
for i = #queue, 1, -1 do
if queue[i][key] == value then
table.remove(queue, i)
end
end
end
end
end
---Erase all undos associated with an object.
---@param obj Human|Item|Vehicle
function util.eraseObjectBodyUndos (obj)
if obj.class == 'Human' then
for i = 0, 15 do
local body = obj:getRigidBody(i)
util.eraseUndosWhere('body', body)
end
else
util.eraseUndosWhere('body', obj.rigidBody)
end
end
---@param body RigidBody
---@return integer numBonds
function util.countBodyBonds (body)
local numBonds = 0
for _, bond in ipairs(bondsGetAll()) do
if bond.body == body or ((bond.type == 7 or bond.type == 8) and bond.otherBody == body) then
numBonds = numBonds + 1
end
end
return numBonds
end
---Clear all bonds with associated bodies that no longer exist.
---@return integer numRemoved
function util.clearOrphanedBonds ()
local numRemoved = 0
for _, bond in ipairs(bondsGetAll()) do
if not bond.body.isActive or ((bond.type == 7 or bond.type == 8) and not bond.otherBody.isActive) then
bond.isActive = false
util.eraseUndosWhere('bond', bond)
numRemoved = numRemoved + 1
end
end
return numRemoved
end
---Clear all bot players that don't have a human.
---@return integer numRemoved
function util.clearOrphanedBotPlayers ()
local numRemoved = 0
for _, ply in ipairs(playersGetAll()) do
if ply.isBot and not isActive(ply.human) then
ply:remove()
numRemoved = numRemoved + 1
end
end
return numRemoved
end
---Clear objects associated with a phone number.
---@param phone integer
---@param objectTable any[]
---@return integer numRemoved
function util.clearObjects (phone, objectTable)
local numRemoved = 0
for _, obj in ipairs(objectTable) do
if obj.data.sandboxCreatorPhone == phone then
numRemoved = numRemoved + 1
util.eraseObjectBodyUndos(obj)
obj:remove()
util.eraseUndosWhere('obj', obj)
end
end
if numRemoved > 0 then
util.clearOrphanedBonds()
end
return numRemoved
end
---Clear bots associated with a phone number.
---@param phone integer
---@return integer numRemoved
function util.clearBots (phone)
local numRemoved = 0
for _, man in ipairs(humansGetAll()) do
if man.data.sandboxCreatorPhone == phone then
local ply = man.player
if not ply or ply.isBot then
numRemoved = numRemoved + 1
util.eraseObjectBodyUndos(man)
man:remove()
util.eraseUndosWhere('obj', man)
end
end
end
if numRemoved > 0 then
util.clearOrphanedBonds()
util.clearOrphanedBotPlayers()
end
return numRemoved
end
---Check if a line intersects a quad on a box with a normal.
---@param outPos Vector The vector to be changed to the intersection position.
---@param pos Vector The origin of the box.
---@param normal Vector The normal of the quad.
---@param distX number
---@param distY number
---@param distZ number
---@param posA Vector The first point of the line.
---@param posB Vector The second point of the line.
---@return number? fraction
local function intersectBoxQuad(outPos, pos, normal, distX, distY, distZ, posA, posB)
local vert0 = pos - distX + distY - distZ
local vert1 = pos + distX + distY - distZ
local vert2 = pos - distX + distY + distZ
local vert3 = pos + distX + distY + distZ
local fraction = physicsLineIntersectTriangle(
outPos,
normal,
posA, posB,
vert0, vert1, vert2
)
if fraction then return fraction end
return physicsLineIntersectTriangle(
outPos,
normal,
posA, posB,
vert3, vert2, vert1
)
end
---Check if a line intersects an item's bounding box.
---@param item Item
---@param posA Vector The first point of the line.
---@param posB Vector The second point of the line.
---@return number? fraction
---@return Vector? pos
---@return Vector? normal
local function intersectItem(item, posA, posB)
local center = item.type.boundsCenter
local pos = item.pos
local rot = item.rot
local normalX = Vector(rot.x1, rot.y1, rot.z1)
local normalY = Vector(rot.x2, rot.y2, rot.z2)
local normalZ = Vector(rot.x3, rot.y3, rot.z3)
local distX = normalX * center.x
local distY = normalY * center.y
local distZ = normalZ * center.z
local outPos = Vector()
local fraction = intersectBoxQuad(outPos, pos, normalX, -distY, distX, distZ, posA, posB)
if fraction then return fraction, outPos, normalX end
fraction = intersectBoxQuad(outPos, pos, -normalX, -distY, -distX, -distZ, posA, posB)
if fraction then return fraction, outPos, -normalX end
fraction = intersectBoxQuad(outPos, pos, normalY, distX, distY, distZ, posA, posB)
if fraction then return fraction, outPos, normalY end
fraction = intersectBoxQuad(outPos, pos, -normalY, -distX, -distY, distZ, posA, posB)
if fraction then return fraction, outPos, -normalY end
fraction = intersectBoxQuad(outPos, pos, normalZ, distX, distZ, -distY, posA, posB)
if fraction then return fraction, outPos, normalZ end
fraction = intersectBoxQuad(outPos, pos, -normalZ, distX, -distZ, distY, posA, posB)
if fraction then return fraction, outPos, -normalZ end
end
---Check if a line intersects an item's bounding box quickly.
---@param item Item
---@param posA Vector The first point of the line.
---@param posB Vector The second point of the line.
---@return number? fraction
local function intersectItemQuick(item, posA, posB)
local pos = item.pos
if (
posA.x - 4 > pos.x
and posB.x - 4 > pos.x
)
or (
posA.y - 4 > pos.y
and posB.y - 4 > pos.y
)
or (
posA.z - 4 > pos.z
and posB.z - 4 > pos.z
)
or (
posA.x + 4 < pos.x
and posB.x + 4 < pos.x
)
or (
posA.y + 4 < pos.y
and posB.y + 4 < pos.y
)
or (
posA.z + 4 < pos.z
and posB.z + 4 < pos.z
) then
return nil
end
local center = item.type.boundsCenter
local rot = item.rot
local normalX = Vector(rot.x1, rot.y1, rot.z1)
local normalY = Vector(rot.x2, rot.y2, rot.z2)
local normalZ = Vector(rot.x3, rot.y3, rot.z3)
local distX = normalX * center.x
local distY = normalY * center.y
local distZ = normalZ * center.z
local outPos = Vector()
local fraction = intersectBoxQuad(outPos, pos, normalX, -distY, distX, distZ, posA, posB)
if fraction then return fraction end
fraction = intersectBoxQuad(outPos, pos, -normalX, -distY, -distX, -distZ, posA, posB)
if fraction then return fraction end
fraction = intersectBoxQuad(outPos, pos, normalY, distX, distY, distZ, posA, posB)
if fraction then return fraction end
fraction = intersectBoxQuad(outPos, pos, -normalY, -distX, -distY, distZ, posA, posB)
if fraction then return fraction end
fraction = intersectBoxQuad(outPos, pos, normalZ, distX, distZ, -distY, posA, posB)
if fraction then return fraction end
fraction = intersectBoxQuad(outPos, pos, -normalZ, distX, -distZ, distY, posA, posB)
return fraction
end
---Find the nearest thing that a ray hits.
---@param man? Human
---@param posA Vector
---@param posB Vector
---@return any?
function util.lineIntersectAllQuick (man, posA, posB)
local fraction = 4096
local object
do
local frac = physicsLineIntersectLevelQuick(posA, posB, false)
if frac and frac < fraction then
fraction = frac
end
end
for _, human in ipairs(humansGetAll()) do
if human ~= man then
local frac = physicsLineIntersectHumanQuick(human, posA, posB, 0.0)
if frac and frac < fraction then
fraction = frac
object = human
end
end
end
for _, vcl in ipairs(vehiclesGetAll()) do
local frac = physicsLineIntersectVehicleQuick(vcl, posA, posB, false)
if frac and frac < fraction then
fraction = frac
object = vcl
end
end
for _, item in ipairs(itemsGetAll()) do
if item.hasPhysics or item.physicsSettled then
local frac = intersectItemQuick(item, posA, posB)
if frac and frac < fraction then
fraction = frac
object = item
end
end
end
return object
end
---Find the nearest thing that a ray hits.
---@param man? Human
---@param posA Vector
---@param posB Vector
---@param withoutLevel? boolean
---@return table
function util.lineIntersectAll (man, posA, posB, withoutLevel)
local hitRays = {}
local ray
if not withoutLevel then
ray = physicsLineIntersectLevel(posA, posB, false)
if ray.hit then
ray.type = 'level'
table.insert(hitRays, ray)
end
end
for _, human in ipairs(humans.getAll()) do
if human ~= man then
ray = physicsLineIntersectHuman(human, posA, posB, 0.0)
if ray.hit then
ray.obj = human
ray.type = 'human'
table.insert(hitRays, ray)
end
end
end
for _, vcl in ipairs(vehicles.getAll()) do
ray = physicsLineIntersectVehicle(vcl, posA, posB, false)
if ray.hit then
ray.obj = vcl
ray.type = 'vehicle'
table.insert(hitRays, ray)
end
end
for _, item in ipairs(items.getAll()) do
if item.hasPhysics or item.physicsSettled then
local fraction, pos, normal = intersectItem(item, posA, posB)
if fraction then
table.insert(hitRays, {
pos = pos,
normal = normal,
fraction = fraction,
hit = true,
obj = item,
type = 'item'
})
end
end
end
table.sort(hitRays, function(a, b)
return a.fraction < b.fraction
end)
return hitRays[1] or { hit = false }
end
local utilLineIntersectAll = util.lineIntersectAll
---Run a tool function.
---@param tools table[]
---@param functionName string
---@param doRay boolean
---@param ply Player
---@param man Human
function util.invokeTool (tools, functionName, doRay, ply, man)
local data = ply.data
local toolID = data.sandboxTool
if not toolID or toolID == 0 then return end
local tool = tools[toolID]
if tool[functionName] then
local ray = doRay and utilLineIntersectAll(man, getEyeLine(man, 2048)) or nil
tool[functionName](ply, man, data, ray)
end
end
---Abort tool use and call the release function.
---@param tools table[]
---@param man Human
function util.abortTools (tools, man)
local ply = man.player
if ply then
local data = man.data
if data.sandboxLeftDown then
data.sandboxLeftDown = nil
util.invokeTool(tools, 'onLeftUp', true, ply, man)
end
if data.sandboxRightDown then
data.sandboxRightDown = nil
util.invokeTool(tools, 'onRightUp', true, ply, man)
end
if data.sandboxEDown then
data.sandboxEDown = nil
util.invokeTool(tools, 'onEUp', false, ply, man)
end
end
end
---@param ply Player
---@param man Human
---@param owner? Player
function util.alertNotFriend (ply, man, owner)
if owner then
ply:sendMessage(string.format('%s (%s) has not added you as a friend', owner.name, dashPhoneNumber(owner.phoneNumber)))
end
util.sound('error', man.pos)
end
local utilAlertNotFriend = util.alertNotFriend
---@param ply Player
---@param man Human
---@param ownerPhone integer
function util.alertNotFriendInfrequently (ply, man, ownerPhone)
local now = osRealClock()
local data = ply.data
local lastAlertTime = data.sandboxLastAlertTime or 0
if now - lastAlertTime >= 2 then
data.sandboxLastAlertTime = now
utilAlertNotFriend(ply, man, players.getByPhone(ownerPhone))
end
end
return util