LUADOC - Farming Simulator 19

Script v1.7.1.0

Engine v1.7.1.0

Foundation Reference

Weeder

Description
Specialization for weeder tool used to remove growing weeds on fields
Functions

doCheckSpeedLimit

Description
Returns if speed limit should be checked
Definition
doCheckSpeedLimit()
Return Values
booleancheckSpeedlimitcheck speed limit
Code
199function Weeder:doCheckSpeedLimit(superFunc)
200 return superFunc(self) or self:getIsImplementChainLowered()
201end

getDefaultSpeedLimit

Description
Returns default speed limit
Definition
getDefaultSpeedLimit()
Return Values
floatspeedLimitspeed limit
Code
319function Weeder.getDefaultSpeedLimit()
320 return 15
321end

getDirtMultiplier

Description
Returns current dirt multiplier
Definition
getDirtMultiplier()
Return Values
floatdirtMultipliercurrent dirt multiplier
Code
206function Weeder:getDirtMultiplier(superFunc)
207 local spec = self.spec_weeder
208 local multiplier = superFunc(self)
209
210 if spec.isWorking then
211 multiplier = multiplier + self:getWorkDirtMultiplier() * self:getLastSpeed() / self.speedLimit
212 end
213
214 return multiplier
215end

getIsWorkAreaActive

Description
Returns true if work area is active
Definition
getIsWorkAreaActive(table workArea)
Arguments
tableworkAreaworkArea
Return Values
booleanisActivework area is active
Code
181function Weeder:getIsWorkAreaActive(superFunc, workArea)
182 if workArea.type == WorkAreaType.WEEDER then
183 local isActive = true
184 if workArea.requiresGroundContact and workArea.groundReferenceNode ~= nil then
185 isActive = isActive and self:getIsGroundReferenceNodeActive(workArea.groundReferenceNode)
186 end
187 if isActive and workArea.disableBackwards then
188 isActive = isActive and self.movingDirection > 0
189 end
190 return isActive
191 end
192
193 return superFunc(self, workArea)
194end

getWearMultiplier

Description
Returns current wear multiplier
Definition
getWearMultiplier()
Return Values
floatdirtMultipliercurrent wear multiplier
Code
220function Weeder:getWearMultiplier(superFunc)
221 local spec = self.spec_weeder
222 local multiplier = superFunc(self)
223
224 if spec.isWorking then
225 multiplier = multiplier + self:getWorkWearMultiplier() * self:getLastSpeed() / self.speedLimit
226 end
227
228 return multiplier
229end

initSpecialization

Description
Called on specialization initializing
Definition
initSpecialization()
Code
15function Weeder.initSpecialization()
16 g_workAreaTypeManager:addWorkAreaType("weeder", true)
17end

loadGroundParticleMapping

Description
Load ground particle mapping from xml file
Definition
loadGroundParticleMapping(integer xmlFile, string key, table mapping, integer index, integer i3dNode)
Arguments
integerxmlFileid of xml object
stringkeykey
tablemappingmapping
integerindexindex
integeri3dNodeid of i3d node
Return Values
booleansuccesssuccess
Code
239function Weeder:loadGroundParticleMapping(superFunc, xmlFile, key, mapping, index, i3dNode)
240 if not superFunc(self, xmlFile, key, mapping, index, i3dNode) then
241 return false
242 end
243
244 mapping.adjustColor = Utils.getNoNil(getXMLBool(xmlFile, key.."#adjustColor"), false)
245 if mapping.adjustColor then
246 local spec = self.spec_weeder
247 if spec.colorParticleSystems == nil then
248 spec.colorParticleSystems = {}
249 end
250 mapping.lastColor = {}
251
252 table.insert(spec.colorParticleSystems, mapping)
253 end
254
255 return true
256end

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
169function Weeder:loadWorkAreaFromXML(superFunc, workArea, xmlFile, key)
170 if workArea.type == WorkAreaType.DEFAULT then
171 workArea.type = WorkAreaType.WEEDER
172 end
173
174 return superFunc(self, workArea, xmlFile, key)
175end

onDeactivate

Description
Definition
onDeactivate()
Code
298function Weeder:onDeactivate()
299 if self.isClient then
300 local spec = self.spec_weeder
301 g_soundManager:stopSamples(spec.samples)
302 spec.isWorkSamplePlaying = false
303 end
304end

onDelete

Description
Called on deleting
Definition
onDelete()
Code
94function Weeder:onDelete()
95 if self.isClient then
96 local spec = self.spec_weeder
97 g_soundManager:deleteSamples(spec.samples)
98 end
99end

