StartDialogScene("/DialogScenes/C3/M3/R1/DialogScene.xdb#xpointer(/DialogScene)");
C3M3_C1 = 0;
C3M3_C2 = 0;
C3M3_C3 = 0;
C3M3_C4 = 0;
C3M3_C5 = 0;
C3M3_C6 = 0;
C3M3_C7 = 0;
C3M3_C8 = 0;
C3M3_C9 = 0;
D_day_timer = 0;
counter = 0;
StaffFlag = 0;
Town1Converted = 0;
Town2Converted = 0;
Town3Converted = 0;
ElvenCondition = 0;
CyrusName = "Tan";
ElvenHero = "Nadaur";

spells = {{1, 2, 3, 4, 11, 12, 15, 23, 24, 25, 26, 39, 237}, {1, 2, 3, 4, 5, 6, 11, 12, 15, 23, 24, 25, 26, 28, 35, 39, 42, 236, 237}};
wizards = {"GREMLIN", "GARGOYLE", "GOLEM", "MAGI", "RAKSHASA", "GENIE", "GIANT", "TITAN"};
transfer_arts = {4, 6, 33};
path = "C3M3_";

plag_eff = "/Effects/_(Effect)/Spells/Plague.xdb#xpointer(/Effect)";
plag_sound = "/Sounds/_(Sound)/Spells/Plague.xdb#xpointer(/Sound)";
curse_eff = "/Effects/_(Effect)/Spells/Weakness.xdb#xpointer(/Effect)";
curse_sound = "/Sounds/_(Sound)/Spells/Weakness.xdb#xpointer(/Sound)";

SetRegionBlocked("antislonik",1,PLAYER_2);
SetRegionBlocked("antislonik",1,PLAYER_3);
SetRegionBlocked("gate",1,PLAYER_2);
SetRegionBlocked("gate",1,PLAYER_3);
SetRegionBlocked("closed",1,PLAYER_2);
SetRegionBlocked("closed",1,PLAYER_3);
SetRegionBlocked("closed2",1,PLAYER_2);
SetRegionBlocked("closed2",1,PLAYER_3);
SetRegionBlocked("cloesd3",1,PLAYER_2);
SetRegionBlocked("cloesd3",1,PLAYER_3);
SetRegionBlocked("landing",1,PLAYER_2);
SetRegionBlocked("landing",1,PLAYER_3);
SetRegionBlocked("ambush1",1,PLAYER_2);
SetRegionBlocked("ambush1",1,PLAYER_3);
SetRegionBlocked("teleport",1,PLAYER_2);
SetRegionBlocked("teleport",1,PLAYER_3);
SetRegionBlocked("shipyard1",1,PLAYER_2);
SetRegionBlocked("shipyard2",1,PLAYER_2);


SetObjectEnabled("El_Safir_teleport",nil);
SetObjectEnabled(CyrusName,nil);

EnableHeroAI("Razzak",nil);
EnableHeroAI("Maahir",nil);

