-----------------------------------------------------------------------------------------
-- Start Activity
-----------------------------------------------------------------------------------------

function DummyAssault:StartActivity()
    	print("START! -- DummyAssault:StartActivity()!");
    	self.AreaTimer = Timer();
    	self.StepTimer = Timer();
    	self.SpawnTimer = Timer();
    	self.AlarmTriggered = false;
	self.AlarmZone = SceneMan.Scene:GetArea("Dummy Base Alarm");
	self.FactoryZone = SceneMan.Scene:GetArea("Dummy Factory Invasion");
	self.SearchArea = SceneMan.Scene:GetArea("Search Area");
	self.braindead = {};
	self.CPUBrain = nil;
	self.WList = { "Grenade Launcher", "Blaster", "Nailgun", "Pistol", "Sniper Rifle", "Repeater", "Destroyer Cannon", "Annihiliator", "Nailer Cannon" };
	self.BList = { "Disruptor Grenade", "Impulse Grenade" };
    
    	--------------------------
    	-- Set up teams

    	-- Team 2 is always CPU
    	self.CPUTeam = Activity.TEAM_2;
    
    	--------------------------
    	-- Set up players

    for player = 0, self.PlayerCount - 1 do
        -- Check if we already have a brain assigned
        if not self:GetPlayerBrain(player) then
	    self.braindead[player] = false;
            local brain = CreateActor("Brain Case");
            brain.Pos = Vector(3348,1128-player*24);
	    brain.RotAngle = math.rad(270);
            -- Let the spawn into the world, passing ownership
            MovableMan:AddActor(brain);
            -- Set the found brain to be the selected actor at start
            self:SetPlayerBrain(brain, player);
            SceneMan:SetScroll(self:GetPlayerBrain(player).Pos, 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

    -- Set up AI modes for the Actors that have been added to the scene by the scene definition
    for actor in MovableMan.AddedActors do
        if actor.Team == self.CPUTeam then
            actor.AIMode = Actor.AIMODE_SENTRY;
	    if actor.PresetName == "Dummy Controller" then
		self.CPUBrain = actor;
	    end
        end
    end

    -- Set the team's funds to 1000 oz.
    self:SetTeamFunds(1000,Activity.TEAM_1);

    --------------------------
    -- Set up tutorial
    
    self.AreaTimer:Reset();
    self.StepTimer:Reset();
    self.SpawnTimer:Reset();
    
end

-----------------------------------------------------------------------------------------
-- Pause Activity
-----------------------------------------------------------------------------------------

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

-----------------------------------------------------------------------------------------
-- End Activity
-----------------------------------------------------------------------------------------

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

-----------------------------------------------------------------------------------------
-- Update Activity
-----------------------------------------------------------------------------------------

function DummyAssault:UpdateActivity()
    	-- Clear all objective markers, they get re-added each frame
    	self:ClearObjectivePoints();
	if not (self.ActivityState == Activity.OVER) then
    		--------------------------
    		-- Iterate through all human players
		for player = 0, self.PlayerCount - 1 do
        		-- The current player's team
        		local team = self:GetTeamOfPlayer(player);
        		-- Make sure the game is not already ending
            		-- Check if any player's brain is dead
            		local brain = self:GetPlayerBrain(player);
            		if not brain or not MovableMan:IsActor(brain) or not brain:HasObjectInGroup("Brains") then
                		self:SetPlayerBrain(nil, player);
                		-- Try to find a new unasigned brain this player can use instead, or if his old brain entered a craft
                		local newBrain = MovableMan:GetUnassignedBrain(team);
                		-- Found new brain actor, assign it and keep on truckin'
                		if newBrain and self.braindead[player] == false then
                	    		self:SetPlayerBrain(newBrain, player);
                	    		self:SwitchToActor(newBrain, player, team);
                		else
                    			FrameMan:SetScreenText("Your brain has been lost!", player, 333, -1, false);
					self.braindead[player] = true;
                    			-- Now see if all brains of self player's team are dead, and if so, end the game
                    			if not MovableMan:GetClosestBrainActor(team) then
                        			self.WinnerTeam = self:OtherTeam(team);
                        			ActivityMan:EndActivity();
                    			end
                		end
            		else
                		-- Update the observation target to the brain, so that if/when it dies, the view flies to it in observation mode
                		self:SetObservationTarget(brain.Pos, player)
        		end
		end
        -- Game over, show the appropriate messages until a certain time
        elseif not self.GameOverTimer:IsPastSimMS(self.GameOverPeriod) then
		for player = 0, self.PlayerCount - 1 do
        		local team = self:GetTeamOfPlayer(player);
        		-- TODO: make more appropriate messages here for run out of funds endings
            		if team == self.WinnerTeam then
                		FrameMan:SetScreenText("Congratulations, you've destroyed the dummy base!", player, 0, -1, false);
            		else
                		FrameMan:SetScreenText("Your brain has been lost!", player, 0, -1, false);
            		end
		end
	end

	if self.AlarmTriggered == true then
	    if self.SpawnTimer:IsPastSimMS(5000) then
		if MovableMan:GetMOIDCount() <= 240 then
			local actor = nil;
			local y = math.random();
			if y > 0.05 then
				actor = CreateAHuman("Dummy");
				actor.Team = 1;
				local z = math.random();
				if z < 0.5 then
					actor.AIMode = Actor.AIMODE_BRAINHUNT;
				elseif z < 0.75 then
					actor.AIMode = Actor.AIMODE_GOTO;
					actor:ClearAIWaypoints();
					actor:AddAISceneWaypoint(self.SearchArea:GetRandomPoint());
				else
					actor.AIMode = Actor.AIMODE_GOTO;
					actor:ClearAIWaypoints();
					actor:AddAIMOWaypoint(self.CPUBrain);
				end
				actor:AddInventoryItem(CreateHDFirearm(self.WList[math.random(#self.WList)],"Dummy.rte"));
				if math.random() > 0.1 then
					actor:AddInventoryItem(CreateTDExplosive(self.BList[math.random(#self.BList)],"Dummy.rte"));
				end
				actor:AddInventoryItem(CreateHDFirearm("Turbo Digger"));
			else
				actor = CreateACrab("Dreadnought");
				actor.Team = 1;
				actor.AIMode = Actor.AIMODE_BRAINHUNT;
			end
			actor.Pos = Vector(5,550);
			MovableMan:AddActor(actor);
		end
		self.SpawnTimer:Reset();
	    end
	else
		for actor in MovableMan.Actors do
		    if actor.Team == Activity.TEAM_1 and self.AlarmZone:IsInside(actor.Pos) then
			self.AlarmTriggered = true;
		    end
		end
	end

    	-- Mark the enemy brain.
	if MovableMan:IsActor(self.CPUBrain) then
	    self:AddObjectivePoint("Destroy!", self.CPUBrain.AboveHUDPos+Vector(0,-16), Activity.TEAM_1, GameActivity.ARROWDOWN);
	else
	    --If it doesn't exist, it's gone!  Player wins!
	    self.WinnerTeam = Activity.TEAM_1;
	    ActivityMan:EndActivity();
	end

      	 -- Sort the objective points
      	self:YSortObjectivePoints();
end