LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

PlaceableHusbandryMilk

Description
Specialization for placeables
Functions

getConditionInfos

Description
Definition
getConditionInfos()
Code
194function PlaceableHusbandryMilk:getConditionInfos(superFunc)
195 local spec = self.spec_husbandryMilk
196 local infos = superFunc(self)
197
198 local info = {}
199 local fillType = g_fillTypeManager:getFillTypeByIndex(spec.fillType)
200 info.title = fillType.title
201 info.value = self:getHusbandryFillLevel(spec.fillType)
202 local capacity = self:getHusbandryCapacity(spec.fillType)
203 local ratio = 0
204 if capacity > 0 then
205 ratio = info.value / capacity
206 end
207 info.ratio = MathUtil.clamp(ratio, 0, 1)
208 info.invertedBar = true
209
210 table.insert(infos, info)
211
212 return infos
213end

onDelete

Description
Definition
onDelete()
Code
112function PlaceableHusbandryMilk:onDelete()
113 local spec = self.spec_husbandryMilk
114 if spec.milkingRobots ~= nil then
115 for _, robot in ipairs(spec.milkingRobots) do
116 robot:delete()
117 end
118 spec.milkingRobots = {}
119 end
120end

onFinalizePlacement

Description
Definition
onFinalizePlacement()
Code
124function PlaceableHusbandryMilk:onFinalizePlacement()
125 local spec = self.spec_husbandryMilk
126 if not self:getHusbandryIsFillTypeSupported(spec.fillType) then
127 Logging.warning("Missing filltype 'milk' in husbandry storage!")
128 end
129
130 for _, robot in ipairs(spec.milkingRobots) do
131 robot:finalizePlacement()
132 end
133end

onHusbandryAnimalsUpdate

Description
Definition
onHusbandryAnimalsUpdate()
Code
163function PlaceableHusbandryMilk:onHusbandryAnimalsUpdate(clusters)
164 local spec = self.spec_husbandryMilk
165 spec.litersPerHour = 0
166 for _, cluster in ipairs(clusters) do
167 local subType = g_currentMission.animalSystem:getSubTypeByIndex(cluster.subTypeIndex)
168 if subType ~= nil then
169 local milk = subType.output.milk
170 if milk ~= nil then
171 local age = cluster:getAge()
172 local litersPerAnimals = milk:get(age)
173 local litersPerDay = litersPerAnimals * cluster:getNumAnimals()
174
175 spec.litersPerHour = spec.litersPerHour + (litersPerDay / 24)
176 end
177 end
178 end
179end

onLoad

Description
Definition
onLoad()
Code
63function PlaceableHusbandryMilk:onLoad(savegame)
64 local spec = self.spec_husbandryMilk
65
66 spec.litersPerHour = 0
67 spec.fillType = FillType.MILK
68 spec.info = {title=g_i18n:getText("fillType_milk"), text=""}
69 spec.milkingRobots = {}
70 spec.husbandry = nil
71
72 self.xmlFile:iterate("placeable.husbandry.milk.milkingRobots.milkingRobot", function(_, key)
73 local filename = Utils.getFilename(self.xmlFile:getValue(key .. "#filename", nil), self.baseDirectory)
74 if filename == nil then
75 Logging.xmlWarning(self.xmlFile, "Milkingrobot filename missing for '%s'", key)
76 return
77 end
78
79 local className = self.xmlFile:getValue(key .. "#class", "")
80 local class = ClassUtil.getClassObject(className)
81 if class == nil then
82 Logging.xmlWarning(self.xmlFile, "Milkingrobot class '%s' not defined for '%s'", className, key)
83 return
84 end
85
86 local linkNode = self.xmlFile:getValue(key .. "#linkNode", nil, self.components, self.i3dMappings)
87 if linkNode == nil then
88 Logging.xmlWarning(self.xmlFile, "Milkingrobot linkNode not defined for '%s'", key)
89 return
90 end
91
92 local args = {}
93 args.loadingTask = self:createLoadingTask(self)
94 local robot = class.new(self, self.baseDirectory)
95 if robot:load(linkNode, filename, self.onMilkingRobotLoaded, self, args) then
96 table.insert(spec.milkingRobots, robot)
97 else
98 self:finishLoadingTask(args.loadingTask)
99 robot:delete()
100 end
101 end)
102end

