new_path = "C4M5_";
transfer_arts = {5, 13, 15, 18, 32, 61, 67};

demons = {"FAMILIAR", "IMP", "DEMON", "HOUND", "CERBERI", "SUCCUBUS", "NIGHTMARE", "FIEND", "BALOR", "DEVIL"};
demon_heroes = {"Deleb", "Marder", "Calid", "Jazaz", "Efion"};

hard_spells = {7, 8, 17, 19, 34, 40, 42, 43};
heroic_spells = {9, 10, 18, 20, 21, 235};

Raelag = "Raelag";
Kelodin = "Kelodin";  
Isabell = "Isabell"
Berein = "Berein"
Veyer = "Veyer"

SetPlayerResource(1, 0, 0);
SetPlayerResource(1, 1, 0);
SetPlayerResource(1, 2, 0);
SetPlayerResource(1, 3, 0);
SetPlayerResource(1, 4, 0);
SetPlayerResource(1, 5, 0);
SetPlayerResource(1, 6, 1000);

--difficulty
if GetDifficulty() == DIFFICULTY_EASY then
	diff = 0;
	print("isi");
end;

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

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

if GetDifficulty() == DIFFICULTY_HEROIC then
	diff = 3;
	for i=0,4 do
		SetPlayerResource(2,i,50);
	end;
	SetPlayerResource(2,6,100000);
	DenyAIHeroesFlee(2,1);
	print("heroic!");
end;

function demon_up()
	StartDialogScene("/DialogScenes/C4/M5/R1/DialogScene.xdb#xpointer(/DialogScene)")
	OpenCircleFog( 10, 29, 0, 5, PLAYER_1 );
	for i=1,5 do
		SetRegionBlocked("stop"..i, not nil, 2);
	end;
	SetRegionBlocked("c1", not nil, 2);
	SetRegionBlocked("c2", not nil, 2);
	SetRegionBlocked("v1", not nil, 2);
	SetRegionBlocked("v2", not nil, 2);
	sleep(2);
	if GetGameVar("C4M4_Raelag_AFH") == "1" then
		GiveArtefact("Raelag", 13);
	end;
	if GetGameVar("C4M4_Kelodin_AFH") == "1" then
		GiveArtefact("Kelodin", 13);
	end;
	for i, hero in demon_heroes do
		ChangeHeroStat(hero, 0, 60000+40000*diff);
		if diff > 0 then
			for j=1,4 do
				ChangeHeroStat(hero, j, diff);
			end;
		end;
		if diff > 1 then
			startThread(teach_spell, hero, hard_spells, 3);
			startThread(add_army, hero, 0.2 + 0.25*(diff-1));
			if diff > 2 then
				startThread(teach_spell, hero, heroic_spells, 2);
			end;
		end;
	end;
	if diff > 1 then
		startThread(add_demons, 0.5*(diff-1));
		if diff > 2 then
			SetHeroLootable("Efion", nil);
			SetHeroLootable("Jazaz", nil);
			SetHeroCombatScript("Efion", '/Maps/Scenario/C4M5/CombatVSVeyer.xdb#xpointer(/Script)');
			GiveHeroSkill("Efion", 3);
			GiveHeroSkill("Efion", 7);
			GiveHeroSkill("Efion", 10);
			GiveHeroSkill("Efion", 47);
			GiveHeroSkill("Efion", 60);
			GiveArtefact("Efion", 1);
			GiveArtefact("Efion", 8);
			GiveArtefact("Efion", 14);
			GiveArtefact("Efion", 16);
			GiveArtefact("Efion", 21);
			GiveArtefact("Efion", 55);
			TeachHeroSpell("Efion", 17);
			TeachHeroSpell("Efion", 18);
			TeachHeroSpell("Efion", 19);
			GiveHeroSkill("Jazaz", 2);
			GiveHeroSkill("Jazaz", 4);
			GiveHeroSkill("Jazaz", 5);
			GiveHeroSkill("Jazaz", 23);
			GiveHeroSkill("Jazaz", 59);
			GiveArtefact("Jazaz", 1);
			GiveArtefact("Jazaz", 9);
			GiveArtefact("Jazaz", 17);
			GiveArtefact("Jazaz", 22);
			GiveArtefact("Jazaz", 61);
			GiveArtefact("Jazaz", 66);
		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 add_army(mon, ind)
	if IsObjectExists(mon) then
		for i = 15, 28 do
			if GetObjectCreatures(mon, i) > 0 then
				AddObjectCreatures(mon, i, ind * GetObjectCreatures(mon, i));
			end;
		end;
	end;