onEndWorkAreaProcessing

Description
Definition
onEndWorkAreaProcessing()
Code
269function Weeder:onEndWorkAreaProcessing(dt, hasProcessed)
270 local spec = self.spec_weeder
271
272 if self.isServer then
273 if spec.workAreaParameters.lastStatsArea > 0 then
274 local ha = MathUtil.areaToHa(spec.workAreaParameters.lastStatsArea, g_currentMission:getFruitPixelsToSqm())
275 local stats = g_currentMission:farmStats(self:getLastTouchedFarmlandFarmId())
276 stats:updateStats("workedHectares", ha)
277 stats:updateStats("workedTime", dt/(1000*60))
278 end
279 end
280
281 if self.isClient then
282 if spec.isWorking then
283 if not spec.isWorkSamplePlaying then
284 g_soundManager:playSample(spec.samples.work)
285 spec.isWorkSamplePlaying = true
286 end
287 else
288 if spec.isWorkSamplePlaying then
289 g_soundManager:stopSample(spec.samples.work)
290 spec.isWorkSamplePlaying = false
291 end
292 end
293 end
294end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
60function Weeder:onLoad(savegame)
61 local spec = self.spec_weeder
62
63 if self.isClient then
64 spec.samples = {}
65 spec.samples.work = g_soundManager:loadSampleFromXML(self.xmlFile, "vehicle.weeder.sounds", "work", self.baseDirectory, self.components, 0, AudioGroup.VEHICLE, self.i3dMappings, self)
66 spec.isWorkSamplePlaying = false
67 end
68
69 spec.startActivationTimeout = 2000
70 spec.startActivationTime = 0
71
72 spec.maxGrowthState = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.weeder.maxGrowthState"), 2)
73
74 spec.workAreaParameters = {}
75 spec.workAreaParameters.lastArea = 0
76 spec.workAreaParameters.lastStatsArea = 0
77
78 spec.isWorking = false
79
80 -- ai setup
81 if self.addAITerrainDetailRequiredRange ~= nil then
82 self:addAITerrainDetailRequiredRange(g_currentMission.sowingValue, g_currentMission.sowingValue, g_currentMission.terrainDetailTypeFirstChannel, g_currentMission.terrainDetailTypeNumChannels)
83 self:addAITerrainDetailRequiredRange(g_currentMission.sowingWidthValue, g_currentMission.sowingWidthValue, g_currentMission.terrainDetailTypeFirstChannel, g_currentMission.terrainDetailTypeNumChannels)
84 end
85
86 local fruitType = g_fruitTypeManager:getFruitTypeByName("weed")
87 if fruitType ~= nil then
88 self:setAIFruitRequirements(fruitType.index, 1, 1)
89 end
90end

onPostAttach

Description
Called if vehicle gets attached
Definition
onPostAttach(table attacherVehicle, integer inputJointDescIndex, integer jointDescIndex)
Arguments
tableattacherVehicleattacher vehicle
integerinputJointDescIndexindex of input attacher joint
integerjointDescIndexindex of attacher joint it gets attached to
Code
311function Weeder:onPostAttach(attacherVehicle, inputJointDescIndex, jointDescIndex)
312 local spec = self.spec_weeder
313 spec.startActivationTime = g_currentMission.time + spec.startActivationTimeout
314end

onStartWorkAreaProcessing

Description
Definition
onStartWorkAreaProcessing()
Code
260function Weeder:onStartWorkAreaProcessing(dt)
261 local spec = self.spec_weeder
262 spec.isWorking = false
263 spec.workAreaParameters.lastArea = 0
264 spec.workAreaParameters.lastStatsArea = 0
265end

onUpdateTick

