LUADOC - Farming Simulator 19

HusbandryModuleFood

Description
This class handles food in husbandry
Parent
HusbandryModuleBase
XML Configuration Parameters
{basepath}#foodProcessType

Functions

addFillLevelFromTool

Description
Definition
addFillLevelFromTool()
Code
294function HusbandryModuleFood:addFillLevelFromTool(farmId, deltaFillLevel, fillTypeIndex)
295 local freeCapacity = self:getFreeCapacity(fillTypeIndex)
296 local changed = self:changeFillLevels(math.min(freeCapacity, deltaFillLevel), fillTypeIndex)
297
298 if deltaFillLevel > 0 and changed ~= 0 then
299 self.owner:updateGlobalProductionFactor()
300 end
301
302 if changed > 0 and self.feedingTrough ~= nil then
303 if self.feedingTrough:getIsFillTypeSupported(fillTypeIndex) then
304 self.feedingTrough:raiseActive()
305 -- self:updateFillPlane()
306 end
307 end
308 return changed
309end

changeFillLevels

Description
Change amount of fillType
Definition
changeFillLevels(float amount, integer index)
Arguments
floatamountto change the fill type
integerindexof the fillType
Return Values
floatamounteffectively changed
Code
316function HusbandryModuleFood:changeFillLevels(fillDelta, fillTypeIndex)
317 local delta = 0.0
318 if self.fillLevels[fillTypeIndex] ~= nil then
319 local oldFillLevel = self.fillLevels[fillTypeIndex]
320 local newFillLevel = oldFillLevel + fillDelta
321 newFillLevel = math.max(newFillLevel, 0.0)
322 delta = newFillLevel - oldFillLevel
323 self:setFillLevel(fillTypeIndex, newFillLevel)
324 end
325 return delta
326end

delete

Description
Deletes instance
Definition
delete()
Code
31function HusbandryModuleFood:delete()
32 if self.feedingTrough ~= nil then
33 self.feedingTrough:delete()
34 self.feedingTrough = nil
35 end
36 if self.fillPlane ~= nil then
37 self.fillPlane:delete()
38 self.fillPlane = nil
39 end
40
41 self.foodGroupCapacities = {}
42end

getCapacity

Description
Gets a fill capacity
Definition
getCapacity(integer index)
Arguments
integerindexof a fill type
Return Values
floatreturnsthe capacity of a fillType
Code
173function HusbandryModuleFood:getCapacity()
174 local animalType = self.owner:getAnimalType()
175 local consumptionType = g_animalFoodManager:getFoodConsumptionTypeByAnimalType(animalType)
176 local foodCapacity = 0.0
177
178 if consumptionType == AnimalFoodManager.FOOD_CONSUME_TYPE_SERIAL then
179 for _, foodGroupInfo in pairs(self.foodGroupCapacities) do
180 foodCapacity = foodCapacity + foodGroupInfo.capacity
181 end
182 elseif consumptionType == AnimalFoodManager.FOOD_CONSUME_TYPE_PARALLEL then
183 for _, foodGroupInfo in pairs(self.foodGroupCapacities) do
184 foodCapacity = foodGroupInfo.capacity
185 break
186 end
187 else
188 foodCapacity = self.fillCapacity
189 end
190 return foodCapacity
191end

getConsumedFood

Description
Definition
getConsumedFood()
Code
330function HusbandryModuleFood:getConsumedFood()
331 return self.consumedFood
332end

getFillLevel

Description
Definition
getFillLevel()
Code
238function HusbandryModuleFood:getFillLevel(fillTypeIndex)
239 local animalType = self.owner:getAnimalType()
240 local fillLevel = g_animalFoodManager:getTotalFillLevelInGroupByFillTypeIndex(animalType, self.fillLevels, fillTypeIndex)
241 return Utils.getNoNil(fillLevel, 0.0)
242end

getFilltypeInfos

