LUADOC - Farming Simulator 17

Printable Version

Script v1.4.4.0

Engine v7.0.0.2

Foundation Reference

Weeder

Description
Class for all weeders
Functions

initSpecialization

Description
Called on specialization initializing
Definition
initSpecialization()
Code
15function Weeder.initSpecialization()
16 WorkArea.registerAreaType("weeder");
17end

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

preLoad

Description
Called before loading
Definition
preLoad(table savegame)
Arguments
tablesavegamesavegame
Code
31function Weeder:preLoad(savegame)
32 self.loadWorkAreaFromXML = Utils.overwrittenFunction(self.loadWorkAreaFromXML, Weeder.loadWorkAreaFromXML);
33 self.doCheckSpeedLimit = Utils.overwrittenFunction(self.doCheckSpeedLimit, Weeder.doCheckSpeedLimit);
34 self.getIsWorkAreaActive = Utils.overwrittenFunction(self.getIsWorkAreaActive, Weeder.getIsWorkAreaActive);
35 self.loadGroundParticleMapping = Utils.overwrittenFunction(self.loadGroundParticleMapping, Weeder.loadGroundParticleMapping);
36
37 self.processWeederAreas = Weeder.processWeederAreas;
38end

load

Description
Called on loading
Definition
load(table savegame)
Arguments
tablesavegamesavegame
Code
43function Weeder:load(savegame)
44
45 if self.isClient then
46 self.sampleWeeder = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.weederSound", nil, self.baseDirectory);
47 end
48
49 self.startActivationTimeout = 2000;
50 self.startActivationTime = 0;
51
52 self.showFieldNotOwnedWarning = false;
53 self.isWeederSpeedLimitActive = false;
54 self.weederHasGroundContact = false
55
56 -- ai setup
57 table.insert(self.terrainDetailRequiredValueRanges, {g_currentMission.sowingValue, g_currentMission.sowingValue, g_currentMission.terrainDetailTypeFirstChannel, g_currentMission.terrainDetailTypeNumChannels});
58 table.insert(self.terrainDetailRequiredValueRanges, {g_currentMission.sowingWidthValue, g_currentMission.sowingWidthValue, g_currentMission.terrainDetailTypeFirstChannel, g_currentMission.terrainDetailTypeNumChannels});
59 table.insert(self.terrainDetailRequiredValueRanges, {g_currentMission.grassValue, g_currentMission.grassValue, g_currentMission.terrainDetailTypeFirstChannel, g_currentMission.terrainDetailTypeNumChannels})
60
61 table.insert(self.terrainDetailProhibitValueRanges, {1, 1, g_currentMission.sprayFirstChannel, g_currentMission.sprayNumChannels});
62 table.insert(self.terrainDetailProhibitValueRanges, {g_currentMission.sprayLevelMaxValue, g_currentMission.sprayLevelMaxValue, g_currentMission.sprayLevelFirstChannel, g_currentMission.sprayLevelNumChannels});
63
64 self.weederDirtyFlag = self:getNextDirtyFlag();
65end

delete

Description
Called on deleting
Definition
delete()
Code
69function Weeder:delete()
70 if self.isClient then
71 SoundUtil.deleteSample(self.sampleWeeder);
72 end
73end

readUpdateStream

Description
Called on on update
Definition
readUpdateStream(integer streamId, integer timestamp, table connection)
Arguments
integerstreamIdstream ID
integertimestamptimestamp
tableconnectionconnection
Code
86function Weeder:readUpdateStream(streamId, timestamp, connection)
87 if connection:getIsServer() then
88 self.weederHasGroundContact = streamReadBool(streamId);
89 self.showFieldNotOwnedWarning = streamReadBool(streamId);
90 end
91end

writeUpdateStream

Description
Called on on update
Definition
writeUpdateStream(integer streamId, table connection, integer dirtyMask)
Arguments
integerstreamIdstream ID
tableconnectionconnection
integerdirtyMaskdirty mask
Code
98function Weeder:writeUpdateStream(streamId, connection, dirtyMask)
99 if not connection:getIsServer() then
100 streamWriteBool(streamId, self.weederHasGroundContact);
101 streamWriteBool(streamId, self.showFieldNotOwnedWarning);
102 end
103end

updateTick

