Code: Select all
--[[
https://forum.giants-software.com/viewtopic.php?t=204793
Source of the original code: LoadTrigger.lua by Giants Software
]]
print("------------------------------------------------------------> START LOADING SCRIPT instantLoad.lua")
InstantLoad = {}
-- adding custom nodes for XML
function InstantLoad.registerXMLPaths(schema, basePath)
schema:register(XMLValueType.BOOL, basePath .. "#instaLoad", "Start loading instantly", false)
schema:register(XMLValueType.BOOL, basePath .. "#instaLoadPause", "Allow to pause loading instantly", true)
end
LoadTrigger.registerXMLPaths = Utils.appendedFunction(LoadTrigger.registerXMLPaths, InstantLoad.registerXMLPaths)
print("------------------------------------------------------------> append done: InstantLoad.registerXMLPaths")
-- customized setting for automaticFilling
function InstantLoad:load(superFunc, components, xmlFile, xmlNode, i3dMappings, rootNode)
self.rootNode = rootNode or xmlFile:getValue(xmlNode .. "#node", nil, components, i3dMappings)
if self.rootNode == nil then
Logging.xmlError(xmlFile, "Missing node '%s#node'", xmlNode)
return false
end
self.objectsInTriggers = {}
XMLUtil.checkDeprecatedXMLElements(xmlFile, xmlNode .. "#scrollerIndex", xmlNode .. "#scrollerNode")
XMLUtil.checkDeprecatedUserAttribute(self.rootNode, "triggerNode", xmlFile, xmlNode .. "#triggerNode")
XMLUtil.checkDeprecatedUserAttribute(self.rootNode, "triggerIndex", xmlFile, xmlNode .. "#triggerNode")
XMLUtil.checkDeprecatedUserAttribute(self.rootNode, "fillLitersPerSecond", xmlFile, xmlNode .. "#fillLitersPerSecond")
XMLUtil.checkDeprecatedUserAttribute(self.rootNode, "dischargeNode", xmlFile, xmlNode .. "#dischargeNode")
XMLUtil.checkDeprecatedUserAttribute(self.rootNode, "fillSoundIdentifier", xmlFile, xmlNode .. "#fillSoundIdentifier")
XMLUtil.checkDeprecatedUserAttribute(self.rootNode, "fillSoundNode", xmlFile, xmlNode .. "#fillSoundNode")
XMLUtil.checkDeprecatedUserAttribute(self.rootNode, "scrollerIndex", xmlFile, xmlNode .. "#scrollerNode")
XMLUtil.checkDeprecatedUserAttribute(self.rootNode, "shaderParameterName", xmlFile, xmlNode .. "#shaderParameterName")
XMLUtil.checkDeprecatedUserAttribute(self.rootNode, "scrollerScrollSpeed", xmlFile, xmlNode .. "#scrollerScrollSpeed")
XMLUtil.checkDeprecatedUserAttribute(self.rootNode, "fillTypeCategories", xmlFile, xmlNode .. "#fillTypeCategories")
XMLUtil.checkDeprecatedUserAttribute(self.rootNode, "fillTypes", xmlFile, xmlNode .. "#fillTypes")
XMLUtil.checkDeprecatedUserAttribute(self.rootNode, "autoStart", xmlFile, xmlNode .. "#autoStart")
XMLUtil.checkDeprecatedUserAttribute(self.rootNode, "infiniteCapacity", xmlFile, xmlNode .. "#infiniteCapacity")
-- >> check customized nodes
XMLUtil.checkDeprecatedUserAttribute(self.rootNode, "instaLoad", xmlFile, xmlNode .. "#instaLoad")
XMLUtil.checkDeprecatedUserAttribute(self.rootNode, "instaLoadPause", xmlFile, xmlNode .. "#instaLoadPause")
-- <<
local triggerNode = xmlFile:getValue(xmlNode .. "#triggerNode", nil, components, i3dMappings)
if triggerNode == nil then
Logging.xmlError(xmlFile, "Missing triggerNode defined in '%s'", xmlNode)
return false
end
self.triggerNode = triggerNode
addTrigger(triggerNode, "loadTriggerCallback", self)
g_currentMission:addNodeObject(triggerNode, self)
self.fillLitersPerMS = xmlFile:getValue(xmlNode .. "#fillLitersPerSecond", 1000) / 1000
self.aiNode = xmlFile:getValue(xmlNode .. "#aiNode", nil, components, i3dMappings)
self.supportsAILoading = self.aiNode ~= nil
local dischargeNode = xmlFile:getValue(xmlNode .. "#dischargeNode", nil, components, i3dMappings)
if dischargeNode ~= nil then
XMLUtil.checkDeprecatedUserAttribute(dischargeNode, "width", xmlFile, xmlNode .. "#dischargeWidth")
XMLUtil.checkDeprecatedUserAttribute(dischargeNode, "length", xmlFile, xmlNode .. "#dischargeLength")
self.dischargeInfo = {
name = "fillVolumeDischargeInfo",
nodes = {}
}
local width = xmlFile:getValue(xmlNode .. "#dischargeWidth", 0.5)
local length = xmlFile:getValue(xmlNode .. "#dischargeLength", 0.5)
table.insert(self.dischargeInfo.nodes, {
priority = 1,
node = dischargeNode,
width = width,
length = length
})
end
self.soundNode = createTransformGroup("loadTriggerSoundNode")
link(dischargeNode or self.triggerNode, self.soundNode)
if self.isClient then
self.effects = g_effectManager:loadEffect(xmlFile, xmlNode, components, self, i3dMappings)
self.samples = {}
local fillSoundIdentifier = xmlFile:getValue(xmlNode .. "#fillSoundIdentifier")
local fillSoundNode = xmlFile:getValue(xmlNode .. "#fillSoundNode", nil, components, i3dMappings)
if fillSoundNode == nil then
fillSoundNode = self.rootNode
end
local xmlSoundFile = loadXMLFile("mapXML", g_currentMission.missionInfo.mapSoundXmlFilename)
if xmlSoundFile ~= nil and xmlSoundFile ~= 0 then
local directory = g_currentMission.baseDirectory
local modName, baseDirectory = Utils.getModNameAndBaseDirectory(g_currentMission.missionInfo.mapSoundXmlFilename)
if modName ~= nil then
directory = baseDirectory .. modName
end
if fillSoundIdentifier ~= nil then
self.samples.load = g_soundManager:loadSampleFromXML(xmlSoundFile, "sound.object", fillSoundIdentifier, directory, getRootNode(), 0, AudioGroup.ENVIRONMENT, nil, nil)
if self.samples.load ~= nil then
link(fillSoundNode, self.samples.load.soundNode)
setTranslation(self.samples.load.soundNode, 0, 0, 0)
end
end
delete(xmlSoundFile)
end
self.scroller = xmlFile:getValue(xmlNode .. "#scrollerNode", nil, components, i3dMappings)
if self.scroller ~= nil then
self.scrollerShaderParameterName = xmlFile:getValue(xmlNode .. "#shaderParameterName", "uvScrollSpeed")
self.scrollerSpeedX, self.scrollerSpeedY = xmlFile:getValue(xmlNode .. "#scrollerScrollSpeed", "0 -0.75")
setShaderParameter(self.scroller, self.scrollerShaderParameterName, 0, 0, 0, 0, false)
end
end
self.fillTypes = {}
local fillTypeCategories = XMLUtil.getValueFromXMLFileOrUserAttribute(xmlFile, xmlNode, "fillTypeCategories", self.rootNode)
local fillTypeNames = XMLUtil.getValueFromXMLFileOrUserAttribute(xmlFile, xmlNode, "fillTypes", self.rootNode)
local fillTypes = nil
if fillTypeCategories ~= nil and fillTypeNames == nil then
fillTypes = g_fillTypeManager:getFillTypesByCategoryNames(fillTypeCategories, "Warning: UnloadTrigger has invalid fillTypeCategory '%s'.")
elseif fillTypeCategories == nil and fillTypeNames ~= nil then
fillTypes = g_fillTypeManager:getFillTypesByNames(fillTypeNames, "Warning: UnloadTrigger has invalid fillType '%s'.")
end
if fillTypes ~= nil then
for _, fillType in pairs(fillTypes) do
self.fillTypes[fillType] = true
end
else
self.fillTypes = nil
end
self.autoStart = xmlFile:getValue(xmlNode .. "#autoStart", false)
self.hasInfiniteCapacity = xmlFile:getValue(xmlNode .. "#infiniteCapacity", false)
self.startFillText = g_i18n:convertText(xmlFile:getValue(xmlNode .. "#startFillText", "$l10n_action_siloStartFilling"))
self.stopFillText = g_i18n:convertText(xmlFile:getValue(xmlNode .. "#stopFillText", "$l10n_action_siloStopFilling"))
self.activatable = LoadTriggerActivatable.new(self)
-- >> load customized nodes
self.instaLoad = xmlFile:getValue(xmlNode .. "#instaLoad", false)
self.instaLoadPause = xmlFile:getValue(xmlNode .. "#instaLoadPause", true)
-- <<
self.activatable:setText(self.startFillText)
self.isLoading = false
self.selectedFillType = FillType.UNKNOWN
-- >> rework parameters
if self.instaLoad then
print("----------> The instaLoad is strong with this one !")
print("----------> Pause allowed: " .. tostring(self.instaLoadPause) )
if not self.autoStart then
print("----------> autoStart: " .. tostring(self.autoStart) )
print("----------> instaLoad will be disabled !")
else
self.automaticFilling = true
end
else
self.automaticFilling = Platform.gameplay.automaticFilling
end
-- <<
self.requiresActiveVehicle = not self.automaticFilling
self.automaticFillingTimer = 0
return true
end
LoadTrigger.load = Utils.overwrittenFunction(LoadTrigger.load, InstantLoad.load)
print("------------------------------------------------------------> overwrite done: InstantLoad:load")
-- implement optional pause
function InstantLoad:loadTriggerCallback(superFunc, triggerId, otherId, onEnter, onLeave, onStay, otherShapeId)
local fillableObject = g_currentMission:getNodeObject(otherId)
if fillableObject ~= nil and fillableObject ~= self.source and fillableObject.getRootVehicle ~= nil and fillableObject.getFillUnitIndexFromNode ~= nil then
local fillTypes = self.source:getSupportedFillTypes()
if fillTypes ~= nil then
local foundFillUnitIndex = fillableObject:getFillUnitIndexFromNode(otherId)
if foundFillUnitIndex ~= nil then
local found = false
for fillTypeIndex, state in pairs(fillTypes) do
if state and (self.fillTypes == nil or self.fillTypes[fillTypeIndex]) and fillableObject:getFillUnitSupportsFillType(foundFillUnitIndex, fillTypeIndex) and fillableObject:getFillUnitAllowsFillType(foundFillUnitIndex, fillTypeIndex) then
found = true
break
end
end
if not found then
foundFillUnitIndex = nil
end
end
if foundFillUnitIndex == nil then
for fillTypeIndex, state in pairs(fillTypes) do
if state and (self.fillTypes == nil or self.fillTypes[fillTypeIndex]) then
local fillUnits = fillableObject:getFillUnits()
for fillUnitIndex, fillUnit in ipairs(fillUnits) do
if fillUnit.exactFillRootNode == nil and fillableObject:getFillUnitSupportsFillType(fillUnitIndex, fillTypeIndex) and fillableObject:getFillUnitAllowsFillType(fillUnitIndex, fillTypeIndex) then
foundFillUnitIndex = fillUnitIndex
break
end
end
end
end
end
if foundFillUnitIndex ~= nil then
if onEnter then
self.fillableObjects[otherId] = {
object = fillableObject,
fillUnitIndex = foundFillUnitIndex
}
fillableObject:addDeleteListener(self)
elseif onLeave then
self.fillableObjects[otherId] = nil
fillableObject:removeDeleteListener(self)
if self.isLoading and self.currentFillableObject == fillableObject then
self:setIsLoading(false)
end
if fillableObject == self.validFillableObject then
self.validFillableObject = nil
self.validFillableFillUnitIndex = nil
end
end
if self.automaticFilling then
if not self.isLoading and next(self.fillableObjects) ~= nil and self:getIsFillableObjectAvailable() then
self:toggleLoading()
-- >> allow pausing
if self.instaLoadPause then
g_currentMission.activatableObjectsSystem:addActivatable(self.activatable)
end
-- <<
end
elseif next(self.fillableObjects) ~= nil then
g_currentMission.activatableObjectsSystem:addActivatable(self.activatable)
else
g_currentMission.activatableObjectsSystem:removeActivatable(self.activatable)
end
end
end
end
end
LoadTrigger.loadTriggerCallback = Utils.overwrittenFunction(LoadTrigger.loadTriggerCallback, InstantLoad.loadTriggerCallback)
print("------------------------------------------------------------> overwrite done: InstantLoad:loadTriggerCallback")