function DifficultyDependency()
	if GetDifficulty() == DIFFICULTY_EASY then
		factor = 0.5;
		SetGameVar("C3M3_HEROIC", 0);
		SetPlayerStartResource(PLAYER_1,ORE,35);
		SetPlayerStartResource(PLAYER_1,WOOD,35);
		SetPlayerStartResource(PLAYER_1,SULFUR,15);
		SetPlayerStartResource(PLAYER_1,CRYSTAL,15);
		SetPlayerStartResource(PLAYER_1,MERCURY,15);
		SetPlayerStartResource(PLAYER_1,GEM,15);
		SetPlayerStartResource(PLAYER_1,GOLD,35000);
		SetTownBuildingLimitLevel("Town1",TOWN_BUILDING_DWELLING_4,0);
		SetTownBuildingLimitLevel("Town1",TOWN_BUILDING_DWELLING_6,0);
		SetTownBuildingLimitLevel("Town1",TOWN_BUILDING_DWELLING_5,0);
		SetTownBuildingLimitLevel("Town1",TOWN_BUILDING_DWELLING_7,0);
		SetTownBuildingLimitLevel("Town1",TOWN_BUILDING_MAGIC_GUILD,2);
		SetTownBuildingLimitLevel("Town1",TOWN_BUILDING_FORT,1);
		SetTownBuildingLimitLevel("Town2",TOWN_BUILDING_DWELLING_4,0);
		SetTownBuildingLimitLevel("Town2",TOWN_BUILDING_DWELLING_6,0);
		SetTownBuildingLimitLevel("Town2",TOWN_BUILDING_DWELLING_5,0);
		SetTownBuildingLimitLevel("Town2",TOWN_BUILDING_DWELLING_7,0);
		SetTownBuildingLimitLevel("Town2",TOWN_BUILDING_MAGIC_GUILD,2);
		SetTownBuildingLimitLevel("Town2",TOWN_BUILDING_FORT,1);
		SetTownBuildingLimitLevel("Town3",TOWN_BUILDING_DWELLING_6,0);
		SetTownBuildingLimitLevel("Town3",TOWN_BUILDING_DWELLING_5,0);
		SetTownBuildingLimitLevel("Town3",TOWN_BUILDING_DWELLING_7,0);
		SetTownBuildingLimitLevel("Town3",TOWN_BUILDING_MAGIC_GUILD,2);
		SetTownBuildingLimitLevel("Town3",TOWN_BUILDING_FORT,1);
		CreateMonster("skeleton_archer",CREATURE_SKELETON_ARCHER,120,117,16,0); --mausoleum
		CreateMonster("lich",CREATURE_LICH,16,80,20,0); --Crystall mine
		CreateMonster("vampire",CREATURE_VAMPIRE,22,138,10,0); --lighthouse
		CreateMonster("vampire_lord",CREATURE_VAMPIRE_LORD,30,95,71,0); --shrine
		CreateMonster("wight",CREATURE_WIGHT,12,129,89,0); --Town1
		CreateMonster("demilich",CREATURE_DEMILICH,20,91,114,0); --Redwood observatory
		CreateMonster("skeleton_archer2",CREATURE_SKELETON_ARCHER,250,30,53,0); --teleport
		CreateMonster("vampire_lord2",CREATURE_VAMPIRE_LORD,30,99,144,0);--center
		AddHeroCreatures("Berein",CREATURE_SKELETON_ARCHER,80);
		AddHeroCreatures("Berein",CREATURE_LICH,10);
		AddHeroCreatures("Berein",CREATURE_MANES,35);
		AddHeroCreatures("Berein",89,3);
		AddHeroCreatures("Isabell",CREATURE_FOOTMAN,15);
		AddHeroCreatures("Isabell",CREATURE_ARCHER,35);
		print("Difficulty level is easy. Factor = ", factor);
	else
		if GetDifficulty() == DIFFICULTY_NORMAL then
			factor = 1;
			SetGameVar("C3M3_HEROIC", 0);
			SetPlayerStartResource(PLAYER_1,ORE,15);
			SetPlayerStartResource(PLAYER_1,WOOD,15);
			SetPlayerStartResource(PLAYER_1,SULFUR,6);
			SetPlayerStartResource(PLAYER_1,CRYSTAL,6);
			SetPlayerStartResource(PLAYER_1,MERCURY,6);
			SetPlayerStartResource(PLAYER_1,GEM,6);
			SetPlayerStartResource(PLAYER_1,GOLD,15000);
			SetTownBuildingLimitLevel("Town1",TOWN_BUILDING_DWELLING_6,0);
			SetTownBuildingLimitLevel("Town1",TOWN_BUILDING_DWELLING_7,0);
			SetTownBuildingLimitLevel("Town1",TOWN_BUILDING_MAGIC_GUILD,3);
			SetTownBuildingLimitLevel("Town1",TOWN_BUILDING_FORT,2);
			SetTownBuildingLimitLevel("Town2",TOWN_BUILDING_DWELLING_6,0);
			SetTownBuildingLimitLevel("Town2",TOWN_BUILDING_DWELLING_7,0);
			SetTownBuildingLimitLevel("Town2",TOWN_BUILDING_MAGIC_GUILD,3);
			SetTownBuildingLimitLevel("Town2",TOWN_BUILDING_FORT,2);
			SetTownBuildingLimitLevel("Town3",TOWN_BUILDING_DWELLING_6,0);
			SetTownBuildingLimitLevel("Town3",TOWN_BUILDING_DWELLING_7,0);
			SetTownBuildingLimitLevel("Town3",TOWN_BUILDING_MAGIC_GUILD,3);
			SetTownBuildingLimitLevel("Town3",TOWN_BUILDING_FORT,2);
			AddHeroCreatures("Berein",CREATURE_SKELETON_ARCHER,40);
			AddHeroCreatures("Isabell",CREATURE_FOOTMAN,10);
			CreateMonster("skeleton_archer",CREATURE_SKELETON_ARCHER,120,117,16,0); --mausoleum
			CreateMonster("lich",CREATURE_LICH,16,80,20,0); --Crystall mine
			CreateMonster("vampire",CREATURE_VAMPIRE,22,138,10,0); --lighthouse
			CreateMonster("vampire_lord",CREATURE_VAMPIRE_LORD,30,95,71,0); --shrine
			CreateMonster("wight",CREATURE_WIGHT,12,129,89,0); --Town1
			CreateMonster("demilich",CREATURE_DEMILICH,20,91,114,0); --Redwood observatory
			CreateMonster("skeleton_archer2",CREATURE_SKELETON_ARCHER,250,30,53,0); --teleport
			CreateMonster("vampire_lord2",CREATURE_VAMPIRE_LORD,30,99,144,0);--center
			AddHeroCreatures("Berein",CREATURE_SKELETON_ARCHER,40);
			AddHeroCreatures("Berein",CREATURE_LICH,5);
			AddHeroCreatures("Berein",CREATURE_MANES,15);
			AddHeroCreatures("Isabell",CREATURE_FOOTMAN,10);
			AddHeroCreatures("Isabell",CREATURE_ARCHER,15);
			print("Difficulty level is normal. Factor = ", factor);
		else
		if GetDifficulty() == DIFFICULTY_HARD then
				SetGameVar("C3M3_HEROIC", 0);
				SetPlayerStartResource(PLAYER_1,ORE,10);
				SetPlayerStartResource(PLAYER_1,WOOD,10);
				SetPlayerStartResource(PLAYER_1,SULFUR,2);
				SetPlayerStartResource(PLAYER_1,CRYSTAL,2);
				SetPlayerStartResource(PLAYER_1,MERCURY,2);
				SetPlayerStartResource(PLAYER_1,GEM,2);
				SetPlayerStartResource(PLAYER_1,GOLD,8000);
				factor = 2;
				CreateMonster("skeleton_archer",CREATURE_SKELETON_ARCHER,120,117,16,0); --mausoleum
				CreateMonster("vampire",CREATURE_VAMPIRE,22,138,10,0); --lighthouse
				CreateMonster("demilich",CREATURE_DEMILICH,20,91,114,0); --Redwood observatory
				CreateMonster("vampire_lord",CREATURE_VAMPIRE_LORD,30,95,71,0); --shrine
				TeachHeroSpell("Maahir",SPELL_PHANTOM);
				TeachHeroSpell("Maahir",SPELL_RESURRECT);
				TeachHeroSpell("Sufi",SPELL_CONJURE_PHOENIX);
				DenyAIHeroesFlee(2, 1);
				DenyAIHeroesFlee(3, 1);
				print("Difficulty level is hard. Factor = ", factor);
			else
				if GetDifficulty() == DIFFICULTY_HEROIC then
					factor = 3;
					SetPlayerStartResource(PLAYER_1,ORE,10);
					SetPlayerStartResource(PLAYER_1,WOOD,10);
					SetPlayerStartResource(PLAYER_1,SULFUR,2);
					SetPlayerStartResource(PLAYER_1,CRYSTAL,2);
					SetPlayerStartResource(PLAYER_1,MERCURY,2);
					SetPlayerStartResource(PLAYER_1,GEM,2);
					SetPlayerStartResource(PLAYER_1,GOLD,8000);
					TeachHeroSpell("Razzak",SPELL_PHANTOM );
					TeachHeroSpell("Razzak",SPELL_RESURRECT);
					TeachHeroSpell("Maahir",SPELL_PHANTOM);
					TeachHeroSpell("Maahir",SPELL_RESURRECT);
					TeachHeroSpell("Sufi",SPELL_PHANTOM );
					TeachHeroSpell("Sufi",SPELL_CONJURE_PHOENIX);
					TeachHeroSpell("Havez",SPELL_PHANTOM );
					DenyAIHeroesFlee(2, 1);
					DenyAIHeroesFlee(3, 1);
					SetGameVar("C3M3_HEROIC", 1);
					SetHeroCombatScript( 'Maahir', '/Maps/Scenario/C3M3/CombatVSNadaur.xdb#xpointer(/Script)' );
					SetHeroCombatScript( 'Razzak', '/Maps/Scenario/C3M3/CombatVSNadaur.xdb#xpointer(/Script)' );
					print("Difficulty level is heroic. Factor = ", factor);
				end;
			end;
		end;
	end;
end;

