StartDialogScene("/DialogScenes/C6/M1/D1/DialogScene.xdb#xpointer(/DialogScene)")

C6M1_R1 = 0;
zehir = "Zehir";

hard_spells = {1, 2, 11, 12, 13, 24, 39};
heroic_spells = {1, 3, 4, 15, 25, 237};

undead = {"SKELETON", "WALKING", "ZOMBIE", "MANES", "GHOST", "VAMPIRE", "LICH", "WIGHT", "KNIGHT"};

if GetDifficulty() == DIFFICULTY_EASY then
	dif = 0;
	print ("easy");
	AddObjectCreatures(zehir, 58, 20);
	AddObjectCreatures(zehir, 62, 10);
	AddObjectCreatures(zehir, 65, 3);
end;

if GetDifficulty() == DIFFICULTY_NORMAL then
	dif = 1;
	print ("normal");
	AddObjectCreatures(zehir, 58, 10);
end;

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

if GetDifficulty() == DIFFICULTY_HEROIC then
	dif = 3;
	print ("heroic");
	AddObjectCreatures("insarius", 30, 33 + random(8));
	AddObjectCreatures("insarius", 31, 12 + random(5));
	AddObjectCreatures("insarius", 33, 9 + random(3));
	AddObjectCreatures("insarius", 35, 4 + random(2));
end;

------------------------------------------------------------------------------------------
function messages( dialog )
	if dialog == 1 then
		C6M1_R1 = 1;
		sleep();
		StartDialogScene("/DialogScenes/C6/M1/R2/DialogScene.xdb#xpointer(/DialogScene)");
	end;
end;

function first_nekr(hero)
	Trigger(PLAYER_ADD_HERO_TRIGGER, 2, nil);
	if dif > 1 then
		ChangeHeroStat(hero, 0, 3150*(dif-1));
		startThread(teach_spell, hero, hard_spells, dif);
		if dif == 3 then
			startThread(teach_spell, hero, heroic_spells, 2);
		end;
	end;
	EnableHeroAI(hero, nil);
	EnableAIHeroHiring(2, "insarius", nil);
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);
					k = k + 1;
				end;
			end;
		end;
	end;
end;

function ZehirGettingLevel()
	while 1 do
		if GetHeroLevel(zehir) == 10 then
			SetObjectiveState('prim3',OBJECTIVE_COMPLETED);
			StartDialogScene("/DialogScenes/C6/M1/R1/DialogScene.xdb#xpointer(/DialogScene)");
			break;
		end
		sleep(10);
	end
end

function InsariusCaptured()
	if GetObjectOwner("insarius") == PLAYER_1 then
		ChangeHeroStat(zehir, STAT_EXPERIENCE, 546);
		SetObjectiveState('prim1',OBJECTIVE_COMPLETED);
		SetRegionBlocked('exit', not nil, PLAYER_2);
		startThread(wizard_town);
	end
	if C6M1_R1 == 0 then
		messages( 1 );
	end;
	if GetObjectOwner("insarius") == PLAYER_2 then
		SetObjectiveState('prim1',OBJECTIVE_ACTIVE);
	end
	
end
	
function Insarius()
	if GetObjectiveState("prim1") == OBJECTIVE_UNKNOWN then
		SetObjectiveState('prim1',OBJECTIVE_ACTIVE);
		else
		print("Warning!!! prim1 is not UNKNOWN");
	end;
end

function wizard_town()
	local day = 2 + GetDate(DAY);
	while GetDate(DAY) < day do
		sleep(2);
	end;
	Play2DSound("/Sounds/_(Sound)/Creatures/Academy/Magi/idle01.xdb#xpointer(/Sound)");
	MessageBox("/Maps/Scenario/C6M1/town.txt", "control_necros");
end;

function control_necros()
	while IsHeroAlive(zehir) do
		sleep(5);
		if IsNecrInRegion() == nil then
			break
		end;
	end;
	startThread(transform_necro);
end;

function IsNecrInRegion()
	local units = {};
	local x = 0;
	local y = 0;
	for i, id in undead do
		units = GetObjectNamesByType(id)
		if units ~= {} then
			for j, cr in units do
				if IsObjectExists(cr) then
					x,y = GetObjectPosition(cr);
					if x > 45 and y < 60 then
						return not nil
					end;
				end;
			end;
		end;
	end;
	return nil
end;

function transform_necro()
	if GetTownHero("insarius") ~= nil then
		MessageBox("/Maps/Scenario/C6M1/town_hero.txt");
	end;
	while GetTownHero("insarius") ~= nil do
		sleep(3);
	end;
	local x,y = GetObjectPosition("insarius");
	x = x + 1.5
	y = y + 0.5
	BlockGame();
	MoveCamera(x,y,0,60,1,0,0,1,1);
	sleep(10);
	Play2DSound("/Sounds/_(Sound)/Spells/HolyWord_______255.xdb#xpointer(/Sound)");
	PlayVisualEffect("/Effects/_(Effect)/Spells/HolyWord.xdb#xpointer(/Effect)", "", "", x, y-2.5, 13, random(360));
	PlayVisualEffect("/Effects/_(Effect)/Spells/HolyWord.xdb#xpointer(/Effect)", "", "", x-2, y+2, 13, random(360));
	PlayVisualEffect("/Effects/_(Effect)/Spells/HolyWord.xdb#xpointer(/Effect)", "", "", x+2, y+2, 13, random(360));
	sleep(17);
	Play2DSound("/Sounds/_(Sound)/Spells/SummonEarth.xdb#xpointer(/Sound)");
	sleep(5);
	Play2DSound("/Sounds/_(Sound)/Heroes/Wizard/Happy.xdb#xpointer(/Sound)");
	TransformTown("insarius", 2);
	sleep(5);
	UnblockGame()
end;

function prim2()
	while 1 do
		if IsHeroAlive(zehir)==nil then
			SetObjectiveState("prim2",OBJECTIVE_FAILED);
			return 1;
		end;
		sleep(2);
	end;
end;
	
function WinLoose()
	while 1 do
		if GetObjectiveState("prim1") == OBJECTIVE_COMPLETED and GetObjectiveState("prim3") == OBJECTIVE_COMPLETED then
			sleep(5);
			Win();
			return
		end;
		if GetObjectiveState("prim1") == OBJECTIVE_FAILED or GetObjectiveState("prim2") == OBJECTIVE_FAILED or
			GetObjectiveState("prim3") == OBJECTIVE_FAILED then
			sleep(40);
			Loose();
			return
		end;
		sleep();
	end;
end;

------
SetRegionBlocked('block', not nil, PLAYER_2);

Trigger(OBJECT_CAPTURE_TRIGGER, "insarius", "InsariusCaptured")
Trigger(PLAYER_ADD_HERO_TRIGGER, 2, "first_nekr" );

startThread( prim2 );
startThread( WinLoose );
startThread( ZehirGettingLevel );