obj01 = 0;
obj03 = 0;
obj04 = 0;
newday = 1;
heroarray = {};
enemyhero = {};
attackday = 1;
atk_type = 0;
DayX = 10000;
RND = random(7)+1;
was = 0;
NameH = "Heam";
Defeat = 0;
b = 0;

artefacts = {1, 5, 8, 9, 14, 16, 18, 20, 27, 32, 34, 55, 56, 57, 60, 61, 62, 64, 65, 66};
hard_spells = {5, 6, 11, 12, 14, 15, 17, 19, 34, 43, 236, 237};
heroic_spells = {7, 8, 18, 20, 40, 43, 235};

SetRegionBlocked("prison2", 1, 2);
SetRegionBlocked("prison1", 1, 2);
SetRegionBlocked("block", 1, 1);

nemor_first = 0;
gles_first = 0;

for a = 43,54 do
	SetObjectDwellingCreatures("Siris", a , 0);
end;

if GetDifficulty() == DIFFICULTY_EASY then
	dif = -2;
	print ("easy");
	SetPlayerHeroesCountNotForHire (2, 6);
	AddObjectCreatures(NameH, CREATURE_SPRITE, 130);
	AddObjectCreatures(NameH, CREATURE_WAR_DANCER, 108);
	AddObjectCreatures(NameH, CREATURE_GRAND_ELF, 86);
	AddObjectCreatures(NameH, CREATURE_DRUID_ELDER, 62);
	AddObjectCreatures(NameH, CREATURE_WAR_UNICORN , 44);
	AddObjectCreatures(NameH, CREATURE_TREANT_GUARDIAN , 21);
end;

if GetDifficulty() == DIFFICULTY_NORMAL then
	dif = 0;
	print ("normal");
	SetPlayerHeroesCountNotForHire (2, 6);
	AddObjectCreatures(NameH, CREATURE_SPRITE, 100);
	AddObjectCreatures(NameH, CREATURE_WAR_DANCER, 71);
	AddObjectCreatures(NameH, CREATURE_GRAND_ELF, 58);
	AddObjectCreatures(NameH, CREATURE_DRUID_ELDER, 41);
	AddObjectCreatures(NameH, CREATURE_WAR_UNICORN , 29);
end;

if GetDifficulty() == DIFFICULTY_HARD then
	dif = 2;
	print ("hard");
	SetPlayerHeroesCountNotForHire (2, 5);
	AddObjectCreatures(NameH, CREATURE_SPRITE, 30);
	AddObjectCreatures(NameH, CREATURE_WAR_DANCER, 25);
	AddObjectCreatures(NameH, CREATURE_GRAND_ELF, 20);
	AddObjectCreatures(NameH, CREATURE_DRUID_ELDER, 15);
	AddObjectCreatures(NameH, CREATURE_WAR_UNICORN , 10);
end;

if GetDifficulty() == DIFFICULTY_HEROIC then
	dif = 3;
	print ("heroic");
	SetPlayerHeroesCountNotForHire (2, 4);
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);
					print("give artefact "..art.." to hero "..hero);
					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 map_up()
	sleep(1);
	if GetGameVar("C5M2_Heam_Boots") == "1" then
		GiveArtefact("Heam", 24);
	end;
	local heroes = GetPlayerHeroes(2);
	local exp = GetHeroStat("Heam", 0);
	if dif > 1 then
		DenyAIHeroesFlee(2,1);
		DenyAIHeroesFlee(3,1);
		for i, hero in heroes do
			ChangeHeroStat(hero, 0, exp/(5-dif));
		end;
		for i=1,4 do
			ChangeHeroStat("Effig", i, dif - 1);
		end;
		sleep(1);
		ChangeHeroStat("Effig", 8, 10*GetHeroStat("Effig", 4)-GetHeroStat("Effig", 8));
	end;
end;

function SirisCapture( oldOwner, newOwner )
	if ( newOwner == PLAYER_2 or newOwner == PLAYER_3 ) then -- enemy has captured Imarium
		if ( obj01 == 1 ) then
			Loose();
		else
			MoveHeroRealTime("Effig", 80 , 125);
			StartDialogScene("/DialogScenes/C5/M3/R1/DialogScene.xdb#xpointer(/DialogScene)");
			SetObjectiveState("CaptureImarium", OBJECTIVE_ACTIVE );
			if dif > 1 then
				ChangeHeroStat("Effig", 5, dif - 1);
			end;
			ChangeHeroStat("Effig", 8, 10*GetHeroStat("Effig", 4)-GetHeroStat("Effig", 8));
		end;
	end;
	if ( newOwner == PLAYER_1 ) then -- player has captured Imarium
		obj01 = 1;
		Save("autosave");
		StartDialogScene("/DialogScenes/C5/M3/D1/DialogScene.xdb#xpointer(/DialogScene)");
		SetObjectiveState("CaptureImarium", OBJECTIVE_COMPLETED);
		sleep (5);
		Reso = GetPlayerResource(PLAYER_1, GOLD) + 5000;
		SetPlayerResource(PLAYER_1, GOLD, Reso);
		SetObjectiveState("DefendImarium", OBJECTIVE_ACTIVE );
