zehir = "Zehir";

transfer_arts = {2, 4, 5, 6, 9, 11, 13, 15, 16, 17, 18, 19, 21, 22, 23, 25, 27, 31, 32, 33, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 57, 58, 59, 60, 61, 62, 67, 68, 69};

town_array = {"town1","town2","town3","town4"};
town_array.n = 4;

necromants = {"Straker", "Pelt", "Gles", "Tamika", "Nemor", "Effig", "Muscip", "Aberrar"};

hard_spells = {3, 4, 6, 11, 12, 15, 17, 24, 25, 28, 31, 42, 237};
heroic_spells = {18, 19, 20, 19, 34, 40, 42, 43, 48};
artefacts = {1, 2, 5, 8, 9, 11, 14, 16, 17, 18, 19, 21, 24, 25, 26, 27, 28, 29, 31, 55, 56, 57, 59, 60, 61, 62, 64, 65, 66};

markel_spells = {18, 19, 20, 21, 34, 40, 43, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 220, 221, 235};

diff = 0;

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

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

if GetDifficulty() == DIFFICULTY_HARD then
	dif = 2;
	print ("hard");
	CreateMonster("", 39, 10, 97, 143, 1, 3, 1);
end;

if GetDifficulty() == DIFFICULTY_HEROIC then
	dif = 3;
	print ("heroic");
	CreateMonster("", 90, 10, 97, 143, 1, 3, 1);
end;
-----------------------------------------
function map_up()
	SetGameVar("C6M3_diff", 0);
	sleep(1);
	SetPlayerHeroesCountNotForHire(1, 6);
	if dif > 1 then
		DenyAIHeroesFlee(2,1);
		startThread(teach_spell, "Nathaniel", hard_spells, dif+2);
		startThread(teach_spell, "Brem", hard_spells, dif+1);
		for i=1,4 do
			ChangeHeroStat("Nathaniel", i, dif);
		end;
		for i=0,5 do
			SetPlayerResource(2, i, 20*dif);
		end;
		startThread(add_army, "town1", 0.25*(dif-1));
		startThread(add_army, "town3", 0.5*(dif-1));
		if dif == 3 then
			SetGameVar("C6M3_diff", 1);
			SetHeroLootable("Nathaniel", nil);
			startThread(teach_spell, "Nathaniel", heroic_spells, 3);
			startThread(give_art, "Nathaniel", artefacts, 5);
			startThread(teach_spell, "Brem", heroic_spells, 2);
			GiveHeroSkill("Nathaniel", 2);
			GiveHeroSkill("Nathaniel", 6);
			GiveHeroSkill("Nathaniel", 7);
			GiveHeroWarMachine("Nathaniel", 1);
			GiveHeroWarMachine("Nathaniel", 3);
			GiveHeroWarMachine("Nathaniel", 4);
			DeployReserveHero("Berein", 137, 87, 1);
			sleep(1);
			for i, spel in markel_spells do
				if KnowHeroSpell("Berein", spel) then
					SetGameVar("C6M3_markel_spell_"..spel, 1);
					print("Markel knows spell "..spel);
					else SetGameVar("C6M3_markel_spell_"..spel, 0);
				end;
			end;
			if HasHeroSkill("Berein", 63) then
				SetGameVar("C6M3_markel_scream", 1);
				else SetGameVar("C6M3_markel_scream", 0);
			end;
			if HasHeroSkill("Berein", 108) then
				SetGameVar("C6M3_markel_link", 1);
				else SetGameVar("C6M3_markel_link", 0);
			end;
			SetHeroLootable("Berein", nil);
			GiveArtefact("Berein", 6);
			GiveArtefact("Berein", 51);
			GiveArtefact("Berein", 70);
			GiveArtefact("Berein", 33);
			GiveArtefact("Berein", 49);
			GiveArtefact("Berein", 36);
			GiveArtefact("Berein", 17);
			GiveArtefact("Berein", 25);
			GiveArtefact("Berein", 41);
			sleep(1);
			RemoveObject("Berein");
		end;
	end;
	local exp = GetHeroStat(zehir, 0);
	GiveExp("Nathaniel", exp * (0.35*dif + 0.35));
	GiveExp("Brem", exp * (0.2*dif + 0.2));
	GiveExp("Orrin", exp * (0.1*dif + 0.1));
end;

