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

ArtifactCollected = 0;
transfer_arts = {4, 13, 15, 21, 24};
new_path = "C5M4_";

demons = {"FAMILIAR", "IMP", "DEMON", "HOUND", "CERBERI", "SUCCUBUS", "NIGHTMARE", "FIEND", "BALOR", "DEVIL"};

if GetDifficulty() == DIFFICULTY_EASY then
	print("isi");
	diff = 0;
	ArmyMult = 0;
	RemoveObject('1alpha-wight');
	RemoveObject('6alpha-water');	
	RemoveObject('beta-liches');	
	RemoveObject('Delta-wight');	
end;

if GetDifficulty() == DIFFICULTY_NORMAL then
	print("normal");
	diff = 1;
	ArmyMult = 0;
	RemoveObject('1alpha-wight');
	RemoveObject('6alpha-water');	
	RemoveObject('beta-liches');	
	RemoveObject('Delta-wight');	
end;

if GetDifficulty() == DIFFICULTY_HARD then
	print("hard!");
	diff = 2;
	ArmyMult = 1;
	RemoveObject('1alpha-wight');
	RemoveObject('6alpha-water');
	RemoveObject('J-treant');
	AddObjectCreatures('earth_elem', CREATURE_EARTH_ELEMENTAL, 10);
	AddObjectCreatures('C-golem', CREATURE_STEEL_GOLEM, 50); --ohrana mashinnogo depo
	AddObjectCreatures('J-cerber', CREATURE_CERBERI, 35); -- ohrana spell powera
	AddObjectCreatures('K-dragon', CREATURE_SHADOW_DRAGON, 6); -- ohrana Ring of Life
	AddObjectCreatures('Delta-matron', CREATURE_MATRON, 5); -- podzemley. ohrana kmowlege
end;

if GetDifficulty() == DIFFICULTY_HEROIC then
	print("heroic!");
	diff = 3;
	ArmyMult = 2;
	RemoveObject('L1-sprites');
	RemoveObject('J-treant');
	AddObjectCreatures('earth_elem', CREATURE_EARTH_ELEMENTAL, 20); -- podzemniy prohod
	AddObjectCreatures('C-golem', CREATURE_STEEL_GOLEM, 150); --ohrana mashinnogo depo
	AddObjectCreatures('C-golem2', CREATURE_IRON_GOLEM, 180); --ohrana spell-powera
	AddObjectCreatures('Gamma-priest', CREATURE_PRIEST, 30); -- podzemley. ohrana nichki
	AddObjectCreatures('K-dragon', CREATURE_SHADOW_DRAGON, 13); -- ohrana Ring of Life
	AddObjectCreatures('Delta-matron', CREATURE_MATRON, 10); -- podzemley. ohrana kmowlege
	AddObjectCreatures('Delta-wight', CREATURE_WIGHT, 25); -- podzemley. mob na hodu
end;	
----------------------------------------------------------------------

function map_up()
	sleep(1);
	for i=0,6 do
		SetPlayerResource(1, i, 0);
	end;
	if GetGameVar("C5M3_Heam_Boots") == "1" then
		GiveArtefact("Heam", 24);
	end;
	if diff > 1 then
		startThread(add_demons, 0.5*(diff-1));
	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 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 CollectOne()
	print ('CollectOne started - ', ArtifactCollected);
	ArtifactCollected = ArtifactCollected +1;
	SetObjectiveProgress('Sec1', ArtifactCollected, PLAYER_1);
	print ('CollectOne ended - ', ArtifactCollected);
	ChangeHeroStat('Heam', STAT_EXPERIENCE, ArtifactCollected*5000);
	if ArtifactCollected == 4 then SetObjectiveState('Sec1', OBJECTIVE_COMPLETED); end;
end;
----------------------------------------------------------------------


