local ScenarioUtils = import('/lua/sim/ScenarioUtilities.lua');
local ScenarioFramework = import('/lua/ScenarioFramework.lua');
local Utilities = import('/lua/utilities.lua');

##This works fairly simply and is easy to make work with any map.
##Create a series of markers on the map, named WAYPOINT_# from 0 to however many you want
##The defence object will spawn at WAYPOINT_0
##To make any waypoint the final waypoint, place a second marker on top of it named it GOAL

###For every waypoint, make a marker named GROUNDSPAWN.  The script keeps track of which
### waypoint the defense object is closest to and spawns ground units from this marker.
##For WAYPOINT_1 there must be a GROUNDSPAWN_1 and so forth for every waypoint.

###For every waypoint, make a marker named AIRSPAWN.  The script keeps track of which
### waypoint the defense object is closest to and spawns air units from this marker.
##For WAYPOINT_1 there must be a AIRSPAWN_1 and so forth for every waypoint.

##T1 units will spawn for Waypoints 0 to 5.  T2 for 5 to 9.  T3 for 10 and up.

#Boss Battles are battles with experimental enemies, specifically Ythothas.  Map maker must specify how many boss #battles he ##has in his map (0 or 50, however many.)  Each ##Boss encounter requires 2 makers, BOSS_BATTLE_1 is the 
#marker that the defense object will cross.  When the defense object is within 50 units, a warning will be given to 
#the team.  When the defense 
##object is within 10 units the Ythothas will spawn at the marker BOSSSPAWN_1 and the 
#players will be given another warning.  
##The script #will then monitor the distance until the next boss battle marker (BOSS_BATTLE_2). Thus boss battles must occur 
##in order, 1 before 2 before 3 before 4, etc etc.
##Note that the number of XPs that #spawn is dependent on the marker number.  1 will spawn for BOSS_BATTLE_1, 2 for 
##BOSS_BATTLE_2, etc etc.  

#Naval Battles can be created.  Place a 'WATERSPAWN' marker where naval units are to spawn.  The script will check 
##the current status of the Defense Object.  If it is under water (and AttackViaWater = 'true') then naval units will ##periodically spawn at the naval waypoint and attack,  ##'Periodically' being defined as the user-inputted WaveDelay * 5.
##Naval unit spawns will cease when the Defense Object leaves the water.  (Ground/air spawns will continue the whole time, 
## fyi)

###Lastly, playable areas are supported.  This allows certain parts of the map to be 'unlocked', like in campaign.
##The script assumes (and requires) that you specified an AREA_1.  If you want no expansion, make AREA_1 the whole map.
##The script also assumes that MAP_EXPANSION_1 will open up area 2 (because AREA_1 starts open) and 2 opens 3, 3 opens 4, etc etc.
##I recommend placing a map expansion point on top of a waypoint so you're 100% sure that it will be triggered.



local TotalWaypoints = 12;  #Map maker needs to input the total number of waypoints excluding WAYPOINT_0
local TotalBossBattles = 2; #Map maker needs to specify the total number of boss battles supported, put 0 for none
local AttackViaWater = 'true'; #Map maker needs to specify if he wants the defense object attacked while under water. 
##Put 'false' if not.
local TotalExpansionPoints = 2;  #The number of playable areas - 1.  This lest the script know when to check for
## map expansion.  If no map expansion points, put 0.
##after MAP_EXPANSION_1 has been activated.

local oldOnPopulate = OnPopulate

   function OnPopulate()
        #oldOnPopulate()
        ScenarioUtils.InitializeArmies()
	Survival_InitGame();
 	ScenarioFramework.SetPlayableArea('AREA_1', false);
   end

   local oldOnStart = OnStart
   function OnStart(self)
      oldOnStart(self)
   end

## broadcast a text message to players
----------------------------------------------------------------------------
BroadcastMSG = function(MSG, Fade, TextColor)
	PrintText(MSG, 20, TextColor, Fade, 'center');	
end


