---GLOBALS---
first_day=1;
power=0;
power1=1;
great_night_progress=0;
first_time=1;
light=0;
m = 1;
night_light = 0;

path = "C5M4_";
new_path = "C5M5_";
transfer_arts = {4, 13, 15, 21, 24, 48, 49, 50, 51};

dang_array = {"Nemor","Pelt","Straker","Tamika","Effig"};
heroic_spells = {7, 8, 9, 18, 19, 20, 40, 43, 235};
good_arts = {2, 4, 6, 7, 11, 13, 15, 17, 19, 21, 22, 23, 24, 25, 26, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 45, 46, 47, 57, 58, 59, 60, 63, 67, 68, 69, 70, 71};

SetCombatLight("/Lights/_(AmbientLight)/AdvMap/C5M5/c4m4_wastes.xdb#xpointer(/AmbientLight)");

---ARRAYS---
shadow_dragons = {"SD1", "SD2", "SD3", "SD4", "SD5", "SD6"};
regions={'sdregion1','sdregion2','sdregion3','sdregion4','sdregion5','sdregion6',"AI_block_1","AI_block_2","AI_block_3","AI_block_4","AI_blok","AI_block_5","AI_block_6","AI_blok","AI_blok1","AI_blok2","AI_blok3","AI_block_7","AI_block_8"}
--'portalregion'
towns={'town1','town2','town3','town4','town5','town6'}
respawns_x={37,84,41,160,95,26}
respawns_y={39,86,152,31,92,21}
respawns_z={GROUND,GROUND,GROUND,GROUND,UNDERGROUND,UNDERGROUND}
kolyan_army_normal={30,25,20,15}
kolyan_army_hard={70,60,40,35}
kolyan_army_heroic={100,75,60,55}

creatures_types={	CREATURE_SKELETON_ARCHER,
					CREATURE_ZOMBIE,
					CREATURE_GHOST,
					CREATURE_VAMPIRE_LORD,
					CREATURE_DEMILICH,
					CREATURE_WRAITH,
					CREATURE_SHADOW_DRAGON}

if GetDifficulty() == DIFFICULTY_EASY then
	print ("easy");
	dif = 0;
	exp = GetHeroStat("Heam", STAT_EXPERIENCE)/4;
	for i,h in dang_array do
		ChangeHeroStat(dang_array[i], STAT_EXPERIENCE , exp);
	end;
	for a = 0,6 do
		SetPlayerResource(2, a, 0);
	end;
	AddObjectCreatures("Heam", CREATURE_GRAND_ELF, 20);
end;

if GetDifficulty() == DIFFICULTY_NORMAL then
	print ("normal");
	dif = 0;
	exp = GetHeroStat("Heam", STAT_EXPERIENCE)/2;
	for i,h in dang_array do
		ChangeHeroStat(dang_array[i], STAT_EXPERIENCE , exp);
	end;
	for a = 0,6 do
		SetPlayerResource(2, a, 0);
	end;
end;

if GetDifficulty() == DIFFICULTY_HARD then
	print ("Hard");
	dif = 1;
	exp = GetHeroStat("Heam", STAT_EXPERIENCE);
	for i,h in dang_array do
		if h == "Pelt" or h == "Tamika" then
			ChangeHeroStat(dang_array[i], STAT_EXPERIENCE , 0.2 * exp);
			else ChangeHeroStat(dang_array[i], STAT_EXPERIENCE , exp);
		end;
		AddObjectCreatures(dang_array[i], 30 , 60);
		AddObjectCreatures(dang_array[i], 31 , 50);
		AddObjectCreatures(dang_array[i], 33 , 30);
	end;
end;