function EnableAIForRazzakAndTimerkhan()
	sleep(5);
	while GetDate(DAY) ~= 36 and GetObjectOwner("Town1") == 2 and GetObjectOwner("Town2") == 2 and GetObjectOwner("Town3") == 2 do
		sleep(15);
		if GetObjectOwner("last_garrison") == 1 then
			break;
		end;
	end;
	SetObjectEnabled("El_Safir_teleport",1);
	print("Teleport near El Safir has been enabled...");
	if IsHeroAlive("Razzak") ~= nil then
		EnableHeroAI("Razzak",1);
		AddHeroCreatures("Razzak",CREATURE_MASTER_GREMLIN,factor*150);
		AddHeroCreatures("Razzak",CREATURE_MASTER_GENIE,factor*20);
		AddHeroCreatures("Razzak",CREATURE_MAGI,factor*30);
		AddHeroCreatures("Razzak",CREATURE_TITAN,factor*4);
		AddHeroCreatures("Razzak",CREATURE_STONE_GARGOYLE,factor*150);
		AddHeroCreatures("Razzak",CREATURE_STEEL_GOLEM,factor*100);
		print("AI has been enabled for hero Razzak.");
		if factor == 3 then
			GiveArtefact("Razzak", 5, 1);
			GiveArtefact("Razzak", 58, 1);
			GiveArtefact("Razzak", 21, 1);
			GiveArtefact("Razzak", 66, 1);
			GiveArtefact("Razzak", 39, 1);
			GiveArtefact("Razzak", 24, 1);
			GiveArtefact("Razzak", 14, 1);
			GiveArtefact("Razzak", 17, 1);
			GiveArtefact("Razzak", 8, 1);
		end;
		ChangeHeroStat("Razzak", 0, 80000+10000*factor);
		if GetObjectOwner("Town1") == PLAYER_1 then
			SetAIHeroAttractor("Town1","Razzak",2);
			startThread(town_unatt, "Razzak", "Town1");
		end;
		else
			print("hero Razzak is dead");
	end;
	while GetDate(DAY) ~= 57 do
		sleep(15);
		if GetObjectOwner("last_garrison") == 1 then
			break;
		end;
		if Town1Converted==1 and Town2Converted==1 and Town3Converted==1 then
			break;
		end;
	end;
	if IsHeroAlive("Maahir") ~= nil then
		EnableHeroAI("Maahir",1);
		AddHeroCreatures("Maahir",CREATURE_MASTER_GREMLIN,factor*250);
		AddHeroCreatures("Maahir",CREATURE_GENIE,factor*30);
		AddHeroCreatures("Maahir",CREATURE_ARCH_MAGI,factor*60);
		AddHeroCreatures("Maahir",CREATURE_TITAN,factor*8);
		AddHeroCreatures("Maahir",CREATURE_OBSIDIAN_GARGOYLE,factor*200);
		AddHeroCreatures("Maahir",CREATURE_STEEL_GOLEM,factor*150);
		AddHeroCreatures("Maahir",CREATURE_RAKSHASA,factor*25);
		ChangeHeroStat("Maahir", 0, 141000+20000*factor);
		if factor == 3 then
			GiveArtefact("Maahir", 45, 1);
			GiveArtefact("Maahir", 51, 1);
			GiveArtefact("Maahir", 42, 1);
			GiveArtefact("Maahir", 59, 1);
			GiveArtefact("Maahir", 39, 1);
			GiveArtefact("Maahir", 49, 1);
			GiveArtefact("Maahir", 44, 1);
			GiveArtefact("Maahir", 19, 1);
			GiveArtefact("Maahir", 25, 1);
		end;
		print("AI has been enabled for hero Maahir.");
		if GetObjectOwner("Town2") == PLAYER_1 then
			SetAIHeroAttractor ("Town2","Maahir",2);
			startThread(town_unatt, "Maahir", "Town2");
		end;
		else
			print("hero Maahir is dead");
	end;
end;

function town_unatt(hero, town)
	while GetObjectOwner(town) ~= 2 do
		sleep(5);
	end;
	if IsHeroAlive(hero) then
		SetAIHeroAttractor(town,hero,0);
	end;
end;

function UpEnemy()
	sleep(2);
	for i = 1,4 do
		ChangeHeroStat(CyrusName, i, 5);
	end;
	ChangeHeroStat(CyrusName, 0, 60000);
	if GetGameVar("C3M2_Bow") == "1" then
		GiveArtefact("Berein", 4);
	end;
	if factor > 1 then
		local heroes = GetPlayerHeroes(2);
		for i, hero in heroes do
			if IsHeroAlive(hero) then
				for j, sp in spells[factor-1] do
					if KnowHeroSpell(hero, sp) == nil then
						TeachHeroSpell(hero, sp);
					end;
				end;
			end;
		end;
		for i=1,3 do
			AddObjectCreatures("Town"..i, 57, (factor - 1) * 20);
			AddObjectCreatures("Town"..i, 59, (factor - 1) * 10);
			AddObjectCreatures("Town"..i, 61, (factor - 1) * 6);
			if random(3) == 0 then
				AddObjectCreatures("Town"..i, 69, factor - 1);
				else if random(2) == 0 then
					AddObjectCreatures("Town"..i, 63, factor * 2);
					else AddObjectCreatures("Town"..i, 65, factor);
				end;
			end;
		end;
		add_army("last_garrison", (factor - 1) * 0.50);
		add_army(ElvenHero, (factor - 1) * 0.25);
		AddObjectCreatures(ElvenHero, 53, 2 * factor - 1);
		for i = 1,4 do
			ChangeHeroStat("Sufi", i, factor-1);
			ChangeHeroStat("Havez", i, factor-1);
			ChangeHeroStat("Razzak", i, factor);
			ChangeHeroStat("Maahir", i, factor+1);
			ChangeHeroStat(ElvenHero, i, factor);
		end;
		if factor == 3 then
			GiveHeroSkill("Razzak", 11);
			GiveHeroSkill("Maahir", 11);
			GiveHeroSkill("Maahir", 10);
			GiveHeroSkill(ElvenHero, 11);
			ChangeHeroStat("Sufi", 0, 30000);
			ChangeHeroStat("Havez", 0, 50000);
			ChangeHeroStat(ElvenHero, 0, 30000);
			print("give some stuff");
			GiveArtefact(ElvenHero, 10);
			GiveArtefact(ElvenHero, 19);
			GiveArtefact(ElvenHero, 4);
			GiveArtefact(ElvenHero, 21);
			GiveArtefact(ElvenHero, 23);
			GiveArtefact(ElvenHero, 27);
			GiveArtefact(ElvenHero, 55);
			GiveArtefact(ElvenHero, 31);
			GiveHeroWarMachine("Razzak", 1);
			GiveHeroWarMachine("Razzak", 4);
			GiveHeroWarMachine("Maahir", 1);
			GiveHeroWarMachine("Maahir", 4);
			GiveHeroWarMachine(ElvenHero, 3);
			GiveHeroWarMachine(ElvenHero, 4);
			SetHeroLootable("Razzak", nil);
			SetHeroLootable(ElvenHero, nil);
			SetHeroLootable("Maahir", nil);
			for i = 1,4 do
				ChangeHeroStat("Razzak", i, 1);
				ChangeHeroStat("Maahir", i, 2);
				ChangeHeroStat(ElvenHero, i, 1);
			end;
		end;
	end;
	ChangeHeroStat("Sufi", 0, 50000);
	ChangeHeroStat("Havez", 0, 70000);
	ChangeHeroStat(ElvenHero, 0, 82000);
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 setupNeutrals()
	sleep(5);
	if factor > 1 then
		startThread(add_neutrals, (factor - 1) * 0.50);
	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 add_neutrals(coef)
	local creatures = {};
	for i, name in wizards do
		creatures = GetObjectNamesByType(name);
		if creatures ~= {} then
			print("add some "..name);
			for j, mon in creatures do
				startThread(add_army, mon, coef);
			end;
		end;
	end;
