LUADOC - Farming Simulator 19

HusbandryModuleFoodSpillage

Description
This class handles water in husbandry
Parent
HusbandryModuleBase
Functions

delete

Description
Deletes instance
Definition
delete()
Code
28function HusbandryModuleFoodSpillage:delete()
29end

getFoodSpillageLevel

Description
Get spillage level
Definition
getFoodSpillageLevel()
Return Values
floatspillagetotal
Code
234function HusbandryModuleFoodSpillage:getFoodSpillageLevel()
235 local totalLevel = 0
236 for _, spillageArea in ipairs(self.spillageAreas) do
237 local xs,_,zs = getWorldTranslation(spillageArea.start)
238 local xw,_,zw = getWorldTranslation(spillageArea.width)
239 local xh,_,zh = getWorldTranslation(spillageArea.height)
240 local fillLevel = DensityMapHeightUtil.getFillLevelAtArea(self.spillageFillType, xs, zs, xw, zw, xh, zh)
241 totalLevel = totalLevel + fillLevel
242 end
243 return totalLevel
244end

getSpillageFactor

Description
Definition
getSpillageFactor()
Code
249function HusbandryModuleFoodSpillage:getSpillageFactor()
250 if self.hasCleanliness then
251 return self.cleanlinessFactor
252 end
253
254 return nil
255end

initDataStructures

Description
Initialize data structures
Definition
initDataStructures()
Code
33function HusbandryModuleFoodSpillage:initDataStructures()
34 HusbandryModuleFoodSpillage:superClass().initDataStructures(self)
35
36 self.spillageAreas = {}
37 self.foodToDrop = 0
38 self.spillageFillType = FillType.UNKNOWN
39 self.lineOffset = 0
40 self.cleanlinessFactor = 0.0
41 self.hasCleanliness = false
42end

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
50function HusbandryModuleFoodSpillage:load(xmlFile, configKey, rootNode, owner, baseDirectory)
51 if not HusbandryModuleFoodSpillage:superClass().load(self, xmlFile, configKey, rootNode, owner, baseDirectory) then
52 return false
53 end
54
55 if not hasXMLProperty(xmlFile, configKey) then
56 return false
57 end
58
59 local i = 0
60 while true do
61 local areaKey = string.format("%s.area(%d)", configKey, i)
62 if not hasXMLProperty(xmlFile, areaKey) then
63 break
64 end
65 local start = I3DUtil.indexToObject(rootNode, getXMLString(xmlFile, areaKey .. "#startNode"))
66 local width = I3DUtil.indexToObject(rootNode, getXMLString(xmlFile, areaKey .. "#widthNode"))
67 local height = I3DUtil.indexToObject(rootNode, getXMLString(xmlFile, areaKey .. "#heightNode"))
68
69 if start ~= nil and width ~= nil and height ~= nil then
70 table.insert(self.spillageAreas, {start = start, width = width, height = height})
71 end
72 i = i + 1
73 end
74
75 local spillageFillType = getXMLString(xmlFile, configKey .. "#fillType")
76 if spillageFillType ~= nil then
77 local fillTypeIndex = g_fillTypeManager:getFillTypeIndexByName(spillageFillType)
78 if fillTypeIndex ~= nil then
79 self.spillageFillType = fillTypeIndex
80 end
81 end
82
83 self.hasCleanliness = true
84
85 return self.spillageFillType ~= nil and #self.spillageAreas > 0
86end

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
182function HusbandryModuleFoodSpillage:loadFromXMLFile(xmlFile, key)
183 HusbandryModuleFoodSpillage:superClass().loadFromXMLFile(self, xmlFile, key)
184
185 if self.hasCleanliness then
186 self.cleanlinessFactor = Utils.getNoNil(getXMLFloat(xmlFile, key.."#cleanlinessFactor"), self.cleanlinessFactor)
187 self.foodToDrop = Utils.getNoNil(getXMLFloat(xmlFile, key.."#foodToDrop"), self.foodToDrop)
188 end
189end

new

Description
Creating manager
Definition
new()
Return Values
tableinstanceinstance of object
Code
21function HusbandryModuleFoodSpillage:new(customMt)
22 local self = HusbandryModuleBase:new(customMt or HusbandryModuleFoodSpillage_mt)
23 return self
24end

onIntervalUpdate