-- initializes the game settings
--------------------------------------------------------------------------
Survival_InitGame = function()

	LOG("----- Survival MOD: Configuring match settings...");
	-- check game configuration
	
		#Checking difficulty options

		#local SpawnMultiplier = ScenarioInfo.Options.opt_difficulty;
		#local WaveDelay = ScenarioInfo.Options.opt_wavetime;


	ScenarioInfo.Options.Victory = 'sandbox'; -- force sandbox in order to implement our own rules

	local Armies = ListArmies();
	Survival_PlayerCount = table.getn(Armies) - 2; -- save player count, subtracting the 2 AI "players"

	-- loop through armies
	for i, Army in ListArmies() do
		-- check if it's a human army
		if (Army == "ARMY_1" or Army == "ARMY_2" or Army == "ARMY_3" or Army == "ARMY_4" or Army == "ARMY_5" or Army == "ARMY_6" or Army == "ARMY_7" or Army == "ARMY_8") then 

			ScenarioFramework.AddRestriction(Army, categories.WALL); -- don't allow them to build walls
			#ScenarioFramework.AddRestriction(Army, categories.AIR); -- don't allow them to build air stuff

			-- loop through other armies to ally with other human armies
			for x, ArmyX in ListArmies() do
				-- if human army
				if (ArmyX == "ARMY_1" or ArmyX == "ARMY_2" or ArmyX == "ARMY_3" or ArmyX == "ARMY_4" or ArmyX == "ARMY_5" or ArmyX == "ARMY_6" or ArmyX == "ARMY_7" or ArmyX == "ARMY_8") then 
					SetAlliance(Army, ArmyX, 'Ally'); 
				end
			end			

			SetAlliance(Army, "ARMY_SURVIVAL_ALLY", 'Ally'); -- friendly AI team
			SetAlliance(Army, "ARMY_SURVIVAL_ENEMY", 'Enemy');  -- enemy AI team

			SetAlliedVictory(Army, true); -- can win together of course :)
		end
	end

	SetAlliance("ARMY_SURVIVAL_ALLY", "ARMY_SURVIVAL_ENEMY", 'Enemy'); -- the friendly and enemy AI teams should be enemies

	SetIgnoreArmyUnitCap('ARMY_SURVIVAL_ENEMY', true); -- remove unit cap from enemy AI team


		Survival_SpawnDef();
		ForkThread(Waypoint_Tracker);

		ForkThread(VictoryCheck);
		ForkThread(SpawnEnemies);
		ForkThread(EmergencyShield);  #currently broken
		#VictoryCheck(SpawnMultiplier, WaveDelay);
		#SpawnEnemies(SpawnMultiplier, WaveDelay);
		if TotalBossBattles > 0 then
			ForkThread(BossBattles);
		end
		if AttackViaWater == 'true' then
			ForkThread(WaterCheck);
		end
		if TotalExpansionPoints > 0 then
			ForkThread(ExpansionCheck);
		end			

end





-- keeps the truck moving
--------------------------------------------------------------------------

Waypoint_Tracker = function(UnitID, ArmyID, POS, OrderID)


	local WaypointNumber = GetClosestWaypoint();
	local TargetWaypoint = ScenarioUtils.MarkerToPosition("WAYPOINT_" .. (WaypointNumber + 1));
	
	if WaypointNumber == 0 then
		BroadcastMSG("Coalition HQ is on the move.  Defend it.  HQ out.", 5);
	end

	local PlatoonList = {};
	table.insert(PlatoonList, Survival_DefUnit); -- add unit to a platoon

	Truck_PlatoonOrder(ArmyID, PlatoonList, OrderID, WaypointNumber, TargetWaypoint); -- give the unit orders
	WaitSeconds(10)


end