end;

function add_demons(coef)
	local creatures = {};
	for i, name in demons do
		creatures = GetObjectNamesByType(name);
		if creatures ~= {} then
			for j, mon in creatures do
				startThread(add_army, mon, coef);
			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 PObjective1() 
	if IsObjectInRegion(Raelag, "PALAEDRA") and IsObjectInRegion(Kelodin, "PALAEDRA") then	
		print("PLAYER 1 in area.........................."); 
		sleep(2);
		SetObjectiveState('prim1',OBJECTIVE_COMPLETED);
		GiveExp( "Raelag", 3000 ); ---addexp!!!
		GiveExp( "Kelodin", 3000 ); ---addexp!!!
		ChangeHeroStat("Raelag", STAT_MORALE, 1);  -------Oblico_Morale!
		ChangeHeroStat("Kelodin", STAT_MORALE, 1);  -------Oblico_Morale!
		startThread(PObjective34); -----Palaedra!!!
	end;
end;


function PObjective2()  
	while 1 do
		sleep(10);
		if IsHeroAlive("Raelag") == nil or IsHeroAlive("Kelodin") == nil then   ----Заменить 2го героя!!!
			print("Heroes deads.................................");
			SetObjectiveState('prim2',OBJECTIVE_FAILED);
			break;
		end;
	end;
end;
---------------------------------------------Захват и разрушение замков!

function Castle_1()
	while 1 do	
		sleep( 10 );
		if (GetObjectOwner("Az_Hakol") == PLAYER_1) then
			startThread(raze_town, "Az_Hakol");
			sleep(20);
			StartDialogScene("/DialogScenes/C4/M5/R2/DialogScene.xdb#xpointer(/DialogScene)")  
			break;
		end;
	end;
end;

function Castle_2()
	while 1 do	
		sleep( 10 );
		if (GetObjectOwner("Mammon") == PLAYER_1) then
			startThread(raze_town, "Mammon");
			sleep(20);
			StartDialogScene("/DialogScenes/C4/M5/R3/DialogScene.xdb#xpointer(/DialogScene)")  
			break;
		end;
	end;
end;

function Castle_3()
	while 1 do	
		sleep( 10 );
		if (GetObjectOwner("Eh_Tares") == PLAYER_1) then	
			startThread(raze_town, "Eh_Tares");
			sleep(20);
			StartDialogScene("/DialogScenes/C4/M5/R4/DialogScene.xdb#xpointer(/DialogScene)")  
			break;
		end;
	end;
end;