Description
Definition
onUpdateTick()
Code
103function Weeder:onUpdateTick(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
104 local spec = self.spec_weeder
105
106 if spec.isWorking then
107 if spec.colorParticleSystems ~= nil then
108 for _, mapping in ipairs(spec.colorParticleSystems) do
109 local wx, wy, wz = getWorldTranslation(mapping.node)
110
111 local densityBits = getDensityAtWorldPos(g_currentMission.terrainDetailId, wx, wy, wz)
112 local isOnField = densityBits ~= 0
113
114 if isOnField then
115 mapping.lastColor[1], mapping.lastColor[2], mapping.lastColor[3], _ = FSDensityMapUtil.getTireTrackColorFromDensityBits(densityBits)
116 else
117 mapping.lastColor[1], mapping.lastColor[2], mapping.lastColor[3], _, _ = getTerrainAttributesAtWorldPos(g_currentMission.terrainRootNode, wx, wy, wz, true, true, true, true, false)
118 end
119
120 -- interpolate between different ground colors to avoid unrealisitic particle color changes
121 if mapping.targetColor == nil then
122 mapping.targetColor = {mapping.lastColor[1],mapping.lastColor[2],mapping.lastColor[3]}
123 mapping.currentColor = {mapping.lastColor[1],mapping.lastColor[2],mapping.lastColor[3]}
124 mapping.alpha = 1
125 end
126
127 if mapping.alpha ~= 1 then
128 mapping.alpha = math.min(mapping.alpha + dt/1000, 1)
129 mapping.currentColor = {MathUtil.vector3ArrayLerp(mapping.lastColor, mapping.targetColor, mapping.alpha)}
130 if mapping.alpha == 1 then
131 mapping.lastColor = {mapping.currentColor[1], mapping.currentColor[2], mapping.currentColor[3]}
132 end
133 end
134
135 if mapping.alpha == 1 and mapping.lastColor[1] ~= mapping.targetColor[1] and mapping.lastColor[2] ~= mapping.targetColor[2] and mapping.lastColor[3] ~= mapping.targetColor[3] then
136 mapping.alpha = 0
137 mapping.targetColor = {mapping.lastColor[1], mapping.lastColor[2], mapping.lastColor[3]}
138 end
139
140 setShaderParameter(mapping.particleSystem.shape, "psColor", mapping.currentColor[1], mapping.currentColor[2], mapping.currentColor[3], 1, false)
141 end
142 end
143 end
144end

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
23function Weeder.prerequisitesPresent(specializations)
24 return SpecializationUtil.hasSpecialization(WorkArea, specializations) and
25 SpecializationUtil.hasSpecialization(AttacherJoints, specializations)
26end

processWeederArea

Description
Definition
processWeederArea()
Code
148function Weeder:processWeederArea(workArea, dt)
149 local spec = self.spec_weeder
150 local xs,_,zs = getWorldTranslation(workArea.start)
151 local xw,_,zw = getWorldTranslation(workArea.width)
152 local xh,_,zh = getWorldTranslation(workArea.height)
153 local area = FSDensityMapUtil.updateWeederArea(xs,zs, xw,zw, xh,zh, spec.maxGrowthState)
154
155 spec.workAreaParameters.lastArea = spec.workAreaParameters.lastArea + area
156 spec.workAreaParameters.lastStatsArea = spec.workAreaParameters.lastStatsArea + area
157
158 spec.isWorking = self:getLastSpeed() > 0.5
159
160 return area, area
161end

registerEventListeners

Description
Definition
registerEventListeners()
Code
47function Weeder.registerEventListeners(vehicleType)
48 SpecializationUtil.registerEventListener(vehicleType, "onLoad", Weeder)
49 SpecializationUtil.registerEventListener(vehicleType, "onDelete", Weeder)
50 SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", Weeder)
51 SpecializationUtil.registerEventListener(vehicleType, "onDeactivate", Weeder)
52 SpecializationUtil.registerEventListener(vehicleType, "onPostAttach", Weeder)
53 SpecializationUtil.registerEventListener(vehicleType, "onStartWorkAreaProcessing", Weeder)
54 SpecializationUtil.registerEventListener(vehicleType, "onEndWorkAreaProcessing", Weeder)
55end

registerFunctions

Description
Definition
registerFunctions()
Code
30function Weeder.registerFunctions(vehicleType)
31 SpecializationUtil.registerFunction(vehicleType, "processWeederArea", Weeder.processWeederArea)
32end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
36function Weeder.registerOverwrittenFunctions(vehicleType)
37 SpecializationUtil.registerOverwrittenFunction(vehicleType, "doCheckSpeedLimit", Weeder.doCheckSpeedLimit)
38 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getDirtMultiplier", Weeder.getDirtMultiplier)
39 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getWearMultiplier", Weeder.getWearMultiplier)
40 SpecializationUtil.registerOverwrittenFunction(vehicleType, "loadWorkAreaFromXML", Weeder.loadWorkAreaFromXML)
41 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getIsWorkAreaActive", Weeder.getIsWorkAreaActive)
42 SpecializationUtil.registerOverwrittenFunction(vehicleType, "loadGroundParticleMapping", Weeder.loadGroundParticleMapping)
43end