--		SetRegionBlocked("enemy", nil);
		DayX = GetDate(DAY)+3;
		startThread(attack, 0);
    end;
end;

function attack( param )
	week = GetDate(MONTH)*4 - 4 + GetDate(WEEK);
	if param == 0 then
		if IsHeroAlive("Nemor") == nil then
			DeployReserveHero("Nemor", RegionToPoint("Start"));
			sleep ( 1 );
			if nemor_first == 0 then
				if dif > 1 then
					startThread(teach_spell, "Nemor", hard_spells, 4);
					for i=1,4 do
						ChangeHeroStat("Nemor", i, dif - 1);
					end;
				end;
				if dif == 3 then
					startThread(give_art, "Nemor", artefacts, 2 + dif);
					SetHeroLootable("Nemor", nil);
					startThread(teach_spell, "Nemor", heroic_spells, 2);
					TeachHeroSpell("Nemor", 41);
					TeachHeroSpell("Nemor", 43);
					GiveHeroSkill("Nemor", 8);
					GiveHeroSkill("Nemor", 9);
					GiveHeroSkill("Nemor", 12);
					GiveHeroSkill("Nemor", 47);
				end;
				nemor_first = 1;
			end;
			exp = GetHeroStat("Heam", STAT_EXPERIENCE) - GetHeroStat("Nemor", 0);
			if exp > 10000 then
				ChangeHeroStat("Nemor", STAT_EXPERIENCE , exp - 5000 + random(10) * 500);
			end;
			AddObjectCreatures("Nemor", 30, (23 + dif)*week);
			AddObjectCreatures("Nemor", 32, (16 + dif)*week);
			AddObjectCreatures("Nemor", 33, (12 + dif)*week);
			AddObjectCreatures("Nemor", 35, 7*week  + dif);
			AddObjectCreatures("Nemor", 37, 5*week + dif);
			EnableHeroAI("Nemor", nil);
			startThread(MoveHeroPatch, "Nemor", "Siris");
			if dif > 1 then
				ChangeHeroStat("Nemor", 5, 1 + random(2));
				AddObjectCreatures("Nemor", 38, 2 * week);
				if dif == 3 then
					SetHeroCombatScript("Nemor", '/Maps/Scenario/C5M3/NecroCombat.xdb#xpointer(/Script)');
				end;
				if random(2) == 0 then
					sleep(1);
					if random(2) == 0 then
						RemoveObjectCreatures("Nemor", 29, GetObjectCreatures("Nemor", 29));
						else RemoveObjectCreatures("Nemor", 35, GetObjectCreatures("Nemor", 35));
					end;
					sleep(1);
					if random(3) == 0 then
						AddObjectCreatures("Nemor", 40, dif + 2 * week);
						else if random(2) == 0 then
							AddObjectCreatures("Nemor", 41, dif - 1 + week);
							else AddObjectCreatures("Nemor", 90, dif - 1 + week);
						end;
					end;
				end;
				startThread(give_mana, "Nemor");
			end;
		end;
	else
		if IsHeroAlive("Gles") == nil then
			DeployReserveHero("Gles", RegionToPoint("Start"));
			sleep ( 1 );
			if gles_first == 0 then
				if dif > 1 then
					startThread(teach_spell, "Gles", hard_spells, 4);
					for i=1,4 do
						ChangeHeroStat("Gles", i, dif - 1);
					end;
				end;
				if dif == 3 then
					startThread(give_art, "Gles", artefacts, 2 + dif);
					SetHeroLootable("Gles", nil);
					startThread(teach_spell, "Gles", heroic_spells, 2);
					GiveHeroSkill("Gles", 2);
					GiveHeroSkill("Gles", 6);
					GiveHeroSkill("Gles", 7);
					GiveHeroSkill("Gles", 10);
					GiveHeroSkill("Gles", 34);
					GiveHeroSkill("Gles", 46);
					GiveHeroSkill("Gles", 63);
					GiveHeroSkill("Gles", 100);
				end;
				gles_first = 1;
			end;
			exp = GetHeroStat("Heam", STAT_EXPERIENCE) - GetHeroStat("Gles", 0);
			if exp > 10000 then
				ChangeHeroStat("Gles", STAT_EXPERIENCE , exp - 5000 + random(10) * 500);
			end;
			AddObjectCreatures("Gles", 29, (29 + dif)*week);
			AddObjectCreatures("Gles", 30, (22 + dif)*week);
			AddObjectCreatures("Gles", 32, (16 + dif)*week);
			AddObjectCreatures("Gles", 34, 10*week + dif);
			AddObjectCreatures("Gles", 37, 5*week + dif);
			EnableHeroAI("Gles", nil);
			startThread(MoveHeroPatch, "Gles", "Siris");
			if dif > 1 then
				AddObjectCreatures("Gles", 36, dif * week);
				AddObjectCreatures("Gles", 42, week);
				startThread(warmachine, "Gles", 2);
				if dif == 3 then
					SetHeroCombatScript("Gles", '/Maps/Scenario/C5M3/NecroCombat.xdb#xpointer(/Script)');
				end;
				if random(2) == 0 then
					sleep(1);
					if random(2) == 0 then
						RemoveObjectCreatures("Gles", 32, GetObjectCreatures("Gles", 32));
						else RemoveObjectCreatures("Gles", 34, GetObjectCreatures("Gles", 34));
					end;
					sleep(1);
					if random(3) == 0 then
						AddObjectCreatures("Gles", 40, dif - 1 + 2 * week);
						else if random(2) == 0 then
							AddObjectCreatures("Gles", 39, dif + 2 * week);
							else AddObjectCreatures("Gles", 89, dif + week);
						end;
					end;
				end;
				startThread(give_mana, "Gles");
			end;
		end;
	end;
