DragonsDefeated = 0;
Priority = 0;
heroes = {};
SetGameVar("C2M4_date", 1);
SetGameVar("C2M4_diff", 0);
current_data = 1;
drag_altern = {13, 14, 70, 83, 84, 91};
drag_coef = {1, 1, 1, 1, 0.8, 0.6};
CreatureList = {43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56};
CreatureList.n = 14;
enemy_heroes = {"Diraya", "Linaas"};
transfer_arts = {13, 24};
path = "C2M3_";
new_path = "C2M4_";

avenger_enemy = {{20, 20}, {22, 15}, {24, 10}, {28, 6}};
avenger_coord = {{134, 136, 0}, {136, 136, 0}, {138, 136, 0}, {140, 136, 0}};

EnableHeroAI("Elleshar",nil);
EnableAIHeroHiring(PLAYER_2,"imarium",false);
SetObjectEnabled("dragons",nil);

SetRegionBlocked("gate1",1,2);
SetRegionBlocked("gate2",1,2);
SetRegionBlocked("gate3",1,2);
SetRegionBlocked("gate4",1,2);
SetRegionBlocked("gate5",1,2);
SetRegionBlocked("tavern1",1,2);
SetRegionBlocked("tavern2",1,2);
SetRegionBlocked("devils",1,2);
SetRegionBlocked("border1",1,2);
SetRegionBlocked("border2",1,2);
SetRegionBlocked("border3",1,2);
SetRegionBlocked("border4",1,2);
SetRegionBlocked("border5",1,2);
SetRegionBlocked("gate_u1",1,3);
SetRegionBlocked("gate_u2",1,3);
SetRegionBlocked("gate_u3",1,3);
SetRegionBlocked("gate_u4",1,3);
SetRegionBlocked("gate_u5",1,3);
SetRegionBlocked("garrison",1,3);
SetRegionBlocked("garrison",1,2);
SetRegionBlocked("Dragons",1,2);
SetRegionBlocked("Dragons",1,3);


SetPlayerStartResource(PLAYER_1,WOOD,10);
SetPlayerStartResource(PLAYER_1,ORE,10);
SetPlayerStartResource(PLAYER_1,CRYSTAL,2);
SetPlayerStartResource(PLAYER_1,SULFUR,2);
SetPlayerStartResource(PLAYER_1,MERCURY,2);
SetPlayerStartResource(PLAYER_1,GEM,2);
SetPlayerStartResource(PLAYER_1,GOLD,20000);

SetPlayerResource(PLAYER_2,WOOD,10);
SetPlayerResource(PLAYER_2,ORE,10);
SetPlayerResource(PLAYER_2,CRYSTAL,2);
SetPlayerResource(PLAYER_2,SULFUR,2);
SetPlayerResource(PLAYER_2,MERCURY,2);
SetPlayerResource(PLAYER_2,GEM,2);
SetPlayerResource(PLAYER_2,GOLD,10000);

if GetDifficulty() == DIFFICULTY_EASY then
	diff = 0;
	print ("easy");
	ChangeHeroStat("Elleshar", 0, 90000);
end;

if GetDifficulty() == DIFFICULTY_NORMAL then
	diff = 1;
	print ("normal");
	ChangeHeroStat("Elleshar", 0, 120000);
end;

