LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

PlaceableHusbandryPallets

Description
Specialization for placeables
Functions

getAllPalletsCallback

Description
Definition
getAllPalletsCallback()
Code
279function PlaceableHusbandryPallets:getAllPalletsCallback(pallets)
280 local spec = self.spec_husbandryPallets
281 local fillLevel = 0
282 local capacity = 0
283 local maxCapacity = 0
284 spec.isPalletInfoUpdateRunning = false
285
286 for _, pallet in pairs(pallets) do
287 local palletCapacity = pallet:getFillUnitCapacity(spec.fillUnitIndex)
288 capacity = capacity + palletCapacity
289 maxCapacity = math.max(maxCapacity, palletCapacity)
290 fillLevel = fillLevel + pallet:getFillUnitFillLevel(spec.fillUnitIndex)
291 end
292
293 if #pallets < spec.maxNumPallets then
294 capacity = maxCapacity * spec.maxNumPallets
295 end
296
297 spec.fillLevel = fillLevel
298 spec.capacity = capacity
299
300 if math.abs(fillLevel - spec.fillLevelSent) > 1 or math.abs(capacity - spec.capacitySent) > 1 then
301 spec.fillLevelSent = fillLevel
302 spec.capacitySent = capacity
303 self:raiseDirtyFlags(spec.dirtyFlag)
304 end
305end

getConditionInfos

Description
Definition
getConditionInfos()
Code
396function PlaceableHusbandryPallets:getConditionInfos(superFunc)
397 local infos = superFunc(self)
398
399 local spec = self.spec_husbandryPallets
400 local info = {}
401 local fillType = g_fillTypeManager:getFillTypeByIndex(spec.fillTypeIndex)
402 info.title = fillType.title
403 info.value = spec.fillLevel
404
405 local ratio = 0
406 if spec.capacity > 0 then
407 ratio = spec.fillLevel / spec.capacity
408 end
409
410 info.ratio = MathUtil.clamp(ratio, 0, 1)
411 info.invertedBar = true
412 info.customUnitText = spec.fillTypeUnit
413
414 table.insert(infos, info)
415
416 return infos
417end

getPalletCallback

Description
Definition
getPalletCallback()
Code
321function PlaceableHusbandryPallets:getPalletCallback(pallet, result, fillTypeIndex)
322 local spec = self.spec_husbandryPallets
323 spec.spawnPending = false
324 spec.currentPallet = pallet
325
326 if pallet ~= nil then
327 if spec.palletLimitReached then
328 spec.palletLimitReached = false
329 self:raiseDirtyFlags(spec.dirtyFlag)
330 end
331
332 if result == PalletSpawner.RESULT_SUCCESS then
333 pallet:emptyAllFillUnits(true)
334 end
335
336 spec.pallets[pallet] = true
337 local delta = pallet:addFillUnitFillLevel(self:getOwnerFarmId(), spec.fillUnitIndex, spec.pendingLiters, fillTypeIndex, ToolType.UNDEFINED)
338
339 spec.pendingLiters = math.max(spec.pendingLiters - delta, 0)
340
341 if spec.pendingLiters > 5 then
342 self:updatePallets()
343 end
344 elseif result == PalletSpawner.RESULT_NO_SPACE then
345 self:showSpawnerBlockedWarning()
346 elseif result == PalletSpawner.PALLET_LIMITED_REACHED then
347 if not spec.palletLimitReached then
348 spec.palletLimitReached = true
349 self:raiseDirtyFlags(spec.dirtyFlag)
350 end
351 end
352
353 self:updatePalletInfo()
354end

loadFromXMLFile

Description
Definition
loadFromXMLFile()
Code
199function PlaceableHusbandryPallets:loadFromXMLFile(xmlFile, key)
200 local spec = self.spec_husbandryPallets
201 self.pendingLiters = xmlFile:getValue(key.."#pendingLiters", spec.pendingLiters)
202
203 self:updatePalletInfo()
204end

onDelete

Description
Definition
onDelete()
Code
129function PlaceableHusbandryPallets:onDelete()
130 local spec = self.spec_husbandryPallets
131 if self.isServer and spec.palletTriggers ~= nil then
132 for _, trigger in ipairs(spec.palletTriggers) do
133 if trigger.added then
134 removeTrigger(trigger.node)
135 end
136 end
137 end
138end

onFinalizePlacement

Description
Definition
onFinalizePlacement()
Code
142function PlaceableHusbandryPallets:onFinalizePlacement()
143 local spec = self.spec_husbandryPallets
144
145 if self.isServer then
146 for _, trigger in ipairs(spec.palletTriggers) do
147 addTrigger(trigger.node, "onPalletTriggerCallback", self)
148 trigger.added = true
149 end
150 end
151end

onHusbandryAnimalsCreated