function boss_nekr(hero)
	if IsInArray(hero, necromants) then
		Trigger(PLAYER_ADD_HERO_TRIGGER, 2, nil);
		sleep(1);
		if dif > 0 then
			print(hero.." is cool necromant!");
			local exp = GetHeroStat(zehir, 0);
			GiveExp(hero, exp * (0.15*dif + 0.15));
			if dif == 3 then
				SetHeroLootable(hero, nil);
				startThread(teach_spell, hero, hard_spells, 4);
				startThread(teach_spell, hero, heroic_spells, 3);
				startThread(give_art, hero, artefacts, 3);
				for i=1,4 do
					ChangeHeroStat(hero, i, 2);
				end;
			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);
					k = k + 1;
				end;
			end;
		end;
	end;
end;

function give_art(hero, array, num)
	if IsHeroAlive(hero) then
		local k = 0;
		local l = length(array);
		local m = 2 * (l - mod(l,num))/num
		for i, art in array do
			if k < num then
				if random(m) == 0 then
					GiveArtefact(hero, art);
					k = k + 1;
				end;
			end;
		end;
	end;
end;

function add_army(mon, ind)
	if IsObjectExists(mon) then
		for i = 1, 14 do
			if GetObjectCreatures(mon, i) > 0 then
				AddObjectCreatures(mon, i, ind * GetObjectCreatures(mon, i));
			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 IsInArray(unit, array)
	if IsObjectExists(unit) then
		if array ~= {} then
			for i, id in array do
				if unit == id then
					return not nil
				end;
			end;
		end;
	end;
	return nil
end;
-------------------------------------------------------------------------------------------
function MeetingHeam()
	while 1 do
		if IsObjectVisible(PLAYER_1, "Heam")==not nil then
		sleep(5);
		StartDialogScene("/DialogScenes/C6/M3/D3/DialogScene.xdb#xpointer(/DialogScene)");
		SetObjectiveState("obj4",OBJECTIVE_ACTIVE);
		SetObjectiveVisible("obj4", not nil);
		SetObjectiveVisible("obj6", not nil);
		SetObjectiveState("obj1",OBJECTIVE_COMPLETED);
		SetObjectOwner("Heam",PLAYER_1);
		ChangeHeroStat("Zehir", STAT_EXPERIENCE, 1473);
		print("Heam should join us");
		return 1;
		end
	sleep();
	end
end	

function MeetingGodric()
	sleep(1);
	StartDialogScene("/DialogScenes/C6/M3/D2/DialogScene.xdb#xpointer(/DialogScene)");
	Trigger(OBJECT_TOUCH_TRIGGER, "prison", nil);
	SetObjectiveVisible("obj3", not nil);
	SetObjectiveVisible("obj7", not nil);
	SetObjectiveState("obj2",OBJECTIVE_COMPLETED);
	SetObjectOwner("Godric",1);
	print("Godric should join us");
	DeployReserveHero("Heam", 84, 41, 0);
	sleep(10);
	startThread(load_arts, "Heam", "C5M5_");
	MoveHero("Heam", 86, 42, 0);
	EnableHeroAI("Heam", nil);
	startThread( MeetingHeam );
	startThread( objective6 );
	startThread( objective7 );
	startThread(AreHeroesAlive);
	ChangeHeroStat("Zehir", STAT_EXPERIENCE, 1532);
	Trigger(REGION_ENTER_AND_STOP_TRIGGER,"guardian","GodricMeetsAllies");
	Trigger(REGION_ENTER_AND_STOP_TRIGGER,"town","GodricFight");
	Trigger(OBJECT_TOUCH_TRIGGER, "prison", nil);
	SetObjectEnabled("prison", nil);
end	

function TownCounter ()
	print( "Town_count() = ",Town_count() );
	if Town_count() == 4 then
		SetObjectiveState("obj3",OBJECTIVE_COMPLETED);
		ChangeHeroStat("Zehir", STAT_EXPERIENCE, 2162);
		SetObjectEnabled("markalgate", not nil);
		Trigger(OBJECT_TOUCH_TRIGGER, "markalgate", nil);
	end;
end;
		
function Town_count()
	local count = 0;
	for i=1, town_array.n do
		if ( GetObjectOwner(town_array[i]) == PLAYER_1 ) then
			count = count + 1;
		end;
	end;
	return count;
end;

--- godric meets allies ---