Description
Update water usage
Definition
onIntervalUpdate(float dayToInterval)
Arguments
floatdayToInterval
Code
144function HusbandryModuleFoodSpillage:onIntervalUpdate(dayToInterval)
145 HusbandryModuleFoodSpillage:superClass().onIntervalUpdate(self, dayToInterval)
146 self:updateCleanlinessFactor(dayToInterval)
147end

readStream

Description
Reads network stream
Definition
readStream(integer streamId, table connection)
Arguments
integerstreamIdnetwork stream identification
tableconnectionconnection information
Code
93function HusbandryModuleFoodSpillage:readStream(streamId, connection)
94 HusbandryModuleFoodSpillage:superClass().readStream(self, streamId, connection)
95
96 if self.hasCleanliness then
97 self.cleanlinessFactor = streamReadUInt8(streamId) / 255
98 end
99end

readUpdateStream

Description
Read updates from network stream
Definition
readUpdateStream(integer streamId, integer timestamp, table connection)
Arguments
integerstreamIdnetwork stream identification
integertimestamp
tableconnectionconnection information
Code
119function HusbandryModuleFoodSpillage:readUpdateStream(streamId, timestamp, connection)
120 HusbandryModuleFoodSpillage:superClass().readUpdateStream(self, streamId, timestamp, connection)
121
122 if self.hasCleanliness then
123 self.cleanlinessFactor = streamReadUInt8(streamId) / 255
124 end
125end

updateFoodSpillage

Description
Update spillage mechanics.
Definition
updateFoodSpillage(float spillageDelta)
Arguments
floatspillageDeltaamount of food to increase
Return Values
floatfooddropped
Code
204function HusbandryModuleFoodSpillage:updateFoodSpillage(spillageDelta)
205 local foodDropped = 0
206
207 if self.hasCleanliness and self.cleanlinessFactor > 0 and spillageDelta > g_densityMapHeightManager:getMinValidLiterValue(self.spillageFillType) then
208 local i = math.random(1, #self.spillageAreas)
209 local spillageArea = self.spillageAreas[i]
210 local xs,_,zs = getWorldTranslation(spillageArea.start)
211 local xw,_,zw = getWorldTranslation(spillageArea.width)
212 local xh,_,zh = getWorldTranslation(spillageArea.height)
213 local ux, uz = xw - xs, zw - zs
214 local vx, vz = xh - xs, zh - zs
215 local vLength = MathUtil.vector2Length(vx,vz)
216 local sx = xs + (math.random() * ux) + (math.random() * vx)
217 local sz = zs + (math.random() * uz) + (math.random() * vz)
218 local ex = xs + (math.random() * ux) + (math.random() * vx)
219 local ez = zs + (math.random() * uz) + (math.random() * vz)
220 local sy = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, sx, 0.0, sz)
221 local ey = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, ex, 0.0, ez)
222 local dropped, lineOffset = DensityMapHeightUtil.tipToGroundAroundLine(nil, spillageDelta, self.spillageFillType, sx,sy,sz, ex,ey,ez, 0, vLength, self.lineOffset, false, nil)
223
224 foodDropped = dropped
225 self.lineOffset = lineOffset
226 end
227
228 return foodDropped
229end

writeStream

Description
Writes network stream
Definition
writeStream(integer streamId, table connection)
Arguments
integerstreamIdnetwork stream identification
tableconnectionconnection information
Code
105function HusbandryModuleFoodSpillage:writeStream(streamId, connection)
106 HusbandryModuleFoodSpillage:superClass().writeStream(self, streamId, connection)
107
108 if self.hasCleanliness then
109 local cleanliness = math.floor(self.cleanlinessFactor * 255 + 0.5)
110 streamWriteUInt8(streamId, cleanliness)
111 end
112end

writeUpdateStream

Description
Write updates from network stream
Definition
writeUpdateStream(integer streamId, table connection, integer dirtyMask)
Arguments
integerstreamIdnetwork stream identification
tableconnectionconnection information
integerdirtyMaskis used to check if we need to update
Code
132function HusbandryModuleFoodSpillage:writeUpdateStream(streamId, connection, dirtyMask)
133 HusbandryModuleFoodSpillage:superClass().writeUpdateStream(self, streamId, connection, dirtyMask)
134
135 if self.hasCleanliness then
136 local cleanliness = math.floor(self.cleanlinessFactor * 255 + 0.5)
137 streamWriteUInt8(streamId, cleanliness)
138 end
139end