#handles the emergency
---------------------------------------
EmergencyShield = function()

	local EmergencyShieldCount = ScenarioInfo.Options.EmergencyShield;

	if EmergencyShieldCount == nil then
		EmergencyShieldCount = 2;
	end

        #local bp = Survival_DefUnit:GetBlueprint().Defense.Shield
	local bp ={
            ImpactEffects = 'UEFShieldHit01',
            ImpactMesh = '/effects/entities/ShieldSection01/ShieldSection01_mesh',
            Mesh = '/effects/entities/Shield01/Shield01_mesh',
            MeshZ = '/effects/entities/Shield01/Shield01z_mesh',
            RegenAssistMult = 60,
            ShieldEnergyDrainRechargeTime = 5,
            ShieldMaxHealth = 500000,
            ShieldRechargeTime = 160,
            ShieldRegenRate = 64,
            ShieldRegenStartTime = 1,
            ShieldSize = 25,
            ShieldVerticalOffset = -7,
	   };


	while not Survival_DefUnit:IsDead() do

		if Survival_DefUnit:GetHealth() < (Survival_DefUnit:GetMaxHealth() * .2) then
			BroadcastMSG("Coalition HQ hull integrity CRITICAL!", 4);
			if EmergencyShieldCount > 0 then
				EmergencyShieldCount = EmergencyShieldCount - 1;
				BroadcastMSG("Emergency Shield activated.  HQ out.", 4);	
      				      ForkThread(function()
          				     WaitTicks(1)
           				     Survival_DefUnit:CreateShield(bp)
           			      end)
				WaitSeconds(30)
				BroadcastMSG("Emergency Shield dissipated.  Remaining Shield count:" .. EmergencyShieldCount, 4);
	   	                Survival_DefUnit:DestroyShield()
			end
	
		WaitSeconds(8)
		end
		WaitSeconds(2)
		#Survival_DefUnit:SetMaxHealth(25000);
	end

end

##issues specific orders to the truck, will que orders until no more waypoints are left
----------------------------------------------------------------------------------
Truck_PlatoonOrder = function(ArmyID, UnitList, OrderID, WaypointNumber, TargetWaypoint)

	if (UnitList == nil) then
		return;
	end


	#BroadcastMSG("The defense object is on the move!");

	while WaypointNumber < TotalWaypoints do

		WaypointNumber = WaypointNumber + 1;

		#local CurrentPosition = Survival_DefUnit:GetPosition()

		TargetWaypoint = ScenarioUtils.MarkerToPosition("WAYPOINT_" .. WaypointNumber);
	
		local aiBrain = GetArmyBrain("ARMY_SURVIVAL_ALLY"); #GetArmyBrain(ArmyID)	--"ARMY_SURVIVAL_ENEMY");
		local aiPlatoon = aiBrain:MakePlatoon('','');
		aiBrain:AssignUnitsToPlatoon(aiPlatoon, UnitList, 'Attack', 'None'); -- platoon, unit list, "mission" and 

		#local POS = ScenarioUtils.MarkerToPosition("WAYPOINT_1"); 
		aiPlatoon:MoveToLocation(TargetWaypoint, false);

		LOG("Truck has qued a move order to waypoint number " .. WaypointNumber);

		WaitSeconds(1)	
		
	
	end


end





#Controls the spawn of enemy XP units
---------------------------------------------------------------------
function BossBattles()
	
	local NextBossBattle = 1;
	local WarningIssued = 'false';

	while not Survival_DefUnit:IsDead() and NextBossBattle <= TotalBossBattles do


		local Distance = VDist3(Survival_DefUnit:GetPosition(),ScenarioUtils.MarkerToPosition("BOSS_BATTLE_" .. NextBossBattle));	
	
			if (Distance < 50) and (WarningIssued == 'false') then
				BroadcastMSG("Intel suggests an Enemy Experimental is approaching.  HQ out.", 5);
				WarningIssued = 'true';
			end
			if (Distance < 10) then
				BroadcastMSG(" Enemy Experimental sighted in the operational area.  Destroy it.", 5);
				Survival_SpawnUnit('xsl0401', 'ARMY_SURVIVAL_ENEMY', ScenarioUtils.MarkerToPosition("BOSSSPAWN_" .. NextBossBattle), 2, NextBossBattle, 'xsl0101');
				NextBossBattle = NextBossBattle + 1;
				WarningIssued = 'false';
			end
			
	WaitSeconds(10)

	end
end



