{$EVENT evtMissionStart:AIBOnMissionStart}
{$EVENT evtTick:AIBOnTick}
const 
	MIN_PLAYERS = 3;
	MAX_PLAYERS = 5;
	MAX_HOUSE_PLANS = 4;
var aHousesData :array[MIN_PLAYERS..MAX_PLAYERS] of array of array[0..2] of Integer;
	aRoadData :array[MIN_PLAYERS..MAX_PLAYERS] of array[0..2] of array of Integer;
	KeepBuildings, SetAutoRoads, AutoFields : array[MIN_PLAYERS..MAX_PLAYERS] of Boolean;
	PlanIndex, LastBuilded, LastPlanSetTime : array[MIN_PLAYERS..MAX_PLAYERS] of Integer;
	AutoRoads : Boolean;
	
Procedure SetHousesData;
var I, LEA, aType, arrayLength, RoadLength, { WineLength, CornLength, } aX, aY,PosX,PosY :Integer;
	aHouses : array of Integer;
begin
	for I := MIN_PLAYERS to MAX_PLAYERS do
	If (States.PlayerIsAI(I) = true) and (KeepBuildings[I] = false) then
	begin		
		arrayLength := 0;
		RoadLength := 0;
		aHouses := States.PlayerGetAllHouses(I);
		for LEA := 1 to length(aHouses)-1 do
		begin
			
			aType := States.HouseType(aHouses[LEA])
			arrayLength:=length(aHousesData[I])
			SetLength(aHousesData[I], arrayLength+1)
			PosX:= States.HousePositionX(aHouses[LEA]);
			PosY:= States.HousePositionY(aHouses[LEA]);
			aHousesData[I][arrayLength][0] := PosX;
			aHousesData[I][arrayLength][1] := PosY;
			aHousesData[I][arrayLength][2] := aType;
			
			for aX := 0 to 8 do
				for aY := 0 to 8 do
				begin
					If (States.IsRoadAt(I,PosX - 4 + aX,PosY - 4 + aY)) and (States.HouseAt(PosX - 4 + aX,PosY - 4 + aY) <=0) then
					If States.UnitAt(PosX - 4 + aX,PosY - 4 + aY)<=0 then 
					begin				
						SetLength(aRoadData[I][0], RoadLength+1);
						SetLength(aRoadData[I][1], RoadLength+1);
						SetLength(aRoadData[I][2], RoadLength+1);
						aRoadData[I][0][RoadLength]:= PosX - 4 + aX;
						aRoadData[I][1][RoadLength]:= PosY - 4 + aY;
						RoadLength := RoadLength + 1
						Actions.RemoveRoad(PosX - 4 + aX,PosY - 4 + aY);
					end;					
				end;
			Actions.HouseDestroy(aHouses[LEA],true);
						
		end;
	end;	
end;

Procedure OnHouseBuilt(aHouse : Integer);
var aOwner : Integer;
begin
	aOwner := States.HouseOwner(aHouse);
	If aOwner <> 0 then
	begin
		If States.HouseType(aHouse) = 13 then 
			If States.StatHouseTypeCount(aOwner,13) = 1 then 
				PlanIndex[aOwner] := PlanIndex[aOwner]+1;
			
		LastBuilded[aOwner] := LastBuilded[aOwner] + 1
	end;
end;
	
Procedure SetHousesPlans(I:Integer);
var aX, aY, RLE :Integer;
	aSecPos : TKMPoint;
