mines = {"SAW", "ORE", "GEM", "SULFUR", "MERCURY", "CRYSTAL"};

if GetDifficulty() == DIFFICULTY_EASY then
	diff = 0;
	print("easy");
end;

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

if GetDifficulty() == DIFFICULTY_HARD then
	diff = 2;
	AddObjectCreatures("guards", 84, 30);
	print("hard");
end;

if GetDifficulty() == DIFFICULTY_HEROIC then
	diff = 3;
	AddObjectCreatures("guards", 84, 100);
	print("heroic");
end;

function map_up()
	local towns = GetObjectNamesByType("TOWN");
	local pl_towns = {};
	local k = 1;
	if diff > 1 then
		for i=2,5 do
			k = 1;
			pl_towns = {};
			DenyAIHeroesFlee(i,1);
			for j, town in towns do
				if GetObjectOwner(town) == i then
					pl_towns[k] = town;
					k = k + 1;
				end;
			end;
			if pl_towns ~= {} then
				startThread(control_towns, pl_towns, i);
			end;
			for j=0,5 do
				SetPlayerResource(i,j,30*diff);
			end;
			SetPlayerResource(i,6,40000*diff);
			Trigger(PLAYER_ADD_HERO_TRIGGER, i, "ai_hero");
		end;
		local m_array = {};
		for i, names in mines do
			m_array = GetObjectNamesByType(names);
			print(names);
			if m_array ~= {} then
				for j, mine in m_array do
					Trigger(OBJECT_CAPTURE_TRIGGER, mine, "mine_flag");
				end;
			end;
		end;
		if diff == 3 then
			ChangeHeroStat("Almegir", 0, 100000);
			for i=1,4 do
				ChangeHeroStat("Almegir", i, diff);
			end;
			SetHeroLootable("Almegir", nil);
			GiveArtefact("Almegir", 36);
			GiveArtefact("Almegir", 37);
			GiveArtefact("Almegir", 38);
			GiveArtefact("Almegir", 39);
			GiveArtefact("Almegir", 41);
			GiveArtefact("Almegir", 42);
			GiveArtefact("Almegir", 43);
			GiveArtefact("Almegir", 22);
			GiveArtefact("Almegir", 25);
		end;
	end;
end;

function control_towns(towns, owner)
	if towns ~= {} then
		if owner ~= 4 then
			local heroes = GetPlayerHeroes(owner);
			for i, hero in heroes do
				startThread(capture_mines, hero);
			end;
			for i, town in towns do
				EnableAIHeroHiring(owner, town, nil);
				if owner == 2 then
					AddObjectCreatures(town, 16, 8 * diff);
					AddObjectCreatures(town, 17, 5 * diff);
					AddObjectCreatures(town, 19, 2*diff);
				end;
				if owner == 3 then
					AddObjectCreatures(town, 57, 5 * diff);
					AddObjectCreatures(town, 59, 3 * diff);
					AddObjectCreatures(town, 61, diff);
				end;
				if owner == 5 then
					AddObjectCreatures(town, 1, 12 * diff);
					AddObjectCreatures(town, 3, 4 * diff);
					AddObjectCreatures(town, 5, 2*diff);
				end;
			end;
			for i, town in towns do
				EnableAIHeroHiring(owner, town, not nil);
				while ((length(heroes)+2*i) > length(GetPlayerHeroes(owner))) do
					sleep(5);
					if GetDate(DAY) == 8 then
						break;
					end;
				end;
				for j, lt in towns do
					EnableAIHeroHiring(owner, lt, nil);
				end;
			end;
			for i, town in towns do
				EnableAIHeroHiring(owner, town, not nil);
			end;
			else startThread(dungeon_town, towns[1]);
		end;
	end;
end;

function dungeon_town(town)
	while GetObjectOwner(town) == 4 do
		sleep(3);
		while GetDate(3) ~= 1 do
			sleep(5);
		end;
		if GetObjectOwner(town) == 4 then
			SetPlayerResource(4, 6, GetPlayerResource(4,6) + 100000);
			SetObjectDwellingCreatures(town, 71, GetObjectDwellingCreatures(town, 71) + 10+7*diff);
			SetObjectDwellingCreatures(town, 73, GetObjectDwellingCreatures(town, 73) + 6+6*diff);
			SetObjectDwellingCreatures(town, 75, GetObjectDwellingCreatures(town, 75) + 7+5*diff);
			SetObjectDwellingCreatures(town, 77, GetObjectDwellingCreatures(town, 77) + 5+4*diff);
			SetObjectDwellingCreatures(town, 79, GetObjectDwellingCreatures(town, 79) + 3+3*diff);
			SetObjectDwellingCreatures(town, 81, GetObjectDwellingCreatures(town, 81) + 2+2*diff);
			SetObjectDwellingCreatures(town, 83, GetObjectDwellingCreatures(town, 83) + 2*diff);
		end;
		if GetDate(0) > 28 then
			for i=2,5 do
				Trigger(PLAYER_ADD_HERO_TRIGGER, i, nil);
			end;
		end;
		while GetDate(3) == 1 do
			sleep(5);
		end;
	end;