#Controls map expansion
---------------------------------------------------------------------
function ExpansionCheck()

	local NextMapExpansion = 1;	

	while not Survival_DefUnit:IsDead() and NextMapExpansion <= TotalExpansionPoints do

		local Distance = VDist3(Survival_DefUnit:GetPosition(),ScenarioUtils.MarkerToPosition("MAP_EXPANSION_" .. NextMapExpansion));	
	
			if (Distance < 10) then
				BroadcastMSG("Waypoint reached.  Expanding the operational area.  HQ out.", 5);
 				ScenarioFramework.SetPlayableArea('AREA_' .. (NextMapExpansion + 1), false);
				NextMapExpansion = NextMapExpansion + 1;
				ForkThread(Waypoint_Tracker);
			end
			
	WaitSeconds(10)

	end
end



#See if the defense object is under water.  Attacks it with naval units if it is
---------------------------------------------------------------------
function WaterCheck()

	local SpawnMultiplier = ScenarioInfo.Options.SpawnMultiplier;
	local WaveDelay = ScenarioInfo.Options.WaveDelay;

	if (SpawnMultiplier == nil) then
		SpawnMultiplier = 1;
	end
	if (WaveDelay == nil) then
		WaveDelay = 10;
	end
	
	
	while not Survival_DefUnit:IsDead() do

			if (Survival_DefUnit:GetCurrentLayer() == 'Seabed') then
				BroadcastMSG("Enemy fleet sighted.  Defend the HQ.", 5);
				Survival_SpawnUnit('xss0203', 'ARMY_SURVIVAL_ENEMY', ScenarioUtils.MarkerToPosition("WATERSPAWN"), 1, (4 * SpawnMultiplier), 'xsl0203');
				Survival_SpawnUnit('xss0201', 'ARMY_SURVIVAL_ENEMY', ScenarioUtils.MarkerToPosition("WATERSPAWN"), 1, (2 * SpawnMultiplier), 'xss0202');
				Survival_SpawnUnit('xss0302', 'ARMY_SURVIVAL_ENEMY', ScenarioUtils.MarkerToPosition("WATERSPAWN"), 1, (1), 'xss0304');
				

			end

			
	WaitSeconds(WaveDelay * 5)

	end
end



------------
function GetMarker(MarkerName)
	return Scenario.MasterChain._MASTERCHAIN_.Markers[MarkerName];
end

---------------------------------------------------------------------
function VictoryCheck()
	
	while not Survival_DefUnit:IsDead() do

		local Distance = VDist3(Survival_DefUnit:GetPosition(),ScenarioUtils.MarkerToPosition("GOAL"));	
	
			if (Distance <= 10) then
			BroadcastMSG("Coalition HQ has reached it's destination.  We live to fight another day.  HQ out.", 4);
			Survival_GameState = 2;
				for i, army in ListArmies() do
					if (army == "ARMY_1" or army == "ARMY_2" or army == "ARMY_3" or army == "ARMY_4" or army == "ARMY_5" or army == "ARMY_6" or army == "ARMY_7" or army == "ARMY_8") then
					GetArmyBrain(army):OnVictory();
					end
				end
			GetArmyBrain("ARMY_SURVIVAL_ENEMY"):OnDefeat();
			end

	WaitSeconds(10)

	end
end

function GetMarker(MarkerName)
	return Scenario.MasterChain._MASTERCHAIN_.Markers[MarkerName];
end


