{$I PTLAttacks.script}

{$EVENT evtMissionStart:OnMissionStart__Mission8_Attacks}
{$EVENT evtTick:OnTick__Mission8_Attacks}
 
//Player 1 AI  
var aiPlayer1AttackGroupsArray: array of GroupData;
var aiPlayer1AttackHouseGroupArray: array of GroupHouseAttackData;
var aiPlayer1IdleGroupsArray: array of GroupIdleData;
var aiPlayer1IdleAttackGroupsArray: array of GroupData;
var aiPlayer1DefencePositionsToReAddArray: array of TKMDefencePositionInfo;
var aiPlayer1DefencePositionsUIDFirstAttackPhaseArray: array of integer;
var aiPlayer1NextAttackScheduledTime: integer;
var aiPlayer1AttackStartedTime: integer;
var aiPlayer1FirstAttackPhaseEnded: boolean;
var aiPlayer1AttackEnded: boolean;

//Player 2 AI  
var aiPlayer2AttackGroupsArray: array of GroupData;
var aiPlayer2AttackHouseGroupArray: array of GroupHouseAttackData;
var aiPlayer2IdleGroupsArray: array of GroupIdleData;
var aiPlayer2IdleAttackGroupsArray: array of GroupData;
var aiPlayer2DefencePositionsToReAddArray: array of TKMDefencePositionInfo;
var aiPlayer2DefencePositionsUIDFirstAttackPhaseArray: array of integer;
var aiPlayer2NextAttackScheduledTime: integer;
var aiPlayer2AttackStartedTime: integer;
var aiPlayer2FirstAttackPhaseEnded: boolean;
var aiPlayer2AttackEnded: boolean;

//Player 3 AI  
var aiPlayer3AttackGroupsArray: array of GroupData;
var aiPlayer3AttackHouseGroupArray: array of GroupHouseAttackData;
var aiPlayer3IdleGroupsArray: array of GroupIdleData;
var aiPlayer3IdleAttackGroupsArray: array of GroupData;
var aiPlayer3DefencePositionsToReAddArray: array of TKMDefencePositionInfo;
var aiPlayer3DefencePositionsUIDFirstAttackPhaseArray: array of integer;
var aiPlayer3NextAttackScheduledTime: integer;
var aiPlayer3AttackStartedTime: integer;
var aiPlayer3FirstAttackPhaseEnded: boolean;
var aiPlayer3AttackEnded: boolean;

//Player 4 AI  
var aiPlayer4AttackGroupsArray: array of GroupData;
var aiPlayer4AttackHouseGroupArray: array of GroupHouseAttackData;
var aiPlayer4IdleGroupsArray: array of GroupIdleData;
var aiPlayer4IdleAttackGroupsArray: array of GroupData;
var aiPlayer4DefencePositionsToReAddArray: array of TKMDefencePositionInfo;
var aiPlayer4DefencePositionsUIDFirstAttackPhaseArray: array of integer;
var aiPlayer4NextAttackScheduledTime: integer;
var aiPlayer4AttackStartedTime: integer;
var aiPlayer4FirstAttackPhaseEnded: boolean;
var aiPlayer4AttackEnded: boolean;

//Player 5 AI  
var aiPlayer5AttackGroupsArray: array of GroupData;
var aiPlayer5AttackHouseGroupArray: array of GroupHouseAttackData;
var aiPlayer5IdleGroupsArray: array of GroupIdleData;
var aiPlayer5IdleAttackGroupsArray: array of GroupData;
var aiPlayer5DefencePositionsToReAddArray: array of TKMDefencePositionInfo;
var aiPlayer5DefencePositionsUIDFirstAttackPhaseArray: array of integer;
var aiPlayer5NextAttackScheduledTime: integer;
var aiPlayer5AttackStartedTime: integer;
var aiPlayer5FirstAttackPhaseEnded: boolean;
var aiPlayer5AttackEnded: boolean;

//Player 6 AI  
var aiPlayer6AttackGroupsArray: array of GroupData;
var aiPlayer6AttackHouseGroupArray: array of GroupHouseAttackData;
var aiPlayer6IdleGroupsArray: array of GroupIdleData;
var aiPlayer6IdleAttackGroupsArray: array of GroupData;
var aiPlayer6DefencePositionsToReAddArray: array of TKMDefencePositionInfo;
var aiPlayer6DefencePositionsUIDFirstAttackPhaseArray: array of integer;
var aiPlayer6NextAttackScheduledTime: integer;
var aiPlayer6AttackStartedTime: integer;
var aiPlayer6FirstAttackPhaseEnded: boolean;
var aiPlayer6AttackEnded: boolean;

//Player 7 AI  
var aiPlayer7AttackGroupsArray: array of GroupData;
var aiPlayer7AttackHouseGroupArray: array of GroupHouseAttackData;
var aiPlayer7IdleGroupsArray: array of GroupIdleData;
var aiPlayer7IdleAttackGroupsArray: array of GroupData;
var aiPlayer7DefencePositionsToReAddArray: array of TKMDefencePositionInfo;
var aiPlayer7DefencePositionsUIDFirstAttackPhaseArray: array of integer;
var aiPlayer7NextAttackScheduledTime: integer;
var aiPlayer7AttackStartedTime: integer;
var aiPlayer7FirstAttackPhaseEnded: boolean;
var aiPlayer7AttackEnded: boolean;

procedure CreateRandomAttackAiPlayer1_FirstAttack__Mission8_Attacks;
	var maxGroupCounts: integer;
	var rangedGroupCount: integer;
	var mountedGroupCount: integer;
	var antiHorseGroupCount: integer;
	var meleeGroupCount: integer;
	var firstAttackPhaseDataArray: array [0..2] of FirstAttackPhaseData;
	var groupHouseAttackData: GroupHouseAttackData;
	var i: integer;
