path = "C6M4_";

town_array = {"town1","town2","town3","town4"};
town_array.n = 4;

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

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

if GetDifficulty() == DIFFICULTY_HARD then
	dif = 2;
	print ("hard");
end;

if GetDifficulty() == DIFFICULTY_HEROIC then
	dif = 3;
	print ("heroic!");
end;

function map_up()
	OpenCircleFog(58, 51, 0, 10, 1);
	StartDialogScene("/DialogScenes/C6/M5/R1/DialogScene.xdb#xpointer(/DialogScene)");
	SetObjectEnabled("bcitadel", nil);
	sleep(1);
	SetGameVar("C6M5_diff", dif);
	startThread(load_arts, "Zehir", path);
	startThread(load_arts, "Godric", path);
	startThread(load_arts, "Heam", path);
	startThread(load_arts, "Raelag", path);
	if dif > 1 then
		for i=1,4 do
			startThread(add_army, "town"..i, 0.5*(dif-1));
		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 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 TownCounter()
	print( "Town_count() = ",Town_count() );
	if Town_count() == 4 then
		SetObjectEnabled("bcitadel", true);
		Trigger(OBJECT_TOUCH_TRIGGER, "bcitadel", "bfight")
	end;
end;
		
function Town_count()
	local count = 0;
	for i=1, town_array.n do
		if ( GetObjectOwner(town_array[i]) == PLAYER_1 ) then
			count = count + 1;
		end;
	end;
	return count; 
end;

function barmy()
	MessageBox('/Maps/Scenario/C6M5/message-5.txt');
end

function bfight()
	QuestionBox('/Maps/Scenario/C6M5/message-6.txt', "fight_godric");
end

-- biara combat sequence --
-- godric heam zehir raelag --

function fight_godric()
	Trigger(OBJECT_TOUCH_TRIGGER, "bcitadel", nil)
	SiegeTown("Godric", "/Maps/Scenario/C6M5/Ur-Hekal.xdb#xpointer(/AdvMapTown)", 
	'/Scenes/CombatArenas/Boss_c6m5_Biara.xdb#xpointer(/AdventureFlybyScene)');
end

function fight_heam()
	SiegeTown("Heam", "/Maps/Scenario/C6M5/Ur-Hekal (2).xdb#xpointer(/AdvMapTown)", 
	'/Scenes/CombatArenas/Boss_c6m5_Biara2.xdb#xpointer(/AdventureFlybyScene)');
end

function fight_zehir()
	SiegeTown("Zehir", "/Maps/Scenario/C6M5/Ur-Hekal (3).xdb#xpointer(/AdvMapTown)", 
	'/Scenes/CombatArenas/Boss_c6m5_Biara3.xdb#xpointer(/AdventureFlybyScene)');
end

function fight_raelag()
	SiegeTown("Raelag", "/Maps/Scenario/C6M5/Ur-Hekal (4).xdb#xpointer(/AdvMapTown)", 
	'/Scenes/CombatArenas/Boss_c6m5_Biara4.xdb#xpointer(/AdventureFlybyScene)');
end

function BiaraIsDead()
	SetObjectiveState("capture_biara",OBJECTIVE_COMPLETED);
	SetObjectiveState("reach_deamon_lord",OBJECTIVE_ACTIVE);
	BlockGame();
	Save("Scene_21")
	sleep(20);
	StartDialogScene("/DialogScenes/C6/M5/D1/DialogScene.xdb#xpointer(/DialogScene)","teleporting");	
	sleep(20);
	UnblockGame();
end

