LUADOC - Farming Simulator 19

LoadTrigger

Description
A load trigger defines the exact position for loading
Parent
Object
Functions

delete

Description
Definition
delete()
Code
147function LoadTrigger:delete()
148 if self.triggerNode ~= nil then
149 removeTrigger(self.triggerNode)
150 end
151 if self.isClient then
152 for _,sample in pairs(self.samples) do
153 g_soundManager:deleteSample(sample)
154 end
155 g_effectManager:deleteEffects(self.effects)
156 end
157
158 -- shared fill sound sample from FillTrigger class due to setFillSoundIsPlaying call
159 g_soundManager:deleteSample(self.sample)
160
161 g_currentMission:removeActivatableObject(self)
162
163 LoadTrigger:superClass().delete(self)
164end

drawActivate

Description
Definition
drawActivate()
Code
313function LoadTrigger:drawActivate()
314 return
315end

farmIdForFillableObject

Description
Get the farm id for given object. If none can be found, SPECTATOR is used.
Definition
farmIdForFillableObject()
Code
250function LoadTrigger:farmIdForFillableObject(fillableObject)
251 local objectFarmId = fillableObject:getOwnerFarmId()
252 if fillableObject.getActiveFarm ~= nil then
253 objectFarmId = fillableObject:getActiveFarm()
254 end
255
256 if objectFarmId == nil then
257 objectFarmId = FarmManager.SPECTATOR_FARM_ID
258 end
259
260 return objectFarmId
261end

getCurrentFillType

Description
Definition
getCurrentFillType()
Code
456function LoadTrigger:getCurrentFillType()
457 return self.selectedFillType
458end

getFillTargetNode

Description
Definition
getFillTargetNode()
Code
462function LoadTrigger:getFillTargetNode()
463 if self.currentFillableObject ~= nil then
464 return self.currentFillableObject:getFillUnitRootNode(self.fillUnitIndex)
465 end
466end

getIsActivatable

Description
Definition
getIsActivatable()
Code
265function LoadTrigger:getIsActivatable()
266 if next(self.fillableObjects) == nil then
267 return false
268 else
269 if self.isLoading then
270 if self.currentFillableObject ~= nil and self.currentFillableObject:getRootVehicle() == g_currentMission.controlledVehicle then
271 return true
272 end
273 else
274 self.validFillableObject = nil
275 self.validFillableFillUnitIndex = nil
276
277 -- last object that was filled has lower prio than the other objects in the trigger
278 -- so we can guarantee that all objects will be filled
279 local hasLowPrioObject = false
280 local numOfObjects = 0
281 for _, fillableObject in pairs(self.fillableObjects) do
282 if fillableObject.lastWasFilled then
283 hasLowPrioObject = true
284 end
285 numOfObjects = numOfObjects + 1
286 end
287 hasLowPrioObject = hasLowPrioObject and (numOfObjects > 1)
288
289 for _, fillableObject in pairs(self.fillableObjects) do
290 if not fillableObject.lastWasFilled or not hasLowPrioObject then
291 if fillableObject.object:getRootVehicle() == g_currentMission.controlledVehicle then
292 if fillableObject.object:getFillUnitSupportsToolType(fillableObject.fillUnitIndex, ToolType.TRIGGER) then
293 if not self.source:getIsFillAllowedToFarm(self:farmIdForFillableObject(fillableObject.object)) then
294 return false
295 end
296
297 self.validFillableObject = fillableObject.object
298 self.validFillableFillUnitIndex = fillableObject.fillUnitIndex
299
300 return true
301 end
302 end
303 end
304 end
305 end
306 end
307
308 return false
309end

load

