path = "C6M3_";
new_path = "C6M4_";

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};

hard_spells = {3, 4, 6, 11, 12, 15, 17, 24, 25, 28, 31, 42, 237};
heroic_spells = {18, 19, 20, 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};

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

dark_spells = {11, 12, 13, 14, 15, 17, 18, 19, 20};
light_spells = {23, 24, 25, 26, 28, 31, 35, 48};
summon_spells = {39, 40, 42, 43, 235};

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");
end;

if GetDifficulty() == DIFFICULTY_HEROIC then
	dif = 3;
	print ("heroic");
end;

function map_up()
	sleep(1);
	Trigger(NEW_DAY_TRIGGER, "new_day");
	SetRegionBlocked("forbid", not nil, 4);
	SetRegionBlocked("forbid", not nil, 5);
	SetGameVar("C6M4_dif", dif);
	startThread(load_arts, "Zehir", path);
	startThread(load_arts, "Godric", path);
	startThread(load_arts, "Heam", path);
	EnableHeroAI("Isabell", nil);
	local heroes = {};
	local exp = 0.33 * (GetHeroStat("Zehir", 0) + GetHeroStat("Heam", 0) + GetHeroStat("Godric", 0));
	SetPlayerHeroesCountNotForHire(1, 6);
	if dif > 1 then
		heroes = GetPlayerHeroes(1);
		for i, hero in heroes do
			SetHeroCombatScript(hero, '/Maps/Scenario/C6M4/TalonCombat.xdb#xpointer(/Script)');
		end;
		for i=7,13 do
			SetTownBuildingLimitLevel("talonguard", i, 2);
		end;
		for i=0,5 do
			SetPlayerResource(3, i, 100*dif);
		end;
		for i=4,5 do
			DenyAIHeroesFlee(i,1);
			heroes = GetPlayerHeroes(i);
			for j=0,5 do
				SetPlayerResource(i, j, 50*dif);
			end;
			for j, hero in heroes do
				startThread(teach_spell, hero, hard_spells, dif+2);
				if dif == 3 then
					startThread(teach_spell, hero, heroic_spells, 3);
					startThread(give_art, hero, artefacts, 3);
					SetHeroLootable(hero, nil);
				end;
				ChangeHeroStat(hero, 0, 0.20 * exp * (dif-1));
				for k=1,4 do
					ChangeHeroStat(hero, k, dif);
				end;
			end;
		end;
		for i=1,4 do
			AddObjectCreatures("town"..i, 1, 20*dif);
			AddObjectCreatures("town"..i, 3, 15*dif);
			AddObjectCreatures("town"..i, 5, 10*dif);
			AddObjectCreatures("town"..i, 7, 4*dif);
			AddObjectCreatures("town"..i, 9, 2*dif);
			AddObjectCreatures("town"..i, 11, dif+random(2));
			if random(2) == 0 then
				AddObjectCreatures("town"..i, 13, 1+random(2));
			end;
		end;
		for i=5,8 do
			startThread(add_army, "town"..i, 0.75*(dif-1));
			if random(3) == 0 then
				AddObjectCreatures("town"..i, 89, 2*dif+random(3));
				else if random(2) == 0 then
					AddObjectCreatures("town"..i, 90, dif+random(4));
					else AddObjectCreatures("town"..i, 41, 1+random(3));
				end;
			end;
			if random(3) == 0 then
				AddObjectCreatures("town"..i, 38, 3*dif+random(3));
				else if random(2) == 0 then
					AddObjectCreatures("town"..i, 40, 2*dif+random(2));
					else AddObjectCreatures("town"..i, 36, 3*dif+random(5));
				end;
			end;
		end;
		if dif == 3 then
			for i=0,5 do
				SetPlayerResource(3, i, 200);
			end;
			SetHeroLootable("Isabell", nil);
			GiveArtefact("Isabell", 43);
			GiveArtefact("Isabell", 51);
			GiveArtefact("Isabell", 22);
			GiveArtefact("Isabell", 21);
			GiveArtefact("Isabell", 31);
			GiveArtefact("Isabell", 68);
			GiveArtefact("Isabell", 13);
			GiveArtefact("Isabell", 16);
			GiveArtefact("Isabell", 25);
			GiveArtefact("Isabell", 11);
		end;
	end;
	AddHeroCreatures("Isabell", CREATURE_MILITIAMAN, 200 + dif * 80);
	AddHeroCreatures("Isabell", CREATURE_MARKSMAN, 100 + dif * 40);
	AddHeroCreatures("Isabell", CREATURE_SWORDSMAN, 70 + dif * 20);
	AddHeroCreatures("Isabell", CREATURE_ROYAL_GRIFFIN, 50 + dif * 15);
	AddHeroCreatures("Isabell", CREATURE_CLERIC, 40 + dif * 10);
	AddHeroCreatures("Isabell", CREATURE_PALADIN, 30 + dif * 8);
	AddHeroCreatures("Isabell", CREATURE_ARCHANGEL, 6 + dif * 5);
