28 | function 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 |
143 | end |
179 | function 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 |
245 | end |
319 | function 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) |
356 | end |