begin
	If length(aHousesData[I]) > 0 then 
	begin
		for aX := 0 to 6 do 
			for aY := 0 to 6 do 
				If U.InRangeI(States.MapTileObject(aHousesData[I][PlanIndex[I]][0]-3+aX,aHousesData[I][PlanIndex[I]][1]-3+aY), 88, 253) then
					Actions.MapTileObjectSet(aHousesData[I][PlanIndex[I]][0]-3+aX,aHousesData[I][PlanIndex[I]][1]-3+aY,255);
		
		aSecPos := States.AIStartPosition(I);
		Actions.PlanAddHouse(I,aHousesData[I][PlanIndex[I]][2],aHousesData[I][PlanIndex[I]][0],aHousesData[I][PlanIndex[I]][1]);
		
		If length(aRoadData[I][0]) > 0 then 
			for RLE := 0 to length(aRoadData[I][0])-1 do 
				If aRoadData[I][2][RLE] = 0 then
					If U.InAreaI(aRoadData[I][0][RLE],aRoadData[I][1][RLE],aHousesData[I][PlanIndex[I]][0]-5,aHousesData[I][PlanIndex[I]][1]-5,aHousesData[I][PlanIndex[I]][0]+5,aHousesData[I][PlanIndex[I]][1]+5) then 
					begin
						Actions.PlanAddRoad(I,aRoadData[I][0][RLE],aRoadData[I][1][RLE]);
						aRoadData[I][2][RLE] := 1;
					end;
		
		If AutoRoads or (PlanIndex[I] <= 1) then
			A.PlanConnectRoad(I,aHousesData[I][PlanIndex[I]][0],aHousesData[I][PlanIndex[I]][1]+1,aSecPos.X,aSecPos.Y, false);
		
				
		If PlanIndex[I] = length(aHousesData[I])-1 then	
			LastPlanSetTime[I] := U.FloorTo(States.GameTime,100) + 600;
		If States.StatHouseTypeCount(I,13) >=1 then 
			PlanIndex[I]:= PlanIndex[I] + 1;
	end;	
end;

Procedure CheckRoadPlans(I:Integer);
var LEA :Integer;
	aSecPos : TKMPoint;
begin
	aSecPos := States.AIStartPosition(I);
	If length(aHousesData[I]) > 0 then 
	begin
		for LEA := 1 to length(aHousesData[I])-1 do
		A.PlanConnectRoad(I,aHousesData[I][LEA][0],aHousesData[I][LEA][1]+1,aSecPos.X,aSecPos.Y, false);
	end;	
end;
Procedure CheckRoadPlansToLastPlan(I:Integer);
var LEA,aX,aY,MIN :Integer;
	aSecPos : TKMPoint;
begin
	aSecPos := States.AIStartPosition(I);
	MIN := PlanIndex[I]-5;
	if MIN < 0 then MIN := 0;
	If length(aHousesData[I]) > 0 then 
	begin
		for LEA := MIN to PlanIndex[I]-1 do
		begin
			A.PlanConnectRoad(I,aHousesData[I][LEA][0],aHousesData[I][LEA][1]+1,aSecPos.X,aSecPos.Y, false);
			
			If AutoFields[I] and (LEA > 1) then 
			begin
				If aHousesData[I][LEA-1][2] = 8 then 
				for aX := 0 to 5 do 
					for aY := 0 to 5 do 
						Actions.PlanAddField(I,aHousesData[I][LEA-1][0]-2+aX,aHousesData[I][LEA-1][1]-3+aY);
				If aHousesData[I][LEA-1][2] = 28 then 
				for aX := 0 to 2 do 
					for aY := 0 to 2 do 
						Actions.PlanAddWineField(I,aHousesData[I][LEA-1][0]-3+aX,aHousesData[I][LEA-1][1]-2+aY);
			end; 
		end;	
	end;	
end;

procedure AIBOnTick;
var K : Integer;
begin		
	If States.GameTime mod 100 = 0 then 		
		for K := MIN_PLAYERS to MAX_PLAYERS do
		begin
			If States.PlayerIsAI(K) and not KeepBuildings[K] then 
			begin
				If ((SetAutoRoads[K]) and (PlanIndex[K] >= LastBuilded[K] + MAX_HOUSE_PLANS)) or (States.GameTime mod 600 = 0) then 
					begin
					SetAutoRoads[K] := false;
					If length(aRoadData[K][0]) = 0 then
						CheckRoadPlansToLastPlan(K);
				end;
				If (LastPlanSetTime[K] = 0) and (PlanIndex[K] < LastBuilded[K] + MAX_HOUSE_PLANS) and (PlanIndex[K] < length(aHousesData[K])) then 
				begin{ 
					If States.GameTime = 50 then 
						SetHousesPlans(K);
					If States.GameTime = 100 then 
						SetHousesPlans(K); }
					If States.GameTime mod 100 = 0 then 
						SetHousesPlans(K);
						
					SetAutoRoads[K] := true;
				end;
			end;
			If States.GameTime = LastPlanSetTime[K] then CheckRoadPlans(K);
		end;
	//If States.GameTime = 6000 then 
	//	HousesToBuild[6] := HousesToBuild[6]+4;
end;
Procedure AIBOnMissionStart;
begin
	KeepBuildings[3] := true;
	KeepBuildings[4] := true;
	KeepBuildings[5] := false;
	SetHousesData;
	AutoRoads := false;
	AutoFields[5] := true;
end;