if GetDifficulty() == DIFFICULTY_HEROIC then
	print ("Impossible");
	dif = 2;
	exp = GetHeroStat("Heam", STAT_EXPERIENCE)*2;
	for i,h in dang_array do
		if h == "Pelt" or h == "Tamika" then
			ChangeHeroStat(dang_array[i], STAT_EXPERIENCE , 0.2 * exp);
			else ChangeHeroStat(dang_array[i], STAT_EXPERIENCE , exp);
		end;
		AddObjectCreatures(dang_array[i], 30 , 150);
		AddObjectCreatures(dang_array[i], 31 , 70);
		AddObjectCreatures(dang_array[i], 33 , 47);
		AddObjectCreatures(dang_array[i], 36 , 14 + random(11));
		AddObjectCreatures(dang_array[i], 38 , 7 + random(7));
		if random(3) == 0 then
			AddObjectCreatures(dang_array[i], 39 , 2 + random(5));
			else if random(2) == 0 then
				AddObjectCreatures(dang_array[i], 41 , 1 + random(4));
				else AddObjectCreatures(dang_array[i], 42 , 1 + random(3));
			end;
		end;
	end;
end;
---FUNCTIONS---
function map_up()
	local day = GetDate(DAY);
	sleep(1);
	DeployReserveHero('Nikolay',91,93,GROUND);
	SetGameVar("C5M5_night", 0);
	sleep(1);
	if dif > 0 then
		startThread(everythingToKolya);
		startThread(disable_hiring);
		startThread(no_morale_arts);
		AddHeroCreatures('Nikolay', 30, 1500*dif);
		for i=1,4 do
			ChangeHeroStat("Nikolay", i, 2+dif);
		end;
		for i=0,5 do
			SetPlayerResource(2, i, 50 + 100 * dif);
		end;
		SetPlayerResource(2, 6, 100000 * dif);
		DenyAIHeroesFlee(2,1);
		for i,h in dang_array do
			for j=1,4 do
				ChangeHeroStat(h, j, dif);
			end;
			if dif == 2 then
				startThread(teach_spell, h, heroic_spells, 2);
			end;
		end;
		for i, drag in shadow_dragons do
			startThread(add_army, drag, 0.5 * dif);
		end;
	end;
	if dif == 2 then
		local heroes = GetPlayerHeroes(1);
		for i, hero in heroes do
			SetHeroCombatScript(hero, '/Maps/Scenario/C5M5/C5M5-CombatScript.xdb#xpointer(/Script)');
		end;
		AddObjectCreatures("town5", 30, 250);
		AddObjectCreatures("town5", 31, 120);
		AddObjectCreatures("town5", 34, 60);
		AddObjectCreatures("town5", 39, 10);
		AddObjectCreatures("town6", 30, 350);
		AddObjectCreatures("town6", 32, 110);
		AddObjectCreatures("town6", 34, 50);
		AddObjectCreatures("town6", 41, 8);
		SetHeroCombatScript("Nikolay", '/Maps/Scenario/C5M5/C5M5-CombatScript.xdb#xpointer(/Script)');
		SetHeroLootable('Nikolay', nil);
		EnableHeroAI('Nikolay', nil);
		GiveHeroSkill('Nikolay', 1);
		GiveHeroSkill('Nikolay', 1);
		GiveHeroSkill('Nikolay', 1);
		GiveHeroSkill('Nikolay', 3);
		GiveHeroSkill('Nikolay', 3);
		GiveHeroSkill('Nikolay', 3);
		GiveHeroSkill('Nikolay', 6);
		GiveHeroSkill('Nikolay', 6);
		GiveHeroSkill('Nikolay', 6);
		GiveHeroSkill('Nikolay', 10);
		GiveHeroSkill('Nikolay', 10);
		GiveHeroSkill('Nikolay', 10);
		GiveHeroSkill('Nikolay', 12);
		GiveHeroSkill('Nikolay', 12);
		GiveHeroSkill('Nikolay', 12);
		GiveHeroSkill('Nikolay', 15);
		GiveHeroSkill('Nikolay', 15);
		sleep(1);
		GiveHeroSkill('Nikolay', 19);
		GiveHeroSkill('Nikolay', 26);
		GiveHeroSkill('Nikolay', 46);
		GiveHeroSkill('Nikolay', 36);
		GiveHeroSkill('Nikolay', 53);
		GiveHeroSkill('Nikolay', 62);
		GiveArtefact('Nikolay', 6);
		GiveArtefact('Nikolay', 71);
		GiveArtefact('Nikolay', 70);
		GiveArtefact('Nikolay', 33);
		TeachHeroSpell('Nikolay', 18);
		TeachHeroSpell('Nikolay', 19);
		TeachHeroSpell('Nikolay', 20);
		TeachHeroSpell('Nikolay', 40);
		TeachHeroSpell('Nikolay', 41);
		TeachHeroSpell('Nikolay', 235);
		TeachHeroSpell('Effig', 5);
		TeachHeroSpell('Effig', 6);
		if random(2) == 0 then
			startThread(death_knight, 'Nikolay');
		end;
	end;
	while GetDate(DAY) == day do
		sleep(5);
		if dif < 2 then
			break;
		end;
	end;
	day = GetDate(DAY);
	if dif == 2 then
		GiveHeroSkill('Nikolay', 47);
		GiveHeroSkill('Nikolay', 99);
		GiveHeroSkill('Nikolay', 101);
		GiveHeroSkill('Nikolay', 104);
		GiveHeroSkill('Nikolay', 108);
		GiveHeroSkill('Nikolay', 110);
	end;
	while GetDate(DAY) == day do
		sleep(5);
		if dif < 2 then
			break;
		end;
	end;
	day = GetDate(DAY);
	if dif == 2 then
		GiveHeroSkill('Nikolay', 143);
		GiveHeroSkill('Nikolay', 112);
	end;
	while GetDate(DAY) == day do
		sleep(5);
		if dif < 2 then
			break;
		end;
	end;
	if dif == 2 then
		GiveHeroSkill('Nikolay', 140);
		sleep(1);
		GiveHeroSkill('Nikolay', 121);
		sleep(1);
		GiveHeroSkill('Nikolay', 111);
	end;
	EnableHeroAI('Nikolay', not nil);
	print("release Kolya!");
	sleep(20);
	ChangeHeroStat('Nikolay', 0, 500000 + GetHeroStat("Heam", 0)*(0.5 + dif));
	while GetDate(DAY) == 4 do
		sleep(5);
		if dif < 2 then
			break;
		end;
	end;
	if dif == 2 then
		local h_stats_1 = GetHeroStat("Heam", 1) + GetHeroStat("Heam", 2);
		local h_stats_2 = GetHeroStat("Heam", 3) + GetHeroStat("Heam", 4);
		local k_stats_1 = GetHeroStat("Heam", 1) + GetHeroStat("Heam", 2);
		local k_stats_2 = GetHeroStat("Heam", 3) + GetHeroStat("Heam", 4);
		if h_stats_1 > k_stats_1 then
			print("Heam is overpowered, upgrading Kolya!");
			ChangeHeroStat("Nikolay", 1, 1 + (h_stats_1 - k_stats_1)/3 - mod(h_stats_1 - k_stats_1,3));
			ChangeHeroStat("Nikolay", 2, 2 + 2*(h_stats_1 - k_stats_1)/3 - mod(2*(h_stats_1 - k_stats_1),3));
			ChangeHeroStat("Nikolay", 3, 2 + (h_stats_2 - k_stats_2)/3 - mod(h_stats_2 - k_stats_2,3));
			ChangeHeroStat("Nikolay", 4, 1 + 2*(h_stats_2 - k_stats_2)/3 - mod(2*(h_stats_2 - k_stats_2),3));
		end;
	end;