---------------------------------------------------------------------
function SpawnEnemies()
	WaitSeconds(10)

	local SpawnMultiplier = ScenarioInfo.Options.SpawnMultiplier;
	local WaveDelay = ScenarioInfo.Options.WaveDelay;

	if (SpawnMultiplier == nil) then
		SpawnMultiplier = 1;
	end
	if (WaveDelay == nil) then
		WaveDelay = 10;
	end

	BroadcastMSG("The Seraphim are attacking us.  Destroy them.  HQ Out.", 5)
	
	while not Survival_DefUnit:IsDead() do

		local Distance = VDist3(Survival_DefUnit:GetPosition(),ScenarioUtils.MarkerToPosition("GOAL"));	
		local ClosestWaypoint = GetClosestWaypoint();
		LOG("Closest waypoint is waypoint " .. ClosestWaypoint);
		SpawnGroundWave(ClosestWaypoint, SpawnMultiplier)
		SpawnAirWave(ClosestWaypoint, SpawnMultiplier)
		#SpawnAirDrop(ClosestWaypoint, SpawnMultiplier)
		#SpawnArtilleryWave(ClosestWaypoint, SpawnMultiplier)		

		if ClosestWaypoint > 1 then
			SpawnGroundWave((ClosestWaypoint - 1), SpawnMultiplier)	
			SpawnAirWave((ClosestWaypoint - 1), SpawnMultiplier)	
		end	
		
		if ClosestWaypoint > 2 then
			SpawnGroundWave((ClosestWaypoint - 2), SpawnMultiplier)	
			SpawnAirWave((ClosestWaypoint - 2), SpawnMultiplier)	
		end

		WaitSeconds(WaveDelay)

	end


end



---------------------------------------------------------------------------------------------
SpawnGroundWave = function(ClosestWaypoint, SpawnMultiplier) -- This generates the units that will be in the wave then spawns them

	local UnitID = '0';
	local UnitID2 = '0';
	local Quantity = 0;

	if ClosestWaypoint < 5 then
	UnitID = 'xsl0201';
	Quantity = (((ClosestWaypoint + 1) * 2) * SpawnMultiplier);
	elseif ClosestWaypoint <10 then
	UnitID = 'xsl0202';
	Quantity = ((ClosestWaypoint) * SpawnMultiplier);
	elseif ClosestWaypoint >= 10 then
	UnitID = 'xsl0303';
	Quantity = (((ClosestWaypoint)/2) * SpawnMultiplier);
	end

	if ClosestWaypoint < 5 then
	UnitID2 = 'xsl0103';
	Quantity = (((ClosestWaypoint * 2) + 1) * SpawnMultiplier);
	elseif ClosestWaypoint <10 then
	UnitID2 = 'xsl0111';
	Quantity = ((ClosestWaypoint) * SpawnMultiplier);
	elseif ClosestWaypoint >= 10 then
	UnitID2 = 'xsl0304';
	Quantity = (((ClosestWaypoint)/2) * SpawnMultiplier);
	end

	local ArmyID = "ARMY_SURVIVAL_ENEMY";
	local POS = ScenarioUtils.MarkerToPosition("GROUNDSPAWN_" .. ClosestWaypoint);
	local OrderID = 2;

	Survival_SpawnUnit(UnitID, ArmyID, POS, OrderID, Quantity, UnitID2)


end


---------------------------------------------------------------------------------------------
SpawnAirWave = function(ClosestWaypoint, SpawnMultiplier) -- This generates the units that will be in the wave then spawns them

	local UnitID = '0';
	local UnitID2 = '0';
	local Quantity = 0;

	if ClosestWaypoint < 5 then
	UnitID = 'xsa0103';
	Quantity = ((ClosestWaypoint + 2) * SpawnMultiplier);
	elseif ClosestWaypoint <10 then
	UnitID = 'xsa0202';
	Quantity = (ClosestWaypoint * SpawnMultiplier);
	elseif ClosestWaypoint >= 10 then
	UnitID = 'xsa0303';
	Quantity = ((ClosestWaypoint)/2 * SpawnMultiplier);
	end


	if ClosestWaypoint < 5 then
	UnitID2 = 'xsa0102';
	Quantity = (((ClosestWaypoint + 1) * 2) * SpawnMultiplier);
	elseif ClosestWaypoint <10 then
	UnitID2 = 'xsa0203';
	Quantity = ((ClosestWaypoint + 1) * SpawnMultiplier);
	elseif ClosestWaypoint >= 10 then
	UnitID2 = 'xsa0304';
	Quantity = (((ClosestWaypoint + 1)/2) * SpawnMultiplier);
	end

	local ArmyID = "ARMY_SURVIVAL_ENEMY";
	local POS = ScenarioUtils.MarkerToPosition("AIRSPAWN_" .. ClosestWaypoint);
	local OrderID = 2;

	Survival_SpawnUnit(UnitID, ArmyID, POS, OrderID, Quantity, UnitID2)