end;

function open_yard(region)
	local open_day = 17 - 3 * factor + random(5);
	local coef = 0;
	while GetDate(DAY) < open_day do
		sleep(5);
		coef = Town1Converted + Town2Converted + Town3Converted;
		if coef > 0 then
			break;
		end;
	end;
	SetRegionBlocked(region,nil,2);
	print("open region "..region.." for AI");
end;

function len( x, y )
local l = sqrt( x * x + y * y );
	return l;
end;


function ObjectiveGetArtefactComplete(ArtefactName,ObjectiveName,Scene)
	while 1 do
		sleep(5);
		if HasArtefact( "Berein", ArtefactName) == true then
			print("Objective ",ObjectiveName," for artefact ",ArtefactName," completed");
			startThread(SetArtefactUntrans,ArtefactName);
			SetObjectiveState(ObjectiveName,OBJECTIVE_COMPLETED);
			StartDialogScene(Scene);
			ObjectiveExp("Berein");
			return
		end;
	end;
end;


function PObjective4()
	while D_day_timer == 0 do
		sleep(5);
	end;
	sleep(30);
	if GetObjectiveState("prim4") == OBJECTIVE_UNKNOWN then
		SetObjectiveState("prim4",OBJECTIVE_ACTIVE);--при установке состояния задания в OBJECTIVE_ACTIVE оно автоматически становится видимым игроку
	else
		print("Warning!!! prim4 is not UNKNOWN");
	end;
	while 1 do
		sleep();
	end;
end;

function PObjective5()
	sleep(30);
while 1 do
	if IsHeroAlive("Berein")==nil or IsHeroAlive("Isabell")==nil then
		SetObjectiveState("prim5",OBJECTIVE_FAILED);
		sleep(6);
		print("Hero is dead!!!");
		Loose(PLAYER_1);
		return 1;
	end;
	if GetObjectiveState("prim2") == OBJECTIVE_COMPLETED and GetObjectiveState("prim1") == OBJECTIVE_COMPLETED
		and GetObjectiveState("prim4") == OBJECTIVE_COMPLETED then
		if GetObjectiveState("prim5") == OBJECTIVE_ACTIVE  then
			SetObjectiveState("prim5",OBJECTIVE_COMPLETED);
		else
			print("Warning!!! prim5 is not ACTIVE");
		end;
		return 1;
	end;
sleep(5);
end;
end;

function SObjective1()
	while 1 do
		sleep(10);
		if HasArtefact("Berein",ARTIFACT_STAFF_OF_VEXINGS) == true then
			SetObjectiveState("sec1",OBJECTIVE_ACTIVE);
			startThread(CompleteSObj1);
			break;
		end;
	end;
end;

function CompleteSObj1()
	while 1 do
		sleep(10);
		if Town1Converted==1 and Town2Converted==1 and Town3Converted==1 then
			print("All towns are converted");
			SetObjectiveState("sec1",OBJECTIVE_COMPLETED);
			ObjectiveExp("Berein");
			break;
		end;
	end;
end;

function messages( dialog )
	if dialog == 1 then
		print("scene 1 has been started...");
		C3M3_C1 = 1;
		StartDialogScene("/DialogScenes/C3/M3/R1/DialogScene.xdb#xpointer(/DialogScene)");
	end;
	if dialog == 2 then
		C3M3_C2 = 1;
		StartDialogScene("/DialogScenes/C3/M3/R2/DialogScene.xdb#xpointer(/DialogScene)");
	end;
	if dialog == 4 then
		C3M3_C4 = 1;
		StartDialogScene("/DialogScenes/C3/M3/R3/DialogScene.xdb#xpointer(/DialogScene)");
	end;
	if dialog == 5 then
		C3M3_C5 = 1;
		StartDialogScene("/DialogScenes/C3/M3/R4/DialogScene.xdb#xpointer(/DialogScene)");
	end;
	if dialog == 6 then
		C3M3_C6 = 1;
		StartDialogScene("/DialogScenes/C3/M3/R5/DialogScene.xdb#xpointer(/DialogScene)");
	end;
	if dialog == 7 then
		C3M3_C7 = 1;
		StartDialogScene("/DialogScenes/C3/M3/R6/DialogScene.xdb#xpointer(/DialogScene)");
	end;
	if dialog == 8 then
		C3M3_C8 = 1;
		StartDialogScene("/DialogScenes/C3/M3/R7/DialogScene.xdb#xpointer(/DialogScene)");
	end;
	if dialog == 9 then
		C3M3_C9 = 1;
		StartDialogScene("/DialogScenes/C3/M3/R8/DialogScene.xdb#xpointer(/DialogScene)");
	end;
	if dialog == 10 then
		C3M3_C10 = 1;
		StartDialogScene("/DialogScenes/C3/M3/R9/DialogScene.xdb#xpointer(/DialogScene)");
	end;
end;
-----------------------------------------------

---Astral retreat---
function cyrus_retreat(heroname)
	if GetObjectOwner(heroname) == PLAYER_1 then
		Trigger(REGION_ENTER_AND_STOP_TRIGGER, "astral_can_be_in_sight", nil);
		SetRegionBlocked("teleport",nil,PLAYER_2);
		SetRegionBlocked("teleport",nil,PLAYER_3);
		print("Thread cyrus_retreat");
		BlockGame();  -------------------!
		OpenRegionFog(PLAYER_1,"CyrusRegion");
		OpenRegionFog(PLAYER_1,"teleport");
		ChangeHeroStat(CyrusName, STAT_MOVE_POINTS, 4000);
		sleep(10);
		MoveHeroRealTime(CyrusName,145,152);
	else
		print("Academy hero ", heroname," has entered in region near Cyrus");
		Trigger( REGION_ENTER_AND_STOP_TRIGGER, "astral_can_be_in_sight","cyrus_retreat", nil );
	end;
end