if GetDifficulty() == DIFFICULTY_HARD then
	diff = 2;
	print ("hard");
	heroes = GetPlayerHeroes(2);
	for i, hero in heroes do
		ChangeHeroStat(hero, 0, 120000);
		for j=1,4 do
			ChangeHeroStat(hero, j, 2);
		end;
		for j = 43, 54 do
			if hero ~= "Elleshar" and GetObjectCreatures(hero, j) > 0 then
				AddObjectCreatures(hero, j, 0.75*GetObjectCreatures(hero, j));
			end;
		end;
		TeachHeroSpell(hero, 23);
		TeachHeroSpell(hero, 24);
		TeachHeroSpell(hero, 25);
		TeachHeroSpell(hero, 28);
		TeachHeroSpell(hero, 6);
	end;
	ChangeHeroStat("Elleshar", 5, 3);
	ChangeHeroStat("Elleshar", 6, 3);
	GiveArtefact("Elleshar", 1);
	GiveArtefact("Elleshar", 9);
	GiveArtefact("Elleshar", 21);
	GiveArtefact("Elleshar", 59);
	GiveArtefact("Elleshar", 62);
	GiveArtefact("Elleshar", 27);
	GiveArtefact("Elleshar", 48);
	GiveArtefact("Elleshar", 17);
	GiveArtefact("Elleshar", 8);
	GiveArtefact("Elleshar", 11);
	SetHeroLootable("Elleshar", nil);
	AddObjectCreatures("imarium", 91, 5);
	AddObjectCreatures("imarium", 52, 20);
	heroes = GetPlayerHeroes(3);
	for i, hero in heroes do
		ChangeHeroStat(hero, 0, 60000);
		for j=1,4 do
			ChangeHeroStat(hero, j, 1);
		end;
		TeachHeroSpell(hero, 1);
		TeachHeroSpell(hero, 3);
		TeachHeroSpell(hero, 4);
		for j = 15, 28 do
			if GetObjectCreatures(hero, j) > 0 then
				AddObjectCreatures(hero, j, GetObjectCreatures(hero, j));
			end;
		end;
	end;
	DenyAIHeroesFlee(2, 1);
	DenyAIHeroesFlee(3, 1);
	for i =0, 5 do
		SetPlayerResource(2, i, 100);
	end;
end;

if GetDifficulty() == DIFFICULTY_HEROIC then
	diff = 4;
	print ("heroic");
	heroes = GetPlayerHeroes(2);
	for i, hero in heroes do
		ChangeHeroStat(hero, 0, 160000);
		for j=1,4 do
			ChangeHeroStat(hero, j, 3);
		end;
		TeachHeroSpell(hero, 23);
		TeachHeroSpell(hero, 24);
		TeachHeroSpell(hero, 25);
		TeachHeroSpell(hero, 26);
		TeachHeroSpell(hero, 28);
		TeachHeroSpell(hero, 31);
		TeachHeroSpell(hero, 40);
		TeachHeroSpell(hero, 6);
		TeachHeroSpell(hero, 236);
		for j = 43, 54 do
			if hero ~= "Elleshar" and GetObjectCreatures(hero, j) > 0 then
				AddObjectCreatures(hero, j, GetObjectCreatures(hero, j));
			end;
		end;
	end;
	ChangeHeroStat("Elleshar", 0, 150000);
	ChangeHeroStat("Elleshar", 5, 3);
	ChangeHeroStat("Elleshar", 6, 3);
	GiveArtefact("Elleshar", 4);
	GiveArtefact("Elleshar", 9);
	GiveArtefact("Elleshar", 21);
	GiveArtefact("Elleshar", 22);
	GiveArtefact("Elleshar", 62);
	GiveArtefact("Elleshar", 27);
	GiveArtefact("Elleshar", 13);
	GiveArtefact("Elleshar", 17);
	GiveArtefact("Elleshar", 8);
	GiveArtefact("Elleshar", 11);
	AddObjectCreatures("imarium", 52, 25);
	AddObjectCreatures("imarium", 91, 10);
	AddObjectCreatures("nebircias", 25, 4);
	AddObjectCreatures("nebircias", 28, 2);
	TeachHeroSpell("Elleshar", 34);
	TeachHeroSpell("Elleshar", 35);
	TeachHeroSpell("Elleshar", 235);
	TeachHeroSpell("Elleshar", 29);
	TeachHeroSpell("Elleshar", 19);
	heroes = GetPlayerHeroes(3);
	for i, hero in heroes do
		ChangeHeroStat(hero, 0, 80000);
		for j=1,4 do
			ChangeHeroStat(hero, j, 2);
		end;
		TeachHeroSpell(hero, 1);
		TeachHeroSpell(hero, 3);
		TeachHeroSpell(hero, 4);
		TeachHeroSpell(hero, 8);
		TeachHeroSpell(hero, 17);
		for j = 15, 28 do
			if GetObjectCreatures(hero, j) > 0 then
				AddObjectCreatures(hero, j, 1.5 * GetObjectCreatures(hero, j));
			end;
		end;
	end;
	DenyAIHeroesFlee(2, 1);
	DenyAIHeroesFlee(3, 1);
	SetHeroLootable("Elleshar", nil);
	SetGameVar("C2M4_diff", 1);
	for i, hero in enemy_heroes do
		ChangeHeroStat(hero, 0, 100000);
		for j=1,4 do
			ChangeHeroStat(hero, j, 3);
		end;
		AddObjectCreatures(hero, 43, 100);
		AddObjectCreatures(hero, 45, 74);
		AddObjectCreatures(hero, 47, 45);
		AddObjectCreatures(hero, 49, 22);
	end;
	for i = 0, 5 do
		SetPlayerResource(2, i, 100);
	end;
