LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

Mulcher

Description
Specialization for all mulchers, requires WorkArea specialization
Functions

doCheckSpeedLimit

Description
Returns if speed limit should be checked
Definition
doCheckSpeedLimit()
Return Values
booleancheckSpeedlimitcheck speed limit
Code
298function Mulcher:doCheckSpeedLimit(superFunc)
299 return superFunc(self) or self:getIsImplementChainLowered()
300end

getDefaultSpeedLimit

Description
Returns default speed limit
Definition
getDefaultSpeedLimit()
Return Values
floatspeedLimitspeed limit
Code
409function Mulcher.getDefaultSpeedLimit()
410 return 15
411end

getDirtMultiplier

Description
Returns current dirt multiplier
Definition
getDirtMultiplier()
Return Values
floatdirtMultipliercurrent dirt multiplier
Code
318function Mulcher:getDirtMultiplier(superFunc)
319 local spec = self.spec_mulcher
320
321 local multiplier = superFunc(self)
322 if spec.isWorking then
323 multiplier = multiplier + self:getWorkDirtMultiplier() * self:getLastSpeed() / spec.speedLimit
324 end
325
326 return multiplier
327end

getDoGroundManipulation

Description
Returns if tool does ground manipulation
Definition
getDoGroundManipulation()
Return Values
booleandoGroundManipulationdo ground manipulation
Code
305function Mulcher:getDoGroundManipulation(superFunc)
306 local spec = self.spec_mulcher
307
308 if not spec.isWorking then
309 return false
310 end
311
312 return superFunc(self)
313end

getWearMultiplier

Description
Returns current wear multiplier
Definition
getWearMultiplier()
Return Values
floatdirtMultipliercurrent wear multiplier
Code
332function Mulcher:getWearMultiplier(superFunc)
333 local spec = self.spec_mulcher
334 local multiplier = superFunc(self)
335
336 if spec.isWorking then
337 local stoneMultiplier = 1
338 if spec.stoneLastState ~= 0 and spec.stoneWearMultiplierData ~= nil then
339 stoneMultiplier = spec.stoneWearMultiplierData[spec.stoneLastState] or 1
340 end
341
342 multiplier = multiplier + self:getWorkWearMultiplier() * self:getLastSpeed() / spec.speedLimit * stoneMultiplier
343 end
344
345 return multiplier
346end

initSpecialization

Description
Called on specialization initializing
Definition
initSpecialization()
Code
24function Mulcher.initSpecialization()
25 g_workAreaTypeManager:addWorkAreaType("mulcher", true)
26
27 local schema = Vehicle.xmlSchema
28 schema:setXMLSpecializationType("Mulcher")
29
30 EffectManager.registerEffectXMLPaths(schema, "vehicle.mulcher.effects.effect(?)")
31 schema:register(XMLValueType.INT, "vehicle.mulcher.effects.effect(?)#workAreaIndex", "Work area index", 1)
32 schema:register(XMLValueType.INT, "vehicle.mulcher.effects.effect(?)#activeDirection", "If vehicle is driving into this direction the effect will be activated (0 = any direction)", 0)
33
34 SoundManager.registerSampleXMLPaths(schema, "vehicle.mulcher.sounds", "work")
35
36 schema:setXMLSpecializationType()
37end

loadWorkAreaFromXML

Description
Loads work areas from xml
Definition
loadWorkAreaFromXML(table workArea, integer xmlFile, string key)
Arguments
tableworkAreaworkArea
integerxmlFileid of xml object
stringkeykey
Return Values
booleansuccesssuccess
Code
354function Mulcher:loadWorkAreaFromXML(superFunc, workArea, xmlFile, key)
355 local retValue = superFunc(self, workArea, xmlFile, key)
356
357 if workArea.type == WorkAreaType.DEFAULT then
358 workArea.type = WorkAreaType.MULCHER
359 end
360
361 return retValue
362end

onDeactivate

Description
Definition
onDeactivate()
Code
366function Mulcher:onDeactivate()
367 local spec = self.spec_mulcher
368 if self.isClient then
369 g_soundManager:stopSamples(spec.samples)
370 spec.isWorkSamplePlaying = false
371 end
372
373 for _, effect in ipairs(spec.effects) do
374 g_effectManager:stopEffects(effect.effects)
375 end
376end

onDelete

Description
Called on deleting
Definition
onDelete()
Code
171function Mulcher:onDelete()
172 local spec = self.spec_mulcher
173 g_soundManager:deleteSamples(spec.samples)
174
175 if spec.effects ~= nil then
176 for _, effect in ipairs(spec.effects) do
177 g_effectManager:deleteEffects(effect.effects)
178 end
179 end
180end

onEndWorkAreaProcessing