Description
Definition
onHusbandryAnimalsCreated()
Code
215function PlaceableHusbandryPallets:onHusbandryAnimalsCreated(husbandry)
216 if husbandry ~= nil then
217 local animalTypeIndex = self:getAnimalTypeIndex()
218 local animalType = g_currentMission.animalSystem:getTypeByIndex(animalTypeIndex)
219 if animalType ~= nil then
220 local spec = self.spec_husbandryPallets
221 local l10nText = "ui_statisticView_" .. tostring(animalType.name):lower()
222 spec.animalTypeName = g_i18n:getText(l10nText, self.customEnvironment)
223 end
224 end
225end

onHusbandryAnimalsUpdate

Description
Definition
onHusbandryAnimalsUpdate()
Code
229function PlaceableHusbandryPallets:onHusbandryAnimalsUpdate(clusters)
230 local spec = self.spec_husbandryPallets
231
232 spec.litersPerHour = 0
233 for _, cluster in ipairs(clusters) do
234 local subType = g_currentMission.animalSystem:getSubTypeByIndex(cluster.subTypeIndex)
235 if subType ~= nil then
236 local pallets = subType.output.pallets
237 if pallets ~= nil then
238 local age = cluster:getAge()
239 local litersPerAnimal = pallets:get(age)
240 local litersPerDay = litersPerAnimal * cluster:getNumAnimals()
241
242 spec.litersPerHour = spec.litersPerHour + (litersPerDay / 24)
243 end
244 end
245 end
246end

onLoad

Description
Definition
onLoad()
Code
80function PlaceableHusbandryPallets:onLoad(savegame)
81 local spec = self.spec_husbandryPallets
82
83 spec.palletSpawner = PalletSpawner.new(self.baseDirectory)
84 spec.palletSpawner:load(self.components, self.xmlFile, "placeable.husbandry.pallets", self.customEnvironment, self.i3dMappings)
85
86 local fillTypeName = self.xmlFile:getValue("placeable.husbandry.pallets#fillType")
87 local fillTypeIndex = g_fillTypeManager:getFillTypeIndexByName(fillTypeName)
88 if fillTypeIndex == nil then
89 Logging.xmlError(self.xmlFile, "Pallet filltype '%s' not defined", fillTypeName)
90 self:setLoadingState(Placeable.LOADING_STATE_ERROR)
91 return
92 end
93 spec.fillTypeIndex = fillTypeIndex
94 spec.fillUnitIndex = 1
95 spec.fillTypeUnit = g_i18n:convertText(self.xmlFile:getValue("placeable.husbandry.pallets#unitText") or "$l10n_unit_literShort", self.customEnvironment)
96
97 spec.maxNumPallets = self.xmlFile:getValue("placeable.husbandry.pallets#maxNumPallets", 1)
98
99 if self.isServer then
100 spec.palletTriggers = {}
101 self.xmlFile:iterate("placeable.husbandry.pallets.palletTrigger", function(_, key)
102 local node = self.xmlFile:getValue(key .. "#node", nil, self.components, self.i3dMappings)
103 if node ~= nil then
104 table.insert(spec.palletTriggers, {node=node, added=false})
105 end
106 end)
107 end
108
109 spec.isPalletInfoUpdateRunning = false
110 spec.animalTypeName = nil
111 spec.currentPallet = nil
112 spec.pendingLiters = 0
113 spec.fillLevel = 0
114 spec.fillLevelSent = 0
115 spec.capacity = 0
116 spec.capacitySent = 0
117 spec.litersPerHour = 0
118 spec.fillType = nil
119 spec.pallets = {}
120 spec.spawnPending = false
121 spec.palletLimitReached = false
122 spec.infoHudTooManyPallets = {title=g_i18n:getText("infohud_tooManyPallets"), accentuate=true}
123
124 spec.dirtyFlag = self:getNextDirtyFlag()
125end

onPalletTriggerCallback

Description
Definition
onPalletTriggerCallback()
Code
250function PlaceableHusbandryPallets:onPalletTriggerCallback(triggerId, otherId, onEnter, onLeave, onStay)
251 if onLeave then
252 local spec = self.spec_husbandryPallets
253 local object = g_currentMission:getNodeObject(otherId)
254 if object ~= nil then
255 if object == spec.currentPallet then
256 spec.currentPallet = nil
257 end
258
259 spec.pallets[object] = nil
260 self:updatePalletInfo()
261 end
262 end
263end

onReadStream

Description
Definition
onReadStream()
Code
155function PlaceableHusbandryPallets:onReadStream(streamId, connection)
156 local spec = self.spec_husbandryPallets
157 spec.fillLevel = streamReadFloat32(streamId)
158 spec.capacity = streamReadFloat32(streamId)
159 spec.palletLimitReached = streamReadBool(streamId)
160end