end;

StartDialogScene("/DialogScenes/C2/M4/R1/DialogScene.xdb#xpointer(/DialogScene)");

function noEmerald()
	local towns = GetObjectNamesByType("TOWN_PRESERVE");
	for i, town in towns do
		SetTownBuildingLimitLevel(town, 13, 0);
		if GetObjectCreatures(town, 56) > 0 then
			RemoveObjectCreatures(town, 56, 100);
		end;
		if GetObjectCreatures(town, 55) > 0 then
			RemoveObjectCreatures(town, 55, 100);
		end;
	end;
	AddObjectCreatures("imarium", 91, 5);
	local dragons = GetObjectNamesByType("DRAGON");
	local x = 1;
	local y = 1;
	local z = 0;
	local num = 0;
	local index = 0;
	local rot = 0;
	for i, drag in dragons do
		x,y,z = GetObjectPosition(drag);
		num = GetObjectCreatures(drag, 55) + GetObjectCreatures(drag, 56);
		if num > 0 then
			RemoveObject(drag);
			index = 1 + random(6);
			sleep(1);
			if x == 27 or x == 37 or x == 155 then
				rot = 180;
			end;
			CreateMonster("", drag_altern[index], num * drag_coef[index], x, y, z, 3, 1, rot);
			num = 0;
			rot = 0;
		end;
	end;
end;

function avenger_up()
	if diff == 4 then
		sleep(5);
		SetObjectPosition("Elleshar", 132, 136, 0);
		startThread(avenger_hate, "Elleshar");
	end;
end;

function avenger_hate(hero)
	if IsHeroAlive(hero) then
		for i, enemy in avenger_enemy do
			CreateMonster("avenger_enemy_"..i, enemy[1], enemy[2], avenger_coord[i][1], avenger_coord[i][2], avenger_coord[i][3], 3, 1);
		end;
		sleep(1);
		if IsHeroAlive(hero) then
			for i, enemy in avenger_enemy do
				if IsObjectExists("avenger_enemy_"..i) then
					MoveHero(hero, GetObjectPosition("avenger_enemy_"..i));
					while IsObjectExists("avenger_enemy_"..i) and IsHeroAlive(hero) do
						sleep(3);
					end;
				end;
			end;
			sleep(5);
		end;
		if IsHeroAlive(hero) then
			SetObjectPosition(hero, 89, 127, 0);
		end;
	end;
end;

function load_arts(hero, mission)
	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 EnemyGate()
	print("Thread Enemy Gate has been started...");
	while 1 do
		sleep(10);
		if GetDate(WEEK) == 3 then
			SetRegionBlocked("border1",0,2);
			SetRegionBlocked("border2",0,2);
			SetRegionBlocked("border3",0,2);
			SetRegionBlocked("border4",0,2);
			print("Now is a third week. Gate to the enemy has been opened.");
			break;
		end;
	end;
end;

function AgraelSurvive()
	print("Thread AgraelSurvive has been started...");
	while 1 do
		sleep(10);
		if (IsHeroAlive("Agrael") == nil) then
			print("Our glorious hero is dead, but he'll live in our hearts forever...");
			SetObjectiveState("prim2",OBJECTIVE_FAILED);
			sleep(15);
			Loose(0);
			break;
		end;
	end;
end;