end;

function ai_hero(hero)
	if GetHeroStat(hero, 0) < 1000 then
		ChangeHeroStat(hero, 0, 1000 + 2000 * (GetDate(WEEK)-1) * (diff-1));
		if random(2) == 0 then
			GiveHeroSkill(hero, 1);
		end;
	end;
	startThread(capture_mines, hero);
end;

function capture_mines(hero)
	local m_array = {};
	if IsHeroAlive(hero) then
		for i, names in mines do
			m_array = GetObjectNamesByType(names);
			if m_array ~= {} then
				for j, mine in m_array do
					if GetObjectOwner(mine) == 0 then
						if len(mine, hero) < 130 then
							SetAIHeroAttractor(mine, hero, 1);
						end;
					end;
				end;
			end;
		end;
	end;
end;

function mine_flag(old, new, hero)
	local mine = find_mine(hero);
	local m_array = {};
	if mine ~= nil then
		Trigger(OBJECT_CAPTURE_TRIGGER, mine, nil);
		local heroes = {};
		for i=2,5 do
			heroes = GetPlayerHeroes(i);
			for j, hero in heroes do
				for k, names in mines do
					m_array = GetObjectNamesByType(names);
					if m_array ~= {} then
						for l, mine in m_array do
							if GetObjectOwner(mine) ~= 0 then
								SetAIHeroAttractor(mine, hero, 0);
							end;
						end;
					end;
				end;
			end;
		end;
	end;
end;

function find_mine(hero)
	local res = nil;
	if IsHeroAlive(hero) then
		local m_array = {};
		local mine_close = nil;
		for i, names in mines do
			m_array = GetObjectNamesByType(names);
			if m_array ~= {} then
				for j, mine in m_array do
					if res == nil then
						res = mine;
					end;
					if GetObjectOwner(mine) == GetObjectOwner(hero) then
						if len(hero, mine) < len(hero,res) then
							res = mine;
						end;
					end;
				end;
			end;
		end;
	end;
	return res
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 objective1()
	StartDialogScene("/DialogScenes/Single/SXL1/R1/DialogScene.xdb#xpointer(/DialogScene)");
	SetObjectiveState("obj1", OBJECTIVE_ACTIVE);
	SetObjectiveState("st_obj", OBJECTIVE_ACTIVE);
end;

function disactivation()
	SetObjectEnabled("portal", nil);
	SetObjectEnabled("seer_hut", nil);
	SetObjectEnabled("dragon_guardian", nil);
	SetObjectEnabled("key_tent", nil);
	SetObjectEnabled("observer", nil);
	SetRegionBlocked("special_for_akimovs_cheat", true, 1);
end;

function SXL1R4_SXL1R5()
	if GetObjectiveState("obj2")==OBJECTIVE_UNKNOWN then
		sleep(2);
		StartDialogScene("/DialogScenes/Single/SXL1/R4/DialogScene.xdb#xpointer(/DialogScene)");
	elseif GetObjectiveState("obj2")==OBJECTIVE_ACTIVE then
		sleep(2);
		StartDialogScene("/DialogScenes/Single/SXL1/R5/DialogScene.xdb#xpointer(/DialogScene)");
	end;
	OpenCircleFog(142, 139, GROUND, 14, 1);
	Trigger(OBJECT_TOUCH_TRIGGER, "observer", nil)
end;	

----------- Objective 2 ----------- 

function SXL1R2_objective2()
	local p_ore = GetPlayerResource(1, ORE);
	SetPlayerResource(1, ORE, p_ore + 5)
	SetObjectiveState("st_obj", OBJECTIVE_COMPLETED);
	sleep(3);
	SetObjectiveState("obj2", OBJECTIVE_ACTIVE);
	sleep(5);
	StartDialogScene("/DialogScenes/Single/SXL1/R2/DialogScene.xdb#xpointer(/DialogScene)");
	sleep(3);
	Trigger(OBJECT_TOUCH_TRIGGER, "seer_hut", "SXL1R2_objective2_complete");
end;

--SXL1R2_SXL1R3--

