package.loaded.Constants = nil; require("Constants");

function BrainVsBrain:SceneTest()
--[[ THIS TEST IS DONE AUTOMATICALLY BY THE GAME NOW; IT SCANS THE SCRIPT FOR ANY MENTIONS OF "GetArea" AND TESTS THE SCENES FOR HAVING THOSE USED AREAS DEFINED!
	-- See if the required areas are present in the test scene
	if not (TestScene:HasArea("LZ Team 1") and TestScene:HasArea("LZ Team 2")) then
		-- If the test scene failed the compatibility test, invalidate it
		TestScene = nil;
	end
--]]
end

function BrainVsBrain:StartActivity()
	print("START! -- BrainVsBrain:StartActivity()!");
	for player = Activity.PLAYER_1, Activity.MAXPLAYERCOUNT - 1 do
		if self:PlayerActive(player) and self:PlayerHuman(player) then
		-- Check if we already have a brain assigned
			if not self:GetPlayerBrain(player) then
				local foundBrain = MovableMan:GetUnassignedBrain(self:GetTeamOfPlayer(player));
				-- If we can't find an unassigned brain in the scene to give each player, then force to go into editing mode to place one
				if not foundBrain then
					self.ActivityState = Activity.EDITING;
					AudioMan:ClearMusicQueue();
					AudioMan:PlayMusic("Base.rte/Music/dBSoundworks/ccambient4.ogg", -1, -1);
				else
					-- Set the found brain to be the selected actor at start
					self:SetPlayerBrain(foundBrain, player);
					self:SwitchToActor(foundBrain, player, self:GetTeamOfPlayer(player));
					self:SetLandingZone(self:GetPlayerBrain(player).Pos, player);
					-- Set the observation target to the brain, so that if/when it dies, the view flies to it in observation mode
					self:SetObservationTarget(self:GetPlayerBrain(player).Pos, player);
				end
			end
		end
	end
	self.LZs = { SceneMan.Scene:GetArea("LZ Team 1"), SceneMan.Scene:GetArea("LZ Team 2"), SceneMan.Scene:GetArea("LZ Team 3"), SceneMan.Scene:GetArea("LZ Team 4") };

	self.StartTimer = Timer();
end

function BrainVsBrain:PauseActivity(pause)
		print("PAUSE! -- BrainVsBrain:PauseActivity()!");
end

function BrainVsBrain:EndActivity()
		print("END! -- BrainVsBrain:EndActivity()!");
end

function BrainVsBrain:UpdateActivity()
	self:ClearObjectivePoints();

	if self.ActivityState ~= Activity.OVER and self.ActivityState ~= Activity.EDITING then
		local teamBrains = { 0, 0, 0, 0 };
		for player = Activity.PLAYER_1, Activity.MAXPLAYERCOUNT - 1 do
			if self:PlayerActive(player) and self:PlayerHuman(player) then
				--Display messages.
				if not self.StartTimer:IsPastSimMS(3000) then
					FrameMan:SetScreenText("Destroy the enemy brain(s)!", player, 333, 5000, true);
				end
			
				-- The current player's team
				local team = self:GetTeamOfPlayer(player);
				-- Check if any player's brain is dead
				if not MovableMan:IsActor(self:GetPlayerBrain(player)) then
					self:SetPlayerBrain(nil, player);
					self:ResetMessageTimer(player);
					FrameMan:ClearScreenText(player);
					FrameMan:SetScreenText("Your brain has been destroyed!", player, 333, -1, false);
				else
					teamBrains[team + 1] = teamBrains[team + 1] + 1;
				end
			end
		end
		
		--Display objectives over brains.
		for team = Activity.TEAM_1, Activity.MAXTEAMCOUNT - 1 do
			if self:TeamActive(team) then
				for player = Activity.PLAYER_1, Activity.MAXPLAYERCOUNT - 1 do
					if self:PlayerActive(player) then
						local brain = self:GetPlayerBrain(player);
						if brain ~= nil then
							if self:GetTeamOfPlayer(player) ~= team then
								self:AddObjectivePoint("Destroy!", brain.AboveHUDPos, team, GameActivity.ARROWDOWN);
							else
								self:AddObjectivePoint("Protect!", brain.AboveHUDPos, team, GameActivity.ARROWDOWN);
							end
						end
					end
				end
			end
		end
		
		--Determine if there is a winner.
		local winner = nil;
		for i = 1, #teamBrains do
			if teamBrains[i] ~= 0 then
				if winner == nil then
					winner = i - 1;
				else
					winner = nil;
					break;
				end
			end
		end
		
		if winner ~= nil then
			self.WinnerTeam = winner;
			for player = Activity.PLAYER_1, Activity.MAXPLAYERCOUNT - 1 do
				if self:PlayerActive(player) and self:PlayerHuman(player) and self:GetTeamOfPlayer(player) == winner then
					self:ResetMessageTimer(player);
					FrameMan:ClearScreenText(player);
					FrameMan:SetScreenText("Your team wins!", player, 333, -1, false);
				end
			end
			ActivityMan:EndActivity();
		end
	else
		self.StartTimer:Reset();
	end
	
	self:YSortObjectivePoints();
end