LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

Plow

Description
Specialization for plows providing additional folding/rotating functionality
Functions

actionControllerRotateEvent

Description
Definition
actionControllerRotateEvent()
Code
947function Plow.actionControllerRotateEvent(self, direction)
948 local spec = self.spec_plow
949 if spec.rotationPart.turnAnimation ~= nil then
950 if self:getIsPlowRotationAllowed() then
951 if spec.rotationMax then
952 self:setRotationMax(false)
953 end
954 end
955 end
956
957 return true
958end

actionEventLimitToField

Description
Definition
actionEventLimitToField()
Code
938function Plow.actionEventLimitToField(self, actionName, inputValue, callbackState, isAnalog)
939 local spec = self.spec_plow
940 if not self:getPlowForceLimitToField() then
941 self:setPlowLimitToField(not spec.limitToField)
942 end
943end

actionEventTurn

Description
Definition
actionEventTurn()
Code
927function Plow.actionEventTurn(self, actionName, inputValue, callbackState, isAnalog)
928 local spec = self.spec_plow
929 if spec.rotationPart.turnAnimation ~= nil then
930 if self:getIsPlowRotationAllowed() then
931 self:setRotationMax(not spec.rotationMax)
932 end
933 end
934end

doCheckSpeedLimit

Description
Definition
doCheckSpeedLimit()
Code
609function Plow:doCheckSpeedLimit(superFunc)
610 return superFunc(self) or (self.spec_plow.onlyActiveWhenLowered and self:getIsImplementChainLowered())
611end

getAIInvertMarkersOnTurn

Description
Definition
getAIInvertMarkersOnTurn()
Code
666function Plow:getAIInvertMarkersOnTurn(superFunc, turnLeft)
667 local spec = self.spec_plow
668 if spec.rotationPart.turnAnimation ~= nil then
669 if turnLeft then
670 return spec.rotationMax == spec.rotateLeftToMax
671 else
672 return spec.rotationMax ~= spec.rotateLeftToMax
673 end
674 end
675
676 return false
677end

getAllowsLowering

Description
Returns true if lowering is allowed
Definition
getAllowsLowering()
Return Values
booleanallowedlowering is allowed
Code
707function Plow:getAllowsLowering(superFunc)
708 local spec = self.spec_plow
709 if self:getIsAnimationPlaying(spec.rotationPart.turnAnimation) then
710 return false
711 end
712
713 return superFunc(self)
714end

getCanAIImplementContinueWork

Description
Definition
getCanAIImplementContinueWork()
Code
655function Plow:getCanAIImplementContinueWork(superFunc)
656 local canContinue, stopAI, stopReason = superFunc(self)
657 if not canContinue then
658 return false, stopAI, stopReason
659 end
660
661 return not self:getIsAnimationPlaying(self.spec_plow.rotationPart.turnAnimation)
662end

getCanBeSelected

Description
Definition
getCanBeSelected()
Code
681function Plow:getCanBeSelected(superFunc)
682 return true
683end

getDefaultSpeedLimit

Description
Definition
getDefaultSpeedLimit()
Code
627function Plow.getDefaultSpeedLimit()
628 return 15
629end

getDirtMultiplier

Description
Definition
getDirtMultiplier()
Code
527function Plow:getDirtMultiplier(superFunc)
528 local multiplier = superFunc(self)
529
530 local spec = self.spec_plow
531 if spec.isWorking then
532 multiplier = multiplier + self:getWorkDirtMultiplier() * self:getLastSpeed() / self.speedLimit
533 end
534
535 return multiplier
536end

getIsAIPreparingToDrive

Description
Definition
getIsAIPreparingToDrive()
Code
729function Plow:getIsAIPreparingToDrive(superFunc)
730 local spec = self.spec_plow
731 if self:getIsAnimationPlaying(spec.rotationPart.turnAnimation) then
732 return true
733 end
734
735 return superFunc(self)
736end

getIsAIReadyToDrive

Description
Definition
getIsAIReadyToDrive()
Code
718function Plow:getIsAIReadyToDrive(superFunc)
719 local spec = self.spec_plow
720 if spec.rotationMax or self:getIsAnimationPlaying(spec.rotationPart.turnAnimation) then
721 return false
722 end
723
724 return superFunc(self)
725end

getIsFoldAllowed

Description
Definition
getIsFoldAllowed()
Code
487function Plow:getIsFoldAllowed(superFunc, direction, onAiTurnOn)
488 local spec = self.spec_plow
489
490 if spec.rotationPart.limitFoldRotationMax ~= nil and spec.rotationPart.limitFoldRotationMax == spec.rotationMax then
491 return false, spec.texts.warningFoldingPlowTurned
492 end
493
494 if spec.rotationPart.turnAnimation ~= nil and self.getAnimationTime ~= nil then
495 local rotationTime = self:getAnimationTime(spec.rotationPart.turnAnimation)
496 if rotationTime > spec.rotationPart.foldRotationMaxLimit or rotationTime < spec.rotationPart.foldRotationMinLimit then
497 return false, spec.texts.warningFoldingPlowTurned
498 end
499 end
500
501 if not spec.rotationPart.rotationAllowedIfLowered and self.getIsLowered ~= nil and self:getIsLowered() then
502 return false, spec.texts.warningFoldingLowered
503 end
504
505 return superFunc(self, direction, onAiTurnOn)
506end

getIsFoldMiddleAllowed