function GodricMeetsAllies()
	if GetObjectOwner("firstborder",PLAYER_1) == not nil then 
		Trigger(REGION_ENTER_AND_STOP_TRIGGER,"guardian",nil)
		else
		SetObjectOwner("firstborder",PLAYER_1)
		sleep(1);
		StartDialogScene("/DialogScenes/C6/M3/R1/DialogScene.xdb#xpointer(/DialogScene)");
		Trigger(REGION_ENTER_AND_STOP_TRIGGER,"guardian",nil)
	end
end

--- godric says he have to fight --
function GodricFight(hero)
	if GetObjectOwner(hero) == PLAYER_1 then
		StartDialogScene("/DialogScenes/C6/M3/R2/DialogScene.xdb#xpointer(/DialogScene)");
		Trigger(REGION_ENTER_AND_STOP_TRIGGER,"town",nil);
	end
end

---Is Zehir Alive---
function objective5()
	if GetObjectiveState("obj5") == OBJECTIVE_UNKNOWN then
		SetObjectiveState('obj5',OBJECTIVE_ACTIVE);
		else
		print("Warning!!! obj5 is not UNKNOWN");
	end;
	while 1 do
		if IsHeroAlive("Zehir")==nil then
			print("Zehir is dead!!!");
			if GetObjectiveState("obj5") == OBJECTIVE_ACTIVE or GetObjectiveState("obj5") == OBJECTIVE_COMPLETED then
				SetObjectiveState("obj5",OBJECTIVE_FAILED);
				else
				print("Warning!!! obj5 is not ACTIVE or COMPLETED");
			end;
			return 1;
		end;
		if GetObjectiveState("obj1") == OBJECTIVE_COMPLETED and
			GetObjectiveState("obj2") == OBJECTIVE_COMPLETED and
			GetObjectiveState("obj3") == OBJECTIVE_COMPLETED and
			GetObjectiveState("obj4") == OBJECTIVE_COMPLETED then
				if GetObjectiveState("obj5") == OBJECTIVE_ACTIVE  then
					SetObjectiveState('obj5',OBJECTIVE_COMPLETED);
					else
					print("Warning!!! obj5 is not ACTIVE");
				end;
				return 1;
		end;
		sleep(2);
	end;
end;

---Is Heam Alive---
function objective6()
	while 1 do
		if IsHeroAlive("Heam") == nil then
			print("Heam is dead!!!");
			if GetObjectiveState("obj6") == OBJECTIVE_ACTIVE or GetObjectiveState("obj6") == OBJECTIVE_COMPLETED then
				SetObjectiveState("obj6",OBJECTIVE_FAILED);
				else
				print("Warning!!! obj6 is not ACTIVE or COMPLETED");
			end;
			return 1;
		end;
		if GetObjectiveState("obj1") == OBJECTIVE_COMPLETED and
			GetObjectiveState("obj2") == OBJECTIVE_COMPLETED and
			GetObjectiveState("obj3") == OBJECTIVE_COMPLETED and
			GetObjectiveState("obj4") == OBJECTIVE_COMPLETED then
			if GetObjectiveState("obj6") == OBJECTIVE_ACTIVE  then
				SetObjectiveState('obj6',OBJECTIVE_COMPLETED);
				else
				print("Warning!!! obj6 is not ACTIVE");
			end;
			return 1;
		end;
		sleep(2);
	end;
end;

---Is Godric Alive---
function objective7()
	while 1 do
		if IsHeroAlive("Godric")==nil then
			print("Godric is dead!!!");
			if GetObjectiveState("obj7") == OBJECTIVE_ACTIVE or GetObjectiveState("obj7") == OBJECTIVE_COMPLETED then
				SetObjectiveState("obj7",OBJECTIVE_FAILED);
				else
				print("Warning!!! obj7 is not ACTIVE or COMPLETED");
			end;
			return 1;
		end;
		if GetObjectiveState("obj1") == OBJECTIVE_COMPLETED and
			GetObjectiveState("obj2") == OBJECTIVE_COMPLETED and
			GetObjectiveState("obj3") == OBJECTIVE_COMPLETED and
        	GetObjectiveState("obj4") == OBJECTIVE_COMPLETED then
			if GetObjectiveState("obj7") == OBJECTIVE_ACTIVE  then
				SetObjectiveState('obj7',OBJECTIVE_COMPLETED);
				else
				print("Warning!!! obj7 is not ACTIVE");
			end;
			return 1;
		end;
		sleep();
	end;
end;

