--Save("Scene 10: Appearance of Realag")

Raelag = "Raelag"
Urunir = "Urunir"
Almegir = "Almegir"
Inagost = "Inagost"
Ohtarig = "Ohtarig"
Menel   = "Menel"
Ferigl = "Ferigl"
Eruina = "Eruina"

Heroes = {"Raelag", "Urunir", "Almegir", "Inagost", "Ohtarig", "Menel", "Ferigl", "Eruina"};
towns = {"second", "third", "fourth", "fifth", "sixth", "seventh", "Eighth"};

hard_spells = {5, 6, 14, 17, 28, 39, 42, 237};
heroic_spells = {7, 8, 18, 19, 34, 40, 43, 236};
artefacts = {1, 5, 8, 9, 14, 16, 17, 18, 19, 21, 24, 26, 27, 32, 55, 56, 57, 59, 60, 61, 62, 64, 65, 66};

----------------------------------
timer=nil
heroname = "Raelag"

----------------------------------
if GetDifficulty() == DIFFICULTY_EASY then
	diff = 0;
	print("isi");
	for i, town in towns do
		SetTownBuildingLimitLevel(town, 1, 1);
		SetTownBuildingLimitLevel(town, 6, 2);
		SetTownBuildingLimitLevel(town, 11, 1);
		SetTownBuildingLimitLevel(town, 12, 0);
		SetTownBuildingLimitLevel(town, 19, 0);
		SetTownBuildingLimitLevel(town, 20, 0);
	end;
end;

if GetDifficulty() == DIFFICULTY_NORMAL then
	diff = 1;
	print("normal");
	for i, town in towns do
		SetTownBuildingLimitLevel(town, 1, 2);
		SetTownBuildingLimitLevel(town, 6, 3);
		SetTownBuildingLimitLevel(town, 12, 1);
		SetTownBuildingLimitLevel(town, 20, 0);
	end;
end;

if GetDifficulty() == DIFFICULTY_HARD then
	diff = 2;
	print("hard!");
	for i, town in towns do
		SetTownBuildingLimitLevel(town, 20, 0);
	end;
end;

if GetDifficulty() == DIFFICULTY_HEROIC then
	diff = 3;
	for i, town in towns do
		if random(3) > 0 then
			SetTownBuildingLimitLevel(town, 20, 0);
		end;
	end;
	for i=2,8 do
		for j=0,5 do
			SetPlayerResource(i,j,50);
		end;
		SetPlayerResource(i,6,20000);
	end;
	print("heroic!");
end;

function warlok_up()
	for i, hero in Heroes do
		if hero ~= Raelag then
			ChangeHeroStat(hero, 0, 1000 + diff * 2000);
			if diff > 1 then
				startThread(teach_spell, hero, hard_spells, 3);
				startThread(warmachine, hero, diff - 1);
				startThread(first_point, hero);
				if diff > 2 then
					SetHeroLootable(hero, nil);
					startThread(teach_spell, hero, heroic_spells, 2);
					startThread(give_art, hero, artefacts, 3);
					for j=1,4 do
						if random(2) == 0 then
							ChangeHeroStat(hero, j, 1);
						end;
					end;
				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 warmachine(hero, num)
	if IsHeroAlive(hero) then
		for i=1,4 do
			if random(num) > 0 and i ~= 2 then
				if HasHeroWarMachine(hero, i) == nil then
					GiveHeroWarMachine(hero, i);
				end;
			end;
		end;
	end;
end;

function first_point(hero)
	if IsHeroAlive(hero) then
		local x,y,z = GetObjectPosition(hero);
		while GetDate(MONTH) < 2 do
			sleep(20);
		end;
		if IsHeroAlive(hero) then
			SetObjectPosition(hero, x, y, z);
			print("teleporting hero "..hero);
		end;
	end;
end;

function no_diplomat(hero)
	if IsHeroAlive(hero) then
		for i=1,70 do
			if GetObjectCreatures(hero, i) > 0 then
				RemoveObjectCreatures(hero, i, GetObjectCreatures(hero, i));
			end;
		end;
	end;
end;
---------------------------------