function ComeToIsland()
	Trigger (REGION_ENTER_AND_STOP_TRIGGER, 'Tieru_Island', nil);
    StartDialogScene("/DialogScenes/C5/M4/R2/DialogScene.xdb#xpointer(/DialogScene)");
	SetObjectiveState('prim1', OBJECTIVE_COMPLETED);
	SetObjectiveState('prim2', OBJECTIVE_ACTIVE);
	DeployReserveHero('Jazaz', RegionToPoint('Jazaz_Deploy_Here'));
	DeployReserveHero('Biara', RegionToPoint('Biara_Deploy_Here'));
	sleep (3);
	EnableHeroAI('Jazaz', nil );
	EnableHeroAI('Biara', nil);
	if diff == DIFFICULTY_EASY then -- UMENSENIE ARMIY DEMONOV DLYA UROVNYA SLOJNOSTI "EASY"
		DemonHalfArmy ('Jazaz');
		DemonHalfArmy ('Biara');
	end;
	if ArmyMult > 0 then 	------ ADDITIONAL TROOPS FOR JAZAZ AND BIARA
		AddHeroCreatures('Jazaz', CREATURE_BALOR, 3*ArmyMult); --- PIT FIENDS
		AddHeroCreatures('Jazaz', CREATURE_CERBERI, 31*ArmyMult); --- CERBERI
		AddHeroCreatures('Jazaz', CREATURE_FRIGHTFUL_NIGHTMARE, 6*ArmyMult); --- HELL CHARGERS
		AddHeroCreatures('Jazaz', CREATURE_HORNED_DEMON, 32*ArmyMult); --- HORNED DEMONS
		if ArmyMult==2 then AddHeroCreatures('Jazaz', CREATURE_ARCHDEVIL, 2*ArmyMult)
		else AddHeroCreatures('Jazaz', CREATURE_DEVIL, 2*ArmyMult); end;
		AddHeroCreatures('Biara', CREATURE_INFERNAL_SUCCUBUS, 29*ArmyMult);
	end;
	startThread(demon_up);
	x, y, z = RegionToPoint('Biara_Here');
	OpenCircleFog(x, y, z, 11, PLAYER_1);
	x, y, z = RegionToPoint('Biara_Deploy_Here');
	OpenCircleFog(x, y, z, 8, PLAYER_1);
	x, y, z = RegionToPoint('Jazaz_Here');
	OpenCircleFog(x, y, z, 8, PLAYER_2);
	SetObjectEnabled('ExitToIsland', nil); -- There is no way back!  -- blokiruem vihod s ostrova.
	Trigger (OBJECT_TOUCH_TRIGGER, 'ExitToIsland', 'NoWayBack');
end;

function NoWayBack()
	MessageBox("/Maps/Scenario/C5M4/Texts/NoWay.txt");
end;

function ComeToTieru()
	Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'Jazaz_Here', nil);
	BlockGame(); 
	MoveHeroRealTime('Biara', RegionToPoint('Biara_Here'));
	sleep(10);
	MoveHeroRealTime('Jazaz', RegionToPoint('Jazaz_Here'));
	sleep(15);	
	UnblockGame();
	sleep(5);
	Trigger(REGION_ENTER_AND_STOP_TRIGGER, 'Cut_Scene2', 'TieruDeath');
end;

function TieruDeath ()
	Trigger (REGION_ENTER_AND_STOP_TRIGGER, 'Cut_Scene2', nil);
	sleep (3);
	BlockGame();  ----- DEATH OF TIERU - THE MOVIE -------------------------------------
	sleep (2);																		 --!
	leftmove = GetHeroStat ('Heam', STAT_MOVE_POINTS);								 --!
	ChangeHeroStat('Heam', STAT_MOVE_POINTS, 1000000);								 --!
	MoveHeroRealTime('Heam', RegionToPoint('Heam_Here'));	
	x, y, z = RegionToPoint('Biara_Here');
	MoveCamera(x, y, z, 65, 3.14/4, 3.14/2+(3.14/6));	--3.14-(3.14/4)				 --!
	--sleep (30); -- ??!?!
	sleep (1); 																		 --!
	SetStandState('TieruHut', 1);
	sleep(8);
	Play2DSound("/Sounds/_(Sound)/Spells/FireWall.xdb#xpointer(/Sound)");
	sleep(17);
	Play2DSound("/Sounds/_(Sound)/Spells/MeteorShower.xdb#xpointer(/Sound)");
	sleep(13);
	Play2DSound("/Sounds/_(Sound)/Spells/UnholyWord.xdb#xpointer(/Sound)");
	sleep(10);																		 --!
	MoveHeroRealTime('Biara', RegionToPoint('Biara_Step_Here'));					 --!
	x, y, z = RegionToPoint('Teleport_Here');										 --!
    y=y+0.5;																    	 --!
	SetObjectPosition ('Biara', x,y,z);												 --!
	UnblockGame();----------------------------------------------------------------------
	startThread(DemonShipCombat);
	ChangeHeroStat('Heam', STAT_MOVE_POINTS,-1000000);
	ChangeHeroStat('Heam', STAT_MOVE_POINTS,leftmove);