onMilkingRobotLoaded

Description
Definition
onMilkingRobotLoaded()
Code
106function PlaceableHusbandryMilk:onMilkingRobotLoaded(robot, args)
107 self:finishLoadingTask(args.loadingTask)
108end

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
22function PlaceableHusbandryMilk.prerequisitesPresent(specializations)
23 return SpecializationUtil.hasSpecialization(PlaceableHusbandry, specializations)
24end

registerEventListeners

Description
Definition
registerEventListeners()
Code
43function PlaceableHusbandryMilk.registerEventListeners(placeableType)
44 SpecializationUtil.registerEventListener(placeableType, "onLoad", PlaceableHusbandryMilk)
45 SpecializationUtil.registerEventListener(placeableType, "onDelete", PlaceableHusbandryMilk)
46 SpecializationUtil.registerEventListener(placeableType, "onFinalizePlacement", PlaceableHusbandryMilk)
47 SpecializationUtil.registerEventListener(placeableType, "onHusbandryAnimalsUpdate", PlaceableHusbandryMilk)
48end

registerFunctions

Description
Definition
registerFunctions()
Code
28function PlaceableHusbandryMilk.registerFunctions(placeableType)
29 SpecializationUtil.registerFunction(placeableType, "onMilkingRobotLoaded", PlaceableHusbandryMilk.onMilkingRobotLoaded)
30end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
34function PlaceableHusbandryMilk.registerOverwrittenFunctions(placeableType)
35 SpecializationUtil.registerOverwrittenFunction(placeableType, "updateOutput", PlaceableHusbandryMilk.updateOutput)
36 SpecializationUtil.registerOverwrittenFunction(placeableType, "updateProduction", PlaceableHusbandryMilk.updateProduction)
37 SpecializationUtil.registerOverwrittenFunction(placeableType, "updateInfo", PlaceableHusbandryMilk.updateInfo)
38 SpecializationUtil.registerOverwrittenFunction(placeableType, "getConditionInfos", PlaceableHusbandryMilk.getConditionInfos)
39end

registerXMLPaths

Description
Definition
registerXMLPaths()
Code
52function PlaceableHusbandryMilk.registerXMLPaths(schema, basePath)
53 schema:setXMLSpecializationType("Husbandry")
54 basePath = basePath .. ".husbandry.milk"
55 schema:register(XMLValueType.NODE_INDEX, basePath .. ".milkingRobots.milkingRobot(?)#linkNode", "Milkingrobot link node")
56 schema:register(XMLValueType.STRING, basePath .. ".milkingRobots.milkingRobot(?)#class", "Milkingrobot class name")
57 schema:register(XMLValueType.STRING, basePath .. ".milkingRobots.milkingRobot(?)#filename", "Milkingrobot config file")
58 schema:setXMLSpecializationType()
59end

updateInfo

Description
Definition
updateInfo()
Code
183function PlaceableHusbandryMilk:updateInfo(superFunc, infoTable)
184 local spec = self.spec_husbandryMilk
185 superFunc(self, infoTable)
186
187 local fillLevel = self:getHusbandryFillLevel(spec.fillType)
188 spec.info.text = string.format("%d l", fillLevel)
189 table.insert(infoTable, spec.info)
190end

updateOutput

Description
Definition
updateOutput()
Code
137function PlaceableHusbandryMilk:updateOutput(superFunc, foodFactor, productionFactor, globalProductionFactor)
138 local spec = self.spec_husbandryMilk
139 if self.isServer and spec.litersPerHour > 0 then
140 local liters = productionFactor * globalProductionFactor * spec.litersPerHour * g_currentMission.environment.timeAdjustment
141 self:addHusbandryFillLevelFromTool(self:getOwnerFarmId(), liters, spec.fillType, nil, nil, nil)
142 end
143
144 superFunc(self, foodFactor, productionFactor, globalProductionFactor)
145end

updateProduction

Description
Definition
updateProduction()
Code
149function PlaceableHusbandryMilk:updateProduction(superFunc, foodFactor)
150 local spec = self.spec_husbandryMilk
151 local factor = superFunc(self, foodFactor)
152
153 local freeCapacity = self:getHusbandryFreeCapacity(spec.fillType)
154 if freeCapacity <= 0 then
155 factor = 0
156 end
157
158 return factor
159end