function messagesC2_C3()
	if ( GetDate(MONTH) == 1 ) and ( GetDate(WEEK) == 2 ) and (GetDate(DAY_OF_WEEK) == 2 ) then
		MessageBox ("/Maps/Scenario/C4M1/Message/C4M1_C2_1.txt");
	end;
	if ( GetDate(MONTH) == 1 ) and ( GetDate(WEEK) == 3 ) and (GetDate(DAY_OF_WEEK) == 2 ) then
		MessageBox ("/Maps/Scenario/C4M1/Message/C4M1_C2_2.txt");
	end;
	if ( GetDate(MONTH) == 1 ) and ( GetDate(WEEK) == 4 ) and (GetDate(DAY_OF_WEEK) == 2 ) then
		MessageBox ("/Maps/Scenario/C4M1/Message/C4M1_C2_3.txt");
	end;
	if ( GetDate(MONTH) == 1 ) and ( GetDate(WEEK) == 4 ) and (GetDate(DAY_OF_WEEK) == 3 ) then
		MessageBox ("/Maps/Scenario/C4M1/Message/C4M1_C3_1.txt");
	end;
	if ( GetDate(MONTH) == 1 ) and ( GetDate(WEEK) == 4 ) and (GetDate(DAY_OF_WEEK) == 4 ) then
		MessageBox ("/Maps/Scenario/C4M1/Message/C4M1_C3_2.txt");
	end;
	if ( GetDate(MONTH) == 1 ) and ( GetDate(WEEK) == 4 ) and (GetDate(DAY_OF_WEEK) == 5 ) then
		MessageBox ("/Maps/Scenario/C4M1/Message/C4M1_C3_3.txt");
	end;
	if ( GetDate(MONTH) == 1 ) and ( GetDate(WEEK) == 4 ) and (GetDate(DAY_OF_WEEK) == 6 ) then
		MessageBox ("/Maps/Scenario/C4M1/Message/C4M1_C3_4.txt");
	end;
	if ( GetDate(MONTH) == 1 ) and ( GetDate(WEEK) == 4 ) and (GetDate(DAY_OF_WEEK) == 7 ) then
		MessageBox ("/Maps/Scenario/C4M1/Message/C4M1_C3_5.txt");
	end;
	if ( GetDate(MONTH) == 2 ) and ( GetDate(WEEK) == 1 ) and (GetDate(DAY_OF_WEEK) == 1 ) then
		MessageBox ("/Maps/Scenario/C4M1/Message/C4M1_C3_6.txt");
		Save("Save1") ---предохраняемся на всякий казуальный случай
	end;
	if ( GetDate(MONTH) == 2 ) and ( GetDate(WEEK) == 1 ) and (GetDate(DAY_OF_WEEK) == 2 ) then
		sleep(2);
		print ('Prepare');
		MessageBox ("/Maps/Scenario/C4M1/Message/C4M1_C4.txt");
		for i, hero in Heroes do
			if hero ~= Raelag then
				EnableHeroAI(hero, nil);
			end;
		end;
		sleep(8);
		startThread(Moveheroes1);
		SetObjectiveState('prim1',OBJECTIVE_COMPLETED);
		ChangeHeroStat("Raelag", STAT_LUCK, 1);  -------Повышаем мораль за комплит задания
		SetObjectiveState('prim2',OBJECTIVE_ACTIVE); ---Выдаём задание 2
	end;
	for i, hero in Heroes do
		if hero ~= Raelag and GetDate(MONTH) == 1 then
			startThread(no_diplomat, hero);
		end;
	end;
end;

------------------------------------Герои телепортятся в подземку
function Moveheroes1()
sleep(3);
print ('Prepare');
SetObjectPosition("Raelag", 142, 163, 0);
print("Go!!!.................................");
sleep(3);
SetObjectOwner("first",PLAYER_NONE);
startThread(Goto);
end;

function Goto()
	while 1 do	
		sleep( 2 );	
		if IsPlayerHeroesInRegion(1, "Level") == true then
			OpenRegionFog (1, "Arena");
			OpenRegionFog (1, "Dragon");
			MoveCamera(91, 63, 1, 50, 1);
			startThread(Moveheroes2);
			startThread(Moveheroes3);
			startThread(Moveheroes4);
			startThread(Moveheroes5);
			startThread(Moveheroes6);
			startThread(Moveheroes7);
			startThread(Moveheroes8);
			for i, hero in Heroes do
				if hero ~= Raelag then
					EnableHeroAI(hero, not nil);
				end;
			end;
			sleep(40);
			startThread(castles_none);
			break;
		end;
	end;
end;

function castles_none()
	for i, town in towns do
		SetObjectOwner(town, 0);
	end;