Description
Definition
load()
Code
28function LoadTrigger:load(rootNode, xmlFile, xmlNode)
29 self.rootNode = rootNode
30
31 -- load triggers
32 self.objectsInTriggers = {}
33 local triggerNodeStr = XMLUtil.getValueFromXMLFileOrUserAttribute(xmlFile, xmlNode, "triggerNode", getXMLString, self.rootNode)
34 local triggerNode = I3DUtil.indexToObject(self.rootNode, triggerNodeStr)
35 -- old way
36 if triggerNode == nil then
37 triggerNodeStr = getUserAttribute(self.rootNode, "triggerIndex")
38 triggerNode = I3DUtil.indexToObject(self.rootNode, triggerNodeStr)
39 end
40
41 if triggerNode == nil then
42 print("Error: LoadTrigger could not load trigger. Check the user attribute 'triggerNode'")
43 printCallstack()
44 return false
45 end
46
47 self.triggerNode = triggerNode
48 addTrigger(triggerNode, "loadTriggerCallback", self)
49 g_currentMission:addNodeObject(triggerNode, self)
50
51 local fillLitersPerMS = XMLUtil.getValueFromXMLFileOrUserAttribute(xmlFile, xmlNode, "fillLitersPerSecond", getXMLInt, self.rootNode)
52 self.fillLitersPerMS = (tonumber(fillLitersPerMS) or 1000) / 1000
53
54 local dischargeNodeStr = XMLUtil.getValueFromXMLFileOrUserAttribute(xmlFile, xmlNode, "dischargeNode", getXMLString, self.rootNode)
55 local dischargeNode = I3DUtil.indexToObject(self.rootNode, dischargeNodeStr)
56 if dischargeNode ~= nil then
57 self.dischargeInfo = {}
58 self.dischargeInfo.name = "fillVolumeDischargeInfo"
59 self.dischargeInfo.nodes = {}
60 local width = Utils.getNoNil( getUserAttribute(dischargeNode, "width"), 0.5 )
61 local length = Utils.getNoNil( getUserAttribute(dischargeNode, "length"), 0.5 )
62 table.insert(self.dischargeInfo.nodes, {node=dischargeNode, width=width, length=length, priority=1})
63 end
64
65 -- place sound at the same position as the trigger or the discharge node
66 self.soundNode = createTransformGroup("loadTriggerSoundNode")
67 link(dischargeNode or self.triggerNode, self.soundNode)
68
69 if self.isClient then
70 self.effects = g_effectManager:loadEffect(xmlFile, xmlNode, self.rootNode, self)
71
72 self.samples = {}
73
74 local fillSoundIdentifier = XMLUtil.getValueFromXMLFileOrUserAttribute(xmlFile, xmlNode, "fillSoundIdentifier", getXMLString, self.rootNode)
75 local fillSoundNode = XMLUtil.getValueFromXMLFileOrUserAttribute(xmlFile, xmlNode, "fillSoundNode", getXMLString, self.rootNode)
76
77 if fillSoundNode == nil then
78 fillSoundNode = self.rootNode
79 end
80
81 local xmlSoundFile = loadXMLFile("mapXML", g_currentMission.missionInfo.mapSoundXmlFilename)
82 if xmlSoundFile ~= nil and xmlSoundFile ~= 0 then
83 local directory = g_currentMission.baseDirectory
84 local modName, baseDirectory = Utils.getModNameAndBaseDirectory(g_currentMission.missionInfo.mapSoundXmlFilename)
85 if modName ~= nil then
86 directory = baseDirectory .. modName
87 end
88 if fillSoundIdentifier ~= nil then
89 self.samples.load = g_soundManager:loadSampleFromXML(xmlSoundFile, "sound.object", fillSoundIdentifier, directory, getRootNode(), 0, AudioGroup.ENVIRONMENT, nil, nil)
90 if self.samples.load ~= nil then
91 link(fillSoundNode, self.samples.load.soundNode)
92 setTranslation(self.samples.load.soundNode, 0,0,0)
93 end
94 end
95 delete(xmlSoundFile)
96 end
97
98 local scrollerStr = XMLUtil.getValueFromXMLFileOrUserAttribute(xmlFile, xmlNode, "scrollerIndex", getXMLString, self.rootNode)
99 self.scroller = I3DUtil.indexToObject(self.rootNode, scrollerStr)
100 if self.scroller ~= nil then
101 local shaderParameterName = XMLUtil.getValueFromXMLFileOrUserAttribute(xmlFile, xmlNode, "shaderParameterName", getXMLString, self.rootNode)
102 local scrollerScrollSpeed = tonumber(XMLUtil.getValueFromXMLFileOrUserAttribute(xmlFile, xmlNode, "scrollerScrollSpeed", getXMLFloat, self.rootNode))
103
104 self.scrollerShaderParameterName = Utils.getNoNil(shaderParameterName, "uvScrollSpeed")
105 if scrollerScrollSpeed ~= nil then
106 self.scrollerSpeedX, self.scrollerSpeedY = StringUtil.getVectorFromString(scrollerScrollSpeed)
107 end
108 self.scrollerSpeedX = Utils.getNoNil(self.scrollerSpeedX, 0)
109 self.scrollerSpeedY = Utils.getNoNil(self.scrollerSpeedY, -0.75)
110 setShaderParameter(self.scroller, self.scrollerShaderParameterName, 0, 0, 0, 0, false)
111 end
112 end
113
114 self.fillTypes = {}
115 local fillTypeCategories = XMLUtil.getValueFromXMLFileOrUserAttribute(xmlFile, xmlNode, "fillTypeCategories", getXMLString, self.rootNode)
116 local fillTypeNames = XMLUtil.getValueFromXMLFileOrUserAttribute(xmlFile, xmlNode, "fillTypes", getXMLString, self.rootNode)
117
118 local fillTypes = nil
119 if fillTypeCategories ~= nil and fillTypeNames == nil then
120 fillTypes = g_fillTypeManager:getFillTypesByCategoryNames(fillTypeCategories, "Warning: UnloadTrigger has invalid fillTypeCategory '%s'.")
121 elseif fillTypeCategories == nil and fillTypeNames ~= nil then
122 fillTypes = g_fillTypeManager:getFillTypesByNames(fillTypeNames, "Warning: UnloadTrigger has invalid fillType '%s'.")
123 end
124 if fillTypes ~= nil then
125 for _,fillType in pairs(fillTypes) do
126 self.fillTypes[fillType] = true
127 end
128 else
129 self.fillTypes = nil
130 end
131
132 self.autoStart = Utils.getNoNil(XMLUtil.getValueFromXMLFileOrUserAttribute(xmlFile, xmlNode, "autoStart", getXMLBool, self.rootNode), false)
133 self.hasInfiniteCapacity = Utils.getNoNil(XMLUtil.getValueFromXMLFileOrUserAttribute(xmlFile, xmlNode, "infiniteCapacity", getXMLBool, self.rootNode), false)
134
135 self.startFillText = g_i18n:getText("action_siloStartFilling")
136 self.stopFillText = g_i18n:getText("action_siloStopFilling")
137 self.activateText = self.startFillText
138
139 self.isLoading = false
140 self.selectedFillType = FillType.UNKNOWN
141
142 return true
143end