end;

function new_hero(hero)
	if dif > 2 then
		SetHeroCombatScript(hero, '/Maps/Scenario/C6M4/TalonCombat.xdb#xpointer(/Script)');
	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);
					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, 90 do
			if GetObjectCreatures(mon, i) > 0 then
				AddObjectCreatures(mon, i, ind * GetObjectCreatures(mon, i));
			end;
		end;
	end;
end;

function new_day()
	if GetDate(DAY_OF_WEEK) == 1 then
		if GetObjectOwner("talonguard") ~= 1 then
			SetObjectDwellingCreatures("talonguard", 1, GetObjectDwellingCreatures("talonguard", 1) + 20+10*dif);
			SetObjectDwellingCreatures("talonguard", 3, GetObjectDwellingCreatures("talonguard", 3) + 15+8*dif);
			SetObjectDwellingCreatures("talonguard", 5, GetObjectDwellingCreatures("talonguard", 5) + 13+5*dif);
			SetObjectDwellingCreatures("talonguard", 7, GetObjectDwellingCreatures("talonguard", 7) + 10+4*dif);
			SetObjectDwellingCreatures("talonguard", 9, GetObjectDwellingCreatures("talonguard", 9) + 5+3*dif);
			SetObjectDwellingCreatures("talonguard", 11, GetObjectDwellingCreatures("talonguard", 11) + 4+2*dif);
			SetObjectDwellingCreatures("talonguard", 13, GetObjectDwellingCreatures("talonguard", 13) + 2+dif);
		end;
		if IsHeroAlive("Raelag") then
			local exp = 0.25 * (GetHeroStat("Zehir", 0) + GetHeroStat("Godric", 0) + GetHeroStat("Raelag", 0) + GetHeroStat("Heam", 0)) - GetHeroStat("Isabell", 0);
			if exp > 0 and dif > 1 then
				ChangeHeroStat("Isabell", 0, exp);
			end;
		end;
	end;
	if GetDate(DAY_OF_WEEK) == 2 then
		startThread(check_isabell);
	end;
	if GetPlayerResource(3,6) < 80000 then
		SetPlayerResource(3, 6, 200000);
	end;
end;