end;

function everythingToKolya()
	local day = 0;
	while IsHeroAlive("Heam") do
		sleep(10);
		if GetDate(3) > random(6) and IsHeroAlive("Nikolay") then
			if IsObjectVisible(1, "Nikolay") == nil then
				for i=29,42 do
					if GetObjectCreatures("Pelt", i) > GetObjectCreatures("Nikolay", i) and ArmyCount("Nikolay") < 7 then
						AddObjectCreatures("Nikolay", i, GetObjectCreatures("Pelt", i));
						RemoveObjectCreatures("Pelt", i, GetObjectCreatures("Pelt", i));
					end;
					if GetObjectCreatures("Tamika", i) > GetObjectCreatures("Nikolay", i) and ArmyCount("Nikolay") < 7 then
						AddObjectCreatures("Nikolay", i, GetObjectCreatures("Tamika", i));
						RemoveObjectCreatures("Tamika", i, GetObjectCreatures("Tamika", i));
					end;
				end;
				for i, art in good_arts do
					if HasArtefact("Pelt", i) and HasArtefact("Nikolay", i) == nil then
						RemoveArtefact("Pelt", i);
						GiveArtefact("Nikolay", i);
					end;
				end;
				day = GetDate(1);
				while GetDate(1) == day do
					sleep(5);
				end;
			end;
		end;
	end;