---assemble 1000 skeletons---
function SObjective2()
local staff_day;
while 1 do
	if GetObjectiveState('prim1')==OBJECTIVE_COMPLETED then
		staff_day=GetDate( ABSOLUTE_DAY ) + 1 ---2 day delay---
		while staff_day >= GetDate( ABSOLUTE_DAY ) do
			sleep(10);
		end;
		print("Assemble 1000 skeletons!");
		SetObjectiveState("sec2",OBJECTIVE_ACTIVE);
		messages( 4 );
		break;
	end;
	sleep(2);
end;
sleep(10);
print("Skeleton quantity checking has been started...");
while 1 do
	skeletons = GetHeroCreatures('Berein', CREATURE_SKELETON) ---чек на количество скелетов у берейна---
	skeleton_archers = GetHeroCreatures('Berein', CREATURE_SKELETON_ARCHER) ---чек на количество скелетов лучников у берейна---
	if skeletons + skeleton_archers  >= 1000 then
		print("Berein assembles ", skeletons, " skeletons");
		print("and ", skeleton_archers , " skeleton_archers.");
		SetObjectiveState("sec2",OBJECTIVE_COMPLETED);
		messages( 5 );
		sleep(10);
		ObjectiveExp("Berein");
		break;
	end;
	sleep(10);
end;
	print('assemble 1000 skeletons - OK');
end;

---assemble 20 bone dragons---
function SObjective3()	
while 1 do
	local BoneDragons = GetHeroCreatures('Berein', CREATURE_BONE_DRAGON); ---количество костяных драконов у берейна---
	local ShadowDragons = GetHeroCreatures('Berein', CREATURE_SHADOW_DRAGON);
	if BoneDragons + ShadowDragons >= 20 then
		if GetObjectiveState('sec3') == OBJECTIVE_ACTIVE  then
			SetObjectiveState('sec3',OBJECTIVE_COMPLETED);
			D_day_timer = 1;
			counter = counter + 1;
			if counter == 1 then
				ObjectiveExp("Berein");
				print("counter = ",counter);
			end;
			sleep(3);
			Trigger(OBJECT_TOUCH_TRIGGER, "cyrus_teleport", "port_check", nil);
		else
			print("Warning!!! sec3 is not ACTIVE");
		end;
		startThread(IfDragonsLost);
		break;
	end;
	sleep(5);
end;
end;

function IfDragonsLost()
	repeat
		BoneDragons = GetObjectCreatures('Berein', CREATURE_BONE_DRAGON);
		ShadowDragons = GetObjectCreatures('Berein', CREATURE_SHADOW_DRAGON);
		sleep(10);
	until BoneDragons + ShadowDragons <= 19;
	SetObjectiveState('sec3',OBJECTIVE_ACTIVE);
	startThread(SObjective3);
end;


-- Secondary objective. It starts when Markal ("Berein" in script) take the artifact "Staff of vexings" to another hero.

function ReturnArtefactToBerein(ArtefactName,ObjectiveName)
	print("Thread ReturnArtefactToBerein for artefact ",ArtefactName," has been started...");
	while 1 do
		PlayerHeroes = GetPlayerHeroes(PLAYER_1);
		for i=0,length(PlayerHeroes) - 1 do
			--print("i = ",i,"Hero is ", PlayerHeroes[i],". Staff");
			if HasArtefact(PlayerHeroes[i],ArtefactName) == true then
				OldArtefactOwner = PlayerHeroes[i];
				break;
			end;
		end;
		sleep(20);
		if HasArtefact("Berein",ArtefactName) == true then
			ObjectiveExp(OldArtefactOwner);
			print("Old Artefact Owner is ",OldArtefactOwner);
			SetObjectiveState(ObjectiveName,OBJECTIVE_COMPLETED);
			startThread(StartTownTrigger,"Town1","CurseTown1");
			startThread(StartTownTrigger,"Town2","CurseTown2");
			startThread(StartTownTrigger,"Town3","CurseTown3");
			return
		end;
	end;
end;

function GiveStaffOfVexings(HeroName)
	if GetObjectOwner(HeroName) == PLAYER_1 then
		GiveArtefact(HeroName, ARTIFACT_STAFF_OF_VEXINGS );
		print("Our hero ",HeroName," has got Staff of vexings");
		if HeroName ~= "Berein" then
			ObjectiveExp(HeroName);
			SetObjectiveState("sec4",OBJECTIVE_ACTIVE);
			startThread(ReturnArtefactToBerein,ARTIFACT_STAFF_OF_VEXINGS,"sec4");
			else
			startThread(StartTownTrigger,"Town1","CurseTown1");
			startThread(StartTownTrigger,"Town2","CurseTown2");
			startThread(StartTownTrigger,"Town3","CurseTown3");
		end;
		Trigger(OBJECT_TOUCH_TRIGGER ,'Town1', nil);
	else
		print("Enemy hero touch staff");
		Trigger(OBJECT_TOUCH_TRIGGER ,'Town1', "GiveStaffOfVexings");
	end;
end;

function GiveCloackOfMourning(HeroName)
	if GetObjectOwner(HeroName) == PLAYER_1 then
		GiveArtefact( HeroName, ARTIFACT_CLOAK_OF_MOURNING );
		print("Our hero ",HeroName," has got Cloak of mourning");
		if HeroName ~= "Berein" then
			ObjectiveExp(HeroName);
			SetObjectiveState("Sec6",OBJECTIVE_ACTIVE);
			startThread(ReturnArtefactToBerein,ARTIFACT_CLOAK_OF_MOURNING,"Sec6");
		end;
		Trigger(OBJECT_TOUCH_TRIGGER ,'Town2', "CurseTown2");
	else
		print("Enemy hero touch cloak");
		Trigger(OBJECT_TOUCH_TRIGGER ,'Town2', "GiveCloackOfMourning");
	end;
end;

function StartTownTrigger(TownName,FunctionName,Cloak)
	while GetObjectOwner(TownName) ~= PLAYER_1 do
		sleep(10);
	end;
	Trigger(OBJECT_TOUCH_TRIGGER ,TownName, FunctionName);
	print("Trigger for ",TownName," with function ",FunctionName," has been started...");
end;


function CurseTown1(HeroName)
	print("CurseTown1 has been started");
	print("Hero name is ",HeroName);
	print("Town owner is ",GetObjectOwner("Town1"));
	print("Has Artefact = ",HasArtefact( "Berein", ARTIFACT_STAFF_OF_VEXINGS )," must be true.");
	print("IsAnyHeroInTown() = ",IsAnyHeroInTown("Town1"));
	if  HeroName == "Berein" and
	    HasArtefact( "Berein", ARTIFACT_STAFF_OF_VEXINGS )==true and
	    GetObjectOwner("Town1") == PLAYER_1 and
		IsAnyHeroInTown("Town1") == 0 then
		QuestionBox("/Maps/Scenario/C3M3/messages/CurseTown.txt", "TransformingTown1");
	end;