function check_isabell()
	for i, spel in isabell_spells do
		if KnowHeroSpell("Isabell", spel) then
			SetGameVar("C6M4_Isabell_spell_"..spel, 1);
			else SetGameVar("C6M4_Isabell_spell_"..spel, 0);
		end;
	end;
	if HasHeroSkill("Isabell", 55) then
		SetGameVar("C6M4_Isabell_charge", 1);
		else SetGameVar("C6M4_Isabell_charge", 0);
	end;
	if HasHeroSkill("Isabell", 56) then
		SetGameVar("C6M4_Isabell_pray", 1);
		else SetGameVar("C6M4_Isabell_pray", 0);
	end;
	if HasHeroSkill("Isabell", 75) then
		SetGameVar("C6M4_Isabell_encourage", 1);
		else SetGameVar("C6M4_Isabell_encourage", 0);
	end;
	if HasHeroSkill("Isabell", 10) then
		for i, spell in dark_spells do
			if KnowHeroSpell("Isabell", spell) == nil then
				TeachHeroSpell("Isabell", spell);
			end;
		end;
	end;
	if HasHeroSkill("Isabell", 11) then
		for i, spell in light_spells do
			if KnowHeroSpell("Isabell", spell) == nil then
				TeachHeroSpell("Isabell", spell);
			end;
		end;
	end;
	if HasHeroSkill("Isabell", 12) then
		for i, spell in summon_spells do
			if KnowHeroSpell("Isabell", spell) == nil then
				TeachHeroSpell("Isabell", spell);
			end;
		end;
		SetGameVar("C6M4_Isabell_wasp", 1);
		else SetGameVar("C6M4_Isabell_wasp", 0);
	end;
end;

function DeployDungeonArmy(hero)
	if GetObjectOwner(hero) == 1 then
		print("DeployDungeonArmy () started!");
		DeployReserveHero("Raelag", 36, 128, 0);
		DeployReserveHero("Kelodin", 33, 129, 0);
		EnableHeroAI("Raelag", nil);
		EnableHeroAI("Kelodin", nil);
		StartDialogScene("/DialogScenes/C6/M4/D2/DialogScene.xdb#xpointer(/DialogScene)")
		SetObjectiveState("prim3",OBJECTIVE_ACTIVE);
		sleep(1);
		MoveHeroRealTime("Raelag", 37, 128, 0);
		MoveHeroRealTime("Kelodin", 34, 129, 0);
		--OpenRegionFog(PLAYER_1, "region1");
		OpenCircleFog(36, 128, 0, 6, PLAYER_1);
		Trigger(REGION_ENTER_AND_STOP_TRIGGER , "deploy", nil);
	end;
end;
	
function RaelagTalks(nameHero)
	if GetObjectOwner(nameHero) ~= PLAYER_1 then
		return
	end;
	Trigger(REGION_ENTER_AND_STOP_TRIGGER, "region1", nil);
	StartDialogScene("/DialogScenes/C6/M4/D4/DialogScene.xdb#xpointer(/DialogScene)")
	sleep(1)
	UnreserveHero("Raelag");
	sleep(1)	
	SetObjectOwner("Raelag", PLAYER_1);
	sleep(1)	
	UnreserveHero("Kelodin");
	sleep(1)		
	SetObjectOwner("Kelodin", PLAYER_1);
	SetObjectiveState("prim4",OBJECTIVE_ACTIVE);
	ChangeHeroStat("Zehir", STAT_EXPERIENCE, 1366);
	SetObjectiveState("prim3",OBJECTIVE_COMPLETED);
	Trigger(REGION_ENTER_AND_STOP_TRIGGER, "region1", nil);
	startThread( objective4 );
	sleep(1);
	startThread(load_arts, "Raelag", "C4M5_");
	startThread(load_arts, "Kelodin", "C4M5_");
end;

function FinalDialogue()
	Save("Scene_19");
	sleep(1);
	startThread(save_artefact, "Godric", new_path, transfer_arts);
	startThread(save_artefact, "Heam", new_path, transfer_arts);
	startThread(save_artefact, "Raelag", new_path, transfer_arts);
	startThread(save_artefact, "Zehir", new_path, transfer_arts);
	sleep(2);
	StartDialogScene("/DialogScenes/C6/M4/D5/DialogScene.xdb#xpointer(/DialogScene)");
	sleep(8);
	StartCutScene("/Maps/Cutscenes/C6M4/_.(AnimScene).xdb#xpointer(/AnimScene)", "");
	sleep(5);
	SetObjectiveState('prim1',OBJECTIVE_COMPLETED);
	sleep(5);
	Win();