loadTriggerCallback

Description
Definition
loadTriggerCallback()
Code
179function LoadTrigger:loadTriggerCallback(triggerId, otherId, onEnter, onLeave, onStay, otherShapeId)
180 local fillableObject = g_currentMission:getNodeObject(otherId)
181 if fillableObject ~= nil then
182 if fillableObject ~= self.source and fillableObject.getRootVehicle ~= nil and fillableObject.getFillUnitIndexFromNode ~= nil then
183 local fillTypes = self.source:getProvidedFillTypes()
184
185 if fillTypes ~= nil then
186 local foundFillUnitIndex = fillableObject:getFillUnitIndexFromNode(otherId)
187
188 if foundFillUnitIndex ~= nil then
189 local found = false
190 for fillTypeIndex, state in pairs(fillTypes) do
191 if state and (self.fillTypes == nil or self.fillTypes[fillTypeIndex]) then
192 if fillableObject:getFillUnitSupportsFillType(foundFillUnitIndex, fillTypeIndex) then
193 if fillableObject:getFillUnitAllowsFillType(foundFillUnitIndex, fillTypeIndex) then
194 found = true
195 break
196 end
197 end
198 end
199 end
200
201 -- ignore fillUnitIndex if does not support or allow one of the provided filltypes
202 if not found then
203 foundFillUnitIndex = nil
204 end
205 end
206
207 if foundFillUnitIndex == nil then
208 for fillTypeIndex, state in pairs(fillTypes) do
209 if state and (self.fillTypes == nil or self.fillTypes[fillTypeIndex]) then
210 local fillUnits = fillableObject:getFillUnits()
211 for fillUnitIndex, fillUnit in ipairs(fillUnits) do
212 -- only check fill units without exactFillRootNodes
213 if fillUnit.exactFillRootNode == nil then
214 if fillableObject:getFillUnitSupportsFillType(fillUnitIndex, fillTypeIndex) then
215 if fillableObject:getFillUnitAllowsFillType(fillUnitIndex, fillTypeIndex) then
216 foundFillUnitIndex = fillUnitIndex
217 break
218 end
219 end
220 end
221 end
222 end
223 end
224 end
225
226 if foundFillUnitIndex ~= nil then
227 if onEnter then
228 self.fillableObjects[otherId] = {object=fillableObject, fillUnitIndex=foundFillUnitIndex}
229 elseif onLeave then
230 self.fillableObjects[otherId] = nil
231 if self.isLoading and self.currentFillableObject == fillableObject then
232 self:setIsLoading(false)
233 end
234 end
235
236 if next(self.fillableObjects) ~= nil then
237 g_currentMission:addActivatableObject(self)
238 else
239 g_currentMission:removeActivatableObject(self)
240 end
241 end
242 end
243 end
244 end
245end