function ImariumCaptured()
	print("Thread ImariumCaptured has been started...");
	while 1 do
		sleep(10);
		if (GetObjectOwner("imarium") == 1) then
			print("Imarium captured!!!");
			print("You won!!!");
			StartDialogScene("/DialogScenes/C2/M4/R8/DialogScene.xdb#xpointer(/DialogScene)");
			SetObjectiveState("prim1",OBJECTIVE_COMPLETED);
			startThread(save_artefact, "Agrael", new_path, transfer_arts);
			sleep(5);
			SetObjectiveState("prim2",OBJECTIVE_COMPLETED);
			sleep(15);
			Win(0);
			break;
		end;
	end;
end;

function PlayerWin()
	print("Thread PlayerWin has been started...");
	while 1 do
		sleep(10);
		local heroes = GetObjectsInRegion("ship",OBJECT_HERO);
		if heroes[0] == "Agrael" then
			print("You won!!!");
			StartDialogScene("/DialogScenes/C2/M4/R8/DialogScene.xdb#xpointer(/DialogScene)");
			SetObjectiveState("prim1",OBJECTIVE_COMPLETED);
			sleep(5);
			SetObjectiveState("prim2",OBJECTIVE_COMPLETED);
			sleep(15);
			Win(0);
			break;
		end;
	end;
end;

function Day2()
	print("Thread Day2 has been started...");
	while 1 do
		sleep(10);
		if (GetDate(DAY) == 2) then
			print("Objective CaptureNebercias has been given...");
			StartDialogScene("/DialogScenes/C2/M4/R2/DialogScene.xdb#xpointer(/DialogScene)");
			SetObjectiveState("sec_capture_town",OBJECTIVE_ACTIVE);
			sleep(10);
			startThread(NeberciasCaptured);
			break;
		end;
	end;
end;


function NeberciasCaptured()
	print("Thread NeberciasCaptured has been started...");
	while 1 do
		sleep(10);
		if (GetObjectOwner("nebircias") == 1) then
			print("Nebercias has been captured");
			StartDialogScene("/DialogScenes/C2/M4/R3/DialogScene.xdb#xpointer(/DialogScene)");
			sleep(10);
			SetObjectiveState("sec_capture_town",OBJECTIVE_COMPLETED);
			--ObjectiveExp("Agrael");
			sleep(10);
			SetRegionBlocked("gate1",nil,2);
			SetRegionBlocked("gate3",nil,2);
			SetRegionBlocked("gate_u1",nil,3);
			SetRegionBlocked("gate_u3",nil,3);
			SetRegionBlocked("gate_u4",nil,3);
			SetRegionBlocked("gate_u5",nil,3);
			break;
		end;
	end;
end;

function DragonsObjective()
	print("Thread DragonsObjective has been started...");
	StartDialogScene("/DialogScenes/C2/M4/D1/DialogScene.xdb#xpointer(/DialogScene)");
	sleep(10);
	SetObjectiveState("sec_dragons",OBJECTIVE_ACTIVE);
	local WoodElf = GetHeroCreatures ("Agrael",CREATURE_WOOD_ELF);
	local GrandElf = GetHeroCreatures ("Agrael",CREATURE_GRAND_ELF);
	sleep(4);
	if (WoodElf+GrandElf > 99) then
		print("Count of Elves more than 99");
		Trigger(REGION_ENTER_AND_STOP_TRIGGER,"Dragons",nil);
		if (WoodElf+GrandElf >= 500) then
			print("Agrael bring to Dragons more than 500 elves");
			StartDialogScene("/DialogScenes/C2/M4/R7/DialogScene.xdb#xpointer(/DialogScene)");
			SetObjectiveState("sec_dragons",OBJECTIVE_COMPLETED);
			RemoveHeroCreatures("Agrael",CREATURE_WOOD_ELF,10000);
			RemoveHeroCreatures("Agrael",CREATURE_GRAND_ELF,10000);
			RemoveObject("dragons");
			sleep(3);
			GiveArtefact("Agrael",ARTIFACT_DRAGON_FLAME_TONGUE);
			ObjectiveExp("Agrael");
		else
			print("Agrael bring to Dragons more then 100 wood elves!");
			StartDialogScene("/DialogScenes/C2/M4/R4/DialogScene.xdb#xpointer(/DialogScene)");
			SetObjectiveState("sec_dragons",OBJECTIVE_COMPLETED);
			ObjectiveExp("Agrael");
			RemoveHeroCreatures("Agrael",CREATURE_WOOD_ELF,10000);
			RemoveHeroCreatures("Agrael",CREATURE_GRAND_ELF,10000);
			RemoveObject("dragons");
		end;
	else
		print("Now Agrael does not have enough elves.");
		startThread(AgraelLeaveDragons);
	end;
