LUADOC - Farming Simulator 19

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
138function BaleGrab:addDynamicMountedObject(object)
139 local spec = self.spec_baleGrab
140 spec.dynamicMountedObjects[object] = object
141end

addNodeObjectMapping

Description
Definition
addNodeObjectMapping()
Code
214function BaleGrab:addNodeObjectMapping(superFunc, list)
215 superFunc(self, list)
216
217 local spec = self.spec_baleGrab
218 if spec.dynamicMountAttacherTrigger ~= nil and spec.dynamicMountAttacherTrigger.triggerNode ~= nil then
219 list[spec.dynamicMountAttacherTrigger.triggerNode] = self
220 end
221end

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
163function BaleGrab:baleGrabTriggerCallback(triggerId, otherActorId, onEnter, onLeave, onStay, otherShapeId)
164 local spec = self.spec_baleGrab
165 if onEnter then
166 local object = g_currentMission:getNodeObject(otherActorId)
167 if object == nil then
168 object = g_currentMission.nodeToObject[otherActorId]
169 end
170 if object ~= nil and object ~= self and object.getSupportsMountDynamic ~= nil and object:getSupportsMountDynamic() then
171 spec.pendingDynamicMountObjects[object] = Utils.getNoNil(spec.pendingDynamicMountObjects[object], 0) + 1
172 end
173 elseif onLeave then
174 local object = g_currentMission:getNodeObject(otherActorId)
175 if object == nil then
176 object = g_currentMission.nodeToObject[otherActorId]
177 end
178 if object ~= nil then
179 if spec.pendingDynamicMountObjects[object] ~= nil then
180 local count = spec.pendingDynamicMountObjects[object]-1
181 if count == 0 then
182 spec.pendingDynamicMountObjects[object] = nil
183
184 if spec.dynamicMountedObjects[object] ~= nil then
185 self:removeDynamicMountedObject(object, false)
186 object:unmountDynamic()
187 end
188 else
189 spec.pendingDynamicMountObjects[object] = count
190 end
191 end
192 end
193 end
194end

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
202function BaleGrab:isComponentJointOutsideLimit(componentJoint, maxRot, cosMaxRot)
203 local x,_,z = localDirectionToLocal(self.components[componentJoint.componentIndices[2]].node, componentJoint.jointNode, 0,0,1)
204 if (x >= 0) == (maxRot >= 0) then
205 if z <= cosMaxRot*math.sqrt(x*x + z*z) then
206 return true
207 end
208 end
209 return false
210end

onDelete

