LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

StrawBlower

Description
Specialization for straw blowers providing functionality to dissipate a bale to lose straw
Functions

addFillUnitFillLevel

Description
Definition
addFillUnitFillLevel()
Code
165function StrawBlower:addFillUnitFillLevel(superFunc, farmId, fillUnitIndex, fillLevelDelta, fillTypeIndex, toolType, fillPositionData)
166 local spec = self.spec_strawBlower
167 if fillUnitIndex == spec.fillUnitIndex then
168 self:setFillUnitCapacity(fillUnitIndex, math.max(self:getFillUnitCapacity(fillUnitIndex), self:getFillUnitFillLevel(fillUnitIndex)+fillLevelDelta))
169 end
170
171 return superFunc(self, farmId, fillUnitIndex, fillLevelDelta, fillTypeIndex, toolType, fillPositionData)
172end

getAllowDynamicMountFillLevelInfo

Description
Definition
getAllowDynamicMountFillLevelInfo()
Code
307function StrawBlower:getAllowDynamicMountFillLevelInfo(superFunc)
308 return false
309end

getDrawFirstFillText

Description
Definition
getDrawFirstFillText()
Code
300function StrawBlower:getDrawFirstFillText(superFunc)
301 local spec = self.spec_strawBlower
302 return superFunc(self) or self:getFillUnitFillLevel(spec.fillUnitIndex) <= 0
303end

initSpecialization

Description
Definition
initSpecialization()
Code
24function StrawBlower.initSpecialization()
25 local schema = Vehicle.xmlSchema
26 schema:setXMLSpecializationType("StrawBlower")
27
28 schema:register(XMLValueType.NODE_INDEX, "vehicle.strawBlower.baleTrigger#node", "Bale trigger node")
29 schema:register(XMLValueType.NODE_INDEX, "vehicle.strawBlower.balePickupTrigger#node", "Bale pickup trigger node")
30 schema:register(XMLValueType.NODE_INDEX, "vehicle.strawBlower.balePickupTrigger#spawnNode", "Bale pickup trigger spawn node")
31 schema:register(XMLValueType.INT, "vehicle.strawBlower.balePickupTrigger#maxBales", "Max. number of pickup bales")
32
33 schema:register(XMLValueType.INT, "vehicle.strawBlower#fillUnitIndex", "Fill unit index", 1)
34
35 AnimationManager.registerAnimationNodesXMLPaths(schema, "vehicle.strawBlower.animationNodes")
36
37 SoundManager.registerSampleXMLPaths(schema, "vehicle.strawBlower.sounds", "start")
38 SoundManager.registerSampleXMLPaths(schema, "vehicle.strawBlower.sounds", "stop")
39 SoundManager.registerSampleXMLPaths(schema, "vehicle.strawBlower.sounds", "work")
40
41 schema:setXMLSpecializationType()
42end

onDelete

Description
Called on deleting
Definition
onDelete()
Code
123function StrawBlower:onDelete()
124 local spec = self.spec_strawBlower
125
126 if spec.triggerId ~= nil then
127 removeTrigger(spec.triggerId)
128 end
129 if spec.pickupTriggerId ~= nil then
130 removeTrigger(spec.pickupTriggerId)
131 end
132 if spec.triggeredBales ~= nil then
133 for bale, _ in pairs(spec.triggeredBales) do
134 if entityExists(bale.nodeId) then
135 I3DUtil.wakeUpObject(bale.nodeId)
136 bale.allowPickup = true
137 end
138 end
139 end
140
141 g_soundManager:deleteSamples(spec.samples)
142 g_animationManager:deleteAnimations(spec.animationNodes)
143end

onDeleteStrawBlowerObject

Description
Definition
onDeleteStrawBlowerObject()
Code
232function StrawBlower:onDeleteStrawBlowerObject(object)
233 local spec = self.spec_strawBlower
234
235 if spec.triggeredBales[object] ~= nil then
236 spec.triggeredBales[object] = nil
237
238 if object == spec.currentBale then
239 spec.currentBale = nil
240 self:addFillUnitFillLevel(self:getOwnerFarmId(), spec.fillUnitIndex, -math.huge, self:getFillUnitFillType(spec.fillUnitIndex), ToolType.UNDEFINED)
241 end
242 end
243end

