-
Notifications
You must be signed in to change notification settings - Fork 0
/
door_maker.txt
1319 lines (1116 loc) · 41.3 KB
/
door_maker.txt
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--@name Door Maker
--@author legokidlogan
--@shared
--@include lkl/input_generator.txt
--@include lkl/sv_dosound.txt
--@include lkl/e2_applytorque.txt
--@include lkl/cl_check_permissions.txt
--@include lkl/cl_dosound.txt
local mRand = math.rand
if CLIENT then
permissions = {
"bass.loadFile",
"bass.loadURL",
"bass.play2D",
}
permissionSatisfied = false
permissionRequestSent = false
soundTbl = {}
setupPermissionRequest( permissions, "Hear custom door sounds", true )
require( "lkl/cl_check_permissions.txt" )
require( "lkl/cl_dosound.txt" )
checkPermissions()
timer.simple( 1, checkPermissions )
return
end
local doorConfig = {}
local doorConfigDefault
local tableInsert = table.insert
-- Config:
-- Each chip will likely have wildly different door configs to handle a specific setup and design.
-- Example door:
tableInsert( doorConfig, {
IsHinge = false, -- false = sliding door, true = rotating 'hinge' door
DontParent = false, -- Use setPos and setAng, but don't parent the door. ONLY WORKS IF THE BASE NEVER MOVES.
OpenTime = 0.75, -- Time it takes to open, in seconds.
CloseTime = 0.25, -- Time it takes to close, in seconds.
-- Sound parameters:
Category = "MetalDoor", -- Which sound category this door should use.
SoundOverride = {}, -- Allows you to override specific parameters of each sound used by the door, such as making a huge door with a lower pitch and longer duration than normal.
DirSound = false, -- Should the sounds be directional? Makes whoever pushed the button able to hear the door sound coming from the door's location, even far away. This requires a Button__X or a WhoPressed__X input, where X corresponds to this door's spot in the config/wire list.
DirSoundDist = 400, -- If DirSound is enabled, the door sound will be played this many units away from the button if defined, and if whoPressed is not defined.
DirSoundDistPly = 200, -- If DirSound is enabled, the door sound will be played this many units away from the player's head if whoPressed is defined.
FinishSounds = true, -- Should ____Finished sounds be played on doors/buttons when a door finishes opening/closing?
ClearSounds = true, -- Should sounds from a door/button be cleared when it is about to move/change direction?
-- Sliding-only parameters:
IsPhysical = false, -- Should the door retain collisions while moving? Makes things less reliable, but can crush/launch/block stuff while moving. Door will not be parented.
OpenDir = Vector( 1, 0, 0 ), -- Which direction to move the door to open it, in local coordinates. This will be converted to a unit-vector for convenience.
OpenMult = 1, -- How far the door should move, relative to the size of its model in the direction of motion. (e.g. a value of 1 will move by the entire length of the door)
-- Hinge-only parameters:
HingeAxis = Vector( 0, 0, 1 ), -- The axis to rotate around, in local coordinates.
HingePos = Vector( 0, 1, 0 ), -- The position of the rotational axis, in local coordinates scaled by half the door's size. (e.g. a value of 1 goes to the edge of the model)
Rotate = 90, -- How many degrees to rotate the door by. Negative values will rotate in the opposite direction, similarly to doing -1 * HingeAxis.
} )
doorConfigDefault = doorConfig[1] -- Stores the example door to use as a fallback in case you wire in more doors than you have config entries for.
doorConfig[1] = nil -- This is simply to remove the example door, so we can still have colored formatting for ease of reading. Put your door configs below.
tableInsert( doorConfig, {
IsHinge = false,
OpenTime = 0.75,
CloseTime = 0.25,
-- Sound parameters:
Category = "MetalDoor",
SoundOverride = {},
DirSound = false,
DirSoundDist = 400,
DirSoundDistPly = 200,
FinishSounds = true,
ClearSounds = true,
-- Sliding-only parameters:
IsPhysical = false,
OpenDir = Vector( 1, 0, 0 ),
OpenMult = 1,
-- Hinge-only parameters:
HingeAxis = Vector( 0, 0, 1 ),
HingePos = Vector( 0, 1, 0 ),
Rotate = 90,
} )
local defaultAlert = false -- Should you be alerted if a new door is added without having its own config entry?
local doorInterval = 25 -- Update rate for the doors, in milliseconds. A smaller number means smoother doors, but more CPU usage and server lag. Min value is 15, as servers with a tickrate of 66 cannot go faster.
local psvStrength = 100 -- Strength of the motion used for sliding doors with IsPhysical == true
torqueStrength = 120 -- Strength of the force which rotates sliding doors with IsPhysical == true to keep them properly aligned
torqueStabilization = 25 -- Stabilizes the rotation force to prevent feedback loops
--[[
- soundTbl lets you define various categories of doors, each with a different collection of sounds.
- Each paramter can be a raw value or a function, which will receive the arguments (ent, soundType), except for Duration which receives (ent, soundType, path, pitch). Useful for randomness and dynamic sounds.
- MetalDoor is the default sound category. If another category is missing a sound event (such as ButtonOpen), it will pull from MetalDoor.
--]]
soundTbl = {
-- Categories ideal for sliding doors:
MetalDoor = {
Open = {
Path = "doors/heavy_metal_move1.wav",
Duration = 2.328,
Volume = 1,
Pitch = 1,
Level = nil, -- Defaults to 75
Delay = nil,
},
Close = {
Path = "doors/garage_stop1.wav",
Duration = 1.483,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
ButtonOpen = {
Path = "buttons/combine_button5.wav",
Duration = 1.047,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
ButtonClose = {
Path = "buttons/combine_button7.wav",
Duration = 0.227,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
--[[OpenFinished = {
Path = "physics/metal/bts5_panels_impact_sm_02.wav",
Duration = 0.9,
Volume = 0.7,
Pitch = 0.95,
Level = nil,
Delay = nil,
},--]]
OpenFinished = {
Path = "physics/metal/metal_canister_impact_soft2.wav",
Duration = 0.918,
Volume = 0.4,
Pitch = 0.9,
Level = nil,
Delay = nil,
},
CloseFinished = {
Path = "doors/heavy_metal_stop1.wav",
Duration = 2.043,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
ButtonOpenFinished = {
Path = "doors/heavy_metal_move1.wav",
Duration = 2.328,
Volume = 0.2,
Pitch = 1,
Level = nil,
Delay = nil,
},
ButtonCloseFinished = {
Path = "doors/heavy_metal_stop1.wav",
Duration = 2.043,
Volume = 0.2,
Pitch = 1,
Level = nil,
Delay = nil,
},
},
MetalGears = {
Open = {
Path = "doors/garage_move1.wav",
Duration = 3,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
Close = {
Path = "doors/garage_move1.wav",
Duration = 3,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
OpenFinished = {
Path = "physics/metal/metal_canister_impact_soft2.wav",
Duration = 0.918,
Volume = 0.8,
Pitch = 0.9,
Level = nil,
Delay = nil,
},
CloseFinished = {
Path = "physics/metal/metal_canister_impact_soft2.wav",
Duration = 0.918,
Volume = 0.8,
Pitch = 0.9,
Level = nil,
Delay = nil,
},
},
MetalCorrugated = {
Open = {
Path = "doors/door_metal_thin_move1.wav",
Duration = 1.583,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
Close = {
Path = "doors/door_metal_rusty_move1.wav",
Duration = 1.535,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
OpenFinished = {
Path = "doors/door_metal_thin_open1.wav",
Duration = 0.709,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
CloseFinished = {
Path = "doors/door_metal_thin_close2.wav",
Duration = 0.625,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
},
Curtain = {
Open = {
Path = "physics/plastic/plastic_box_scrape_rough_loop1.wav",
Duration = 2,
Volume = 0.5,
Pitch = 1.1,
Level = 60,
Delay = nil,
},
Close = {
Path = "physics/plastic/plastic_box_scrape_rough_loop1.wav",
Duration = 2,
Volume = 0.5,
Pitch = 1.1,
Level = 60,
Delay = nil,
},
OpenFinished = {
Path = "physics/metal/chain_impact_soft3.wav",
Duration = 0.43,
Volume = 0.2,
Pitch = 0.9,
Level = 60,
Delay = nil,
},
CloseFinished = {
Path = "physics/metal/chain_impact_soft3.wav",
Duration = 0.43,
Volume = 0.2,
Pitch = 0.9,
Level = 60,
Delay = nil,
},
},
-- Categories ideal for hinge doors:
MetalFence = {
Open = {
Path = "physics/metal/metal_chainlink_impact_soft3.wav",
Duration = 0.932,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
Close = {
Path = "physics/metal/metal_chainlink_impact_hard1.wav",
Duration = 0.731,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
OpenFinished = {
Path = "physics/metal/chain_impact_soft3.wav",
Duration = 0.43,
Volume = 0.5,
Pitch = 0.95,
Level = nil,
Delay = nil,
},
CloseFinished = {
Path = "physics/metal/chain_impact_hard1.wav",
Duration = 0.282,
Volume = 0.7,
Pitch = 0.95,
Level = nil,
Delay = nil,
},
},
SciFiCar = {
Open = {
Path = "doors/doormove2.wav",
Duration = 1.384,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
Close = {
Path = "doors/doormove3.wav",
Duration = 1.572,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
OpenFinished = {
Path = "doors/generic_door_close.wav",
Duration = 0.538 / 0.9,
Volume = 0.5,
Pitch = 0.9,
Level = nil,
Delay = nil,
},
CloseFinished = {
Path = "doors/generic_door_close.wav",
Duration = 0.268 / 0.9,
Volume = 0.5,
Pitch = 0.9,
Level = nil,
Delay = nil,
},
},
WoodDoorSoft = {
Open = {
Path = "doors/door1_move.wav",
Duration = 0.81,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
Close = {
Path = "",
},
OpenFinished = {
Path = "",
},
CloseFinished = {
Path = "doors/door1_stop.wav",
Duration = 1.106,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
},
WoodDoorHarsh = {
Open = {
Path = "doors/wood_move1.wav",
Duration = 1.213,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
Close = {
Path = "",
},
OpenFinished = {
Path = "",
},
CloseFinished = {
Path = "doors/wood_stop1.wav",
Duration = 0.445,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
},
Trapdoor = {
Open = {
Path = "doors/door_latch1.wav",
Duration = 0.886,
Volume = 0.6,
Pitch = 1,
Level = nil,
Delay = nil,
},
Close = {
Path = "doors/default_move.wav",
Duration = 0.787,
Volume = 0.5,
Pitch = 1,
Level = nil,
Delay = nil,
},
OpenFinished = {
Path = "doors/door_metal_medium_open1.wav",
Duration = function( _, _, _, pitch ) return 0.703 / ( pitch or 1 ) end,
Volume = 0.5,
Pitch = function() return mRand( 0.95, 1.03 ) end,
Level = nil,
Delay = nil,
},
CloseFinished = {
Path = "doors/door_metal_medium_open1.wav",
Duration = function( _, _, _, pitch ) return 0.703 / ( pitch or 1 ) end,
Volume = 0.5,
Pitch = function() return mRand( 0.95, 1.03 ) end,
Level = nil,
Delay = nil,
},
},
MinecraftWoodDoor = {
Open = {
Path = "https://cdn.discordapp.com/attachments/303869552503291904/934285331442843738/mc_door_open.mp3",
Duration = 0.358,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
Close = {
Path = "https://cdn.discordapp.com/attachments/303869552503291904/934285331644158042/mc_door_close.mp3",
Duration = 0.338,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
OpenFinished = {
Path = "",
},
CloseFinished = {
Path = "",
},
ButtonOpen = {
Path = "",
},
ButtonClose = {
Path = "",
},
ButtonOpenFinished = {
Path = "",
},
ButtonCloseFinished = {
Path = "",
},
},
-- Categories good for both door types:
StoneDoor = {
Open = {
Path = "physics/concrete/concrete_block_scrape_rough_loop1.wav",
Duration = 1.7,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
Close = {
Path = "physics/concrete/boulder_impact_hard3.wav",
Duration = 1.08,
Volume = 1,
Pitch = 1,
Level = nil,
Delay = nil,
},
OpenFinished = {
Path = "physics/concrete/concrete_impact_hard2.wav",
Duration = 0.4,
Volume = 0.8,
Pitch = 0.9,
Level = nil,
Delay = nil,
},
CloseFinished = {
Path = "physics/concrete/rock_impact_hard2.wav",
Duration = 0.237,
Volume = 08,
Pitch = 0.9,
Level = nil,
Delay = nil,
},
},
NoSound = {
Open = {
Path = "",
},
Close = {
Path = "",
},
ButtonOpen = {
Path = "",
},
ButtonClose = {
Path = "",
},
OpenFinished = {
Path = "",
},
CloseFinished = {
Path = "",
},
ButtonOpenFinished = {
Path = "",
},
ButtonCloseFinished = {
Path = "",
},
},
}
-- End Config
projectNameID = "DoorMaker." .. chip():entIndex()
soundCategoryFallback = "MetalDoor"
require( "lkl/sv_dosound.txt" )
require( "lkl/e2_applytorque.txt" )
inputNames = inputNames or {}
inputTypes = inputTypes or {}
inGenRepeatMaxDefault = 20
inGenRepeatNames = inGenRepeatNames or {}
inGenRepeatTypes = inGenRepeatTypes or {}
inGenRepeatGroups = inGenRepeatGroups or {}
inGenRepeatMaxes = inGenRepeatMaxes or {}
tableInsert( inputNames, "StateAll" )
tableInsert( inputTypes, "NUMBER" )
tableInsert( inGenRepeatNames, "State" )
tableInsert( inGenRepeatTypes, "NUMBER" )
tableInsert( inGenRepeatGroups, 1 )
tableInsert( inGenRepeatMaxes, inGenRepeatMaxDefault )
tableInsert( inGenRepeatNames, "Door" )
tableInsert( inGenRepeatTypes, "ENTITY" )
tableInsert( inGenRepeatGroups, 1 )
tableInsert( inGenRepeatMaxes, inGenRepeatMaxDefault )
tableInsert( inGenRepeatNames, "Base" )
tableInsert( inGenRepeatTypes, "ENTITY" )
tableInsert( inGenRepeatGroups, 1 )
tableInsert( inGenRepeatMaxes, inGenRepeatMaxDefault )
tableInsert( inGenRepeatNames, "Button" )
tableInsert( inGenRepeatTypes, "ENTITY" )
tableInsert( inGenRepeatGroups, 1 )
tableInsert( inGenRepeatMaxes, inGenRepeatMaxDefault )
tableInsert( inGenRepeatNames, "WhoPressed" )
tableInsert( inGenRepeatTypes, "ENTITY" )
tableInsert( inGenRepeatGroups, 1 )
tableInsert( inGenRepeatMaxes, inGenRepeatMaxDefault )
tableInsert( inGenRepeatNames, "Children" )
tableInsert( inGenRepeatTypes, "ARRAY" )
tableInsert( inGenRepeatGroups, 1 )
tableInsert( inGenRepeatMaxes, inGenRepeatMaxDefault )
require( "lkl/input_generator.txt" )
wire.adjustInputs( inputNames, inputTypes )
local mMax = math.max
local mAbs = math.abs
local mSign = math.sign
doorInterval = mMax( doorInterval, 15 ) / 1000
local initialized = false
local stateAll = wire.ports.StateAll ~= 0
local doorCount = 0
local ang0 = Angle( 0, 0, 0 )
local vecTiny = Vector( 0.1, 0.1, 0.1 )
local printName = "[" .. projectNameID .. "]"
local chipLink = wire.getWirelink( chip() )
local doorStates = {}
local doorEnts = {}
local baseEnts = {}
local buttonEnts = {}
local whoPressedEnts = {}
local childrenTbls = {}
local doorLookup = {}
local wireEnts = {}
local wireNames = {}
local doorSounds = table.getKeys( soundTbl.MetalDoor )
local buttonSounds = {}
local doorSoundCount = #doorSounds
local buttonSoundCount = 0
for i = doorSoundCount, 1, -1 do
local sound = doorSounds[i]
if string.find( sound, "Button" ) then
table.remove( doorSounds, i )
doorSoundCount = doorSoundCount - 1
buttonSoundCount = buttonSoundCount + 1
buttonSounds[buttonSoundCount] = sound
end
end
local function unparentChildren( ent )
local children = ent.doorChildren
if not children then return end
for _, child in ipairs( children ) do
if isValid( child ) then
child:unparent()
end
end
end
local function parentChildren( ent )
if not isValid( ent ) then return end
local children = ent.doorChildren
if not children then return end
for _, child in ipairs( children ) do
if isValid( child ) then
child:unparent()
child:setParent( ent )
end
end
end
local function createDirHolo( door )
if not isValid( door ) then return end
local holo = door.doorHolo or false
if isValid( holo ) then return holo end
if not hologram.canSpawn() then return false end
pcall( function()
holo = hologram.create( door:getPos(), ang0, "models/holograms/cube.mdl", vecTiny )
end )
door.doorHolo = holo
holo.isHolo = true
return holo
end
local function removeDirHolo( door )
if not isValid( door ) then return end
local holo = door.doorHolo
if not isValid( holo ) then return end
holo:remove()
end
local function moveDirHolo( door )
if not isValid( door ) then return end
local holo = door.doorHolo
if not isValid( holo ) then return end
if not door.doorDirSound then return end
local ply = door.doorWhoPressed
local origin
local dist
if isValid( ply ) and ply:isPlayer() then
origin = ply:getEyePos()
dist = door.doorDirSoundDistPly
else
local button = door.doorButton
if not isValid( button ) then return end
origin = button:getPos()
dist = door.doorDirSoundDist
end
holo:setPos( origin + ( door:getPos() - origin ):getNormalized() * dist )
end
local function onLast()
for i = 1, doorCount do
local ent = doorEnts[i]
if isValid( ent ) then
ent:unparent()
unparentChildren( ent )
end
end
hologram.removeAll()
end
local function relativeRotate( ent, worldPos, worldAng, axis, deg, dontMove )
local locPos, locAng = worldToLocal( ent:getPos(), ent:getAngles(), worldPos, worldAng )
locPos = locPos:rotateAroundAxis( axis, deg )
locAng = locAng:rotateAroundAxis( axis, deg )
local newPos, newAng = localToWorld( locPos, locAng, worldPos, worldAng )
if not dontMove then
local parent = ent:getParent()
if isValid( parent ) then
newPos = parent:worldToLocal( newPos )
--newAng = parent:worldToLocalAngles( newAng )
end
ent:setPos( newPos )
ent:setAngles( newAng )
end
return newPos, newAng
end
local function hingeRotate( ent, hingePos, axis, deg, dontMove )
local worldPos = ent:localToWorld( hingePos )
local worldAng = ent:getAngles()
return relativeRotate( ent, worldPos, worldAng, axis, deg, dontMove )
end
local function runSounds( door, ind, affix )
affix = affix or ""
local state = door.doorState
local doorSound = ( state and "Open" or "Close" ) .. affix
local buttonSound = ( state and "ButtonOpen" or "ButtonClose" ) .. affix
local info = doorConfig[ind] or doorConfigDefault
local category = info.Category
local override = info.SoundOverride or {}
local button = buttonEnts[ind]
local holo = door.doorHolo
doSound( door, doorSound, category, override[doorSound] )
doSound( button, buttonSound, category, override[buttonSound] )
if info.DirSound then
createDirHolo( door )
moveDirHolo( door )
doSound( holo, doorSound, category, override[doorSound] )
end
end
local function slideDoor( door, ind )
if not isValid( door ) or not door.doorMoving then return end
local state = door.doorState
local base = door.doorBase
local step = state and door.doorOpenStep or door.doorCloseStep
local targetPos = state and door.doorOpenPos or door.doorClosePos
local curPos = door:getPos()
local intendedAng = door.doorAng
local physical = door.doorPhysical
local dontParent = door.dontParent
if isValid( base ) then
if physical then
targetPos = base:localToWorld( targetPos )
elseif dontParent then
targetPos = base:localToWorld( targetPos )
else
curPos = base:worldToLocal( curPos ) -- In sf, :setPos() on parented props will place them locally to the parent
end
intendedAng = base:localToWorldAngles( intendedAng )
end
local dist = curPos:getDistance( targetPos )
if dist <= step then
local info = doorConfig[ind] or doorConfigDefault
if physical then
door:enableMotion( false )
end
door.doorMoving = false
door:setPos( targetPos )
door:setAngles( intendedAng )
if info.FinishSounds then
runSounds( door, ind, "Finished" )
end
else
local posChange = ( targetPos - curPos ) * step / dist
if physical then
local physObj = door:getPhysicsObject()
if not isValid( physObj ) then return end
physObj:setVelocity( posChange * psvStrength )
torqueAlign( door, door.doorAng )
else
door:setPos( curPos + posChange )
end
end
end
local function rotateDoor( door, ind )
if not isValid( door ) or not door.doorMoving then return end
local state = door.doorState
local base = door.doorBase
local step = state and door.doorOpenStep or door.doorCloseStep
local targetAng = state and door.doorOpenAng or door.doorCloseAng
local targetPos = state and door.doorOpenPos or door.doorClosePos
local curAng = door:getAngles()
if isValid( base ) then
targetAng = base:localToWorldAngles( targetAng )
--targetPos = base:localToWorld( targetPos ) -- In sf, :setPos() on parented props will place them locally to the parent, so don't globalize this
end
local rotFull = state and door.doorRotFull or 0
local rot = door.doorRot
local rotDist = rotFull - rot
if mAbs( rotDist ) >= 360 then
local rotChange = mSign( rotDist ) * mAbs( step )
targetPos, targetAng = hingeRotate( door, door.doorHingePos, door.doorAxis, step, false )
door.doorRot = rot + rotChange
elseif rotDist ~= 0 then
door.doorRot = rotFull
end
local CQ = curAng:getQuaternion()
local TQ = targetAng:getQuaternion()
local Q = TQ / CQ
local dist = Q:getRotationAngle()
if mAbs( dist ) <= mAbs( step ) and mAbs( rotDist ) <= mAbs( step ) then
local info = doorConfig[ind] or doorConfigDefault
door.doorMoving = false
door.doorRot = rotFull
door:setAngles( targetAng )
door:setPos( targetPos )
if info.FinishSounds then
runSounds( door, ind, "Finished" )
end
else
--hingeRotate( door, door.doorHingePos, door.doorAxis, step )
--hingeRotate( door, door.doorHingePos, door.doorAxis, mSign( dist ) * mMin( mAbs( step ), mAbs( dist ) ) )
--hingeRotate( door, door.doorHingePos, door.doorAxis, step )
--hingeRotate( door, door.doorHingePos, Q:getRotationAxis(), step )
hingeRotate( door, door.doorHingePos, door:worldToLocal( door:getPos() + Q:getRotationAxis() ), mSign( dist ) * mAbs( step ) )
end
end
local function setState( ind, newState )
local door = doorEnts[ind]
if not isValid( door ) then return end
if ( door.doorState or false ) == newState then return end
local info = doorConfig[ind] or doorConfigDefault
local button = buttonEnts[ind]
local holo = door.doorHolo
button = isValid( button ) and button
door.doorState = newState
door.doorWhoPressed = whoPressedEnts[ind]
door.doorMoving = true
if info.ClearSounds then
for i = 1, doorSoundCount do
local sound = doorSounds[i]
stopSound( door, sound )
stopSound( holo, sound )
end
if button then
for i = 1, buttonSoundCount do
stopSound( button, buttonSounds[i] )
end
end
end
if door.doorPhysical then
door:enableMotion( true )
end
runSounds( door, ind )
end
local function revertInput( name, oldLinkEnt, oldLinkName )
local rewired = false
if isValid( oldLinkEnt ) then
rewired = pcall( function()
wire.create( chip(), oldLinkEnt, name, oldLinkName )
end )
end
if not rewired then
wire.delete( chip(), name )
end
end
local function wireInput( name, value )
local baseName, id = inGenGetInfo( name )
local oldLinkEnt = wireEnts[name]
local oldLinkName = wireNames[name]
wireEnts[name] = chipLink:isWired( name ) and chipLink:getWiredTo( name )
wireNames[name] = chipLink:getWiredToName( name )
if id then
if baseName == "State" then
local state = value ~= 0
doorStates[id] = state
setState( id, state or stateAll )
elseif baseName == "Door" then
local door = isValid( value ) and value
local dupInd = doorLookup[door]
if door then
local base = baseEnts[id]
if id > doorCount then
doorCount = id
end
if dupInd and dupInd ~= id then
revertInput( name, oldLinkEnt, oldLinkName )
print(
c_white, printName .. " ",
c_yellow, tostring( door ),
c_red, " is already a door, and cannot be used twice!\n",
c_white, "If you want to give it multiple states/directions/modes of movement, link up a new door entity and set its base to this one."
)
return
end
if door == base then
revertInput( name, oldLinkEnt, oldLinkName )
print(