function SXL1R2_objective2_complete()
	local res = GetPlayerResource(1, GOLD) - 50000
	print("seerqest 1 started...");
	if GetPlayerResource(1, GOLD)>=50000 then
		SetPlayerResource(1, GOLD, res)
		SetObjectiveState("obj2", OBJECTIVE_COMPLETED);
		LevelUpHero("Muscip");
		sleep(5);
		StartDialogScene("/DialogScenes/Single/SXL1/R3/DialogScene.xdb#xpointer(/DialogScene)");
		sleep(3);
		SetObjectiveState("obj3", OBJECTIVE_ACTIVE);
		Trigger(OBJECT_TOUCH_TRIGGER, "seer_hut", "SXL1R12_objective3");
	end;
end;

----------- Objective 3 ----------- 

function SXL1R12_objective3(heroName)
	local res = GetPlayerResource(1, GOLD) + 10000
	if HasArtefact(heroName, ARTIFACT_DRAGON_TEETH_NECKLACE) then
		SetObjectiveState("obj3", OBJECTIVE_COMPLETED);
		SetPlayerResource(1, GOLD, res)
		sleep(3);
		StartDialogScene("/DialogScenes/Single/SXL1/R12/DialogScene.xdb#xpointer(/DialogScene)");
		sleep(3);
		SetObjectiveState("obj7", OBJECTIVE_ACTIVE);
		Trigger(OBJECT_TOUCH_TRIGGER, "seer_hut", "SXL1R13_objective7");
	end;
end;

----------- Objective 4 ----------- 

function SXL1R6_objective4()
	StartDialogScene("/DialogScenes/Single/SXL1/R6/DialogScene.xdb#xpointer(/DialogScene)");
	SetObjectiveState("obj4", OBJECTIVE_ACTIVE);
	sleep(5);
	Trigger(OBJECT_TOUCH_TRIGGER, "key_tent", "SXL1R7_key_master");
end;

function SXL1R7_key_master(heroName)	
	local p_gold = GetPlayerResource(1, GOLD) + 3000
	if GetHeroCreatures(heroName,CREATURE_FOOTMAN) > 99 then
		print("Hero ",heroName,"got 100 swardsman");
		sleep(3);
		RemoveHeroCreatures(heroName, CREATURE_FOOTMAN, 100);
		sleep(3);
		StartDialogScene("/DialogScenes/Single/SXL1/R7/DialogScene.xdb#xpointer(/DialogScene)");
		sleep(3);
		SetObjectiveState("obj4", OBJECTIVE_COMPLETED);
		SetPlayerResource(1, GOLD, p_gold)
		sleep(3);
		GiveBorderguardKey(1, RED_KEY);
		sleep(3);
		Trigger(OBJECT_TOUCH_TRIGGER, "key_tent", nil);
		startThread(SXL1R8_objective5);
	end;
end;	

----------- Objective 5 ----------- 

function SXL1R8_objective5()
while 1 do
	if IsObjectExists("bg")==nil then
		OpenCircleFog(142, 139, GROUND, 14, 1);
		BlockGame();
		sleep(2);
		EnableHeroAI("Almegir", nil);
		sleep(20);
		SetObjectPos("Almegir", 145, 146, GROUND);
		MoveCamera(142, 139, GROUND,50, 3.14/3, 0);
		ChangeHeroStat("Almegir", STAT_MOVE_POINTS, 3000);
		sleep(15);
		MoveHeroRealTime("Almegir", 142, 139, GROUND);
		local len = 100; 
		while len > 1 do
			x, y = GetObjectPos( "Almegir" );
			len = sqrt( (x - 142) * (x - 142) + (y - 139) * (y - 139) );
			print( "len = ", len);
			sleep( 1 );
		end;
		ChangeHeroStat("Almegir", STAT_MOVE_POINTS, 3000);
		RemoveObject("necklace");
		GiveArtefact("Almegir", ARTIFACT_DRAGON_TEETH_NECKLACE);
		--sleep(15);
		MoveHeroRealTime("Almegir", 145, 146, GROUND);
		local len = 100;
		while len > 1 do
			x, y = GetObjectPos( "Almegir" );
			len = sqrt( (x - 145) * (x - 145) + (y - 146) * (y - 146) );
			print( "len = ", len);
			sleep( 1 );
		end;
		--sleep(15);
		SetObjectPos("Almegir", 62, 27, UNDERGROUND);
		EnableHeroAI("Almegir", true);
		UnblockGame();
		sleep(3);
		StartDialogScene("/DialogScenes/Single/SXL1/R8/DialogScene.xdb#xpointer(/DialogScene)");
		SetObjectiveState("obj5", OBJECTIVE_ACTIVE);
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "wing_stealing", nil);
		startThread(SXL1R11_objective5_complete);
		break;
	end;
	sleep(3);
end;
end;

