-- Star Wars Empire at War: Secrets of a Fallen Empire
-- Lua Script: //Scripts/GameObject/Despawn_Spectator_Upgrades_H_ShieldGen.lua
-- Author: Galyana
-- Updated: 6 February, 2026

-- Note: Script will check for multiple structures and abort if it finds any

require("PGStateMachine")

function Definitions()
	DebugMessage("%s -- In Definitions", tostring(Script))
	Define_State("State_Init", State_Init);
	ServiceRate = 1.0

	spectator_player = nil

	team_1_upgrades_list =
	{
		"Spectator_T1_HL_Enhanced_Base_Shield_Upgrade"
	}
	
	team_2_upgrades_list =
	{
		"Spectator_T2_HL_Enhanced_Base_Shield_Upgrade"
	}
end

function State_Init(message)
	if message == OnEnter then
		spectator_player = Find_First_Object("Spectator_Reveal_Marker_Land")

		if not spectator_player then
			ScriptExit()
		end 
		
		shield_list_1 = Find_All_Objects_Of_Type("H_Ground_Base_Shield_740")
		for i, building in pairs(shield_list_1) do
			if TestValid(building) and building.Get_Owner().Get_Team() == Object.Get_Owner().Get_Team() then
				Object.Despawn()
				ScriptExit()
			end
		end
		shield_list_2 = Find_All_Objects_Of_Type("H_Ground_Base_Shield_370")
		for i, building in pairs(shield_list_2) do
			if TestValid(building) and building.Get_Owner().Get_Team() == Object.Get_Owner().Get_Team() then
				Object.Despawn()
				ScriptExit()
			end
		end
		shield_list_3 = Find_All_Objects_Of_Type("H_Ground_Base_Shield_740_Pad")
		for i, building in pairs(shield_list_3) do
			if TestValid(building) and building.Get_Owner().Get_Team() == Object.Get_Owner().Get_Team() then
				Object.Despawn()
				ScriptExit()
			end
		end
		shield_list_4 = Find_All_Objects_Of_Type("H_Ground_Base_Shield_370_Pad")
		for i, building in pairs(shield_list_4) do
			if TestValid(building) and building.Get_Owner().Get_Team() == Object.Get_Owner().Get_Team() then
				Object.Despawn()
				ScriptExit()
			end
		end

		if Object.Get_Owner().Get_Team() == 0 then
			for i, upgrade_name in pairs(team_1_upgrades_list) do
				local upgrade = Find_First_Object(upgrade_name)
				if TestValid(upgrade) then
					upgrade.Despawn()
				end
			end 
		else 
			if Object.Get_Owner().Get_Team() == 1 then
				for i, upgrade_name in pairs(team_2_upgrades_list) do
					local upgrade = Find_First_Object(upgrade_name)
					if TestValid(upgrade) then
						upgrade.Despawn()
					end
				end
			end
		end 
	end
	Object.Despawn()
	ScriptExit()
end