Description
Definition
getIsFoldMiddleAllowed()
Code
510function Plow:getIsFoldMiddleAllowed(superFunc)
511 local spec = self.spec_plow
512
513 if spec.rotationPart.limitFoldRotationMax ~= nil and spec.rotationPart.limitFoldRotationMax == spec.rotationMax then
514 return false
515 end
516 if spec.rotationPart.turnAnimation ~= nil and self.getAnimationTime ~= nil then
517 local rotationTime = self:getAnimationTime(spec.rotationPart.turnAnimation)
518 if rotationTime > spec.rotationPart.foldRotationMaxLimit or rotationTime < spec.rotationPart.foldRotationMinLimit then
519 return false
520 end
521 end
522 return superFunc(self)
523end

getIsPlowRotationAllowed

Description
Definition
getIsPlowRotationAllowed()
Code
439function Plow:getIsPlowRotationAllowed()
440 local spec = self.spec_plow
441
442 if self.getFoldAnimTime ~= nil then
443 local foldAnimTime = self:getFoldAnimTime()
444 if foldAnimTime > spec.rotationPart.rotationFoldMaxLimit or foldAnimTime < spec.rotationPart.rotationFoldMinLimit then
445 return false
446 end
447 end
448
449 if not spec.rotationPart.rotationAllowedIfLowered and self.getIsLowered ~= nil and self:getIsLowered() then
450 return false
451 end
452
453 return true
454end

getIsSpeedRotatingPartActive

Description
Definition
getIsSpeedRotatingPartActive()
Code
569function Plow:getIsSpeedRotatingPartActive(superFunc, speedRotatingPart)
570 local spec = self.spec_plow
571
572 if spec.rotationPart.turnAnimation ~= nil and speedRotatingPart.disableOnTurn then
573 local turnAnimTime = self:getAnimationTime(spec.rotationPart.turnAnimation)
574 if turnAnimTime ~= nil then
575 local enabled
576 if speedRotatingPart.turnAnimLimitSide < 0 then
577 enabled = (turnAnimTime <= speedRotatingPart.turnAnimLimit)
578 elseif speedRotatingPart.turnAnimLimitSide > 0 then
579 enabled = (1-turnAnimTime <= speedRotatingPart.turnAnimLimit)
580 else
581 enabled = (turnAnimTime <= speedRotatingPart.turnAnimLimit or 1-turnAnimTime <= speedRotatingPart.turnAnimLimit)
582 end
583 if not enabled then
584 return false
585 end
586 end
587 end
588
589 return superFunc(self, speedRotatingPart)
590end

getIsWorkAreaActive

Description
Definition
getIsWorkAreaActive()
Code
633function Plow:getIsWorkAreaActive(superFunc, workArea)
634 if not superFunc(self, workArea) then
635 return false
636 end
637
638 local spec = self.spec_plow
639
640 if spec.startActivationTime > g_currentMission.time then
641 return false
642 end
643
644 if spec.onlyActiveWhenLowered and self.getIsLowered ~= nil then
645 if not self:getIsLowered(false) then
646 return false
647 end
648 end
649
650 return true
651end

getPlowForceLimitToField

Description
Returns if plow limit to field is forced and not changeable
Definition
getPlowForceLimitToField()
Return Values
booleanisForcedis forced
Code
466function Plow:getPlowForceLimitToField()
467 return self.spec_plow.forceLimitToField
468end

getPlowLimitToField

Description
Returns if plow is limited to the field
Definition
getPlowLimitToField()
Return Values
booleanisLimitedis limited to field
Code
459function Plow:getPlowLimitToField()
460 return self.spec_plow.limitToField
461end

getSpeedRotatingPartDirection

Description
Definition
getSpeedRotatingPartDirection()
Code
594function Plow:getSpeedRotatingPartDirection(superFunc, speedRotatingPart)
595 local spec = self.spec_plow
596
597 if spec.rotationPart.turnAnimation ~= nil then
598 local turnAnimTime = self:getAnimationTime(spec.rotationPart.turnAnimation)
599 if turnAnimTime > 0.5 and speedRotatingPart.invertDirectionOnRotation then
600 return -1
601 end
602 end
603
604 return superFunc(self, speedRotatingPart)
605end

getWearMultiplier

Description
Returns current wear multiplier
Definition
getWearMultiplier()
Return Values
floatdirtMultipliercurrent wear multiplier
Code
541function Plow:getWearMultiplier(superFunc)
542 local multiplier = superFunc(self)
543
544 local spec = self.spec_plow
545 if spec.isWorking then
546 multiplier = multiplier + self:getWorkWearMultiplier() * self:getLastSpeed() / self.speedLimit
547 end
548
549 return multiplier
550end

initSpecialization

Description
Definition
initSpecialization()
Code
35function Plow.initSpecialization()
36 g_configurationManager:addConfigurationType("plow", g_i18n:getText("configuration_design"), "plow", nil, nil, nil, ConfigurationUtil.SELECTOR_MULTIOPTION)
37 g_workAreaTypeManager:addWorkAreaType("plow", true)
38
39 local schema = Vehicle.xmlSchema
40 schema:setXMLSpecializationType("Plow")
41
42 Plow.registerXMLPaths(schema, "vehicle.plow")
43 Plow.registerXMLPaths(schema, "vehicle.plow.plowConfigurations.plowConfiguration(?)")
44
45 ObjectChangeUtil.registerObjectChangeXMLPaths(schema, "vehicle.plow.plowConfigurations.plowConfiguration(?)")
46
47 schema:register(XMLValueType.BOOL, SpeedRotatingParts.SPEED_ROTATING_PART_XML_KEY .. "#disableOnTurn", "Disable while turning", true)
48 schema:register(XMLValueType.FLOAT, SpeedRotatingParts.SPEED_ROTATING_PART_XML_KEY .. "#turnAnimLimit", "Turn animation limit", 0)
49 schema:register(XMLValueType.FLOAT, SpeedRotatingParts.SPEED_ROTATING_PART_XML_KEY .. "#turnAnimLimitSide", "Turn animation limit side", 0)
50 schema:register(XMLValueType.BOOL, SpeedRotatingParts.SPEED_ROTATING_PART_XML_KEY .. "#invertDirectionOnRotation", "Invert direction on rotation", true)
51
52 schema:setXMLSpecializationType()
53
54 local schemaSavegame = Vehicle.xmlSchemaSavegame
55 schemaSavegame:register(XMLValueType.BOOL, "vehicles.vehicle(?).plow#rotationMax", "Rotation max.")
56 schemaSavegame:register(XMLValueType.FLOAT, "vehicles.vehicle(?).plow#turnAnimTime", "Turn animation time")
57end