function SXL1R11_objective5_complete()
	while 1 do
		if IsHeroAlive("Almegir")==nil then
			SetObjectiveState("obj5", OBJECTIVE_COMPLETED);
			sleep(3);
			StartDialogScene("/DialogScenes/Single/SXL1/R11/DialogScene.xdb#xpointer(/DialogScene)");
			if diff == 3 then
				GiveArtefact("Muscip", ARTIFACT_DRAGON_TEETH_NECKLACE);
			end;
			break
		end;
	sleep(5);
	end;
end;


----------- Objective 6 ----------- 

function SXL1R9_objective6()
	local he = GetPlayerHeroes(1)
	for i, hero in he do
		if IsObjectInRegion( hero, "underground" ) then
		--if GetObjectsInRegion("underground", OBJECT_HERO)==he then
		StartDialogScene("/DialogScenes/Single/SXL1/R9/DialogScene.xdb#xpointer(/DialogScene)");
		SetObjectiveState("obj6", OBJECTIVE_ACTIVE);
		Trigger(REGION_ENTER_AND_STOP_TRIGGER, "underground", nil);
		startThread (objective6_completed);
		break;
		end;
	end;
end;

function objective6_completed()
	while 1 do
		if KnowHeroSpell("Muscip", SPELL_DIMENSION_DOOR)==true then
			StartDialogScene("/DialogScenes/Single/SXL1/R10/DialogScene.xdb#xpointer(/DialogScene)");
			SetObjectiveState("obj6", OBJECTIVE_COMPLETED);
			break;
		end;
	sleep(3)
	end;
end;


----------- Objective 7 ----------- 

function SXL1R13_objective7()
	local res = GetPlayerResource(1, GEM) - 100
	if GetPlayerResource(1, GEM)>=100 then
		SetPlayerResource(1, GEM, res)
		SetObjectiveState("obj7", OBJECTIVE_COMPLETED);
		LevelUpHero("Muscip");
		sleep(3);
		StartDialogScene("/DialogScenes/Single/SXL1/R13/DialogScene.xdb#xpointer(/DialogScene)");
		sleep(3);
		SetObjectiveState("obj8", OBJECTIVE_ACTIVE);
	end;
end;

----------- Objective 8 ----------- 

function SXL1R14_dguard(heroName)
	if HasArtefact(heroName, ARTIFACT_DRAGON_TEETH_NECKLACE)==true and GetObjectiveState("obj7")==OBJECTIVE_COMPLETED then
		BlockGame();
		StartDialogScene("/DialogScenes/Single/SXL1/R14/DialogScene.xdb#xpointer(/DialogScene)");
		SetRegionBlocked("special_for_akimovs_cheat", nil, 1);
		sleep(5);
		SetObjectPos("dragon_guardian",159, 2, GROUND);
		sleep(5)
		RemoveObject("dragon_guardian");
		UnblockGame();
		SetObjectiveState("obj1", OBJECTIVE_COMPLETED);
		SetObjectiveState("obj8", OBJECTIVE_COMPLETED);
		sleep(3);
		SetObjectiveState("obj9", OBJECTIVE_ACTIVE);
		startThread(SXL1R15_objective9);
		Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "dragon_guardian", nil);
	else
		MessageBox("/Maps/SingleMissions/SXL1/dragon_answer.txt");
	end;
end;

----------- Objective 9 ----------- 

function SXL1R15_objective9()
	while 1 do
		if IsObjectExists("guards")==nil then
			StartDialogScene("/DialogScenes/Single/SXL1/R15/DialogScene.xdb#xpointer(/DialogScene)");
			sleep(10);
			SetObjectiveState("obj9", OBJECTIVE_COMPLETED);
			SetObjectiveState("n_obj", OBJECTIVE_COMPLETED);			
			sleep(3);
			Win();
			break
		end;
	sleep(3);
	end;
end;	

----------- Lose ----------- 

function Lose()
	while 1 do
		if IsHeroAlive("Muscip")==nil then
			SetObjectiveState("n_obj", OBJECTIVE_FAILED);
			sleep(3);
			Loose();
			break
		end;
	sleep(5);
	end;
end;

------Start Line-------

objective1();
disactivation();
startThread( Lose );
startThread(map_up);
Trigger( OBJECT_TOUCH_TRIGGER, "observer", "SXL1R4_SXL1R5" );
Trigger( OBJECT_TOUCH_TRIGGER, "seer_hut", "SXL1R2_objective2" );
Trigger( OBJECT_TOUCH_TRIGGER, "key_tent", "SXL1R6_objective4" );
Trigger( REGION_ENTER_AND_STOP_TRIGGER, "underground", "SXL1R9_objective6" );
Trigger(REGION_ENTER_WITHOUT_STOP_TRIGGER, "dragon_guardian", "SXL1R14_dguard");