--[[

Game:    Farming Simulator 19
Script:  ExtendedExhaust
Author:  ThundR
Created: 12/15/2020

original script by modelleicher
www.schwabenmodding.bplaced.net

Notes:
    - Adds exhaust features
    - Adds exhaust particles
        + Particle system emits if the engine load is above a certain threshold (psThreshold value)

--]]

local g_modName = g_currentModName
local g_specName = "spec_"..g_modName..".extendedExhaust"

ExtendedExhaust = {}

-- <<[Initialization]>> ---------------------------------------------------------------------------

-- <<prerequisitesPresent>>
function ExtendedExhaust.prerequisitesPresent(specializations)
    return SpecializationUtil.hasSpecialization(Motorized, specializations)
end

-- <<registerOverwrittenFunctions>>
function ExtendedExhaust.registerOverwrittenFunctions(vehicleType)
    SpecializationUtil.registerOverwrittenFunction(vehicleType, "loadExhaustEffects", ExtendedExhaust.loadExhaustEffects)
end

-- <<registerEventListeners>>
function ExtendedExhaust.registerEventListeners(vehicleType)
    SpecializationUtil.registerEventListener(vehicleType, "onStartMotor", ExtendedExhaust)
    SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", ExtendedExhaust)
    SpecializationUtil.registerEventListener(vehicleType, "onDelete", ExtendedExhaust)
end

-- <<[Event Functions]>> --------------------------------------------------------------------------

-- <<onUpdateTick>>
function ExtendedExhaust:onUpdateTick(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
    local spec = self[g_specName]
    local motorizedSpec = self.spec_motorized

    if self.isClient then
        if self:getIsMotorStarted() then
            if spec.particleSystems ~= nil then
                local motorLoad = motorizedSpec.actualLoadPercentage
                local showParticles = false

                if motorLoad >= spec.psThreshold or spec.psMotorStartTime > 0 then
                    spec.psMotorStartTime = math.max(0, spec.psMotorStartTime - dt)
                    showParticles = true
                end

                for _, ps in ipairs(spec.particleSystems) do
                    ParticleUtil.setEmittingState(ps, showParticles)
                end

                self:raiseActive()
            end
        end
    end
end

-- <<onStartMotor>>
function ExtendedExhaust:onStartMotor()
    local spec = self[g_specName]

    if self.isClient then
        if spec.particleSystems ~= nil then
            spec.psMotorStartTime = spec.psMotorStartDuration
        end
    end
end

-- <<onDelete>>
function ExtendedExhaust:onDelete()
    local spec = self[g_specName]
    if self.isClient then
        if spec.particleSystems ~= nil then
            ParticleUtil.deleteParticleSystems(spec.particleSystems)
        end
    end
end

-- <<[Specialization Overrides]>> -----------------------------------------------------------------

-- <<loadExhaustEffects>>
function ExtendedExhaust:loadExhaustEffects(superFunc, xmlFile)
    local spec = self[g_specName]
    superFunc(self, xmlFile)

    local extendedExhaustKey = "vehicle.motorized.extendedExhaust"

    spec.psThreshold = Utils.getNoNil(getXMLFloat(xmlFile, extendedExhaustKey.."#psThreshold"), 0.85)
    spec.psMotorStartDuration = Utils.getNoNil(getXMLFloat(xmlFile, extendedExhaustKey.."#motorStartDuration"), 0)
    spec.psMotorStartTime = 0
    spec.particleSystems = {}

    local idx = 0
    while true do
        local psKey = string.format("%s.particleSystems.particleSystem(%d)", extendedExhaustKey, idx)
        if not hasXMLProperty(xmlFile, psKey) then
            break
        end

        local ps = {}

        ParticleUtil.loadParticleSystem(xmlFile, ps, psKey, self.components, false, nil, self.baseDirectory)
        table.insert(spec.particleSystems, ps)

        idx = idx + 1
    end

    if #spec.particleSystems == 0 then
        spec.particleSystems = nil
    end
end
