{$I PTLAttacks.script}

{$EVENT evtMissionStart:OnMissionStart__Mission7_Attacks}
{$EVENT evtTick:OnTick__Mission7_Attacks}


var attacksUnlocked: boolean; 
 
//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;

procedure CreateRandomAttackAiPlayer1__Mission7_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(aiPlayer1NextAttackScheduledTime = -1) then 
		begin
			aiPlayer1NextAttackScheduledTime := States.GameTime + Utils.TimeToTick(0,Utils.RandomRangeI(20,25),0);
		end;
	if((aiPlayer1NextAttackScheduledTime <= States.GameTime) and (HasEnoughSoldiersForAttack(1,1,1,1,2))) then 
		begin
			aiPlayer1AttackStartedTime := States.GameTime;
			aiPlayer1AttackEnded := false;
			maxGroupCounts := Length(firstAttackPhaseDataArray);
			meleeGroupCount := 1;
			antiHorseGroupCount := 1;
			rangedGroupCount := Utils.RandomRangeI(0, 1);
			mountedGroupCount := Utils.RandomRangeI(maxGroupCounts - 2 - rangedGroupCount, maxGroupCounts - 2 - rangedGroupCount);
			aiPlayer1AttackGroupsArray := AddGroupIdsToAttackArray(aiPlayer1AttackGroupsArray,1,meleeGroupCount,antiHorseGroupCount,rangedGroupCount,mountedGroupCount);
			aiPlayer1DefencePositionsToReAddArray := RemoveDefencePositionsUntilAttackIsOver(1, aiPlayer1AttackGroupsArray, aiPlayer1DefencePositionsToReAddArray);
           
			firstAttackPhaseDataArray[0].firstAttackPhasePosition.X := 49
			firstAttackPhaseDataArray[0].firstAttackPhasePosition.Y := 63
			firstAttackPhaseDataArray[0].firstAttackPhaseDirection := dirS;
			
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.X := 56
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.Y := 63
			firstAttackPhaseDataArray[1].firstAttackPhaseDirection := dirS;
			
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.X := 63
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.Y := 63
			firstAttackPhaseDataArray[2].firstAttackPhaseDirection := dirS;
			
			firstAttackPhaseDataArray[3].firstAttackPhasePosition.X := 56
			firstAttackPhaseDataArray[3].firstAttackPhasePosition.Y := 58
			firstAttackPhaseDataArray[3].firstAttackPhaseDirection := dirS;
			
			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__Mission7_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(aiPlayer2NextAttackScheduledTime = -1) then 
		begin
			aiPlayer2NextAttackScheduledTime := States.GameTime + Utils.TimeToTick(0,Utils.RandomRangeI(45,50),0);
		end;
	if((aiPlayer2NextAttackScheduledTime <= States.GameTime) and (HasEnoughSoldiersForAttack(2,3,2,1,1))) then 
		begin
			aiPlayer2AttackStartedTime := States.GameTime;
			aiPlayer2AttackEnded := false;
			maxGroupCounts := Length(firstAttackPhaseDataArray);
			meleeGroupCount :=  Utils.RandomRangeI(1,3);
			antiHorseGroupCount := Utils.RandomRangeI(maxGroupCounts - 2 - meleeGroupCount, 2);
			rangedGroupCount := Utils.RandomRangeI(Utils.MaxI(maxGroupCounts - 1 - antiHorseGroupCount - meleeGroupCount, 0), Utils.MinI(maxGroupCounts - antiHorseGroupCount - meleeGroupCount, 1));
			mountedGroupCount := Utils.RandomRangeI(maxGroupCounts - antiHorseGroupCount - rangedGroupCount - meleeGroupCount, maxGroupCounts - rangedGroupCount - meleeGroupCount - antiHorseGroupCount);
			aiPlayer2AttackGroupsArray := AddGroupIdsToAttackArray(aiPlayer2AttackGroupsArray,2,meleeGroupCount,antiHorseGroupCount,rangedGroupCount,mountedGroupCount);
			aiPlayer2DefencePositionsToReAddArray := RemoveDefencePositionsUntilAttackIsOver(2, aiPlayer2AttackGroupsArray, aiPlayer2DefencePositionsToReAddArray);

			firstAttackPhaseDataArray[0].firstAttackPhasePosition.X := 108
			firstAttackPhaseDataArray[0].firstAttackPhasePosition.Y := 88
			firstAttackPhaseDataArray[0].firstAttackPhaseDirection := dirW;
			
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.X := 108
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.Y := 82
			firstAttackPhaseDataArray[1].firstAttackPhaseDirection := dirW;
			
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.X := 102
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.Y := 88
			firstAttackPhaseDataArray[2].firstAttackPhaseDirection := dirW;
			
			firstAttackPhaseDataArray[3].firstAttackPhasePosition.X := 102
			firstAttackPhaseDataArray[3].firstAttackPhasePosition.Y := 82
			firstAttackPhaseDataArray[3].firstAttackPhaseDirection := dirW;
			
			firstAttackPhaseDataArray[4].firstAttackPhasePosition.X := 113
			firstAttackPhaseDataArray[4].firstAttackPhasePosition.Y := 84
			firstAttackPhaseDataArray[4].firstAttackPhaseDirection := dirW;
			

			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__Mission7_Attacks;
	var maxGroupCounts: integer;
	var rangedGroupCount: integer;
	var mountedGroupCount: integer;
	var antiHorseGroupCount: integer;
	var meleeGroupCount: integer;
	var firstAttackPhaseDataArray: array [0..6] of FirstAttackPhaseData;
	var groupHouseAttackData: GroupHouseAttackData;
	var i: integer;