begin
	if(aiPlayer1NextAttackScheduledTime = -1) then 
		begin
			aiPlayer1NextAttackScheduledTime := States.GameTime + Utils.TimeToTick(0,Utils.RandomRangeI(10,15),0);
		end;
	if((aiPlayer1NextAttackScheduledTime <= States.GameTime) and (HasEnoughSoldiersForAttack(1,2,0,0,1))) then 
		begin
			aiPlayer1AttackStartedTime := States.GameTime;
			aiPlayer1AttackEnded := false;
			maxGroupCounts := Length(firstAttackPhaseDataArray);
			meleeGroupCount := 2;
			antiHorseGroupCount := 0;
			rangedGroupCount := 0;
			mountedGroupCount := 1;
			aiPlayer1AttackGroupsArray := AddGroupIdsToAttackArray(aiPlayer1AttackGroupsArray,1,meleeGroupCount,antiHorseGroupCount,rangedGroupCount,mountedGroupCount);
			aiPlayer1DefencePositionsToReAddArray := RemoveDefencePositionsUntilAttackIsOver(1, aiPlayer1AttackGroupsArray, aiPlayer1DefencePositionsToReAddArray);
           
			firstAttackPhaseDataArray[0].firstAttackPhasePosition.X := 89;
			firstAttackPhaseDataArray[0].firstAttackPhasePosition.Y := 110;
			firstAttackPhaseDataArray[0].firstAttackPhaseDirection := dirN;
			
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.X := 94;
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.Y := 110;
			firstAttackPhaseDataArray[1].firstAttackPhaseDirection := dirN;
			
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.X := 93;
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.Y := 115;
			firstAttackPhaseDataArray[2].firstAttackPhaseDirection := dirN;
			
			for i := 0 to Length(aiPlayer1DefencePositionsToReAddArray) - 1 do
				begin
					if(States.HouseAt(firstAttackPhaseDataArray[i].firstAttackPhasePosition.X, firstAttackPhaseDataArray[i].firstAttackPhasePosition.Y) <> -1) then
						begin
							groupHouseAttackData.groupId := aiPlayer1DefencePositionsToReAddArray[i].GroupID;
							groupHouseAttackData.houseId := States.HouseAt(firstAttackPhaseDataArray[i].firstAttackPhasePosition.X, firstAttackPhaseDataArray[i].firstAttackPhasePosition.Y);
							aiPlayer1AttackHouseGroupArray := AddElementToGroupHouseAttackDataArray(aiPlayer1AttackHouseGroupArray, groupHouseAttackData);
						end;
				end;
			aiPlayer1FirstAttackPhaseEnded := false;
			aiPlayer1DefencePositionsUIDFirstAttackPhaseArray  :=  PlaceDefencePositionsForFirstAttackPhase(1, aiPlayer1DefencePositionsToReAddArray, firstAttackPhaseDataArray,aiPlayer1DefencePositionsUIDFirstAttackPhaseArray);
			Actions.Log('attack player 1 group types:' + ' meleeGroupCount ' + IntToStr(meleeGroupCount) + ', antiHorseGroupCount ' + IntToStr(antiHorseGroupCount) + ', rangedGroupCount ' + IntToStr(rangedGroupCount) + ', mountedGroupCount ' + IntToStr(mountedGroupCount));	
		end;
end;

procedure CreateRandomAttackAiPlayer2__Mission8_Attacks;
	var maxGroupCounts: integer;
	var rangedGroupCount: integer;
	var mountedGroupCount: integer;
	var antiHorseGroupCount: integer;
	var meleeGroupCount: integer;
	var firstAttackPhaseDataArray: array [0..2] of FirstAttackPhaseData;
	var groupHouseAttackData: GroupHouseAttackData;
	var i: integer;
begin
	if(aiPlayer2NextAttackScheduledTime = -1) then 
		begin
			aiPlayer2NextAttackScheduledTime := States.GameTime + Utils.TimeToTick(0,Utils.RandomRangeI(10,15),0);
		end;
	if((aiPlayer2NextAttackScheduledTime <= States.GameTime) and (HasEnoughSoldiersForAttack(2,1,0,0,2))) then 
		begin
			aiPlayer2AttackStartedTime := States.GameTime;
			aiPlayer2AttackEnded := false;
			maxGroupCounts := Length(firstAttackPhaseDataArray);
			meleeGroupCount := 1;
			antiHorseGroupCount := 0;
			rangedGroupCount := 0;
			mountedGroupCount := 2;
			aiPlayer2AttackGroupsArray := AddGroupIdsToAttackArray(aiPlayer2AttackGroupsArray,2,meleeGroupCount,antiHorseGroupCount,rangedGroupCount,mountedGroupCount);
			aiPlayer2DefencePositionsToReAddArray := RemoveDefencePositionsUntilAttackIsOver(2, aiPlayer2AttackGroupsArray, aiPlayer2DefencePositionsToReAddArray);

			firstAttackPhaseDataArray[0].firstAttackPhasePosition.X := 134;
			firstAttackPhaseDataArray[0].firstAttackPhasePosition.Y := 120;
			firstAttackPhaseDataArray[0].firstAttackPhaseDirection := dirN;
			
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.X := 139;
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.Y := 120;
			firstAttackPhaseDataArray[1].firstAttackPhaseDirection := dirN;
			
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.X := 136;
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.Y := 123;
			firstAttackPhaseDataArray[2].firstAttackPhaseDirection := dirN;

			for i := 0 to Length(aiPlayer2DefencePositionsToReAddArray) - 1 do
				begin
					if(States.HouseAt(firstAttackPhaseDataArray[i].firstAttackPhasePosition.X, firstAttackPhaseDataArray[i].firstAttackPhasePosition.Y) <> -1) then
						begin
							groupHouseAttackData.groupId := aiPlayer2DefencePositionsToReAddArray[i].GroupID;
							groupHouseAttackData.houseId := States.HouseAt(firstAttackPhaseDataArray[i].firstAttackPhasePosition.X, firstAttackPhaseDataArray[i].firstAttackPhasePosition.Y);
							aiPlayer2AttackHouseGroupArray := AddElementToGroupHouseAttackDataArray(aiPlayer2AttackHouseGroupArray, groupHouseAttackData);
						end;
				end;
			aiPlayer2FirstAttackPhaseEnded := false;
			aiPlayer2DefencePositionsUIDFirstAttackPhaseArray  :=  PlaceDefencePositionsForFirstAttackPhase(2, aiPlayer2DefencePositionsToReAddArray, firstAttackPhaseDataArray,aiPlayer2DefencePositionsUIDFirstAttackPhaseArray);
			Actions.Log('attack player 2 group types:' + ' meleeGroupCount ' + IntToStr(meleeGroupCount) + ', antiHorseGroupCount ' + IntToStr(antiHorseGroupCount) + ', rangedGroupCount ' + IntToStr(rangedGroupCount) + ', mountedGroupCount ' + IntToStr(mountedGroupCount));	
		end;
end;

procedure CreateRandomAttackAiPlayer3__Mission8_Attacks;
	var maxGroupCounts: integer;
	var rangedGroupCount: integer;
	var mountedGroupCount: integer;
	var antiHorseGroupCount: integer;
	var meleeGroupCount: integer;
	var firstAttackPhaseDataArray: array [0..5] of FirstAttackPhaseData;
	var groupHouseAttackData: GroupHouseAttackData;
	var i: integer;