isDetachAllowed

Description
Returns true if detach is allowed
Definition
isDetachAllowed()
Return Values
booleandetachAlloweddetach is allowed
Code
688function Plow:isDetachAllowed(superFunc)
689 local spec = self.spec_plow
690 if self:getIsAnimationPlaying(spec.rotationPart.turnAnimation) then
691 return false
692 end
693
694 if spec.rotationPart.turnAnimation ~= nil then
695 local animTime = self:getAnimationTime(spec.rotationPart.turnAnimation)
696 if animTime < spec.rotationPart.detachMinLimit or animTime > spec.rotationPart.detachMaxLimit then
697 return false, spec.rotationPart.detachWarning, true
698 end
699 end
700
701 return superFunc(self)
702end

loadSpeedRotatingPartFromXML

Description
Definition
loadSpeedRotatingPartFromXML()
Code
554function Plow:loadSpeedRotatingPartFromXML(superFunc, speedRotatingPart, xmlFile, key)
555 if not superFunc(self, speedRotatingPart, xmlFile, key) then
556 return false
557 end
558
559 speedRotatingPart.disableOnTurn = xmlFile:getValue(key .. "#disableOnTurn", true)
560 speedRotatingPart.turnAnimLimit = xmlFile:getValue(key .. "#turnAnimLimit", 0)
561 speedRotatingPart.turnAnimLimitSide = xmlFile:getValue(key .. "#turnAnimLimitSide", 0)
562 speedRotatingPart.invertDirectionOnRotation = xmlFile:getValue(key .. "#invertDirectionOnRotation", true)
563
564 return true
565end

loadWorkAreaFromXML

Description
Definition
loadWorkAreaFromXML()
Code
615function Plow:loadWorkAreaFromXML(superFunc, workArea, xmlFile, key)
616 local retValue = superFunc(self, workArea, xmlFile, key)
617
618 if workArea.type == WorkAreaType.DEFAULT then
619 workArea.type = WorkAreaType.PLOW
620 end
621
622 return retValue
623end

onAIImplementStartTurn

Description
Definition
onAIImplementStartTurn()
Code
863function Plow:onAIImplementStartTurn()
864 self.spec_plow.ai.lastHeadlandPosition = 0
865end

onAIImplementTurnProgress

Description
Definition
onAIImplementTurnProgress()
Code
869function Plow:onAIImplementTurnProgress(progress, left)
870 local spec = self.spec_plow
871
872 if spec.ai.lastHeadlandPosition < spec.ai.rotateToCenterHeadlandPos and progress > spec.ai.rotateToCenterHeadlandPos
873 and progress < spec.ai.rotateCompletelyHeadlandPos then
874 self:setRotationCenter()
875 elseif spec.ai.lastHeadlandPosition < spec.ai.rotateCompletelyHeadlandPos and progress > spec.ai.rotateCompletelyHeadlandPos then
876 self:setRotationMax(left)
877 end
878
879 spec.ai.lastHeadlandPosition = progress
880end

onDeactivate

Description
Definition
onDeactivate()
Code
853function Plow:onDeactivate()
854 if self.isClient then
855 local spec = self.spec_plow
856 g_soundManager:stopSamples(spec.samples)
857 spec.isWorkSamplePlaying = false
858 end
859end

onDelete

Description
Definition
onDelete()
Code
260function Plow:onDelete()
261 local spec = self.spec_plow
262 g_soundManager:deleteSamples(spec.samples)
263end

onEndWorkAreaProcessing

Description
Definition
onEndWorkAreaProcessing()
Code
785function Plow:onEndWorkAreaProcessing(dt)
786 local spec = self.spec_plow
787
788 if self.isServer then
789 local stats = g_currentMission:farmStats(self:getLastTouchedFarmlandFarmId())
790
791 local lastStatsArea = spec.workAreaParameters.lastStatsArea
792 if lastStatsArea > 0 then
793 local ha = MathUtil.areaToHa(lastStatsArea, g_currentMission:getFruitPixelsToSqm()) -- 4096px are mapped to 2048m
794 stats:updateStats("plowedHectares", ha)
795 self:updateLastWorkedArea(lastStatsArea)
796 end
797
798 if spec.isWorking then
799 stats:updateStats("plowedTime", dt/(1000*60))
800 end
801 end
802
803 if self.isClient then
804 if spec.isWorking then
805 if not spec.isWorkSamplePlaying then
806 g_soundManager:playSample(spec.samples.work)
807 spec.isWorkSamplePlaying = true
808 end
809 else
810 if spec.isWorkSamplePlaying then
811 g_soundManager:stopSample(spec.samples.work)
812 spec.isWorkSamplePlaying = false
813 end
814 end
815 end
816end