Description
Definition
onEndWorkAreaProcessing()
Code
388function Mulcher:onEndWorkAreaProcessing(dt)
389 local spec = self.spec_mulcher
390
391 if self.isClient then
392 if spec.isWorking then
393 if not spec.isWorkSamplePlaying then
394 g_soundManager:playSample(spec.samples.work)
395 spec.isWorkSamplePlaying = true
396 end
397 else
398 if spec.isWorkSamplePlaying then
399 g_soundManager:stopSample(spec.samples.work)
400 spec.isWorkSamplePlaying = false
401 end
402 end
403 end
404end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
81function Mulcher:onLoad(savegame)
82 if self:getGroundReferenceNodeFromIndex(1) == nil then
83 print("Warning: No ground reference nodes in "..self.configFileName)
84 end
85
86 local spec = self.spec_mulcher
87
88 if self.isClient then
89 spec.samples = {}
90 spec.samples.work = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.mulcher.sounds", "work", self.baseDirectory, self.components, 0, AudioGroup.VEHICLE, self.i3dMappings, self)
91 spec.isWorkSamplePlaying = false
92 end
93
94 spec.effects = {}
95 spec.workAreaToEffects = {}
96 local i = 0
97 while true do
98 local key = string.format("vehicle.mulcher.effects.effect(%d)", i)
99 if not self.xmlFile:hasProperty(key) then
100 break
101 end
102
103 local effects = g_effectManager:loadEffect(self.xmlFile, key, self.components, self, self.i3dMappings)
104 if effects ~= nil then
105 local effect = {}
106 effect.effects = effects
107 effect.workAreaIndex = self.xmlFile:getValue(key .. "#workAreaIndex", 1)
108 effect.activeDirection = self.xmlFile:getValue(key .. "#activeDirection", 0)
109 effect.activeTime = -1
110 effect.activeTimeDuration = 250
111 effect.isActive = false
112 effect.isActiveSent = false
113
114 table.insert(spec.effects, effect)
115 end
116 i = i + 1
117 end
118
119 spec.effectFillType = FillType.WHEAT
120
121 if self.addAIGroundTypeRequirements ~= nil then
122 self:addAIGroundTypeRequirements(Mulcher.AI_REQUIRED_GROUND_TYPES)
123
124 self:clearAIFruitRequirements()
125 for _, fruitType in ipairs(g_fruitTypeManager:getFruitTypes()) do
126 if fruitType.destruction.canBeDestroyed then
127 if fruitType.mulcher.state > fruitType.cutState then
128 self:addAIFruitRequirement(fruitType.index, 2, fruitType.mulcher.state - 1)
129 else
130 self:addAIFruitRequirement(fruitType.index, 2, 15)
131 end
132 end
133 end
134 end
135
136 spec.isWorking = false
137 spec.lastWorkTime = -math.huge
138
139 spec.stoneLastState = 0
140 spec.stoneWearMultiplierData = g_currentMission.stoneSystem:getWearMultiplierByType("MULCHER")
141
142 spec.effectDirtyFlag = self:getNextDirtyFlag()
143
144 if not self.isClient or #spec.effects == 0 then
145 SpecializationUtil.removeEventListener(self, "onUpdateTick", Mulcher)
146 end
147end

onPostLoad

Description
Definition
onPostLoad()
Code
151function Mulcher:onPostLoad(savegame)
152 local spec = self.spec_mulcher
153 for i=#spec.effects, 1, -1 do
154 local effect = spec.effects[i]
155 local workArea = self:getWorkAreaByIndex(effect.workAreaIndex)
156 if workArea ~= nil then
157 if spec.workAreaToEffects[workArea.index] == nil then
158 spec.workAreaToEffects[workArea.index] = {}
159 end
160 table.insert(spec.workAreaToEffects[workArea.index], effect)
161 else
162 Logging.xmlWarning(self.xmlFile, "Invalid workAreaIndex '%d' for effect 'vehicle.mulcher.effects.effect(%d)'!", effect.workAreaIndex, i)
163 table.remove(spec.effects, i)
164 end
165 end
166end

onReadStream

Description
Definition
onReadStream()
Code
184function Mulcher:onReadStream(streamId, connection)
185 local spec = self.spec_mulcher
186 for _, effect in ipairs(spec.effects) do
187 if streamReadBool(streamId) then
188 g_effectManager:setFillType(effect.effects, spec.effectFillType)
189 g_effectManager:startEffects(effect.effects)
190 else
191 g_effectManager:stopEffects(effect.effects)
192 end
193 end
194end

onReadUpdateStream

Description
Definition
onReadUpdateStream()
Code
207function Mulcher:onReadUpdateStream(streamId, timestamp, connection)
208 if connection:getIsServer() then
209 local spec = self.spec_mulcher
210 if streamReadBool(streamId) then
211 for _, effect in ipairs(spec.effects) do
212 if streamReadBool(streamId) then
213 g_effectManager:setFillType(effect.effects, spec.effectFillType)
214 g_effectManager:startEffects(effect.effects)
215 else
216 g_effectManager:stopEffects(effect.effects)
217 end
218 end
219 end
220 end
221end

onStartWorkAreaProcessing

Description
Definition
onStartWorkAreaProcessing()
Code
380function Mulcher:onStartWorkAreaProcessing(dt)
381 local spec = self.spec_mulcher
382
383 spec.isWorking = false
384end

onUpdateTick