begin
	if(aiPlayer3NextAttackScheduledTime = -1) then 
		begin
			aiPlayer3NextAttackScheduledTime := States.GameTime + Utils.TimeToTick(0,Utils.RandomRangeI(10,15),0);
		end;
	if((aiPlayer3NextAttackScheduledTime <= States.GameTime) and (HasEnoughSoldiersForAttack(3,1,1,3,1))) then 
		begin
			aiPlayer3AttackStartedTime := States.GameTime;
			aiPlayer3AttackEnded := false;
			maxGroupCounts := Length(firstAttackPhaseDataArray);
			meleeGroupCount := 1;
			antiHorseGroupCount := 1;
			rangedGroupCount := 3;
			mountedGroupCount := 1;
			aiPlayer3AttackGroupsArray := AddGroupIdsToAttackArray(aiPlayer3AttackGroupsArray,3,meleeGroupCount,antiHorseGroupCount,rangedGroupCount,mountedGroupCount);
			aiPlayer3DefencePositionsToReAddArray := RemoveDefencePositionsUntilAttackIsOver(3, aiPlayer3AttackGroupsArray, aiPlayer3DefencePositionsToReAddArray);

			firstAttackPhaseDataArray[0].firstAttackPhasePosition.X := 13;
			firstAttackPhaseDataArray[0].firstAttackPhasePosition.Y := 124;
			firstAttackPhaseDataArray[0].firstAttackPhaseDirection := dirE;
			
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.X := 13;
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.Y := 130;
			firstAttackPhaseDataArray[1].firstAttackPhaseDirection := dirE;
			
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.X := 13;
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.Y := 136;
			firstAttackPhaseDataArray[2].firstAttackPhaseDirection := dirE;
			
			firstAttackPhaseDataArray[3].firstAttackPhasePosition.X := 6;
			firstAttackPhaseDataArray[3].firstAttackPhasePosition.Y := 124;
			firstAttackPhaseDataArray[3].firstAttackPhaseDirection := dirE;
			
			firstAttackPhaseDataArray[4].firstAttackPhasePosition.X := 6;
			firstAttackPhaseDataArray[4].firstAttackPhasePosition.Y := 130;
			firstAttackPhaseDataArray[4].firstAttackPhaseDirection := dirE;
			
			firstAttackPhaseDataArray[5].firstAttackPhasePosition.X := 6;
			firstAttackPhaseDataArray[5].firstAttackPhasePosition.Y := 136;
			firstAttackPhaseDataArray[5].firstAttackPhaseDirection := dirE;

			for i := 0 to Length(aiPlayer3DefencePositionsToReAddArray) - 1 do
				begin
					if(States.HouseAt(firstAttackPhaseDataArray[i].firstAttackPhasePosition.X, firstAttackPhaseDataArray[i].firstAttackPhasePosition.Y) <> -1) then
						begin
							groupHouseAttackData.groupId := aiPlayer3DefencePositionsToReAddArray[i].GroupID;
							groupHouseAttackData.houseId := States.HouseAt(firstAttackPhaseDataArray[i].firstAttackPhasePosition.X, firstAttackPhaseDataArray[i].firstAttackPhasePosition.Y);
							aiPlayer3AttackHouseGroupArray := AddElementToGroupHouseAttackDataArray(aiPlayer3AttackHouseGroupArray, groupHouseAttackData);
						end;
				end;
			aiPlayer3FirstAttackPhaseEnded := false;
			aiPlayer3DefencePositionsUIDFirstAttackPhaseArray  :=  PlaceDefencePositionsForFirstAttackPhase(3, aiPlayer3DefencePositionsToReAddArray, firstAttackPhaseDataArray, aiPlayer3DefencePositionsUIDFirstAttackPhaseArray);
			Actions.Log('attack player 3 group types:' + ' meleeGroupCount ' + IntToStr(meleeGroupCount) + ', antiHorseGroupCount ' + IntToStr(antiHorseGroupCount) + ', rangedGroupCount ' + IntToStr(rangedGroupCount) + ', mountedGroupCount ' + IntToStr(mountedGroupCount));	
		end;
end;

procedure CreateRandomAttackAiPlayer4__Mission8_Attacks;
	var maxGroupCounts: integer;
	var rangedGroupCount: integer;
	var mountedGroupCount: integer;
	var antiHorseGroupCount: integer;
	var meleeGroupCount: integer;
	var firstAttackPhaseDataArray: array [0..2] of FirstAttackPhaseData;
	var groupHouseAttackData: GroupHouseAttackData;
	var i: integer;
begin
	if(aiPlayer4NextAttackScheduledTime = -1) then 
		begin
			aiPlayer4NextAttackScheduledTime := States.GameTime + Utils.TimeToTick(0,Utils.RandomRangeI(10,15),0);
		end;
	if((aiPlayer4NextAttackScheduledTime <= States.GameTime) and (HasEnoughSoldiersForAttack(4,1,0,0,2))) then 
		begin
			aiPlayer4AttackStartedTime := States.GameTime;
			aiPlayer4AttackEnded := false;
			maxGroupCounts := Length(firstAttackPhaseDataArray);
			meleeGroupCount := 1;
			antiHorseGroupCount := 0;
			rangedGroupCount := 0;
			mountedGroupCount := 2;
			aiPlayer4AttackGroupsArray := AddGroupIdsToAttackArray(aiPlayer4AttackGroupsArray,4,meleeGroupCount,antiHorseGroupCount,rangedGroupCount,mountedGroupCount);
			aiPlayer4DefencePositionsToReAddArray := RemoveDefencePositionsUntilAttackIsOver(4, aiPlayer4AttackGroupsArray, aiPlayer4DefencePositionsToReAddArray);

			firstAttackPhaseDataArray[0].firstAttackPhasePosition.X := 170;
			firstAttackPhaseDataArray[0].firstAttackPhasePosition.Y := 112;
			firstAttackPhaseDataArray[0].firstAttackPhaseDirection := dirSW;
			
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.X := 172;
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.Y := 125;
			firstAttackPhaseDataArray[1].firstAttackPhaseDirection := dirW;
			
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.X := 172;
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.Y := 131;
			firstAttackPhaseDataArray[2].firstAttackPhaseDirection := dirW;

			for i := 0 to Length(aiPlayer4DefencePositionsToReAddArray) - 1 do
				begin
					if(States.HouseAt(firstAttackPhaseDataArray[i].firstAttackPhasePosition.X, firstAttackPhaseDataArray[i].firstAttackPhasePosition.Y) <> -1) then
						begin
							groupHouseAttackData.groupId := aiPlayer4DefencePositionsToReAddArray[i].GroupID;
							groupHouseAttackData.houseId := States.HouseAt(firstAttackPhaseDataArray[i].firstAttackPhasePosition.X, firstAttackPhaseDataArray[i].firstAttackPhasePosition.Y);
							aiPlayer4AttackHouseGroupArray := AddElementToGroupHouseAttackDataArray(aiPlayer4AttackHouseGroupArray, groupHouseAttackData);
						end;
				end;
			aiPlayer4FirstAttackPhaseEnded := false;
			aiPlayer4DefencePositionsUIDFirstAttackPhaseArray  :=  PlaceDefencePositionsForFirstAttackPhase(4, aiPlayer4DefencePositionsToReAddArray, firstAttackPhaseDataArray, aiPlayer4DefencePositionsUIDFirstAttackPhaseArray);
			Actions.Log('attack player 4 group types:' + ' meleeGroupCount ' + IntToStr(meleeGroupCount) + ', antiHorseGroupCount ' + IntToStr(antiHorseGroupCount) + ', rangedGroupCount ' + IntToStr(rangedGroupCount) + ', mountedGroupCount ' + IntToStr(mountedGroupCount));	
		end;
end;

procedure CreateRandomAttackAiPlayer5__Mission8_Attacks;
	var maxGroupCounts: integer;
	var rangedGroupCount: integer;
	var mountedGroupCount: integer;
	var antiHorseGroupCount: integer;
	var meleeGroupCount: integer;
	var firstAttackPhaseDataArray: array [0..3] of FirstAttackPhaseData;
	var groupHouseAttackData: GroupHouseAttackData;
	var i: integer;