onFinishAnimation

Description
Definition
onFinishAnimation()
Code
893function Plow:onFinishAnimation(animName)
894 local spec = self.spec_plow
895 if animName == spec.rotationPart.turnAnimation then
896 g_soundManager:stopSample(spec.samples.turn)
897 end
898end

onLoad

Description
Definition
onLoad()
Code
154function Plow:onLoad(savegame)
155
156 if self:getGroundReferenceNodeFromIndex(1) == nil then
157 print("Warning: No ground reference nodes in "..self.configFileName)
158 end
159
160 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.rotationPart", "vehicle.plow.rotationPart") --FS17 to FS19
161 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.ploughDirectionNode#index", "vehicle.plow.directionNode#node") --FS17 to FS19
162 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.rotateLeftToMax#value", "vehicle.plow.rotateLeftToMax#value") --FS17 to FS19
163 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.animTimeCenterPosition#value", "vehicle.plow.ai#centerPosition") --FS17 to FS19
164 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.aiPlough#rotateEarly", "vehicle.plow.ai#rotateCompletelyHeadlandPos") --FS17 to FS19
165 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.onlyActiveWhenLowered#value", "vehicle.plow.onlyActiveWhenLowered#value") --FS17 to FS19
166
167 local plowConfigurationId = self.configurations["plow"] or 1
168 local configKey = string.format("vehicle.plow.plowConfigurations.plowConfiguration(%d)", plowConfigurationId - 1)
169 ObjectChangeUtil.updateObjectChanges(self.xmlFile, "vehicle.plow.plowConfigurations.plowConfiguration", plowConfigurationId, self.components, self)
170
171 if not self.xmlFile:hasProperty(configKey) then
172 configKey = "vehicle.plow"
173 end
174
175 local spec = self.spec_plow
176
177 spec.rotationPart = {}
178 spec.rotationPart.turnAnimation = self.xmlFile:getValue(configKey .. ".rotationPart#turnAnimationName")
179 spec.rotationPart.foldMinLimit = self.xmlFile:getValue(configKey .. ".rotationPart#foldMinLimit", 0)
180 spec.rotationPart.foldMaxLimit = self.xmlFile:getValue(configKey .. ".rotationPart#foldMaxLimit", 1)
181 spec.rotationPart.limitFoldRotationMax = self.xmlFile:getValue(configKey .. ".rotationPart#limitFoldRotationMax")
182 spec.rotationPart.foldRotationMinLimit = self.xmlFile:getValue(configKey .. ".rotationPart#foldRotationMinLimit", 0)
183 spec.rotationPart.foldRotationMaxLimit = self.xmlFile:getValue(configKey .. ".rotationPart#foldRotationMaxLimit", 1)
184 spec.rotationPart.rotationFoldMinLimit = self.xmlFile:getValue(configKey .. ".rotationPart#rotationFoldMinLimit", 0)
185 spec.rotationPart.rotationFoldMaxLimit = self.xmlFile:getValue(configKey .. ".rotationPart#rotationFoldMaxLimit", 1)
186 spec.rotationPart.detachMinLimit = self.xmlFile:getValue(configKey .. ".rotationPart#detachMinLimit", 0)
187 spec.rotationPart.detachMaxLimit = self.xmlFile:getValue(configKey .. ".rotationPart#detachMaxLimit", 1)
188 spec.rotationPart.rotationAllowedIfLowered = self.xmlFile:getValue(configKey .. ".rotationPart#rotationAllowedIfLowered", true)
189
190 spec.rotationPart.detachWarning = string.format(self.xmlFile:getValue(configKey .. ".rotationPart#detachWarning", "warning_detachNotAllowedPlowTurn", self.customEnvironment, false))
191
192 spec.directionNode = self.xmlFile:getValue(configKey .. ".directionNode#node", self.components[1].node, self.components, self.i3dMappings)
193
194 self:setPlowAIRequirements()
195
196 spec.ai = {}
197 spec.ai.centerPosition = self.xmlFile:getValue(configKey .. ".ai#centerPosition", 0.5)
198 spec.ai.rotateToCenterHeadlandPos = self.xmlFile:getValue(configKey .. ".ai#rotateToCenterHeadlandPos", 0.5)
199 spec.ai.rotateCompletelyHeadlandPos = self.xmlFile:getValue(configKey .. ".ai#rotateCompletelyHeadlandPos", 0.5)
200 spec.ai.lastHeadlandPosition = 0
201
202 spec.rotateLeftToMax = self.xmlFile:getValue(configKey .. ".rotateLeftToMax#value", true)
203 spec.onlyActiveWhenLowered = self.xmlFile:getValue(configKey .. ".onlyActiveWhenLowered#value", true)
204 spec.rotationMax = false
205 spec.startActivationTimeout = 2000
206 spec.startActivationTime = 0
207 spec.lastPlowArea = 0
208 spec.limitToField = true
209 spec.forceLimitToField = false
210 spec.wasTurnAnimationStopped = false
211 spec.isWorking = false
212
213 if self.isClient then
214 spec.samples = {}
215 spec.samples.turn = g_soundManager:loadSampleFromXML(self.xmlFile, configKey .. ".sounds", "turn", self.baseDirectory, self.components, 0, AudioGroup.VEHICLE, self.i3dMappings, self)
216 spec.samples.work = g_soundManager:loadSampleFromXML(self.xmlFile, configKey .. ".sounds", "work", self.baseDirectory, self.components, 0, AudioGroup.VEHICLE, self.i3dMappings, self)
217 spec.isWorkSamplePlaying = false
218 end
219
220 spec.texts = {}
221 spec.texts.warningFoldingLowered = g_i18n:getText("warning_foldingNotWhileLowered")
222 spec.texts.warningFoldingPlowTurned = g_i18n:getText("warning_foldingNotWhilePlowTurned")
223 spec.texts.turnPlow = g_i18n:getText("action_turnPlow")
224 spec.texts.allowCreateFields = g_i18n:getText("action_allowCreateFields")
225 spec.texts.limitToFields = g_i18n:getText("action_limitToFields")
226
227 spec.workAreaParameters = {}
228 spec.workAreaParameters.limitToField = self:getPlowLimitToField()
229 spec.workAreaParameters.forceLimitToField = self:getPlowForceLimitToField()
230 spec.workAreaParameters.angle = 0
231 spec.workAreaParameters.lastChangedArea = 0
232 spec.workAreaParameters.lastStatsArea = 0
233 spec.workAreaParameters.lastTotalArea = 0
234
235 if not self.isClient then
236 SpecializationUtil.removeEventListener(self, "onUpdate", Plow)
237 end
238end

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
823function Plow:onPostAttach(attacherVehicle, inputJointDescIndex, jointDescIndex)
824 local spec = self.spec_plow
825
826 spec.startActivationTime = g_currentMission.time + spec.startActivationTimeout
827 if spec.wasTurnAnimationStopped then
828 local dir = 1
829 if not spec.rotationMax then
830 dir = -1
831 end
832 self:playAnimation(spec.rotationPart.turnAnimation, dir, self:getAnimationTime(spec.rotationPart.turnAnimation), true)
833 spec.wasTurnAnimationStopped = false
834 end
835end

