LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

PlaceableHusbandryWater

Description
Specialization for placeables
Functions

getConditionInfos

Description
Definition
getConditionInfos()
Code
206function PlaceableHusbandryWater:getConditionInfos(superFunc)
207 local infos = superFunc(self)
208 local spec = self.spec_husbandryWater
209
210 if not spec.automaticWaterSupply then
211 local info = {}
212 local fillType = g_fillTypeManager:getFillTypeByIndex(spec.fillType)
213 info.title = fillType.title
214 info.value = self:getHusbandryFillLevel(spec.fillType)
215 local capacity = self:getHusbandryCapacity(spec.fillType)
216 local ratio = 0
217 if capacity > 0 then
218 ratio = info.value / capacity
219 end
220 info.ratio = MathUtil.clamp(ratio, 0, 1)
221 info.invertedBar = false
222
223 table.insert(infos, info)
224 end
225
226 return infos
227end

onDelete

Description
Definition
onDelete()
Code
94function PlaceableHusbandryWater:onDelete()
95 local spec = self.spec_husbandryWater
96 if spec.waterPlane ~= nil then
97 spec.waterPlane:delete()
98 spec.waterPlane = nil
99 end
100end

onFinalizePlacement

Description
Definition
onFinalizePlacement()
Code
104function PlaceableHusbandryWater:onFinalizePlacement()
105 local spec = self.spec_husbandryWater
106
107 if not spec.automaticWaterSupply then
108 if not self:getHusbandryIsFillTypeSupported(spec.fillType) then
109 Logging.warning("Missing filltype 'water' in husbandry storage! Changing to automatic water supply")
110 spec.automaticWaterSupply = true
111 end
112 end
113end

onHusbandryAnimalsCreated

Description
Definition
onHusbandryAnimalsCreated()
Code
164function PlaceableHusbandryWater:onHusbandryAnimalsCreated(husbandry)
165 if husbandry ~= nil then
166 local spec = self.spec_husbandryWater
167 spec.husbandry = husbandry
168 for _, waterPlace in ipairs(spec.waterPlaces) do
169 waterPlace.place = addFeedingPlace(husbandry, waterPlace.node, 0.0, AnimalHusbandryFeedingType.WATER)
170 end
171 end
172end

onHusbandryAnimalsUpdate

Description
Definition
onHusbandryAnimalsUpdate()
Code
176function PlaceableHusbandryWater:onHusbandryAnimalsUpdate(clusters)
177 local spec = self.spec_husbandryWater
178
179 spec.litersPerHour = 0
180 for _, cluster in ipairs(clusters) do
181 local subType = g_currentMission.animalSystem:getSubTypeByIndex(cluster.subTypeIndex)
182 if subType ~= nil then
183 local water = subType.input.water
184 if water ~= nil then
185 local age = cluster:getAge()
186 local litersPerAnimal = water:get(age)
187 local litersPerDay = litersPerAnimal * cluster:getNumAnimals()
188
189 spec.litersPerHour = spec.litersPerHour + (litersPerDay / 24)
190 end
191 end
192 end
193end

onHusbandryFillLevelChanged

Description
Definition
onHusbandryFillLevelChanged()
Code
197function PlaceableHusbandryWater:onHusbandryFillLevelChanged(fillTypeIndex, delta)
198 local spec = self.spec_husbandryWater
199 if fillTypeIndex == spec.fillType then
200 self:updateWaterPlane()
201 end
202end

onLoad

Description
Definition
onLoad()
Code
61function PlaceableHusbandryWater:onLoad(savegame)
62 local spec = self.spec_husbandryWater
63
64 spec.husbandry = nil
65 spec.litersPerHour = 0
66 spec.automaticWaterSupply = false
67 spec.fillType = FillType.WATER
68 spec.waterPlaces = {}
69 spec.info = {title=g_i18n:getText("fillType_water"), text=""}
70 spec.automaticWaterSupply = self.xmlFile:getValue("placeable.husbandry.water#automaticWaterSupply", spec.automaticWaterSupply)
71
72 spec.waterPlane = FillPlane.new()
73 if spec.waterPlane:load(self.components, self.xmlFile, "placeable.husbandry.water.waterPlane", self.i3dMappings) then
74 spec.waterPlane:setState(spec.automaticWaterSupply and 1 or 0)
75 else
76 spec.waterPlane:delete()
77 spec.waterPlane = nil
78 end
79
80 self.xmlFile:iterate("placeable.husbandry.water.waterPlaces.waterPlace", function(_, key)
81 local node = self.xmlFile:getValue(key .. "#node", nil, self.components, self.i3dMappings)
82 table.insert(spec.waterPlaces, {node=node, place=nil})
83 end)
84end