end;


function CurseTown2(HeroName)
	print("CurseTown2 has been started");
	print("Hero name is ",HeroName);
	print("Town owner is ",GetObjectOwner("Town2"));
	print("Has Artefact = ",HasArtefact( "Berein", ARTIFACT_STAFF_OF_VEXINGS )," must be true.");
	print("IsAnyHeroInTown() = ",IsAnyHeroInTown("Town2"));
	if  HeroName == "Berein" and
		HasArtefact( "Berein", ARTIFACT_STAFF_OF_VEXINGS )==true and
		GetObjectOwner("Town2") == PLAYER_1 and
		IsAnyHeroInTown("Town2") == 0 then
		QuestionBox("/Maps/Scenario/C3M3/messages/CurseTown.txt", "TransformingTown2");
	end;
end;

function CurseTown3(HeroName)
	print("CurseTown3 has been started");
	print("Hero name is ",HeroName);
	print("Town owner is ",GetObjectOwner("Town3"));
	print("Has Artefact = ",HasArtefact( "Berein", ARTIFACT_STAFF_OF_VEXINGS )," must be true.");
	print("IsAnyHeroInTown() = ",IsAnyHeroInTown("Town3"));
	if  HeroName == "Berein" and
		HasArtefact( "Berein", ARTIFACT_STAFF_OF_VEXINGS )==true and
		GetObjectOwner("Town3") == PLAYER_1 and
		IsAnyHeroInTown("Town3") == 0 then
		QuestionBox("/Maps/Scenario/C3M3/messages/CurseTown.txt", "TransformingTown3");
	end;
end;

function IsAnyHeroInTown(TownName)
	if GetTownHero(TownName) ~= nil then return 1 else return 0 end;
end;

function SObjective5()
	while GetObjectOwner('Town1') ~= PLAYER_1 and GetObjectOwner('Town2') ~= PLAYER_1 and IsObjectVisible( PLAYER_1, ElvenHero ) == nil do --town capture check
		sleep();
	end;	
	SetObjectiveState("sec5",OBJECTIVE_ACTIVE);--при установке состояния задания в OBJECTIVE_ACTIVE оно автоматически становится видимым игроку
	if C3M3_C7 == 0 then
		messages( 7 );
	end;
	EnableHeroAI(ElvenHero, 1);
	startThread(elves_help_completed);
end;
---elves help---
function elves_help_completed()
	while 1 do
		sleep(10);
		if IsHeroAlive(ElvenHero) == nil then
			print("Elven Hero is dead");
			if ElvenCondition == 0 then
				SetObjectiveState("sec5",OBJECTIVE_COMPLETED);
				StartDialogScene("/DialogScenes/C3/M3/R7/DialogScene.xdb#xpointer(/DialogScene)");
				sleep(5);
				--ObjectiveExp("Berein");
			else
				print("Game over");
			end;
			return
		end;
	end;
end;


---portal check---
function port_check(HeroName)
	if HeroName == "Berein" then
		if GetObjectiveState("sec3") == OBJECTIVE_COMPLETED then
			if      GetObjectiveState("prim1") == OBJECTIVE_COMPLETED
				and GetObjectiveState("prim2") == OBJECTIVE_COMPLETED then
				ElvenCondition = 1;
				SetObjectiveState("prim5",OBJECTIVE_COMPLETED);
				sleep(3);
				SetObjectiveState("prim4",OBJECTIVE_COMPLETED);
				sleep(3);
				StartDialogScene("/DialogScenes/C3/M3/R9/DialogScene.xdb#xpointer(/DialogScene)");
				startThread(save_artefact, "Berein", path, transfer_arts);
				sleep(5);
				Win(PLAYER_1);
			else
				MessageBox("/Maps/Scenario/C3M3/MessageBox_VampirsGarmentNeed.txt");
				Trigger(OBJECT_TOUCH_TRIGGER, "cyrus_teleport", "port_check", nil);
			end;
		else
			MessageBox("/Maps/Scenario/C3M3/MessageBox_MarkalMustHaveDragons.txt");
			Trigger(OBJECT_TOUCH_TRIGGER, "cyrus_teleport", "port_check", nil);
		end;
	else
		MessageBox("/Maps/Scenario/C3M3/MessageBox_OnlyBereinInPortal.txt");
		Trigger(OBJECT_TOUCH_TRIGGER, "cyrus_teleport", "port_check", nil);
	end;
end


function SetBuildingsEnabled(TownName)
	SetTownBuildingLimitLevel(TownName,TOWN_BUILDING_DWELLING_4,2);
	SetTownBuildingLimitLevel(TownName,TOWN_BUILDING_DWELLING_6,2);
	SetTownBuildingLimitLevel(TownName,TOWN_BUILDING_DWELLING_5,2);
	SetTownBuildingLimitLevel(TownName,TOWN_BUILDING_DWELLING_7,2);
	SetTownBuildingLimitLevel(TownName,TOWN_BUILDING_MAGIC_GUILD,5);
	SetTownBuildingLimitLevel(TownName,TOWN_BUILDING_FORT,3);
	print("High level buildings were enabled to construct for town ",TownName);
end;

function TransformingTown1()
	local currentRes = GetPlayerResource( PLAYER_1, GOLD );
	local x = 122.4;
	local y = 95.5;
	if IsAnyHeroInTown("Town1") == 0 then
		if currentRes < 10000 then
			MessageBox( "/Maps/Scenario/C3M3/messages/CurseNegative.txt" );
			else
			BlockGame();
			Trigger(OBJECT_TOUCH_TRIGGER, "Town1", nil);
			SetPlayerResource( PLAYER_1, GOLD, currentRes - 10000 );
			Play2DSound(plag_sound);
			Play2DSound(curse_sound);
			PlayVisualEffect(curse_eff, "", "", x-1.4, y+0.8, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x, y+1.3, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x-0.7, y-1, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x, y, 8, random(360));
			PlayVisualEffect(curse_eff, "", "", x, y-2.2, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x+0.7, y+1, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x+1.4, y-0.8, 4, random(360));
			sleep(2);
			Play2DSound(curse_sound);
			PlayVisualEffect(curse_eff, "", "", x, y, 8, random(360));
			PlayVisualEffect(plag_eff, "", "", x-1.2, y, 4, random(360));
			PlayVisualEffect(curse_eff, "", "", x-0.9, y-0.8, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x, y-2.2, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x+0.9, y, 4, random(360));
			PlayVisualEffect(curse_eff, "", "", x+1.3, y-1.3, 4, random(360));
			sleep(2);
			Play2DSound(curse_sound);
			PlayVisualEffect(curse_eff, "", "", x+1.4, y, 4, random(360));
			PlayVisualEffect(curse_eff, "", "", x-0.8, y+1.1, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x-1.2, y, 4, random(360));
			PlayVisualEffect(curse_eff, "", "", x+0.8, y-1.3, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x+1.3, y, 4, random(360));
			PlayVisualEffect(curse_eff, "", "", x+0.2, y+1.7, 4, random(360));
			sleep(13);
			TransformTown("Town1", TOWN_NECROMANCY);
			MessageBox( "/Maps/Scenario/C3M3/messages/Town1Transformed.txt" );
			Town1Converted = 1;
			UnblockGame();
			if (GetDifficulty() == DIFFICULTY_NORMAL or GetDifficulty() == DIFFICULTY_EASY) then SetBuildingsEnabled("Town1"); end;
		end;
	else
		print("Can not transform. Hero is in Town.");
		MessageBox( "/Maps/Scenario/C3M3/HeroInTown.txt" );
	end;