onPostLoad

Description
Definition
onPostLoad()
Code
242function Plow:onPostLoad(savegame)
243 if savegame ~= nil and not savegame.resetVehicles then
244 local rotationMax = savegame.xmlFile:getValue(savegame.key..".plow#rotationMax")
245 if rotationMax ~= nil then
246 if self:getIsPlowRotationAllowed() then
247 local plowTurnAnimTime = savegame.xmlFile:getValue(savegame.key..".plow#turnAnimTime")
248 self:setRotationMax(rotationMax, true, plowTurnAnimTime)
249
250 if self.updateCylinderedInitial ~= nil then
251 self:updateCylinderedInitial(false)
252 end
253 end
254 end
255 end
256end

onPreDetach

Description
Called if vehicle gets detached
Definition
onPreDetach(table attacherVehicle, table implement)
Arguments
tableattacherVehicleattacher vehicle
tableimplementimplement
Code
841function Plow:onPreDetach(attacherVehicle, implement)
842 local spec = self.spec_plow
843
844 spec.limitToField = true
845 if self:getIsAnimationPlaying(spec.rotationPart.turnAnimation) then
846 self:stopAnimation(spec.rotationPart.turnAnimation, true)
847 spec.wasTurnAnimationStopped = true
848 end
849end

onReadStream

Description
Definition
onReadStream()
Code
278function Plow:onReadStream(streamId, connection)
279 local spec = self.spec_plow
280
281 local rotationMax = streamReadBool(streamId)
282 local turnAnimTime
283 if spec.rotationPart.turnAnimation ~= nil and self.playAnimation ~= nil then
284 turnAnimTime = streamReadFloat32(streamId)
285 end
286
287 self:setRotationMax(rotationMax, true, turnAnimTime)
288
289 if self.updateCylinderedInitial ~= nil then
290 self:updateCylinderedInitial(false)
291 end
292end

onRegisterActionEvents

Description
Definition
onRegisterActionEvents()
Code
740function Plow:onRegisterActionEvents(isActiveForInput, isActiveForInputIgnoreSelection)
741 if self.isClient then
742 local spec = self.spec_plow
743 self:clearActionEventsTable(spec.actionEvents)
744
745 if isActiveForInputIgnoreSelection then
746 if spec.rotationPart.turnAnimation ~= nil then
747 local _, actionEventId = self:addPoweredActionEvent(spec.actionEvents, InputAction.IMPLEMENT_EXTRA, self, Plow.actionEventTurn, false, true, false, true, nil)
748 g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_HIGH)
749 g_inputBinding:setActionEventText(actionEventId, spec.texts.turnPlow)
750 end
751
752 local _, actionEventId = self:addActionEvent(spec.actionEvents, InputAction.IMPLEMENT_EXTRA3, self, Plow.actionEventLimitToField, false, true, false, true, nil)
753 g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_NORMAL)
754 end
755 end
756end

onRootVehicleChanged

Description
Called if root vehicle changes
Definition
onRootVehicleChanged(table rootVehicle)
Arguments
tablerootVehicleroot vehicle
Code
903function Plow:onRootVehicleChanged(rootVehicle)
904 local spec = self.spec_foldable
905 if #spec.foldingParts > 0 then
906 local actionController = rootVehicle.actionController
907 if actionController ~= nil then
908 if spec.controlledActionRotate ~= nil then
909 spec.controlledActionRotate:updateParent(actionController)
910 return
911 end
912
913 spec.controlledActionRotate = actionController:registerAction("rotatePlow", nil, 3)
914 spec.controlledActionRotate:setCallback(self, Plow.actionControllerRotateEvent)
915 spec.controlledActionRotate:setFinishedFunctions(self, function(vehicle) return not vehicle.spec_foldable.rotationMax end, false, false)
916 spec.controlledActionRotate:addAIEventListener(self, "onAIImplementPrepare", -1, true)
917 else
918 if spec.controlledActionRotate ~= nil then
919 spec.controlledActionRotate:remove()
920 end
921 end
922 end
923end

