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

kills = 0;

elf_spells = {1, 2, 3, 4, 5, 6, 24, 39, 237};
human_spells = {1, 2, 3, 4, 11, 12, 23, 24, 25, 26, 28, 39, 42, 237};

if GetDifficulty() == DIFFICULTY_EASY then
	diff = 0;
	print("easy");
end;

if GetDifficulty() == DIFFICULTY_NORMAL then
	diff =  1;
	print("normal");
end;

if GetDifficulty() == DIFFICULTY_HARD then
	diff = 2;
	print("hard");
end;

if GetDifficulty() == DIFFICULTY_HEROIC then
	diff = 3;
	for i=2,4 do
		DenyAIHeroesFlee(i,1);
	end;
	print("heroic");
end;

function map_up()
	ChangeHeroStat("Inagost", 0, 3000 + 2000 * diff);
	ChangeHeroStat("Brem", 0, 3000 + 3000 * diff);
	SetRegionBlocked("block1", 1, 2);
	SetRegionBlocked("block2", 1, 2);
	SetRegionBlocked("block1", 1, 3);
	SetRegionBlocked("block2", 1, 3);
	if diff == 2 then
		for i, spell in elf_spells do
			if random(3) == 0 then
				TeachHeroSpell("Inagost", spell);
			end;
		end;
	end;
	if diff == 3 then
		for i, spell in elf_spells do
			if random(2) == 0 then
				TeachHeroSpell("Inagost", spell);
			end;
		end;
		for i, spell in human_spells do
			if random(2) == 0 then
				TeachHeroSpell("Brem", spell);
			end;
		end;
	end;
end;

function LooseHome ()
	SetObjectiveState("DefendHome", OBJECTIVE_FAILED);
	sleep (10);
	Loose (); 
end;

function LostHero( HeroName )
	if ( HeroName == "Eruina" ) then
		SetObjectiveState("Survival", OBJECTIVE_FAILED);
		sleep (10);
		Loose();
	end;
end;

function LostHero5( HeroName5 )
	if HeroName5 == "Inagost" then
		StartDialogScene("/DialogScenes/Single/SM2/R3/DialogScene.xdb#xpointer(/DialogScene)");
		Trigger( PLAYER_REMOVE_HERO_TRIGGER, 2, nil );
		kills = kills + 1;
	end;
	if HeroName5 == "Brem" then
		Trigger( PLAYER_REMOVE_HERO_TRIGGER, 3, nil );
		kills = kills + 1;
	end;
end;

function Objective1()
	while 1 do 
	  sleep (10);
	    if GetObjectOwner("Haven") == 1 and GetObjectOwner("Dungeon") == 1 and kills == 2 then
			SetObjectiveState('Start', OBJECTIVE_COMPLETED);
			DeployReserveHero ('Nadaur', RegionToPoint('EnemyHere'));
			sleep (2);
			LevelUpHero("Eruina");
			startThread(AddTroops);
			MoveHero('Nadaur', RegionToPoint('Enter'));
			sleep (5);
			SetObjectiveState('Final', OBJECTIVE_ACTIVE); 
			startThread(FinalBattle);
			break
		end;
		if GetDate(MONTH) == 4 then
			SetObjectiveState('Start', OBJECTIVE_FAILED);
			DeployReserveHero ('Nadaur', RegionToPoint('EnemyHere'));
			sleep (2);
			startThread(AddTroops);
			EnableHeroAI('Nadaur', false);	
			MoveHero('Nadaur', RegionToPoint('EnemyHere1'));
			sleep (5);
			SetObjectiveState('Final', OBJECTIVE_ACTIVE); 
			startThread(FinalBattle);
			break
	    end;
    end;
end;

function AddTroops ()
	for i, spell in human_spells do
		if random(5-diff) == 0 then
			TeachHeroSpell("Nadaur", spell);
		end;
	end;
	if diff == 3 then
		SetHeroLootable("Nadaur", nil);
		GiveArtefact("Nadaur", 4);
		GiveArtefact("Nadaur", 51);
		GiveArtefact("Nadaur", 21);
		GiveArtefact("Nadaur", 22);
		GiveArtefact("Nadaur", 62);
		GiveArtefact("Nadaur", 27);
		GiveArtefact("Nadaur", 36);
		GiveArtefact("Nadaur", 16);
		GiveArtefact("Nadaur", 25);
		GiveArtefact("Nadaur", 11);
	end;
	X = (GetDate(MONTH) - 1)*4 + GetDate(WEEK) + diff;
	ChangeHeroStat('Nadaur', STAT_EXPERIENCE, 10000*X);
	AddObjectCreatures('Nadaur', CREATURE_SPRITE , 16*X);
	AddObjectCreatures('Nadaur', CREATURE_WAR_DANCER , 11*X);
	AddObjectCreatures('Nadaur', CREATURE_GRAND_ELF , 8*X);
	AddObjectCreatures('Nadaur', CREATURE_DRUID_ELDER , 4*X);
	AddObjectCreatures('Nadaur', CREATURE_WAR_UNICORN , 3*X);
	AddObjectCreatures('Nadaur', CREATURE_TREANT_GUARDIAN , 2*X);
	AddObjectCreatures('Nadaur', CREATURE_GOLD_DRAGON , 1*X);
	filmdata = 3;
	Trigger( NEW_DAY_TRIGGER , "film" );
end;

function film()
	filmdata = filmdata - 1;
	if filmdata == 0 then
		StartDialogScene("/DialogScenes/Single/SM2/R2/DialogScene.xdb#xpointer(/DialogScene)");
		Trigger( NEW_DAY_TRIGGER , nil );
		ChangeHeroStat('Nadaur', 8, 10 * GetHeroStat('Nadaur', 4) - GetHeroStat('Nadaur', 8));
	end;
end;

function FinalBattle ()
	Trigger( PLAYER_REMOVE_HERO_TRIGGER, PLAYER_4, "LostHero2" );
	function LostHero2( HeroName )
		if ( HeroName == "Nadaur" ) then
			StartDialogScene("/DialogScenes/Single/SM2/R6/DialogScene.xdb#xpointer(/DialogScene)");
			SetObjectiveState("Final", OBJECTIVE_COMPLETED);
			sleep (10);
			Win (0);
		end;
	end;
end;

SetObjectiveState('Start', OBJECTIVE_ACTIVE); 
Trigger( PLAYER_REMOVE_HERO_TRIGGER, 1, "LostHero" );
Trigger( PLAYER_REMOVE_HERO_TRIGGER, 2, "LostHero5" );
Trigger( PLAYER_REMOVE_HERO_TRIGGER, 3, "LostHero5" );
Trigger (OBJECT_CAPTURE_TRIGGER, 'Home', 'LooseHome');
startThread(Objective1);
startThread(map_up);