StartDialogScene("/DialogScenes/Single/SM6/R1/DialogScene.xdb#xpointer(/DialogScene)")

if GetDifficulty() == DIFFICULTY_EASY then
	print ("easy");
	Diff = 0;
	ArmyMult = 0;
	DayOfInvasion = 19;
	RemoveObject('Manes');
end;

if GetDifficulty() == DIFFICULTY_NORMAL then
	print ("normal");
	Diff = 1;
	ArmyMult = 0;
	DayOfInvasion = 16;
	RemoveObject('Manes');
end;

if GetDifficulty() == DIFFICULTY_HARD then
	print ("hard");
	Diff = 2;
	ArmyMult = 1;
	DayOfInvasion = 14;
end;

if GetDifficulty() == DIFFICULTY_HEROIC then
	print ("heroic");
	Diff = 3;
	ArmyMult = 2;
	DayOfInvasion = 10;
	DenyAIHeroesFlee(2,1);
	AddObjectCreatures("DemonSiege", 28, 4);
end;

function map_up()
	sleep(1);
	SetRegionBlocked('DemonBorder1', not nil, 2);
	SetRegionBlocked('DemonBorder2', not nil, 2);
	SetRegionBlocked('ElfDwelling', not nil, 2);
	for res_count=0,5,1 do
		SetPlayerResource(1, res_count, 0);
	end;
	SetPlayerResource(1, 6, 15000 - 5000 * ArmyMult);
end;

function boss_demon(hero)
	if hero ~= 'Grok' then
		ChangeHeroStat(hero, 0, 1000 + 4000 * Diff);
		if Diff == 3 then
			AddHeroCreatures(hero, 28, ArmyMult);
			TeachHeroSpell(hero, 5);
			TeachHeroSpell(hero, 6);
			TeachHeroSpell(hero, 12);
		end;
		Trigger(PLAYER_ADD_HERO_TRIGGER, 2, nil);
	end;
end;

function CastleTaken()
	sleep (4);
	DemonThiefAppears();
end;

function DemonThiefAppears ()
		SetObjectiveState  ('prim2', OBJECTIVE_ACTIVE);
		DeployReserveHero('Grok', RegionToPoint('Portal'));
		if IsObjectExists('SukBarrier') then RemoveObject('SukBarrier'); end;
		if IsObjectExists('DemonBarrier') then RemoveObject('DemonBarrier'); end;
		if IsObjectExists('Druids') then RemoveObject('Druids'); end;
		sleep (5);
		EnableHeroAI('Grok', nil);
		if ArmyMult > 0 then
			AddHeroCreatures('Grok', CREATURE_PIT_FIEND, 1*ArmyMult); --- PIT FIENDS
			AddHeroCreatures('Grok', CREATURE_CERBERI, 4*ArmyMult); --- CERBERI
			AddHeroCreatures('Grok', CREATURE_NIGHTMARE, 2*ArmyMult); --- HELL CHARGERS
			AddHeroCreatures('Grok', CREATURE_HORNED_DEMON, 16*ArmyMult); --- HORNED DEMONS
			if Diff == 3 then
				AddHeroCreatures("Grok", 28, 1);
			end;
		end;
	BlockGame();
		x, y, z = RegionToPoint('BowPlace');
		OpenCircleFog(x, y, z, 9, 1);
		MoveCamera(x, y, z, 50, 3.14/4, 0);
		x, y, z = RegionToPoint('PortalCenter');
		OpenCircleFog(x, y, z, 3, 1);
		 MoveHeroRealTime ('Grok', RegionToPoint('BowPlace'));
		sleep (55);
	UnblockGame();
		Trigger (PLAYER_REMOVE_HERO_TRIGGER, 2, 'DemonThiefDie');		
end;

