LUADOC - Farming Simulator 19

GreenhousePlaceable

Description
Class for placeable greenhouse
Parent
Placeable
Functions

collectPickObjects

Description
Collect pick objects. (Skip unload trigger nodes to protect against double registration)
Definition
collectPickObjects(integer node)
Arguments
integernodenode id
Code
292function GreenhousePlaceable:collectPickObjects(node)
293 local foundNode = false
294 for _, unloadTrigger in ipairs(self.manureTankStation.unloadTriggers) do
295 if node == unloadTrigger.exactFillRootNode then
296 foundNode = true
297 break
298 end
299 end
300
301 for _, unloadTrigger in ipairs(self.waterTankStation.unloadTriggers) do
302 if node == unloadTrigger.exactFillRootNode then
303 foundNode = true
304 break
305 end
306 end
307
308 if not foundNode then
309 GreenhousePlaceable:superClass().collectPickObjects(self, node)
310 end
311end

delete

Description
Deleting placeable greenhouse
Definition
delete()
Code
53function GreenhousePlaceable:delete()
54 if self.manureTankStation ~= nil then
55 g_currentMission.storageSystem:removeUnloadingStation(self.manureTankStation)
56 self.manureTankStation:delete()
57 end
58 if self.waterTankStation ~= nil then
59 g_currentMission.storageSystem:removeUnloadingStation(self.waterTankStation)
60 self.waterTankStation:delete()
61 end
62
63 if self.interactionTriggerNode ~= nil then
64 removeTrigger(self.interactionTriggerNode)
65 end
66
67 unregisterObjectClassName(self)
68 g_currentMission.environment:removeHourChangeListener(self)
69
70 if g_client ~= nil then
71 for _,sample in pairs(self.samples) do
72 g_soundManager:deleteSample(sample)
73 end
74 end
75
76 GreenhousePlaceable:superClass().delete(self)
77end

finalizePlacement

Description
Called if placeable is placed
Definition
finalizePlacement()
Code
236function GreenhousePlaceable:finalizePlacement()
237 GreenhousePlaceable:superClass().finalizePlacement(self)
238
239 g_currentMission.environment:addHourChangeListener(self)
240end

getCanInteract

Description
Returns true if info should show
Definition
getCanInteract()
Return Values
booleanshowInfoshow info
Code
330function GreenhousePlaceable:getCanInteract()
331 if g_currentMission.controlPlayer and self.playerInRange then
332 return true
333 end
334 if not g_currentMission.controlPlayer then
335 for vehicle in pairs(self.vehiclesInRange) do
336 if vehicle:getIsActiveForInput(true) then
337 return true
338 end
339 end
340 end
341
342 return false
343end

getCapacity

Description
Get capacity
Definition
getCapacity()
Code
418function GreenhousePlaceable:getCapacity(fillTypeIndex)
419 if fillTypeIndex == FillType.MANURE then
420 return self.manureTankCapacity
421 else
422 return self.waterTankCapacity
423 end
424end

getFillLevel

Description
Get fill level
Definition
getFillLevel()
Code
408function GreenhousePlaceable:getFillLevel(fillTypeIndex)
409 if fillTypeIndex == FillType.MANURE then
410 return self.manureTankFillLevel
411 else
412 return self.waterTankFillLevel
413 end
414end

getFreeCapacity

Description
Get free capacity
Definition
getFreeCapacity()
Code
469function GreenhousePlaceable:getFreeCapacity(fillTypeIndex)
470 if fillTypeIndex == FillType.MANURE then
471 return self.manureTankCapacity - self.manureTankFillLevel
472 else
473 return self.waterTankCapacity - self.waterTankFillLevel
474 end
475end

getIsFillTypeSupported

Description
Get supported fill types for given station
Definition
getIsFillTypeSupported()
Code
402function GreenhousePlaceable:getIsFillTypeSupported(fillTypeIndex, station)
403 return (fillTypeIndex == FillType.MANURE and station == self.manureTankStation) or (fillTypeIndex == FillType.WATER and station == self.waterTankStation)
404end

getSupportedFillTypes

Description
Get supported fill types
Definition
getSupportedFillTypes()
Code
479function GreenhousePlaceable:getSupportedFillTypes()
480 return {FillType.MANURE, FillType.WATER}
481end

hourChanged

