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

-- Note: Script assumes only 1 structure is ever present

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_RL_Heavy_Armor_L1_Upgrade",
		"Spectator_T1_RL_Heavy_Armor_L2_Upgrade",
		"Spectator_T1_RL_Heavy_Armor_L3_Upgrade",
		"Spectator_T1_RL_Improved_Treads_L1_Upgrade",
		"Spectator_T1_RL_Improved_Treads_L2_Upgrade",
		"Spectator_T1_RL_Improved_Tank_Damage_L1_Upgrade",
		"Spectator_T1_RL_Experimental_Shielding_L1_Upgrade",
		"Spectator_T1_RL_Upgraded_Scopes_L2_Upgrade"
	}
	
	team_2_upgrades_list =
	{
		"Spectator_T2_RL_Heavy_Armor_L1_Upgrade",
		"Spectator_T2_RL_Heavy_Armor_L2_Upgrade",
		"Spectator_T2_RL_Heavy_Armor_L3_Upgrade",
		"Spectator_T2_RL_Improved_Treads_L1_Upgrade",
		"Spectator_T2_RL_Improved_Treads_L2_Upgrade",
		"Spectator_T2_RL_Improved_Tank_Damage_L1_Upgrade",
		"Spectator_T2_RL_Experimental_Shielding_L1_Upgrade",
		"Spectator_T2_RL_Upgraded_Scopes_L2_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 

		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
end