function WinLoose()
	while 1 do
		if GetObjectiveState("obj1") == OBJECTIVE_COMPLETED and
			GetObjectiveState("obj2") == OBJECTIVE_COMPLETED and
			GetObjectiveState("obj3") == OBJECTIVE_COMPLETED and
			GetObjectiveState("obj4") == OBJECTIVE_COMPLETED then
			startThread(save_artefact, "Heam", new_path, transfer_arts);
			startThread(save_artefact, "Zehir", new_path, transfer_arts);
			startThread(save_artefact, "Godric", new_path, transfer_arts);
			sleep(20);
			Win();
			return
		end;
		if GetObjectiveState("obj1") == OBJECTIVE_FAILED or
			GetObjectiveState("obj2") == OBJECTIVE_FAILED or
			GetObjectiveState("obj3") == OBJECTIVE_FAILED or
			GetObjectiveState("obj4") == OBJECTIVE_FAILED or
			GetObjectiveState("obj5") == OBJECTIVE_FAILED or
			GetObjectiveState("obj6") == OBJECTIVE_FAILED or
			GetObjectiveState("obj7") == OBJECTIVE_FAILED then
			sleep(40);
			Loose();
			return
		end;
		sleep(10);
	end;
end;

-- zehir godric findan --

function fight_zehir()
	print("starting combat");
	BlockGame();
	diff = 4 * GetDate(2) + GetDate(1) - 5 + dif;
	print("difficulty coef "..diff);
	SetGameVar("C6M3_date", diff);
	sleep(1);
	StartCombat("Zehir", "Berein", 6,
		90, 10 + diff * 2,
		CREATURE_SKELETON_ARCHER, 150 + diff * 20,
		CREATURE_ZOMBIE, 40 + diff * 15,
		CREATURE_GHOST, 35 + diff * 10,		
		CREATURE_VAMPIRE_LORD, 20 + diff * 6,
		CREATURE_DEMILICH, 8 + diff * 3,
		'/Maps/Scenario/C6M3/CombatScript_zehir.xdb#xpointer(/Script)',
		"fight_godric",
		'/Scenes/CombatArenas/Boss_c6m3_Dirt.xdb#xpointer(/AdventureFlybyScene)');
	Trigger(REGION_ENTER_AND_STOP_TRIGGER,"gathering",nil)
	Trigger(OBJECT_TOUCH_TRIGGER, "spes", nil);
	Trigger(OBJECT_TOUCH_TRIGGER, "post", nil);
end	

function fight_godric()
	OpenCircleFog(24, 15, 0, 30, 1);
	sleep(8);
	MoveCamera(28.5,10.5,0,15,1,1.4,0,1,1);
	sleep(5);
	Play2DSound("/Sounds/_(Sound)/Spells/FireBallHit.xdb#xpointer(/Sound)");
	PlayVisualEffect("/Effects/_(Effect)/Spells/FireBallHit.xdb#xpointer(/Effect)", "", "", 28.5, 10.5, 0, random(360));
	sleep(2);
	PlayVisualEffect("/Effects/_(Effect)/Towns/Inferno/MagicGuild.xdb#xpointer(/Effect)", "", "", 28.5, 10.5, 0, random(360));
	sleep(25);
	StartCombat("Godric", "Berein", 5,
		CREATURE_DEMILICH, 15 + diff * 3,
		CREATURE_SKELETON_ARCHER, 170 + diff * 25,
		89, 8 + 2 * diff,
		CREATURE_MANES, 40 + diff * 12,		
		CREATURE_VAMPIRE, 25 + diff * 5,
		'/Maps/Scenario/C6M3/CombatScript_godric.xdb#xpointer(/Script)',
		"SpesFight",
		'/Scenes/CombatArenas/Boss_c6m3_Dirt2.xdb#xpointer(/AdventureFlybyScene)')		
end	