onPostFinalizePlacement

Description
Definition
onPostFinalizePlacement()
Code
88function PlaceableHusbandryWater:onPostFinalizePlacement()
89 self:updateWaterPlane()
90end

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

registerEventListeners

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

registerFunctions

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

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
30function PlaceableHusbandryWater.registerOverwrittenFunctions(placeableType)
31 SpecializationUtil.registerOverwrittenFunction(placeableType, "updateFeeding", PlaceableHusbandryWater.updateFeeding)
32 SpecializationUtil.registerOverwrittenFunction(placeableType, "getConditionInfos", PlaceableHusbandryWater.getConditionInfos)
33 SpecializationUtil.registerOverwrittenFunction(placeableType, "updateInfo", PlaceableHusbandryWater.updateInfo)
34end

registerXMLPaths

Description
Definition
registerXMLPaths()
Code
50function PlaceableHusbandryWater.registerXMLPaths(schema, basePath)
51 schema:setXMLSpecializationType("Husbandry")
52 basePath = basePath .. ".husbandry.water"
53 schema:register(XMLValueType.BOOL, basePath .. "#automaticWaterSupply", "If husbandry has a automatic water supply", false)
54 schema:register(XMLValueType.NODE_INDEX, basePath .. ".waterPlaces.waterPlace(?)#node", "Water place")
55 FillPlane.registerXMLPaths(schema, basePath .. ".waterPlane")
56 schema:setXMLSpecializationType()
57end

updateFeeding

Description
Definition
updateFeeding()
Code
141function PlaceableHusbandryWater:updateFeeding(superFunc)
142 local factor = superFunc(self)
143 local spec = self.spec_husbandryWater
144
145 if self.isServer and spec.litersPerHour > 0 then
146 local delta = spec.litersPerHour * g_currentMission.environment.timeAdjustment
147
148 if spec.automaticWaterSupply then
149 local fillType = g_fillTypeManager:getFillTypeByIndex(spec.fillType)
150 local price = delta * fillType.pricePerLiter
151 g_currentMission:addMoney(-price, self:getOwnerFarmId(), MoneyType.PURCHASE_WATER, false, false)
152 else
153 local remainingLiters = self:removeHusbandryFillLevel(nil, delta, spec.fillType)
154 local usedDelta = (delta - remainingLiters)
155 factor = factor * MathUtil.clamp(usedDelta / delta, 0, 1)
156 end
157 end
158
159 return factor
160end

updateInfo

Description
Definition
updateInfo()
Code
231function PlaceableHusbandryWater:updateInfo(superFunc, infoTable)
232 superFunc(self, infoTable)
233 local spec = self.spec_husbandryWater
234 if not spec.automaticWaterSupply then
235 local fillLevel = self:getHusbandryFillLevel(spec.fillType)
236 spec.info.text = string.format("%d l", fillLevel)
237 table.insert(infoTable, spec.info)
238 end
239end

updateWaterPlane

Description
Definition
updateWaterPlane()
Code
117function PlaceableHusbandryWater:updateWaterPlane()
118 local spec = self.spec_husbandryWater
119 local fillLevel = self:getHusbandryFillLevel(spec.fillType, nil)
120
121 if spec.waterPlane ~= nil then
122 local capacity = self:getHusbandryCapacity(spec.fillType, nil)
123 local factor = 0
124 if capacity > 0 then
125 factor = fillLevel / capacity
126 end
127 spec.waterPlane:setState(factor)
128 end
129
130 if spec.husbandry ~= nil then
131 for _, waterPlace in pairs(spec.waterPlaces) do
132 if waterPlace.place ~= nil then
133 updateFeedingPlace(spec.husbandry, waterPlace.place, fillLevel)
134 end
135 end
136 end
137end