Description
Called on update tick
Definition
updateTick(float dt)
Arguments
floatdttime since last call in ms
Code
117function Weeder:updateTick(dt)
118 self.isWeederSpeedLimitActive = false;
119 if self:getIsActive() then
120 local showFieldNotOwnedWarning = false;
121
122 if self.isServer then
123 local hasGroundContact = self:getIsTypedWorkAreaActive(WorkArea.AREATYPE_WEEDER);
124 if self.weederHasGroundContact ~= hasGroundContact then
125 self:raiseDirtyFlags(self.weederDirtyFlag);
126 self.weederHasGroundContact = hasGroundContact;
127 end;
128 end;
129 local hasGroundContact = self.weederHasGroundContact;
130
131 self.doGroundManipulation = (self.movingDirection > 0 and hasGroundContact and (not self.onlyActiveWhenLowered or self:isLowered(false)) and self.startActivationTime <= g_currentMission.time);
132
133 if self.doGroundManipulation then
134 if self.weederColorParticleSystems ~= nil then
135 for _, mapping in pairs(self.weederColorParticleSystems) do
136 local wx, wy, wz = getWorldTranslation(mapping.node);
137
138 local densityBits = getDensityAtWorldPos(g_currentMission.terrainDetailId, wx, wy, wz);
139 local isOnField = densityBits ~= 0;
140
141 if isOnField then
142 mapping.lastColor[1], mapping.lastColor[2], mapping.lastColor[3], _ = Utils.getTireTrackColorFromDensityBits(densityBits)
143 else
144 mapping.lastColor[1], mapping.lastColor[2], mapping.lastColor[3], _, _ = getTerrainAttributesAtWorldPos(g_currentMission.terrainRootNode, wx, wy, wz, true, true, true, true, false);
145 end
146
147 -- interpolate between different ground colors to avoid unrealisitic particle color changes
148 if mapping.targetColor == nil then
149 mapping.targetColor = {mapping.lastColor[1],mapping.lastColor[2],mapping.lastColor[3]};
150 mapping.currentColor = {mapping.lastColor[1],mapping.lastColor[2],mapping.lastColor[3]};
151 mapping.alpha = 1;
152 end;
153
154 if mapping.alpha ~= 1 then
155 mapping.alpha = math.min(mapping.alpha + dt/1000, 1);
156 mapping.currentColor = {Utils.vector3ArrayLerp(mapping.lastColor, mapping.targetColor, mapping.alpha)};
157 if mapping.alpha == 1 then
158 mapping.lastColor = {mapping.currentColor[1], mapping.currentColor[2], mapping.currentColor[3]};
159 end;
160 end;
161
162 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
163 mapping.alpha = 0;
164 mapping.targetColor = {mapping.lastColor[1], mapping.lastColor[2], mapping.lastColor[3]};
165 end;
166
167 setShaderParameter(mapping.particleSystem.shape, "psColor", mapping.currentColor[1], mapping.currentColor[2], mapping.currentColor[3], 1, false);
168 end;
169 end
170
171 self.isWeederSpeedLimitActive = true;
172 if self.isServer then
173 local workAreas, showWarning, _ = self:getTypedNetworkAreas(WorkArea.AREATYPE_WEEDER, true);
174 showFieldNotOwnedWarning = showWarning;
175
176 if #workAreas > 0 then
177 local totalNumPixels = self:processWeederAreas(workAreas);
178 local ha = Utils.areaToHa(totalNumPixels, g_currentMission:getFruitPixelsToSqm());
179 g_currentMission.missionStats:updateStats("workedHectares", ha);
180 end;
181 end;
182 g_currentMission.missionStats:updateStats("workedTime", dt/(1000*60));
183 end
184
185 if self.isClient then
186 if self.doGroundManipulation and self:getLastSpeed() > 3 then
187 if self:getIsActiveForSound() then
188 SoundUtil.playSample(self.sampleWeeder, 0, 0, nil);
189 end;
190 else
191 SoundUtil.stopSample(self.sampleWeeder);
192 end;
193 end;
194
195 if self.isServer then
196 if showFieldNotOwnedWarning ~= self.showFieldNotOwnedWarning then
197 self.showFieldNotOwnedWarning = showFieldNotOwnedWarning;
198 self:raiseDirtyFlags(self.weederDirtyFlag);
199 end
200 end
201 end;
202end

draw

Description
Called on draw
Definition
draw()
Code
206function Weeder:draw()
207 if self.isClient then
208 if self.showFieldNotOwnedWarning then
209 g_currentMission:showBlinkingWarning(g_i18n:getText("warning_youDontOwnThisField"));
210 end
211 end
212end

onAttach