function teleporting()
	SetObjectPosition("Zehir", 69, 67);	
	BlockGame();
	SetObjectPosition("Godric", 68, 66);	
	SetObjectPosition("Heam", 67, 65);	
	SetObjectPosition("Raelag", 66, 64);	
	sleep(10);
	ChangeHeroStat("Zehir", STAT_EXPERIENCE, 2356);
	ChangeHeroStat("Heam", STAT_EXPERIENCE, 2562);
	ChangeHeroStat("Godric", STAT_EXPERIENCE, 2346);
	ChangeHeroStat("Raelag", STAT_EXPERIENCE, 2784);
	UnblockGame();
	ChangeHeroStat("Zehir",STAT_MOVE_POINTS,3000)
	ChangeHeroStat("Godric",STAT_MOVE_POINTS,3000)
	ChangeHeroStat("Heam",STAT_MOVE_POINTS,3000)
	ChangeHeroStat("Raelag",STAT_MOVE_POINTS,3000)
end

function Question()
	QuestionBox('/Maps/Scenario/C6M5/message-8.txt', "Info");
end

function Info()
	MessageBox('/Maps/Scenario/C6M5/message-9.txt', "fight_godric_sovereign");
end

-- sovereign combat sequence --
-- godric heam zehir raelag --

function fight_godric_sovereign()
	StartCombat("Godric", "Kha-Beleth", 1, 
		16, 80 + dif * 15,	
		'/Maps/Scenario/C6M5/sovereign_godric.xdb#xpointer(/Script)', 
		"fight_heam_sovereign", 
		'/Arenas/CombatArena/FinalCombat/FinalCombat.(AdvMapTownCombat).xdb#xpointer(/AdvMapTownCombat)');
end

function fight_heam_sovereign()
	StartCombat("Heam", "Kha-Beleth", 1, 
		16, 100 + dif * 15,	
		'/Maps/Scenario/C6M5/sovereign_heam.xdb#xpointer(/Script)', 
		"fight_zehir_sovereign", 
		'/Arenas/CombatArena/FinalCombat/FinalCombat.(AdvMapTownCombat).xdb#xpointer(/AdvMapTownCombat)');
end

function fight_zehir_sovereign()
	StartCombat("Zehir", "Kha-Beleth", 2, 
		16, 83 + dif * 14,	85, 20 + dif * 7,
		'/Maps/Scenario/C6M5/sovereign_zehir.xdb#xpointer(/Script)', 
		"fight_raelag_sovereign", 
		'/Arenas/CombatArena/FinalCombat/FinalCombat.(AdvMapTownCombat).xdb#xpointer(/AdvMapTownCombat)');
end

function fight_raelag_sovereign()
	StartCombat("Raelag", "Kha-Beleth", 1, 
		16, 92 + dif * 10,	
		'/Maps/Scenario/C6M5/sovereign_raelag.xdb#xpointer(/Script)', 
		"CrossBattleDialogue", 
		'/Arenas/CombatArena/FinalCombat/FinalCombat.(AdvMapTownCombat).xdb#xpointer(/AdvMapTownCombat)');
end

function CrossBattleDialogue()
	if IsHeroAlive("Raelag") == true then
		StartDialogScene("/DialogScenes/C6/M5/D2/DialogScene.xdb#xpointer(/DialogScene)", "fight_godric_sovereign_noshield");
		else Loose()
	end		
end
	
--shield is destroyed--

function fight_godric_sovereign_noshield()
	print("starting combat");
	StartCombat("Godric", "Kha-Beleth", 2, 
		16, 100 + dif * 10, 18, 55 + dif * 15,
		'/Maps/Scenario/C6M5/sovereign_godric_noshield.xdb#xpointer(/Script)', 
		"fight_heam_sovereign_noshield", 
		'/Scenes/CombatArenas/Boss_c6m5_Sovereign2.xdb#xpointer(/AdventureFlybyScene)');
end	

function fight_heam_sovereign_noshield()
	print("starting combat");
	StartCombat("Heam", "Kha-Beleth", 2, 
		16, 110 + dif * 15, 39, 8 + dif * 4,
		'/Maps/Scenario/C6M5/sovereign_heam_noshield.xdb#xpointer(/Script)', 
		"fight_zehir_sovereign_noshield", 
		'/Scenes/CombatArenas/Boss_c6m5_Sovereign2.xdb#xpointer(/AdventureFlybyScene)');
end	

