286 | function 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 |
296 | end |
328 | function 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 |
352 | end |
53 | function 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 |
119 | end |
205 | function 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 |
222 | end |
229 | function 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 |
246 | end |
145 | function 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 |
159 | end |
416 | function 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) |
431 | end |
165 | function 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 |
179 | end |