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

{$EVENT evtMissionStart:OnMissionStart__Mission2}
{$EVENT evtTick:OnTick__Mission2}

var newDefencePositionsPlaced: boolean;

procedure OnMissionStart__Mission2;
var aiPlayer1RedZone: Rectangle;
var aiPlayer2RedZone: Rectangle;
var aiPlayer3RedZone: Rectangle;
var aiPlayer4RedZone: Rectangle;
var aiPlayer5RedZone: Rectangle;
begin
	Actions.Log('MISSION 2 BEGIN');
	Actions.LogLinesMaxCnt(10000);
	
	Actions.ShowMsg(0, '<$0>');
	Actions.HouseAllowAllyToSelectAll(1, true);
	newDefencePositionsPlaced := false;
	FillAlliancesPerPlayerIdMap();
		
	aiPlayer1RedZone.topLeftCorner.X := 73;
	aiPlayer1RedZone.topLeftCorner.Y := 74;
	aiPlayer1RedZone.bottomRightCorner.X := 96;
	aiPlayer1RedZone.bottomRightCorner.Y := 92;
	
	aiPlayer2RedZone.topLeftCorner.X := 123;
	aiPlayer2RedZone.topLeftCorner.Y := 87;
	aiPlayer2RedZone.bottomRightCorner.X := 136; 
	aiPlayer2RedZone.bottomRightCorner.Y := 95;
	
	
	aiPlayer3RedZone.topLeftCorner.X := 25;
	aiPlayer3RedZone.topLeftCorner.Y := 30;
	aiPlayer3RedZone.bottomRightCorner.X := 62; 
	aiPlayer3RedZone.bottomRightCorner.Y := 52;
	
	aiPlayer4RedZone.topLeftCorner.X := 92;
	aiPlayer4RedZone.topLeftCorner.Y := 1;
	aiPlayer4RedZone.bottomRightCorner.X := 112; 
	aiPlayer4RedZone.bottomRightCorner.Y := 15;
	
	aiPlayer5RedZone.topLeftCorner.X := 113;
	aiPlayer5RedZone.topLeftCorner.Y := 58;
	aiPlayer5RedZone.bottomRightCorner.X := 144; 
	aiPlayer5RedZone.bottomRightCorner.Y := 65;
	
	aiPlayersRedZonesArray := AddElementToRectangleArray(aiPlayersRedZonesArray, aiPlayer1RedZone);
	aiPlayersRedZonesArray := AddElementToRectangleArray(aiPlayersRedZonesArray, aiPlayer2RedZone);
	aiPlayersRedZonesArray := AddElementToRectangleArray(aiPlayersRedZonesArray, aiPlayer3RedZone);		
	aiPlayersRedZonesArray := AddElementToRectangleArray(aiPlayersRedZonesArray, aiPlayer4RedZone);		
	aiPlayersRedZonesArray := AddElementToRectangleArray(aiPlayersRedZonesArray, aiPlayer5RedZone);		
	
end;	