Description
Definition
getFilltypeInfos()
Code
375function HusbandryModuleFood:getFilltypeInfos()
376 local result = {}
377
378 for _, foodGroupInfo in pairs(self.foodGroupCapacities) do
379 local foodGroup = foodGroupInfo.foodGroup
380 local totalFillLevel = g_animalFoodManager:getTotalFillLevelInGroup(foodGroupInfo.foodGroup, self.fillLevels)
381 local capacity = foodGroupInfo.capacity
382 table.insert(result, {foodGroup=foodGroup, fillLevel=totalFillLevel, capacity=capacity})
383 end
384 return result
385end

getFoodFactor

Description
Definition
getFoodFactor()
Code
336function HusbandryModuleFood:getFoodFactor()
337 return self.foodFactor
338end

getFreeCapacity

Description
Sets a capacity
Definition
getFreeCapacity()
Code
195function HusbandryModuleFood:getFreeCapacity(fillTypeIndex)
196 local animalType = self.owner:getAnimalType()
197 local foodMixture = g_animalFoodManager:getFoodMixtureByFillType(fillTypeIndex)
198
199 if foodMixture ~= nil then
200 local checkedFoodGroups = {}
201 local capacity = 0
202 local fillLevel = 0
203 for _, ingredient in ipairs(foodMixture.ingredients) do
204 for _, fillType in ipairs(ingredient.fillTypes) do
205 local foodGroup = g_animalFoodManager:getFoodGroupByFillType(animalType, fillType)
206 if checkedFoodGroups[foodGroup] == nil then
207 for _, foodGroupInfo in pairs(self.foodGroupCapacities) do
208 if foodGroupInfo.foodGroup == foodGroup then
209 capacity = capacity + foodGroupInfo.capacity
210 break
211 end
212 end
213 fillLevel = fillLevel + self:getFillLevel(fillType)
214
215 checkedFoodGroups[foodGroup] = true
216 end
217 end
218 end
219
220 return capacity - fillLevel
221
222 else
223 local foodGroup = g_animalFoodManager:getFoodGroupByFillType(animalType, fillTypeIndex)
224 local capacity = 0.0
225 for _, foodGroupInfo in pairs(self.foodGroupCapacities) do
226 if foodGroupInfo.foodGroup == foodGroup then
227 capacity = foodGroupInfo.capacity
228 break
229 end
230 end
231
232 return capacity - self:getFillLevel(fillTypeIndex)
233 end
234end

getTotalFillLevel

Description
Definition
getTotalFillLevel()
Code
246function HusbandryModuleFood:getTotalFillLevel()
247 local animalType = self.owner:getAnimalType()
248 local consumptionType = g_animalFoodManager:getFoodConsumptionTypeByAnimalType(animalType)
249 local totalFillLevel = 0.0
250
251 if consumptionType == AnimalFoodManager.FOOD_CONSUME_TYPE_SERIAL then
252 for _, foodGroupInfo in pairs(self.foodGroupCapacities) do
253 totalFillLevel = totalFillLevel + g_animalFoodManager:getTotalFillLevelInGroup(foodGroupInfo.foodGroup, self.fillLevels)
254 end
255 elseif consumptionType == AnimalFoodManager.FOOD_CONSUME_TYPE_PARALLEL and #self.foodGroupCapacities > 0 then
256 local nbGroups = #self.foodGroupCapacities
257 for _, foodGroupInfo in pairs(self.foodGroupCapacities) do
258 totalFillLevel = totalFillLevel + g_animalFoodManager:getTotalFillLevelInGroup(foodGroupInfo.foodGroup, self.fillLevels)
259 end
260 totalFillLevel = totalFillLevel / nbGroups
261 else
262 totalFillLevel = HusbandryModuleFood:superClass().getTotalFillLevel(self)
263 end
264 return totalFillLevel
265end

initDataStructures