Description
Called if hour changed
Definition
hourChanged()
Code
347function GreenhousePlaceable:hourChanged()
348 if self.isServer then
349 self:setFillLevel(self.waterTankFillLevel - self.waterTankUsagePerHour, FillType.WATER)
350 self:setFillLevel(self.manureTankFillLevel - self.manureUsagePerHour, FillType.MANURE)
351
352 if self.isFruitAlive then
353 local income = self.incomePerHour
354 if self.manureTankFillLevel > 0 then
355 income = income * 2
356 end
357
358 g_currentMission:addMoney(income, self:getOwnerFarmId(), MoneyType.PROPERTY_INCOME, true)
359 end
360 end
361end

interactionTriggerCallback

Description
interactionTriggerCallback
Definition
interactionTriggerCallback(integer triggerId, integer otherId, boolean onEnter, boolean onLeave, boolean onStay, integer otherId)
Arguments
integertriggerIdid of trigger
integerotherIdid of actor
booleanonEnteron enter
booleanonLeaveon leave
booleanonStayon stay
integerotherIdid of other actor
Code
503function GreenhousePlaceable:interactionTriggerCallback(triggerId, otherId, onEnter, onLeave, onStay, otherShapeId)
504 if onEnter or onLeave then
505 if g_currentMission.player ~= nil and otherId == g_currentMission.player.rootNode then
506 if onEnter then
507 self.playerInRange = true
508 else
509 self.playerInRange = false
510 end
511 else
512 local vehicle = g_currentMission.nodeToObject[otherShapeId]
513 if vehicle ~= nil then
514 if onEnter then
515 if self.vehiclesInRange[vehicle] == nil then
516 self.vehiclesInRange[vehicle] = true
517 self.numVehiclesInRange = self.numVehiclesInRange + 1
518 end
519 else
520 if self.vehiclesInRange[vehicle] then
521 self.vehiclesInRange[vehicle] = nil
522 self.numVehiclesInRange = self.numVehiclesInRange - 1
523 end
524 end
525 end
526 end
527 end
528end

load

Description
Load greenhouse
Definition
load(string xmlFilename, float x, float y, float z, float rx, float ry, float rz, boolean initRandom)
Arguments
stringxmlFilenamexml file name
floatxx world position
floatyz world position
floatzz world position
floatrxrx world rotation
floatryry world rotation
floatrzrz world rotation
booleaninitRandominitialize random
Return Values
booleansuccesssuccess
Code
162function GreenhousePlaceable:load(xmlFilename, x,y,z, rx,ry,rz, initRandom)
163 if not GreenhousePlaceable:superClass().load(self, xmlFilename, x,y,z, rx,ry,rz, initRandom) then
164 return false
165 end
166
167 local xmlFile = loadXMLFile("TempXML", xmlFilename)
168
169 local fruitAlive = I3DUtil.indexToObject(self.nodeId, getXMLString(xmlFile, "placeable.greenhouse.fruit#alive"))
170 local fruitDead = I3DUtil.indexToObject(self.nodeId, getXMLString(xmlFile, "placeable.greenhouse.fruit#dead"))
171 if fruitAlive ~= nil then
172 self.fruitAlive = fruitAlive
173 end
174 if fruitDead ~= nil then
175 self.fruitDead = fruitDead
176 end
177
178 self.interactionTriggerNode = I3DUtil.indexToObject(self.nodeId, getXMLString(xmlFile, "placeable.greenhouse.interactionTrigger#node"))
179 if self.interactionTriggerNode ~= nil then
180 addTrigger(self.interactionTriggerNode, "interactionTriggerCallback", self)
181 end
182
183 self.waterTankCapacity = Utils.getNoNil(getXMLFloat(xmlFile, "placeable.greenhouse.waterTank#capacity"), self.waterTankCapacity)
184 self.waterTankUsagePerHour = Utils.getNoNil(getXMLFloat(xmlFile, "placeable.greenhouse.waterTank#usagePerHour"), self.waterTankUsagePerHour)
185 self.waterTankFillLitersPerSecond = Utils.getNoNil(getXMLFloat(xmlFile, "placeable.greenhouse.waterTank#fillLitersPerSecond"), self.waterTankFillLitersPerSecond)
186 local unloadingNode = I3DUtil.indexToObject(self.nodeId, getXMLString(xmlFile, "placeable.greenhouse.waterTank.unloadingStation#node"))
187 if unloadingNode ~= nil then
188 local unloadingStation = UnloadingStation:new(self.isServer, self.isClient)
189 if unloadingStation:load(unloadingNode, xmlFile, "placeable.greenhouse.waterTank.unloadingStation", self.customEnvironment) then
190 self.waterTankStation = unloadingStation
191 unloadingStation:addTargetStorage(self)
192 else
193 unloadingStation:delete()
194 end
195 end
196
197 self.samples = {}
198 if g_client ~= nil then
199 self.samples.refuel = g_soundManager:loadSampleFromXML(xmlFile, "placeable.greenhouse.waterTank.sounds", "refuel", self.baseDirectory, self.nodeId, 0, AudioGroup.VEHICLE, nil, nil)
200 end
201
202 self.manurePlane = I3DUtil.indexToObject(self.nodeId, getXMLString(xmlFile, "placeable.greenhouse.manureTank.manurePlane#node"))
203
204 local unloadingNode = I3DUtil.indexToObject(self.nodeId, getXMLString(xmlFile, "placeable.greenhouse.manureTank.unloadingStation#node"))
205 if unloadingNode ~= nil then
206 local unloadingStation = UnloadingStation:new(self.isServer, self.isClient)
207 if unloadingStation:load(unloadingNode, xmlFile, "placeable.greenhouse.manureTank.unloadingStation", self.customEnvironment) then
208 self.manureTankStation = unloadingStation
209 unloadingStation:addTargetStorage(self)
210 else
211 unloadingStation:delete()
212 end
213 end
214
215 local minY, maxY = StringUtil.getVectorFromString(getXMLString(xmlFile, "placeable.greenhouse.manureTank.manurePlane#minMaxY"))
216 if minY ~= nil and maxY ~= nil then
217 self.manurePlaneMinY = minY
218 self.manurePlaneMaxY = maxY
219 end
220 self.manureTankCapacity = Utils.getNoNil(getXMLFloat(xmlFile, "placeable.greenhouse.manureTank#capacity"), self.manureTankCapacity)
221 self.manureUsagePerHour = Utils.getNoNil(getXMLFloat(xmlFile, "placeable.greenhouse.manureTank#usagePerHour"), self.manureUsagePerHour)
222
223 delete(xmlFile)
224
225 self:setFillLevel(0, FillType.WATER)
226 self.sentWaterTankFillLevel = self.waterTankFillLevel
227
228 self:setFillLevel(0, FillType.MANURE)
229 self.sentManureFillLevel = self.manureTankFillLevel
230
231 return true
232end

