LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

BaleGrab

Description
Specialization for a balegrab tool adding soft attaching of bales
Functions

addDynamicMountedObject

Description
Add dynamic mount object
Definition
addDynamicMountedObject(table object)
Arguments
tableobjectobject
Code
160function BaleGrab:addDynamicMountedObject(object)
161 local spec = self.spec_baleGrab
162 spec.dynamicMountedObjects[object] = object
163end

addNodeObjectMapping

Description
Definition
addNodeObjectMapping()
Code
260function BaleGrab:addNodeObjectMapping(superFunc, list)
261 superFunc(self, list)
262
263 local spec = self.spec_baleGrab
264 if spec.dynamicMountAttacherTrigger ~= nil and spec.dynamicMountAttacherTrigger.triggerNode ~= nil then
265 list[spec.dynamicMountAttacherTrigger.triggerNode] = self
266 end
267end

baleGrabTriggerCallback

Description
Trigger callback
Definition
baleGrabTriggerCallback(integer triggerId, integer otherActorId, boolean onEnter, boolean onLeave, boolean onStay, integer otherShapeId)
Arguments
integertriggerIdid of trigger
integerotherActorIdid of other actor
booleanonEnteron enter
booleanonLeaveon leave
booleanonStayon stay
integerotherShapeIdid of other shape
Code
185function 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
215end

initSpecialization

Description
Definition
initSpecialization()
Code
21function 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()
36end

isComponentJointOutsideLimit

Description
Returns if component joint is outside the rotation limit
Definition
isComponentJointOutsideLimit(integer componentJoint, float maxRot, float cosMaxRot)
Arguments
integercomponentJointindex of component joint
floatmaxRotmax rotation
floatcosMaxRotcos max rotation
Return Values
booleanisOutsideis outside the rotation limit
Code
223function BaleGrab:isComponentJointOutsideLimit(componentJoint, maxRot, cosMaxRot)
224 local x,_,z = localDirectionToLocal(self.components[componentJoint.componentIndices[2]].node, componentJoint.jointNode, 0,0,1)
225 if (x >= 0) == (maxRot >= 0) then
226 if z <= cosMaxRot*math.sqrt(x*x + z*z) then
227 return true
228 end
229 end
230 return false
231end

mountBaleGrabObject

Description
Mounts a dynamic object to the bale grab
Definition
mountBaleGrabObject(table object)
Arguments
tableobjectobject
Return Values
booleansuccesssuccess
Code
237function BaleGrab:mountBaleGrabObject(object)
238 local dynamicMountData = self.spec_baleGrab.dynamicMountAttacherTrigger
239 if object:mountDynamic(self, dynamicMountData.rootNode, dynamicMountData.jointNode, dynamicMountData.attacherJointType, dynamicMountData.forceAcceleration) then
240 self:addDynamicMountedObject(object)
241 return true
242 end
243
244 return false
245end

onDelete

Description
Called on deleting
Definition
onDelete()
Code
113function BaleGrab:onDelete()
114 local spec = self.spec_baleGrab
115 if self.isServer and spec.dynamicMountedObjects ~= nil then
116 for object,_ in pairs(spec.dynamicMountedObjects) do
117 self:unmountBaleGrabObject(object)
118 end
119 end
120 if spec.dynamicMountAttacherTrigger ~= nil then
121 removeTrigger(spec.dynamicMountAttacherTrigger.triggerNode)
122 end
123end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
67function 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
109end

onUpdateTick

Description
Called on update tick
Definition
onUpdateTick(float dt, boolean isActiveForInput, boolean isSelected)
Arguments
floatdttime since last call in ms
booleanisActiveForInputtrue if vehicle is active for input
booleanisSelectedtrue if vehicle is selected
Code
130function 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
155end

prerequisitesPresent

Description
Definition
prerequisitesPresent()
Code
15function BaleGrab.prerequisitesPresent(specializations)
16 return true
17end

registerEventListeners

Description
Definition
registerEventListeners()
Code
58function BaleGrab.registerEventListeners(vehicleType)
59 SpecializationUtil.registerEventListener(vehicleType, "onLoad", BaleGrab)
60 SpecializationUtil.registerEventListener(vehicleType, "onDelete", BaleGrab)
61 SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", BaleGrab)
62end

registerFunctions

Description
Definition
registerFunctions()
Code
40function 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)
47end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
51function BaleGrab.registerOverwrittenFunctions(vehicleType)
52 SpecializationUtil.registerOverwrittenFunction(vehicleType, "addNodeObjectMapping", BaleGrab.addNodeObjectMapping)
53 SpecializationUtil.registerOverwrittenFunction(vehicleType, "removeNodeObjectMapping", BaleGrab.removeNodeObjectMapping)
54end

removeDynamicMountedObject

Description
Remove dynamic mount object
Definition
removeDynamicMountedObject(table object, boolean isDeleting)
Arguments
tableobjectobject
booleanisDeletingis deleting
Code
169function BaleGrab:removeDynamicMountedObject(object, isDeleting)
170 local spec = self.spec_baleGrab
171 spec.dynamicMountedObjects[object] = nil
172 if isDeleting then
173 spec.pendingDynamicMountObjects[object] = nil
174 end
175end

removeNodeObjectMapping

Description
Definition
removeNodeObjectMapping()
Code
271function BaleGrab:removeNodeObjectMapping(superFunc, list)
272 superFunc(self, list)
273
274 local spec = self.spec_baleGrab
275 if spec.dynamicMountAttacherTrigger ~= nil and spec.dynamicMountAttacherTrigger.triggerNode ~= nil then
276 list[spec.dynamicMountAttacherTrigger.triggerNode] = nil
277 end
278end

unmountBaleGrabObject

Description
Unmounts a dynamic object from the bale grab
Definition
unmountBaleGrabObject(table object)
Arguments
tableobjectobject
Return Values
booleansuccesssuccess
Code
251function BaleGrab:unmountBaleGrabObject(object)
252 self:removeDynamicMountedObject(object, false)
253 object:unmountDynamic()
254
255 return true
256end