{$I HKS_SaveCData.script}

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

{$I HKS_Functions.script}

var EVENT_Bridge : KD_Event;
	EvHouse : KD_HouseStats;
	
procedure OnHouseBuilt(aHouse : Integer);
var HH : KD_HouseStats;
begin
	If EVENT_Bridge.Finished then 
		Exit;
	HH := SetHouse(aHouse, false);
	
	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;
	If EvHouse.aID = -1 then
	begin
		If PointNearPoint(U.KMPoint(57, 6), HH.aPos, 10 ) then //check if store is near the bridge
		begin
			EvHouse := HH;
			A.ShowMsg(PLAYER, '<$3>')
		end;	
	end	else
		A.ShowMsg(PLAYER, 'You have storehouse near the bridge already');
end;

procedure OnHouseDestroyed(aHouse, aDestroyer : Integer);
begin
	If EVENT_Bridge.Finished then 
		Exit;
	If aHouse = EvHouse.aID then 
	begin
		EvHouse.aID := -1		
		A.ShowMsg(PLAYER, '4');
	end;
end;

procedure CheckBridgeEvent;
begin
	EvHouse := SetHouse(EvHouse.aID, true);
	with EvHouse do 
		If Wares[2] >= 30 then 
		begin
			FinEv(EVENT_Bridge);
			A.HouseTakeWaresFrom(aID, 2, 30);
		end;
end;

procedure RebuildBridge;
var I : Integer;
begin
	for I := 55 to 60 do 
	begin
		A.MapTileSet(I, 6, 350, 3);
		A.MapTileObjectSet(I, 6, 51);
		A.MapTileObjectSet(I, 7, 51);
	end;
	A.GroupOrderWalk(A.GiveGroup(PLAYER, 15, 61, 11, 0, 12, 4),49, 3, 6); 
	A.GroupOrderWalk(A.GiveGroup(PLAYER, 19, 62, 16, 0, 9, 3),46, 7, 6); 
	A.GroupOrderWalk(A.GiveGroup(PLAYER, 25, 57, 13, 0, 12, 4),41, 14, 5); 
end;

procedure OnTick;
begin
	if States.GameTime = 10 then
		Actions.ShowMsg(0, '<$1>');
	//Message After 4 Minutes
	if States.GameTime = 2400 then
		Actions.ShowMsg(0, '<$2>');
		
	If GT mod 50 <> 0 then 
		Exit;
		
	If EVENT_Bridge.Finished then 
		Exit;
		
	If EvHouse.aID > 0 then 
		CheckBridgeEvent;
end;


procedure OnMissionStart;
begin
	EvHouse.aID := -1;
	EvCreate(EVENT_Bridge, 'Bridge', '5')
	EVENT_Bridge.DoAtEnd := @RebuildBridge;
end;