end;
------------------------------------ 
function Moveheroes2()
	while 1 do
		sleep(10);
		if IsHeroAlive("Eruina") == true then
			SetObjectPosition("Eruina", 98, 67, 1);
			AddHeroCreatures("Eruina", CREATURE_ASSASSIN, 10 + 5 * diff + random(10));
			AddHeroCreatures("Eruina", CREATURE_CHAOS_HYDRA, 2 + diff + random(2));
			AddHeroCreatures("Eruina", CREATURE_RAVAGER, 2 + diff + random(3) );
			AddHeroCreatures("Eruina", CREATURE_MINOTAUR_KING, 2 + 2 * diff + random(7));
			break;
		end;
	end;
end;

function Moveheroes3()
	while 1 do
		sleep(11);
		if IsHeroAlive("Urunir") == true then
			SetObjectPosition("Urunir", 98, 58, 1);
			AddHeroCreatures("Urunir", CREATURE_ASSASSIN, 3 + 2 * diff + random(7));
			AddHeroCreatures("Urunir", CREATURE_RAVAGER, 2 + diff + random(2));
			AddHeroCreatures("Urunir", CREATURE_MATRON, 1 + diff + random(2));
			break;
		end;
	end;
end;

function Moveheroes4()
	while 1 do
		sleep(13);
		if IsHeroAlive("Almegir") == true then
			SetObjectPosition("Almegir", 95, 54, 1);
			AddHeroCreatures("Almegir", CREATURE_ASSASSIN, 4 + 2 * diff + random(6));
			AddHeroCreatures("Almegir", CREATURE_MATRON, 1 + diff + random(2));
			AddHeroCreatures("Almegir", CREATURE_MINOTAUR_KING, 4 + 3 * diff + random(8));
			break;
		end;
	end;
end;

function Moveheroes5()
	while 1 do
		sleep(14);
		if IsHeroAlive("Inagost") == true then
			SetObjectPosition("Inagost", 86, 54, 1);
			AddHeroCreatures("Inagost", CREATURE_ASSASSIN, 7 + 3 * diff + random(6));
			AddHeroCreatures("Inagost", CREATURE_MINOTAUR_KING, 2 + 2 * diff + random(5));
			AddHeroCreatures("Inagost", CREATURE_BLOOD_WITCH, 2 + 2 * diff + random(7));
			break;
		end;
	end;
end;

function Moveheroes6()
	while 1 do
		sleep(15);
		if IsHeroAlive("Ohtarig") == true then
			SetObjectPosition("Ohtarig", 83, 59, 1);
			AddHeroCreatures("Ohtarig", CREATURE_BLOOD_WITCH, 12 + 4 * diff + random(7));
			AddHeroCreatures("Ohtarig", CREATURE_MATRIARCH, 1 + random(2));
			AddHeroCreatures("Ohtarig", CREATURE_RAVAGER, 7 + 3 * diff + random(3));
			AddHeroCreatures("Ohtarig", CREATURE_ASSASSIN, 5 + 5 * diff + random(6));
			break;
		end;
	end;
end;

function Moveheroes7()
	while 1 do
		sleep(16);
		if IsHeroAlive("Menel") == true then
			SetObjectPosition("Menel", 83, 68, 1);
			AddHeroCreatures("Menel", CREATURE_HYDRA, 1 + diff + random(2));
			AddHeroCreatures("Menel", CREATURE_MINOTAUR_KING, 2 + 2 * diff + random(4));
			AddHeroCreatures("Menel", CREATURE_BLOOD_WITCH, 2 + 2 * diff + random(7));
			break;
		end;
	end;
end;

function Moveheroes8()
	while 1 do
		sleep(16);
		if IsHeroAlive("Ferigl") == true then
			SetObjectPosition("Ferigl", 86, 71, 1);
			AddHeroCreatures("Ferigl", CREATURE_HYDRA, 2 + diff + random(3));
			AddHeroCreatures("Ferigl", CREATURE_MINOTAUR_KING, 2 + 2 * diff + random(5));
			AddHeroCreatures("Ferigl", CREATURE_BLOOD_WITCH, 1 + 2 * diff + random(6));
			AddHeroCreatures("Ferigl", CREATURE_ASSASSIN, 1 + 3 * diff + random(7));
			sleep(10);
			timer=GetDate(ABSOLUTE_DAY)+10   ----таймер старта драки
			startThread(Finalcombat); ----------натравливаем двух последних героев
			startThread(fight);
			break;
		end;
	end;