onStartAnimation

Description
Definition
onStartAnimation()
Code
884function Plow:onStartAnimation(animName)
885 local spec = self.spec_plow
886 if animName == spec.rotationPart.turnAnimation then
887 g_soundManager:playSample(spec.samples.turn)
888 end
889end

onStartWorkAreaProcessing

Description
Definition
onStartWorkAreaProcessing()
Code
760function Plow:onStartWorkAreaProcessing(dt)
761 local spec = self.spec_plow
762
763 spec.isWorking = false
764
765 local limitToField = self:getPlowLimitToField()
766 local limitFruitDestructionToField = limitToField
767 if not g_currentMission:getHasPlayerPermission("createFields", self:getOwner(), nil, true) then
768 limitToField = true
769 limitFruitDestructionToField = true
770 end
771
772 local dx,_,dz = localDirectionToWorld(spec.directionNode, 0, 0, 1)
773 local angle = FSDensityMapUtil.convertToDensityMapAngle(MathUtil.getYRotationFromDirection(dx, dz), g_currentMission.fieldGroundSystem:getGroundAngleMaxValue())
774
775 spec.workAreaParameters.limitToField = limitToField
776 spec.workAreaParameters.limitFruitDestructionToField = limitFruitDestructionToField
777 spec.workAreaParameters.angle = angle
778 spec.workAreaParameters.lastChangedArea = 0
779 spec.workAreaParameters.lastStatsArea = 0
780 spec.workAreaParameters.lastTotalArea = 0
781end

onUpdate

Description
Definition
onUpdate()
Code
308function Plow:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
309 if self.isClient then
310 local spec = self.spec_plow
311
312 local actionEvent = spec.actionEvents[InputAction.IMPLEMENT_EXTRA3]
313 if actionEvent ~= nil then
314 if not self:getPlowForceLimitToField() and g_currentMission:getHasPlayerPermission("createFields", self:getOwner()) then
315 g_inputBinding:setActionEventActive(actionEvent.actionEventId, true)
316
317 if self:getPlowLimitToField() then
318 g_inputBinding:setActionEventText(actionEvent.actionEventId, spec.texts.allowCreateFields)
319 else
320 g_inputBinding:setActionEventText(actionEvent.actionEventId, spec.texts.limitToFields)
321 end
322 else
323 g_inputBinding:setActionEventActive(actionEvent.actionEventId, false)
324 end
325 end
326
327 if spec.rotationPart.turnAnimation ~= nil then
328 actionEvent = spec.actionEvents[InputAction.IMPLEMENT_EXTRA]
329 if actionEvent ~= nil then
330 g_inputBinding:setActionEventActive(actionEvent.actionEventId, self:getIsPlowRotationAllowed())
331 end
332 end
333 end
334end

onWriteStream

Description
Definition
onWriteStream()
Code
296function Plow:onWriteStream(streamId, connection)
297 local spec = self.spec_plow
298
299 streamWriteBool(streamId, spec.rotationMax)
300 if spec.rotationPart.turnAnimation ~= nil and self.playAnimation ~= nil then
301 local turnAnimTime = self:getAnimationTime(spec.rotationPart.turnAnimation)
302 streamWriteFloat32(streamId, turnAnimTime)
303 end
304end

prerequisitesPresent

Description
Definition
prerequisitesPresent()
Code
91function Plow.prerequisitesPresent(specializations)
92 return SpecializationUtil.hasSpecialization(WorkArea, specializations)
93end

processPlowArea

Description
Definition
processPlowArea()
Code
338function Plow:processPlowArea(workArea, dt)
339 local spec = self.spec_plow
340
341 local xs,_,zs = getWorldTranslation(workArea.start)
342 local xw,_,zw = getWorldTranslation(workArea.width)
343 local xh,_,zh = getWorldTranslation(workArea.height)
344
345 local params = spec.workAreaParameters
346 local changedArea, totalArea = 0, 0
347
348 if self.tailwaterDepth < 0.1 then
349 changedArea, totalArea = FSDensityMapUtil.updatePlowArea(xs,zs, xw,zw, xh,zh, not params.limitToField, params.limitFruitDestructionToField, params.angle)
350 changedArea = changedArea + FSDensityMapUtil.updateVineCultivatorArea(xs, zs, xw, zw, xh, zh)
351 end
352
353 params.lastChangedArea = params.lastChangedArea + changedArea
354 params.lastStatsArea = params.lastStatsArea + changedArea
355 params.lastTotalArea = params.lastTotalArea + totalArea
356
357 FSDensityMapUtil.eraseTireTrack(xs,zs, xw,zw, xh,zh)
358
359 spec.isWorking = self:getLastSpeed() > 0.5
360
361 return changedArea, totalArea
362end

registerEventListeners

