Script v1.7.1.0
- AI
- Animals
- AnimalFoodManager
- AnimalHusbandryNoMorePall...
- AnimalManager
- AnimalNameManager
- CrowsWildlife
- Dog
- Horse
- HusbandryModuleBase
- HusbandryModuleFood
- HusbandryModuleFoodSpilla...
- HusbandryModuleLiquidManu...
- HusbandryModuleManure
- HusbandryModuleMilk
- HusbandryModulePallets
- HusbandryModuleStraw
- LightWildlife
- RideableAnimal
- Contracts
- Debug
- Economy
- Effects
- Events
- Farms
- GUI
- Handtools
- I3d
- Materials
- Misc
- Objects
- Placeables
- Player
- Shop
- Sounds
- Specializations
- Triggers
- Utils
- Vehicles
- Weather
Engine v1.7.1.0
- AI
- Animation
- Camera
- Entity
- Fillplanes
- General
- I3D
- Input
- Lighting
- Math
- Network
- Node
- Overlays
- Particle System
- Physics
- Rendering
- Scenegraph
- Shape
- Sound
- Spline
- String
- Terrain Detail
- Text Rendering
- Tire Track
- XML
- general
Foundation Reference
HusbandryModuleFood
DescriptionThis class handles food in husbandryParent
HusbandryModuleBaseXML Configuration Parameters
{basepath}#foodProcessType |
Functions
- addFillLevelFromTool
- changeFillLevels
- delete
- getCapacity
- getConsumedFood
- getFillLevel
- getFilltypeInfos
- getFoodFactor
- getFreeCapacity
- getTotalFillLevel
- initDataStructures
- load
- loadFromXMLFile
- new
- onIntervalUpdate
- readStream
- setCapacity
- setupFoodGroups
- updateFillPlaneColor
- writeStream
addFillLevelFromTool
DescriptionDefinitionaddFillLevelFromTool()Code
294 | function 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 |
309 | end |
changeFillLevels
DescriptionChange amount of fillTypeDefinition
changeFillLevels(float amount, integer index)Arguments
float | amount | to change the fill type |
integer | index | of the fillType |
float | amount | effectively changed |
316 | function 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 |
326 | end |
delete
DescriptionDeletes instanceDefinition
delete()Code
31 | function 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 = {} |
42 | end |
getCapacity
DescriptionGets a fill capacityDefinition
getCapacity(integer index)Arguments
integer | index | of a fill type |
float | returns | the capacity of a fillType |
173 | function 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 |
191 | end |
getConsumedFood
DescriptionDefinitiongetConsumedFood()Code
330 | function HusbandryModuleFood:getConsumedFood() |
331 | return self.consumedFood |
332 | end |
getFillLevel
DescriptionDefinitiongetFillLevel()Code
238 | function 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) |
242 | end |
getFilltypeInfos
DescriptionDefinitiongetFilltypeInfos()Code
375 | function 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 |
385 | end |
getFoodFactor
DescriptionDefinitiongetFoodFactor()Code
336 | function HusbandryModuleFood:getFoodFactor() |
337 | return self.foodFactor |
338 | end |
getFreeCapacity
DescriptionSets a capacityDefinition
getFreeCapacity()Code
195 | function 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 |
234 | end |
getTotalFillLevel
DescriptionDefinitiongetTotalFillLevel()Code
246 | function 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 |
265 | end |
initDataStructures
DescriptionInitialize data structuresDefinition
initDataStructures()Code
46 | function HusbandryModuleFood:initDataStructures() |
47 | HusbandryModuleFood:superClass().initDataStructures(self) |
48 | self.feedingTrough = nil |
49 | self.foodFactor = 0.0 |
50 | self.consumedFood = {} |
51 | end |
load
DescriptionLoads data from xmlDefinition
load(table xmlFile, string xmlKey, table rootNode)Arguments
table | xmlFile | handle |
string | xmlKey | from which to read the configuration |
table | rootNode | of the husbandry |
boolean | true | if loading was successful else false |
59 | function 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 |
88 | end |
loadFromXMLFile
DescriptionLoads information from attributes and node. Retrives from xml file information.Definition
loadFromXMLFile(table xmlFile, string key)Arguments
table | xmlFile | XML file handler |
string | key | XML base key |
94 | function HusbandryModuleFood:loadFromXMLFile(xmlFile, key) |
95 | HusbandryModuleFood:superClass().loadFromXMLFile(self, xmlFile, key) |
96 | |
97 | self:updateFillPlane() |
98 | end |
new
DescriptionCreating managerDefinition
new()Return Values
table | instance | instance of object |
22 | function HusbandryModuleFood:new(customMt) |
23 | local self = HusbandryModuleBase:new(customMt or HusbandryModuleFood_mt) |
24 | |
25 | self.foodGroupCapacities = {} |
26 | return self |
27 | end |
onIntervalUpdate
DescriptionUpdate food usageDefinition
onIntervalUpdate(float dayToInterval)Arguments
float | dayToInterval |
270 | function 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 |
290 | end |
readStream
DescriptionCalled on client side on joinDefinition
readStream(integer streamId, table connection)Arguments
integer | streamId | stream ID |
table | connection | connection |
104 | function 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 |
112 | end |
setCapacity
DescriptionSets a capacityDefinition
setCapacity(float newCapacity)Arguments
float | newCapacity |
160 | function 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() |
167 | end |
setupFoodGroups
DescriptionDefinitionsetupFoodGroups()Code
130 | function 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 |
155 | end |
updateFillPlaneColor
DescriptionDefinitionupdateFillPlaneColor()Code
351 | function 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 |
371 | end |
writeStream
DescriptionCalled on server side on joinDefinition
writeStream(integer streamId, table connection)Arguments
integer | streamId | stream ID |
table | connection | connection |
118 | function 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 |
126 | end |