end;

function TransformingTown2()
	local currentRes = GetPlayerResource( PLAYER_1, GOLD );
	local x = 71.5;
	local y = 140.5;
	if IsAnyHeroInTown("Town2") == 0 then
		if currentRes < 10000 then
			MessageBox( "/Maps/Scenario/C3M3/messages/CurseNegative.txt" );
		else
			BlockGame();
			Trigger(OBJECT_TOUCH_TRIGGER, "Town2", nil);
			SetPlayerResource( PLAYER_1, GOLD, currentRes - 10000 );
			Play2DSound(plag_sound);
			Play2DSound(curse_sound);
			PlayVisualEffect(curse_eff, "", "", x-1.4, y+0.8, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x, y+1.3, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x-0.7, y-1, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x, y, 8, random(360));
			PlayVisualEffect(curse_eff, "", "", x, y-2.2, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x+0.7, y+1, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x+1.4, y-0.8, 4, random(360));
			sleep(2);
			Play2DSound(curse_sound);
			PlayVisualEffect(curse_eff, "", "", x, y, 8, random(360));
			PlayVisualEffect(plag_eff, "", "", x-1.2, y, 4, random(360));
			PlayVisualEffect(curse_eff, "", "", x-0.9, y-0.8, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x, y-2.2, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x+0.9, y, 4, random(360));
			PlayVisualEffect(curse_eff, "", "", x+1.3, y-1.3, 4, random(360));
			sleep(2);
			Play2DSound(curse_sound);
			PlayVisualEffect(curse_eff, "", "", x+1.4, y, 4, random(360));
			PlayVisualEffect(curse_eff, "", "", x-0.8, y+1.1, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x-1.2, y, 4, random(360));
			PlayVisualEffect(curse_eff, "", "", x+0.8, y-1.3, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x+1.3, y, 4, random(360));
			PlayVisualEffect(curse_eff, "", "", x+0.2, y+1.7, 4, random(360));
			sleep(13);
			TransformTown("Town2", TOWN_NECROMANCY);
			MessageBox( "/Maps/Scenario/C3M3/messages/Town2Transformed.txt" );
			UnblockGame();
			Town2Converted = 1;
			if (GetDifficulty() == DIFFICULTY_NORMAL or GetDifficulty() == DIFFICULTY_EASY) then SetBuildingsEnabled("Town2");end;
		end;
	else
		print("Can not transform. Hero is in Town.");
		MessageBox( "/Maps/Scenario/C3M3/HeroInTown.txt" );
	end;
end;

function TransformingTown3()
	local currentRes = GetPlayerResource( PLAYER_1, GOLD );
	local x = 45.5;
	local y = 97.5
	if IsAnyHeroInTown("Town3") == 0 then
		if currentRes < 10000 then
			MessageBox( "/Maps/Scenario/C3M3/messages/CurseNegative.txt" );
		else
			BlockGame();
			Trigger(OBJECT_TOUCH_TRIGGER, "Town3", nil);
			SetPlayerResource( PLAYER_1, GOLD, currentRes - 10000 );
			Play2DSound(plag_sound);
			Play2DSound(curse_sound);
			PlayVisualEffect(curse_eff, "", "", x-1.4, y+1.5, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x, y+1.3, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x-0.7, y-1, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x, y, 8, random(360));
			PlayVisualEffect(curse_eff, "", "", x+2.2, y, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x+0.7, y+1.3, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x+1.4, y-0.8, 4, random(360));
			sleep(2);
			Play2DSound(curse_sound);
			PlayVisualEffect(curse_eff, "", "", x, y, 8, random(360));
			PlayVisualEffect(plag_eff, "", "", x-1.2, y+0.6, 4, random(360));
			PlayVisualEffect(curse_eff, "", "", x-0.9, y+0.8, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x+2.2, y, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x+0.9, y+1.4, 4, random(360));
			PlayVisualEffect(curse_eff, "", "", x+1.3, y-1.3, 4, random(360));
			sleep(2);
			Play2DSound(curse_sound);
			PlayVisualEffect(curse_eff, "", "", x-1.7, y, 4, random(360));
			PlayVisualEffect(curse_eff, "", "", x-0.8, y+1.1, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x-1.2, y, 4, random(360));
			PlayVisualEffect(curse_eff, "", "", x+0.8, y-1.3, 4, random(360));
			PlayVisualEffect(plag_eff, "", "", x+1.3, y+0.5, 4, random(360));
			PlayVisualEffect(curse_eff, "", "", x+1.2, y+1.4, 4, random(360));
			sleep(13);
			TransformTown("Town3", TOWN_NECROMANCY);
			MessageBox( "/Maps/Scenario/C3M3/messages/Town3Transformed.txt" );
			UnblockGame();
			Town3Converted = 1;
			if (GetDifficulty() == DIFFICULTY_NORMAL or GetDifficulty() == DIFFICULTY_EASY) then SetBuildingsEnabled("Town3");end;
		end;
	else
		print("Can not transform. Hero is in Town.");
		MessageBox( "/Maps/Scenario/C3M3/HeroInTown.txt" );
	end;
end;

function ObjectiveExp(HeroName)
	local ToLevel = GetExpToLevel(GetHeroLevel(HeroName)+1);
	local delta = (ToLevel - GetHeroStat(HeroName, STAT_EXPERIENCE)) / 2;
	print("delta = ", delta);
	if delta >= 0 then
		ChangeHeroStat(HeroName, STAT_EXPERIENCE,delta);
	else
		print("Warning! Delta is negative. Hero gain 100 exp");
		ChangeHeroStat(HeroName, STAT_EXPERIENCE,100);
	end;
	print("Now ",HeroName, " has ", GetHeroStat(HeroName, STAT_EXPERIENCE)," exp");
end;