end;

function disable_hiring()
	EnableAIHeroHiring(2, "town1", nil);
	EnableAIHeroHiring(2, "town2", nil);
	EnableAIHeroHiring(2, "town3", nil);
	EnableAIHeroHiring(2, "town5", nil);
	EnableAIHeroHiring(2, "town6", nil);
	while length(GetPlayerHeroes(2)) < 7 do
		sleep(5);
	end;
	EnableAIHeroHiring(2, "town4", nil);
	EnableAIHeroHiring(2, "town6", not nil);
	while length(GetPlayerHeroes(2)) < 8 do
		sleep(5);
	end;
	EnableAIHeroHiring(2, "town1", not nil);
	EnableAIHeroHiring(2, "town2", not nil);
	EnableAIHeroHiring(2, "town3", not nil);
	EnableAIHeroHiring(2, "town4", not nil);
	EnableAIHeroHiring(2, "town5", not nil);
end;

function no_morale_arts()
	local necros = {};
	while IsHeroAlive("Heam") do
		sleep(10);
		necros = GetPlayerHeroes(2);
		for i, hero in necros do
			if HasArtefact(hero, 16) then
				RemoveArtefact(hero, 16);
			end;
			if HasArtefact(hero, 31) then
				RemoveArtefact(hero, 31);
			end;
		end;
	end;
end;

function death_knight(hero)
	print("Hero "..hero.." will hire only death knight");
	local count = 0;
	while IsHeroAlive(hero) do
		sleep(5);
		count = GetHeroCreatures(hero, 39) + GetHeroCreatures(hero, 40)
		if count > 0 then
			RemoveObjectCreatures(hero, 39, count);
			RemoveObjectCreatures(hero, 40, count);
			sleep(1);
			AddHeroCreatures(hero, 90, count);
		end;
	end;
end;

function control_dwarf()
	local heroes = {};
	while not nil do
		sleep(4);
		heroes = GetPlayerHeroes(1);
		for i,hero in heroes do
			if HasArtefact(hero, 49) then
				SetGameVar("C5M5_"..hero.."_greaves", 1);
				else SetGameVar("C5M5_"..hero.."_greaves", 0);
			end;
			if HasArtefact(hero, 50) then
				SetGameVar("C5M5_"..hero.."_helmet", 1);
				else SetGameVar("C5M5_"..hero.."_helmet", 0);
			end;
			if HasArtefact(hero, 51) then
				SetGameVar("C5M5_"..hero.."_shield", 1);
				else SetGameVar("C5M5_"..hero.."_shield", 0);
			end;
			if KnowHeroSpell(hero, 31) then
				SetGameVar("C5M5_"..hero.."_antimagic", 1);
				else SetGameVar("C5M5_"..hero.."_antimagic", 0);
			end;
		end;
	end;
end;

function load_arts(hero, mission)
	sleep(1);
	if IsHeroAlive(hero) then
		for i = 1, 71 do
			if GetGameVar(mission..hero.."_"..i) == "1" then
				GiveArtefact(hero, i);
			end;
		end;
	end;
end;

function save_artefact(hero, mission, array)
	if IsHeroAlive(hero) then
		for i, art in array do
			if HasArtefact(hero, art) then
				SetGameVar(mission..hero.."_"..art, 1);
				print("saving artefact "..art);
			end;
		end;
	end;
end;