loadFromXMLFile

Description
Loading from attributes and nodes
Definition
loadFromXMLFile(integer xmlFile, string key, boolean resetVehicles)
Arguments
integerxmlFileid of xml object
stringkeykey
booleanresetVehiclesreset vehicles
Return Values
booleansuccesssuccess
Code
260function GreenhousePlaceable:loadFromXMLFile(xmlFile, key, resetVehicles)
261 if not GreenhousePlaceable:superClass().loadFromXMLFile(self, xmlFile, key, resetVehicles) then
262 return false
263 end
264
265 local waterTankFillLevel = getXMLFloat(xmlFile, key.."#waterTankFillLevel")
266 if waterTankFillLevel ~= nil then
267 self:setFillLevel(waterTankFillLevel, FillType.WATER)
268 end
269 local manureFillLevel = getXMLFloat(xmlFile, key.."#manureFillLevel")
270 if manureFillLevel ~= nil then
271 self:setFillLevel(manureFillLevel, FillType.MANURE)
272 end
273
274 return true
275end

new

Description
Creating placeable greenhouse
Definition
new(boolean isServer, boolean isClient, table customMt)
Arguments
booleanisServeris server
booleanisClientis client
tablecustomMtcustom metatable
Return Values
tableinstanceInstance of object
Code
18function GreenhousePlaceable:new(isServer, isClient, customMt)
19 local self = Placeable:new(isServer, isClient, customMt or GreenhousePlaceable_mt)
20
21 registerObjectClassName(self, "GreenhousePlaceable")
22
23 self.waterTankCapacity = 1000
24 self.waterTankFillLevel = 1000
25 self.sentWaterTankFillLevel = self.waterTankFillLevel
26 self.waterTankUsagePerHour = 0
27 self.waterTankFillLitersPerSecond = 200
28 self.waterTrailers = {}
29 self.isFruitAlive = true
30 self.displayFruit = false
31
32 self.manureTankFillLevel = 0
33 self.manureUsagePerHour = 0
34 self.manureTankCapacity = 200
35 self.manurePlaneMinY = 0
36 self.manurePlaneMaxY = 1
37 self.sentManureFillLevel = self.manureTankFillLevel
38
39 self.vehiclesInRange = {}
40 self.playerInRange = false
41
42 self.greenhousePlaceableDirtyFlag = self:getNextDirtyFlag()
43
44 self.playerInRange = false
45 self.vehiclesInRange = {}
46 self.numVehiclesInRange = 0
47
48 return self
49end

readStream

