LUADOC - Farming Simulator 22

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
289function ReceivingHopper.actionEventToggleBoxCreation(self, actionName, inputValue, callbackState, isAnalog)
290 local spec = self.spec_receivingHopper
291 self:setCreateBoxes(not spec.createBoxes)
292end

collisionTestCallback

Description
Definition
collisionTestCallback()
Code
197function ReceivingHopper:collisionTestCallback(transformId)
198 if g_currentMission.nodeToObject[transformId] ~= nil or g_currentMission.players[transformId] ~= nil then
199 if g_currentMission.nodeToObject[transformId] ~= self then
200 local spec = self.spec_receivingHopper
201 spec.foundObjectAtSpawnPlace = true
202 end
203 end
204end

createBox

Description
Create box
Definition
createBox()
Code
208function ReceivingHopper:createBox()
209 local spec = self.spec_receivingHopper
210 if self.isServer then
211 if spec.createBoxes then
212 local fillType = self:getFillUnitFillType(spec.fillUnitIndex)
213 if spec.boxes[fillType] ~= nil then
214 local x, _, z = getWorldTranslation(spec.spawnPlace)
215 local dirX, _, dirZ = localDirectionToWorld(spec.spawnPlace, 0, 0, 1)
216 local yRot = MathUtil.getYRotationFromDirection(dirX, dirZ)
217 local xmlFilename = Utils.getFilename(spec.boxes[fillType], self.baseDirectory)
218 local location = {x=x, z=z, yRot=yRot}
219
220 spec.creatingBox = true
221 VehicleLoadingUtil.loadVehicle(xmlFilename, location, true, 0, Vehicle.PROPERTY_STATE_OWNED, self:getOwnerFarmId(), nil, nil, self.onCreateBoxFinished, self)
222 end
223 end
224 end
225end

getCanBeSelected

Description
Definition
getCanBeSelected()
Code
261function ReceivingHopper:getCanBeSelected(superFunc)
262 return true
263end

getCanSpawnNextBox

Description
Definition
getCanSpawnNextBox()
Code
165function ReceivingHopper:getCanSpawnNextBox()
166 local spec = self.spec_receivingHopper
167
168 if spec.creatingBox then
169 return false
170 end
171
172 local fillType = self:getFillUnitFillType(spec.fillUnitIndex)
173 if spec.boxes[fillType] ~= nil then
174 if spec.lastBox ~= nil then
175 if spec.lastBox:getFillUnitFreeCapacity(1) > 0 then
176 return false
177 end
178 end
179
180 local xmlFilename = Utils.getFilename(spec.boxes[fillType], self.baseDirectory)
181 local size = StoreItemUtil.getSizeValues(xmlFilename, "vehicle", 0)
182
183 local x,y,z = getWorldTranslation(spec.spawnPlace)
184 local rx,ry,rz = getWorldRotation(spec.spawnPlace)
185
186 spec.foundObjectAtSpawnPlace = false
187 overlapBox(x, y, z, rx, ry, rz, size.width*0.5, 2, size.length*0.5, "collisionTestCallback", self, 5468288)
188
189 return not spec.foundObjectAtSpawnPlace
190 end
191
192 return false
193end

handleDischargeRaycast

Description
Definition
handleDischargeRaycast()
Code
240function ReceivingHopper:handleDischargeRaycast(superFunc, dischargeNode, hitObject, hitShape, hitDistance, hitFillUnitIndex, hitTerrain)
241 local stopDischarge = false
242 if hitObject ~= nil then
243 local fillType = self:getDischargeFillType(dischargeNode)
244 local allowFillType = hitObject:getFillUnitAllowsFillType(hitFillUnitIndex, fillType)
245 if allowFillType and hitObject:getFillUnitFreeCapacity(hitFillUnitIndex) > 0 then
246 self:setDischargeState(Dischargeable.DISCHARGE_STATE_OBJECT, true)
247 else
248 stopDischarge = true
249 end
250 else
251 stopDischarge = true
252 end
253
254 if stopDischarge and self:getDischargeState() == Dischargeable.DISCHARGE_STATE_OBJECT then
255 self:setDischargeState(Dischargeable.DISCHARGE_STATE_OFF, true)
256 end
257end

initSpecialization

