LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

PlaceableHusbandryStraw

Description
Specialization for placeables
Functions

getConditionInfos

Description
Definition
getConditionInfos()
Code
217function PlaceableHusbandryStraw:getConditionInfos(superFunc)
218 local infos = superFunc(self)
219 local spec = self.spec_husbandryStraw
220
221 local info = {}
222 local fillType = g_fillTypeManager:getFillTypeByIndex(spec.inputFillType)
223 info.title = fillType.title
224 info.value = self:getHusbandryFillLevel(spec.inputFillType)
225 local capacity = self:getHusbandryCapacity(spec.inputFillType)
226 local ratio = 0
227 if capacity > 0 then
228 ratio = info.value / capacity
229 end
230 info.ratio = MathUtil.clamp(ratio, 0, 1)
231 info.invertedBar = false
232
233 table.insert(infos, info)
234
235 return infos
236end

onDelete

Description
Definition
onDelete()
Code
90function PlaceableHusbandryStraw:onDelete()
91 local spec = self.spec_husbandryStraw
92 if spec.strawPlane ~= nil then
93 spec.strawPlane:delete()
94 spec.strawPlane = nil
95 end
96end

onFinalizePlacement

Description
Definition
onFinalizePlacement()
Code
100function PlaceableHusbandryStraw:onFinalizePlacement()
101 local spec = self.spec_husbandryStraw
102 if not self:getHusbandryIsFillTypeSupported(spec.inputFillType) then
103 Logging.warning("Missing filltype 'straw' in husbandry storage!")
104 end
105 if self.isManureActive and not self:getHusbandryIsFillTypeSupported(spec.outputFillType) then
106 Logging.warning("Missing filltype 'manure' in husbandry storage!")
107 end
108end

onHusbandryAnimalsUpdate

Description
Definition
onHusbandryAnimalsUpdate()
Code
178function PlaceableHusbandryStraw:onHusbandryAnimalsUpdate(clusters)
179 local spec = self.spec_husbandryStraw
180 spec.inputLitersPerHour = 0
181 spec.outputLitersPerHour = 0
182 for _, cluster in ipairs(clusters) do
183 local subType = g_currentMission.animalSystem:getSubTypeByIndex(cluster.subTypeIndex)
184 if subType ~= nil then
185 local straw = subType.input.straw
186 if straw ~= nil then
187 local age = cluster:getAge()
188 local litersPerAnimal = straw:get(age)
189 local litersPerDay = litersPerAnimal * cluster:getNumAnimals()
190
191 spec.inputLitersPerHour = spec.inputLitersPerHour + (litersPerDay / 24)
192 end
193
194 local manure = subType.output.manure
195 if manure ~= nil then
196 local age = cluster:getAge()
197 local litersPerAnimal = manure:get(age)
198 local litersPerDay = litersPerAnimal * cluster:getNumAnimals()
199
200 spec.outputLitersPerHour = spec.outputLitersPerHour + (litersPerDay / 24)
201 end
202 end
203 end
204end

onHusbandryFillLevelChanged

Description
Definition
onHusbandryFillLevelChanged()
Code
208function PlaceableHusbandryStraw:onHusbandryFillLevelChanged(fillTypeIndex, delta)
209 local spec = self.spec_husbandryStraw
210 if fillTypeIndex == spec.inputFillType then
211 self:updateStrawPlane()
212 end
213end

onLoad

Description
Definition
onLoad()
Code
62function PlaceableHusbandryStraw:onLoad(savegame)
63 local spec = self.spec_husbandryStraw
64
65 spec.manureFactor = self.xmlFile:getValue("placeable.husbandry.straw.manure#factor", 1)
66 spec.isManureActive = self.xmlFile:getValue("placeable.husbandry.straw.manure#active", true)
67
68 spec.strawPlane = FillPlane.new()
69 if spec.strawPlane:load(self.components, self.xmlFile, "placeable.husbandry.straw.strawPlane", self.i3dMappings) then
70 spec.strawPlane:setState(0)
71 else
72 spec.strawPlane:delete()
73 spec.strawPlane = nil
74 end
75 spec.inputFillType = FillType.STRAW
76 spec.outputFillType = FillType.MANURE
77 spec.inputLitersPerHour = 0
78 spec.outputLitersPerHour = 0
79 spec.info = {title=g_i18n:getText("fillType_straw"), text=""}
80end

onPostFinalizePlacement

Description
Definition
onPostFinalizePlacement()
Code
84function PlaceableHusbandryStraw:onPostFinalizePlacement()
85 self:updateStrawPlane()
86end

onReadStream

Description
Definition
onReadStream()
Code
112function PlaceableHusbandryStraw:onReadStream(streamId, connection)
113 self:updateStrawPlane()
114end

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

registerEventListeners