Description
Called on deleting
Definition
onDelete()
Code
87function BaleGrab:onDelete()
88 local spec = self.spec_baleGrab
89 if self.isServer then
90 for object,_ in pairs(spec.dynamicMountedObjects) do
91 object:unmountDynamic()
92 end
93 end
94 if spec.dynamicMountAttacherTrigger ~= nil then
95 removeTrigger(spec.dynamicMountAttacherTrigger.triggerNode)
96 end
97end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
46function BaleGrab:onLoad(savegame)
47 local spec = self.spec_baleGrab
48
49 if self.isServer then
50 local attacherTriggerTriggerNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.baleGrab#triggerNode"), self.i3dMappings)
51 local attacherTriggerRootNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.baleGrab#rootNode"), self.i3dMappings)
52 local attacherTriggerJointNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.baleGrab#jointNode"), self.i3dMappings)
53 local attacherJointTypeString = Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.baleGrab#jointType"), "TYPE_AUTO_ATTACH_XYZ")
54 local attacherJointType = DynamicMountUtil.TYPE_AUTO_ATTACH_XYZ
55 if DynamicMountUtil[attacherJointTypeString] ~= nil then
56 attacherJointType = DynamicMountUtil[attacherJointTypeString]
57 end
58
59 if attacherTriggerTriggerNode ~= nil and attacherTriggerRootNode ~= nil and attacherTriggerJointNode ~= nil then
60 local forceAcceleration = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.baleGrab#forceAcceleration"), 20)
61 addTrigger(attacherTriggerTriggerNode, "baleGrabTriggerCallback", self)
62
63 local grabRefComponentJointIndex1 = getXMLInt(self.xmlFile, "vehicle.baleGrab#grabRefComponentJointIndex1")
64 local grabRefComponentJointIndex2 = getXMLInt(self.xmlFile, "vehicle.baleGrab#grabRefComponentJointIndex2")
65 local componentJoint1, componentJoint2
66 if grabRefComponentJointIndex1 ~= nil then
67 componentJoint1 = self.componentJoints[grabRefComponentJointIndex1+1]
68 end
69 if grabRefComponentJointIndex2 ~= nil then
70 componentJoint2 = self.componentJoints[grabRefComponentJointIndex2+1]
71 end
72 local rotDiffThreshold1 = math.rad(Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.baleGrab#rotDiffThreshold1"), 2))
73 local rotDiffThreshold2 = math.rad(Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.baleGrab#rotDiffThreshold2"), 2))
74 spec.dynamicMountAttacherTrigger = {
75 triggerNode=attacherTriggerTriggerNode, rootNode=attacherTriggerRootNode, jointNode=attacherTriggerJointNode, attacherJointType=attacherJointType, forceAcceleration=forceAcceleration,
76 componentJoint1=componentJoint1, rotDiffThreshold1=rotDiffThreshold1, cosRotDiffThreshold1=math.cos(rotDiffThreshold1),
77 componentJoint2=componentJoint2, rotDiffThreshold2=rotDiffThreshold2, cosRotDiffThreshold2=math.cos(rotDiffThreshold2)
78 }
79 end
80 spec.dynamicMountedObjects = {}
81 spec.pendingDynamicMountObjects = {}
82 end
83end

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
104function BaleGrab:onUpdateTick(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
105 if self.isServer then
106 local spec = self.spec_baleGrab
107 local attachTrigger = spec.dynamicMountAttacherTrigger
108
109 local isClosed = true
110 if attachTrigger.componentJoint1 ~= nil then
111 isClosed = self:isComponentJointOutsideLimit(attachTrigger.componentJoint1, attachTrigger.rotDiffThreshold1, attachTrigger.cosRotDiffThreshold1)
112 end
113 if isClosed and attachTrigger.componentJoint2 ~= nil then
114 isClosed = self:isComponentJointOutsideLimit(attachTrigger.componentJoint2, attachTrigger.rotDiffThreshold2, attachTrigger.cosRotDiffThreshold2)
115 end
116 if isClosed then
117 for object,_ in pairs(spec.pendingDynamicMountObjects) do
118 if spec.dynamicMountedObjects[object] == nil then
119 object:unmountDynamic()
120 local dynamicMountData = spec.dynamicMountAttacherTrigger
121 if object:mountDynamic(self, dynamicMountData.rootNode, dynamicMountData.jointNode, dynamicMountData.attacherJointType, dynamicMountData.forceAcceleration) then
122 self:addDynamicMountedObject(object)
123 end
124 end
125 end
126 else
127 for object,_ in pairs(spec.dynamicMountedObjects) do
128 self:removeDynamicMountedObject(object, false)
129 object:unmountDynamic()
130 end
131 end
132 end
133end

prerequisitesPresent

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

registerEventListeners

Description
Definition
registerEventListeners()
Code
37function BaleGrab.registerEventListeners(vehicleType)
38 SpecializationUtil.registerEventListener(vehicleType, "onLoad", BaleGrab)
39 SpecializationUtil.registerEventListener(vehicleType, "onDelete", BaleGrab)
40 SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", BaleGrab)
41end

registerFunctions

Description
Definition
registerFunctions()
Code
21function BaleGrab.registerFunctions(vehicleType)
22 SpecializationUtil.registerFunction(vehicleType, "baleGrabTriggerCallback", BaleGrab.baleGrabTriggerCallback)
23 SpecializationUtil.registerFunction(vehicleType, "addDynamicMountedObject", BaleGrab.addDynamicMountedObject)
24 SpecializationUtil.registerFunction(vehicleType, "removeDynamicMountedObject", BaleGrab.removeDynamicMountedObject)
25 SpecializationUtil.registerFunction(vehicleType, "isComponentJointOutsideLimit", BaleGrab.isComponentJointOutsideLimit)
26end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
30function BaleGrab.registerOverwrittenFunctions(vehicleType)
31 SpecializationUtil.registerOverwrittenFunction(vehicleType, "addNodeObjectMapping", BaleGrab.addNodeObjectMapping)
32 SpecializationUtil.registerOverwrittenFunction(vehicleType, "removeNodeObjectMapping", BaleGrab.removeNodeObjectMapping)
33end

removeDynamicMountedObject

Description
Remove dynamic mount object
Definition
removeDynamicMountedObject(table object, boolean isDeleting)
Arguments
tableobjectobject
booleanisDeletingis deleting
Code
147function BaleGrab:removeDynamicMountedObject(object, isDeleting)
148 local spec = self.spec_baleGrab
149 spec.dynamicMountedObjects[object] = nil
150 if isDeleting then
151 spec.pendingDynamicMountObjects[object] = nil
152 end
153end

removeNodeObjectMapping

Description
Definition
removeNodeObjectMapping()
Code
225function BaleGrab:removeNodeObjectMapping(superFunc, list)
226 superFunc(self, list)
227
228 local spec = self.spec_baleGrab
229 if spec.dynamicMountAttacherTrigger ~= nil and spec.dynamicMountAttacherTrigger.triggerNode ~= nil then
230 list[spec.dynamicMountAttacherTrigger.triggerNode] = nil
231 end
232end