Description
Definition
initSpecialization()
Code
25function ReceivingHopper.initSpecialization()
26 local schema = Vehicle.xmlSchema
27 schema:setXMLSpecializationType("ReceivingHopper")
28
29 schema:register(XMLValueType.INT, "vehicle.receivingHopper#fillUnitIndex", "Fill unit index", 1)
30 schema:register(XMLValueType.NODE_INDEX, "vehicle.receivingHopper.boxes#spawnPlaceNode", "Spawn place node")
31 schema:register(XMLValueType.STRING, "vehicle.receivingHopper.boxes.box(?)#fillType", "Fill type name")
32 schema:register(XMLValueType.STRING, "vehicle.receivingHopper.boxes.box(?)#filename", "Box filename")
33
34 schema:setXMLSpecializationType()
35
36 local schemaSavegame = Vehicle.xmlSchemaSavegame
37 schemaSavegame:register(XMLValueType.BOOL, "vehicles.vehicle(?).receivingHopper#createBoxes", "Create boxes")
38end

onCreateBoxFinished

Description
Definition
onCreateBoxFinished()
Code
229function ReceivingHopper:onCreateBoxFinished(vehicle, vehicleLoadState, arguments)
230 local spec = self.spec_receivingHopper
231 spec.creatingBox = false
232
233 if vehicleLoadState == VehicleLoadingUtil.VEHICLE_LOAD_OK then
234 spec.lastBox = vehicle
235 end
236end

onFillUnitFillLevelChanged

Description
Definition
onFillUnitFillLevelChanged()
Code
281function ReceivingHopper:onFillUnitFillLevelChanged(fillUnitIndex, fillLevelDelta, fillType, toolType, fillPositionData, appliedDelta)
282 if self:getFillUnitFillLevel(fillUnitIndex) > 0 then
283 self:raiseActive()
284 end
285end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
69function ReceivingHopper:onLoad(savegame)
70 local spec = self.spec_receivingHopper
71
72 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.receivingHopper#unloadingDelay", "Dischargeable functionalities") -- FS17 to FS19
73 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.receivingHopper#unloadInfoIndex", "Dischargeable functionalities") -- FS17 to FS19
74 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.receivingHopper#dischargeInfoIndex", "Dischargeable functionalities") -- FS17 to FS19
75 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.receivingHopper.tipTrigger#index", "Dischargeable functionalities") -- FS17 to FS19
76 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.receivingHopper.boxTrigger#index", "Dischargeable functionalities") -- FS17 to FS19
77 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.receivingHopper.fillScrollerNodes.fillScrollerNode", "Dischargeable functionalities") -- FS17 to FS19
78 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.receivingHopper.fillEffect", "Dischargeable functionalities") -- FS17 to FS19
79 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.receivingHopper.fillEffect", "Dischargeable functionalities") -- FS17 to FS19
80 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.receivingHopper.boxTrigger#litersPerMinute", "Dischargeable functionalities") -- FS17 to FS19
81 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.receivingHopper.raycastNode#index", "Dischargeable functionalities") -- FS17 to FS19
82 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.receivingHopper.raycastNode#raycastLength", "Dischargeable functionalities") -- FS17 to FS19
83
84 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.receivingHopper.boxTrigger#boxSpawnPlaceIndex", "vehicle.receivingHopper.boxes#spawnPlaceNode") -- FS17 to FS19
85 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, "vehicle.receivingHopper.boxTrigger.box(0)", "vehicle.receivingHopper.boxes.box(0)") -- FS17 to FS19
86
87 spec.fillUnitIndex = self.xmlFile:getValue("vehicle.receivingHopper#fillUnitIndex", 1)
88 spec.spawnPlace = self.xmlFile:getValue("vehicle.receivingHopper.boxes#spawnPlaceNode", nil, self.components, self.i3dMappings)
89
90 spec.boxes = {}
91 local i = 0
92 while true do
93 local baseName = string.format("vehicle.receivingHopper.boxes.box(%d)", i)
94 if not self.xmlFile:hasProperty(baseName) then
95 break
96 end
97
98 local fillTypeStr = self.xmlFile:getValue(baseName.."#fillType")
99 local filename = self.xmlFile:getValue(baseName.."#filename")
100 local fillTypeIndex = g_fillTypeManager:getFillTypeIndexByName(fillTypeStr)
101 if fillTypeIndex ~= nil then
102 spec.boxes[fillTypeIndex] = filename
103 else
104 Logging.xmlWarning(self.xmlFile, "Invalid fillType '%s'", fillTypeStr)
105 end
106
107 i = i + 1
108 end
109
110 spec.createBoxes = false
111 spec.lastBox = nil
112 spec.creatingBox = false
113
114 if savegame ~= nil then
115 spec.createBoxes = savegame.xmlFile:getValue(savegame.key..".receivingHopper#createBoxes", spec.createBoxes)
116 end
117
118 if not self.isServer then
119 SpecializationUtil.removeEventListener(self, "onUpdateTick", ReceivingHopper)
120 end
121end

