LUADOC - Farming Simulator 22

UnloadTrigger

Parent
Object
Functions

addFillUnitFillLevel

Description
Increase fill level
Definition
addFillUnitFillLevel(integer fillUnitIndex, float fillLevelDelta, integer fillTypeIndex, table toolType, table fillPositionData)
Arguments
integerfillUnitIndex
floatfillLevelDelta
integerfillTypeIndex
tabletoolType
tablefillPositionData
Return Values
bool
Code
286function UnloadTrigger:addFillUnitFillLevel(farmId, fillUnitIndex, fillLevelDelta, fillTypeIndex, toolType, fillPositionData, extraAttributes)
287 -- TODO: merge tables
288 local fillTypeConverison = self.fillTypeConversions[fillTypeIndex]
289 if fillTypeConverison ~= nil then
290 local convertedFillType, ratio = fillTypeConverison.outgoingFillType, fillTypeConverison.ratio
291 local applied = self.target:addFillLevelFromTool(farmId, fillLevelDelta*ratio, convertedFillType, fillPositionData, toolType, extraAttributes or self.extraAttributes)
292 return applied / ratio
293 end
294 local applied = self.target:addFillLevelFromTool(farmId, fillLevelDelta, fillTypeIndex, fillPositionData, toolType, extraAttributes or self.extraAttributes)
295 return applied
296end

delete

Description
Definition
delete()
Code
123function UnloadTrigger:delete()
124 if self.baleTrigger ~= nil then
125 self.baleTrigger:delete()
126 end
127
128 if self.woodTrigger ~= nil then
129 self.woodTrigger:delete()
130 end
131
132 if self.exactFillRootNode ~= nil then
133 g_currentMission:removeNodeObject(self.exactFillRootNode)
134 end
135
136 UnloadTrigger:superClass().delete(self)
137end

getCustomDischargeNotAllowedWarning

Description
Definition
getCustomDischargeNotAllowedWarning()
Code
400function UnloadTrigger:getCustomDischargeNotAllowedWarning()
401 return self.notAllowedWarningText
402end

getFillUnitAllowsFillType

Description
Checks if fill type is allowed
Definition
getFillUnitAllowsFillType(integer fillUnitIndex, integer fillType)
Arguments
integerfillUnitIndex
integerfillType
Return Values
booltrueif allowed
Code
312function UnloadTrigger:getFillUnitAllowsFillType(fillUnitIndex, fillType)
313 return self:getIsFillTypeAllowed(fillType)
314end

getFillUnitExactFillRootNode

Description
Returns exactFillRootNode
Definition
getFillUnitExactFillRootNode(integer fillUnitIndex)
Arguments
integerfillUnitIndexindex of fillunit
Code
274function UnloadTrigger:getFillUnitExactFillRootNode(fillUnitIndex)
275 return self.exactFillRootNode
276end

getFillUnitFreeCapacity

Description
Returns the free capacity
Definition
getFillUnitFreeCapacity(integer fillUnitIndex, integer fillTypeIndex)
Arguments
integerfillUnitIndexfill unit index
integerfillTypeIndexfill type index
Return Values
floatfreeCapacityfree capacity
Code
367function UnloadTrigger:getFillUnitFreeCapacity(fillUnitIndex, fillTypeIndex, farmId)
368 if self.target.getFreeCapacity ~= nil then
369 local conversion = self.fillTypeConversions[fillTypeIndex]
370 if conversion ~= nil then
371 return self.target:getFreeCapacity(conversion.outgoingFillType, farmId, self.extraAttributes) / conversion.ratio
372 end
373 return self.target:getFreeCapacity(fillTypeIndex, farmId, self.extraAttributes)
374 end
375 return 0
376end

getFillUnitIndexFromNode

Description
Returns default value '1'
Definition
getFillUnitIndexFromNode(integer node)
Arguments
integernodescenegraph node
Code
267function UnloadTrigger:getFillUnitIndexFromNode(node)
268 return 1
269end

getIsFillTypeAllowed

Description
Checks if fillType is allowed
Definition
getIsFillTypeAllowed(integer fillType)
Arguments
integerfillType
Return Values
booleanisAllowedtrue if fillType is supported else false
Code
320function UnloadTrigger:getIsFillTypeAllowed(fillType)
321 return self:getIsFillTypeSupported(fillType)
322end

getIsFillTypeSupported