onDischargeStateChanged

Description
Called on discharge state change
Definition
onDischargeStateChanged()
Code
353function StrawBlower:onDischargeStateChanged(state)
354 local spec = self.spec_strawBlower
355 local samples = spec.samples
356 if self.isClient then
357 if state ~= Dischargeable.DISCHARGE_STATE_OFF then
358 g_soundManager:stopSample(samples.work)
359 g_soundManager:stopSample(samples.stop)
360 g_soundManager:playSample(samples.start)
361 g_soundManager:playSample(samples.work, 0, samples.start)
362 g_animationManager:startAnimations(spec.animationNodes)
363 else
364 g_soundManager:stopSample(samples.start)
365 g_soundManager:stopSample(samples.work)
366 g_soundManager:playSample(samples.stop)
367 g_animationManager:stopAnimations(spec.animationNodes)
368 end
369 end
370end

onFillUnitFillLevelChanged

Description
Set unit fill level
Definition
onFillUnitFillLevelChanged(integer fillUnitIndex, float fillLevel, integer fillType, boolean force, table fillInfo)
Arguments
integerfillUnitIndexindex of fill unit
floatfillLevelnew fill level
integerfillTypefill type
booleanforceforce action
tablefillInfofill info for fill volume
Code
318function StrawBlower:onFillUnitFillLevelChanged(fillUnitIndex, fillLevelDelta, fillTypeIndex, toolType, fillPositionData, appliedDelta)
319 local spec = self.spec_strawBlower
320 if fillUnitIndex == spec.fillUnitIndex then
321 local newFillLevel = self:getFillUnitFillLevel(spec.fillUnitIndex)
322
323 if self.isServer then
324 local bale = spec.currentBale
325 if bale ~= nil then
326 if newFillLevel <= 0.01 then
327 if self.removeDynamicMountedObject ~= nil then
328 self:removeDynamicMountedObject(bale)
329 end
330
331 local baleOwner = bale:getOwnerFarmId()
332
333 bale:delete()
334 spec.currentBale = nil
335 spec.triggeredBales[bale] = nil
336
337 self:setFillUnitCapacity(spec.fillUnitIndex, 1)
338 self:addFillUnitFillLevel(baleOwner, spec.fillUnitIndex, -math.huge, FillType.UNKNOWN, ToolType.UNDEFINED)
339 elseif newFillLevel < bale:getFillLevel() and fillTypeIndex == bale:getFillType() then
340 bale:setFillLevel(newFillLevel)
341 end
342 end
343 else
344 if newFillLevel <= 0 then
345 self:setFillUnitCapacity(spec.fillUnitIndex, 1)
346 end
347 end
348 end
349end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
73function StrawBlower:onLoad(savegame)
74 local spec = self.spec_strawBlower
75
76 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.strawBlower.baleTrigger#index", "vehicle.strawBlower.baleTrigger#node") --FS17 to FS19
77 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.strawBlower.doorAnimation#name", "vehicle.foldable.foldingParts.foldingPart.animationName") --FS17 to FS19
78
79 spec.triggeredBales = {}
80
81 if self.isServer then
82 spec.triggerId = self.xmlFile:getValue("vehicle.strawBlower.baleTrigger#node", nil, self.components, self.i3dMappings)
83 if spec.triggerId ~= nil then
84 addTrigger(spec.triggerId, "strawBlowerBaleTriggerCallback", self)
85 end
86
87 spec.pickupTriggerId = self.xmlFile:getValue("vehicle.strawBlower.balePickupTrigger#node", nil, self.components, self.i3dMappings)
88 if spec.pickupTriggerId ~= nil then
89 addTrigger(spec.pickupTriggerId, "strawBlowerBalePickupTriggerCallback", self)
90 end
91
92 spec.pickupTriggerSpawnNode = self.xmlFile:getValue("vehicle.strawBlower.balePickupTrigger#spawnNode", nil, self.components, self.i3dMappings)
93 spec.pickupTriggerMaxBales = self.xmlFile:getValue("vehicle.strawBlower.balePickupTrigger#maxBales", 2)
94 end
95
96 if self.isClient then
97 spec.animationNodes = g_animationManager:loadAnimations(self.xmlFile, "vehicle.strawBlower.animationNodes", self.components, self, self.i3dMappings)
98
99 spec.samples = {}
100 spec.samples.start = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.strawBlower.sounds", "start", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
101 spec.samples.stop = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.strawBlower.sounds", "stop", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
102 spec.samples.work = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.strawBlower.sounds", "work", self.baseDirectory, self.components, 0, AudioGroup.VEHICLE, self.i3dMappings, self)
103 end
104
105 spec.fillUnitIndex = self.xmlFile:getValue("vehicle.strawBlower#fillUnitIndex", 1)
106
107 -- we need to send the full fill level rather than percentages since we change the capacity
108 local fillUnit = self:getFillUnitByIndex(spec.fillUnitIndex)
109 fillUnit.synchronizeFullFillLevel = true
110 fillUnit.needsSaving = false
111
112 if savegame ~= nil and not savegame.resetVehicles then
113 self:addFillUnitFillLevel(self:getOwnerFarmId(), spec.fillUnitIndex, -math.huge, FillType.UNKNOWN, ToolType.UNDEFINED)
114 end
115
116 if not self.isServer then
117 SpecializationUtil.removeEventListener(self, "onUpdateTick", StrawBlower)
118 end
119end

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
150function StrawBlower:onUpdateTick(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
151 local spec = self.spec_strawBlower
152 if spec.currentBale == nil and self:getFillUnitSupportsToolType(spec.fillUnitIndex, ToolType.BALE) then
153 local bale = next(spec.triggeredBales)
154 if bale ~= nil then
155 self:setFillUnitCapacity(spec.fillUnitIndex, bale:getFillLevel())
156 self:addFillUnitFillLevel(self:getOwnerFarmId(), spec.fillUnitIndex, -math.huge, FillType.UNKNOWN, ToolType.UNDEFINED)
157 self:addFillUnitFillLevel(self:getOwnerFarmId(), spec.fillUnitIndex, bale:getFillLevel(), bale:getFillType(), ToolType.BALE)
158 spec.currentBale = bale
159 end
160 end
161end

prerequisitesPresent

Description
Checks if all prerequisite specializations are loaded
Definition
prerequisitesPresent(table specializations)
Arguments
tablespecializationsspecializations
Return Values
booleanhasPrerequisitetrue if all prerequisite specializations are loaded
Code
17function StrawBlower.prerequisitesPresent(specializations)
18 return SpecializationUtil.hasSpecialization(FillUnit, specializations)
19 and SpecializationUtil.hasSpecialization(Trailer, specializations)
20end

registerEventListeners

Description
Definition
registerEventListeners()
Code
62function StrawBlower.registerEventListeners(vehicleType)
63 SpecializationUtil.registerEventListener(vehicleType, "onLoad", StrawBlower)
64 SpecializationUtil.registerEventListener(vehicleType, "onDelete", StrawBlower)
65 SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", StrawBlower)
66 SpecializationUtil.registerEventListener(vehicleType, "onFillUnitFillLevelChanged", StrawBlower)
67 SpecializationUtil.registerEventListener(vehicleType, "onDischargeStateChanged", StrawBlower)
68end

registerFunctions

Description
Definition
registerFunctions()
Code
46function StrawBlower.registerFunctions(vehicleType)
47 SpecializationUtil.registerFunction(vehicleType, "strawBlowerBaleTriggerCallback", StrawBlower.strawBlowerBaleTriggerCallback)
48 SpecializationUtil.registerFunction(vehicleType, "onDeleteStrawBlowerObject", StrawBlower.onDeleteStrawBlowerObject)
49 SpecializationUtil.registerFunction(vehicleType, "strawBlowerBalePickupTriggerCallback", StrawBlower.strawBlowerBalePickupTriggerCallback)
50end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
54function StrawBlower.registerOverwrittenFunctions(vehicleType)
55 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getDrawFirstFillText", StrawBlower.getDrawFirstFillText)
56 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getAllowDynamicMountFillLevelInfo", StrawBlower.getAllowDynamicMountFillLevelInfo)
57 SpecializationUtil.registerOverwrittenFunction(vehicleType, "addFillUnitFillLevel", StrawBlower.addFillUnitFillLevel)
58end

strawBlowerBalePickupTriggerCallback

Description
Trigger callback
Definition
strawBlowerBalePickupTriggerCallback(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
253function StrawBlower:strawBlowerBalePickupTriggerCallback(triggerId, otherActorId, onEnter, onLeave, onStay, otherShapeId)
254 local spec = self.spec_strawBlower
255
256 if onEnter then
257 -- this happens if a compound child of a deleted compound is entering
258 if otherActorId ~= 0 then
259 local object = g_currentMission:getNodeObject(otherActorId)
260 if object ~= nil then
261 if object:isa(Bale) and g_currentMission.accessHandler:canFarmAccess(self:getActiveFarm(), object) and object:getAllowPickup() then
262 if self:getFillUnitSupportsFillType(spec.fillUnitIndex, object:getFillType()) then
263 if spec.triggeredBales[object] == nil then
264 local maxHeight = 0
265 local count = 0
266 for bale, _ in pairs(spec.triggeredBales) do
267 local _, y, _ = getWorldTranslation(bale.nodeId)
268 y = y + (bale.baleDiameter or bale.baleHeight) / 2
269 if y > maxHeight then
270 maxHeight = y
271 end
272 count = count + 1
273 end
274
275 if count < spec.pickupTriggerMaxBales then
276 local x, y, z = getWorldTranslation(spec.pickupTriggerSpawnNode)
277 local baleHeight = (object.baleDiameter or object.baleHeight) / 2
278 y = math.max(y, maxHeight) + baleHeight
279
280 removeFromPhysics(object.nodeId)
281
282 setTranslation(object.nodeId, x, y, z)
283 setWorldRotation(object.nodeId, getWorldRotation(spec.pickupTriggerSpawnNode))
284
285 addToPhysics(object.nodeId)
286
287 local vx, vy, vz = getLinearVelocity(self:getParentComponent(spec.pickupTriggerSpawnNode))
288 setLinearVelocity(object.nodeId, vx, vy, vz)
289 end
290 end
291 end
292 end
293 end
294 end
295 end
296end

strawBlowerBaleTriggerCallback

Description
Trigger callback
Definition
strawBlowerBaleTriggerCallback(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
182function StrawBlower:strawBlowerBaleTriggerCallback(triggerId, otherActorId, onEnter, onLeave, onStay, otherShapeId)
183 local spec = self.spec_strawBlower
184
185 if onEnter then
186 -- this happens if a compound child of a deleted compound is entering
187 if otherActorId ~= 0 then
188 local object = g_currentMission:getNodeObject(otherActorId)
189 if object ~= nil then
190 if object:isa(Bale) and g_currentMission.accessHandler:canFarmAccess(self:getActiveFarm(), object) and object:getAllowPickup() then
191 if self:getFillUnitSupportsFillType(spec.fillUnitIndex, object:getFillType()) then
192 spec.triggeredBales[object] = Utils.getNoNil(spec.triggeredBales[object], 0) + 1
193 object.allowPickup = false
194
195 if spec.triggeredBales[object] == 1 then
196 if object.addDeleteListener ~= nil then
197 object:addDeleteListener(self, "onDeleteStrawBlowerObject")
198 end
199 end
200 end
201 end
202 end
203 end
204 elseif onLeave then
205 if otherActorId ~= 0 then
206 local object = g_currentMission:getNodeObject(otherActorId)
207 if object ~= nil then
208 local triggerCount = spec.triggeredBales[object]
209 if triggerCount ~= nil then
210 if triggerCount == 1 then
211 spec.triggeredBales[object] = nil
212 object.allowPickup = true
213 if object == spec.currentBale then
214 spec.currentBale = nil
215 self:addFillUnitFillLevel(self:getOwnerFarmId(), spec.fillUnitIndex, -math.huge, self:getFillUnitFillType(spec.fillUnitIndex), ToolType.UNDEFINED)
216 end
217
218 if object.removeDeleteListener ~= nil then
219 object:removeDeleteListener(self)
220 end
221 else
222 spec.triggeredBales[object] = triggerCount-1
223 end
224 end
225 end
226 end
227 end
228end