onReadUpdateStream

Description
Definition
onReadUpdateStream()
Code
173function PlaceableHusbandryPallets:onReadUpdateStream(streamId, timestamp, connection)
174 if connection:getIsServer() then
175 if streamReadBool(streamId) then
176 local spec = self.spec_husbandryPallets
177 spec.fillLevel = streamReadFloat32(streamId)
178 spec.capacity = streamReadFloat32(streamId)
179 spec.palletLimitReached = streamReadBool(streamId)
180 end
181 end
182end

onWriteStream

Description
Definition
onWriteStream()
Code
164function PlaceableHusbandryPallets:onWriteStream(streamId, connection)
165 local spec = self.spec_husbandryPallets
166 streamWriteFloat32(streamId, spec.fillLevelSent)
167 streamWriteFloat32(streamId, spec.capacitySent)
168 streamWriteBool(streamId, spec.palletLimitReached)
169end

onWriteUpdateStream

Description
Definition
onWriteUpdateStream()
Code
186function PlaceableHusbandryPallets:onWriteUpdateStream(streamId, connection, dirtyMask)
187 if not connection:getIsServer() then
188 local spec = self.spec_husbandryPallets
189 if streamWriteBool(streamId, bitAND(dirtyMask, spec.dirtyFlag) ~= 0) then
190 streamWriteFloat32(streamId, spec.fillLevelSent)
191 streamWriteFloat32(streamId, spec.capacitySent)
192 streamWriteBool(streamId, spec.palletLimitReached)
193 end
194 end
195end

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
18function PlaceableHusbandryPallets.prerequisitesPresent(specializations)
19 return true
20end

registerEventListeners

Description
Definition
registerEventListeners()
Code
44function PlaceableHusbandryPallets.registerEventListeners(placeableType)
45 SpecializationUtil.registerEventListener(placeableType, "onLoad", PlaceableHusbandryPallets)
46 SpecializationUtil.registerEventListener(placeableType, "onDelete", PlaceableHusbandryPallets)
47 SpecializationUtil.registerEventListener(placeableType, "onFinalizePlacement", PlaceableHusbandryPallets)
48 SpecializationUtil.registerEventListener(placeableType, "onReadStream", PlaceableHusbandryPallets)
49 SpecializationUtil.registerEventListener(placeableType, "onWriteStream", PlaceableHusbandryPallets)
50 SpecializationUtil.registerEventListener(placeableType, "onReadUpdateStream", PlaceableHusbandryPallets)
51 SpecializationUtil.registerEventListener(placeableType, "onWriteUpdateStream", PlaceableHusbandryPallets)
52 SpecializationUtil.registerEventListener(placeableType, "onHusbandryAnimalsUpdate", PlaceableHusbandryPallets)
53 SpecializationUtil.registerEventListener(placeableType, "onHusbandryAnimalsCreated", PlaceableHusbandryPallets)
54end

registerFunctions

Description
Definition
registerFunctions()
Code
24function PlaceableHusbandryPallets.registerFunctions(placeableType)
25 SpecializationUtil.registerFunction(placeableType, "onPalletTriggerCallback", PlaceableHusbandryPallets.onPalletTriggerCallback)
26 SpecializationUtil.registerFunction(placeableType, "getAllPalletsCallback", PlaceableHusbandryPallets.getAllPalletsCallback)
27 SpecializationUtil.registerFunction(placeableType, "updatePallets", PlaceableHusbandryPallets.updatePallets)
28 SpecializationUtil.registerFunction(placeableType, "getPalletCallback", PlaceableHusbandryPallets.getPalletCallback)
29 SpecializationUtil.registerFunction(placeableType, "showSpawnerBlockedWarning", PlaceableHusbandryPallets.showSpawnerBlockedWarning)
30 SpecializationUtil.registerFunction(placeableType, "showPalletBlockedWarning", PlaceableHusbandryPallets.showPalletBlockedWarning)
31 SpecializationUtil.registerFunction(placeableType, "updatePalletInfo", PlaceableHusbandryPallets.updatePalletInfo)
32end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
36function PlaceableHusbandryPallets.registerOverwrittenFunctions(placeableType)
37 SpecializationUtil.registerOverwrittenFunction(placeableType, "getConditionInfos", PlaceableHusbandryPallets.getConditionInfos)
38 SpecializationUtil.registerOverwrittenFunction(placeableType, "updateOutput", PlaceableHusbandryPallets.updateOutput)
39 SpecializationUtil.registerOverwrittenFunction(placeableType, "updateInfo", PlaceableHusbandryPallets.updateInfo)
40end

registerSavegameXMLPaths