Description
Checks if fillType is supported
Definition
getIsFillTypeSupported(integer fillType)
Arguments
integerfillType
Return Values
booleanisSupportedtrue if fillType is supported else false
Code
328function UnloadTrigger:getIsFillTypeSupported(fillType)
329 if self.fillTypes ~= nil then
330 if not self.fillTypes[fillType] then
331 return false
332 end
333 end
334
335 if self.avoidFillTypes ~= nil then
336 if self.avoidFillTypes[fillType] then
337 return false
338 end
339 end
340
341 if self.target ~= nil then
342 local conversion = self.fillTypeConversions[fillType]
343 if conversion ~= nil then
344 fillType = conversion.outgoingFillType
345 end
346 if not self.target:getIsFillTypeAllowed(fillType, self.extraAttributes) then
347 return false
348 end
349 end
350
351 return true
352end

getIsToolTypeAllowed

Description
Checks if toolType is allowed
Definition
getIsToolTypeAllowed(integer toolType)
Arguments
integertoolType
Return Values
booleanisAllowedtrue if toolType is allowed else false
Code
382function UnloadTrigger:getIsToolTypeAllowed(toolType)
383 local accepted = true
384
385 if self.acceptedToolTypes ~= nil then
386 if self.acceptedToolTypes[toolType] ~= true then
387 accepted = false
388 end
389 end
390
391 if accepted then
392 return self.target:getIsToolTypeAllowed(toolType)
393 else
394 return false
395 end
396end

load

Description
Loads elements of the class
Definition
load(table components, table xmlFile, string xmlNode, table target, table extraAttributes, table i3dMappings)
Arguments
tablecomponentscomponents
tablexmlFilexml file object
stringxmlNodexml key
tabletargettarget object
tableextraAttributesextra attributes
tablei3dMappingsi3dMappings
Return Values
booleansuccesssuccess
Code
53function UnloadTrigger:load(components, xmlFile, xmlNode, target, extraAttributes, i3dMappings)
54 local baleTriggerKey = xmlNode..".baleTrigger"
55 if xmlFile:hasProperty(baleTriggerKey) then
56 self.baleTrigger = BaleUnloadTrigger.new(self.isServer, self.isClient)
57 if self.baleTrigger:load(components, xmlFile, baleTriggerKey, self, i3dMappings) then
58 self.baleTrigger:setTarget(self)
59 self.baleTrigger:register(true)
60 else
61 self.baleTrigger = nil
62 end
63 end
64
65 local woodTriggerKey = xmlNode..".woodTrigger"
66 if xmlFile:hasProperty(woodTriggerKey) then
67 self.woodTrigger = WoodUnloadTrigger.new(self.isServer, self.isClient)
68 if self.woodTrigger:load(components, xmlFile, woodTriggerKey, self, i3dMappings) then
69 self.woodTrigger:setTarget(self)
70 self.woodTrigger:register(true)
71 else
72 self.woodTrigger = nil
73 end
74 end
75
76 self.exactFillRootNode = xmlFile:getValue(xmlNode .. "#exactFillRootNode", nil, components, i3dMappings)
77
78 if self.exactFillRootNode ~= nil then
79 if not CollisionFlag.getHasFlagSet(self.exactFillRootNode, CollisionFlag.FILLABLE) then
80 Logging.xmlWarning(xmlFile, "Missing collision mask bit '%d'. Please add this bit to exact fill root node '%s' of unloadTrigger", CollisionFlag.getBit(CollisionFlag.FILLABLE), I3DUtil.getNodePath(self.exactFillRootNode))
81 return false
82 end
83
84 g_currentMission:addNodeObject(self.exactFillRootNode, self)
85 end
86
87 self.aiNode = xmlFile:getValue(xmlNode .. "#aiNode", nil, components, i3dMappings)
88 self.supportsAIUnloading = self.aiNode ~= nil
89
90 local priceScale = xmlFile:getValue(xmlNode .. "#priceScale", nil)
91 if priceScale ~= nil then
92 self.extraAttributes = {priceScale = priceScale}
93 end
94
95 xmlFile:iterate(xmlNode .. ".fillTypeConversion", function(index, fillTypeConversionPath)
96 local fillTypeIndexIncoming = g_fillTypeManager:getFillTypeIndexByName(xmlFile:getValue(fillTypeConversionPath .. "#incomingFillType"))
97 if fillTypeIndexIncoming ~= nil then
98 local fillTypeIndexOutgoing = g_fillTypeManager:getFillTypeIndexByName(xmlFile:getValue(fillTypeConversionPath .. "#outgoingFillType"))
99 if fillTypeIndexOutgoing ~= nil then
100 local ratio = MathUtil.clamp(xmlFile:getValue(fillTypeConversionPath .. "#ratio", 1), 0.01, 10000)
101 self.fillTypeConversions[fillTypeIndexIncoming] = {outgoingFillType=fillTypeIndexOutgoing, ratio=ratio}
102 end
103 end
104 end)
105
106 if target ~= nil then
107 self:setTarget(target)
108 end
109
110 self:loadFillTypes(xmlFile, xmlNode)
111 self:loadAcceptedToolType(xmlFile, xmlNode)
112 self:loadAvoidFillTypes(xmlFile, xmlNode)
113 self.isEnabled = true
114
115 --TODO: merge tables
116 self.extraAttributes = extraAttributes or self.extraAttributes
117
118 return true
119end