new

Description
Definition
new()
Code
18function LoadTrigger:new(isServer, isClient, customMt)
19 local self = Object:new(isServer, isClient, customMt or LoadTrigger_mt)
20
21 self.fillableObjects = {}
22
23 return self
24end

onActivateObject

Description
Definition
onActivateObject()
Code
319function LoadTrigger:onActivateObject()
320 if not self.isLoading then
321 local fillLevels, capacity = self.source:getAllFillLevels(g_currentMission:getFarmId())
322
323 local fillableObject = self.validFillableObject
324 local fillUnitIndex = self.validFillableFillUnitIndex
325
326 local firstFillType = nil
327 local validFillLevels = {}
328 for fillTypeIndex, fillLevel in pairs(fillLevels) do
329 if self.fillTypes == nil or self.fillTypes[fillTypeIndex] then
330 if fillableObject:getFillUnitAllowsFillType(fillUnitIndex, fillTypeIndex) then
331 validFillLevels[fillTypeIndex] = fillLevel
332
333 if firstFillType == nil then
334 firstFillType = fillTypeIndex
335 end
336 end
337 end
338 end
339
340 if not self.autoStart then
341 local text
342 if self.hasInfiniteCapacity then
343 text = string.format("%s", self.source.stationName)
344 else
345 text = string.format("%s (%s)", self.source.stationName, g_i18n:formatFluid(capacity))
346 end
347
348 g_gui:showSiloDialog({title=text, fillLevels=validFillLevels, capacity=capacity, callback=self.onFillTypeSelection, target=self, hasInfiniteCapacity = self.hasInfiniteCapacity})
349 else
350 self:onFillTypeSelection(firstFillType)
351 end
352 else
353 self:setIsLoading(false)
354 end
355 g_currentMission:addActivatableObject(self)
356end

onFillTypeSelection

Description
Definition
onFillTypeSelection()
Code
360function LoadTrigger:onFillTypeSelection(fillType)
361 if fillType ~= nil and fillType ~= FillType.UNKNOWN then
362 local validFillableObject = self.validFillableObject
363 if validFillableObject ~= nil and validFillableObject:getRootVehicle() == g_currentMission.controlledVehicle then
364 local fillUnitIndex = self.validFillableFillUnitIndex
365 self:setIsLoading(true, validFillableObject, fillUnitIndex, fillType)
366 end
367 end
368end