end

---------------------------------------------------------------------------------------------
Survival_SpawnUnit = function(UnitID, ArmyID, POS, OrderID, Quantity, UnitID2) -- blueprint, army, position, order

--	LOG("----- Survival MOD: SPAWNUNIT: Start function...");
	local PlatoonList = {};
	local Counter = 0;

	while Counter < Quantity do
		local NewUnit = CreateUnitHPR(UnitID, ArmyID, POS[1], POS[2], POS[3], 0, Random(0,360),0);
		local NewUnit2 = CreateUnitHPR(UnitID2, ArmyID, POS[1], POS[2], POS[3], 0, Random(0,360),0);
		table.insert(PlatoonList, NewUnit); -- add unit to a platoon
		table.insert(PlatoonList, NewUnit2); -- add unit to a platoon
		Survival_PlatoonOrder(ArmyID, PlatoonList, OrderID); -- give the unit orders
		Counter = Counter + 1;
	end
end



--------------------------------------------------------------------------
Survival_PlatoonOrder = function(ArmyID, UnitList, OrderID)	

--	LOG("----- Survival MOD: PLATOON: Start function...");

	if (UnitList == nil) then
		return;
	end

	local aiBrain = GetArmyBrain(ArmyID); --"ARMY_SURVIVAL_ENEMY");
	local aiPlatoon = aiBrain:MakePlatoon('','');
	aiBrain:AssignUnitsToPlatoon(aiPlatoon, UnitList, 'Attack', 'None'); -- platoon, unit list, "mission" and formation

 	-- 1 center / 2 waypoint / 3 spawn
 

	if (OrderID == 2) then -- attack move

		-- attack move to random path
		local ClosestWaypoint = GetClosestWaypoint();
		POS = Survival_DefUnit:GetPosition();
		aiPlatoon:AggressiveMoveToLocation(POS);
	end

end




#Determine which waypoint the defense object is closest to so that way we can spawn enemies from corresponding spawn points
----------------------------
function GetClosestWaypoint()


	local WaypointCounter = 0;
	local ShortestDistance = 20000;
	local ShortestDistanceWaypoint = 100;
	local WaypointDistance = 100;

	while (WaypointCounter < (TotalWaypoints + 1)) do

		WaypointDistance = VDist3(Survival_DefUnit:GetPosition(),ScenarioUtils.MarkerToPosition("WAYPOINT_" .. WaypointCounter))
		if WaypointDistance < ShortestDistance then
			ShortestDistance = WaypointDistance;
			ShortestDistanceWaypoint = WaypointCounter;
		end

	WaypointCounter = WaypointCounter + 1;

	end

	return ShortestDistanceWaypoint;
end