Description
Definition
registerEventListeners()
Code
132function Plow.registerEventListeners(vehicleType)
133 SpecializationUtil.registerEventListener(vehicleType, "onLoad", Plow)
134 SpecializationUtil.registerEventListener(vehicleType, "onPostLoad", Plow)
135 SpecializationUtil.registerEventListener(vehicleType, "onDelete", Plow)
136 SpecializationUtil.registerEventListener(vehicleType, "onReadStream", Plow)
137 SpecializationUtil.registerEventListener(vehicleType, "onWriteStream", Plow)
138 SpecializationUtil.registerEventListener(vehicleType, "onUpdate", Plow)
139 SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", Plow)
140 SpecializationUtil.registerEventListener(vehicleType, "onStartWorkAreaProcessing", Plow)
141 SpecializationUtil.registerEventListener(vehicleType, "onEndWorkAreaProcessing", Plow)
142 SpecializationUtil.registerEventListener(vehicleType, "onPostAttach", Plow)
143 SpecializationUtil.registerEventListener(vehicleType, "onPreDetach", Plow)
144 SpecializationUtil.registerEventListener(vehicleType, "onDeactivate", Plow)
145 SpecializationUtil.registerEventListener(vehicleType, "onAIImplementStartTurn", Plow)
146 SpecializationUtil.registerEventListener(vehicleType, "onAIImplementTurnProgress", Plow)
147 SpecializationUtil.registerEventListener(vehicleType, "onStartAnimation", Plow)
148 SpecializationUtil.registerEventListener(vehicleType, "onFinishAnimation", Plow)
149 SpecializationUtil.registerEventListener(vehicleType, "onRootVehicleChanged", Plow)
150end

registerFunctions

Description
Definition
registerFunctions()
Code
97function Plow.registerFunctions(vehicleType)
98 SpecializationUtil.registerFunction(vehicleType, "processPlowArea", Plow.processPlowArea)
99 SpecializationUtil.registerFunction(vehicleType, "setRotationMax", Plow.setRotationMax)
100 SpecializationUtil.registerFunction(vehicleType, "setRotationCenter", Plow.setRotationCenter)
101 SpecializationUtil.registerFunction(vehicleType, "setPlowLimitToField", Plow.setPlowLimitToField)
102 SpecializationUtil.registerFunction(vehicleType, "getIsPlowRotationAllowed", Plow.getIsPlowRotationAllowed)
103 SpecializationUtil.registerFunction(vehicleType, "getPlowLimitToField", Plow.getPlowLimitToField)
104 SpecializationUtil.registerFunction(vehicleType, "getPlowForceLimitToField", Plow.getPlowForceLimitToField)
105 SpecializationUtil.registerFunction(vehicleType, "setPlowAIRequirements", Plow.setPlowAIRequirements)
106end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
110function Plow.registerOverwrittenFunctions(vehicleType)
111 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getIsFoldAllowed", Plow.getIsFoldAllowed)
112 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getIsFoldMiddleAllowed", Plow.getIsFoldMiddleAllowed)
113 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getDirtMultiplier", Plow.getDirtMultiplier)
114 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getWearMultiplier", Plow.getWearMultiplier)
115 SpecializationUtil.registerOverwrittenFunction(vehicleType, "loadSpeedRotatingPartFromXML", Plow.loadSpeedRotatingPartFromXML)
116 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getIsSpeedRotatingPartActive", Plow.getIsSpeedRotatingPartActive)
117 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getSpeedRotatingPartDirection", Plow.getSpeedRotatingPartDirection)
118 SpecializationUtil.registerOverwrittenFunction(vehicleType, "doCheckSpeedLimit", Plow.doCheckSpeedLimit)
119 SpecializationUtil.registerOverwrittenFunction(vehicleType, "loadWorkAreaFromXML", Plow.loadWorkAreaFromXML)
120 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getIsWorkAreaActive", Plow.getIsWorkAreaActive)
121 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getCanAIImplementContinueWork", Plow.getCanAIImplementContinueWork)
122 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getAIInvertMarkersOnTurn", Plow.getAIInvertMarkersOnTurn)
123 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getCanBeSelected", Plow.getCanBeSelected)
124 SpecializationUtil.registerOverwrittenFunction(vehicleType, "isDetachAllowed", Plow.isDetachAllowed)
125 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getAllowsLowering", Plow.getAllowsLowering)
126 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getIsAIReadyToDrive", Plow.getIsAIReadyToDrive)
127 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getIsAIPreparingToDrive", Plow.getIsAIPreparingToDrive)
128end

registerXMLPaths

Description
Definition
registerXMLPaths()
Code
61function Plow.registerXMLPaths(schema, basePath)
62 schema:register(XMLValueType.STRING, basePath .. ".rotationPart#turnAnimationName", "Turn animation name")
63 schema:register(XMLValueType.FLOAT, basePath .. ".rotationPart#foldMinLimit", "Fold min. limit", 0)
64 schema:register(XMLValueType.FLOAT, basePath .. ".rotationPart#foldMaxLimit", "Fold max. limit", 1)
65 schema:register(XMLValueType.BOOL, basePath .. ".rotationPart#limitFoldRotationMax", "Block folding if in max state")
66 schema:register(XMLValueType.FLOAT, basePath .. ".rotationPart#foldRotationMinLimit", "Fold allow if inbetween this limit", 0)
67 schema:register(XMLValueType.FLOAT, basePath .. ".rotationPart#foldRotationMaxLimit", "Fold allow if inbetween this limit", 1)
68 schema:register(XMLValueType.FLOAT, basePath .. ".rotationPart#rotationFoldMinLimit", "Rotation allow if fold time inbetween this limit", 0)
69 schema:register(XMLValueType.FLOAT, basePath .. ".rotationPart#rotationFoldMaxLimit", "Rotation allow if fold time inbetween this limit", 1)
70 schema:register(XMLValueType.FLOAT, basePath .. ".rotationPart#detachMinLimit", "Detach is allowed if turn animation between these values", 0)
71 schema:register(XMLValueType.FLOAT, basePath .. ".rotationPart#detachMaxLimit", "Detach is allowed if turn animation between these values", 1)
72 schema:register(XMLValueType.BOOL, basePath .. ".rotationPart#rotationAllowedIfLowered", "Allow plow rotation if lowered", true)
73
74 schema:register(XMLValueType.L10N_STRING, basePath .. ".rotationPart#detachWarning", "Warning to be displayed if not in correct turn state for detach", "warning_detachNotAllowedPlowTurn")
75
76 schema:register(XMLValueType.NODE_INDEX, basePath .. ".directionNode#node", "Plow direction node")
77
78 schema:register(XMLValueType.FLOAT, basePath .. ".ai#centerPosition", "Center position", 0.5)
79 schema:register(XMLValueType.FLOAT, basePath .. ".ai#rotateToCenterHeadlandPos", "Rotate to center headland position", 0.5)
80 schema:register(XMLValueType.FLOAT, basePath .. ".ai#rotateCompletelyHeadlandPos", "Rotate completely headland position", 0.5)
81
82 schema:register(XMLValueType.BOOL, basePath .. ".rotateLeftToMax#value", "Rotate left to max", true)
83 schema:register(XMLValueType.BOOL, basePath .. ".onlyActiveWhenLowered#value", "Only active when lowered", true)
84
85 SoundManager.registerSampleXMLPaths(schema, basePath .. ".sounds", "turn")
86 SoundManager.registerSampleXMLPaths(schema, basePath .. ".sounds", "work")
87end

