{$I PTL03Attacks.script}
{$I PTLRebuild.script}
{$I PTLDefence.script}

{$EVENT evtMissionStart:OnMissionStart__Mission3}
{$EVENT evtTick:OnTick__Mission3}

var newDefencePositionsPlaced: boolean;
var timer: integer;

procedure OnMissionStart__Mission3;
var aiPlayer1RedZone: Rectangle;
var aiPlayer2RedZone: Rectangle;
var aiPlayer3RedZone: Rectangle;
var aiPlayer4RedZone: Rectangle;
var aiPlayer5RedZone: Rectangle;
begin
	Actions.Log('MISSION 3 BEGIN');
	Actions.LogLinesMaxCnt(10000);
	
	Actions.ShowMsg(0, '<$0>');
	Actions.HouseAllowAllyToSelectAll(1, true);
	newDefencePositionsPlaced := false;
	FillAlliancesPerPlayerIdMap();
		
	aiPlayer1RedZone.topLeftCorner.X := 34;
	aiPlayer1RedZone.topLeftCorner.Y := 105;
	aiPlayer1RedZone.bottomRightCorner.X := 189;
	aiPlayer1RedZone.bottomRightCorner.Y := 189;
	
	aiPlayer2RedZone.topLeftCorner.X := 63;
	aiPlayer2RedZone.topLeftCorner.Y := 2;
	aiPlayer2RedZone.bottomRightCorner.X := 106; 
	aiPlayer2RedZone.bottomRightCorner.Y := 26;
	
	
	aiPlayer3RedZone.topLeftCorner.X := 111;
	aiPlayer3RedZone.topLeftCorner.Y := 3;
	aiPlayer3RedZone.bottomRightCorner.X := 144; 
	aiPlayer3RedZone.bottomRightCorner.Y := 28;
	
	aiPlayer4RedZone.topLeftCorner.X := 163;
	aiPlayer4RedZone.topLeftCorner.Y := 2;
	aiPlayer4RedZone.bottomRightCorner.X := 190; 
	aiPlayer4RedZone.bottomRightCorner.Y := 28;
	
	aiPlayer5RedZone.topLeftCorner.X := 1;
	aiPlayer5RedZone.topLeftCorner.Y := 1;
	aiPlayer5RedZone.bottomRightCorner.X := 189; 
	aiPlayer5RedZone.bottomRightCorner.Y := 189;
	
	aiPlayersRedZonesArray := AddElementToRectangleArray(aiPlayersRedZonesArray, aiPlayer1RedZone);
	aiPlayersRedZonesArray := AddElementToRectangleArray(aiPlayersRedZonesArray, aiPlayer2RedZone);
	aiPlayersRedZonesArray := AddElementToRectangleArray(aiPlayersRedZonesArray, aiPlayer3RedZone);		
	aiPlayersRedZonesArray := AddElementToRectangleArray(aiPlayersRedZonesArray, aiPlayer4RedZone);		
	aiPlayersRedZonesArray := AddElementToRectangleArray(aiPlayersRedZonesArray, aiPlayer5RedZone);		
	
	timer := 54000;
end;	

procedure OnTick__Mission3;
var timerString: string;
var playersWinAfterTimerArray: array [0..0] of integer;
begin
	if States.GameTime mod 10 = 0 then
		begin
			Actions.HouseAllowAllyToSelectAll(1, true);
			if(timer > 0) then 
				begin
					timer := timer - 10;
				end
			else
				begin
					playersWinAfterTimerArray[0] := 0;
					Actions.PlayerWin(playersWinAfterTimerArray, true);
				end;
			timerString := Utils.TimeToString(timer);
			Actions.OverlayTextSet(-1, timerString);
			
			if((States.HouseAt(129,182) <> -1) and (States.HouseSchoolQueue(States.HouseAt(129,182),1) = -1)) then
				begin
					Actions.HouseSchoolQueueAddEx(States.HouseAt(129,182),utRecruit,1);
				end;
		end;
end;