{$I AIBuilding.script}

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

{$I HKS_Functions.script}

{$I HKS_SaveCData.script}


var RandomDelation, CoalDeliveryTime : Integer;
	EVENT_CastleRuins : KD_Event;
	
procedure CastleRuins;
begin
	If S.StatHouseTypeCount(PLAYER, 21) > 0 then 
	begin
		A.GiveWeapons(PLAYER, 17, 30);
		A.GiveWeapons(PLAYER, 19, 30);
		A.GiveWeapons(PLAYER, 21, 30);
	end else
	begin
		A.GiveWares(PLAYER, 17, 30);
		A.GiveWares(PLAYER, 19, 30);
		A.GiveWares(PLAYER, 21, 30);
	
	end;
end;
procedure OnTick;
begin
	      If States.GameTime = 10 then
begin
     Actions.OverlayTextSetFormatted(-1, '1', []);
     Actions.OverlayTextAppendFormatted(-1, '<$1>', []);
     A.PlayOgg(0, '01', 1);
end;
     If States.GameTime = 900 then
begin
   Actions.OverlayTextSetFormatted(-1, '2', []);
   Actions.OverlayTextAppendFormatted(-1, '<$2>', []);
   A.PlayOgg(0, '02', 1);
end;
     If States.GameTime = 54000 then
begin
   Actions.OverlayTextSetFormatted(-1, '3', []);
   Actions.OverlayTextAppendFormatted(-1, '<$3>', []);
   A.PlayOgg(0, '03', 1);
end;
	
	//coal deliver ~30 mins
	If GT = CoalDeliveryTime then // GT is my function that result is S.GameTime
	begin
		Actions.OverlayTextSetFormatted(-1, '4', []);
   Actions.OverlayTextAppendFormatted(-1, '<$4>', []);
   A.PlayOgg(0, '04', 1);
		A.GiveWares(0, 5, 600)
	end;
	If GT mod 50 <> 0 then 
		Exit;
		
	If EVENT_CastleRuins.Finished then
		Exit;
		
	If ( (S.UnitAt(43, 89) > 0) and (S.UnitOwner(S.UnitAt(43, 89)) = 0)) or ( (S.UnitAt(48, 92) > 0) and (S.UnitOwner(S.UnitAt(48, 92)) = 0)) or ( (S.UnitAt(43, 93) > 0) and (S.UnitOwner(S.UnitAt(43, 93)) = 0)) then 
		FinEv(EVENT_CastleRuins);
	
end;

procedure OnMissionStart;
begin
	RandomDelation := S.KamRandomI(6000);//number picked randomly to adjust to coal delivery time
	CoalDeliveryTime := 17000 + RandomDelation;
	
	EvCreate(EVENT_CastleRuins, 'Ruins bonus', '<$5>');
	EVENT_CastleRuins.DoAtEnd := @CastleRuins;
	
	//SetDifficulty_EquipDelay([2], [0], [600], [2, 1, 0.01] );//equip delay for difficulty setup
	
end;