begin
	if(aiPlayer5NextAttackScheduledTime = -1) then 
		begin
			aiPlayer5NextAttackScheduledTime := States.GameTime + Utils.TimeToTick(0,Utils.RandomRangeI(15,20),0);
		end;
	if((aiPlayer5NextAttackScheduledTime <= States.GameTime) and (HasEnoughSoldiersForAttack(5,1,0,1,2))) then 
		begin
			aiPlayer5AttackStartedTime := States.GameTime;
			aiPlayer5AttackEnded := false;
			maxGroupCounts := Length(firstAttackPhaseDataArray);
			meleeGroupCount := 1;
			antiHorseGroupCount := 0;
			rangedGroupCount := 1;
			mountedGroupCount := 2;
			aiPlayer5AttackGroupsArray := AddGroupIdsToAttackArray(aiPlayer5AttackGroupsArray,5,meleeGroupCount,antiHorseGroupCount,rangedGroupCount,mountedGroupCount);
			aiPlayer5DefencePositionsToReAddArray := RemoveDefencePositionsUntilAttackIsOver(5, aiPlayer5AttackGroupsArray, aiPlayer5DefencePositionsToReAddArray);

			firstAttackPhaseDataArray[0].firstAttackPhasePosition.X := 83;
			firstAttackPhaseDataArray[0].firstAttackPhasePosition.Y := 90;
			firstAttackPhaseDataArray[0].firstAttackPhaseDirection := dirS;
			
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.X := 88;
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.Y := 90;
			firstAttackPhaseDataArray[1].firstAttackPhaseDirection := dirS;
			
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.X := 83;
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.Y := 95;
			firstAttackPhaseDataArray[2].firstAttackPhaseDirection := dirS;
			
			firstAttackPhaseDataArray[3].firstAttackPhasePosition.X := 88;
			firstAttackPhaseDataArray[3].firstAttackPhasePosition.Y := 95;
			firstAttackPhaseDataArray[3].firstAttackPhaseDirection := dirS;

			for i := 0 to Length(aiPlayer5DefencePositionsToReAddArray) - 1 do
				begin
					if(States.HouseAt(firstAttackPhaseDataArray[i].firstAttackPhasePosition.X, firstAttackPhaseDataArray[i].firstAttackPhasePosition.Y) <> -1) then
						begin
							groupHouseAttackData.groupId := aiPlayer5DefencePositionsToReAddArray[i].GroupID;
							groupHouseAttackData.houseId := States.HouseAt(firstAttackPhaseDataArray[i].firstAttackPhasePosition.X, firstAttackPhaseDataArray[i].firstAttackPhasePosition.Y);
							aiPlayer5AttackHouseGroupArray := AddElementToGroupHouseAttackDataArray(aiPlayer5AttackHouseGroupArray, groupHouseAttackData);
						end;
				end;
			aiPlayer5FirstAttackPhaseEnded := false;
			aiPlayer5DefencePositionsUIDFirstAttackPhaseArray  :=  PlaceDefencePositionsForFirstAttackPhase(5, aiPlayer5DefencePositionsToReAddArray, firstAttackPhaseDataArray, aiPlayer5DefencePositionsUIDFirstAttackPhaseArray);
			Actions.Log('attack player 5 group types:' + ' meleeGroupCount ' + IntToStr(meleeGroupCount) + ', antiHorseGroupCount ' + IntToStr(antiHorseGroupCount) + ', rangedGroupCount ' + IntToStr(rangedGroupCount) + ', mountedGroupCount ' + IntToStr(mountedGroupCount));	
		end;
end;

procedure CreateRandomAttackAiPlayer6__Mission8_Attacks;
	var maxGroupCounts: integer;
	var rangedGroupCount: integer;
	var mountedGroupCount: integer;
	var antiHorseGroupCount: integer;
	var meleeGroupCount: integer;
	var firstAttackPhaseDataArray: array [0..4] of FirstAttackPhaseData;
	var groupHouseAttackData: GroupHouseAttackData;
	var i: integer;
begin
	if(aiPlayer6NextAttackScheduledTime = -1) then 
		begin
			aiPlayer6NextAttackScheduledTime := States.GameTime + Utils.TimeToTick(0,Utils.RandomRangeI(15,20),0);
		end;
	if((aiPlayer6NextAttackScheduledTime <= States.GameTime) and (HasEnoughSoldiersForAttack(6,1,1,1,2))) then 
		begin
			aiPlayer6AttackStartedTime := States.GameTime;
			aiPlayer6AttackEnded := false;
			maxGroupCounts := Length(firstAttackPhaseDataArray);
			meleeGroupCount := 1;
			antiHorseGroupCount := 1;
			rangedGroupCount := 1;
			mountedGroupCount := 2;
			aiPlayer6AttackGroupsArray := AddGroupIdsToAttackArray(aiPlayer6AttackGroupsArray,6,meleeGroupCount,antiHorseGroupCount,rangedGroupCount,mountedGroupCount);
			aiPlayer6DefencePositionsToReAddArray := RemoveDefencePositionsUntilAttackIsOver(6, aiPlayer6AttackGroupsArray, aiPlayer6DefencePositionsToReAddArray);

			firstAttackPhaseDataArray[0].firstAttackPhasePosition.X := 129;
			firstAttackPhaseDataArray[0].firstAttackPhasePosition.Y := 88;
			firstAttackPhaseDataArray[0].firstAttackPhaseDirection := dirS;
			
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.X := 137;
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.Y := 94;
			firstAttackPhaseDataArray[1].firstAttackPhaseDirection := dirS;
			
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.X := 146;
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.Y := 94;
			firstAttackPhaseDataArray[2].firstAttackPhaseDirection := dirS;
			
			firstAttackPhaseDataArray[3].firstAttackPhasePosition.X := 151;
			firstAttackPhaseDataArray[3].firstAttackPhasePosition.Y := 94;
			firstAttackPhaseDataArray[3].firstAttackPhaseDirection := dirS;

			firstAttackPhaseDataArray[4].firstAttackPhasePosition.X := 128;
			firstAttackPhaseDataArray[4].firstAttackPhasePosition.Y := 83;
			firstAttackPhaseDataArray[4].firstAttackPhaseDirection := dirS;

			for i := 0 to Length(aiPlayer6DefencePositionsToReAddArray) - 1 do
				begin
					if(States.HouseAt(firstAttackPhaseDataArray[i].firstAttackPhasePosition.X, firstAttackPhaseDataArray[i].firstAttackPhasePosition.Y) <> -1) then
						begin
							groupHouseAttackData.groupId := aiPlayer6DefencePositionsToReAddArray[i].GroupID;
							groupHouseAttackData.houseId := States.HouseAt(firstAttackPhaseDataArray[i].firstAttackPhasePosition.X, firstAttackPhaseDataArray[i].firstAttackPhasePosition.Y);
							aiPlayer6AttackHouseGroupArray := AddElementToGroupHouseAttackDataArray(aiPlayer6AttackHouseGroupArray, groupHouseAttackData);
						end;
				end;
			aiPlayer6FirstAttackPhaseEnded := false;
			aiPlayer6DefencePositionsUIDFirstAttackPhaseArray  :=  PlaceDefencePositionsForFirstAttackPhase(6, aiPlayer6DefencePositionsToReAddArray, firstAttackPhaseDataArray, aiPlayer6DefencePositionsUIDFirstAttackPhaseArray);
			Actions.Log('attack player 6 group types:' + ' meleeGroupCount ' + IntToStr(meleeGroupCount) + ', antiHorseGroupCount ' + IntToStr(antiHorseGroupCount) + ', rangedGroupCount ' + IntToStr(rangedGroupCount) + ', mountedGroupCount ' + IntToStr(mountedGroupCount));	
		end;