saveToXMLFile

Description
Definition
saveToXMLFile()
Code
267function Plow:saveToXMLFile(xmlFile, key, usedModNames)
268 local spec = self.spec_plow
269 xmlFile:setValue(key.."#rotationMax", spec.rotationMax)
270 if spec.rotationPart.turnAnimation ~= nil and self.playAnimation ~= nil then
271 local turnAnimTime = self:getAnimationTime(spec.rotationPart.turnAnimation)
272 xmlFile:setValue(key.."#turnAnimTime", turnAnimTime)
273 end
274end

setPlowAIRequirements

Description
Sets plow ai requirements and optional exclude the given ground type
Definition
setPlowAIRequirements(table excludedGroundTypes)
Arguments
tableexcludedGroundTypesthese ground types will be excluded
Code
473function Plow:setPlowAIRequirements(excludedGroundTypes)
474 if self.clearAITerrainDetailRequiredRange ~= nil then
475 self:clearAITerrainDetailRequiredRange()
476
477 if excludedGroundTypes ~= nil then
478 self:addAIGroundTypeRequirements(Plow.AI_REQUIRED_GROUND_TYPES, unpack(excludedGroundTypes))
479 else
480 self:addAIGroundTypeRequirements(Plow.AI_REQUIRED_GROUND_TYPES)
481 end
482 end
483end

setPlowLimitToField

Description
Definition
setPlowLimitToField()
Code
411function Plow:setPlowLimitToField(plowLimitToField, noEventSend)
412 local spec = self.spec_plow
413
414 if spec.limitToField ~= plowLimitToField then
415 if noEventSend == nil or noEventSend == false then
416 if g_server ~= nil then
417 g_server:broadcastEvent(PlowLimitToFieldEvent.new(self, plowLimitToField), nil, nil, self)
418 else
419 g_client:getServerConnection():sendEvent(PlowLimitToFieldEvent.new(self, plowLimitToField))
420 end
421 end
422 spec.limitToField = plowLimitToField
423
424 local actionEvent = spec.actionEvents[InputAction.IMPLEMENT_EXTRA3]
425 if actionEvent ~= nil then
426 local text
427 if spec.limitToField then
428 text = spec.texts.allowCreateFields
429 else
430 text = spec.texts.limitToFields
431 end
432 g_inputBinding:setActionEventText(actionEvent.actionEventId, text)
433 end
434 end
435end

setRotationCenter

Description
Definition
setRotationCenter()
Code
394function Plow:setRotationCenter()
395 local spec = self.spec_plow
396
397 if spec.rotationPart.turnAnimation ~= nil then
398 self:setAnimationStopTime(spec.rotationPart.turnAnimation, spec.ai.centerPosition)
399
400 local animTime = self:getAnimationTime(spec.rotationPart.turnAnimation)
401 if animTime < spec.ai.centerPosition then
402 self:playAnimation(spec.rotationPart.turnAnimation, 1, animTime, true)
403 elseif animTime > spec.ai.centerPosition then
404 self:playAnimation(spec.rotationPart.turnAnimation, -1, animTime, true)
405 end
406 end
407end

setRotationMax

Description
Definition
setRotationMax()
Code
366function Plow:setRotationMax(rotationMax, noEventSend, turnAnimationTime)
367 if noEventSend == nil or noEventSend == false then
368 if g_server ~= nil then
369 g_server:broadcastEvent(PlowRotationEvent.new(self, rotationMax), nil, nil, self)
370 else
371 g_client:getServerConnection():sendEvent(PlowRotationEvent.new(self, rotationMax))
372 end
373 end
374 local spec = self.spec_plow
375
376 spec.rotationMax = rotationMax
377
378 if spec.rotationPart.turnAnimation ~= nil then
379 if turnAnimationTime == nil then
380 local animTime = self:getAnimationTime(spec.rotationPart.turnAnimation)
381 if spec.rotationMax then
382 self:playAnimation(spec.rotationPart.turnAnimation, 1, animTime, true)
383 else
384 self:playAnimation(spec.rotationPart.turnAnimation, -1, animTime, true)
385 end
386 else
387 self:setAnimationTime(spec.rotationPart.turnAnimation, turnAnimationTime, true)
388 end
389 end
390end