-- spawns a defense object
--------------------------------------------------------------------------
Survival_SpawnDef = function()

	LOG("----- Survival MOD: Initializing defense object...");

	local POS = ScenarioUtils.MarkerToPosition("WAYPOINT_0");

	Survival_DefUnit = CreateUnitHPR('UEL0401', "ARMY_SURVIVAL_ALLY", POS[1], POS[2], POS[3], 0,0,0);  


	Survival_DefUnit:SetReclaimable(false);
	Survival_DefUnit:SetCapturable(false);

	Survival_DefUnit:SetSpeedMult(0.5);

	Survival_DefUnit:HideBone('Turret_Right01', true)
	Survival_DefUnit:HideBone('Turret_Right01_Barrel01', true)
	Survival_DefUnit:HideBone('Turret_Right01_Barrel02', true)
	Survival_DefUnit:HideBone('Turret_Right01_Barrel03', true)
	Survival_DefUnit:HideBone('Turret_Right01_Muzzle01', true)
	Survival_DefUnit:HideBone('Turret_Right01_Muzzle02', true)
	Survival_DefUnit:HideBone('Turret_Right01_Muzzle03', true)

	Survival_DefUnit:HideBone('Turret_Right02', true)
	Survival_DefUnit:HideBone('Turret_Right02_Barrel01', true)
	Survival_DefUnit:HideBone('Turret_Right02_Barrel02', true)
	Survival_DefUnit:HideBone('Turret_Right02_Barrel03', true)
	Survival_DefUnit:HideBone('Turret_Right02_Muzzle01', true)
	Survival_DefUnit:HideBone('Turret_Right02_Muzzle02', true)
	Survival_DefUnit:HideBone('Turret_Right02_Muzzle03', true)

	Survival_DefUnit:HideBone('Turret_Left01', true)
	Survival_DefUnit:HideBone('Turret_Left01_Barrel01', true)
	Survival_DefUnit:HideBone('Turret_Left01_Barrel02', true)
	Survival_DefUnit:HideBone('Turret_Left01_Barrel03', true)
	Survival_DefUnit:HideBone('Turret_Left01_Muzzle01', true)
	Survival_DefUnit:HideBone('Turret_Left01_Muzzle02', true)
	Survival_DefUnit:HideBone('Turret_Left01_Muzzle03', true)

	Survival_DefUnit:HideBone('Turret_Left02', true)
	Survival_DefUnit:HideBone('Turret_Left02_Barrel01', true)
	Survival_DefUnit:HideBone('Turret_Left02_Barrel02', true)
	Survival_DefUnit:HideBone('Turret_Left02_Barrel03', true)
	Survival_DefUnit:HideBone('Turret_Left02_Muzzle01', true)
	Survival_DefUnit:HideBone('Turret_Left02_Muzzle02', true)
	Survival_DefUnit:HideBone('Turret_Left02_Muzzle03', true)

	Survival_DefUnit:SetWeaponEnabledByLabel('LeftTurret01', false)
	Survival_DefUnit:SetWeaponEnabledByLabel('LeftTurret02', false)
	Survival_DefUnit:SetWeaponEnabledByLabel('RightTurret01', false)
	Survival_DefUnit:SetWeaponEnabledByLabel('RightTurret02', false)

	Survival_DefUnit:DestroyShield()
	Survival_DefUnit:SetCustomName("Coalition HQ");

	#Survival_DefUnit:SetRegenRate(45 - (Survival_PlayerCount * 5));
	#Survival_DefUnit:SetMaxHealth(25000);
	#Survival_DefUnit:SetHealth(nil, 25000);

	local Buff = import('/lua/sim/Buff.lua')
	
        if not Buffs['SeraphimACUDamageStabilization'] then
               BuffBlueprint {
                    Name = 'SeraphimACUDamageStabilization',
                    DisplayName = 'SeraphimACUDamageStabilization',
                    BuffType = 'ACUUPGRADEDMG',
                    Stacks = 'ALWAYS',
                    Duration = -1,
                    Affects = {
                        MaxHealth = {
                            Add = 7500,
                            Mult = 1.0,
                        },
                        Regen = {
                            Add = 40,
                            Mult = 1.0,
                        },
                         },
                } 	
	end
        Buff.ApplyBuff(Survival_DefUnit, 'SeraphimACUDamageStabilization')    
	
	Survival_DefUnit:SetIntelRadius('Vision', 50);

	-- when the def object dies
	Survival_DefUnit.OldOnKilled = Survival_DefUnit.OnKilled;
	 

	#Survival_DefUnit.OldOnDamage = Survival_DefUnit.OnDamage;

	#Survival_DefUnit.OnDamage = function(self, instigator)
	#	self.OldOnDamage
	
	#end

	Survival_DefUnit.OnKilled = function(self, instigator, type, overkillRatio)

		BroadcastMSG("Coalition HQ has been lost.  The war is over.", 8);
		self.OldOnKilled(self, instigator, type, overkillRatio);

		Survival_GameState = 3;

		for i, army in ListArmies() do

			if (army == "ARMY_1" or army == "ARMY_2" or army == "ARMY_3" or army == "ARMY_4" or army == "ARMY_5" or army == "ARMY_6" or army == "ARMY_7" or army == "ARMY_8") then
				--killAllCommanders(army);
				GetArmyBrain(army):OnDefeat();
			end
		end
	end

	#Survival_DefLastHP = Survival_DefUnit:GetHealth();


end