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

{$I HKS_Functions.script}
{$I HKS_SaveCData.script}

var EVENT_DefeatBarons, EVENT_BlackHole : KD_Event;
	PlayersToDefeat, WaresToGather : array of Integer;
	
function WareTypeToQuestCount(aType: integer): integer;
begin
	case aType of
		10: RESULT := 70;
		13: RESULT := 120;
		8: RESULT := 60;
		16: RESULT := 18;
		18: RESULT := 15;
		20: RESULT := 27;
		25: RESULT := 40;
		else RESULT := 0; 
	end;
end;


procedure CheckDefeat;
var I : Integer;
	canWin : Boolean;
begin
	canWin := true;
	for I := 0 to high(PlayersToDefeat) do 
		If (S.StatArmyCount(PlayersToDefeat[I]) > 0) or (S.StatHouseMultipleTypesCount(PlayersToDefeat[I], [11, 13, 21]) > 0) then 
			canWin := false;
	If canWin then
	begin
		FinEv(EVENT_DefeatBarons);
		EVENT_DefeatBarons.aTime := GT + 36000;
	end;
end;

procedure ShowObjectives;
var WARES : array[0..27] of Integer;
	I, J : Integer;
	canWin : Boolean;
begin
	canWin := true;
	
	with HKS_Players[PLAYER].Houses[11] do 
		If length(IDs) > 0 then 
			for I := 0 to high(IDs) do 
				for J := 0 to high(WaresToGather) do 
					WARES[J] := WARES[J] + S.HouseResourceAmount(IDs[I], WaresToGather[J]);
					
	with HKS_Players[PLAYER].Houses[21] do 
		If length(IDs) > 0 then 
			for I := 0 to high(IDs) do 
				for J := 0 to high(WaresToGather) do 
					WARES[J] := WARES[J] + S.HouseResourceAmount(IDs[I], WaresToGather[J]);
	If EVENT_DefeatBarons.aTime - GT < 0 then 
		A.PlayerDefeat(0);
		
	A.OverlayTextSet(PLAYER, '<$2> : ' + U.TimeToString(EVENT_DefeatBarons.aTime - GT));
	
	for J := 0 to high(WaresToGather) do 
		If WareTypeToQuestCount(WaresToGather[J]) > 0 then
		begin
				A.OverlayTextAppend(PLAYER, ' |');
			If WARES[J] < WareTypeToQuestCount(WaresToGather[J]) then 
				canWin := false
			else
				A.OverlayTextAppend(PLAYER, '[$00ff00]');
			A.OverlayTextAppendFormatted(PLAYER, ' %s[$ffffff] : %d / %d ', [S.WareTypeName(WaresToGather[J]), WARES[J], WareTypeToQuestCount(WaresToGather[J])])
				
		end;
	If canWin then 
		A.PlayerWin([0], true);
		
end;
procedure KillPlayersGroup;
var I : Integer;
	aArray : array of Integer;
begin
	aArray := S.PlayerGetAllGroups(PLAYER);
	for I := 0 to high(aArray) do 
		A.GroupKillAll(aArray[I], false);
end;


procedure OnTick;
begin
	OnTickHero;
	If gCustomLocation.StoreBuilded then 
		If GT mod 10 = 0 then 
		begin
			If not EVENT_DefeatBarons.Finished then 
					CheckDefeat
			else
				ShowObjectives;
			with EVENT_BlackHole do 
			If not finished then 
				If GT > aTime then 
					case EvNumber of 
						0 : If aHeros[0].Pos = U.KMPoint(160, 81) then 
							begin
								inc(EvNumber);
								A.ShowMSG(0, 'Hello there!')
								aTime := GT + 100;
							end;
						1 : begin
								inc(EvNumber);
								A.ShowMSG(0, 'Uhh... That is the part when you say "General Kenobi"!')
								aTime := GT + 600;
							end;
						2 : If aHeros[0].Pos = U.KMPoint(160, 81) then 
							begin
								inc(EvNumber);
								A.ShowMSG(0, 'Leave me alone! I do not want you to have any troubles.')
								aTime := GT + 600;
							end;
						3 : If aHeros[0].Pos = U.KMPoint(160, 81) then 
							begin
								inc(EvNumber);
								A.ShowMSG(0, 'The door is locked!!!');
								aTime := GT + 1200;
							end;
						4 : If aHeros[0].Pos = U.KMPoint(160, 81) then 
							begin
								inc(EvNumber);
								A.ShowMSG(0, 'What is wrong with you???');
								aTime := GT + 300;
							end;
						5 : If aHeros[0].Pos = U.KMPoint(160, 81) then 
							begin
								inc(EvNumber);
								A.ShowMSG(0, 'Ok. King Dyron got mad right now!');
								aTime := GT + 200;
							end;
						6 : If aHeros[0].Pos = U.KMPoint(160, 81) then 
							begin
								inc(EvNumber);
								A.ShowMSG(0, 'I am a master of all scripters. Do you know what I can do with scripts?');
								aTime := GT + 800;
							end;
						7 : If aHeros[0].Pos = U.KMPoint(160, 81) then 
							begin
								inc(EvNumber);
								A.ShowMSG(0, 'Ok. Seriously just go away and finish the map.');
								aTime := GT + 400;
							end;
						8 : If aHeros[0].Pos = U.KMPoint(160, 81) then 
							begin
								inc(EvNumber);
								A.ShowMSG(0, 'Enough!!! You made me do it!!!');
								aTime := GT + 100;
							end;
						9 :begin
								inc(EvNumber);
								KillPlayersGroup;
								A.ShowMSG(0, 'Poof... You have just lost your army.');
								aTime := GT + 400;
							end;
						10 : begin
								inc(EvNumber);
								A.ShowMSG(0, 'Oh, Player is sad now...');
								aTime := GT + 400;
							end;
						11 : If aHeros[0].Pos = U.KMPoint(160, 81) then 
							begin
								inc(EvNumber);
								A.ShowMSG(0, 'Ehh. Ok. Sorry for that. I do not want to be a villain, so here is your reward. I am curios why did you stay nearby this tunnel so long. Btw I made your hero really strong.');
								SetHeroStats(54, 60, 793, 7, 35, 0, hcMathew);
								aHeros[0].RegPoints := 2;
								aHeros[0].FeedHero := true;
								aTime := GT + 100;
								FinEv(EVENT_BlackHole);
							end;
					end;
		end;
end;


procedure OnMissionStart;
begin
	EvCreate(EVENT_DefeatBarons, 'Defeat barons', '<$1>');
	EvCreate(EVENT_BlackHole, 'The Tunnel', '');
	//EVENT_DefeatBarons.Finished := true;
	//EVENT_DefeatBarons.aTime := GT + 36000;
	gCustomLocation.TimeToSelect := 1800;
	gCustomLocation.Radius := 9;
	gCustomLocation.LocationPoses := [U.KMPoint(114, 184), U.KMPoint(62, 134), U.KMPoint(150, 110), U.KMPoint(49, 169) ];
	AddOvMessage(0, 20);
	WaresToGather := [8, 10, 13, 16, 18, 20, 25];
	PlayersToDefeat := [2, 3, 4];
	
	
	AddHero(S.GroupAt(49,189));
	SetHeroStats(42, 25, 189, 11, 14, 0, hcMathew);
	
end;