end;

function AgraelLeaveDragons()
	print("Thread AgraelLeaveDragons has been started...");
	while 1 do
		sleep(10);
		if (IsObjectInRegion("Agrael","Dragons") == nil) and (DragonsDefeated == 0) then
			print("Agrael has returned to dragons");
			Trigger(REGION_ENTER_AND_STOP_TRIGGER,"Dragons","DragonsObjectiveCompleted");
			break;
		end;
	end;
end;


function DragonsObjectiveCompleted()
	print("Thread DragonsObjectiveCompleted has been started...");
	WoodElf = GetHeroCreatures ("Agrael",CREATURE_WOOD_ELF);
	GrandElf = GetHeroCreatures ("Agrael",CREATURE_GRAND_ELF);
	print("Number of Elves = ",GrandElf+WoodElf);
	if (WoodElf+GrandElf > 99) then
		print("Count of Elves more than 99");
		Trigger(REGION_ENTER_AND_STOP_TRIGGER,"Dragons",nil);
		if (WoodElf+GrandElf >= 500) then
			print("Agrael bring to Dragons more than 500 elves");
			GiveArtefact("Agrael",ARTIFACT_DRAGON_FLAME_TONGUE);
			StartDialogScene("/DialogScenes/C2/M4/R7/DialogScene.xdb#xpointer(/DialogScene)");
			SetObjectiveState("sec_dragons",OBJECTIVE_COMPLETED);
			ObjectiveExp("Agrael");
			RemoveHeroCreatures("Agrael",CREATURE_WOOD_ELF,10000);
			RemoveHeroCreatures("Agrael",CREATURE_GRAND_ELF,10000);
			RemoveObject("dragons");
		else
			print("Agrael bring to Dragons more then 100 wood elves!");
			StartDialogScene("/DialogScenes/C2/M4/R4/DialogScene.xdb#xpointer(/DialogScene)");
			SetObjectiveState("sec_dragons",OBJECTIVE_COMPLETED);
			ObjectiveExp("Agrael");
			RemoveHeroCreatures("Agrael",CREATURE_WOOD_ELF,10000);
			RemoveHeroCreatures("Agrael",CREATURE_GRAND_ELF,10000);
			RemoveObject("dragons");
		end;
	else
		print("Agrael bring to Dragons not enough elves. Need 100");
		StartDialogScene("/DialogScenes/C2/M4/R6/DialogScene.xdb#xpointer(/DialogScene)");
		Trigger(REGION_ENTER_AND_STOP_TRIGGER,"Dragons","DragonsObjectiveCompleted");
	end;
end;

--new day shit
function NewDay()
	print("Thread desertir has been started...");
	for i=1,14 do
		if GetHeroCreatures("Agrael",CreatureList[i]) > 5 then
			if i <= 4 then
				quantity = 1+random(6);
			end;
			if i > 4 and i <=10 then
				quantity = 1+random(2);
			end;
			if i > 10 then
				quantity = 1;
			end;
			RemoveHeroCreatures("Agrael",CreatureList[i],quantity);
			print("Agrael lost ",quantity," creatures. Creature ID = ",CreatureList[i]);
		else
			print("Hero has less then 6 creatures this type. Creature ID = ",CreatureList[i]);
		end;
	end;
	if GetDate(DAY) > 1 and GetDate(DAY_OF_WEEK) == 1 and GetObjectOwner("imarium") == 2 then
		print("Thread Erewel Reinforcements has been started");
		current_data = current_data + 1;
		SetGameVar("C2M4_date", current_data);
		if diff == 2 then
			print("Difficulty level is HARD. Reinforcements added...");
			AddObjectCreatures("imarium",CREATURE_PIXIE, 14);
			AddObjectCreatures("imarium",CREATURE_GRAND_ELF, 9);
			AddObjectCreatures("imarium",CREATURE_DRUID_ELDER, 7);
			AddObjectCreatures("imarium",CREATURE_WAR_DANCER, 11);
			AddObjectCreatures("imarium",CREATURE_WAR_UNICORN, 3);
			if random(2) == 0 then
				AddObjectCreatures("imarium", 91, 1);
			end;
			else
			if diff == 4 then
				print("Difficulty level is HEROIC. Reinforcements added...");
				AddObjectCreatures("imarium",CREATURE_PIXIE, 22);
				AddObjectCreatures("imarium",CREATURE_GRAND_ELF, 15);
				AddObjectCreatures("imarium",CREATURE_DRUID_ELDER, 11);
				AddObjectCreatures("imarium",CREATURE_WAR_DANCER, 17);	
				AddObjectCreatures("imarium",CREATURE_WAR_UNICORN, 6);
				AddObjectCreatures("imarium", 91, 1);
			end;
		end;
	end;