Description
Definition
onUpdateTick()
Code
238function Mulcher:onUpdateTick(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
239 if self.isServer then
240 local spec = self.spec_mulcher
241 for _, effect in ipairs(spec.effects) do
242 if effect.isActive and g_currentMission.time > effect.activeTime then
243 effect.isActive = false
244 self:raiseDirtyFlags(spec.effectDirtyFlag)
245 g_effectManager:stopEffects(effect.effects)
246 end
247 end
248 end
249end

onWriteStream

Description
Definition
onWriteStream()
Code
198function Mulcher:onWriteStream(streamId, connection)
199 local spec = self.spec_mulcher
200 for _, effect in ipairs(spec.effects) do
201 streamWriteBool(streamId, effect.isActive)
202 end
203end

onWriteUpdateStream

Description
Definition
onWriteUpdateStream()
Code
225function Mulcher:onWriteUpdateStream(streamId, connection, dirtyMask)
226 if not connection:getIsServer() then
227 local spec = self.spec_mulcher
228 if streamWriteBool(streamId, bitAND(dirtyMask, spec.effectDirtyFlag) ~= 0) then
229 for _, effect in ipairs(spec.effects) do
230 streamWriteBool(streamId, effect.isActive)
231 end
232 end
233 end
234end

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
43function Mulcher.prerequisitesPresent(specializations)
44 return SpecializationUtil.hasSpecialization(WorkArea, specializations) and SpecializationUtil.hasSpecialization(GroundReference, specializations)
45end

processMulcherArea

Description
Definition
processMulcherArea()
Code
253function Mulcher:processMulcherArea(workArea, dt)
254 local spec = self.spec_mulcher
255
256 local xs,_,zs = getWorldTranslation(workArea.start)
257 local xw,_,zw = getWorldTranslation(workArea.width)
258 local xh,_,zh = getWorldTranslation(workArea.height)
259
260 local realArea, area = FSDensityMapUtil.updateMulcherArea(xs,zs, xw,zw, xh,zh)
261 if realArea > 0 and self:getLastSpeed() > 0.5 then
262 local effects = spec.workAreaToEffects[workArea.index]
263 if effects ~= nil then
264 for _, effect in ipairs(effects) do
265 if effect.activeDirection == 0 or self.movingDirection == effect.activeDirection then
266 effect.activeTime = g_currentMission.time + effect.activeTimeDuration
267
268 if not effect.isActive then
269 g_effectManager:setFillType(effect.effects, spec.effectFillType)
270 g_effectManager:startEffects(effect.effects)
271
272 effect.isActive = true
273 self:raiseDirtyFlags(spec.effectDirtyFlag)
274 end
275 end
276 end
277 end
278
279 spec.lastWorkTime = g_time
280 end
281
282 FSDensityMapUtil.eraseTireTrack(xs,zs, xw,zw, xh,zh)
283
284 spec.isWorking = (g_time - spec.lastWorkTime) < 500
285
286 if spec.isWorking then
287 spec.stoneLastState = FSDensityMapUtil.getStoneArea(xs, zs, xw, zw, xh, zh)
288 else
289 spec.stoneLastState = 0
290 end
291
292 return realArea, area
293end

registerEventListeners

Description
Definition
registerEventListeners()
Code
64function Mulcher.registerEventListeners(vehicleType)
65 SpecializationUtil.registerEventListener(vehicleType, "onLoad", Mulcher)
66 SpecializationUtil.registerEventListener(vehicleType, "onPostLoad", Mulcher)
67 SpecializationUtil.registerEventListener(vehicleType, "onDelete", Mulcher)
68 SpecializationUtil.registerEventListener(vehicleType, "onReadStream", Mulcher)
69 SpecializationUtil.registerEventListener(vehicleType, "onWriteStream", Mulcher)
70 SpecializationUtil.registerEventListener(vehicleType, "onReadUpdateStream", Mulcher)
71 SpecializationUtil.registerEventListener(vehicleType, "onWriteUpdateStream", Mulcher)
72 SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", Mulcher)
73 SpecializationUtil.registerEventListener(vehicleType, "onDeactivate", Mulcher)
74 SpecializationUtil.registerEventListener(vehicleType, "onStartWorkAreaProcessing", Mulcher)
75 SpecializationUtil.registerEventListener(vehicleType, "onEndWorkAreaProcessing", Mulcher)
76end

registerFunctions

Description
Definition
registerFunctions()
Code
49function Mulcher.registerFunctions(vehicleType)
50 SpecializationUtil.registerFunction(vehicleType, "processMulcherArea", Mulcher.processMulcherArea)
51end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
55function Mulcher.registerOverwrittenFunctions(vehicleType)
56 SpecializationUtil.registerOverwrittenFunction(vehicleType, "doCheckSpeedLimit", Mulcher.doCheckSpeedLimit)
57 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getDoGroundManipulation", Mulcher.getDoGroundManipulation)
58 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getDirtMultiplier", Mulcher.getDirtMultiplier)
59 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getWearMultiplier", Mulcher.getWearMultiplier)
60end