Description
Called if vehicle gets attached
Definition
onAttach(table attacherVehicle)
Arguments
tableattacherVehicleattacher vehicle
Code
217function Weeder:onAttach(attacherVehicle)
218 self.startActivationTime = g_currentMission.time + self.startActivationTimeout;
219end;

onDeactivate

Description
Called on deactivate
Definition
onDeactivate()
Code
223function Weeder:onDeactivate()
224 self.showFieldNotOwnedWarning = false;
225end

onDeactivateSounds

Description
Called on deactivating sounds
Definition
onDeactivateSounds()
Code
229function Weeder:onDeactivateSounds()
230 if self.isClient then
231 SoundUtil.stopSample(self.sampleWeeder, true);
232 end
233end

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
241function Weeder:loadWorkAreaFromXML(superFunc, workArea, xmlFile, key)
242 local retValue = true;
243 if superFunc ~= nil then
244 retValue = superFunc(self, workArea, xmlFile, key)
245 end
246
247 if workArea.type == WorkArea.AREATYPE_DEFAULT then
248 workArea.type = WorkArea.AREATYPE_WEEDER;
249 end
250
251 return retValue;
252end

getIsWorkAreaActive

Description
Returns true if work area is active
Definition
getIsWorkAreaActive(table workArea)
Arguments
tableworkAreaworkArea
Return Values
booleanisActivework area is active
Code
258function Weeder:getIsWorkAreaActive(superFunc, workArea)
259 if workArea.type == WorkArea.AREATYPE_WEEDER and (workArea.refNode ~= nil and workArea.refNode.isActive) then
260 return true
261 end
262
263 if superFunc ~= nil then
264 return superFunc(self, workArea);
265 end
266 return true;
267end;

getDoGroundManipulation

Description
Returns if tool does ground manipulation
Definition
getDoGroundManipulation()
Return Values
booleandoGroundManipulationdo ground manipulation
Code
272function Weeder:getDoGroundManipulation(superFunc)
273 if not self.doGroundManipulation then
274 return false;
275 end;
276
277 if superFunc ~= nil then
278 return superFunc(self);
279 end
280 return true;
281end;

getDirtMultiplier

Description
Returns current dirt multiplier
Definition
getDirtMultiplier()
Return Values
floatdirtMultipliercurrent dirt multiplier
Code
286function Weeder:getDirtMultiplier(superFunc)
287 local multiplier = 0;
288 if superFunc ~= nil then
289 multiplier = multiplier + superFunc(self);
290 end;
291
292 if self.doGroundManipulation then
293 multiplier = multiplier + self.workMultiplier * self:getLastSpeed() / self.speedLimit;
294 end;
295
296 return multiplier;
297end;

doCheckSpeedLimit

Description
Returns if speed limit should be checked
Definition
doCheckSpeedLimit()
Return Values
booleancheckSpeedlimitcheck speed limit
Code
302function Weeder:doCheckSpeedLimit(superFunc)
303 local parent = false;
304 if superFunc ~= nil then
305 parent = superFunc(self);
306 end
307
308 return parent or self.isWeederSpeedLimitActive;
309end;

getDefaultSpeedLimit

Description
Returns default speed limit
Definition
getDefaultSpeedLimit()
Return Values
floatspeedLimitspeed limit
Code
314function Weeder.getDefaultSpeedLimit()
315 return 15;
316end

processWeederAreas

Description
Process weeder areas
Definition
processWeederAreas(table workAreas)
Arguments
tableworkAreaswork areas to process
Return Values
floattotalNumPixelstotal number of pixels changed
Code
322function Weeder:processWeederAreas(workAreas)
323 local totalNumPixels = 0;
324 for _, area in pairs(workAreas) do
325 totalNumPixels = totalNumPixels + Utils.updateWeederArea(area[1], area[2], area[3], area[4], area[5], area[6]);
326 end;
327 return totalNumPixels;
328end;

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
338function Weeder:loadGroundParticleMapping(superFunc, xmlFile, key, mapping, index, i3dNode)
339 if superFunc ~= nil then
340 if not superFunc(self, xmlFile, key, mapping, index, i3dNode) then
341 return false
342 end
343 end;
344
345 mapping.adjustColor = Utils.getNoNil(getXMLBool(xmlFile, key.."#adjustColor"), false)
346 if mapping.adjustColor then
347 if self.weederColorParticleSystems == nil then
348 self.weederColorParticleSystems = {}
349 end
350 mapping.lastColor = {}
351
352 table.insert(self.weederColorParticleSystems, mapping)
353 end
354
355 return true;
356end;