-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
HoseSystemPlayerInteractiveHandling.lua
1102 lines (884 loc) · 44.8 KB
/
HoseSystemPlayerInteractiveHandling.lua
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
--
-- HoseSystemPlayerInteractiveHandling
--
-- Authors: Wopster
-- Description: Class to handle all the attach and detach interaction with the hose to player and vehicles
--
-- Copyright (c) Wopster, 2017
HoseSystemPlayerInteractiveHandling = {}
HoseSystemPlayerInteractiveHandling.KINEMATIC_HELPER_TRANS_OFFSET = { -0.4, -0.1, 0.3 }
HoseSystemPlayerInteractiveHandling.JOINT_XZ_ROT_LIMIT = 5 -- deg
HoseSystemPlayerInteractiveHandling.BLINKING_WARNING_TIME = 5000 -- ms
local HoseSystemPlayerInteractiveHandling_mt = Class(HoseSystemPlayerInteractiveHandling, HoseSystemPlayerInteractive)
---
-- @param object
-- @param mt
--
function HoseSystemPlayerInteractiveHandling:new(object, mt)
local interactiveHandling = HoseSystemPlayerInteractive:new(object, mt == nil and HoseSystemPlayerInteractiveHandling_mt or mt)
return interactiveHandling
end
---
--
function HoseSystemPlayerInteractiveHandling:delete()
HoseSystemPlayerInteractiveHandling:superClass().delete(self)
end
---
-- @param dt
--
function HoseSystemPlayerInteractiveHandling:update(dt)
HoseSystemPlayerInteractiveHandling:superClass().update(self, dt)
-- only client sided
if not self.object.isClient then
return
end
if g_currentMission.player ~= nil and g_currentMission.player.hoseSystem ~= nil and g_currentMission.player.hoseSystem.interactiveHandling ~= nil then
local index = g_currentMission.player.hoseSystem.index
local grabPoint = self.object.grabPoints[index]
if grabPoint ~= nil then
if HoseSystem:getIsAttached(grabPoint.state) and grabPoint.isOwned then
g_currentMission:addExtraPrintText(g_i18n:getText('input_mouseInteract'):format(g_i18n:getText('input_mouseInteractMouseRight')) .. ' ' .. g_i18n:getText('action_dropHose'):format(self.object.typeDesc))
if InputBinding.hasEvent(InputBinding.detachHose) then
self:drop(index, grabPoint.currentOwner)
end
if HoseSystemReferences:getHasReferenceInRange(self.object) then
local object = networkGetObject(self.object.vehicleToMountHoseSystem)
if object ~= nil then
local reference = HoseSystemReferences:getReference(object, self.object.referenceIdToMountHoseSystem, grabPoint)
local node = reference ~= nil and reference.node or grabPoint.node
HoseSystemUtil:renderHelpTextOnNode(node, g_i18n:getText('action_attachHose'):format(self.object.typeDesc), g_i18n:getText('input_mouseInteract'):format(g_i18n:getText('input_mouseInteractMouseLeft')))
g_currentMission:enableHudIcon('attach', 1)
if InputBinding.hasEvent(InputBinding.attachHose) then
self:attach(index, object, self.object.referenceIdToMountHoseSystem, self.object.referenceIsExtendable)
end
end
end
end
end
else
-- local index = self:getIsPlayerInGrabPointRange()
if g_currentMission.player ~= nil and g_currentMission.player.hoseSystem ~= nil and g_currentMission.player.hoseSystem.closestIndex ~= nil and g_currentMission.player.hoseSystem.closestHoseSystem == self.object then
local index = g_currentMission.player.hoseSystem.closestIndex
local grabPoint = self.object.grabPoints[index]
if grabPoint ~= nil then
if HoseSystem:getIsDetached(grabPoint.state) then
HoseSystemUtil:renderHelpTextOnNode(grabPoint.node, g_i18n:getText('action_grabHose'):format(self.object.typeDesc), g_i18n:getText('input_mouseInteract'):format(g_i18n:getText('input_mouseInteractMouseLeft')))
g_currentMission:enableHudIcon('attach', 1)
if InputBinding.hasEvent(InputBinding.attachHose) then
self:grab(index, g_currentMission.player)
end
elseif HoseSystem:getIsConnected(grabPoint.state) or HoseSystem:getIsParked(grabPoint.state) then
--
if HoseSystemReferences:getAllowsDetach(self.object, index) then
if grabPoint.hasJointIndex then
HoseSystemUtil:renderHelpTextOnNode(grabPoint.node, g_i18n:getText('action_detachHose'):format(self.object.typeDesc), g_i18n:getText('input_mouseInteract'):format(g_i18n:getText('input_mouseInteractMouseRight')))
if InputBinding.hasEvent(InputBinding.detachHose) then
local vehicle = grabPoint.connectorVehicle
local reference = HoseSystemReferences:getReference(vehicle, grabPoint.connectorRefId, grabPoint)
if reference ~= nil then
local extenable = not reference.parkable and ((reference.connectable ~= nil and reference.connectable) or (grabPoint.connectable ~= nil and grabPoint.connectable))
if reference.hasJointIndex then
vehicle.poly.interactiveHandling:detach(reference.id, self.object, index, extenable)
else
self:detach(index, vehicle, reference.id, extenable)
end
end
end
end
end
end
end
end
end
end
---
--
function HoseSystemPlayerInteractiveHandling:draw()
end
---
-- @param index
-- @param player
-- @param syncState
-- @param noEventSend
--
function HoseSystemPlayerInteractiveHandling:grab(index, player, syncState, noEventSend)
if self.object.grabPoints ~= nil then
if not noEventSend and syncState == nil then
HoseSystemGrabEvent.sendEvent(self.object, index, player, HoseSystemUtil.eventHelper.STATE_CLIENT, noEventSend)
end
local grabPoint = self.object.grabPoints[index]
if grabPoint == nil or player == nil then
return
end
grabPoint.state = HoseSystem.STATE_ATTACHED
if self.object.isServer then
-- But do not event it again since we already getting reloaded by the grab event
self:setGrabPointOwner(index, true, player)
end
if syncState == nil then
player:setWoodWorkVisibility(true, false)
self.object.walkingSpeed = player.walkingSpeed
self.object.runningFactor = player.runningFactor
if self.object.isServer then
self:grab(index, player, HoseSystemUtil.eventHelper.STATE_SERVER, noEventSend)
else
HoseSystemGrabEvent.sendEvent(self.object, index, player, HoseSystemUtil.eventHelper.STATE_SERVER, self.object.isServer)
end
elseif syncState == HoseSystemUtil.eventHelper.STATE_CLIENT then
if player ~= g_currentMission.player then
player:setWoodWorkVisibility(true, false)
end
elseif syncState == HoseSystemUtil.eventHelper.STATE_SERVER then
if self.object.isServer then
player.hoseSystem.mass = self.object.data.generatedMass
player.hoseSystem.kinematicHelper = {
node = clone(player.pickUpKinematicHelper.node, true, false, true)
}
link(player.toolsRootNode, player.hoseSystem.kinematicHelper.node)
setTranslation(player.hoseSystem.kinematicHelper.node, unpack(HoseSystemPlayerInteractiveHandling.KINEMATIC_HELPER_TRANS_OFFSET)) -- fixed location
-- Set kinematicHelper node dependent on player rotation
local angle = HoseSystemUtil:calculateCosAngle(grabPoint.node, player.toolsRootNode, 3)
local y = angle > 0 and math.rad(0) or math.rad(180)
setRotation(player.hoseSystem.kinematicHelper.node, 0, y, 0)
player.hoseSystem.jointIndex = HoseSystemPlayerInteractiveHandling:constructPlayerJoint({
actor1 = player.hoseSystem.kinematicHelper.node,
actor2 = self.object.components[grabPoint.componentIndex].node,
anchor1 = player.hoseSystem.kinematicHelper.node,
anchor2 = grabPoint.node
}, player.hoseSystem)
for i, component in pairs(self.object.components) do
if i ~= grabPoint.componentIndex then
setPairCollision(component.node, player.hoseSystem.kinematicHelper.node, false)
end
end
-- When there is a connected grabPoint we set the limit of the joint so we can move freely without pushing the connector vehicle
for index, gp in pairs(self.object.grabPoints) do
if index ~= grabPoint.id and HoseSystem:getIsConnected(gp.state) then
self:setJointRotAndTransLimit(gp.playerJointRotLimit, gp.playerJointTransLimit)
end
end
end
-- Set collision mask on hose components to disable collision with CCT
-- Since we cannot set non collidable objects for the CCT we just set the collision mask of the object
if grabPoint.componentIndex ~= nil then
setCollisionMask(self.object.components[grabPoint.componentIndex].node, HoseSystem.CCT_COLLISION_MASK)
setCollisionMask(self.object.components[(#self.object.components + 1) / 2].node, HoseSystem.CCT_COLLISION_MASK)
end
end
end
end
---
-- @param index
-- @param player
-- @param syncState
-- @param noEventSend
--
function HoseSystemPlayerInteractiveHandling:drop(index, player, syncState, noEventSend)
if self.object.grabPoints ~= nil then
if not noEventSend and syncState == nil then
HoseSystemDropEvent.sendEvent(self.object, index, player, HoseSystemUtil.eventHelper.STATE_CLIENT, noEventSend)
end
-- if noEventSend == nil or noEventSend == false then
-- if g_server ~= nil then
-- g_server:broadcastEvent(HoseSystemDropEvent:new(self.object, index, connectionEventState, player), nil, nil, self.object)
-- else
-- -- Send drop request to server and return
-- g_client:getServerConnection():sendEvent(HoseSystemDropEvent:new(self.object, index, connectionEventState, player))
--
-- return
-- end
-- end
-- HoseSystemDropEvent.sendEvent(self.object, index, player, HoseSystemUtil.eventHelper.STATE_CLIENT, noEventSend)
local grabPoint = self.object.grabPoints[index]
if grabPoint == nil or player == nil then
return
end
grabPoint.state = HoseSystem.STATE_DETACHED
if self.object.isServer then
self:setGrabPointOwner(index, false, player)
end
if syncState == nil then
player:setWoodWorkVisibility(false, false)
player.walkingSpeed = self.object.walkingSpeed
player.runningFactor = self.object.runningFactor
if self.object.isServer then
self:drop(index, player, HoseSystemUtil.eventHelper.STATE_SERVER, noEventSend)
else
HoseSystemDropEvent.sendEvent(self.object, index, player, HoseSystemUtil.eventHelper.STATE_SERVER, self.object.isServer)
end
elseif syncState == HoseSystemUtil.eventHelper.STATE_CLIENT then
if player ~= g_currentMission.player then
player:setWoodWorkVisibility(false, false)
end
elseif syncState == HoseSystemUtil.eventHelper.STATE_SERVER then
if self.object.isServer then
if player.hoseSystem ~= nil then
if player.hoseSystem.jointIndex ~= 0 then
removeJoint(player.hoseSystem.jointIndex)
end
setTranslation(grabPoint.node, unpack(grabPoint.nodeOrgTrans))
setRotation(grabPoint.node, unpack(grabPoint.nodeOrgRot))
-- Player
if player.hoseSystem.kinematicHelper.node ~= nil then
unlink(player.hoseSystem.kinematicHelper.node)
delete(player.hoseSystem.kinematicHelper.node)
end
player.hoseSystem.jointIndex = 0
player.hoseSystem.kinematicHelper.node = nil
-- set the joint limits back to the state that it only rotates on the connector
for index, gp in pairs(self.object.grabPoints) do
if index ~= grabPoint.id and HoseSystem:getIsConnected(gp.state) then
self:setJointRotAndTransLimit({ HoseSystemPlayerInteractiveHandling.JOINT_XZ_ROT_LIMIT, HoseSystemPlayerInteractiveHandling.JOINT_XZ_ROT_LIMIT, 0 }, { 0, 0, 0 })
end
end
end
end
if grabPoint.componentIndex ~= nil then
setCollisionMask(self.object.components[grabPoint.componentIndex].node, HoseSystem.HOSESYSTEM_COLLISION_MASK)
setCollisionMask(self.object.components[(#self.object.components + 1) / 2].node, HoseSystem.HOSESYSTEM_COLLISION_MASK)
end
end
end
end
---
-- @param index
-- @param vehicle
-- @param referenceId
-- @param isExtendable
-- @param noEventSend
--
function HoseSystemPlayerInteractiveHandling:attach(index, vehicle, referenceId, isExtendable, noEventSend)
if self.object.grabPoints ~= nil then
HoseSystemAttachEvent.sendEvent(self.object, index, vehicle, referenceId, isExtendable, noEventSend)
local grabPoint = self.object.grabPoints[index]
if grabPoint == nil or vehicle == nil then
return
end
local object = HoseSystemReferences:getReferenceVehicle(vehicle)
local reference = HoseSystemReferences:getReference(vehicle, referenceId, grabPoint)
if reference ~= nil then
local grabPoints = { grabPoint }
if self.object.isServer then
local attachedGrabPoint = HoseSystemUtil:getAttachedGrabPoint(self.object.grabPoints, index)
-- get the other grabPoint if it's attached and drop it
if attachedGrabPoint ~= nil and attachedGrabPoint.isOwned then
self:drop(attachedGrabPoint.id, attachedGrabPoint.currentOwner)
end
end
if reference.parkable then
-- handle parked hose
if self.object.data.length <= reference.parkLength then
table.insert(grabPoints, index > 1 and self.object.grabPoints[1] or self.object.grabPoints[#self.object.grabPoints])
if self.object.isServer then
local lastIndex = #grabPoints
if HoseSystem:getIsConnected(grabPoints[lastIndex].state) then
local lastReference = HoseSystemReferences:getReference(grabPoints[lastIndex].connectorVehicle, grabPoints[lastIndex].connectorRefId, grabPoints[lastIndex])
if lastReference ~= nil then
self:detach(grabPoints[lastIndex].id, grabPoints[lastIndex].connectorVehicle, grabPoints[lastIndex].connectorRefId, (lastReference.connectable ~= nil and lastReference.connectable) or (grabPoints[lastIndex].connectable ~= nil and grabPoints[lastIndex].connectable), true)
end
end
if HoseSystem:getIsAttached(grabPoint.state) and grabPoint.isOwned then -- we can't call it on start since we don't want to drop when the parking place is to small.
-- Do this after above else it will fuckup the data..
self:drop(index, grabPoint.currentOwner)
end
end
self:hardParkHose(grabPoints, object, reference)
else
if g_currentMission.player == grabPoint.currentOwner then
g_currentMission:showBlinkingWarning(string.format(g_i18n:getText('info_hoseParkingPlaceToShort'), reference.parkLength, self.object.data.length), HoseSystemPlayerInteractiveHandling.BLINKING_WARNING_TIME)
end
return
end
else
-- handle connected hose
if self.object.isServer then
if HoseSystem:getIsAttached(grabPoint.state) and grabPoint.isOwned then -- we can't call it on start since we don't want to drop when the parking place is to small.
self:drop(index, grabPoint.currentOwner)
end
end
if (isExtendable or grabPoint.connectable) then
-- Set 2 way recognition
reference.connectorRefId = grabPoint.id -- current grabPoint
reference.connectorVehicle = self.object
if grabPoint.connectable then
self.object:toggleLock(index, true) -- close lock
else
object:toggleLock(reference.id, true) -- close lock
end
end
self:hardConnect(grabPoint, object, reference)
end
for _, grabPoint in pairs(grabPoints) do
grabPoint.connectorRefId = referenceId
grabPoint.connectorVehicle = vehicle
if self.object.isServer then
self:setGrabPointIsUsed(grabPoint.id, true, isExtendable, false)
end
end
-- force the mesh updates again
if self.object.isClient then
self.object:updateSpline(true)
self.object.jointSpline.firstRunUpdates = 0
end
end
end
end
---
-- @param index
-- @param vehicle
-- @param referenceId
-- @param isExtendable
-- @param noEventSend
--
function HoseSystemPlayerInteractiveHandling:detach(index, vehicle, referenceId, isExtendable, noEventSend)
if self.object.grabPoints ~= nil then
HoseSystemDetachEvent.sendEvent(self.object, index, vehicle, referenceId, isExtendable, noEventSend)
local grabPoint = self.object.grabPoints[index]
if grabPoint == nil or vehicle == nil then
return
end
local object = HoseSystemReferences:getReferenceVehicle(vehicle)
local reference = HoseSystemReferences:getReference(vehicle, referenceId, grabPoint)
if reference ~= nil then
local grabPoints = { grabPoint }
if self.object.isServer then
local attachedGrabPoint = HoseSystemUtil:getAttachedGrabPoint(self.object.grabPoints, index)
-- get the other grabPoint if it's attached and drop it
if attachedGrabPoint ~= nil and attachedGrabPoint.isOwned then
self:drop(attachedGrabPoint.id, attachedGrabPoint.currentOwner)
end
end
if reference.parkable then
-- handle parked hose
table.insert(grabPoints, index > 1 and self.object.grabPoints[1] or self.object.grabPoints[#self.object.grabPoints])
self:hardUnparkHose(grabPoints, object, reference)
else
-- handle connected hose
if grabPoint.connectable then
self.object:toggleLock(index, false) -- open lock
end
if reference.connectable then
object:toggleLock(reference.id, false) -- open lock
end
self:hardDisconnect(grabPoint, object, reference)
end
for _, grabPoint in pairs(grabPoints) do
if self.object.isServer then
setTranslation(grabPoint.node, unpack(grabPoint.nodeOrgTrans))
setRotation(grabPoint.node, unpack(grabPoint.nodeOrgRot))
self:setGrabPointIsUsed(grabPoint.id, false, isExtendable, false)
end
grabPoint.connectorRefId = 0
grabPoint.connectorVehicle = nil
end
end
end
end
---
-- @param index
-- @param state
-- @param player
-- @param noEventSend
--
function HoseSystemPlayerInteractiveHandling:setGrabPointOwner(index, state, player, noEventSend)
HoseSystemSetOwnerEvent.sendEvent(self.object, index, state, player, noEventSend)
local grabPoint = self.object.grabPoints[index]
grabPoint.isOwned = state
grabPoint.currentOwner = (state and player ~= nil) and player or nil
if player == nil then
return
end
if player.hoseSystem == nil then
player.hoseSystem = {}
end
player.hoseSystem.interactiveHandling = state and self or nil
player.hoseSystem.index = state and index or nil
end
---
-- @param index
-- @param isConnected
-- @param isExtendable
-- @param isCalledFromReference
-- @param noEventSend
--
function HoseSystemPlayerInteractiveHandling:setGrabPointIsUsed(index, isConnected, isExtendable, isCalledFromReference, noEventSend)
if isCalledFromReference == nil then
isCalledFromReference = false
end
-- event
HoseSystemIsUsedEvent.sendEvent(self.object, index, isConnected, isExtendable, isCalledFromReference, noEventSend)
local grabPoint = self.object.grabPoints[index]
if grabPoint ~= nil then
local vehicle = HoseSystemReferences:getReferenceVehicle(grabPoint.connectorVehicle)
grabPoint.state = isConnected and HoseSystem.STATE_CONNECTED or HoseSystem.STATE_DETACHED
grabPoint.hasJointIndex = not isCalledFromReference and isConnected -- tell clients on which grabPoint to call the detach function on
if vehicle ~= nil and not isCalledFromReference then
local reference = HoseSystemReferences:getReference(grabPoint.connectorVehicle, grabPoint.connectorRefId, grabPoint)
-- we have to call on depending objects to tell that we are connecting
if self.object.isServer then
-- call reference functions
if reference.connectable or isExtendable then
vehicle.poly.interactiveHandling:setGrabPointIsUsed(reference.id, isConnected, grabPoint.connectable, true)
else
vehicle:setIsUsed(reference.id, isConnected, isConnected and self.object or nil)
end
end
end
end
end
---
-- @param jointDesc
-- @param playerHoseDesc
--
function HoseSystemPlayerInteractiveHandling:constructPlayerJoint(jointDesc, playerHoseDesc)
local constructor = JointConstructor:new()
constructor:setActors(jointDesc.actor1, jointDesc.actor2)
constructor:setJointTransforms(jointDesc.anchor1, jointDesc.anchor2)
constructor:setEnableCollision(false)
local rotLimitSpring = {}
local rotLimitDamping = {}
local transLimitSpring = {}
local translimitDamping = {}
local springMass = playerHoseDesc.mass * 60
for i = 1, 3 do
rotLimitSpring[i] = springMass
rotLimitDamping[i] = math.sqrt(playerHoseDesc.mass * rotLimitSpring[i]) * 2
transLimitSpring[i] = springMass
translimitDamping[i] = math.sqrt(playerHoseDesc.mass * transLimitSpring[i]) * 2
end
constructor:setRotationLimitSpring(rotLimitSpring[1], rotLimitDamping[1], rotLimitSpring[2], rotLimitDamping[2], rotLimitSpring[3], rotLimitDamping[3])
constructor:setTranslationLimitSpring(transLimitSpring[1], translimitDamping[1], transLimitSpring[2], translimitDamping[1], transLimitSpring[3], translimitDamping[3])
for axis = 0, 2 do
constructor:setRotationLimit(axis, 0, 0)
constructor:setTranslationLimit(axis, true, 0, 0)
end
-- local forceLimit = playerHoseDesc.mass * 25 -- only when stucked behind object
-- constructor:setBreakable(forceLimit, forceLimit)
local jointIndex = constructor:finalize()
-- addJointBreakReport(jointIndex, 'onGrabJointBreak', self)
return jointIndex
end
---
-- @param jointDesc
--
function HoseSystemPlayerInteractiveHandling:constructReferenceJoints(jointDesc)
local constructor = JointConstructor:new()
constructor:setActors(jointDesc.actor1, jointDesc.actor2)
constructor:setJointTransforms(jointDesc.anchor1, jointDesc.anchor2)
constructor:setEnableCollision(false)
local rotLimitSpring = { 0, 0, 0 }
local rotLimitDamping = { 0, 0, 0 }
local transLimitSpring = { 0, 0, 0 }
local translimitDamping = { 0, 0, 0 }
if jointDesc.isConnector then
local connectorMass = getMass(jointDesc.actor1) -- * 100 -- create a strong joint
for i = 1, 3 do
rotLimitSpring[i] = connectorMass
rotLimitDamping[i] = math.sqrt(connectorMass * rotLimitSpring[i]) * 2
transLimitSpring[i] = connectorMass
translimitDamping[i] = math.sqrt(connectorMass * transLimitSpring[i]) * 2
end
end
constructor:setRotationLimitSpring(rotLimitSpring[1], rotLimitDamping[1], rotLimitSpring[2], rotLimitDamping[2], rotLimitSpring[3], rotLimitDamping[3])
constructor:setTranslationLimitSpring(transLimitSpring[1], translimitDamping[1], transLimitSpring[2], translimitDamping[1], transLimitSpring[3], translimitDamping[3])
for axis = 1, 3 do
constructor:setRotationLimit(axis - 1, -jointDesc.rotLimit[axis], jointDesc.rotLimit[axis])
constructor:setTranslationLimit(axis - 1, true, -jointDesc.transLimit[axis], jointDesc.transLimit[axis])
end
return constructor:finalize()
end
---
-- @param rotLimit
-- @param transLimit
--
function HoseSystemPlayerInteractiveHandling:setJointRotAndTransLimit(rotLimit, transLimit)
if not self.object.isServer then
return
end
for i, jointDesc in pairs(self.object.componentJoints) do
if jointDesc.hoseJointIndex ~= nil then
for axis = 1, 3 do
setJointRotationLimit(jointDesc.hoseJointIndex, axis - 1, true, -rotLimit[axis], rotLimit[axis])
setJointTranslationLimit(jointDesc.hoseJointIndex, axis - 1, true, -transLimit[axis], transLimit[axis])
end
end
end
end
---
-- @param jointIndex
-- @param breakingImpulse
--
function HoseSystemPlayerInteractiveHandling:onGrabJointBreak(jointIndex, breakingImpulse)
if jointIndex == g_currentMission.player.hoseSystem.jointIndex then
g_currentMission.player.hoseSystem.interactiveHandling:drop(g_currentMission.player.hoseSystem.index, g_currentMission.player)
end
return false
end
---
-- @param grabPoint
-- @param vehicle
-- @param reference
--
function HoseSystemPlayerInteractiveHandling:hardConnect(grabPoint, vehicle, reference)
-- Note: cause we delete the connector vehicle from physics we have to attach it back later on
local grabPoints = {}
-- Get all the hoses that are connected to a references from the Vehicle
local connectedReferences = HoseSystemUtil:getReferencesWithSingleConnection(vehicle, reference.id)
local connectedHoseSystems = HoseSystemUtil:getConnectedHoseSystems(vehicle)
-- print('connected hoses = ' .. #connectedHoseSystems)
-- print('connected references = ' .. #connectedReferences)
for index, gp in pairs(self.object.grabPoints) do
if gp.id ~= grabPoint.id and HoseSystem:getIsConnected(gp.state) then
table.insert(grabPoints, gp)
end
end
-- print('connected grabPoints = ' .. #grabPoints)
for i, r in pairs(connectedReferences) do
HoseSystemUtil:removeHoseSystemJoint(r.reference)
end
-- we always completely delete the current handled hose
self.object:removeFromPhysics()
if not reference.isObject then
HoseSystemUtil:removeFromPhysicsRecursively(vehicle)
else
removeFromPhysics(vehicle.nodeId)
end
for i, h in pairs(connectedHoseSystems) do
-- h.hoseSystem:removeFromPhysics()
h.hoseSystem.poly.interactiveHandling:hardDisconnect(h.grabPoint, h.vehicle, h.vehicle.grabPoints[h.grabPoint.connectorRefId])
end
setIsCompoundChild(self.object.components[grabPoint.componentIndex].node, true)
local linkComponent = function(vehicle, grabPoint, reference)
local moveDir = grabPoint.id > 1 and -1 or 1
moveDir = (grabPoint.connectable or reference.connectable) and (moveDir > 0 and (reference.id > 1 and 1 or -1) or -1) or moveDir
local direction = { localDirectionToLocal(vehicle.components[grabPoint.componentIndex].node, grabPoint.node, 0, 0, moveDir) }
local upVector = { localDirectionToLocal(vehicle.components[grabPoint.componentIndex].node, grabPoint.node, 0, grabPoint.id > 1 and -1 or 1, 0) }
setDirection(vehicle.components[grabPoint.componentIndex].node, direction[1], direction[2], direction[3], upVector[1], upVector[2], upVector[3])
-- Todo: make offset value dynamic
local translation = { localToLocal(vehicle.components[grabPoint.componentIndex].node, grabPoint.node, 0, 0, (grabPoint.connectable or reference.connectable) and -0.025 or 0) }
setTranslation(vehicle.components[grabPoint.componentIndex].node, unpack(translation))
link(reference.node, vehicle.components[grabPoint.componentIndex].node)
end
linkComponent(self.object, grabPoint, reference)
for i, r in pairs(connectedReferences) do
HoseSystemUtil:createHoseSystemJoint(r.reference)
end
-- Only add the hose to physics partly when not dealing with an extenable hose
if not grabPoint.connectable and not reference.connectable then
self:addToPhysicsParts(grabPoint, grabPoints, vehicle, reference, true)
else
self.object:addToPhysics()
end
-- Add the vehicles back to physics but sync wheels after the joint creation.
if not reference.isObject then
HoseSystemUtil:addToPhysicsRecursively(vehicle)
else
addToPhysics(vehicle.nodeId)
end
for i, h in pairs(connectedHoseSystems) do
h.hoseSystem.poly.interactiveHandling:hardConnect(h.grabPoint, h.vehicle, h.vehicle.grabPoints[h.grabPoint.connectorRefId])
end
if self.object.isServer then
self.object:raiseDirtyFlags(self.object.vehicleDirtyFlag) -- set bit.. we update the server
self.object.forceCompontentUpdate = true
if not reference.isObject then
vehicle:raiseDirtyFlags(vehicle.vehicleDirtyFlag)
end
end
end
---
-- @param grabPoint
-- @param vehicle
-- @param reference
--
function HoseSystemPlayerInteractiveHandling:hardDisconnect(grabPoint, vehicle, reference)
local grabPoints = {}
-- Get all the hoses that are connected to a references from the Vehicle
local connectedReferences = HoseSystemUtil:getReferencesWithSingleConnection(vehicle, reference.id)
for index, gp in pairs(self.object.grabPoints) do
if gp.id ~= grabPoint.id and HoseSystem:getIsConnected(gp.state) then
table.insert(grabPoints, gp)
end
end
self:deleteCustomComponentJoint()
if self.object.isServer then
local removeJoint = function(grabPoint)
if grabPoint.jointIndex ~= nil then
removeJoint(grabPoint.jointIndex)
grabPoint.jointIndex = nil
end
end
removeJoint(grabPoint)
end
for i, r in pairs(connectedReferences) do
HoseSystemUtil:removeHoseSystemJoint(r.reference)
end
self.object:removeFromPhysics()
if not reference.isObject then
HoseSystemUtil:removeFromPhysicsRecursively(vehicle)
else
removeFromPhysics(vehicle.nodeId)
end
setIsCompound(self.object.components[grabPoint.componentIndex].node, true)
-- setIsCompoundChild(self.components[grabPoint.componentIndex].node, true)
-- local translation = {getWorldTranslation(self.components[grabPoint.componentIndex].node)}
-- setTranslation(self.components[grabPoint.componentIndex].node, unpack(translation))
-- local moveDir = grabPoint.id > 1 and -1 or 1
-- moveDir = (grabPoint.connectable or reference.connectable) and (moveDir > 0 and (reference.id > 1 and 1 or -1) or -1) or moveDir
-- local direction = {localDirectionToWorld(grabPoint.node, 0, 0, 1)}
-- local upVector = {localDirectionToWorld(grabPoint.node, 0, 1, 0)}
-- setDirection(self.components[grabPoint.componentIndex].node, direction[1], direction[2], direction[3], upVector[1], upVector[2], upVector[3])
-- link(getRootNode(), self.components[grabPoint.componentIndex].node)
local translation = { getWorldTranslation(self.object.components[grabPoint.componentIndex].node) }
setTranslation(self.object.components[grabPoint.componentIndex].node, unpack(translation))
local direction = { localDirectionToWorld(self.object.rootNode, 0, 0, 1) }
local upVector = { localDirectionToWorld(self.object.rootNode, 0, 1, 0) }
setDirection(self.object.components[grabPoint.componentIndex].node, direction[1], direction[2], direction[3], upVector[1], upVector[2], upVector[3])
link(getRootNode(), self.object.components[grabPoint.componentIndex].node)
if not reference.isObject then
HoseSystemUtil:addToPhysicsRecursively(vehicle)
else
addToPhysics(vehicle.nodeId)
end
for i, r in pairs(connectedReferences) do
HoseSystemUtil:createHoseSystemJoint(r.reference)
end
-- only add the hose partly to physics when there are attached grabPoints
if #grabPoints > 0 and not grabPoint.connectable and not reference.connectable then
self:addToPhysicsParts(grabPoint, grabPoints, vehicle, reference, false)
else
self.object:addToPhysics() -- add it back
end
if self.object.isServer then
self.object:raiseDirtyFlags(self.object.vehicleDirtyFlag)
end
end
---
-- @param grabPoints
-- @param vehicle
-- @param reference
--
function HoseSystemPlayerInteractiveHandling:hardParkHose(grabPoints, vehicle, reference)
if #grabPoints < 2 then
return
end
local connectedReferences = HoseSystemUtil:getReferencesWithSingleConnection(vehicle, reference.id)
for i, r in pairs(connectedReferences) do
HoseSystemUtil:removeHoseSystemJoint(r.reference)
end
if not reference.isObject then
HoseSystemUtil:removeFromPhysicsRecursively(vehicle)
end
-- set the controlling index
local index = grabPoints[1].id
local startTargetNode = createTransformGroup('hoseSystem_startTargetNode')
local centerTargetNode = createTransformGroup('hoseSystem_centerTargetNode')
local endTargetNode = createTransformGroup('hoseSystem_endTargetNode')
link(reference.node, startTargetNode)
link(reference.node, centerTargetNode)
link(reference.node, endTargetNode)
self.object:removeFromPhysics()
-- Move all components to compound childs
setIsCompoundChild(self.object.components[grabPoints[1].componentIndex].node, true)
setIsCompoundChild(self.object.components[(#self.object.components + 1) / 2].node, true)
setIsCompoundChild(self.object.components[grabPoints[2].componentIndex].node, true)
-- Center node needs some dummy data
local parkData = {
leadingIndex = index,
reference = reference,
hoseLength = self.object.data.length
}
HoseSystemPlayerInteractiveHandling:hardParkHoseComponent(parkData, grabPoints[1], startTargetNode, self.object.components[grabPoints[1].componentIndex].node, reference.startTransOffset, reference.startRotOffset)
HoseSystemPlayerInteractiveHandling:hardParkHoseComponent(parkData, { node = self.object.data.centerNode, id = 2 }, centerTargetNode, self.object.components[(#self.object.components + 1) / 2].node, { 0, 0, 0 }, { 0, 0, 0 }, self.object.data.length / 2)
HoseSystemPlayerInteractiveHandling:hardParkHoseComponent(parkData, grabPoints[2], endTargetNode, self.object.components[grabPoints[2].componentIndex].node, reference.endTransOffset, reference.endRotOffset, self.object.data.length)
self.object.data.parkStartTargetNode = startTargetNode
self.object.data.parkCenterTargetNode = centerTargetNode
self.object.data.parkEndTargetNode = endTargetNode
for i, r in pairs(connectedReferences) do
HoseSystemUtil:createHoseSystemJoint(r.reference)
end
if not reference.isObject then
HoseSystemUtil:addToPhysicsRecursively(vehicle)
end
if self.object.isServer then
-- this should force and update on the components
self.object:raiseDirtyFlags(self.object.vehicleDirtyFlag)
self.object.forceCompontentUpdate = true
if not reference.isObject then
vehicle:raiseDirtyFlags(vehicle.vehicleDirtyFlag)
end
end
end
---
-- @param parkData
-- @param grabPoint
-- @param linkNode
-- @param componentNode
-- @param transOffset
-- @param rotOffset
-- @param offset
--
function HoseSystemPlayerInteractiveHandling:hardParkHoseComponent(parkData, grabPoint, linkNode, componentNode, transOffset, rotOffset, offset)
local reference = parkData.reference
local referenceTranslation = HoseSystemUtil:getOffsetTargetTranslation(reference.node, reference.offsetDirection, offset)
local xRotOffset, yRotOffset, zRotOffset = 0, 0, 0
local xOffset, yOffset, zOffset = 0, 0, 0
-- make offset depending the hoseLength
if parkData.hoseLength >= reference.offsetThreshold then
xRotOffset, yRotOffset, zRotOffset = HoseSystemUtil:getOffsetTargetRotation(parkData.leadingIndex, unpack(rotOffset))
xOffset, yOffset, zOffset = unpack(transOffset)
end
local referenceRotation = { localRotationToLocal(componentNode, grabPoint.node, xRotOffset, yRotOffset, zRotOffset) }
referenceRotation[2] = HoseSystemUtil:processTargetYRotation(parkData.leadingIndex, reference.offsetDirection, referenceRotation[2])
local direction = { localDirectionToLocal(componentNode, grabPoint.node, 0, 0, grabPoint.id > 1 and 1 or -1) } -- grabPoints.id > 1 and 1 or -1 grabPoints.id > 1 and 1 or -1
local upVector = { localDirectionToLocal(componentNode, grabPoint.node, 0, 1, 0) } -- grabPoints.id > 1 and -1 or 1
setDirection(componentNode, direction[1], direction[2], direction[3], upVector[1], upVector[2], upVector[3])
local translation = { localToLocal(componentNode, grabPoint.node, xOffset, yOffset, zOffset) }
setTranslation(componentNode, unpack(translation))
setRotation(componentNode, unpack(referenceRotation))
setWorldTranslation(linkNode, unpack(referenceTranslation))
link(linkNode, componentNode)
end
---
-- @param grabPoints
-- @param vehicle
-- @param reference
--
function HoseSystemPlayerInteractiveHandling:hardUnparkHose(grabPoints, vehicle, reference)
if #grabPoints < 2 then
return
end
local connectedReferences = HoseSystemUtil:getReferencesWithSingleConnection(vehicle, reference.id)
for i, r in pairs(connectedReferences) do
HoseSystemUtil:removeHoseSystemJoint(r.reference)
end
if not reference.isObject then
HoseSystemUtil:removeFromPhysicsRecursively(vehicle)
end
self.object:removeFromPhysics()
setIsCompound(self.object.components[grabPoints[1].componentIndex].node, true)
setIsCompound(self.object.components[(#self.object.components + 1) / 2].node, true)
setIsCompound(self.object.components[grabPoints[2].componentIndex].node, true)
HoseSystemPlayerInteractiveHandling:hardUnparkHoseComponent(grabPoints[1].node, self.object.components[grabPoints[1].componentIndex].node)
HoseSystemPlayerInteractiveHandling:hardUnparkHoseComponent(self.object.data.centerNode, self.object.components[(#self.object.components + 1) / 2].node)
HoseSystemPlayerInteractiveHandling:hardUnparkHoseComponent(grabPoints[2].node, self.object.components[grabPoints[2].componentIndex].node)
HoseSystemUtil:safeDeleteNode(self.object.data.parkStartTargetNode)
HoseSystemUtil:safeDeleteNode(self.object.data.parkCenterTargetNode)
HoseSystemUtil:safeDeleteNode(self.object.data.parkEndTargetNode)
if not reference.isObject then
HoseSystemUtil:addToPhysicsRecursively(vehicle)
end
for i, r in pairs(connectedReferences) do
HoseSystemUtil:createHoseSystemJoint(r.reference)
end
self.object:addToPhysics()
if self.object.isServer then
self.object:raiseDirtyFlags(self.object.vehicleDirtyFlag)
end
end
---
-- @param grabPointNode
-- @param componentNode
--
function HoseSystemPlayerInteractiveHandling:hardUnparkHoseComponent(grabPointNode, componentNode)
local translation = { getWorldTranslation(componentNode) }
local direction = { localDirectionToWorld(grabPointNode, 0, 0, 1) }
local upVector = { localDirectionToWorld(grabPointNode, 0, 1, 0) }
setTranslation(componentNode, unpack(translation))
setDirection(componentNode, direction[1], direction[2], direction[3], upVector[1], upVector[2], upVector[3])
link(getRootNode(), componentNode)
end
---
-- @param grabPoint
-- @param connectedGrabPoints
-- @param vehicle
-- @param reference
-- @param isConnecting
--
function HoseSystemPlayerInteractiveHandling:addToPhysicsParts(grabPoint, connectedGrabPoints, vehicle, reference, isConnecting)