end

function cut()
	StartCutScene("/Maps/Cutscenes/C6M4/_.(AnimScene).xdb#xpointer(/AnimScene)");
end

---Are Primary Heroes Alive---
function objective2()
	if GetObjectiveState("prim2") == OBJECTIVE_UNKNOWN then
		SetObjectiveState('prim2',OBJECTIVE_ACTIVE);
		else
		print("Warning!!! prim2 is not UNKNOWN");
	end;
	while 1 do
		if IsHeroAlive("Zehir")==nil or IsHeroAlive("Godric")==nil or IsHeroAlive("Heam")==nil then
			print("One of the primary heroes is dead!!!");
			if GetObjectiveState("prim2") == OBJECTIVE_ACTIVE or GetObjectiveState("prim2") == OBJECTIVE_COMPLETED then
				SetObjectiveState("prim2",OBJECTIVE_FAILED);
				else
				print("Warning!!! prim2 is not ACTIVE or COMPLETED");
			end;
			return 1;
		end;
		if GetObjectiveState("prim1") == OBJECTIVE_COMPLETED and 
			GetObjectiveState("prim3") == OBJECTIVE_COMPLETED and
			GetObjectiveState("prim4") == OBJECTIVE_COMPLETED then
			if GetObjectiveState("prim2") == OBJECTIVE_ACTIVE  then
				SetObjectiveState("prim2",OBJECTIVE_COMPLETED);
				else
				print("Warning!!! prim2 is not ACTIVE");
			end;
			return 1;
		end;
	sleep();
	end;
end;

---Are Secondary Heroes Alive---
function objective4()
	while 1 do
		if IsHeroAlive("Raelag")==nil or IsHeroAlive("Kelodin")==nil then
			print("One of the secondary heroes is dead!!!");
			if GetObjectiveState("prim4") == OBJECTIVE_ACTIVE or GetObjectiveState("prim4") == OBJECTIVE_COMPLETED then
				SetObjectiveState("prim4",OBJECTIVE_FAILED);
				else
				print("Warning!!! prim4 is not ACTIVE or COMPLETED");
			end;
			return 1;
		end;
		if GetObjectiveState("prim1") == OBJECTIVE_COMPLETED and 
			GetObjectiveState("prim3") == OBJECTIVE_COMPLETED then
				if GetObjectiveState("prim4") == OBJECTIVE_ACTIVE  then
					SetObjectiveState('prim4',OBJECTIVE_COMPLETED);
					else
					print("Warning!!! prim4 is not ACTIVE");
				end;
			return 1;
		end;
	sleep();
end;
end;

function WinLoose()
	while 1 do
		-- if GetObjectiveState("prim1") == OBJECTIVE_COMPLETED and
			-- GetObjectiveState("prim3") == OBJECTIVE_COMPLETED then
			-- sleep(20);
			-- return
		-- end;
		if GetObjectiveState("prim1") == OBJECTIVE_FAILED or
			GetObjectiveState("prim2") == OBJECTIVE_FAILED or
			GetObjectiveState("prim3") == OBJECTIVE_FAILED or
			GetObjectiveState("prim4") == OBJECTIVE_FAILED then
			sleep(40);
			Loose();
			return
		end;
		sleep();
	end;
end;

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

Trigger(PLAYER_ADD_HERO_TRIGGER, 1, "new_hero");
Trigger(REGION_ENTER_AND_STOP_TRIGGER, "region1", "RaelagTalks");
Trigger(REGION_ENTER_AND_STOP_TRIGGER , "deploy", "DeployDungeonArmy");
Trigger(OBJECT_CAPTURE_TRIGGER, "talonguard", "FinalDialogue");
startThread( objective2 );
startThread( WinLoose );

--removing monsters off Isabell Army on EASY
startThread(map_up);