end;

function MoveHeroPatch(hero, obj)
	print("move hero "..hero.." to "..obj);
	local x,y,z = GetObjectPosition(obj);
	local player = GetObjectOwner(hero);
	local heroes = GetPlayerHeroes(1);
	local victim = nil;
	local x1 = 0;
	local y1 = 0;
	while IsHeroAlive(hero) do
		sleep(2);
		while GetCurrentPlayer() ~= player do
			sleep(2);
		end;
		victim = nil;
		if IsHeroAlive(hero) then
			heroes = GetPlayerHeroes(1);
			for i, h in heroes do
				x1,y1 = GetObjectPosition(h);
				if x1 > 84 and y1 > 124 then
					if len(hero, h) < len(hero, obj) then
						if CalcHeroMoveCost(hero, x1, y1) < GetHeroStat(hero, 7) and CalcHeroMoveCost(hero, x1, y1) > 0 then
							print(hero.." want to destroy hero "..h);
							victim = h;
							break;
						end;
					end;
				end;
			end;
			if victim == nil then
				MoveHeroRealTime(hero, x, y, z);
				else MoveHeroRealTime(hero, x1, y1);
				while IsHeroAlive(hero) and IsObjectExists(victim) do
					sleep(2);
				end;
				if IsHeroAlive(hero) then
					MoveHeroRealTime(hero, x, y, z);
				end;
			end;
		end;
		while GetCurrentPlayer() == player do
			sleep(2);
			if IsHeroAlive(hero) then
				MoveHeroRealTime(hero, x, y, z);
				sleep(2);
			end;
		end;
		ChangeHeroStat(hero, 7, -10000);
	end;
end;

function len(x,y)
	local l = 1000;
	if IsObjectExists(x) and IsObjectExists(y) then
		local x1,y1,z1 = GetObjectPosition(x);
		local x2,y2,z2 = GetObjectPosition(y);
		if z1 == z2 then
			l = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
		end;
	end;
	return l;
end;

function give_mana(hero)
	local player = GetObjectOwner(hero);
	while GetCurrentPlayer() ~= player do
		sleep(2);
	end;
	if IsHeroAlive(hero) then
		ChangeHeroStat(hero, 8, 10 * GetHeroStat(hero, 4) - GetHeroStat(hero, 8));
	end;
end;

function NewHero(namename)
	if namename == "Nadaur" or namename == "Diraya" then
		b = b + 1;
	end;
	if b == 2 then
		if ( obj03 == 0 ) then
			StartDialogScene("/DialogScenes/C5/M3/R4/DialogScene.xdb#xpointer(/DialogScene)");
			SetObjectiveState( "twoheros", OBJECTIVE_COMPLETED );
			obj03 = 1;
			sleep (5);
			LevelUpHero("Heam");
		end;
	end;