Description
Initialize data structures
Definition
initDataStructures()
Code
46function HusbandryModuleFood:initDataStructures()
47 HusbandryModuleFood:superClass().initDataStructures(self)
48 self.feedingTrough = nil
49 self.foodFactor = 0.0
50 self.consumedFood = {}
51end

load

Description
Loads data from xml
Definition
load(table xmlFile, string xmlKey, table rootNode)
Arguments
tablexmlFilehandle
stringxmlKeyfrom which to read the configuration
tablerootNodeof the husbandry
Return Values
booleantrueif loading was successful else false
Code
59function HusbandryModuleFood:load(xmlFile, configKey, rootNode, owner, baseDirectory)
60 if not HusbandryModuleFood:superClass().load(self, xmlFile, configKey, rootNode, owner, baseDirectory) then
61 return false
62 end
63
64 if not hasXMLProperty(xmlFile, configKey) then
65 return false
66 end
67
68 local foodNodeId = I3DUtil.indexToObject(rootNode, getXMLString(xmlFile, configKey .. "#node"))
69 if foodNodeId ~= nil then
70 local feedingTrough = UnloadFeedingTrough:new(self.owner.isServer, self.owner.isClient)
71
72 if feedingTrough:load(foodNodeId, xmlFile, configKey, self) then
73 feedingTrough:register(true)
74 self.feedingTrough = feedingTrough
75 self:setupFoodGroups()
76
77 self.fillPlane = FillPlane:new()
78 self.fillPlane:load(rootNode, xmlFile, configKey..".fillPlane")
79
80 return true
81 else
82 feedingTrough:delete()
83 return false
84 end
85 end
86
87 return false
88end

loadFromXMLFile

Description
Loads information from attributes and node. Retrives from xml file information.
Definition
loadFromXMLFile(table xmlFile, string key)
Arguments
tablexmlFileXML file handler
stringkeyXML base key
Code
94function HusbandryModuleFood:loadFromXMLFile(xmlFile, key)
95 HusbandryModuleFood:superClass().loadFromXMLFile(self, xmlFile, key)
96
97 self:updateFillPlane()
98end

new

Description
Creating manager
Definition
new()
Return Values
tableinstanceinstance of object
Code
22function HusbandryModuleFood:new(customMt)
23 local self = HusbandryModuleBase:new(customMt or HusbandryModuleFood_mt)
24
25 self.foodGroupCapacities = {}
26 return self
27end

onIntervalUpdate

Description
Update food usage
Definition
onIntervalUpdate(float dayToInterval)
Arguments
floatdayToInterval
Code
270function HusbandryModuleFood:onIntervalUpdate(dayToInterval)
271 HusbandryModuleFood:superClass().onIntervalUpdate(self, dayToInterval)
272
273 self.consumedFood = {}
274 -- print(string.format("-- [HusbandryModuleFood:onIntervalUpdate] CALLED"))
275 if self.singleAnimalUsagePerDay > 0.0 then
276 local totalNumAnimals = self.owner:getNumOfAnimals()
277 local foodNeeded = totalNumAnimals * self.singleAnimalUsagePerDay * dayToInterval
278 -- print(string.format("-- [HusbandryModuleFood:onIntervalUpdate] foodNeeded(%.3f) totalNumAnimals(%d) singleAnimalUsagePerDay(%.3f) dayToInterval(%.3f) ",
279 -- foodNeeded,
280 -- totalNumAnimals,
281 -- self.singleAnimalUsagePerDay,
282 -- dayToInterval))
283 if foodNeeded > 0.0 then
284 local animalType = self.owner:getAnimalType()
285 self.foodFactor = g_animalFoodManager:consumeFood(animalType, foodNeeded, self.fillLevels, self.consumedFood)
286
287 self:updateFillPlane()
288 end
289 end
290end

readStream