Description
Definition
registerSavegameXMLPaths()
Code
71function PlaceableHusbandryPallets.registerSavegameXMLPaths(schema, basePath)
72 schema:setXMLSpecializationType("Husbandry")
73 schema:register(XMLValueType.FLOAT, basePath .. "#pendingLiters", "Pending liters")
74 schema:register(XMLValueType.FLOAT, basePath .. "#fillLevel", "Fill Level")
75 schema:setXMLSpecializationType()
76end

registerXMLPaths

Description
Definition
registerXMLPaths()
Code
58function PlaceableHusbandryPallets.registerXMLPaths(schema, basePath)
59 schema:setXMLSpecializationType("Husbandry")
60 basePath = basePath .. ".husbandry.pallets"
61 schema:register(XMLValueType.NODE_INDEX, basePath .. ".palletTrigger(?)#node", "A pallet trigger")
62 schema:register(XMLValueType.STRING, basePath .. "#fillType", "Pallet fill type")
63 schema:register(XMLValueType.STRING, basePath .. "#unitText", "Pallet fill type unit")
64 schema:register(XMLValueType.INT, basePath .. "#maxNumPallets", "Maximum number of pallets")
65 PalletSpawner.registerXMLPaths(schema, basePath)
66 schema:setXMLSpecializationType()
67end

saveToXMLFile

Description
Definition
saveToXMLFile()
Code
208function PlaceableHusbandryPallets:saveToXMLFile(xmlFile, key, usedModNames)
209 local spec = self.spec_husbandryPallets
210 xmlFile:setValue(key.."#pendingLiters", spec.pendingLiters)
211end

showPalletBlockedWarning

Description
Definition
showPalletBlockedWarning()
Code
373function PlaceableHusbandryPallets:showPalletBlockedWarning()
374 local spec = self.spec_husbandryPallets
375 if self.isClient and g_currentMission:getFarmId() == self:getOwnerFarmId() then
376 local fillType = g_fillTypeManager:getFillTypeByIndex(spec.fillTypeIndex)
377 if fillType ~= nil and spec.animalTypeName ~= nil then
378 local text = string.format(g_i18n:getText("ingameNotification_palletSpawnerBlocked"), fillType.title, spec.animalTypeName)
379 g_currentMission:addIngameNotification(FSBaseMission.INGAME_NOTIFICATION_CRITICAL, text)
380 end
381 end
382end

showSpawnerBlockedWarning

Description
Definition
showSpawnerBlockedWarning()
Code
358function PlaceableHusbandryPallets:showSpawnerBlockedWarning()
359 local spec = self.spec_husbandryPallets
360 if not spec.showedWarning then
361 if self.isServer then
362 g_currentMission:broadcastEventToFarm(AnimalHusbandryNoMorePalletSpaceEvent.new(self), self:getOwnerFarmId(), false)
363 end
364
365 self:showPalletBlockedWarning()
366
367 spec.showedWarning = true
368 end
369end

updateInfo

Description
Definition
updateInfo()
Code
386function PlaceableHusbandryPallets:updateInfo(superFunc, infoTable)
387 superFunc(self, infoTable)
388 local spec = self.spec_husbandryPallets
389 if spec.palletLimitReached then
390 table.insert(infoTable, spec.infoHudTooManyPallets)
391 end
392end

updateOutput

Description
Definition
updateOutput()
Code
421function PlaceableHusbandryPallets:updateOutput(superFunc, foodFactor, productionFactor, globalProductionFactor)
422 superFunc(self, foodFactor, productionFactor, globalProductionFactor)
423
424 local spec = self.spec_husbandryPallets
425 spec.showedWarning = false
426
427 if self.isServer and spec.litersPerHour > 0 then
428 spec.pendingLiters = spec.pendingLiters + productionFactor * globalProductionFactor * spec.litersPerHour * g_currentMission.environment.timeAdjustment
429 self:updatePallets()
430 end
431end

updatePalletInfo

Description
Definition
updatePalletInfo()
Code
267function PlaceableHusbandryPallets:updatePalletInfo()
268 if self.isServer then
269 local spec = self.spec_husbandryPallets
270 if not spec.isPalletInfoUpdateRunning then
271 spec.isPalletInfoUpdateRunning = true
272 spec.palletSpawner:getAllPallets(spec.fillTypeIndex, self.getAllPalletsCallback, self)
273 end
274 end
275end

updatePallets

Description
Definition
updatePallets()
Code
309function PlaceableHusbandryPallets:updatePallets()
310 if self.isServer then
311 local spec = self.spec_husbandryPallets
312 if not spec.spawnPending and spec.pendingLiters > 5 then
313 spec.spawnPending = true
314 spec.palletSpawner:getOrSpawnPallet(self:getOwnerFarmId(), spec.fillTypeIndex, self.getPalletCallback, self)
315 end
316 end
317end