procedure OnTick__Mission2;
var defencePosition: TKMDefencePositionInfo;
begin
	if States.GameTime mod 100 = 0 then
		begin
			if(isPlayerDestroyed(3)) then 
				begin
					Actions.AIDefencePositionRemove(1,60,69);
					Actions.AIDefencePositionRemove(1,69,69);
					Actions.AIDefencePositionRemove(1,73,67);
					Actions.AIDefencePositionRemove(1,80,67);
					Actions.AIDefencePositionRemove(1,76,72);
					Actions.AIDefencePositionRemove(1,80,72);	
				
					Actions.PlanConnectRoad(1,71,66,70,51,false);
					Actions.PlanAddHouseEx(1,htStore,50,51);
					Actions.PlanAddHouseEx(1,htInn,55,51);
					Actions.PlanAddHouseEx(1,htSchool,60,51);
					Actions.PlanAddHouseEx(1,htIronSmithy,43,53);
					Actions.PlanAddHouseEx(1,htIronMine,44,50);
					Actions.PlanAddHouseEx(1,htWeaponSmithy,39,56);
					Actions.PlanAddHouseEx(1,htFishermans,26,53);
					Actions.PlanAddHouseEx(1,htArmorSmithy,31,40);
					Actions.PlanAddHouseEx(1,htArmorSmithy,44,57);
					Actions.PlanConnectRoad(1,44,58,42,57,false);
					Actions.PlanAddHouseEx(1,htCoalMine,35,35);
					Actions.PlanAddHouseEx(1,htIronMine,37,31);
					Actions.PlanConnectRoad(1,37,32,39,36,false);
					Actions.PlanAddHouseEx(1,htIronMine,36,47);
					Actions.PlanConnectRoad(1,36,48,30,48,false);
					Actions.PlanAddHouseEx(1,htIronMine,44,30);
					Actions.PlanAddHouseEx(1,htCoalMine,43,35);
					Actions.PlanAddHouseEx(1,htCoalMine,50,41);
					Actions.PlanAddHouseEx(1,htCoalMine,53,47);
					Actions.PlanAddHouseEx(1,htIronSmithy,57,41);
					Actions.PlanAddHouseEx(1,htIronMine,65,46);
					Actions.PlanAddHouseEx(1,htWatchTower,33,19);
					Actions.PlanAddHouseEx(1,htWatchTower,81,50);
					Actions.PlanAddHouseEx(1,htWatchTower,63,36);
					Actions.PlanAddHouseEx(1,htWoodcutters,30,56);
					Actions.PlanConnectRoad(1,30,57,31,54,false);
					Actions.PlanAddHouseEx(1,htWoodcutters,30,28);
					Actions.PlanConnectRoad(1,30,29,31,29,false);
					Actions.PlanAddHouseEx(1,htWeaponWorkshop,27,35);
					Actions.PlanAddHouseEx(1,htSawmill,32,53);
					Actions.PlanAddHouseEx(1,htWatchTower,33,19);
					Actions.PlanAddHouseEx(1,htWatchTower,33,19);
					Actions.PlanAddHouseEx(1,htWatchTower,33,19);
					Actions.PlanAddHouseEx(1,htWatchTower,33,19);
					Actions.PlanAddHouseEx(1,htWatchTower,33,19);
					Actions.PlanAddHouseEx(1,htFarm,75,68);
					Actions.PlanAddHouseEx(1,htFarm,78,73);				
					Actions.PlanAddHouseEx(1,htStables,81,69);
					Actions.PlanAddField(1,72,70);					
					Actions.PlanAddField(1,73,70);
					Actions.PlanAddField(1,74,70);
					Actions.PlanAddField(1,75,70);
					Actions.PlanAddField(1,76,70);
					Actions.PlanAddField(1,77,70);
					Actions.PlanAddField(1,73,71);
					Actions.PlanAddField(1,74,71);
					Actions.PlanAddField(1,75,71);
					Actions.PlanAddField(1,76,71);
					Actions.PlanAddField(1,74,72);
					Actions.PlanAddField(1,75,72);
					Actions.PlanAddField(1,76,72);
					Actions.PlanAddField(1,75,73);
					Actions.PlanAddField(1,76,73);
					Actions.PlanAddField(1,74,74);
					Actions.PlanAddField(1,75,74);
					Actions.PlanAddField(1,76,74);
					Actions.PlanAddField(1,77,74);
					Actions.PlanAddField(1,75,75);
					Actions.PlanAddField(1,76,75);
					Actions.PlanAddField(1,77,75);
					Actions.PlanAddField(1,78,75);
					Actions.PlanAddField(1,79,75);
					Actions.PlanAddField(1,80,75);
					Actions.PlanAddField(1,81,75);
					Actions.PlanAddField(1,82,75);
					Actions.PlanAddField(1,77,76);
					Actions.PlanAddField(1,78,76);
					Actions.PlanAddField(1,78,77);
					Actions.PlanConnectRoad(1,78,74,84,74,false);	
					
					if(not newDefencePositionsPlaced) then
						begin		
							defencePosition.X := 76;
							defencePosition.Y := 48;
							defencePosition.Radius := 20;
							defencePosition.Dir := dirNE;
							defencePosition.GroupType := gtRanged;
							defencePosition.PositionType := dtBackLine;
							Actions.AIDefencePositionAddEx(1,-1,defencePosition);
							
							defencePosition.X := 77;
							defencePosition.Y := 53;
							defencePosition.Radius := 20;
							defencePosition.Dir := dirN;
							defencePosition.GroupType := gtMelee;
							defencePosition.PositionType := dtBackLine;
							Actions.AIDefencePositionAddEx(1,-1,defencePosition);
							
							defencePosition.X := 60;
							defencePosition.Y := 35;
							defencePosition.Radius := 20;
							defencePosition.Dir := dirN;
							defencePosition.GroupType := gtMounted;
							defencePosition.PositionType := dtFrontLine;
							Actions.AIDefencePositionAddEx(1,-1,defencePosition);

							defencePosition.X := 30;
							defencePosition.Y := 21;
							defencePosition.Radius := 30;
							defencePosition.Dir := dirE;
							defencePosition.GroupType := gtRanged;
							defencePosition.PositionType := dtFrontLine;
							Actions.AIDefencePositionAddEx(1,4,defencePosition);
							
							defencePosition.X := 49;
							defencePosition.Y := 35;
							defencePosition.Radius := 20;
							defencePosition.Dir := dirNE;
							defencePosition.GroupType := gtRanged;
							defencePosition.PositionType := dtFrontLine;
							Actions.AIDefencePositionAddEx(1,-1,defencePosition);
							
							defencePosition.X := 25;
							defencePosition.Y := 28;
							defencePosition.Radius := 30;
							defencePosition.Dir := dirE;
							defencePosition.GroupType := gtAntiHorse;
							defencePosition.PositionType := dtFrontLine;
							Actions.AIDefencePositionAddEx(1,-1,defencePosition);
							newDefencePositionsPlaced := true;
						end;	
				end;
				Actions.HouseAllowAllyToSelectAll(1, true);
		end;
end;