Description
Called on client side on join
Definition
readStream(integer streamId, table connection)
Arguments
integerstreamIdstream ID
tableconnectionconnection
Code
104function HusbandryModuleFood:readStream(streamId, connection)
105 HusbandryModuleFood:superClass().readStream(self, streamId, connection)
106
107 if self.loadPlace ~= nil then
108 local loadPlaceId = NetworkUtil.readNodeObjectId(streamId)
109 self.loadPlace:readStream(streamId, connection)
110 g_client:finishRegisterObject(self.loadPlace, loadPlaceId)
111 end
112end

setCapacity

Description
Sets a capacity
Definition
setCapacity(float newCapacity)
Arguments
floatnewCapacity
Code
160function HusbandryModuleFood:setCapacity(newCapacity)
161 self.fillCapacity = 0.0
162 for _, foodGroupInfo in pairs(self.foodGroupCapacities) do
163 foodGroupInfo.capacity = newCapacity
164 end
165
166 self:updateFillPlane()
167end

setupFoodGroups

Description
Definition
setupFoodGroups()
Code
130function HusbandryModuleFood:setupFoodGroups()
131 if self.feedingTrough ~= nil then
132 local animalType = self.owner:getAnimalType()
133 local foodGroups = g_animalFoodManager:getFoodGroupByAnimalType(animalType)
134 local foodMixtures = g_animalFoodManager:getFoodMixturesByAnimalType(animalType)
135
136 if foodGroups ~= nil then
137 for _, foodGroup in pairs(foodGroups) do
138 for _, fillTypeIndex in pairs(foodGroup.fillTypes) do
139 self.fillLevels[fillTypeIndex] = 0.0
140 self.providedFillTypes[fillTypeIndex] = true
141 end
142 table.insert(self.foodGroupCapacities, {foodGroup=foodGroup, capacity=0.0})
143 end
144 if foodMixtures ~= nil then
145 for _, foodMixtureFillType in ipairs(foodMixtures) do
146 self.providedFillTypes[foodMixtureFillType] = true
147 end
148 end
149 self:setCapacity(0.0)
150 self.feedingTrough:initFillTypesFromFoodGroups(foodGroups)
151 else
152 print("Error: food group for animal type '" .. animalType .. "' not found")
153 end
154 end
155end

updateFillPlaneColor

Description
Definition
updateFillPlaneColor()
Code
351function HusbandryModuleFood:updateFillPlaneColor()
352 if self.fillPlane ~= nil and self.fillPlane.colorChange then
353 local colorScale = {0.0, 0.0, 0.0}
354
355 for filltypeIndex, state in pairs(self.feedingTrough.fillTypes) do
356 if state then
357 local fillType = g_fillTypeManager:getFillTypeByIndex(filltypeIndex)
358 local fillLevelRatio = self.feedingTrough.target:getFillLevel(filltypeIndex) / self.feedingTrough.target:getCapacity()
359 local fillPlaneColors = {1.0, 1.0, 1.0}
360
361 if fillType.fillPlaneColors ~= nil then
362 fillPlaneColors = fillType.fillPlaneColors
363 end
364 for i = 1, 3 do
365 colorScale[i] = MathUtil.clamp(colorScale[i] + fillLevelRatio * fillPlaneColors[i], 0.0, 1.0)
366 end
367 end
368 end
369 self.fillPlane:setColorScale(colorScale)
370 end
371end

writeStream

Description
Called on server side on join
Definition
writeStream(integer streamId, table connection)
Arguments
integerstreamIdstream ID
tableconnectionconnection
Code
118function HusbandryModuleFood:writeStream(streamId, connection)
119 HusbandryModuleFood:superClass().writeStream(self, streamId, connection)
120
121 if self.loadPlace ~= nil then
122 NetworkUtil.writeNodeObjectId(streamId, NetworkUtil.getObjectId(self.loadPlace))
123 self.loadPlace:writeStream(streamId, connection)
124 g_server:registerObjectInStream(connection, self.loadPlace)
125 end
126end