function teach_spell(hero, array, num)
	if IsHeroAlive(hero) then
		local k = 0;
		local l = length(array);
		local m = 2 * (l - mod(l,num))/num
		for i, spell in array do
			if k < num then
				if random(m) == 0 and KnowHeroSpell(hero, spell) == nil then
					TeachHeroSpell(hero, spell);
					print(hero.." know spell "..spell);
					k = k + 1;
				end;
			end;
		end;
	end;
end;

function add_army(mon, ind)
	if IsObjectExists(mon) then
		for i = 1, 90 do
			if GetObjectCreatures(mon, i) > 0 then
				AddObjectCreatures(mon, i, ind * GetObjectCreatures(mon, i));
			end;
		end;
	end;
end;

function ArmyCount(hero)
	local k = 0;
	if IsHeroAlive(hero) then
		for i=1,91 do
			if GetObjectCreatures(hero, i) > 0 then
				k = k + 1;
			end;
		end;
	end;
	return k
end;

function LostHero( HeroName )
	if ( HeroName == "Heam" ) then
		SetObjectiveState("Prim4", OBJECTIVE_FAILED);
		sleep (15);
		Loose();
	end;
end;

function blocking()  --блокает проходимость тайлов вокруг шадоу драконов для PLAYER_2, чтоб он сам их не поубивал как дурак
	for i,h in regions do
		SetRegionBlocked(regions[i], not nil, PLAYER_2)
		print (regions[i], "block");
	end
end

function dragon_messeges() --чекает в отдельном треде наличие шадоу драгонов на карте.
	local m=0;r7=0;r6=0
	while 1 do
		m=dragons_count()
		if m==0 and night_light == 0 then
				StartDialogScene('/DialogScenes/C5/M5/R8/DialogScene.xdb#xpointer(/DialogScene)') --все драконы убиты
				SetAmbientLight(GROUND, 'c5m5_day',not nil , 5) --меняет освещение на карте на дневное
				SetCombatLight("/Lights/_(AmbientLight)/AdvMap/C5M5/Day.xdb#xpointer(/AmbientLight)");
				light=1
				return --драконы убиты, можно из цикла выйти
		else
			if m==3 and r7==0 then
				StartDialogScene('/DialogScenes/C5/M5/R7/DialogScene.xdb#xpointer(/DialogScene)') --3 группы драконов убиты
				r7=1 --диалог С5М5R7 был показан игроку
				SetAmbientLight(GROUND, 'c5m5_twilight2',not nil , 5) --меняет освещение на карте на чуть более светлые сумерки
				SetCombatLight("/Lights/_(AmbientLight)/AdvMap/C5M5/c4m4_wastes (2).xdb#xpointer(/AmbientLight)");
			else
				if m==5 and r6==0 then
					StartDialogScene('/DialogScenes/C5/M5/R6/DialogScene.xdb#xpointer(/DialogScene)') --1 группа драконов убита
					r6=1 --диалог С5М5R6 был показан игроку
				end;
			end;
		end
		sleep(5)
	end
end

function dragons_count() --возвращает количество оставшихся на карте шадоу драгонов
	local m=0
	for i,h in shadow_dragons do
		if not IsObjectExists(shadow_dragons[i])==nil then --"жив" ли данный стек шадоу драгонов?
			m=m+1
		else
			SetRegionBlocked(regions[i], nil,PLAYER_2) --разблочиваем проходимость региона для PLAYER_2 потому как дракона уже нет
		end
	end
	return m
end

