LUADOC - Farming Simulator 19

Script v1.7.1.0

Engine v1.7.1.0

Foundation Reference

ReceivingHopper

Description
Specialization for receiving hoppers filling tipped filltypes into boxes (e.g. potatoes)
Functions

actionEventToggleBoxCreation

Description
Definition
actionEventToggleBoxCreation()
Code
253function ReceivingHopper.actionEventToggleBoxCreation(self, actionName, inputValue, callbackState, isAnalog)
254 local spec = self.spec_receivingHopper
255 self:setCreateBoxes(not spec.createBoxes)
256end

collisionTestCallback

Description
Definition
collisionTestCallback()
Code
172function ReceivingHopper:collisionTestCallback(transformId)
173 if g_currentMission.nodeToObject[transformId] ~= nil or g_currentMission.players[transformId] ~= nil or g_currentMission:getNodeObject(transformId) ~= nil then
174 if g_currentMission.nodeToObject[transformId] ~= self then
175 local spec = self.spec_receivingHopper
176 spec.foundObjectAtSpawnPlace = true
177 end
178 end
179end

createBox

Description
Create box
Definition
createBox()
Code
183function ReceivingHopper:createBox()
184 local spec = self.spec_receivingHopper
185 if self.isServer then
186 if spec.createBoxes then
187 local fillType = self:getFillUnitFillType(spec.fillUnitIndex)
188 if spec.boxes[fillType] ~= nil then
189 local x, _, z = getWorldTranslation(spec.spawnPlace)
190 local dirX, _, dirZ = localDirectionToWorld(spec.spawnPlace, 0, 1, 0)
191 local yRot = MathUtil.getYRotationFromDirection(dirX, dirZ);
192 local xmlFilename = Utils.getFilename(spec.boxes[fillType], self.baseDirectory)
193 local vehicle = g_currentMission:loadVehicle(xmlFilename, x, nil, z, 0, yRot, true, 0, Vehicle.PROPERTY_STATE_OWNED, self:getOwnerFarmId(), nil, nil)
194 if vehicle ~= nil then
195 spec.lastBox = vehicle
196 end
197 end
198 end
199 end
200end

getCanBeSelected

Description
Definition
getCanBeSelected()
Code
225function ReceivingHopper:getCanBeSelected(superFunc)
226 return true
227end

getCanSpawnNextBox

Description
Definition
getCanSpawnNextBox()
Code
144function ReceivingHopper:getCanSpawnNextBox()
145 local spec = self.spec_receivingHopper
146
147 local fillType = self:getFillUnitFillType(spec.fillUnitIndex)
148 if spec.boxes[fillType] ~= nil then
149 if spec.lastBox ~= nil then
150 if spec.lastBox:getFillUnitFreeCapacity(1) > 0 then
151 return false
152 end
153 end
154
155 local xmlFilename = Utils.getFilename(spec.boxes[fillType], self.baseDirectory)
156 local sizeWidth, sizeLength, _, _ = StoreItemUtil.getSizeValues(xmlFilename, "vehicle", 0)
157
158 local x,y,z = getWorldTranslation(spec.spawnPlace)
159 local rx,ry,rz = getWorldRotation(spec.spawnPlace)
160
161 spec.foundObjectAtSpawnPlace = false
162 overlapBox(x, y, z, rx, ry, rz, sizeWidth*0.5, 2, sizeLength*0.5, "collisionTestCallback", self, 5468288)
163
164 return not spec.foundObjectAtSpawnPlace
165 end
166
167 return false
168end

handleDischargeRaycast

Description
Definition
handleDischargeRaycast()
Code
204function ReceivingHopper:handleDischargeRaycast(superFunc, dischargeNode, hitObject, hitShape, hitDistance, hitFillUnitIndex, hitTerrain)
205 local stopDischarge = false
206 if hitObject ~= nil then
207 local fillType = self:getDischargeFillType(dischargeNode)
208 local allowFillType = hitObject:getFillUnitAllowsFillType(hitFillUnitIndex, fillType)
209 if allowFillType and hitObject:getFillUnitFreeCapacity(hitFillUnitIndex) > 0 then
210 self:setDischargeState(Dischargeable.DISCHARGE_STATE_OBJECT, true)
211 else
212 stopDischarge = true
213 end
214 else
215 stopDischarge = true
216 end
217
218 if stopDischarge and self:getDischargeState() == Dischargeable.DISCHARGE_STATE_OBJECT then
219 self:setDischargeState(Dischargeable.DISCHARGE_STATE_OFF, true)
220 end
221end

