
var ArmyDefeated : Boolean;
	ArmyDefeatedTime : integer;

{$I HKS_SaveCData.script}

procedure OnWarriorEquipped(UnitID,aGroupID: Integer);
var aBarracks, newUnit : Integer;
	aX, aY, aDir, aType, aOwner : integer;
begin
	aX := States.UnitPositionX(UnitID);
	aY := States.UnitPositionY(UnitID);
	aDir := States.UnitDirection(UnitID);
	aType := States.UnitType(UnitID);
	aOwner := States.UnitOwner(UnitID);
	aBarracks := States.HouseAt(aX,aY-1);	
	if States.HouseRepair(aBarracks) then 
	begin
		If aType = 14 then
			If States.HouseResourceAmount(aBarracks,20) >= 5 then
			begin
				Actions.HouseTakeWaresFrom(aBarracks,20,5)
				Actions.UnitKill(UnitID, true);
				Actions.GroupOrderLink(A.GiveGroup(aOwner,23,aX,aY,aDir,1,1),aGroupID);
				Actions.GroupOrderWalk(aGroupID,S.HouseBarracksRallyPointX(aBarracks),S.HouseBarracksRallyPointY(aBarracks),aDir);
			end;
		If aType = 16 then
			If States.HouseResourceAmount(aBarracks,21) >= 2 then
			begin
				Actions.HouseTakeWaresFrom(aBarracks,21,2)
				A.HouseAddWaresTo(aBarracks,17,1)
				Actions.UnitKill(UnitID, true);
				Actions.GroupOrderLink(A.GiveGroup(aOwner,26,aX,aY,aDir,1,1),aGroupID);
				Actions.GroupOrderWalk(aGroupID,S.HouseBarracksRallyPointX(aBarracks),S.HouseBarracksRallyPointY(aBarracks),aDir);
			end;
		If aType = 19 then
			begin
				Actions.HouseAddWaresTo(aBarracks,18,1)
				Actions.UnitKill(UnitID, true);
				Actions.GroupOrderLink(A.GiveGroup(aOwner,24,aX,aY,aDir,1,1),aGroupID);
				Actions.GroupOrderWalk(aGroupID,S.HouseBarracksRallyPointX(aBarracks),S.HouseBarracksRallyPointY(aBarracks),aDir);
			end;
		If aType = 21 then
			begin
				Actions.HouseAddWaresTo(aBarracks,18,1)
				Actions.HouseAddWaresTo(aBarracks,16,1)
				Actions.UnitKill(UnitID, true);
				Actions.GroupOrderLink(A.GiveGroup(aOwner,27,aX,aY,aDir,1,1),aGroupID);
				Actions.GroupOrderWalk(aGroupID,S.HouseBarracksRallyPointX(aBarracks),S.HouseBarracksRallyPointY(aBarracks),aDir);
			end;
		If aType = 17 then
			begin
				Actions.HouseAddWaresTo(aBarracks,18,1)
				Actions.UnitKill(UnitID, true);
				Actions.GroupOrderLink(A.GiveGroup(aOwner,25,aX,aY,aDir,1,1),aGroupID);
				Actions.GroupOrderWalk(aGroupID,S.HouseBarracksRallyPointX(aBarracks),S.HouseBarracksRallyPointY(aBarracks),aDir);
			end;
	end;	
end;

function GetRandomPos(aSingle : Single) : TKMPoint;
begin

	case aSingle of 
	0..0.3 : Result := U.KMPoint(1, 97 + S.KamRandomI(20));
	0.31..0.6 : Result := U.KMPoint(127, 83 + S.KamRandomI(19));
	0.61..1 : Result := U.KMPoint(57, 127 + S.KamRandomI(33));
	end;
end;
procedure BlockAllHouses;
var aArray : array of Integer;
	I, K : Integer;
	aPos : TKMPoint;
begin
	for I := 1 to 4 do 
	begin
		aArray := S.PlayerGetAllHouses(I);
		If Length(aArray) = -1 then 
			Continue;
		for K := 0 to high(aArray) do
			A.HouseDeliveryBlock(aArray[K], true);
	end;
end;

procedure MakeCitizensWalk;
var aArray : array of Integer;
	I, K : Integer;
	aPos : TKMPoint;
begin
	BlockAllHouses;
	for I := 1 to 4 do 
	begin
		aArray := S.PlayerGetAllUnits(I);
		If Length(aArray) > 0 then 
			for K := 0 to high(aArray) do
			begin
				aPos := GetRandomPos(S.KamRandom);
				A.UnitOrderWalk(aArray[K], aPos.X, aPos.Y);
			end;
	end;
end;

function CheckCitizensIdle : Boolean;
var aArray : array of Integer;
	I, K : Integer;
	aPos : TKMPoint;
begin
	Result := true;
	for I := 1 to 4 do 
	begin
		aArray := S.PlayerGetAllUnits(I);
		If Length(aArray) = -1 then 
			Continue;
		for K := 0 to high(aArray) do
			If not S.UnitIdle(aArray[K]) then 
				Result := false
			else
				A.UnitKill(aArray[K], true);
	end;
	//If not Result then 
	//	MakeCitizensWalk;
end;
procedure KillArmy;
var I, K : Integer;
	J :array of Integer;
begin
	for I := 1 to 4 do 
	begin
		J := S.PlayerGetAllGroups(I);
		If length(J) = 0 then 
			Continue;
			
		for K := 0 to high(J) do 
			A.GroupKillAll(J[K], false);
	end;
end;
Procedure OnTick;
begin
	If S.GameTIme = 100 then 
		KillArmy;
  if States.GameTime = 10 then
	Actions.ShowMsg(0, '<$1>');
//Message After 2 Minutes
  if States.GameTime = 60 then
	Actions.ShowMsg(0, '<$2>');
//Message After 5 Minutes
  if States.GameTime = 6000 then
	Actions.ShowMsg(0, '<$3>');
	
	If S.GameTime = ArmyDefeatedTime + 10 then 
		MakeCitizensWalk;
		
	If S.GameTIme mod 100 = 0 then 
	begin
		If ArmyDefeated then
		begin
			If CheckCitizensIdle then 
				A.PlayerWin([0], true);
				
			Exit;
		end;
		If (S.StatArmyCount(1) = 0 )
		and (S.StatArmyCount(2) = 0 )
		and (S.StatArmyCount(3) = 0 )
		and (S.StatArmyCount(4) = 0 ) then
		begin
			A.ShowMsg(-1, 'Defeated - the enemy citizens a runing away')//do zmienienia przez kamrata
			ArmyDefeated := true;
			MakeCitizensWalk;
			ArmyDefeatedTime := S.GameTime;
		end;
	
	
	end;
	
	
end;