begin
	if(aiPlayer3NextAttackScheduledTime = -1) then 
		begin
			aiPlayer3NextAttackScheduledTime := States.GameTime + Utils.TimeToTick(0,Utils.RandomRangeI(60,65),0);
		end;
	if((aiPlayer3NextAttackScheduledTime <= States.GameTime) and (HasEnoughSoldiersForAttack(3,3,2,2,3))) then 
		begin
			aiPlayer3AttackStartedTime := States.GameTime;
			aiPlayer3AttackEnded := false;
			maxGroupCounts := Length(firstAttackPhaseDataArray);
			meleeGroupCount := Utils.RandomRangeI(0,3);
			antiHorseGroupCount := Utils.RandomRangeI(Utils.MaxI(maxGroupCounts - 5 - meleeGroupCount, 0), 2);
			rangedGroupCount := Utils.RandomRangeI(Utils.MaxI(maxGroupCounts - 3 - antiHorseGroupCount, 0), 2);
			mountedGroupCount := Utils.RandomRangeI(maxGroupCounts - rangedGroupCount - antiHorseGroupCount - meleeGroupCount, maxGroupCounts - rangedGroupCount - antiHorseGroupCount - meleeGroupCount);
			aiPlayer3AttackGroupsArray := AddGroupIdsToAttackArray(aiPlayer3AttackGroupsArray,3,meleeGroupCount,antiHorseGroupCount,rangedGroupCount,mountedGroupCount);
			aiPlayer3DefencePositionsToReAddArray := RemoveDefencePositionsUntilAttackIsOver(3, aiPlayer3AttackGroupsArray, aiPlayer3DefencePositionsToReAddArray);

			firstAttackPhaseDataArray[0].firstAttackPhasePosition.X := 86
			firstAttackPhaseDataArray[0].firstAttackPhasePosition.Y := 115
			firstAttackPhaseDataArray[0].firstAttackPhaseDirection := dirN;
			
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.X := 93
			firstAttackPhaseDataArray[1].firstAttackPhasePosition.Y := 115
			firstAttackPhaseDataArray[1].firstAttackPhaseDirection := dirN;
			
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.X := 84
			firstAttackPhaseDataArray[2].firstAttackPhasePosition.Y := 55
			firstAttackPhaseDataArray[2].firstAttackPhaseDirection := dirW;
			
			firstAttackPhaseDataArray[3].firstAttackPhasePosition.X := 99
			firstAttackPhaseDataArray[3].firstAttackPhasePosition.Y := 112
			firstAttackPhaseDataArray[3].firstAttackPhaseDirection := dirNW;
			
			firstAttackPhaseDataArray[4].firstAttackPhasePosition.X := 105
			firstAttackPhaseDataArray[4].firstAttackPhasePosition.Y := 105
			firstAttackPhaseDataArray[4].firstAttackPhaseDirection := dirNW;
			
			firstAttackPhaseDataArray[5].firstAttackPhasePosition.X := 103
			firstAttackPhaseDataArray[5].firstAttackPhasePosition.Y := 115
			firstAttackPhaseDataArray[5].firstAttackPhaseDirection := dirNW;
			
			firstAttackPhaseDataArray[6].firstAttackPhasePosition.X := 109
			firstAttackPhaseDataArray[6].firstAttackPhasePosition.Y := 109
			firstAttackPhaseDataArray[6].firstAttackPhaseDirection := dirNW;



			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 OnTick_aiPlayer1__Mission7_Attacks;