function GetExpToLevel( j )
	local sum;      --LEVEL 1 2    3    4    5    6    7    8     9     10    11    12
	ExpArrayLess12 = {0,1000,2000,3200,4600,6200,8000,10000,12200,14700,17500,20600};
	ExpArrayLess12.n = 12;
					--LEVEL 13    14    15    16    17    18    19    20    21    22     23     24
	ExpArrayMore12 = {24320,28784,34141,40569,48283,57539,68647,81977,97972,117166,140200,167839};
	ExpArrayMore12.n = 12;
					--LEVEL 25     26     27     28     29     30     31      32      33      34
	ExpArrayMore25 = {201007,244126,304491,395040,539917,786208,1229533,2071000,3756484,7294215};
	ExpArrayMore25.n = 10;
	if j <= 12 then
		sum = ExpArrayLess12[j];
	else
		if j < 25 then
			sum = ExpArrayMore12[j-12];
		else
			if j < 35 then
				sum = ExpArrayMore25[j-24];
			else
				print("Das ist fantastisch!!!");
				sum = 0;
			end;
		end;
	end;
	print("Hero need ", sum, " experience to gain level ",j);
	return sum;
end;

function cyrus_out(heroname)
	print("Cyrus has gone");
	Trigger(REGION_ENTER_AND_STOP_TRIGGER,"slonek",nil);
	Trigger(OBJECT_TOUCH_TRIGGER, "cyrus_teleport", "port_check", nil);
	UnblockGame();
	messages(9);
	SetObjectiveState("sec3",OBJECTIVE_ACTIVE);
	SetObjectiveVisible("prim3",nil);
	sleep(5);
	startThread( SObjective3 );
	SetObjectEnabled("cyrus_teleport",nil);
	SetRegionBlocked("teleport",1,PLAYER_2);
	SetRegionBlocked("teleport",1,PLAYER_3);
	SetObjectPosition(CyrusName, 168, 173);
end;

function onError()
	print("Error occured in function SetArtefactUntrans");
end;

function SetArtefactUntrans(ArtefactName)
	errorHook(onError);
	RemoveArtefact("Berein",ArtefactName);
	GiveArtefact("Berein",ArtefactName,1);
end;

function ShowLevel()
	print("Town1. Dwelling 7 Level = ",GetTownBuildingLevel("Town1",TOWN_BUILDING_DWELLING_7));
	print("Town1. Dwelling 6 Level = ",GetTownBuildingLevel("Town1",TOWN_BUILDING_DWELLING_6));
	print("Town1. Magic Guild Level = ",GetTownBuildingLevel("Town1",TOWN_BUILDING_MAGIC_GUILD));
	print("Town2. Dwelling 7 Level = ",GetTownBuildingLevel("Town2",TOWN_BUILDING_DWELLING_7));
	print("Town2. Dwelling 6 Level = ",GetTownBuildingLevel("Town2",TOWN_BUILDING_DWELLING_6));
	print("Town2. Magic Guild Level = ",GetTownBuildingLevel("Town2",TOWN_BUILDING_MAGIC_GUILD));
	print("Town3. Dwelling 7 Level = ",GetTownBuildingLevel("Town3",TOWN_BUILDING_DWELLING_7));
	print("Town3. Dwelling 6 Level = ",GetTownBuildingLevel("Town3",TOWN_BUILDING_DWELLING_6));
	print("Town3. Magic Guild Level = ",GetTownBuildingLevel("Town3",TOWN_BUILDING_MAGIC_GUILD));
end;
--ambush
function ambush_time()
	while GetDate(WEEK) == 1 do
		sleep(5);
	end;
	if factor > 0.6 then
		Trigger( REGION_ENTER_WITHOUT_STOP_TRIGGER, "ambush2", "haven_ambush" );
	end;
end;

function haven_ambush(hero)
	if hero == "Isabell" then
		local move = GetHeroStat("Isabell", 7);
		local diff = GetDate(WEEK) - 2 + factor;
		ChangeHeroStat("Isabell", 7, -10000);
		sleep(1);
		ChangeHeroStat("Isabell", 7, move);
		MessageBox("/Maps/Scenario/C3M3/haven_ambush.txt");
		if factor == 3 then
			StartCombat(hero, nil, 6, 2, 40 + diff * 15, 3, 20 + diff * 5, 11, diff, 5, 5  + diff * 5, 9, 1 + diff, 1, 40 + diff * 15, nil,nil);
			else StartCombat(hero, nil, 5, 2, 20 + diff * 10, 3, 15 + diff * 5, 10, 1 + diff, 5, 10  + diff * 5, 1, 20  + diff * 10, nil,nil);
		end;
		Trigger( REGION_ENTER_WITHOUT_STOP_TRIGGER, "ambush2", nil );
	end;
end;

function mag_ambush(hero)
	if GetObjectOwner(hero) == 1 and IsHeroAlive(hero) then
		local diff = GetDate(WEEK) - 1 + factor;
		MessageBox("/Maps/Scenario/C3M3/mag_ambush.txt");
		StartCombat(hero, nil, 5, 58, 80 + diff * 20, 62, 30 + diff * 10, 63, 10 + 3 * diff, 64, 11  + diff * 4, 66, 8  + 2 * diff, nil,"mag_result");
	end;
end;

function mag_result(hero, result)
	if result and IsHeroAlive(hero) then
		Trigger( REGION_ENTER_AND_STOP_TRIGGER, "ambush1",nil );
	end;
end;

-----------------
---C3M3 script---
-----------------
---Disable AI of quest heroes---
EnableHeroAI(CyrusName, nil);
EnableHeroAI(ElvenHero, nil);
startThread( ObjectiveGetArtefactComplete,  ARTIFACT_STAFF_OF_VEXINGS, "prim1","/DialogScenes/C3/M3/R2/DialogScene.xdb#xpointer(/DialogScene)");
startThread( ObjectiveGetArtefactComplete,  ARTIFACT_CLOAK_OF_MOURNING, "prim2","/DialogScenes/C3/M3/R5/DialogScene.xdb#xpointer(/DialogScene)");
startThread( PObjective4 );
startThread( PObjective5 );
startThread( SObjective1 );
startThread( SObjective2 );
startThread( SObjective5 );
startThread(UpEnemy);
startThread(DifficultyDependency);
startThread(setupNeutrals);
startThread(EnableAIForRazzakAndTimerkhan);
startThread(ambush_time);
startThread(open_yard, "shipyard1");
startThread(open_yard, "shipyard2");
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "astral_can_be_in_sight","cyrus_retreat", nil );
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "ambush1","mag_ambush" );
Trigger(OBJECT_TOUCH_TRIGGER ,'Town1', "GiveStaffOfVexings");
Trigger(OBJECT_TOUCH_TRIGGER ,'Town2', "GiveCloackOfMourning");
Trigger(REGION_ENTER_AND_STOP_TRIGGER,"slonek","cyrus_out");
print("all functions is run");