----------------------------DEMON-THIEF AND HIS RUN-------------------------------------
function DemonTakeBow (heroname)
   if heroname == 'Grok' then
	sleep (10);	
	print ('Demon take bow. TAKE BOW I SAD!');	
	sleep (5);
	StartDialogScene("/DialogScenes/Single/SM6/R2/DialogScene.xdb#xpointer(/DialogScene)")
	sleep (5);
	GiveArtefact('Grok', ARTIFACT_UNICORN_HORN_BOW,not nil);
	if IsObjectExists('Bow') then RemoveObject('Bow'); end;
	Trigger (REGION_ENTER_AND_STOP_TRIGGER, 'NearPortal', 'ComeToPortal')
	MoveHero('Grok', RegionToPoint ('InfernoNearHome'));
	Trigger (REGION_ENTER_AND_STOP_TRIGGER, 'InfernoNearHome','RoadToInfernoPartTwo')
	x, y, z = RegionToPoint('InfernoHome');
	OpenCircleFog(x, y, z, 5, 1);
	Trigger (REGION_ENTER_AND_STOP_TRIGGER, 'BowPlace', nil);
	ChangeHeroStat ('Orrin', STAT_EXPERIENCE, 2500);	
	if Diff < 2 then startThread(TriggerPlayer); end; -- zapusk threada pro zamedlenie
	end;
end;

function ComeToPortal (heroname)
	print ('Come to potral');
	if heroname == 'Grok' then
		print ('Disable grok');
		ChangeHeroStat ('Grok', STAT_MOVE_POINTS,-100000); --Stop GRok
		Trigger (REGION_ENTER_AND_STOP_TRIGGER, 'NearPortal', nil);
	end;
end;

function RoadToInfernoPartTwo (heroname) -- prohodim cherez garnizon
	if heroname == 'Grok' then
		SetRegionBlocked('DemonBorder2', nil, 2);
		sleep (3);
		MoveHero('Grok', RegionToPoint ('InfernoHome'));
		Trigger (REGION_ENTER_WITHOUT_STOP_TRIGGER, 'InfernoNearHome', nil)
	end;
end;

function BringBowToInferno (heroName)
	if (heroName == 'Grok') and HasArtefact('Grok', ARTIFACT_UNICORN_HORN_BOW) then
    StartDialogScene("/DialogScenes/Single/SM6/R4/DialogScene.xdb#xpointer(/DialogScene)")
	Loose ();
	end;
end;
----------------------------DEATH-OF-DEMON-THIEF-------------------------------------------
function DemonThiefDie (whodie, whydie)
	if whodie == 'Grok' then
		print ('GRok is dead');
		if IsObjectExists ('Bow') == not nil then
			MessageBox ("/Maps/SingleMissions/SM6/GrockIsDead.txt");
			print ('GRok died without bow');
			RemoveObject('Bow');
			GiveArtefact('Orrin', ARTIFACT_UNICORN_HORN_BOW,not nil);	
			TakeBow ();			
		elseif whydie == nil then
			print ('Grok was killed by mobs');
			MessageBox ("/Maps/SingleMissions/SM6/GrockIsDead.txt");
			GiveArtefact('Orrin', ARTIFACT_UNICORN_HORN_BOW,not nil);	
			TakeBow ();
		elseif whydie == 'Orrin' then
			print ('Grok was killed by Orrin');
			ChangeHeroStat ('Orrin', STAT_EXPERIENCE, 3500);
			TakeBow ();
			else print ('Grok was killed by other hero ', whydie);
				sleep (2);
				RemoveArtefact(whydie, ARTIFACT_UNICORN_HORN_BOW,not nil);	
				GiveArtefact('Orrin', ARTIFACT_UNICORN_HORN_BOW,not nil);	
				TakeBow ();
		end;
	end;		
end;

function TakeBow ()
    StartDialogScene("/DialogScenes/Single/SM6/R3/DialogScene.xdb#xpointer(/DialogScene)")
	sleep (2);
	SetObjectiveState  ('prim2', OBJECTIVE_COMPLETED);
	SetObjectiveState  ('prim3', OBJECTIVE_ACTIVE);
	SetObjectiveState  ('fake_objective', OBJECTIVE_ACTIVE); -- Fake obj needet to show final movie
	Trigger (OBJECTIVE_STATE_CHANGE_TRIGGER, 'prim3', 'DefeatAllDemons');