end;

function Garnison()
	local NameH = "";
	while 1 do
		sleep ( 20 );
		NameH = GetTownHero("Siris");
		print(NameH);
		if NameH == nil then
			townd = GetObjectCreatures("Siris", CREATURE_DRUID_ELDER);
			townu = GetObjectCreatures("Siris", CREATURE_WAR_UNICORN);
			towndr = GetObjectCreatures("Siris", CREATURE_GREEN_DRAGON) + GetObjectCreatures("Siris", CREATURE_GOLD_DRAGON);
		else
			townd = GetObjectCreatures("Siris", CREATURE_DRUID_ELDER) + GetHeroCreatures(NameH, CREATURE_DRUID_ELDER);
			townu = GetObjectCreatures("Siris", CREATURE_WAR_UNICORN) + GetHeroCreatures(NameH, CREATURE_WAR_UNICORN);
			towndr = GetObjectCreatures("Siris", CREATURE_GREEN_DRAGON) + GetObjectCreatures("Siris", CREATURE_GOLD_DRAGON) + GetHeroCreatures(NameH, CREATURE_GREEN_DRAGON) + GetHeroCreatures(NameH, CREATURE_GOLD_DRAGON);
		end;
		if townd >= 50 and townu >= 30 and towndr >= 10 then
			if obj04 == 0 then
				StartDialogScene("/DialogScenes/C5/M3/R3/DialogScene.xdb#xpointer(/DialogScene)");
				SetObjectiveState("harrison", OBJECTIVE_COMPLETED);
				sleep (5);
				LevelUpHero("Heam");
				obj04 = 1;
			end;
			if obj04 == 2 then
				SetObjectiveState("harrison", OBJECTIVE_COMPLETED);
				obj04 = 1;
			end;
		else
			if obj04 == 1 then
				SetObjectiveState( "harrison", OBJECTIVE_ACTIVE );
				obj04 = 2;
			end;
		end;
		if obj03 == 1 and obj04 == 1 then
			StartDialogScene("/DialogScenes/C5/M3/R5/DialogScene.xdb#xpointer(/DialogScene)");
			if HasArtefact("Heam", 24) then
				SetGameVar("C5M2_Heam_Boots", 1);
				else SetGameVar("C5M2_Heam_Boots", 0);
			end;
			sleep(5);
			Win();
			break;
		end;
	end;
end;	

function NewDay()
	if GetDate(DAY) == 7 and obj01 == 0 then
		Loose();
	end;
	print ("next" , RND + DayX);
	if GetDate(DAY) == RND + DayX then
		startThread(attack, atk_type);
		atk_type = 1 - atk_type;
		RND = RND + random(8) + 8 - dif;
	end;
end;

function LostHero( HeroName )
	if ( HeroName == "Heam" ) or ( HeroName == "Nadaur" ) or ( HeroName == "Diraya" ) then
		Loose();
	end;
end;

function LostHero2( HeroName2 )
	if ( HeroName2 == "Nemor" ) then
		local day = GetDate(DAY);
		while GetDate(DAY) == day do
			sleep(2);
		end;
		startThread(attack, 1);
		StartDialogScene("/DialogScenes/C5/M3/R2/DialogScene.xdb#xpointer(/DialogScene)");
		sleep (1);
		MessageBox ("/Maps/Scenario/C5M3/C5M3_2.txt");
		sleep ( 1 );
		SetObjectiveState("harrison", OBJECTIVE_ACTIVE );
		sleep ( 2 );
		SetObjectiveState("twoheros", OBJECTIVE_ACTIVE );
		startThread(Garnison);
		Trigger( PLAYER_REMOVE_HERO_TRIGGER, PLAYER_3, nil );
	end;
end;

--------------------
SetObjectiveState("HeamSurvive", OBJECTIVE_ACTIVE );
Trigger( PLAYER_REMOVE_HERO_TRIGGER, PLAYER_1, "LostHero" );
Trigger( PLAYER_REMOVE_HERO_TRIGGER, PLAYER_3, "LostHero2" );
Trigger( PLAYER_ADD_HERO_TRIGGER, PLAYER_1,"NewHero" );
Trigger( OBJECT_CAPTURE_TRIGGER, "Siris", "SirisCapture" );
Trigger( NEW_DAY_TRIGGER, "NewDay" );
MoveHeroRealTime("Effig", 80 , 125);
EnableHeroAI("Effig", nil);
SetPlayerResource(PLAYER_2, GOLD, 30000);

startThread(map_up);