Description
Called on client side on join
Definition
readStream(integer streamId, table connection)
Arguments
integerstreamIdstream ID
tableconnectionconnection
Code
83function GreenhousePlaceable:readStream(streamId, connection)
84 GreenhousePlaceable:superClass().readStream(self, streamId, connection)
85 if connection:getIsServer() then
86 local waterTankFillLevel = streamReadUInt8(streamId)/255*self.waterTankCapacity
87 self:setFillLevel(waterTankFillLevel, FillType.WATER)
88
89 local manureFillLevel = streamReadUInt8(streamId)/255*self.manureTankCapacity
90 self:setFillLevel(manureFillLevel, FillType.MANURE)
91
92 local unloadTriggerId = NetworkUtil.readNodeObjectId(streamId)
93 self.waterTankStation:readStream(streamId, connection)
94 g_client:finishRegisterObject(self.waterTankStation, unloadTriggerId)
95
96 local unloadingStationId = NetworkUtil.readNodeObjectId(streamId)
97 self.manureTankStation:readStream(streamId, connection)
98 g_client:finishRegisterObject(self.manureTankStation, unloadingStationId)
99 end
100end

readUpdateStream

Description
Called on client side on update
Definition
readUpdateStream(integer streamId, integer timestamp, table connection)
Arguments
integerstreamIdstream ID
integertimestamptimestamp
tableconnectionconnection
Code
127function GreenhousePlaceable:readUpdateStream(streamId, timestamp, connection)
128 GreenhousePlaceable:superClass().readUpdateStream(self, streamId, timestamp, connection)
129 if connection:getIsServer() then
130 local waterTankFillLevel = streamReadUInt8(streamId)/255*self.waterTankCapacity
131 self:setFillLevel(waterTankFillLevel, FillType.WATER)
132
133 local manureFillLevel = streamReadUInt8(streamId)/255*self.manureTankCapacity
134 self:setFillLevel(manureFillLevel, FillType.MANURE)
135 end
136end

register

Description
Called when the object is registered. Register any owned objects
Definition
register()
Code
244function GreenhousePlaceable:register(...)
245 GreenhousePlaceable:superClass().register(self, ...)
246
247 self.manureTankStation:register(true)
248 g_currentMission.storageSystem:addUnloadingStation(self.manureTankStation)
249
250 self.waterTankStation:register(true)
251 g_currentMission.storageSystem:addUnloadingStation(self.waterTankStation)
252end

saveToXMLFile

Description
Get save attributes and nodes
Definition
saveToXMLFile(string nodeIdent)
Arguments
stringnodeIdentnode ident
Return Values
stringattributesattributes
stringnodesnodes
Code
282function GreenhousePlaceable:saveToXMLFile(xmlFile, key, usedModNames)
283 GreenhousePlaceable:superClass().saveToXMLFile(self, xmlFile, key, usedModNames)
284
285 setXMLFloat(xmlFile, key.."#waterTankFillLevel", self.waterTankFillLevel)
286 setXMLFloat(xmlFile, key.."#manureFillLevel", self.manureTankFillLevel)
287end

setFillLevel

Description
Set fill level
Definition
setFillLevel()
Code
428function GreenhousePlaceable:setFillLevel(fillLevel, fillTypeIndex)
429 if fillTypeIndex == FillType.MANURE then
430 self.manureTankFillLevel = MathUtil.clamp(fillLevel, 0, self.manureTankCapacity)
431
432 if self.manurePlane ~= nil then
433 setVisibility(self.manurePlane, self.manureTankFillLevel > 0.0001)
434 local x,y,z = getTranslation(self.manurePlane)
435 y = self.manurePlaneMinY + self.manureTankFillLevel / self.manureTankCapacity * (self.manurePlaneMaxY - self.manurePlaneMinY)
436
437 setTranslation(self.manurePlane, x,y,z)
438 end
439
440 if self.manureTankFillLevel ~= self.sentManureFillLevel then
441 self:raiseDirtyFlags(self.greenhousePlaceableDirtyFlag)
442 self.sentManureFillLevel = self.manureTankFillLevel
443 end
444 elseif fillTypeIndex == FillType.WATER then
445 self.waterTankFillLevel = MathUtil.clamp(fillLevel, 0, self.waterTankCapacity)
446
447 if self.waterTankFillLevel ~= self.sentWaterTankFillLevel then
448 self:raiseDirtyFlags(self.greenhousePlaceableDirtyFlag)
449 self.sentWaterTankFillLevel = self.waterTankFillLevel
450 end
451
452 self.isFruitAlive = self.waterTankFillLevel > 0.0001
453
454 if self.waterTankFillLevel > 0 then
455 self.displayFruit = true
456 end
457
458 if self.fruitAlive ~= nil then
459 setVisibility(self.fruitAlive, self.isFruitAlive and self.displayFruit)
460 end
461 if self.fruitDead ~= nil then
462 setVisibility(self.fruitDead, not self.isFruitAlive and self.displayFruit)
463 end
464 end
465end

