{$I HKS_SaveCData.script}

{.$DEFINE FEED}
{$DEFINE RES_HOUSES}
{.$DEFINE PATROL}
{.$DEFINE DIFFICULTY}
{.$DEFINE ANIMATION}

{$I HKS_Functions.script}

var EVENT_RebuildVillage, EVENT_Watchtower : KD_Event;

procedure DoWatchTower;
var aArray : array of Integer;
	
begin
	aArray := S.PlayerGetAllGroups(PLAYER);
	If length(aArray) > 1 then 
	begin
		A.GroupKillAll(aArray[0], true);
		A.GroupKillAll(aArray[1], true);
	end;
	CampaignData.WatchTowerVisited := true;
end;

procedure RebuildVillage;
var I : integer;
begin
	If S.HouseAt(20, 61) = -1 then 
		Exit;
		
	A.HouseAddRepair(S.HouseAt(20, 61), 600);
	for I := 0 to 2 do
		A.GiveUnit(1, 0, 20, 63, 4);
	for I := 0 to 3 do
		A.GiveUnit(1, 9, 20, 65, 4);
	
	If A.GiveHouse(1, 9, 4, 54) > 0 then 
		A.GiveUnit(1, 1, 4, 55, 4);
		
	If A.GiveHouse(1, 8, 15, 70) > 0 then 
		A.GiveUnit(1, 4, 15, 71, 4);
end;

procedure OnBeacon(aPlayer, aX, aY : Integer);
var HH : KD_HouseStats;
begin
	HH := SetHouse(S.HouseAt(aX,aY), true);
	
	If HH.aOwner <> PLAYER then //check if owner of the house is a player
		Exit;
		
	If HH.aType <> 11 then //check if it's storehouse
		Exit;
		
	with HH do 
	If (Wares[1] >= 40) and (Wares[2] >= 40 )then 
	begin
		A.HouseTakeWaresFrom(aID, 1, 40);
		A.HouseTakeWaresFrom(aID, 2, 40);
		
		FinEv(EVENT_RebuildVillage);
		
	end else
		Actions.OverlayTextSetFormatted(-1, '5', []);
		 Actions.OverlayTextAppendFormatted(-1, '<$5>', []);
		
end;

procedure OnTick;
begin
  if States.GameTime = 10 then
	Actions.ShowMsg(0, '<$1>');
//Message After 3 Minutes
  if States.GameTime = 1800 then
	Actions.ShowMsg(0, '<$2>');
//Message After 105 Minutes
	if States.GameTime = 63000 then
		Actions.ShowMsg(0, '<$3>');
		
	If EVENT_Watchtower.Finished then 
		Exit;
	
	If S.UnitAt(115, 13) > 0 then
		FinEv(EVENT_Watchtower);
end;

procedure OnMissionStart;
begin
	EvCreate(EVENT_RebuildVillage, 'Rebuild Village', 'You have just rebuilded the village. You can now enjoy the help of your new ally.');
	EvCreate(EVENT_Watchtower, 'Visit the WatchTower', '<$6>');
	EVENT_Watchtower.DoAtEnd := @DoWatchTower;
	EVENT_Watchtower.OvMsg := [7, 8, 9, 10];// zbiór ID wiadomości które mają się wyświetlać po wykonaniu zadania. Jak jest więcej niż jedna wiadomość, to skrypt będzie je odtwarzał z opóźnieniem 10s
	
	EVENT_RebuildVillage.DoAtEnd := @RebuildVillage;
end;