onRegisterActionEvents

Description
Definition
onRegisterActionEvents()
Code
267function ReceivingHopper:onRegisterActionEvents(isActiveForInput, isActiveForInputIgnoreSelection)
268 if self.isClient then
269 local spec = self.spec_receivingHopper
270 self:clearActionEventsTable(spec.actionEvents)
271 if isActiveForInputIgnoreSelection then
272 local _, actionEventId = self:addActionEvent(spec.actionEvents, InputAction.IMPLEMENT_EXTRA, self, ReceivingHopper.actionEventToggleBoxCreation, false, true, false, true, nil)
273 g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_NORMAL)
274 ReceivingHopper.updateActionEvents(self)
275 end
276 end
277end

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
136function ReceivingHopper:onUpdateTick(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
137 local spec = self.spec_receivingHopper
138 if spec.createBoxes then
139 if self:getDischargeState() == Dischargeable.DISCHARGE_STATE_OFF then
140 if self:getCanSpawnNextBox() then
141 self:createBox()
142 end
143 end
144 end
145end

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
59function ReceivingHopper.registerEventListeners(vehicleType)
60 SpecializationUtil.registerEventListener(vehicleType, "onLoad", ReceivingHopper)
61 SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", ReceivingHopper)
62 SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", ReceivingHopper)
63 SpecializationUtil.registerEventListener(vehicleType, "onFillUnitFillLevelChanged", ReceivingHopper)
64end

registerFunctions

Description
Definition
registerFunctions()
Code
42function ReceivingHopper.registerFunctions(vehicleType)
43 SpecializationUtil.registerFunction(vehicleType, "setCreateBoxes", ReceivingHopper.setCreateBoxes)
44 SpecializationUtil.registerFunction(vehicleType, "getCanSpawnNextBox", ReceivingHopper.getCanSpawnNextBox)
45 SpecializationUtil.registerFunction(vehicleType, "collisionTestCallback", ReceivingHopper.collisionTestCallback)
46 SpecializationUtil.registerFunction(vehicleType, "createBox", ReceivingHopper.createBox)
47 SpecializationUtil.registerFunction(vehicleType, "onCreateBoxFinished", ReceivingHopper.onCreateBoxFinished)
48end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
52function ReceivingHopper.registerOverwrittenFunctions(vehicleType)
53 SpecializationUtil.registerOverwrittenFunction(vehicleType, "handleDischargeRaycast", ReceivingHopper.handleDischargeRaycast)
54 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getCanBeSelected", ReceivingHopper.getCanBeSelected)
55end

saveToXMLFile

Description
Definition
saveToXMLFile()
Code
125function ReceivingHopper:saveToXMLFile(xmlFile, key, usedModNames)
126 local spec = self.spec_receivingHopper
127
128 xmlFile:setValue(key.."#createBoxes", spec.createBoxes)
129end

setCreateBoxes

Description
Toggle box creating
Definition
setCreateBoxes(boolean state, boolean noEventSend)
Arguments
booleanstatenew state
booleannoEventSendno event send
Code
151function ReceivingHopper:setCreateBoxes(state, noEventSend)
152 local spec = self.spec_receivingHopper
153 if state ~= spec.createBoxes then
154 ReceivingHopperSetCreateBoxesEvent.sendEvent(self, state, noEventSend)
155
156 spec.createBoxes = state
157 ReceivingHopper.updateActionEvents(self)
158
159 spec.lastBox = nil
160 end
161end

updateActionEvents

Description
Definition
updateActionEvents()
Code
296function ReceivingHopper.updateActionEvents(self)
297 local spec = self.spec_receivingHopper
298 local actionEvent = spec.actionEvents[InputAction.IMPLEMENT_EXTRA]
299 if actionEvent ~= nil then
300 if spec.createBoxes then
301 g_inputBinding:setActionEventText(actionEvent.actionEventId, g_i18n:getText("action_disablePalletSpawning"))
302 else
303 g_inputBinding:setActionEventText(actionEvent.actionEventId, g_i18n:getText("action_enablePalletSpawning"))
304 end
305 end
306end