LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

ExtendedSowingMachine

Description
Specialization to track seed usage
Functions

actionEventChangeSeedRate

Description
Definition
actionEventChangeSeedRate()
Code
236function ExtendedSowingMachine.actionEventChangeSeedRate(self, actionName, inputValue, callbackState, isAnalog, ...)
237 self:setManualSeedRate(self.spec_extendedSowingMachine.manualSeedRate + MathUtil.sign(inputValue))
238end

actionEventToggleAuto

Description
Definition
actionEventToggleAuto()
Code
230function ExtendedSowingMachine.actionEventToggleAuto(self, actionName, inputValue, callbackState, isAnalog)
231 self:setSeedRateAutoMode()
232end

onEndWorkAreaProcessing

Description
Definition
onEndWorkAreaProcessing()
Code
116function ExtendedSowingMachine:onEndWorkAreaProcessing(dt, hasProcessed)
117 local spec = self.spec_sowingMachine
118 if self.isServer then
119 if spec.workAreaParameters.lastChangedArea > 0 then
120 local fruitDesc = g_fruitTypeManager:getFruitTypeByIndex(spec.workAreaParameters.seedsFruitType)
121 local realHa = MathUtil.areaToHa(self.spec_extendedSowingMachine.lastRealChangedArea, g_currentMission:getFruitPixelsToSqm())
122 local lastHa = MathUtil.areaToHa(spec.workAreaParameters.lastChangedArea, g_currentMission:getFruitPixelsToSqm())
123 local usage = fruitDesc.seedUsagePerSqm * lastHa * 10000
124 local usageRegular = fruitDesc.seedUsagePerSqm * realHa * 10000
125
126 local damage = self:getVehicleDamage()
127 if damage > 0 then
128 usage = usage * (1 + damage * SowingMachine.DAMAGED_USAGE_INCREASE)
129 usageRegular = usageRegular * (1 + damage * SowingMachine.DAMAGED_USAGE_INCREASE)
130 end
131
132 local farmlandStatistics, _, farmlandId = self:getPFStatisticInfo()
133 if farmlandStatistics ~= nil then
134 if farmlandId ~= nil then
135 farmlandStatistics:updateStatistic(farmlandId, "usedSeeds", usage)
136 farmlandStatistics:updateStatistic(farmlandId, "usedSeedsRegular", usageRegular)
137 end
138 end
139 end
140 end
141end

onRegisterActionEvents

Description
Definition
onRegisterActionEvents()
Code
145function ExtendedSowingMachine:onRegisterActionEvents(isActiveForInput, isActiveForInputIgnoreSelection)
146 if self.isClient then
147 local spec = self.spec_extendedSowingMachine
148 self:clearActionEventsTable(spec.actionEvents)
149
150 if isActiveForInputIgnoreSelection then
151 local _, actionEventId = self:addActionEvent(spec.actionEvents, spec.inputActionToggleAuto, self, ExtendedSowingMachine.actionEventToggleAuto, false, true, false, true, nil)
152 g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_HIGH)
153
154 _, actionEventId = self:addActionEvent(spec.actionEvents, spec.inputActionToggleRate, self, ExtendedSowingMachine.actionEventChangeSeedRate, false, true, false, true, nil)
155 g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_HIGH)
156 g_inputBinding:setActionEventText(actionEventId, spec.texts.inputChangeSeedRate)
157
158 ExtendedSowingMachine.updateActionEventState(self)
159 end
160
161 spec.attachStateChanged = true
162 end
163end

onUpdate

Description
Definition
onUpdate()
Code
78function ExtendedSowingMachine:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
79 local spec = self.spec_extendedSowingMachine
80 spec.lastGroundUpdateDistance = spec.lastGroundUpdateDistance + self.lastMovedDistance
81 if spec.lastGroundUpdateDistance > spec.groundUpdateDistance then
82 spec.lastGroundUpdateDistance = 0
83
84 local workArea = self:getWorkAreaByIndex(1)
85 if workArea ~= nil then
86 local x, z
87
88 -- if the work area starts in the middle of the vehicle we use the start node, otherwise the middle between start and width
89 local lx, _, _ = localToLocal(workArea.start, self.rootNode, 0, 0, 0)
90 if math.abs(lx) < 0.5 then
91 x, _, z = getWorldTranslation(workArea.start)
92 else
93 local x1, _, z1 = getWorldTranslation(workArea.start)
94 local x2, _, z2 = getWorldTranslation(workArea.width)
95 x, z = (x1 + x2) * 0.5, (z1 + z2) * 0.5
96 end
97
98 local isOnField, _ = FSDensityMapUtil.getFieldDataAtWorldPosition(x, 0, z)
99 if isOnField then
100 local soilTypeIndex = spec.soilMap:getTypeIndexAtWorldPos(x, z)
101 if soilTypeIndex > 0 then
102 local fruitTypeIndex = self.spec_sowingMachine.seeds[self.spec_sowingMachine.currentSeed]
103 spec.seedRateRecommendation = spec.seedRateMap:getOptimalSeedRateByFruitTypeAndSoiltype(fruitTypeIndex, soilTypeIndex)
104 else
105 spec.seedRateRecommendation = nil
106 end
107 else
108 spec.seedRateRecommendation = nil
109 end
110 end
111 end
112end

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
27function ExtendedSowingMachine.prerequisitesPresent(specializations)
28 return SpecializationUtil.hasSpecialization(SowingMachine, specializations) and SpecializationUtil.hasSpecialization(PrecisionFarmingStatistic, specializations)
29end