end;

procedure CreateRandomAttackAiPlayer7__Mission8_Attacks;
	var maxGroupCounts: integer;
	var rangedGroupCount: integer;
	var mountedGroupCount: integer;
	var antiHorseGroupCount: integer;
	var meleeGroupCount: integer;
	var firstAttackPhaseDataArray: array [0..5] of FirstAttackPhaseData;
	var groupHouseAttackData: GroupHouseAttackData;
	var i: integer;
begin
	if(aiPlayer7NextAttackScheduledTime = -1) then 
		begin
			aiPlayer7NextAttackScheduledTime := States.GameTime + Utils.TimeToTick(0,Utils.RandomRangeI(20,25),0);
		end;
	if((aiPlayer7NextAttackScheduledTime <= States.GameTime) and (HasEnoughSoldiersForAttack(7,2,1,2,1))) then 
		begin
			aiPlayer7AttackStartedTime := States.GameTime;
			aiPlayer7AttackEnded := false;
			maxGroupCounts := Length(firstAttackPhaseDataArray);
			meleeGroupCount := 2;
			antiHorseGroupCount := 1;
			rangedGroupCount := 2;
			mountedGroupCount := 1;
			aiPlayer7AttackGroupsArray := AddGroupIdsToAttackArray(aiPlayer7AttackGroupsArray,7,meleeGroupCount,antiHorseGroupCount,rangedGroupCount,mountedGroupCount);
			aiPlayer7DefencePositionsToReAddArray := RemoveDefencePositionsUntilAttackIsOver(7, aiPlayer7AttackGroupsArray, aiPlayer7DefencePositionsToReAddArray);

			firstAttackPhaseDataArray[0].firstAttackPhasePosition.X := 110;
			firstAttackPhaseDataArray[0].firstAttackPhasePosition.Y := 97;
			firstAttackPhaseDataArray[0].firstAttackPhaseDirection := dirS;
			
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.X := 104;
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.Y := 97;
			firstAttackPhaseDataArray[1].firstAttackPhaseDirection := dirS;
			
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.X := 110;
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.Y := 90;
			firstAttackPhaseDataArray[2].firstAttackPhaseDirection := dirS;
			
			firstAttackPhaseDataArray[3].firstAttackPhasePosition.X := 104;
			firstAttackPhaseDataArray[3].firstAttackPhasePosition.Y := 90;
			firstAttackPhaseDataArray[3].firstAttackPhaseDirection := dirS;

			firstAttackPhaseDataArray[4].firstAttackPhasePosition.X := 99;
			firstAttackPhaseDataArray[4].firstAttackPhasePosition.Y := 97;
			firstAttackPhaseDataArray[4].firstAttackPhaseDirection := dirS;

			firstAttackPhaseDataArray[5].firstAttackPhasePosition.X := 99;
			firstAttackPhaseDataArray[5].firstAttackPhasePosition.Y := 90;
			firstAttackPhaseDataArray[5].firstAttackPhaseDirection := dirS;

			for i := 0 to Length(aiPlayer7DefencePositionsToReAddArray) - 1 do
				begin
					if(States.HouseAt(firstAttackPhaseDataArray[i].firstAttackPhasePosition.X, firstAttackPhaseDataArray[i].firstAttackPhasePosition.Y) <> -1) then
						begin
							groupHouseAttackData.groupId := aiPlayer7DefencePositionsToReAddArray[i].GroupID;
							groupHouseAttackData.houseId := States.HouseAt(firstAttackPhaseDataArray[i].firstAttackPhasePosition.X, firstAttackPhaseDataArray[i].firstAttackPhasePosition.Y);
							aiPlayer7AttackHouseGroupArray := AddElementToGroupHouseAttackDataArray(aiPlayer7AttackHouseGroupArray, groupHouseAttackData);
						end;
				end;
			aiPlayer7FirstAttackPhaseEnded := false;
			aiPlayer7DefencePositionsUIDFirstAttackPhaseArray  :=  PlaceDefencePositionsForFirstAttackPhase(7, aiPlayer7DefencePositionsToReAddArray, firstAttackPhaseDataArray, aiPlayer7DefencePositionsUIDFirstAttackPhaseArray);
			Actions.Log('attack player 7 group types:' + ' meleeGroupCount ' + IntToStr(meleeGroupCount) + ', antiHorseGroupCount ' + IntToStr(antiHorseGroupCount) + ', rangedGroupCount ' + IntToStr(rangedGroupCount) + ', mountedGroupCount ' + IntToStr(mountedGroupCount));	
		end;
end;

procedure OnTick_aiPlayer1__Mission8_Attacks;
begin
	if States.GameTime mod 600 = 0 then
		begin
			if(aiPlayer1AttackEnded) then
				begin
					CreateRandomAttackAiPlayer1_FirstAttack__Mission8_Attacks();	
				end;
		end;
	if States.GameTime mod 100 = 0 then
		begin
			aiPlayer1AttackGroupsArray := SetGroupsHaltIfSpread(1, aiPlayer1AttackGroupsArray);
			if(aiPlayer1FirstAttackPhaseEnded = false) then begin
				OrderGroupHouseAttacks(aiPlayer1AttackHouseGroupArray);
			end;
		end;
	if States.GameTime mod 10 = 0 then
		begin
			AttackClosestHousePlanIdForAllPlayerGroups(1);
			if(not aiPlayer1AttackEnded) then 
				begin	
					aiPlayer1AttackGroupsArray := DeleteDeadAttackGroupsFromArray(aiPlayer1AttackGroupsArray);

					if((aiPlayer1FirstAttackPhaseEnded = false) and (ShouldFinishFirstAttackPhase(1, aiPlayer1AttackStartedTime, 600, aiPlayer1DefencePositionsUIDFirstAttackPhaseArray))) then
						begin
							aiPlayer1FirstAttackPhaseEnded := true;
							SetLength(aiPlayer1AttackHouseGroupArray,0);
							aiPlayer1DefencePositionsUIDFirstAttackPhaseArray := RemoveDefencePositions(1,aiPlayer1DefencePositionsUIDFirstAttackPhaseArray);
						end;
					if(aiPlayer1FirstAttackPhaseEnded) then
						begin
							OrderAttackForAllAttackGroups(1, aiPlayer1AttackGroupsArray);
						end;
					if((Length(aiPlayer1AttackGroupsArray) = 0) or (States.GameTime - aiPlayer1AttackStartedTime > 6000)) then 
						begin
							aiPlayer1DefencePositionsToReAddArray := ReAddRemovedDefencePositions(1, aiPlayer1DefencePositionsToReAddArray);
							aiPlayer1FirstAttackPhaseEnded := false;
							aiPlayer1AttackEnded := true;		
							aiPlayer1NextAttackScheduledTime := States.GameTime + Utils.TimeToTick(0,Utils.RandomRangeI(10,15),0);							
						end;
				end;
				aiPlayer1IdleGroupsArray := DeleteDeadIdleGroupsFromArray(aiPlayer1IdleGroupsArray);
				aiPlayer1IdleAttackGroupsArray := DeleteDeadAttackGroupsFromArray(aiPlayer1IdleAttackGroupsArray);
									
				if(States.GameTime > 100) then 
					begin
						aiPlayer1IdleGroupsArray := UpdateGroupIdleDataArray(1, aiPlayer1IdleGroupsArray);
						aiPlayer1IdleAttackGroupsArray := AddIdleGroupsToAttackGroupsArray(aiPlayer1IdleGroupsArray, aiPlayer1IdleAttackGroupsArray);
					end;
				OrderAttackForAllAttackGroups(1, aiPlayer1IdleAttackGroupsArray);
		end;