onFillUnitFillLevelChanged

Description
Definition
onFillUnitFillLevelChanged()
Code
245function ReceivingHopper:onFillUnitFillLevelChanged(fillUnitIndex, fillLevelDelta, fillType, toolType, fillPositionData, appliedDelta)
246 if self:getFillUnitFillLevel(fillUnitIndex) > 0 then
247 self:raiseActive()
248 end
249end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
51function ReceivingHopper:onLoad(savegame)
52 local spec = self.spec_receivingHopper
53
54 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.receivingHopper#unloadingDelay", "Dischargeable functionalities") -- FS17 to FS19
55 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.receivingHopper#unloadInfoIndex", "Dischargeable functionalities") -- FS17 to FS19
56 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.receivingHopper#dischargeInfoIndex", "Dischargeable functionalities") -- FS17 to FS19
57 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.receivingHopper.tipTrigger#index", "Dischargeable functionalities") -- FS17 to FS19
58 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.receivingHopper.boxTrigger#index", "Dischargeable functionalities") -- FS17 to FS19
59 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.receivingHopper.fillScrollerNodes.fillScrollerNode", "Dischargeable functionalities") -- FS17 to FS19
60 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.receivingHopper.fillEffect", "Dischargeable functionalities") -- FS17 to FS19
61 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.receivingHopper.fillEffect", "Dischargeable functionalities") -- FS17 to FS19
62 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.receivingHopper.boxTrigger#litersPerMinute", "Dischargeable functionalities") -- FS17 to FS19
63 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.receivingHopper.raycastNode#index", "Dischargeable functionalities") -- FS17 to FS19
64 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.receivingHopper.raycastNode#raycastLength", "Dischargeable functionalities") -- FS17 to FS19
65
66 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.receivingHopper.boxTrigger#boxSpawnPlaceIndex", "vehicle.receivingHopper.boxes#spawnPlaceNode") -- FS17 to FS19
67 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.receivingHopper.boxTrigger.box(0)", "vehicle.receivingHopper.boxes.box(0)") -- FS17 to FS19
68
69 spec.fillUnitIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.receivingHopper#fillUnitIndex"), 1)
70 spec.spawnPlace = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.receivingHopper.boxes#spawnPlaceNode"), self.i3dMappings)
71
72 spec.boxes = {}
73 local i = 0
74 while true do
75 local baseName = string.format("vehicle.receivingHopper.boxes.box(%d)", i)
76 if not hasXMLProperty(self.xmlFile, baseName) then
77 break
78 end
79
80 local fillTypeStr = getXMLString(self.xmlFile, baseName.."#fillType")
81 local filename = getXMLString(self.xmlFile, baseName.."#filename")
82 local fillTypeIndex = g_fillTypeManager:getFillTypeIndexByName(fillTypeStr)
83 if fillTypeIndex ~= nil then
84 spec.boxes[fillTypeIndex] = filename
85 else
86 g_logManager:xmlWarning(self.configFileName, "Invalid fillType '%s'", fillTypeStr)
87 end
88
89 i = i + 1
90 end
91
92 spec.createBoxes = false
93 spec.lastBox = nil
94
95 if savegame ~= nil then
96 spec.createBoxes = Utils.getNoNil(getXMLBool(savegame.xmlFile, savegame.key..".receivingHopper#createBoxes"), spec.createBoxes)
97 end
98end

onRegisterActionEvents

Description
Definition
onRegisterActionEvents()
Code
231function ReceivingHopper:onRegisterActionEvents(isActiveForInput, isActiveForInputIgnoreSelection)
232 if self.isClient then
233 local spec = self.spec_receivingHopper
234 self:clearActionEventsTable(spec.actionEvents)
235 if isActiveForInputIgnoreSelection then
236 local _, actionEventId = self:addActionEvent(spec.actionEvents, InputAction.IMPLEMENT_EXTRA, self, ReceivingHopper.actionEventToggleBoxCreation, false, true, false, true, nil)
237 g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_NORMAL)
238 ReceivingHopper.updateActionEvents(self)
239 end
240 end
241end