end;

function DialogBeforeCombatVSdragons(heroname)
	HeroName = heroname;
	print("Dialog 1 check has been started...");
	QuestionBox("/Maps/Scenario/C2M4/BeforeCombatVSDragons.txt", "combatVSdragons");
end;

function combatVSdragons(heroname)
	print("Thread combatVSdragons has been started...");
	StartDialogScene("/DialogScenes/C2/M4/R5/DialogScene.xdb#xpointer(/DialogScene)");
	StartCombat(HeroName, nil,3,CREATURE_SHADOW_DRAGON,6 + 5 * diff,CREATURE_SHADOW_DRAGON,6 + 5 * diff,CREATURE_SHADOW_DRAGON,6 + 5 * diff,nil,"FinishCombat");
end;

function FinishCombat(heroname,result)
	print("Thread FinishCombat has been started");
	if result == not nil then
		print("Our glorious ",heroname," has killed the dragons!!!");
		RemoveObject("dragons");
		SetRegionBlocked("Dragons",nil,2);
		SetRegionBlocked("Dragons",nil,3);
		DragonsDefeated = 1;
		Trigger(REGION_ENTER_AND_STOP_TRIGGER,"Dragons",nil);
		SetObjectiveState("sec_dragons",OBJECTIVE_FAILED);
		else
		print(heroname, " has been killed by dragons! :(");
	end;
end;

function ObjectiveExp(HeroName)
	local ToLevel = GetExpToLevel(GetHeroLevel(HeroName)+1);
	local delta = (ToLevel - GetHeroStat(HeroName, STAT_EXPERIENCE)) / 2;
	ChangeHeroStat(HeroName, STAT_EXPERIENCE,delta);
	print("Now ",HeroName, " has ", GetHeroStat(HeroName, STAT_EXPERIENCE)," exp");
end;

function GetExpToLevel( j )
	local sum;      --LEVEL 1 2    3    4    5    6    7    8     9     10    11    12
	ExpArrayLess12 = {0,1000,2000,3200,4600,6200,8000,10000,12200,14700,17500,20600};
	ExpArrayLess12.n = 12;
					--LEVEL 13    14    15    16    17    18    19    20    21    22     23     24
	ExpArrayMore12 = {24320,28784,34141,40569,48283,57539,68647,81977,97972,117166,140200,167839};
	ExpArrayMore12.n = 12;
					--LEVEL 25     26     27     28     29     30     31      32      33      34
	ExpArrayMore25 = {201007,244126,304491,395040,539917,786208,1229533,2071000,3756484,7294215};
	ExpArrayMore25.n = 10;
	if j <= 12 then
		sum = ExpArrayLess12[j];
	else
		if j < 25 then
			sum = ExpArrayMore12[j-12];
		else
			if j < 35 then
				sum = ExpArrayMore25[j-24];
			else
				print("Das ist fantastisch!!!");
				sum = 0;
			end;
		end;
	end;
	print("Hero need ", sum, " experience to gain level ",j);
	return sum;
end;