end;

procedure OnTick_aiPlayer2__Mission8_Attacks;
begin
	if States.GameTime mod 600 = 0 then
		begin
			if(aiPlayer2AttackEnded) then
				begin			
					CreateRandomAttackAiPlayer2__Mission8_Attacks();
				end;
		end;
	if States.GameTime mod 100 = 0 then
		begin
			aiPlayer2AttackGroupsArray := SetGroupsHaltIfSpread(2, aiPlayer2AttackGroupsArray);
			if(aiPlayer2FirstAttackPhaseEnded = false) then begin
				OrderGroupHouseAttacks(aiPlayer2AttackHouseGroupArray);
			end;
		end;
	if States.GameTime mod 10 = 0 then
		begin
			AttackClosestHousePlanIdForAllPlayerGroups(2);
			if(not aiPlayer2AttackEnded) then 
				begin
					aiPlayer2AttackGroupsArray := DeleteDeadAttackGroupsFromArray(aiPlayer2AttackGroupsArray);
					
					if((aiPlayer2FirstAttackPhaseEnded = false) and (ShouldFinishFirstAttackPhase(2, aiPlayer2AttackStartedTime, 600, aiPlayer2DefencePositionsUIDFirstAttackPhaseArray))) then
						begin
							aiPlayer2FirstAttackPhaseEnded := true;
							SetLength(aiPlayer2AttackHouseGroupArray,0);
							aiPlayer2DefencePositionsUIDFirstAttackPhaseArray := RemoveDefencePositions(2,aiPlayer2DefencePositionsUIDFirstAttackPhaseArray);
						end;
					if(aiPlayer2FirstAttackPhaseEnded) then
						begin
							OrderAttackForAllAttackGroups(2,aiPlayer2AttackGroupsArray);
						end;
					if((Length(aiPlayer2AttackGroupsArray) = 0) or (States.GameTime - aiPlayer2AttackStartedTime > 6000)) then 
						begin
							aiPlayer2DefencePositionsToReAddArray := ReAddRemovedDefencePositions(2, aiPlayer2DefencePositionsToReAddArray);
							aiPlayer2FirstAttackPhaseEnded := false;
							aiPlayer2AttackEnded := true;	
							aiPlayer2NextAttackScheduledTime := States.GameTime + Utils.TimeToTick(0,Utils.RandomRangeI(10,15),0);							
						end;
				end;
				aiPlayer2IdleGroupsArray := DeleteDeadIdleGroupsFromArray(aiPlayer2IdleGroupsArray);
				aiPlayer2IdleAttackGroupsArray := DeleteDeadAttackGroupsFromArray(aiPlayer2IdleAttackGroupsArray);
									
				if(States.GameTime > 100) then 
					begin
						aiPlayer2IdleGroupsArray := UpdateGroupIdleDataArray(2, aiPlayer2IdleGroupsArray);
						aiPlayer2IdleAttackGroupsArray := AddIdleGroupsToAttackGroupsArray(aiPlayer2IdleGroupsArray, aiPlayer2IdleAttackGroupsArray);
					end;
				OrderAttackForAllAttackGroups(2,aiPlayer2IdleAttackGroupsArray);
		end;
end;

procedure OnTick_aiPlayer3__Mission8_Attacks;
begin
	if States.GameTime mod 600 = 0 then
		begin
			if(aiPlayer3AttackEnded) then
				begin			
					CreateRandomAttackAiPlayer3__Mission8_Attacks();
				end;
		end;
	if States.GameTime mod 100 = 0 then
		begin
			aiPlayer3AttackGroupsArray := SetGroupsHaltIfSpread(3, aiPlayer3AttackGroupsArray);
			if(aiPlayer3FirstAttackPhaseEnded = false) then begin
				OrderGroupHouseAttacks(aiPlayer3AttackHouseGroupArray);
			end;
		end;
	if States.GameTime mod 10 = 0 then
		begin
			AttackClosestHousePlanIdForAllPlayerGroups(3);
			if(not aiPlayer3AttackEnded) then 
				begin	
					aiPlayer3AttackGroupsArray := DeleteDeadAttackGroupsFromArray(aiPlayer3AttackGroupsArray);
								
					if((aiPlayer3FirstAttackPhaseEnded = false) and (ShouldFinishFirstAttackPhase(3, aiPlayer3AttackStartedTime, 1200, aiPlayer3DefencePositionsUIDFirstAttackPhaseArray))) then
						begin
							aiPlayer3FirstAttackPhaseEnded := true;
							SetLength(aiPlayer3AttackHouseGroupArray,0);
							aiPlayer3DefencePositionsUIDFirstAttackPhaseArray := RemoveDefencePositions(3,aiPlayer3DefencePositionsUIDFirstAttackPhaseArray);
						end;
					if(aiPlayer3FirstAttackPhaseEnded) then
						begin
							OrderAttackForAllAttackGroups(3,aiPlayer3AttackGroupsArray);
						end;
					if((Length(aiPlayer3AttackGroupsArray) = 0) or (States.GameTime - aiPlayer3AttackStartedTime > 6000)) then 
						begin
							aiPlayer3DefencePositionsToReAddArray := ReAddRemovedDefencePositions(3, aiPlayer3DefencePositionsToReAddArray);
							aiPlayer3FirstAttackPhaseEnded := false;
							aiPlayer3AttackEnded := true;
							aiPlayer3NextAttackScheduledTime := States.GameTime + Utils.TimeToTick(0,Utils.RandomRangeI(10,15),0);							
						end;
				end;
				aiPlayer3IdleGroupsArray := DeleteDeadIdleGroupsFromArray(aiPlayer3IdleGroupsArray);
				aiPlayer3IdleAttackGroupsArray := DeleteDeadAttackGroupsFromArray(aiPlayer3IdleAttackGroupsArray);
									
				if(States.GameTime > 100) then 
					begin
						aiPlayer3IdleGroupsArray := UpdateGroupIdleDataArray(3, aiPlayer3IdleGroupsArray);
						aiPlayer3IdleAttackGroupsArray := AddIdleGroupsToAttackGroupsArray(aiPlayer3IdleGroupsArray, aiPlayer3IdleAttackGroupsArray);
					end;
				OrderAttackForAllAttackGroups(3,aiPlayer3IdleAttackGroupsArray);
		end;
