185 | function BaleGrab:baleGrabTriggerCallback(triggerId, otherActorId, onEnter, onLeave, onStay, otherShapeId) |
186 | local spec = self.spec_baleGrab |
187 | if onEnter then |
188 | local object = g_currentMission:getNodeObject(otherActorId) |
189 | if object == nil then |
190 | object = g_currentMission.nodeToObject[otherActorId] |
191 | end |
192 | if object ~= nil and object ~= self and object.getSupportsMountDynamic ~= nil and object:getSupportsMountDynamic() then |
193 | spec.pendingDynamicMountObjects[object] = Utils.getNoNil(spec.pendingDynamicMountObjects[object], 0) + 1 |
194 | end |
195 | elseif onLeave then |
196 | local object = g_currentMission:getNodeObject(otherActorId) |
197 | if object == nil then |
198 | object = g_currentMission.nodeToObject[otherActorId] |
199 | end |
200 | if object ~= nil then |
201 | if spec.pendingDynamicMountObjects[object] ~= nil then |
202 | local count = spec.pendingDynamicMountObjects[object]-1 |
203 | if count == 0 then |
204 | spec.pendingDynamicMountObjects[object] = nil |
205 | |
206 | if spec.dynamicMountedObjects[object] ~= nil then |
207 | self:unmountBaleGrabObject(object) |
208 | end |
209 | else |
210 | spec.pendingDynamicMountObjects[object] = count |
211 | end |
212 | end |
213 | end |
214 | end |
215 | end |
21 | function BaleGrab.initSpecialization() |
22 | local schema = Vehicle.xmlSchema |
23 | schema:setXMLSpecializationType("BaleGrab") |
24 | |
25 | schema:register(XMLValueType.NODE_INDEX, "vehicle.baleGrab#triggerNode", "Trigger node") |
26 | schema:register(XMLValueType.NODE_INDEX, "vehicle.baleGrab#rootNode", "Root node") |
27 | schema:register(XMLValueType.NODE_INDEX, "vehicle.baleGrab#jointNode", "Joint node") |
28 | schema:register(XMLValueType.STRING, "vehicle.baleGrab#jointType", "Joint type", "TYPE_AUTO_ATTACH_XYZ") |
29 | schema:register(XMLValueType.FLOAT, "vehicle.baleGrab#forceAcceleration", "Force acceleration", 20) |
30 | schema:register(XMLValueType.INT, "vehicle.baleGrab#grabRefComponentJointIndex1", "Component joint index of grab 1") |
31 | schema:register(XMLValueType.INT, "vehicle.baleGrab#grabRefComponentJointIndex2", "Component joint index of grab 2") |
32 | schema:register(XMLValueType.ANGLE, "vehicle.baleGrab#rotDiffThreshold1", "Rotation difference between component and joint to mount bale", 2) |
33 | schema:register(XMLValueType.ANGLE, "vehicle.baleGrab#rotDiffThreshold2", "Rotation difference between component and joint to mount bale", 2) |
34 | |
35 | schema:setXMLSpecializationType() |
36 | end |
67 | function BaleGrab:onLoad(savegame) |
68 | local spec = self.spec_baleGrab |
69 | |
70 | if self.isServer then |
71 | local dynamicMountAttacherTrigger = {} |
72 | dynamicMountAttacherTrigger.triggerNode = self.xmlFile:getValue("vehicle.baleGrab#triggerNode", nil, self.components, self.i3dMappings) |
73 | dynamicMountAttacherTrigger.rootNode = self.xmlFile:getValue("vehicle.baleGrab#rootNode", nil, self.components, self.i3dMappings) |
74 | dynamicMountAttacherTrigger.jointNode = self.xmlFile:getValue("vehicle.baleGrab#jointNode", nil, self.components, self.i3dMappings) |
75 | |
76 | local attacherJointTypeString = self.xmlFile:getValue("vehicle.baleGrab#jointType", "TYPE_AUTO_ATTACH_XYZ") |
77 | dynamicMountAttacherTrigger.attacherJointType = DynamicMountUtil.TYPE_AUTO_ATTACH_XYZ |
78 | if DynamicMountUtil[attacherJointTypeString] ~= nil then |
79 | dynamicMountAttacherTrigger.attacherJointType = DynamicMountUtil[attacherJointTypeString] |
80 | end |
81 | |
82 | if dynamicMountAttacherTrigger.triggerNode ~= nil and dynamicMountAttacherTrigger.rootNode ~= nil and dynamicMountAttacherTrigger.jointNode ~= nil then |
83 | dynamicMountAttacherTrigger.forceAcceleration = self.xmlFile:getValue("vehicle.baleGrab#forceAcceleration", 20) |
84 | addTrigger(dynamicMountAttacherTrigger.triggerNode, "baleGrabTriggerCallback", self) |
85 | |
86 | local grabRefComponentJointIndex1 = self.xmlFile:getValue("vehicle.baleGrab#grabRefComponentJointIndex1") |
87 | local grabRefComponentJointIndex2 = self.xmlFile:getValue("vehicle.baleGrab#grabRefComponentJointIndex2") |
88 | if grabRefComponentJointIndex1 ~= nil then |
89 | dynamicMountAttacherTrigger.componentJoint1 = self.componentJoints[grabRefComponentJointIndex1+1] |
90 | end |
91 | if grabRefComponentJointIndex2 ~= nil then |
92 | dynamicMountAttacherTrigger.componentJoint2 = self.componentJoints[grabRefComponentJointIndex2+1] |
93 | end |
94 | |
95 | dynamicMountAttacherTrigger.rotDiffThreshold1 = self.xmlFile:getValue("vehicle.baleGrab#rotDiffThreshold1", 2) |
96 | dynamicMountAttacherTrigger.rotDiffThreshold2 = self.xmlFile:getValue("vehicle.baleGrab#rotDiffThreshold2", 2) |
97 | |
98 | dynamicMountAttacherTrigger.cosRotDiffThreshold1 = math.cos(dynamicMountAttacherTrigger.rotDiffThreshold1) |
99 | dynamicMountAttacherTrigger.cosRotDiffThreshold2 = math.cos(dynamicMountAttacherTrigger.rotDiffThreshold2) |
100 | |
101 | spec.dynamicMountAttacherTrigger = dynamicMountAttacherTrigger |
102 | end |
103 | |
104 | spec.dynamicMountedObjects = {} |
105 | spec.pendingDynamicMountObjects = {} |
106 | else |
107 | SpecializationUtil.removeEventListener(self, "onUpdateTick", BaleGrab) |
108 | end |
109 | end |
130 | function BaleGrab:onUpdateTick(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected) |
131 | if self.isServer then |
132 | local spec = self.spec_baleGrab |
133 | local attachTrigger = spec.dynamicMountAttacherTrigger |
134 | |
135 | local isClosed = true |
136 | if attachTrigger.componentJoint1 ~= nil then |
137 | isClosed = self:isComponentJointOutsideLimit(attachTrigger.componentJoint1, attachTrigger.rotDiffThreshold1, attachTrigger.cosRotDiffThreshold1) |
138 | end |
139 | if isClosed and attachTrigger.componentJoint2 ~= nil then |
140 | isClosed = self:isComponentJointOutsideLimit(attachTrigger.componentJoint2, attachTrigger.rotDiffThreshold2, attachTrigger.cosRotDiffThreshold2) |
141 | end |
142 | if isClosed then |
143 | for object,_ in pairs(spec.pendingDynamicMountObjects) do |
144 | if spec.dynamicMountedObjects[object] == nil then |
145 | self:unmountBaleGrabObject(object) |
146 | self:mountBaleGrabObject(object) |
147 | end |
148 | end |
149 | else |
150 | for object,_ in pairs(spec.dynamicMountedObjects) do |
151 | self:unmountBaleGrabObject(object) |
152 | end |
153 | end |
154 | end |
155 | end |
40 | function BaleGrab.registerFunctions(vehicleType) |
41 | SpecializationUtil.registerFunction(vehicleType, "baleGrabTriggerCallback", BaleGrab.baleGrabTriggerCallback) |
42 | SpecializationUtil.registerFunction(vehicleType, "addDynamicMountedObject", BaleGrab.addDynamicMountedObject) |
43 | SpecializationUtil.registerFunction(vehicleType, "removeDynamicMountedObject", BaleGrab.removeDynamicMountedObject) |
44 | SpecializationUtil.registerFunction(vehicleType, "isComponentJointOutsideLimit", BaleGrab.isComponentJointOutsideLimit) |
45 | SpecializationUtil.registerFunction(vehicleType, "mountBaleGrabObject", BaleGrab.mountBaleGrabObject) |
46 | SpecializationUtil.registerFunction(vehicleType, "unmountBaleGrabObject", BaleGrab.unmountBaleGrabObject) |
47 | end |