end;

function DefeatAllDemons ()
	print ('Defeat all demons')
	StartDialogScene("/DialogScenes/Single/SM6/R5/DialogScene.xdb#xpointer(/DialogScene)");
	SetObjectiveState  ('fake_objective', OBJECTIVE_COMPLETED); -- Fake obj needet to show final movie
end;

---------- DEMONIC INVASION - MESSAGE AND OPEN PATHS FOR DEMON HEROES -----
--ElvenCityUnderSiege = not nil;
function ElvenCitySiege ()
	if GetDate(DAY) == DayOfInvasion then
		MessageBox ("/Maps/SingleMissions/SM6/ElvenCitySiege.txt");
		x, y, z = RegionToPoint('ElvenCityCenter');
		OpenCircleFog(x, y, z, 7, 1);
		SetRegionBlocked('DemonBorder1', nil, 2);
		SetRegionBlocked('DemonBorder2', nil, 2);
		Trigger (NEW_DAY_TRIGGER, nil);
	end;	
end;
function ElvenCity (oldplayer, newplayer, hero)
	--	if GetObjectOwner ('SylvanBorder')==1 then
	if oldplayer == 2 and newplayer == 1 then
		MessageBox ("/Maps/SingleMissions/SM6/ElvenCitySiegeOver.txt");
		SetObjectOwner ('SylvanCastle', 1);
		--ElvenCityUnderSiege = false;
		Trigger (OBJECT_CAPTURE_TRIGGER, 'SylvanBorder', nil);
	end;
end;
---------------ZAMEDLENIE-GROKA-NA-UROVNE-SLOJNOSTI-NORMAL--------------------------------------

function TriggerPlayer()
	print("Grok neset artifact...");
	while IsObjectExists('Grok') do
--and HasArtefact('Grok', ARTIFACT_UNICORN_HORN_BOW) do
		print ('cicl');
		CurrentPlayer = GetCurrentPlayer();
		while CurrentPlayer == GetCurrentPlayer() do
			CurrentPlayer = GetCurrentPlayer();
			sleep(1);
		end;
		print("Player triggered");
		if CurrentPlayer == 1 then
			print("PLAYER'S 2 turn");
			startThread(HeroSlow);
			else print("PLAYER'S 1 turn");
		end;
		sleep(10);
	end;
end;

function HeroSlow ()
	print ('Slow');
	if IsObjectExists('Grok') then
--and GetCurrentPlayer() == 2 then
		print ('STOYAT GROK, YA SKAZAL STOYAT!');
		MovePoints = GetHeroStat('Grok', STAT_MOVE_POINTS);
		print ('old ', MovePoints);
		Delta = (MovePoints/4)*(-1);
		print ('delta ', Delta);
		ChangeHeroStat('Grok', STAT_MOVE_POINTS, Delta);
		sleep (1);
		MovePoints = GetHeroStat('Grok', STAT_MOVE_POINTS);
		print ('new ', MovePoints);
	end;
end;

Trigger (NEW_DAY_TRIGGER, 'ElvenCitySiege');
Trigger(PLAYER_ADD_HERO_TRIGGER, 2, "boss_demon");
Trigger (OBJECTIVE_STATE_CHANGE_TRIGGER, 'prim1', 'CastleTaken');
Trigger (REGION_ENTER_WITHOUT_STOP_TRIGGER, 'InfernoHome', 'BringBowToInferno');
Trigger (REGION_ENTER_AND_STOP_TRIGGER, 'BowPlace', 'DemonTakeBow')
Trigger (OBJECT_CAPTURE_TRIGGER, 'SylvanBorder', 'ElvenCity');
startThread(map_up);