---------TEMP - ubrat 2 next strochki dlya testa
	sleep(10);
	Save('Will_of_Tieru');
    StartDialogScene("/DialogScenes/C5/M4/D1/DialogScene.xdb#xpointer(/DialogScene)");
	SetObjectiveState('prim2', OBJECTIVE_FAILED);
	SetObjectiveState('prim3', OBJECTIVE_ACTIVE);
	Trigger(PLAYER_REMOVE_HERO_TRIGGER, PLAYER_7, 'BiaraLoose')
	sleep(40);
	x, y, z = RegionToPoint('Teleport_Here');
	OpenCircleFog(x, y, z, 10, PLAYER_1);	
	MoveCamera(x, y, z, 40, 3.14/2, 3.14);
end;

function demon_up()
	if diff > 1 then
		sleep(1);
		ChangeHeroStat('Biara', 0, 0.25*(diff-1)*GetHeroStat("Heam", 0));
		TeachHeroSpell('Biara', 11);
		TeachHeroSpell('Biara', 12);
		TeachHeroSpell('Biara', 14);
		TeachHeroSpell('Biara', 15);
		TeachHeroSpell('Biara', 17);
		TeachHeroSpell('Biara', 19);
		TeachHeroSpell('Jazaz', 11);
		TeachHeroSpell('Jazaz', 12);
		TeachHeroSpell('Jazaz', 14);
		TeachHeroSpell('Jazaz', 15);
		TeachHeroSpell('Jazaz', 17);
		TeachHeroSpell('Jazaz', 19);
		TeachHeroSpell('Jazaz', 7);
		TeachHeroSpell('Jazaz', 8);
		TeachHeroSpell('Jazaz', 40);
		TeachHeroSpell('Jazaz', 42);
		TeachHeroSpell('Jazaz', 43);
		GiveHeroWarMachine('Biara', 1);
		GiveHeroWarMachine('Biara', 3);
		GiveHeroWarMachine('Biara', 4);
		GiveHeroWarMachine('Jazaz', 1);
		if diff > 2 then
			SetHeroCombatScript("Biara", '/Maps/Scenario/C5M4/BattleVSDemons.xdb#xpointer(/Script)');
			SetHeroCombatScript("Jazaz", '/Maps/Scenario/C5M4/BattleVSDemons.xdb#xpointer(/Script)');
			TeachHeroSpell('Biara', 20);
			TeachHeroSpell('Jazaz', 235);
			TeachHeroSpell('Biara', 34);
			GiveArtefact('Biara', 1);
			GiveArtefact('Biara', 13);
			GiveArtefact('Biara', 9);
			GiveArtefact('Biara', 19);
			GiveArtefact('Biara', 23);
			GiveArtefact('Biara', 63);
			GiveArtefact('Biara', 62);
			GiveArtefact('Biara', 27);
			GiveArtefact('Biara', 66);
			GiveArtefact('Jazaz', 5);
			GiveArtefact('Jazaz', 9);
			GiveArtefact('Jazaz', 14);
			GiveArtefact('Jazaz', 16);
			GiveArtefact('Jazaz', 20);
			GiveArtefact('Jazaz', 21);
			GiveArtefact('Jazaz', 55);
			GiveArtefact('Jazaz', 61);
			GiveArtefact('Jazaz', 62);
			SetHeroLootable('Biara', nil);
			SetHeroLootable('Jazaz', nil);
			AddHeroCreatures('Biara', 28, 4 + random(3));
		end;
		for i=1,4 do
			ChangeHeroStat('Biara', i, 1+diff);
			ChangeHeroStat('Jazaz', i, diff);
		end;
		ChangeHeroStat('Biara', 5, diff);
		ChangeHeroStat('Biara', 6, diff+1);
		ChangeHeroStat('Jazaz', 6, diff-1);
	end;
end;