loadAcceptedToolType

Description
Loads accepted tool type
Definition
loadAcceptedToolType(table rootNode, string xmlFile, string xmlNode)
Arguments
tablerootNodeof the object
stringxmlFilefile to read
stringxmlNodexmlNode to read from
Code
186function UnloadTrigger:loadAcceptedToolType(xmlFile, xmlNode)
187 local acceptedToolTypeNames = xmlFile:getValue(xmlNode .. "#acceptedToolTypes")
188 local acceptedToolTypes = string.getVector(acceptedToolTypeNames)
189
190 if acceptedToolTypes ~= nil then
191 for _,acceptedToolType in pairs(acceptedToolTypes) do
192 local toolTypeInt = g_toolTypeManager:getToolTypeIndexByName(acceptedToolType)
193 self.acceptedToolTypes[toolTypeInt] = true
194 end
195 else
196 self.acceptedToolTypes = nil
197 end
198end

loadAvoidFillTypes

Description
Loads avoid fill Types
Definition
loadAvoidFillTypes(table rootNode, string xmlFile, string xmlNode)
Arguments
tablerootNodeof the object
stringxmlFilefile to read
stringxmlNodexmlNode to read from
Code
205function UnloadTrigger:loadAvoidFillTypes(xmlFile, xmlNode)
206 local avoidFillTypeCategories = xmlFile:getValue(xmlNode .. "#avoidFillTypeCategories")
207 local avoidFillTypeNames = xmlFile:getValue(xmlNode .. "#avoidFillTypes")
208 local avoidFillTypes = nil
209
210 if avoidFillTypeCategories ~= nil and avoidFillTypeNames == nil then
211 avoidFillTypes = g_fillTypeManager:getFillTypesByCategoryNames(avoidFillTypeCategories, "Warning: UnloadTrigger has invalid avoidFillTypeCategory '%s'.")
212 elseif avoidFillTypeCategories == nil and avoidFillTypeNames ~= nil then
213 avoidFillTypes = g_fillTypeManager:getFillTypesByNames(avoidFillTypeNames, "Warning: UnloadTrigger has invalid avoidFillType '%s'.")
214 end
215 if avoidFillTypes ~= nil then
216 for _,fillType in pairs(avoidFillTypes) do
217 self.avoidFillTypes[fillType] = true
218 end
219 else
220 self.avoidFillTypes = nil
221 end
222end

loadFillTypes

Description
Loads fill Types
Definition
loadFillTypes(table rootNode, string xmlFile, string xmlNode)
Arguments
tablerootNodeof the object
stringxmlFilefile to read
stringxmlNodexmlNode to read from
Code
229function UnloadTrigger:loadFillTypes(xmlFile, xmlNode)
230 local fillTypeCategories = xmlFile:getValue(xmlNode .. "#fillTypeCategories")
231 local fillTypeNames = xmlFile:getValue(xmlNode .. "#fillTypes")
232 local fillTypes = nil
233
234 if fillTypeCategories ~= nil and fillTypeNames == nil then
235 fillTypes = g_fillTypeManager:getFillTypesByCategoryNames(fillTypeCategories, "Warning: UnloadTrigger has invalid fillTypeCategory '%s'.")
236 elseif fillTypeNames ~= nil then
237 fillTypes = g_fillTypeManager:getFillTypesByNames(fillTypeNames, "Warning: UnloadTrigger has invalid fillType '%s'.")
238 end
239 if fillTypes ~= nil then
240 for _, fillType in pairs(fillTypes) do
241 self.fillTypes[fillType] = true
242 end
243 else
244 self.fillTypes = nil
245 end
246end

new

Description
Creates a new instance of the class
Definition
new(bool isServer, bool isClient, table customMt)
Arguments
boolisServertrue if we are server
boolisClienttrue if we are client
tablecustomMtmeta table
Return Values
tableselfreturns the instance
Code
30function UnloadTrigger.new(isServer, isClient, customMt)
31 local self = Object.new(isServer, isClient, customMt or UnloadTrigger_mt)
32
33 self.fillTypes = {}
34 self.avoidFillTypes = {}
35 self.acceptedToolTypes = {}
36 self.fillTypeConversions = {}
37 self.notAllowedWarningText = nil
38
39 self.extraAttributes = nil
40
41 return self
42end