Description
Definition
registerEventListeners()
Code
39function PlaceableHusbandryStraw.registerEventListeners(placeableType)
40 SpecializationUtil.registerEventListener(placeableType, "onLoad", PlaceableHusbandryStraw)
41 SpecializationUtil.registerEventListener(placeableType, "onPostFinalizePlacement", PlaceableHusbandryStraw)
42 SpecializationUtil.registerEventListener(placeableType, "onDelete", PlaceableHusbandryStraw)
43 SpecializationUtil.registerEventListener(placeableType, "onFinalizePlacement", PlaceableHusbandryStraw)
44 SpecializationUtil.registerEventListener(placeableType, "onReadStream", PlaceableHusbandryStraw)
45 SpecializationUtil.registerEventListener(placeableType, "onHusbandryAnimalsUpdate", PlaceableHusbandryStraw)
46 SpecializationUtil.registerEventListener(placeableType, "onHusbandryFillLevelChanged", PlaceableHusbandryStraw)
47end

registerFunctions

Description
Definition
registerFunctions()
Code
24function PlaceableHusbandryStraw.registerFunctions(placeableType)
25 SpecializationUtil.registerFunction(placeableType, "updateStrawPlane", PlaceableHusbandryStraw.updateStrawPlane)
26end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
30function PlaceableHusbandryStraw.registerOverwrittenFunctions(placeableType)
31 SpecializationUtil.registerOverwrittenFunction(placeableType, "updateOutput", PlaceableHusbandryStraw.updateOutput)
32 SpecializationUtil.registerOverwrittenFunction(placeableType, "updateProduction", PlaceableHusbandryStraw.updateProduction)
33 SpecializationUtil.registerOverwrittenFunction(placeableType, "getConditionInfos", PlaceableHusbandryStraw.getConditionInfos)
34 SpecializationUtil.registerOverwrittenFunction(placeableType, "updateInfo", PlaceableHusbandryStraw.updateInfo)
35end

registerXMLPaths

Description
Definition
registerXMLPaths()
Code
51function PlaceableHusbandryStraw.registerXMLPaths(schema, basePath)
52 schema:setXMLSpecializationType("Husbandry")
53 basePath = basePath .. ".husbandry.straw"
54 FillPlane.registerXMLPaths(schema, basePath .. ".strawPlane")
55 schema:register(XMLValueType.FLOAT, basePath .. ".manure#factor", "Factor to transform straw to manure", 1)
56 schema:register(XMLValueType.BOOL, basePath .. ".manure#active", "Enable manure production", true)
57 schema:setXMLSpecializationType()
58end

updateInfo

Description
Definition
updateInfo()
Code
240function PlaceableHusbandryStraw:updateInfo(superFunc, infoTable)
241 superFunc(self, infoTable)
242 local spec = self.spec_husbandryStraw
243
244 local fillLevel = self:getHusbandryFillLevel(spec.inputFillType)
245 spec.info.text = string.format("%d l", fillLevel)
246 table.insert(infoTable, spec.info)
247end

updateOutput

Description
Definition
updateOutput()
Code
134function PlaceableHusbandryStraw:updateOutput(superFunc, foodFactor, productionFactor, globalProductionFactor)
135 if self.isServer then
136 local spec = self.spec_husbandryStraw
137
138 if spec.inputLitersPerHour > 0 then
139 local amount = spec.inputLitersPerHour * g_currentMission.environment.timeAdjustment
140 local delta = amount - self:removeHusbandryFillLevel(self:getOwnerFarmId(), amount, spec.inputFillType)
141 if spec.outputLitersPerHour > 0 and delta > 0 then
142 local liters = foodFactor * spec.outputLitersPerHour * (delta/amount) * g_currentMission.environment.timeAdjustment -- outputLitersPerHour increases with number of animals
143 if liters > 0 then
144 self:addHusbandryFillLevelFromTool(self:getOwnerFarmId(), liters, spec.outputFillType, nil, nil, nil)
145 end
146 end
147
148 self:updateStrawPlane()
149 end
150 end
151
152 superFunc(self, foodFactor, productionFactor, globalProductionFactor)
153end

updateProduction

Description
Definition
updateProduction()
Code
157function PlaceableHusbandryStraw:updateProduction(superFunc, foodFactor)
158 local factor = superFunc(self, foodFactor)
159
160 if self.isServer then
161 local spec = self.spec_husbandryStraw
162 local fillLevel = self:getHusbandryFillLevel(spec.inputFillType)
163 if fillLevel > 0 then
164 local freeCapacity = self:getHusbandryFreeCapacity(spec.outputFillType)
165 if freeCapacity <= 0 then
166 factor = factor * 0.75
167 end
168 else
169 factor = factor * 0.9
170 end
171 end
172
173 return factor
174end

updateStrawPlane

Description
Definition
updateStrawPlane()
Code
118function PlaceableHusbandryStraw:updateStrawPlane()
119 local spec = self.spec_husbandryStraw
120 if spec.strawPlane ~= nil then
121 local capacity = self:getHusbandryCapacity(spec.inputFillType, nil)
122 local fillLevel = self:getHusbandryFillLevel(spec.inputFillType, nil)
123 local factor = 0
124 if capacity > 0 then
125 factor = fillLevel / capacity
126 end
127
128 spec.strawPlane:setState(factor)
129 end
130end