function DemonShipCombat()
	if diff > 1 then
		DeployReserveHero('Oddrema', 124, 50, 0);
		sleep(1);
		EnableHeroAI('Oddrema', nil);
		startThread(follow, 'Oddrema', "Heam");
		TeachHeroSpell('Oddrema', 4);
		TeachHeroSpell('Oddrema', 6);
		TeachHeroSpell('Oddrema', 11);
		TeachHeroSpell('Oddrema', 12);
		TeachHeroSpell('Oddrema', 15);
		TeachHeroSpell('Oddrema', 17);
		TeachHeroSpell('Oddrema', 19);
		AddHeroCreatures('Oddrema', 22, 16 + 2 * random(3));
		if diff > 2 then
			SetHeroCombatScript("Oddrema", '/Maps/Scenario/C5M4/BattleVSDemons.xdb#xpointer(/Script)');
			TeachHeroSpell('Oddrema', 7);
			TeachHeroSpell('Oddrema', 8);
			TeachHeroSpell('Oddrema', 9);
			TeachHeroSpell('Oddrema', 18);
			TeachHeroSpell('Oddrema', 20);
			GiveHeroSkill('Oddrema', 3);
			GiveArtefact('Oddrema', 32);
			GiveArtefact('Oddrema', 55);
			GiveArtefact('Oddrema', 56);
			GiveArtefact('Oddrema', 59);
			GiveArtefact('Oddrema', 65);
			SetHeroLootable('Oddrema', nil);
			AddHeroCreatures('Oddrema', 28, 2 + random(2));
			GiveHeroWarMachine('Oddrema', 4);
		end;
		ChangeHeroStat('Oddrema', 0, 0.25*(diff-1) * GetHeroStat("Heam", 0));
		for i=1,4 do
			ChangeHeroStat('Oddrema', i, diff);
		end;
		ChangeHeroStat('Oddrema', 5, diff-1);
	end;
end;

function follow(hero, target)
	local player = GetObjectOwner(hero);
	while IsHeroAlive(hero) do
		sleep(5);
		while GetCurrentPlayer() ~= player do
			sleep(2);
		end;
		if IsHeroAlive(hero) then
			if IsObjectExists(target) then
				MoveHero(hero, GetObjectPosition(target));
				else EnableHeroAI(hero, not nil); break
			end;
		end;
	end;
end;

function BiaraLoose(LooseHero, WinnerHero)
	if LooseHero == 'Biara' then
		StartDialogScene("/DialogScenes/C5/M4/D2/DialogScene.xdb#xpointer(/DialogScene)");
		startThread(save_artefact, "Heam", new_path, transfer_arts);
		sleep (20);
		ChangeHeroStat('Heam', STAT_EXPERIENCE, 1000);
		SetObjectiveState('prim3', OBJECTIVE_COMPLETED);
		Win ();
	end;
end;

--------------------------------------------------
function DemonHalfArmy(heroname) -- functiya upolovinivaet armiyu heroname-a (esli on demon)
	DividingNumber = 2;
	local creatures = {
	CREATURE_FAMILIAR,
	CREATURE_IMP,
	CREATURE_DEMON,
	CREATURE_HORNED_DEMON,
	CREATURE_HELL_HOUND,
	CREATURE_CERBERI,
	CREATURE_SUCCUBUS,
	CREATURE_INFERNAL_SUCCUBUS,
	CREATURE_NIGHTMARE,
	CREATURE_FRIGHTFUL_NIGHTMARE,
	CREATURE_PIT_FIEND,
	CREATURE_BALOR,
	CREATURE_DEVIL,
	CREATURE_ARCHDEVIL	
	}
	for index, creature in creatures do
		local count = GetHeroCreatures(heroname, creature) / DividingNumber;
		if count > 0 then
			RemoveHeroCreatures(heroname, creature, count);
		end;
	end;
 end;
 
Trigger (REGION_ENTER_AND_STOP_TRIGGER, 'Jazaz_Here', 'ComeToTieru');
Trigger (REGION_ENTER_AND_STOP_TRIGGER, 'Tieru_Island', 'ComeToIsland');
Trigger (OBJECT_TOUCH_TRIGGER, 'Armor', 'CollectOne');
Trigger (OBJECT_TOUCH_TRIGGER, 'Pendant_of_Mastery', 'CollectOne');
Trigger (OBJECT_TOUCH_TRIGGER, 'Bow', 'CollectOne');
Trigger (OBJECT_TOUCH_TRIGGER, 'Ring_of_Life', 'CollectOne');

startThread(map_up);
---------------------------------------------------