StartDialogScene("/DialogScenes/C2/M5/R1/DialogScene.xdb#xpointer(/DialogScene)");
SetWarfogBehaviour(1,0);
SetPlayerStartResource(PLAYER_1,WOOD,5);
SetPlayerStartResource(PLAYER_1,ORE,5);
SetPlayerStartResource(PLAYER_1,SULFUR,0);
SetPlayerStartResource(PLAYER_1,GEM,0);
SetPlayerStartResource(PLAYER_1,MERCURY,0);
SetPlayerStartResource(PLAYER_1,CRYSTAL,0);
SetPlayerStartResource(PLAYER_1,GOLD,500);
OpenCircleFog(27,15,0,16,PLAYER_1);
drag_altern = {14, 70, 84, 91};
drag_coef = {1, 1, 0.7, 0.5};
path = "C2M4_";

if GetDifficulty() == DIFFICULTY_HEROIC then
	print("difficulty is heroic!");
	drag_coef = {2, 2, 1.7, 1.5};
end;

	
function NoEmerald()
	local dragons = GetObjectNamesByType("DRAGON");
	local x = 0;
	local y = 0;
	local z = 0;
	local num = 0;
	local index = 0;
	local rot = 320;
	for i, drag in dragons do
		x,y,z = GetObjectPosition(drag);
		num = GetObjectCreatures(drag, 55) + GetObjectCreatures(drag, 56);
		if num > 0 then
			RemoveObject(drag);
			index = 1 + random(4);
			sleep(1);
			if x == 89 or x == 154 then
				rot = 180;
			end;
			CreateMonster("", drag_altern[index], num * drag_coef[index], x, y, z, 3, 1, rot);
			print("remove emerald dragon, create monster "..index);
			num = 0;
			rot = 320;
		end;
	end;
end;

function load_arts(hero, mission)
	sleep(1);
	if IsHeroAlive(hero) then
		for i = 1, 71 do
			if GetGameVar(mission..hero.."_"..i) == "1" then
				GiveArtefact(hero, i);
			end;
		end;
	end;
end;

function AgraelComeToTieru()
	print("Thread AgraelComeToTieru has been started...");
	Save("VisitToTieru");
	StartDialogScene("/DialogScenes/C2/M5/D1/DialogScene.xdb#xpointer(/DialogScene)");
	sleep(10);
	SetObjectiveState('prim1', OBJECTIVE_COMPLETED);
	sleep(10);
	SetObjectiveState('prim2', OBJECTIVE_COMPLETED);
	sleep(20);
	Win(0);
end;

function AgraelSurvive()
	print ("Thread AgraelSurvive has been started...");
	while 1 do
		sleep(10);
		if (IsHeroAlive("Agrael") == nil) then
			print("Our glorious hero is dead, but his dark soul will be with us forever...");
			sleep(20);
			SetObjectiveState("prim2",OBJECTIVE_FAILED);
			sleep(30);
			Loose(0);
			break;
		end;
	end;
end;


---script---
startThread(AgraelSurvive);
startThread(NoEmerald);
startThread(load_arts, "Agrael", path);
Trigger(REGION_ENTER_AND_STOP_TRIGGER,'tieru', 'AgraelComeToTieru')