onUpdateTick

Description
Called on update tick
Definition
onUpdateTick(float dt, boolean isActiveForInput, boolean isSelected)
Arguments
floatdttime since last call in ms
booleanisActiveForInputtrue if vehicle is active for input
booleanisSelectedtrue if vehicle is selected
Code
113function ReceivingHopper:onUpdateTick(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
114 if self.isServer then
115 local spec = self.spec_receivingHopper
116 if spec.createBoxes then
117 if self:getDischargeState() == Dischargeable.DISCHARGE_STATE_OFF then
118 if self:getCanSpawnNextBox() then
119 self:createBox()
120 end
121 end
122 end
123 end
124end

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
19function ReceivingHopper.prerequisitesPresent(specializations)
20 return SpecializationUtil.hasSpecialization(FillUnit, specializations) and SpecializationUtil.hasSpecialization(Dischargeable, specializations)
21end

registerEventListeners

Description
Definition
registerEventListeners()
Code
41function ReceivingHopper.registerEventListeners(vehicleType)
42 SpecializationUtil.registerEventListener(vehicleType, "onLoad", ReceivingHopper)
43 SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", ReceivingHopper)
44 SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", ReceivingHopper)
45 SpecializationUtil.registerEventListener(vehicleType, "onFillUnitFillLevelChanged", ReceivingHopper)
46end

registerFunctions

Description
Definition
registerFunctions()
Code
25function ReceivingHopper.registerFunctions(vehicleType)
26 SpecializationUtil.registerFunction(vehicleType, "setCreateBoxes", ReceivingHopper.setCreateBoxes)
27 SpecializationUtil.registerFunction(vehicleType, "getCanSpawnNextBox", ReceivingHopper.getCanSpawnNextBox)
28 SpecializationUtil.registerFunction(vehicleType, "collisionTestCallback", ReceivingHopper.collisionTestCallback)
29 SpecializationUtil.registerFunction(vehicleType, "createBox", ReceivingHopper.createBox)
30end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
34function ReceivingHopper.registerOverwrittenFunctions(vehicleType)
35 SpecializationUtil.registerOverwrittenFunction(vehicleType, "handleDischargeRaycast", ReceivingHopper.handleDischargeRaycast)
36 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getCanBeSelected", ReceivingHopper.getCanBeSelected)
37end

saveToXMLFile

Description
Definition
saveToXMLFile()
Code
102function ReceivingHopper:saveToXMLFile(xmlFile, key, usedModNames)
103 local spec = self.spec_receivingHopper
104
105 setXMLBool(xmlFile, key.."#createBoxes", spec.createBoxes)
106end

setCreateBoxes

Description
Toggle box creating
Definition
setCreateBoxes(boolean state, boolean noEventSend)
Arguments
booleanstatenew state
booleannoEventSendno event send
Code
130function ReceivingHopper:setCreateBoxes(state, noEventSend)
131 local spec = self.spec_receivingHopper
132 if state ~= spec.createBoxes then
133 ReceivingHopperSetCreateBoxesEvent.sendEvent(self, state, noEventSend)
134
135 spec.createBoxes = state
136 ReceivingHopper.updateActionEvents(self)
137
138 spec.lastBox = nil
139 end
140end

updateActionEvents

Description
Definition
updateActionEvents()
Code
260function ReceivingHopper.updateActionEvents(self)
261 local spec = self.spec_receivingHopper
262 local actionEvent = spec.actionEvents[InputAction.IMPLEMENT_EXTRA]
263 if actionEvent ~= nil then
264 if spec.createBoxes then
265 g_inputBinding:setActionEventText(actionEvent.actionEventId, g_i18n:getText("action_disablePalletSpawning"))
266 else
267 g_inputBinding:setActionEventText(actionEvent.actionEventId, g_i18n:getText("action_enablePalletSpawning"))
268 end
269 end
270end