setIsWaterTankFilling

Description
Set is water tank filling
Definition
setIsWaterTankFilling(boolean isWaterTankFilling, table trailer, boolean noEventSend)
Arguments
booleanisWaterTankFillingis water tank filling
tabletrailertrailer
booleannoEventSendno event send
Code
368function GreenhousePlaceable:setIsWaterTankFilling(isWaterTankFilling, trailer, noEventSend)
369 GreenhouseSetIsWaterTankFillingEvent.sendEvent(self, isWaterTankFilling, trailer, noEventSend)
370 if self.isWaterTankFilling ~= isWaterTankFilling then
371 self.isWaterTankFilling = isWaterTankFilling
372 self.waterTankFillTrailer = trailer
373 end
374 if g_client ~= nil then
375 if isWaterTankFilling then
376 g_soundManager:playSample(self.samples.refuel)
377 else
378 g_soundManager:stopSample(self.samples.refuel)
379 end
380 end
381end

setOwnerFarmId

Description
Set owner farm. Updates unloading station for manure
Definition
setOwnerFarmId()
Code
385function GreenhousePlaceable:setOwnerFarmId(ownerFarmId, noEventSend)
386 GreenhousePlaceable:superClass().setOwnerFarmId(self, ownerFarmId, noEventSend)
387
388 if self.manureTankStation ~= nil then
389 self.manureTankStation:setOwnerFarmId(ownerFarmId)
390 end
391 if self.waterTankStation ~= nil then
392 self.waterTankStation:setOwnerFarmId(ownerFarmId)
393 end
394end

update

Description
Update
Definition
update(float dt)
Arguments
floatdttime since last call in ms
Code
316function GreenhousePlaceable:update(dt)
317 GreenhousePlaceable:superClass().update(self, dt)
318
319 if self:getCanInteract() then
320 g_currentMission:addExtraPrintText(g_i18n:getText("info_waterFillLevel")..": "..math.floor(self.waterTankFillLevel).." ("..math.floor(100*self.waterTankFillLevel/self.waterTankCapacity).."%)")
321 g_currentMission:addExtraPrintText(g_i18n:getText("info_manureFillLevel")..": "..math.floor(self.manureTankFillLevel).." ("..math.floor(100*self.manureTankFillLevel/self.manureTankCapacity).."%)")
322 end
323
324 self:raiseActive()
325end

writeStream

Description
Called on server side on join
Definition
writeStream(integer streamId, table connection)
Arguments
integerstreamIdstream ID
tableconnectionconnection
Code
106function GreenhousePlaceable:writeStream(streamId, connection)
107 GreenhousePlaceable:superClass().writeStream(self, streamId, connection)
108 if not connection:getIsServer() then
109 streamWriteUInt8(streamId, math.floor(self.waterTankFillLevel/self.waterTankCapacity * 255))
110 streamWriteUInt8(streamId, math.floor(self.manureTankFillLevel/self.manureTankCapacity * 255))
111
112 NetworkUtil.writeNodeObjectId(streamId, NetworkUtil.getObjectId(self.waterTankStation))
113 self.waterTankStation:writeStream(streamId, connection)
114 g_server:registerObjectInStream(connection, self.waterTankStation)
115
116 NetworkUtil.writeNodeObjectId(streamId, NetworkUtil.getObjectId(self.manureTankStation))
117 self.manureTankStation:writeStream(streamId, connection)
118 g_server:registerObjectInStream(connection, self.manureTankStation)
119 end
120end

writeUpdateStream

Description
Called on server side on update
Definition
writeUpdateStream(integer streamId, table connection, integer dirtyMask)
Arguments
integerstreamIdstream ID
tableconnectionconnection
integerdirtyMaskdirty mask
Code
143function GreenhousePlaceable:writeUpdateStream(streamId, connection, dirtyMask)
144 GreenhousePlaceable:superClass().writeUpdateStream(self, streamId, connection, dirtyMask)
145 if not connection:getIsServer() then
146 streamWriteUInt8(streamId, math.floor(self.waterTankFillLevel/self.waterTankCapacity * 255))
147 streamWriteUInt8(streamId, math.floor(self.manureTankFillLevel/self.manureTankCapacity * 255))
148 end
149end