processSowingMachineArea

Description
Definition
processSowingMachineArea()
Code
198function ExtendedSowingMachine:processSowingMachineArea(superFunc, workArea, dt)
199 local changedArea, totalArea = superFunc(self, workArea, dt)
200 if changedArea > 0 then
201 local spec = self.spec_extendedSowingMachine
202 local specSowingMachine = self.spec_sowingMachine
203 local workAreaParameters = specSowingMachine.workAreaParameters
204
205 local sx, _, sz = getWorldTranslation(workArea.start)
206 local wx, _, wz = getWorldTranslation(workArea.width)
207 local hx, _, hz = getWorldTranslation(workArea.height)
208
209 local fruitType = workAreaParameters.seedsFruitType
210
211 local realUsage, realSeedRate, realSeedRateIndex = spec.seedRateMap:updateSeedArea(sx, sz, wx, wz, hx, hz, fruitType, spec.seedRateAutoMode, spec.manualSeedRate)
212
213 if realSeedRateIndex ~= nil then
214 local fruitDesc = g_fruitTypeManager:getFruitTypeByIndex(fruitType)
215 local usageOffset = realUsage / fruitDesc.seedUsagePerSqm
216
217 spec.lastRealChangedArea = workAreaParameters.lastChangedArea
218 workAreaParameters.lastChangedArea = workAreaParameters.lastChangedArea * usageOffset
219
220 spec.lastSeedRate = realSeedRate
221 spec.lastSeedRateIndex = realSeedRateIndex
222 end
223 end
224
225 return changedArea, totalArea
226end

setManualSeedRate

Description
Definition
setManualSeedRate()
Code
186function ExtendedSowingMachine:setManualSeedRate(seedRate, noEventSend)
187 local spec = self.spec_extendedSowingMachine
188 seedRate = MathUtil.clamp(seedRate, ExtendedSowingMachine.MIN_SEED_RATE, ExtendedSowingMachine.MAX_SEED_RATE)
189 if seedRate ~= spec.manualSeedRate then
190 spec.manualSeedRate = seedRate
191
192 ExtendedSowingMachineRateEvent.sendEvent(self, spec.seedRateAutoMode, spec.manualSeedRate, noEventSend)
193 end
194end

setSeedRateAutoMode

Description
Definition
setSeedRateAutoMode()
Code
167function ExtendedSowingMachine:setSeedRateAutoMode(state, noEventSend)
168 local spec = self.spec_extendedSowingMachine
169 if state == nil then
170 state = not spec.seedRateAutoMode
171 end
172
173 if state ~= spec.seedRateAutoMode then
174 spec.seedRateAutoMode = state
175
176 if self.isClient then
177 ExtendedSowingMachine.updateActionEventState(self)
178 end
179
180 ExtendedSowingMachineRateEvent.sendEvent(self, spec.seedRateAutoMode, spec.manualSeedRate, noEventSend)
181 end
182end

updateActionEventState

Description
Definition
updateActionEventState()
Code
242function ExtendedSowingMachine.updateActionEventState(self)
243 local spec = self.spec_extendedSowingMachine
244 local actionEventToggleAuto = spec.actionEvents[spec.inputActionToggleAuto]
245 if actionEventToggleAuto ~= nil then
246 g_inputBinding:setActionEventText(actionEventToggleAuto.actionEventId, spec.seedRateAutoMode and spec.texts.inputToggleAutoModeNeg or spec.texts.inputToggleAutoModePos)
247 end
248
249 local actionEventToggleRate = spec.actionEvents[spec.inputActionToggleRate]
250 if actionEventToggleRate ~= nil then
251 g_inputBinding:setActionEventActive(actionEventToggleRate.actionEventId, not spec.seedRateAutoMode)
252 end
253end