function day_check() ---тригерная функция, чкает состояние дел в начале каждого дня
	great_night_progress=great_night_progress+dragons_count()*(1+0.5*dif) --сколько "зла" нагенерили драконы
	print ("zlo = ", great_night_progress);
	power=great_night_progress/1008 --насколько драконы закончили свою работу
	print('power= ',power)
	if power>1 then
		ownage()
		if night_light == 0 then
			for i,reg in regions do
				SetRegionBlocked(reg, nil, 2);
				print (reg, "unblock");
			end;
			TeachHeroSpell("Nikolay", 31);
			SetGameVar("C5M5_night", 1);
			night_light = 1;
			SetCombatLight("/Lights/_(AmbientLight)/AdvMap/C5M5/Night.xdb#xpointer(/AmbientLight)");
			Play2DSound("/Sounds/_(Sound)/Creatures/Necropolis/ShadowDragon/happy.xdb#xpointer(/Sound)");
			sleep(1);
			SetAmbientLight(GROUND, 'c5m5_night', not nil, 5);
			Play2DSound("/Sounds/_(Sound)/Creatures/Necropolis/Ghosts/happy.xdb#xpointer(/Sound)");
			sleep(1);
			Play2DSound("/Sounds/_(Sound)/Creatures/Necropolis/Zombie/happy.xdb#xpointer(/Sound)");
			sleep(2);
			Play2DSound("/Sounds/_(Sound)/Heroes/NikolayVampire/happy.xdb#xpointer(/Sound)");
		end;
		return
	end;
	if GetDate(MONTH) == 2 and GetDate(DAY) == 1 and GetDate(WEEK) == 1 then
		StartDialogScene('/DialogScenes/C5/M5/D1/DialogScene.xdb#xpointer(/DialogScene)');
	end;
end

function ownage() --вызывается когда параметр power становится больше 1, типа "Великая Ночь" достигла своего апогея и андедов уже не остановить
	sleep(2);
	AddHeroCreatures('Nikolay', CREATURE_SHADOW_DRAGON , 666);
end

function ressurect(heroname,winner) --триггерная функция, запускается после смерти героя PLAYER_2, респавнит Коляна...или не респавнит
	if heroname=='Nikolay' then --убитого героя зовут Колян?
		if not winner==nil then --его убили или сам слажал?
			if GetHeroCreatures(winner, CREATURE_PHOENIX)>0 or light==1 then --были ли у убийцы фениксы в армии на конец битвы, или Коляна убили при солнечном свете?
				SetObjectiveState( 'prim1', OBJECTIVE_COMPLETED); --вот и пришел злостному рецедевисту Коляну конец
				Save("autosave");
				startThread(save_artefact, "Heam", new_path, transfer_arts);
				sleep ( 1 );
				StartDialogScene('/DialogScenes/C5/M5/D2/DialogScene.xdb#xpointer(/DialogScene)');
				sleep(10);
				Win();
				return
			else --облом с фениксами или светом вышел?
				if first_time==1 then --который уже раз Коляна валим?
					first_time=0
					StartDialogScene('/DialogScenes/C5/M5/R2/DialogScene.xdb#xpointer(/DialogScene)');
				end
				DeployReserveHero('Nikolay',check_place()) --респавн Коляна
				sleep(1) --без этой паузы следующая функция не стаботает!!!!
				army() --добавление Коляну свежей армии
				if dif == 2 then
					SetHeroCombatScript("Nikolay", '/Maps/Scenario/C5M5/C5M5-CombatScript.xdb#xpointer(/Script)');
					if random(2) == 0 then
						startThread(death_knight, 'Nikolay');
					end;
				end;
			end
		else
			DeployReserveHero('Nikolay',check_place()) --респавн Коляна
			sleep() --без этой паузы следующая функция не стаботает!!!!
			army() --добавление Коляну свежей армии
			if dif == 2 then
				SetHeroCombatScript("Nikolay", '/Maps/Scenario/C5M5/C5M5-CombatScript.xdb#xpointer(/Script)');
			end;
			if random(2) == 0 and dif == 2 then
				startThread(death_knight, 'Nikolay');
			end;
		end;
	end;
end;

function new_elf(hero)
	if IsHeroAlive(hero) then
		SetHeroCombatScript(hero, '/Maps/Scenario/C5M5/C5M5-CombatScript.xdb#xpointer(/Script)');
	end;
end;

function new_necr(hero)
	if IsHeroAlive(hero) and dif > 0 and hero ~= "Nikolay" then
		if GetHeroLevel(hero) < 3 then
			if dif == 1 then
				ChangeHeroStat(hero, 0, 20000);
				else ChangeHeroStat(hero, 0, 40000);
			end;
		end;
	end;