function DisableHeroAI()
	InfernoHeroes = GetPlayerHeroes(PLAYER_3);
	PreserveHeroes = GetPlayerHeroes(PLAYER_2);
	for i=0,length(InfernoHeroes)-1 do
		EnableHeroAI(InfernoHeroes[i],nil);
		print("AI has been disabled for inferno hero ",InfernoHeroes[i]);
	end;
	for i=0,length(PreserveHeroes)-1 do
		EnableHeroAI(PreserveHeroes[i],nil);
		print("AI has been disabled for preserve hero ",PreserveHeroes[i]);
	end;
end;

function AIPressingTownHolin(heroname)
	print("Thread AIPressingTownHolin has been started...")
	repeat
		sleep(20);
	until GetObjectOwner("holin") == PLAYER_1;
	print("Town Holin has been captured by Player");
	local x,y = GetObjectPosition("holin");
	while 1 do
		sleep(20);
		if IsHeroAlive(heroname) == true then
			HeroMovePointsToHolin = CalcHeroMoveCost(heroname,x,y);
			AgraelMovePointsToHolin = CalcHeroMoveCost("Agrael",x,y);
			x1,y1,Floor = GetObjectPosition("Agrael");
			if IsObjectVisible(PLAYER_2, "Agrael") == nil or Floor ~= 0 then
				Priority = 2;
				SetAIHeroAttractor("holin", heroname, Priority);
			else
				if HeroMovePointsToHolin <= AgraelMovePointsToHolin then
					Priority = 2;
					SetAIHeroAttractor("holin", heroname, Priority);
				else
					Priority = -1;
					SetAIHeroAttractor("holin", heroname, Priority);
				end;
			end;
		else
			print("Hero ",heroname," is dead.");
			break;
		end;
	end;
end;

function NearCapitol(hero)
	if GetObjectOwner(hero) == 1 then
		MessageBox("/Maps/Scenario/C2M4/Boss.txt");
		Trigger(REGION_ENTER_AND_STOP_TRIGGER,"garrison","");
	end;
end;

--new enemies
function new_hero(hero)
	if dif > 2 then
		SetHeroCombatScript(hero, '/Maps/Scenario/C2M4/CombatScript_Dragons.xdb#xpointer(/Script)');
	end;
end;

function NewElf(hero)
	if IsHeroAlive(hero) and diff > 1 then
		if GetHeroLevel(hero) < 3 then
			if diff == 2 then
				ChangeHeroStat(hero, 0, 30000);
				else ChangeHeroStat(hero, 0, 60000);
				TeachHeroSpell(hero, 23);
				TeachHeroSpell(hero, 24);
				TeachHeroSpell(hero, 25);
				TeachHeroSpell(hero, 28);
				TeachHeroSpell(hero, 6);
				for j=1,4 do
					ChangeHeroStat(hero, j, 1);
				end;
			end;
		end;
	end;
end;

function NewDemon(hero)
	if IsHeroAlive(hero) and diff > 1 then
		if GetHeroLevel(hero) < 3 then
			if diff == 2 then
				ChangeHeroStat(hero, 0, 22000);
				else ChangeHeroStat(hero, 0, 30000);
				TeachHeroSpell(hero, 1);
				TeachHeroSpell(hero, 3);
				TeachHeroSpell(hero, 4);
				TeachHeroSpell(hero, 237);
			end;
		end;
	end;
end;

-- starting
startThread(Day2);
startThread(AgraelSurvive);
startThread(ImariumCaptured);
startThread(EnemyGate);
Trigger(REGION_ENTER_AND_STOP_TRIGGER,"Dragons","DragonsObjective");
Trigger(NEW_DAY_TRIGGER,"NewDay");
Trigger(OBJECT_TOUCH_TRIGGER, "dragons","DialogBeforeCombatVSdragons",nil);
Trigger(PLAYER_ADD_HERO_TRIGGER, 1, "new_hero");
Trigger(PLAYER_ADD_HERO_TRIGGER, 2, "NewElf");
Trigger(PLAYER_ADD_HERO_TRIGGER, 3, "NewDemon");
startThread(AIPressingTownHolin,"Diraya");
startThread(noEmerald);
startThread(load_arts, "Agrael", path);
startThread(avenger_up);

if diff == 4 then
	Trigger(REGION_ENTER_AND_STOP_TRIGGER,"garrison", "NearCapitol");
end;

print("All started!");