function raze_town(town)
	BlockGame();
	local x,y = GetObjectPosition( town );
	x = x + 1.5;
	y = y + 0.5
	Play2DSound("/Sounds/_(Sound)/Spells/Earthquake.xdb#xpointer(/Sound)");
	PlayVisualEffect( "/Effects/_(Effect)/Buildings/Capture/Start_dust_S.xdb#xpointer(/Effect)", "","", x-1,y,0,random(360));
	PlayVisualEffect( "/Effects/_(Effect)/Buildings/Capture/Start_dust_S.xdb#xpointer(/Effect)", "","", x+1,y,0,random(360));
	PlayVisualEffect( "/Effects/_(Effect)/Buildings/Capture/Start_dust_S.xdb#xpointer(/Effect)", "","", x,y-1,0,random(360));
	PlayVisualEffect( "/Effects/_(Effect)/Buildings/Capture/Start_dust_S.xdb#xpointer(/Effect)", "","", x,y+1,0,random(360));
	PlayVisualEffect( "/Effects/_(Effect)/Spells/MeteorShower.xdb#xpointer(/Effect)", "","", x,y,0, random(360));
	Play2DSound("/Sounds/_(Sound)/Spells/MeteorShower.xdb#xpointer(/Sound)");
	PlayVisualEffect( "/Effects/_(Effect)/Characters/Heroes/DemonLord/Path/Level_2b.xdb#xpointer(/Effect)","","", x+2,y,0,random(360));
	sleep(1);
	PlayVisualEffect( "/Effects/_(Effect)/Spells/FireBallHit.xdb#xpointer(/Effect)","","", x+1,y-1,0,random(360));
	Play2DSound("/Sounds/_(Sound)/Spells/FireBallHit.xdb#xpointer(/Sound)");
	PlayVisualEffect( "/Effects/_(Effect)/Spells/Earthquake.xdb#xpointer(/Effect)","","", x,y,0, random(360));
	Play2DSound("/Sounds/_(Sound)/Spells/Earthquake.xdb#xpointer(/Sound)");
	sleep(1);
	PlayVisualEffect( "/Effects/_(Effect)/Characters/Heroes/DemonLord/Path/Level_2b.xdb#xpointer(/Effect)","","", x-2,y,0,random(360));
	PlayVisualEffect( "/Effects/_(Effect)/Spells/FireBallHit.xdb#xpointer(/Effect)","","", x-1,y+1,0,random(360));
	Play2DSound("/Sounds/_(Sound)/SFX/Seige-Destr/tower-crash.xdb#xpointer(/Sound)");
	sleep(1);
	PlayVisualEffect( "/Effects/_(Effect)/Characters/Heroes/DemonLord/Path/Level_2b.xdb#xpointer(/Effect)","","", x,y+2,0,random(360));
	PlayVisualEffect( "/Effects/_(Effect)/Spells/FireBallHit.xdb#xpointer(/Effect)","","", x+1,y+1,0,random(360));
	PlayVisualEffect( "/Effects/_(Effect)/Spells/FireBallHit.xdb#xpointer(/Effect)","","", x,y-2,0,random(360));
	sleep(1);
	PlayVisualEffect( "/Effects/_(Effect)/Characters/Heroes/DemonLord/Path/Level_2b.xdb#xpointer(/Effect)","","", x,y+1,0,random(360));
	Play2DSound("/Sounds/_(Sound)/Spells/FireBallHit.xdb#xpointer(/Sound)");
	sleep(1);
	PlayVisualEffect( "/Effects/_(Effect)/Characters/Heroes/DemonLord/Path/Level_2b.xdb#xpointer(/Effect)","","", x+1,y+1,0,random(360));
	PlayVisualEffect( "/Effects/_(Effect)/Spells/FireBallHit.xdb#xpointer(/Effect)","","", x,y-2,0,random(360));
	sleep(1);
	PlayVisualEffect( "/Effects/_(Effect)/Characters/Heroes/DemonLord/Path/Level_2b.xdb#xpointer(/Effect)","","", x-1,y-1,0,random(360));
	Play2DSound("/Sounds/_(Sound)/Spells/FireBallHit.xdb#xpointer(/Sound)");
	sleep(1);
	PlayVisualEffect( "/Effects/_(Effect)/Characters/Heroes/DemonLord/Path/Level_2b.xdb#xpointer(/Effect)","","", x+1,y-1,0,random(360));
	PlayVisualEffect( "/Effects/_(Effect)/Spells/FireBallHit.xdb#xpointer(/Effect)","","", x+1,y+1,0,random(360));
	sleep(1);
	PlayVisualEffect( "/Effects/_(Effect)/Characters/Heroes/DemonLord/Path/Level_2b.xdb#xpointer(/Effect)","","", x-1,y+1,0,random(360));
	RazeTown(town);
	UnblockGame();
end;

------------------------------------------------
function WinLoose_1()
	while 1 do
		if GetObjectiveState("prim2") == OBJECTIVE_FAILED then
			Loose();
			return
		end;
		sleep();
	end;
end;
------------------------------------------------
function Post_01()  
	while 1 do
		sleep(10);
		if IsPlayerHeroesInRegion(1, "post_1") == true then   
			SetObjectOwner("POST1",1);
			SetObjectOwner("POST2",1);
			break;
		end;
	end;
end;
------------------------------------------------------///////ACT2