end;

function army() --добавление армии Коляну после респавна
	if GetDifficulty()==DIFFICULTY_NORMAL then
		give_creatures(kolyan_army_normal)
	else
		if GetDifficulty()==DIFFICULTY_HARD then
			give_creatures(kolyan_army_hard)
		else
			give_creatures(kolyan_army_heroic)
		end;
	end;
end;

function give_creatures(diff_mod)
	for i,h in diff_mod do
		AddHeroCreatures('Nikolay', creatures_types[i+3] , 1+diff_mod[i])
		print("Kolyan gain ",1+diff_mod[i], creatures_types[i+3])
	end;
end

function check_place() --выбирает место куда отреспавнить Коляна, респавнит рядом с одним из своих замков, если таких нет, то у любого из андедских
	local fake_array={};a=6
	for i,h in towns do
		if GetObjectOwner(towns[i])==PLAYER_2 then
			fake_array[i]=towns[i]
		end
	end
	if a==0 then
		a=length(fake_array[i])
	end	
	b=random(a)+1
	return respawns_x[b],respawns_y[b],respawns_z[b]
end

function quest(hero_n)
	if hero_n == "Heam" then
		Trigger(OBJECT_TOUCH_TRIGGER, "hut", nil);
		SetObjectiveState( 'sec1', OBJECTIVE_ACTIVE);
		sleep (15);
		StartDialogScene('/DialogScenes/C5/M5/R3/DialogScene.xdb#xpointer(/DialogScene)');
		startThread(quest_progress);
	else
		ShowFlyingSign("/Maps/Scenario/C5M5/C5M5.txt", "hut", -1, 3.0);
	end;
end;

function quest_progress()
	while 1 do
		sleep (15);
		if HasArtefact("Heam", 48) == not nil and HasArtefact("Heam", 49) == not nil and HasArtefact("Heam", 50) == not nil and HasArtefact("Heam", 51) == not nil then
			StartDialogScene('/DialogScenes/C5/M5/R4/DialogScene.xdb#xpointer(/DialogScene)');
			sleep ( 5 );
			SetObjectiveState( 'sec1', OBJECTIVE_COMPLETED);
			SetObjectiveState( 'sec2', OBJECTIVE_ACTIVE);
			Trigger(OBJECT_TOUCH_TRIGGER, "hut", "quest_final");
			break;
		end;
	end;
end;

function quest_final(hero_n)
	if hero_n == "Heam" then
		Trigger(OBJECT_TOUCH_TRIGGER, "hut", nil);
		StartDialogScene('/DialogScenes/C5/M5/R5/DialogScene.xdb#xpointer(/DialogScene)');
		AddHeroCreatures("Heam",CREATURE_PHOENIX,5);
		sleep( 5 );
		SetObjectiveState( 'sec2', OBJECTIVE_COMPLETED);
		SetObjectFlashlight("Heam" , "phoenix");
		RemoveArtefact("Heam", 48);
		RemoveArtefact("Heam", 49);
		RemoveArtefact("Heam", 50);
		RemoveArtefact("Heam", 51);
		ResetObjectFlashlight("hut");
	end;
end;

	
---script start---
StartDialogScene('/DialogScenes/C5/M5/R1/DialogScene.xdb#xpointer(/DialogScene)');
Trigger(PLAYER_ADD_HERO_TRIGGER, 1, "new_elf");
Trigger(PLAYER_ADD_HERO_TRIGGER, 2, "new_necr");
Trigger(PLAYER_REMOVE_HERO_TRIGGER, 1, "LostHero" );
Trigger(PLAYER_REMOVE_HERO_TRIGGER, 2, 'ressurect');
Trigger(NEW_DAY_TRIGGER,'day_check');
Trigger(OBJECT_TOUCH_TRIGGER, "hut", "quest");
SetObjectEnabled('hut', nil);

blocking();
startThread(dragon_messeges);
startThread(load_arts, "Heam", path);
startThread(map_up);
startThread(control_dwarf);