begin
	if States.GameTime mod 600 = 0 then
		begin
			if(attacksUnlocked and aiPlayer1AttackEnded) then
				begin			
					CreateRandomAttackAiPlayer1__Mission7_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(20,25),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__Mission7_Attacks;
begin
	if States.GameTime mod 600 = 0 then
		begin
			if(attacksUnlocked and aiPlayer2AttackEnded) then
				begin			
					CreateRandomAttackAiPlayer2__Mission7_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, 1200, aiPlayer2DefencePositionsUIDFirstAttackPhaseArray))) then
						begin
							aiPlayer2FirstAttackPhaseEnded := true;
							SetLength(aiPlayer2AttackHouseGroupArray,0);
							aiPlayer2DefencePositionsUIDFirstAttackPhaseArray := RemoveDefencePositions(2,aiPlayer2DefencePositionsUIDFirstAttackPhaseArray);
						end;
					if(aiPlayer2FirstAttackPhaseEnded) then
						begin
							OrderAttackForAllAttackGroups(1,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(30,35),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(1,aiPlayer2IdleAttackGroupsArray);
		end;
end;

procedure OnTick_aiPlayer3__Mission7_Attacks;
begin
	if States.GameTime mod 600 = 0 then
		begin
			if(attacksUnlocked and aiPlayer3AttackEnded) then
				begin			
					CreateRandomAttackAiPlayer3__Mission7_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(1,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(30,35),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(1,aiPlayer3IdleAttackGroupsArray);
		end;
end;

procedure OnMissionStart__Mission7_Attacks;
begin
	aiPlayer1NextAttackScheduledTime := -1;
	aiPlayer2NextAttackScheduledTime := -1;
	aiPlayer3NextAttackScheduledTime := -1;
	
	aiPlayer1FirstAttackPhaseEnded := true;
	aiPlayer1AttackEnded := true;
	
	aiPlayer2FirstAttackPhaseEnded := true;
	aiPlayer2AttackEnded := true;
	
	aiPlayer3FirstAttackPhaseEnded := true;
	aiPlayer3AttackEnded := true;
	
	attacksUnlocked := false;
end;

procedure OnTick__Mission7_Attacks;
begin
	if States.GameTime mod 600 = 0 then
		begin
			Actions.Log('stone' + IntToStr(States.MapTileIsStone(49,106)) + ',' + IntToStr(States.MapTileIsStone(50,106)) + ',' + IntToStr(States.MapTileIsStone(51,106)) + ',' + IntToStr(States.MapTileIsStone(52,107)) + ',' + IntToStr(States.MapTileIsStone(53,107)) + ',' + IntToStr(States.MapTileIsStone(54,107)));
			if((States.MapTileIsStone(49,106) = 0) or (States.MapTileIsStone(50,106) = 0) or (States.MapTileIsStone(51,106) = 0) or (States.MapTileIsStone(52,107) = 0) or (States.MapTileIsStone(53,107) = 0) or (States.MapTileIsStone(54,107) = 0)) then 
				begin
					attacksUnlocked := true;
				end 
			else
				begin
					attacksUnlocked := false;
				end;
		end;
	OnTick_aiPlayer1__Mission7_Attacks();
	OnTick_aiPlayer2__Mission7_Attacks();
	OnTick_aiPlayer3__Mission7_Attacks();
end;