-----------------------------------------------------------------------------
--  File     : /mods/4DC_V0.8/hook/units/UEL0303/UEL0303_script.lua
--
--  Author(s): EbolaSoup, Resin Smoker, Optimus Prime, Vissroid 
--
--  Special Thanks to : Exavier_Macbeth & Mithy
--
--  Summary  : UEF Siege Assault Bot Script (With Napalm Launcher Enhancement)
--
--  Copyright © 2011 4DC_V0.8  All rights reserved.
-----------------------------------------------------------------------------

local TerranWeaponFile = import('/lua/terranweapons.lua')
local TWalkingLandUnit = import('/lua/terranunits.lua').TWalkingLandUnit
local TDFHeavyPlasmaCannonWeapon = TerranWeaponFile.TDFHeavyPlasmaCannonWeapon
local MissileWeapon = import('/lua/terranweapons.lua').TSAMLauncher
local Buff = import('/lua/sim/Buff.lua')

UEL0303 = Class(TWalkingLandUnit) {

    Weapons = {   
        HeavyTwinPlasma = Class(TDFHeavyPlasmaCannonWeapon) {
            DisabledFiringBones = {
                'Torso', 'ArmR_B02', 'Barrel_R', 'ArmR_B03', 'ArmR_B04',
                'ArmL_B02', 'Barrel_L', 'ArmL_B03', 'ArmL_B04',
            },
        },  
        
        Missile_Pod = Class(MissileWeapon) {
        
            OnCreate = function(self) 
                MissileWeapon.OnCreate(self)
                -- Initialization of the missile counter 
                self.MissileCounter = 1
                -- A value of "true" indicates an odd numbered missile being fired
                self.OddEven = true  
            end, 
            
            CreateProjectileAtMuzzle = function(self, muzzle)
                -- Hyjacking CreateProjectileAtMuzzle
                local proj = MissileWeapon.CreateProjectileAtMuzzle(self, muzzle)
                
                local data = {  
                    Launcher = self.unit,
                    Target = self.unit.MissilePod:GetCurrentTarget(),      
                    Radius = self.unit.MissilePod:GetBlueprint().MaxRadius,
                    Priorities = self.unit.MissilePod:GetBlueprint().TargetPriorities,
                    Lifetime = self.unit.MissilePod:GetBlueprint().ProjectileLifetime,
                }                     
                if not proj or proj:BeenDestroyed()then
                    return proj
                else
                    proj:PassData(data)
                end                  
                              
                -- Custom CreateProjectileAtMuzzle script --                
                if not self.unit:IsDead() and self.unit:GetWeaponByLabel('Missile_Pod'):GetCurrentTarget() then                                  
                    -- Get the current target of this weapon
                    local myTarget = self.unit:GetWeaponByLabel('Missile_Pod'):GetCurrentTarget()                
                    --- Get the x, y, z, location of the target
                    local pos = myTarget:GetPosition()                                                                                               
                    -- Determines the angle from center
                    local angle = (2*math.pi) / self.unit.NumMissiles                   
                    -- Stagger the projectile spread between Odd and Even projectiles
                    -- Odd spread farther out and Even move closer in
                    local spreadMulti = nil
                    if self.OddEven == true then
                        self.OddEven = false
                        spreadMulti = self.unit.MissleSpread * 1.0
                    else
                        self.OddEven = true
                        spreadMulti = self.unit.MissleSpread / 1.75                    
                    end                                        
                    -- Do the math
                    local xVec = pos[1] + math.sin(self.MissileCounter*angle) * spreadMulti
                    local zVec = pos[3] + math.cos(self.MissileCounter*angle) * spreadMulti                  
                    local gPos = GetSurfaceHeight( xVec, zVec )
                    local modpos = {xVec, gPos, zVec}                    
                    -- Pass the location information to the projectile 
                    proj:SetNewTargetGround(modpos)                                                                       
                    if self.MissileCounter >= self.unit.NumMissiles then
                        -- Resets the counter
                        self.MissileCounter = 1
                        self.OddEven = true  
                    else
                        -- Increment the counter
                        self.MissileCounter = self.MissileCounter + 1
                    end                        
                end                 
                -- End of custom CreateProjectileAtMuzzle script --                     
                return proj
            end,            
                                              
            OnLostTarget = function(self)
                MissileWeapon.OnLostTarget(self)
                self.MissileCounter = 1
                self.OddEven = true  
            end                                 
        },             
    },  
                       
    OnCreate = function(self)
        self.MissilePod = self:GetWeaponByLabel('Missile_Pod')
        -- Number of missiles in eash salvo 
        self.NumMissiles = self.MissilePod:GetBlueprint().ProjectilesPerOnFire 
        -- Adjusts the width of the missile dispersal
        self.MissleSpread = self.MissilePod:GetBlueprint().DamageRadius 
        -- Hide the launcher bones until it is needed
        self:HideBone('Missile_Launcher', true)
        -- Set the flag to false
        self.LauncherEnhancement = false
        -- Disable the launcher
        self:SetWeaponEnabledByLabel('Missile_Pod', false)
        self.MissilePod:ChangeMaxRadius(0) 
        self.MissilePod:ChangeMinRadius(0) 
        TWalkingLandUnit.OnCreate(self)        
    end,
    
    ShowBone = function(self, bone, affectChildren)
        -- Big thank you to Mithy for his assistance 
        TWalkingLandUnit.ShowBone(self, bone, affectChildren)
        if bone == 0 and not self.LauncherEnhancement then
            self:HideBone('Missile_Launcher', true)
        end
    end,     
    
    CreateEnhancement = function(self, enh)
        TWalkingLandUnit.CreateEnhancement(self, enh)
        local bp = self:GetBlueprint().Enhancements[enh]
        if not bp then return end
        if enh == 'NapalmMissile' then            
            if not Buffs['Titan_NapalmMissile'] then
                BuffBlueprint {
                    Name = 'Titan_NapalmMissile',
                    DisplayName = 'Titan_NapalmMissile',
                    BuffType = 'TITAN_HEALTH',
                    Stacks = 'REPLACE',
                    Duration = -1,
                    Affects = {
                        MaxHealth = {
                            Add = bp.NewHealth,
                            Mult = 1.0,
                        },
                        Regen = {
                            Add = bp.NewRegenRate,
                            Mult = 1.0,
                        },                                                                    
                    },
                }
            end            
            Buff.ApplyBuff(self, 'Titan_NapalmMissile') 
            self:SetSpeedMult(0.75)
            -- Unhide Range-Ring circles
            self.MissilePod:ChangeMaxRadius(40) 
            self.MissilePod:ChangeMinRadius(10) 
            -- Enable missile pod
            self:SetWeaponEnabledByLabel('Missile_Pod', true)
            -- Set Flag to true
            self.LauncherEnhancement = true
        elseif enh =='NapalmMissileRemove' then
            if Buff.HasBuff( self, 'Titan_NapalmMissile' ) then
                Buff.RemoveBuff( self, 'Titan_NapalmMissile' )
            end
            self:SetSpeedMult(1.0)
            -- Disable the launcher
            self:SetWeaponEnabledByLabel('Missile_Pod', false)
            self.MissilePod:ChangeMaxRadius(0) 
            self.MissilePod:ChangeMinRadius(0) 
            -- Set Flag to false 
            self.LauncherEnhancement = false                       
        end
    end,             
}
TypeClass = UEL0303