function SpesFight()
	if IsHeroAlive("Godric") then 
		sleep(7);
		DeployReserveHero("Berein", 137, 87, 1);
		MoveCamera(25.5,21.5,0,58,1,0.2,0,1,1);
		sleep(5);
		Play2DSound("/Sounds/_(Sound)/Spells/HolyWord_______255.xdb#xpointer(/Sound)");
		PlayVisualEffect("/Effects/_(Effect)/Spells/HolyWord.xdb#xpointer(/Effect)", "", "", 25.5, 21.5, 13, random(360));
		for i = 29,42 do
			if GetObjectCreatures("spes", i) > 0 then
				AddObjectCreatures("Berein", i, GetObjectCreatures("spes", i));
				RemoveObjectCreatures("spes", i, GetObjectCreatures("spes", i));
			end;
		end;
		sleep(1);
		SetHeroCombatScript("Heam", '/Maps/Scenario/C6M3/CombatScript_heam.xdb#xpointer(/Script)');
		SetObjectPosition("Berein", 19, 15, 0);
		sleep(19);
		Play2DSound("/Sounds/_(Sound)/Spells/FireBallHit.xdb#xpointer(/Sound)");
		PlayVisualEffect("/Effects/_(Effect)/Towns/Inferno/DemonGate.xdb#xpointer(/Effect)", "", "", 25.5, 21.5, 0, random(360));
		sleep(15);
		SiegeTown("Heam", "spes", '/Scenes/CombatArenas/Boss_c6m3_Siege.xdb#xpointer(/AdventureFlybyScene)');
		while IsHeroAlive("Heam") and IsHeroAlive("Berein") do
			sleep(1);
		end;
		UnblockGame();
		startThread(theend);
		else Loose()
	end;
end;

function theend()
	if IsHeroAlive("Zehir")==nil or IsHeroAlive("Heam")==nil or IsHeroAlive("Godric")==nil then
			sleep();
			Loose();
		else
		FinalSave();
	end
end

function FinalSave()
	Save("Scene_18");
	sleep(10);
	FinalCutscene();
end

function FinalCutscene()
	--cutscene c6m3 markal's death --
	print("executing cutscene")
	StartCutScene("/Maps/Cutscenes/C6M3/_.(AnimScene).xdb#xpointer(/AnimScene)");
	sleep(20);
	SetObjectiveState("obj4",OBJECTIVE_COMPLETED);
end

function AreHeroesAlive()
	while 1 do
		if IsHeroAlive("Zehir")==nil or IsHeroAlive("Heam")==nil or IsHeroAlive("Godric")==nil then
			sleep();
			Loose();
		end
	sleep(2);
	end
end

-- Entering Markal's realm SelectionBox --

function AreYouReady()
	QuestionBox('/Maps/Scenario/C6M3/message-8.txt', "fight_zehir");
end

function YouAreNotReady()
	MessageBox('/Maps/Scenario/C6M3/message-9.txt');
end


---markal's gate are locked ---

function MarkalGateMessage()
	MessageBox('/Maps/Scenario/C6M3/message-7.txt');
end

-- Zehir is too fast --

function FastZehirCheck()
	while 1 do
		if IsHeroAlive("Heam") and IsHeroAlive("Godric") then
			Trigger(OBJECT_TOUCH_TRIGGER, "spes", "AreYouReady");
			Trigger(OBJECT_TOUCH_TRIGGER, "post", "AreYouReady");
			Trigger(REGION_ENTER_AND_STOP_TRIGGER,"gathering","AreYouReady");
			print("checking full party");
			break;
		end
	sleep();
	end
end

---perform---
StartDialogScene("/DialogScenes/C6/M3/D1/DialogScene.xdb#xpointer(/DialogScene)");


SetRegionBlocked("heam", not nil, PLAYER_2);
SetRegionBlocked("block", not nil, PLAYER_2);

SetObjectEnabled("markalgate", nil);
SetObjectEnabled("spes", nil);
SetObjectEnabled("post", nil);

SetObjectiveVisible("obj3", nil);
SetObjectiveVisible("obj4", nil);
SetObjectiveVisible("obj6", nil);
SetObjectiveVisible("obj7", nil);	

Trigger(PLAYER_ADD_HERO_TRIGGER, 2, "boss_nekr");
Trigger(OBJECT_TOUCH_TRIGGER, "spes", "YouAreNotReady");
Trigger(OBJECT_TOUCH_TRIGGER, "post", "YouAreNotReady");
Trigger(REGION_ENTER_AND_STOP_TRIGGER,"gathering","YouAreNotReady");
Trigger(OBJECT_CAPTURE_TRIGGER, "town1", "TownCounter");
Trigger(OBJECT_CAPTURE_TRIGGER, "town2", "TownCounter");
Trigger(OBJECT_CAPTURE_TRIGGER, "town3", "TownCounter");
Trigger(OBJECT_CAPTURE_TRIGGER, "town4", "TownCounter");
Trigger(OBJECT_TOUCH_TRIGGER, "prison", "MeetingGodric");
Trigger(OBJECT_TOUCH_TRIGGER, "markalgate", "MarkalGateMessage");
startThread( FastZehirCheck );
startThread( objective5 );
startThread( WinLoose );
startThread(map_up);