24 | function DynamicMountAttacher:load(savegame) |
25 | |
26 | self.dynamicMountTriggerCallback = DynamicMountAttacher.dynamicMountTriggerCallback; |
27 | self.addDynamicMountedObject = DynamicMountAttacher.addDynamicMountedObject; |
28 | self.removeDynamicMountedObject = DynamicMountAttacher.removeDynamicMountedObject; |
29 | |
30 | self.getFillLevelInformation = Utils.overwrittenFunction(self.getFillLevelInformation, DynamicMountAttacher.getFillLevelInformation); |
31 | |
32 | -- Allow mountable object to attach them selfs to us |
33 | self.dynamicMountAttacherNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.dynamicMountAttacher#index")); |
34 | self.dynamicMountAttacherForceLimitScale = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.dynamicMountAttacher#forceLimitScale"), 1); |
35 | self.dynamicMountAttacherTimeToMount = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.dynamicMountAttacher#timeToMount"), 1000); |
36 | |
37 | local closedMountType = getXMLString(self.xmlFile, "vehicle.dynamicMountAttacher.grab#closedMountType") |
38 | if closedMountType ~= nil then |
39 | self.dynamicMountAttacherGrab = {}; |
40 | self.dynamicMountAttacherGrab.openMountType = Utils.getNoNil(DynamicMountUtil[getXMLString(self.xmlFile, "vehicle.dynamicMountAttacher.grab#openMountType")], DynamicMountUtil.TYPE_FORK); |
41 | self.dynamicMountAttacherGrab.closedMountType = Utils.getNoNil(DynamicMountUtil[closedMountType], DynamicMountUtil.TYPE_AUTO_ATTACH_XYZ); |
42 | self.dynamicMountAttacherGrab.currentMountType = self.dynamicMountAttacherGrab.openMountType; |
43 | end; |
44 | |
45 | if self.isServer then |
46 | self.dynamicMountCollisionMasks = {}; |
47 | local i=0; |
48 | while true do |
49 | local key = string.format("vehicle.dynamicMountAttacher.mountCollisionMask(%d)", i); |
50 | if not hasXMLProperty(self.xmlFile, key) then |
51 | break; |
52 | end |
53 | local node = Utils.indexToObject(self.components, getXMLString(self.xmlFile, key.."#index")); |
54 | local mask = getXMLInt(self.xmlFile, key.."#collisionMask"); |
55 | if node ~= nil and mask ~= nil then |
56 | table.insert(self.dynamicMountCollisionMasks, {node=node, mountedCollisionMask=mask, unmountedCollisionMask=getCollisionMask(node)}); |
57 | end |
58 | i=i+1; |
59 | end |
60 | |
61 | local attacherTriggerTriggerNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.dynamicMountAttacher#triggerNode")); |
62 | local attacherTriggerRootNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.dynamicMountAttacher#rootNode")); |
63 | local attacherTriggerJointNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.dynamicMountAttacher#jointNode")); |
64 | if attacherTriggerTriggerNode ~= nil and attacherTriggerRootNode ~= nil and attacherTriggerJointNode ~= nil then |
65 | local forceAcceleration = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.dynamicMountAttacher#forceAcceleration"), 30); |
66 | addTrigger(attacherTriggerTriggerNode, "dynamicMountTriggerCallback", self); |
67 | local mountTypeString = Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.dynamicMountAttacher#mountType"), "TYPE_AUTO_ATTACH_XZ"); |
68 | local mountType = Utils.getNoNil(DynamicMountUtil[mountTypeString], DynamicMountUtil.TYPE_AUTO_ATTACH_XZ); |
69 | self.dynamicMountAttacherTrigger = {triggerNode=attacherTriggerTriggerNode, rootNode=attacherTriggerRootNode, jointNode=attacherTriggerJointNode, forceAcceleration=forceAcceleration, mountType = mountType, mountTypeClosed=mountTypeClosed, currentMountType=mountType}; |
70 | end |
71 | self.pendingDynamicMountObjects = {}; |
72 | end |
73 | |
74 | self.dynamicMountedObjects = {}; |
75 | self.dynamicMountedObjectsDirtyFlag = self:getNextDirtyFlag(); |
76 | end; |
143 | function DynamicMountAttacher:updateTick(dt) |
144 | if self.isServer then |
145 | for object,_ in pairs(self.pendingDynamicMountObjects) do |
146 | if self.dynamicMountedObjects[object] == nil then |
147 | if object.lastMoveTime + self.dynamicMountAttacherTimeToMount < g_currentMission.time then |
148 | local doAttach = false; |
149 | if object.components ~= nil then |
150 | if entityExists(object.components[1].node) then |
151 | doAttach = true; |
152 | end; |
153 | end; |
154 | if object.nodeId ~= nil then |
155 | if entityExists(object.nodeId) then |
156 | doAttach = true; |
157 | end; |
158 | end; |
159 | if doAttach then |
160 | local trigger = self.dynamicMountAttacherTrigger; |
161 | if object:mountDynamic(self, trigger.rootNode, trigger.jointNode, trigger.mountType, trigger.forceAcceleration) then |
162 | self:addDynamicMountedObject(object); |
163 | end |
164 | else |
165 | self.pendingDynamicMountObjects[object] = nil; |
166 | end; |
167 | end |
168 | end |
169 | end |
170 | |
171 | if self.dynamicMountAttacherGrab ~= nil and self.shovelOpenNode ~= nil then |
172 | for object,_ in pairs(self.dynamicMountedObjects) do |
173 | local closed = false; |
174 | |
175 | local x,_,_ = getRotation(self.shovelOpenNode); |
176 | if self.shovelOpenDirection > 0 then |
177 | if x < self.shovelOpenRotation then |
178 | closed = true; |
179 | end; |
180 | else |
181 | if x > self.shovelOpenRotation then |
182 | closed = true; |
183 | end |
184 | end |
185 | |
186 | local usedMountType = self.dynamicMountAttacherGrab.openMountType; |
187 | if closed then |
188 | usedMountType = self.dynamicMountAttacherGrab.closedMountType; |
189 | end; |
190 | |
191 | if self.dynamicMountAttacherGrab.currentMountType ~= usedMountType then |
192 | self.dynamicMountAttacherGrab.currentMountType = usedMountType; |
193 | |
194 | local x, y, z = getWorldTranslation(self.dynamicMountAttacherNode); |
195 | setJointPosition(object.dynamicMountJointIndex, 1, x,y,z); |
196 | if usedMountType == DynamicMountUtil.TYPE_FORK then |
197 | |
198 | setJointRotationLimit(object.dynamicMountJointIndex, 0, true, 0, 0); |
199 | setJointRotationLimit(object.dynamicMountJointIndex, 1, true, 0, 0); |
200 | setJointRotationLimit(object.dynamicMountJointIndex, 2, true, 0, 0); |
201 | |
202 | if object.dynamicMountSingleAxisFreeX then |
203 | setJointTranslationLimit(object.dynamicMountJointIndex, 0, false, 0, 0); |
204 | else |
205 | setJointTranslationLimit(object.dynamicMountJointIndex, 0, true, -0.01, 0.01); |
206 | end |
207 | if object.dynamicMountSingleAxisFreeY then |
208 | setJointTranslationLimit(object.dynamicMountJointIndex, 1, false, 0, 0); |
209 | else |
210 | setJointTranslationLimit(object.dynamicMountJointIndex, 1, true, -0.01, 0.01); |
211 | end |
212 | setJointTranslationLimit(object.dynamicMountJointIndex, 2, false, 0, 0); |
213 | else |
214 | setJointRotationLimit(object.dynamicMountJointIndex, 0, true, 0, 0); |
215 | setJointRotationLimit(object.dynamicMountJointIndex, 1, true, 0, 0); |
216 | setJointRotationLimit(object.dynamicMountJointIndex, 2, true, 0, 0); |
217 | |
218 | if usedMountType == DynamicMountUtil.TYPE_AUTO_ATTACH_XYZ or usedMountType == DynamicMountUtil.TYPE_FIX_ATTACH then |
219 | setJointTranslationLimit(object.dynamicMountJointIndex, 0, true, -0.01, 0.01); |
220 | setJointTranslationLimit(object.dynamicMountJointIndex, 1, true, -0.01, 0.01); |
221 | setJointTranslationLimit(object.dynamicMountJointIndex, 2, true, -0.01, 0.01); |
222 | elseif usedMountType == DynamicMountUtil.TYPE_AUTO_ATTACH_XZ then |
223 | setJointTranslationLimit(object.dynamicMountJointIndex, 0, true, -0.01, 0.01); |
224 | setJointTranslationLimit(object.dynamicMountJointIndex, 1, false, 0, 0); |
225 | setJointTranslationLimit(object.dynamicMountJointIndex, 2, true, -0.01, 0.01); |
226 | elseif usedMountType == DynamicMountUtil.TYPE_AUTO_ATTACH_Y then |
227 | setJointTranslationLimit(object.dynamicMountJointIndex, 0, false, 0, 0); |
228 | setJointTranslationLimit(object.dynamicMountJointIndex, 1, true, -0.01, 0.01); |
229 | setJointTranslationLimit(object.dynamicMountJointIndex, 2, false, 0, 0); |
230 | end; |
231 | end; |
232 | end; |
233 | end; |
234 | end; |
235 | end |
236 | end; |
319 | function DynamicMountAttacher:getFillLevelInformation(superFunc, fillLevelInformations) |
320 | if superFunc ~= nil then |
321 | superFunc(self, fillLevelInformations) |
322 | end |
323 | |
324 | for object,_ in pairs(self.dynamicMountedObjects) do |
325 | if object.getFillLevel ~= nil and object.getFillType ~= nil then |
326 | local added = false; |
327 | for _,fillLevelInformation in pairs(fillLevelInformations) do |
328 | if fillLevelInformation.fillType == object:getFillType() then |
329 | fillLevelInformation.fillLevel = fillLevelInformation.fillLevel + object:getFillLevel(); |
330 | if object.getCapacity ~= nil then |
331 | fillLevelInformation.capacity = fillLevelInformation.capacity + object:getCapacity(); |
332 | else |
333 | fillLevelInformation.capacity = fillLevelInformation.capacity + object:getFillLevel(); -- bale |
334 | end |
335 | added = true; |
336 | break; |
337 | end |
338 | end |
339 | if not added then |
340 | if object.getCapacity ~= nil then |
341 | table.insert(fillLevelInformations, {fillType=object:getFillType(), fillLevel=object:getFillLevel(), capacity=object:getCapacity()}); |
342 | else |
343 | table.insert(fillLevelInformations, {fillType=object:getFillType(), fillLevel=object:getFillLevel(), capacity=object:getFillLevel()}); |
344 | end |
345 | end |
346 | end |
347 | end |
348 | end |