readStream

Description
Called on client side on join
Definition
readStream(integer streamId, table connection)
Arguments
integerstreamIdstream ID
tableconnectionconnection
Code
145function UnloadTrigger:readStream(streamId, connection)
146 UnloadTrigger:superClass().readStream(self, streamId, connection)
147 if connection:getIsServer() then
148 if self.baleTrigger ~= nil then
149 local baleTriggerId = NetworkUtil.readNodeObjectId(streamId)
150 self.baleTrigger:readStream(streamId, connection)
151 g_client:finishRegisterObject(self.baleTrigger, baleTriggerId)
152 end
153 if self.woodTrigger ~= nil then
154 local woodTriggerId = NetworkUtil.readNodeObjectId(streamId)
155 self.woodTrigger:readStream(streamId, connection)
156 g_client:finishRegisterObject(self.woodTrigger, woodTriggerId)
157 end
158 end
159end

registerXMLPaths

Description
Definition
registerXMLPaths()
Code
416function UnloadTrigger.registerXMLPaths(schema, basePath)
417 BaleUnloadTrigger.registerXMLPaths(schema, basePath .. ".baleTrigger")
418 WoodUnloadTrigger.registerXMLPaths(schema, basePath .. ".woodTrigger")
419
420 schema:register(XMLValueType.NODE_INDEX, basePath .. "#exactFillRootNode", "Exact fill root node")
421 schema:register(XMLValueType.FLOAT, basePath .. "#priceScale", "Price scale added for sold goods")
422 schema:register(XMLValueType.STRING, basePath .. "#acceptedToolTypes", "List of accepted tool types")
423 schema:register(XMLValueType.STRING, basePath .. "#avoidFillTypeCategories", "Avoided fill type categories (Even if target would allow the fill type)")
424 schema:register(XMLValueType.STRING, basePath .. "#avoidFillTypes", "Avoided fill types (Even if target would allow the fill type)")
425 schema:register(XMLValueType.STRING, basePath .. "#fillTypeCategories", "Supported fill type categories")
426 schema:register(XMLValueType.STRING, basePath .. "#fillTypes", "Supported fill types")
427 schema:register(XMLValueType.NODE_INDEX, basePath .. "#aiNode", "AI target node, required for the station to support AI. AI drives to the node in positive Z direction. Height is not relevant.")
428 schema:register(XMLValueType.STRING, basePath .. ".fillTypeConversion(?)#incomingFillType", "Filltype to be converted")
429 schema:register(XMLValueType.STRING, basePath .. ".fillTypeConversion(?)#outgoingFillType", "Filltype to be converted to")
430 schema:register(XMLValueType.FLOAT, basePath .. ".fillTypeConversion(?)#ratio", "Conversion ratio between input- and output amount", 1)
431end

setTarget

Description
Connects object using the trigger to the trigger
Definition
setTarget(table object)
Arguments
tableobjecttarget on which the unload trigger is attached
Code
251function UnloadTrigger:setTarget(object)
252 assert(object.getIsFillTypeAllowed ~= nil, "Missing 'getIsFillTypeAllowed' method for given target")
253 assert(object.getIsToolTypeAllowed ~= nil, "Missing 'getIsToolTypeAllowed' method for given target")
254 assert(object.addFillLevelFromTool ~= nil, "Missing 'addFillLevelFromTool' method for given target")
255 assert(object.getFreeCapacity ~= nil, "Missing 'getFreeCapacity' method for given target")
256
257 self.target = object
258end

writeStream

Description
Called on server side on join
Definition
writeStream(integer streamId, table connection)
Arguments
integerstreamIdstream ID
tableconnectionconnection
Code
165function UnloadTrigger:writeStream(streamId, connection)
166 UnloadTrigger:superClass().writeStream(self, streamId, connection)
167 if not connection:getIsServer() then
168 if self.baleTrigger ~= nil then
169 NetworkUtil.writeNodeObjectId(streamId, NetworkUtil.getObjectId(self.baleTrigger))
170 self.baleTrigger:writeStream(streamId, connection)
171 g_server:registerObjectInStream(connection, self.baleTrigger)
172 end
173 if self.woodTrigger ~= nil then
174 NetworkUtil.writeNodeObjectId(streamId, NetworkUtil.getObjectId(self.woodTrigger))
175 self.woodTrigger:writeStream(streamId, connection)
176 g_server:registerObjectInStream(connection, self.woodTrigger)
177 end
178 end
179end