setFillSoundIsPlaying

Description
Definition
setFillSoundIsPlaying()
Code
470function LoadTrigger:setFillSoundIsPlaying(state)
471 if self.dischargeInfo == nil then
472 local target = self:getFillTargetNode()
473 if target ~= nil then
474 local x, y, z = getWorldTranslation(target)
475 setWorldTranslation(self.soundNode, x, y, z)
476 end
477 end
478
479 FillTrigger.setFillSoundIsPlaying(self, state)
480end

setIsLoading

Description
Definition
setIsLoading()
Code
372function LoadTrigger:setIsLoading(isLoading, targetObject, fillUnitIndex, fillType, noEventSend)
373 LoadTriggerSetIsLoadingEvent.sendEvent(self, isLoading, targetObject, fillUnitIndex, fillType, noEventSend)
374 if isLoading then
375 self:startLoading(fillType, targetObject, fillUnitIndex)
376 else
377 self:stopLoading()
378 end
379
380 self:setFillSoundIsPlaying(isLoading)
381
382 if self.currentFillableObject ~= nil then
383 if self.currentFillableObject.setFillSoundIsPlaying ~= nil then
384 self.currentFillableObject:setFillSoundIsPlaying(isLoading)
385 end
386 end
387end

setSource

Description
Definition
setSource()
Code
168function LoadTrigger:setSource(object)
169 assert(object.getProvidedFillTypes ~= nil)
170 assert(object.getAllFillLevels ~= nil)
171 assert(object.addFillLevelToFillableObject ~= nil)
172 assert(object.getIsFillAllowedToFarm ~= nil)
173
174 self.source = object
175end

startLoading

Description
Definition
startLoading()
Code
391function LoadTrigger:startLoading(fillType, fillableObject, fillUnitIndex)
392 if not self.isLoading then
393 self:raiseActive()
394 self.isLoading = true
395 self.selectedFillType = fillType
396 self.currentFillableObject = fillableObject
397 self.fillUnitIndex = fillUnitIndex
398 self.activateText = self.stopFillText
399
400 if self.isClient then
401 g_effectManager:setFillType(self.effects, self.selectedFillType)
402 g_effectManager:startEffects(self.effects)
403 g_soundManager:playSample(self.samples.load)
404
405 if self.scroller ~= nil then
406 setShaderParameter(self.scroller, self.scrollerShaderParameterName, self.scrollerSpeedX, self.scrollerSpeedY, 0, 0, false)
407 end
408 end
409 end
410end

stopLoading

Description
Definition
stopLoading()
Code
414function LoadTrigger:stopLoading()
415 if self.isLoading then
416 self:raiseActive()
417 self.isLoading = false
418 self.selectedFillType = FillType.UNKNOWN
419 self.activateText = self.startFillText
420
421 for i, fillableObject in pairs(self.fillableObjects) do
422 fillableObject.lastWasFilled = fillableObject.object == self.validFillableObject
423 end
424
425 if self.isClient then
426 g_effectManager:stopEffects(self.effects)
427 g_soundManager:stopSample(self.samples.load)
428
429 if self.scroller ~= nil then
430 setShaderParameter(self.scroller, self.scrollerShaderParameterName, 0, 0, 0, 0, false)
431 end
432 end
433 end
434end

update

Description
Definition
update()
Code
438function LoadTrigger:update(dt)
439 if self.isServer then
440 if self.isLoading then
441 if self.currentFillableObject ~= nil then
442 local fillDelta = self.source:addFillLevelToFillableObject(self.currentFillableObject, self.fillUnitIndex, self.selectedFillType, self.fillLitersPerMS*dt, self.dischargeInfo, ToolType.TRIGGER)
443 if fillDelta == nil or fillDelta < 0.001 then
444 self:setIsLoading(false)
445 end
446 elseif self.isLoading then
447 self:setIsLoading(false)
448 end
449 self:raiseActive()
450 end
451 end
452end