end;
----------------------------------------------
function Prim2_complit()
	while 1 do
		sleep(10);
		if HasArtefact("Raelag",ARTIFACT_RING_OF_THE_SHADOWBRAND) then
			StartDialogScene("/DialogScenes/C4/M1/R2/DialogScene.xdb#xpointer(/DialogScene)")  ---Сцена получения артефакта
			SetObjectiveState('prim2',OBJECTIVE_COMPLETED);
			break;
		end;
	end;
end;

-----------------------------------------Win_Lose
function Dead_hero()
	while 1 do
		sleep(10);
		if IsHeroAlive("Raelag") == nil then
			print("Heroes deads.................................");
			SetObjectiveState('prim3',OBJECTIVE_FAILED);
			break;
		end;
	end;
end;

function WinLoose()
	while 1 do
		if GetObjectiveState("prim2") == OBJECTIVE_COMPLETED then
			GiveExp( "Raelag", 500 ); ---addexp!!!
			sleep(10);
			Win();
			return
		end;
		if GetObjectiveState("prim3") == OBJECTIVE_FAILED then
			Loose();
			return
		end;
		sleep();
	end;
end;
------------------------------Драконы сваливают домой и открывют проход
function Dooropen()
	while 1 do
		sleep(10);
		if IsHeroAlive("Urunir") == nil and IsHeroAlive("Eruina") == nil and IsHeroAlive("Almegir") == nil and IsHeroAlive("Inagost") == nil and IsHeroAlive("Ohtarig") == nil and IsHeroAlive("Menel") == nil then
			OpenRegionFog (1, "Arena2");
			SetObjectPosition("Drag", 122, 97, 1);
			break;
		end;
	end;
end;
--------------------------------Последние герои

function Finalcombat()
	while 1 do
		sleep(4);
		if GetDate(ABSOLUTE_DAY)==timer then
			print ('Start_special_action....');
--			EnableDynamicBattleMode(1);  ---------------------Динамический бой включаем!
			startThread(EngageHero1);
			startThread(EngageHero2);
			sleep(1);
			break;
		end;
	end;
end;

function fight()
	if IsHeroAlive("Urunir") ~= nil or IsHeroAlive("Eruina") ~= nil or IsHeroAlive("Almegir") ~= nil or IsHeroAlive("Inagost") ~= nil or IsHeroAlive("Ohtarig") ~= nil or IsHeroAlive("Menel") ~= nil then
		fight1 = random (2)+1;
		startThread(fighter);
	end;
end;

function fighter()
	fighter1 = random (7)+2;
	if IsHeroAlive (Heroes[fighter1]) then
		startThread(target);
		print ("figter ", Heroes[fighter1]);
	else
		startThread(fighter);
	end;
end;

function target()
	target1 = random (7)+1;
	if IsHeroAlive (Heroes[target1]) and Heroes[target1] ~= Heroes[fighter1]then
		startThread(fightkill);
		print ("target ", Heroes[target1]);
	else
		startThread(target);
	end;
end;

function fightkill()
	if fight1 > 0 then
		print ("go-go-go");
		tar = Heroes[target1]
		fig = Heroes[fighter1]
		MoveHero(fig, GetObjectPosition(tar));
		fight1 = fight1 - 1;
		startThread(fighter);
		Trigger( NEW_DAY_TRIGGER, "fight" );
	end;
end;

function EngageHero1()
	while IsHeroAlive ("Eruina") do
		while GetCurrentPlayer() ~= PLAYER_2 do
			sleep( 1 );
		end;
		MoveHero( "Eruina", GetObjectPosition( heroname ) );
			while GetCurrentPlayer() ~= PLAYER_1 do
			sleep( 1 );
		end;
	end;
end;

function EngageHero2()
	while IsHeroAlive ("Ohtarig") do
		while GetCurrentPlayer() ~= PLAYER_6 do
			sleep( 1 );
		end;
		MoveHero( "Ohtarig", GetObjectPosition( heroname ) );
			while GetCurrentPlayer() ~= PLAYER_1 do
			sleep( 1 );
		end;
	end;
end;
--------------------------------Запускаем
StartDialogScene("/DialogScenes/C4/M1/D1/DialogScene.xdb#xpointer(/DialogScene)", "", "Save2")  ----Сцена стартовая
--Save("Save2")

Trigger( NEW_DAY_TRIGGER, "messagesC2_C3",nil );

startThread(warlok_up);

startThread(Prim2_complit);
startThread(Dead_hero);
startThread(WinLoose);

startThread(Dooropen);