function fight_zehir_sovereign_noshield()
	print("starting combat");
	StartCombat("Zehir", "Kha-Beleth", 2, 
		16, 79 + dif * 13,	21, 24 + dif * 4,
		'/Maps/Scenario/C6M5/sovereign_zehir_noshield.xdb#xpointer(/Script)', 
		"fight_raelag_sovereign_noshield", 
		'/Scenes/CombatArenas/Boss_c6m5_Sovereign2.xdb#xpointer(/AdventureFlybyScene)');
end	

function fight_raelag_sovereign_noshield()
	print("starting combat");
	StartCombat("Raelag", "Kha-Beleth", 2, 
		16, 88 + dif * 12,	26, 11 + dif * 3,
		'/Maps/Scenario/C6M5/sovereign_raelag_noshield.xdb#xpointer(/Script)', 
		"SovereignIsDead", 
		'/Scenes/CombatArenas/Boss_c6m5_Sovereign2.xdb#xpointer(/AdventureFlybyScene)');
end	


function SovereignIsDead()
	if IsHeroAlive("Raelag") == true then
			Save("Scene_22");
			sleep(40);
			StartDialogScene("/DialogScenes/C6/M5/D3/DialogScene.xdb#xpointer(/DialogScene)");
			sleep(10);
			SetObjectiveState("reach_deamon_lord",OBJECTIVE_COMPLETED);
			SetObjectiveState("save_isabell",OBJECTIVE_COMPLETED);
		else Loose()
	end
end

function HeroesMustSurvive()
	print("Thread HeroesMustSurvive has been started...");
	while 1 do
		sleep(1);
		if (   IsHeroAlive("Zehir")  == nil 
			or IsHeroAlive("Godric") == nil 
			or IsHeroAlive("Raelag") == nil 
			or IsHeroAlive("Heam")   == nil) then
			SetObjectiveState("prim4",OBJECTIVE_FAILED);
			sleep(1);
			Loose(0);
			break;
		end;
	end;
end;

function WinLoose()
	while 1 do
		if GetObjectiveState("save_isabell") == OBJECTIVE_COMPLETED and
			GetObjectiveState("capture_biara") == OBJECTIVE_COMPLETED then
			sleep(20);
			Win(); 
			return
		end;
		if GetObjectiveState("save_isabell") == OBJECTIVE_FAILED or
			GetObjectiveState("capture_biara") == OBJECTIVE_FAILED then
			sleep(40);
			Loose();
			return
		end;
		sleep();
	end;
end;

g = 0
h = 0
j = 0
k = 0

function ddoorTransfer()
	while 1 do
		if IsObjectInRegion("Zehir", "ddoor01") == true then g =1 end
		if IsObjectInRegion("Godric", "ddoor03") == true then h =1 end
		if IsObjectInRegion("Raelag", "ddoor04") == true then j =1 end
		if IsObjectInRegion("Heam", "ddoor02") == true then k =1 end
		if g + h + j + k == 4 then 
			SetObjectEnabled("bcitadel", true);
			Trigger(OBJECT_TOUCH_TRIGGER, "bcitadel", "bfight") ;
			break;
			end
	sleep(1);
	end
end

function own()
	SetObjectPosition("Zehir", 56, 45);	
	SetObjectPosition("Godric", 57, 46);	
	SetObjectPosition("Heam", 58, 47);	
	SetObjectPosition("Raelag", 59, 48);	
end

Trigger(OBJECT_CAPTURE_TRIGGER, "town1", "TownCounter");
Trigger(OBJECT_CAPTURE_TRIGGER, "town2", "TownCounter");
Trigger(OBJECT_CAPTURE_TRIGGER, "town3", "TownCounter");
Trigger(OBJECT_CAPTURE_TRIGGER, "town4", "TownCounter");
startThread(map_up);
startThread(HeroesMustSurvive);
startThread(WinLoose);
startThread(ddoorTransfer);

Trigger(OBJECT_TOUCH_TRIGGER, "bcitadel", "barmy")
Trigger(OBJECT_TOUCH_TRIGGER, "scitadel", "Question")