end;

procedure OnTick_aiPlayer4__Mission8_Attacks;
begin
	if States.GameTime mod 600 = 0 then
		begin
			if(aiPlayer4AttackEnded) then
				begin			
					CreateRandomAttackAiPlayer4__Mission8_Attacks();
				end;
		end;
	if States.GameTime mod 100 = 0 then
		begin
			aiPlayer4AttackGroupsArray := SetGroupsHaltIfSpread(4, aiPlayer4AttackGroupsArray);
			if(aiPlayer4FirstAttackPhaseEnded = false) then begin
				OrderGroupHouseAttacks(aiPlayer4AttackHouseGroupArray);
			end;
		end;
	if States.GameTime mod 10 = 0 then
		begin
			AttackClosestHousePlanIdForAllPlayerGroups(4);
			if(not aiPlayer4AttackEnded) then 
				begin	
					aiPlayer4AttackGroupsArray := DeleteDeadAttackGroupsFromArray(aiPlayer4AttackGroupsArray);
								
					if((aiPlayer4FirstAttackPhaseEnded = false) and (ShouldFinishFirstAttackPhase(4, aiPlayer4AttackStartedTime, 1800, aiPlayer4DefencePositionsUIDFirstAttackPhaseArray))) then
						begin
							aiPlayer4FirstAttackPhaseEnded := true;
							SetLength(aiPlayer4AttackHouseGroupArray,0);
							aiPlayer4DefencePositionsUIDFirstAttackPhaseArray := RemoveDefencePositions(4,aiPlayer4DefencePositionsUIDFirstAttackPhaseArray);
						end;
					if(aiPlayer4FirstAttackPhaseEnded) then
						begin
							OrderAttackForAllAttackGroups(4,aiPlayer4AttackGroupsArray);
						end;
					if((Length(aiPlayer4AttackGroupsArray) = 0) or (States.GameTime - aiPlayer4AttackStartedTime > 6000)) then 
						begin
							aiPlayer4DefencePositionsToReAddArray := ReAddRemovedDefencePositions(4, aiPlayer4DefencePositionsToReAddArray);
							aiPlayer4FirstAttackPhaseEnded := false;
							aiPlayer4AttackEnded := true;
							aiPlayer4NextAttackScheduledTime := States.GameTime + Utils.TimeToTick(0,Utils.RandomRangeI(10,15),0);							
						end;
				end;
				aiPlayer4IdleGroupsArray := DeleteDeadIdleGroupsFromArray(aiPlayer4IdleGroupsArray);
				aiPlayer4IdleAttackGroupsArray := DeleteDeadAttackGroupsFromArray(aiPlayer4IdleAttackGroupsArray);
									
				if(States.GameTime > 100) then 
					begin
						aiPlayer4IdleGroupsArray := UpdateGroupIdleDataArray(4, aiPlayer4IdleGroupsArray);
						aiPlayer4IdleAttackGroupsArray := AddIdleGroupsToAttackGroupsArray(aiPlayer4IdleGroupsArray, aiPlayer4IdleAttackGroupsArray);
					end;
				OrderAttackForAllAttackGroups(4,aiPlayer4IdleAttackGroupsArray);
		end;
end;

procedure OnTick_aiPlayer5__Mission8_Attacks;
begin
	if States.GameTime mod 600 = 0 then
		begin
			if(aiPlayer5AttackEnded) then
				begin			
					CreateRandomAttackAiPlayer5__Mission8_Attacks();
				end;
		end;
	if States.GameTime mod 100 = 0 then
		begin
			aiPlayer5AttackGroupsArray := SetGroupsHaltIfSpread(5, aiPlayer5AttackGroupsArray);
			if(aiPlayer5FirstAttackPhaseEnded = false) then begin
				OrderGroupHouseAttacks(aiPlayer5AttackHouseGroupArray);
			end;
		end;
	if States.GameTime mod 10 = 0 then
		begin
			AttackClosestHousePlanIdForAllPlayerGroups(5);
			if(not aiPlayer5AttackEnded) then 
				begin	
					aiPlayer5AttackGroupsArray := DeleteDeadAttackGroupsFromArray(aiPlayer5AttackGroupsArray);
								
					if((aiPlayer5FirstAttackPhaseEnded = false) and (ShouldFinishFirstAttackPhase(5, aiPlayer5AttackStartedTime, 1800, aiPlayer5DefencePositionsUIDFirstAttackPhaseArray))) then
						begin
							aiPlayer5FirstAttackPhaseEnded := true;
							SetLength(aiPlayer5AttackHouseGroupArray,0);
							aiPlayer5DefencePositionsUIDFirstAttackPhaseArray := RemoveDefencePositions(5,aiPlayer5DefencePositionsUIDFirstAttackPhaseArray);
						end;
					if(aiPlayer5FirstAttackPhaseEnded) then
						begin
							OrderAttackForAllAttackGroups(5,aiPlayer5AttackGroupsArray);
						end;
					if((Length(aiPlayer5AttackGroupsArray) = 0) or (States.GameTime - aiPlayer5AttackStartedTime > 6000)) then 
						begin
							aiPlayer5DefencePositionsToReAddArray := ReAddRemovedDefencePositions(5, aiPlayer5DefencePositionsToReAddArray);
							aiPlayer5FirstAttackPhaseEnded := false;
							aiPlayer5AttackEnded := true;
							aiPlayer5NextAttackScheduledTime := States.GameTime + Utils.TimeToTick(0,Utils.RandomRangeI(15,20),0);							
						end;
				end;
				aiPlayer5IdleGroupsArray := DeleteDeadIdleGroupsFromArray(aiPlayer5IdleGroupsArray);
				aiPlayer5IdleAttackGroupsArray := DeleteDeadAttackGroupsFromArray(aiPlayer5IdleAttackGroupsArray);
									
				if(States.GameTime > 100) then 
					begin
						aiPlayer5IdleGroupsArray := UpdateGroupIdleDataArray(5, aiPlayer5IdleGroupsArray);
						aiPlayer5IdleAttackGroupsArray := AddIdleGroupsToAttackGroupsArray(aiPlayer5IdleGroupsArray, aiPlayer5IdleAttackGroupsArray);
					end;
				OrderAttackForAllAttackGroups(5,aiPlayer5IdleAttackGroupsArray);
		end;
end;