function PObjective34()
	sleep(2);
	BlockGame();
	sleep(2);
	DeployReserveHero( "Veyer", 4, 31, GROUND );
	DeployReserveHero( "Isabell", 4, 24, GROUND );
	DeployReserveHero( "Berein", 3, 23, GROUND );
	EnableHeroAI("Isabell",nil);
	EnableHeroAI("Berein",nil);   
	sleep(6);
	if diff > 2 then
		SetHeroCombatScript("Veyer", '/Maps/Scenario/C4M5/CombatVSVeyer.xdb#xpointer(/Script)');
		AddObjectCreatures("Veyer", 18, 140 + 10 * random(5));
		AddObjectCreatures("Veyer", 26, 12 + random(5));
		GiveHeroSkill("Veyer", 59);
		GiveHeroSkill("Veyer", 19);
		GiveHeroSkill("Veyer", 32);
		GiveHeroSkill("Veyer", 86);
		GiveHeroSkill("Veyer", 91);
		GiveArtefact("Veyer", 2);
		GiveArtefact("Veyer", 58);
		GiveArtefact("Veyer", 21);
		GiveArtefact("Veyer", 59);
		GiveArtefact("Veyer", 62);
		GiveArtefact("Veyer", 27);
		GiveArtefact("Veyer", 56);
		GiveArtefact("Veyer", 19);
		GiveArtefact("Veyer", 66);
		ChangeHeroStat("Veyer", 5, 2);
		ChangeHeroStat("Veyer", 6, 2);
		TeachHeroSpell("Veyer", 7);
		TeachHeroSpell("Veyer", 8);
		TeachHeroSpell("Veyer", 9);
		TeachHeroSpell("Veyer", 17);
		TeachHeroSpell("Veyer", 18);
		TeachHeroSpell("Veyer", 19);
		TeachHeroSpell("Veyer", 20);
		TeachHeroSpell("Veyer", 40);
		TeachHeroSpell("Veyer", 43);
		TeachHeroSpell("Veyer", 235);
		startThread(add_army, "Veyer", 0.35);
		SetHeroLootable("Veyer", nil);
	end;
	MoveHeroRealTime( "Isabell", 5, 25, GROUND );
	MoveHeroRealTime( "Berein", 4, 23, GROUND );
	sleep(2);
	SetObjectiveState('prim3',OBJECTIVE_ACTIVE); 
	SetObjectiveState('prim4',OBJECTIVE_ACTIVE);
	sleep(1);
	UnblockGame();
	sleep(2);
	startThread(PObjective3_4complete);
	startThread(PObjective3_4defead);
	ChangeHeroStat("Raelag", STAT_MOVE_POINTS, 12000);
	local x,y = GetObjectPosition("Isabell");
	MoveHero("Veyer", x, y);
end;

function PObjective3_4complete()  
	while 1 do
		sleep(10);
		if IsHeroAlive("Veyer") == nil then 
			SetObjectiveState('prim3',OBJECTIVE_COMPLETED);
			--SetObjectiveState('prim4',OBJECTIVE_COMPLETED);
			ChangeHeroStat("Raelag", STAT_MORALE, 1);  -------Oblico_Morale!
			ChangeHeroStat("Kelodin", STAT_MORALE, 1);  -------Oblico_Morale!
			startThread(save_artefact, "Raelag", new_path, transfer_arts);
			startThread(save_artefact, "Kelodin", new_path, transfer_arts);
			sleep(6);
			Save("Save") 
			sleep(10);
			StartDialogScene("/DialogScenes/C4/M5/D1/DialogScene.xdb#xpointer(/DialogScene)")
			sleep(6);
			Win(); 
			break;
		end;	
	end;
end;

function PObjective3_4defead()  
	while 1 do
		sleep(10);
		if IsHeroAlive("Isabell") == nil then
			SetObjectiveState('prim4',OBJECTIVE_FAILED);
			sleep(20);
			Loose();
			break;
		end;
	end;
end;
--------------------------------//Main thread//
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "PALAEDRA","PObjective1", nil );  --PALAEDRA!!!

startThread(PObjective2);
startThread(Castle_1);
startThread(Castle_2);
startThread(Castle_3);
startThread(WinLoose_1);
startThread(Post_01);
startThread(demon_up);