procedure OnTick_aiPlayer6__Mission8_Attacks;
begin
	if States.GameTime mod 600 = 0 then
		begin
			if(aiPlayer6AttackEnded) then
				begin			
					CreateRandomAttackAiPlayer6__Mission8_Attacks();
				end;
		end;
	if States.GameTime mod 100 = 0 then
		begin
			aiPlayer6AttackGroupsArray := SetGroupsHaltIfSpread(6, aiPlayer6AttackGroupsArray);
			if(aiPlayer6FirstAttackPhaseEnded = false) then begin
				OrderGroupHouseAttacks(aiPlayer6AttackHouseGroupArray);
			end;
		end;
	if States.GameTime mod 10 = 0 then
		begin
			AttackClosestHousePlanIdForAllPlayerGroups(6);
			if(not aiPlayer6AttackEnded) then 
				begin	
					aiPlayer6AttackGroupsArray := DeleteDeadAttackGroupsFromArray(aiPlayer6AttackGroupsArray);
								
					if((aiPlayer6FirstAttackPhaseEnded = false) and (ShouldFinishFirstAttackPhase(6, aiPlayer6AttackStartedTime, 1800, aiPlayer6DefencePositionsUIDFirstAttackPhaseArray))) then
						begin
							aiPlayer6FirstAttackPhaseEnded := true;
							SetLength(aiPlayer6AttackHouseGroupArray,0);
							aiPlayer6DefencePositionsUIDFirstAttackPhaseArray := RemoveDefencePositions(6,aiPlayer6DefencePositionsUIDFirstAttackPhaseArray);
						end;
					if(aiPlayer6FirstAttackPhaseEnded) then
						begin
							OrderAttackForAllAttackGroups(6,aiPlayer6AttackGroupsArray);
						end;
					if((Length(aiPlayer6AttackGroupsArray) = 0) or (States.GameTime - aiPlayer6AttackStartedTime > 6000)) then 
						begin
							aiPlayer6DefencePositionsToReAddArray := ReAddRemovedDefencePositions(6, aiPlayer6DefencePositionsToReAddArray);
							aiPlayer6FirstAttackPhaseEnded := false;
							aiPlayer6AttackEnded := true;
							aiPlayer6NextAttackScheduledTime := States.GameTime + Utils.TimeToTick(0,Utils.RandomRangeI(15,20),0);							
						end;
				end;
				aiPlayer6IdleGroupsArray := DeleteDeadIdleGroupsFromArray(aiPlayer6IdleGroupsArray);
				aiPlayer6IdleAttackGroupsArray := DeleteDeadAttackGroupsFromArray(aiPlayer6IdleAttackGroupsArray);
									
				if(States.GameTime > 100) then 
					begin
						aiPlayer6IdleGroupsArray := UpdateGroupIdleDataArray(6, aiPlayer6IdleGroupsArray);
						aiPlayer6IdleAttackGroupsArray := AddIdleGroupsToAttackGroupsArray(aiPlayer6IdleGroupsArray, aiPlayer6IdleAttackGroupsArray);
					end;
				OrderAttackForAllAttackGroups(6,aiPlayer6IdleAttackGroupsArray);
		end;
end;

procedure OnTick_aiPlayer7__Mission8_Attacks;
begin
	if States.GameTime mod 600 = 0 then
		begin
			if(aiPlayer7AttackEnded) then
				begin			
					CreateRandomAttackAiPlayer7__Mission8_Attacks();
				end;
		end;
	if States.GameTime mod 100 = 0 then
		begin
			aiPlayer7AttackGroupsArray := SetGroupsHaltIfSpread(7, aiPlayer7AttackGroupsArray);
			if(aiPlayer7FirstAttackPhaseEnded = false) then begin
				OrderGroupHouseAttacks(aiPlayer7AttackHouseGroupArray);
			end;
		end;
	if States.GameTime mod 10 = 0 then
		begin
			AttackClosestHousePlanIdForAllPlayerGroups(7);
			if(not aiPlayer7AttackEnded) then 
				begin	
					aiPlayer7AttackGroupsArray := DeleteDeadAttackGroupsFromArray(aiPlayer7AttackGroupsArray);
								
					if((aiPlayer7FirstAttackPhaseEnded = false) and (ShouldFinishFirstAttackPhase(7, aiPlayer7AttackStartedTime, 1800, aiPlayer7DefencePositionsUIDFirstAttackPhaseArray))) then
						begin
							aiPlayer7FirstAttackPhaseEnded := true;
							SetLength(aiPlayer7AttackHouseGroupArray,0);
							aiPlayer7DefencePositionsUIDFirstAttackPhaseArray := RemoveDefencePositions(7,aiPlayer7DefencePositionsUIDFirstAttackPhaseArray);
						end;
					if(aiPlayer7FirstAttackPhaseEnded) then
						begin
							OrderAttackForAllAttackGroups(7,aiPlayer7AttackGroupsArray);
						end;
					if((Length(aiPlayer7AttackGroupsArray) = 0) or (States.GameTime - aiPlayer7AttackStartedTime > 6000)) then 
						begin
							aiPlayer7DefencePositionsToReAddArray := ReAddRemovedDefencePositions(7, aiPlayer7DefencePositionsToReAddArray);
							aiPlayer7FirstAttackPhaseEnded := false;
							aiPlayer7AttackEnded := true;
							aiPlayer7NextAttackScheduledTime := States.GameTime + Utils.TimeToTick(0,Utils.RandomRangeI(20,25),0);							
						end;
				end;
				aiPlayer7IdleGroupsArray := DeleteDeadIdleGroupsFromArray(aiPlayer7IdleGroupsArray);
				aiPlayer7IdleAttackGroupsArray := DeleteDeadAttackGroupsFromArray(aiPlayer7IdleAttackGroupsArray);
									
				if(States.GameTime > 100) then 
					begin
						aiPlayer7IdleGroupsArray := UpdateGroupIdleDataArray(7, aiPlayer7IdleGroupsArray);
						aiPlayer7IdleAttackGroupsArray := AddIdleGroupsToAttackGroupsArray(aiPlayer7IdleGroupsArray, aiPlayer7IdleAttackGroupsArray);
					end;
				OrderAttackForAllAttackGroups(7,aiPlayer7IdleAttackGroupsArray);
		end;
end;



procedure OnMissionStart__Mission8_Attacks;
begin
	aiPlayer1NextAttackScheduledTime := -1;
	aiPlayer2NextAttackScheduledTime := -1;
	aiPlayer3NextAttackScheduledTime := -1;
	aiPlayer4NextAttackScheduledTime := -1;
	aiPlayer5NextAttackScheduledTime := -1;
	aiPlayer6NextAttackScheduledTime := -1;
	aiPlayer7NextAttackScheduledTime := -1;
	
	aiPlayer1FirstAttackPhaseEnded := true;
	aiPlayer1AttackEnded := true;
	
	aiPlayer2FirstAttackPhaseEnded := true;
	aiPlayer2AttackEnded := true;
	
	aiPlayer3FirstAttackPhaseEnded := true;
	aiPlayer3AttackEnded := true;
	
	aiPlayer4FirstAttackPhaseEnded := true;
	aiPlayer4AttackEnded := true;
	
	aiPlayer5FirstAttackPhaseEnded := true;
	aiPlayer5AttackEnded := true;
	
	aiPlayer6FirstAttackPhaseEnded := true;
	aiPlayer6AttackEnded := true;
	
	aiPlayer7FirstAttackPhaseEnded := true;
	aiPlayer7AttackEnded := true;
end;

procedure OnTick__Mission8_Attacks;
begin
	OnTick_aiPlayer1__Mission8_Attacks();
	OnTick_aiPlayer2__Mission8_Attacks();
	OnTick_aiPlayer3__Mission8_Attacks();
	OnTick_aiPlayer4__Mission8_Attacks();
	OnTick_aiPlayer5__Mission8_Attacks();
	OnTick_aiPlayer6__Mission8_Attacks();
	OnTick_aiPlayer7__Mission8_Attacks();
end;