---FUNCTIONS---
for a = 0,6 do
	SetPlayerResource(PLAYER_1, a, 0);
end;

if GetDifficulty() == DIFFICULTY_EASY then
	dif = 0;
	print("easy");
	AddObjectCreatures("Raelag", CREATURE_ASSASSIN , 50);
	AddObjectCreatures("Raelag", CREATURE_BLOOD_WITCH , 30);
	AddObjectCreatures("Raelag", CREATURE_MATRIARCH , 5);
	RemoveObject("dogs");
end;

if GetDifficulty() == DIFFICULTY_NORMAL then
	dif = 0;
	print("normal");
	AddObjectCreatures("Raelag", CREATURE_ASSASSIN , 25);
	AddObjectCreatures("Raelag", CREATURE_BLOOD_WITCH , 15);
	AddObjectCreatures("Raelag", CREATURE_MATRIARCH , 2);
	RemoveObject("dogs");
end;

if GetDifficulty() == DIFFICULTY_HARD then
	dif = 1;
	print ("hard");
	AddHeroCreatures('Efion', 15, 150);
	GiveHeroSkill("Efion", 10);
	TeachHeroSpell("Efion", 5);
	TeachHeroSpell("Efion", 7);
	TeachHeroSpell("Efion", 9);
	TeachHeroSpell("Efion", 12);
	TeachHeroSpell("Efion", 17);
	RemoveArtefact("Efion", 7);
	GiveArtefact("Efion", 1);
	GiveArtefact("Efion", 9);
	GiveArtefact("Efion", 63);
	GiveArtefact("Efion", 23);
	GiveArtefact("Efion", 32);
	GiveArtefact("Efion", 57);
	GiveArtefact("Efion", 56);
	GiveArtefact("Efion", 19);
	GiveArtefact("Efion", 8);
	GiveHeroWarMachine("Efion", 1);
	GiveHeroWarMachine("Efion", 3);
	GiveHeroWarMachine("Efion", 4);
	SetHeroLootable("Efion", nil);
end;

if GetDifficulty() == DIFFICULTY_HEROIC then
	dif = 2;
	print ("heroic!");
	SetHeroCombatScript("Efion", '/Maps/Scenario/C4M4/BattleVSGrol.xdb#xpointer(/Script)');
	AddHeroCreatures('Efion', 24 , 14 + random(4));
	GiveHeroSkill("Efion", 3);
	GiveHeroSkill("Efion", 9);
	GiveHeroSkill("Efion", 10);
	GiveHeroSkill("Efion", 60);
	TeachHeroSpell("Efion", 7);
	TeachHeroSpell("Efion", 9);
	TeachHeroSpell("Efion", 12);
	TeachHeroSpell("Efion", 17);
	TeachHeroSpell("Efion", 21);
	TeachHeroSpell("Efion", 40);
	TeachHeroSpell("Efion", 235);
	RemoveArtefact("Efion", 7);
	GiveArtefact("Efion", 43);
	GiveArtefact("Efion", 51);
	GiveArtefact("Efion", 22);
	GiveArtefact("Efion", 42);
	GiveArtefact("Efion", 62);
	GiveArtefact("Efion", 49);
	GiveArtefact("Efion", 48);
	GiveArtefact("Efion", 15);
	GiveArtefact("Efion", 25);
	GiveArtefact("Efion", 41);
	GiveHeroWarMachine("Efion", 1);
	GiveHeroWarMachine("Efion", 3);
	GiveHeroWarMachine("Efion", 4);
	for i = 1,4 do
		ChangeHeroStat("Efion", i, 1 + random(3));
	end;
	SetHeroLootable("Efion", nil);
end;

function LostHero( HeroName )
	print ( HeroName );
	if HeroName == "Raelag" or HeroName == "Kelodin" then
		SetObjectiveState("prim3", OBJECTIVE_FAILED);
		sleep (10);
		Loose();
	end;
end;

function LostHero2( HeroName3 )
	if (HeroName3 == "Efion") then
		StartDialogScene('/DialogScenes/C4/M4/R3/DialogScene.xdb#xpointer(/DialogScene)');
		SetObjectiveState("prim2", OBJECTIVE_COMPLETED);
		sleep ( 2 );
		GiveBorderguardKey(1 , RED_KEY);
		LevelUpHero("Raelag");
	end;
end;

function key()
	StartDialogScene('/DialogScenes/C4/M4/R4/DialogScene.xdb#xpointer(/DialogScene)');
	SetObjectiveState('prim1', OBJECTIVE_COMPLETED);
	if HasArtefact("Raelag", 13) then
		SetGameVar("C4M4_Raelag_AFH", 1);
		else SetGameVar("C4M4_Raelag_AFH", 0);
	end;
	if HasArtefact("Kelodin", 13) then
		SetGameVar("C4M4_Kelodin_AFH", 1);
		else SetGameVar("C4M4_Kelodin_AFH", 0);
	end;
	sleep(5);
	LevelUpHero("Raelag");
	sleep(10);
	Win ();
end;

function tent_use()
	--	RemoveObject("Mushrooms");
	Trigger(OBJECT_TOUCH_TRIGGER, 'tent' , "tent_use2");
	sleep (1);
	StartDialogScene('/DialogScenes/C4/M4/D1/DialogScene.xdb#xpointer(/DialogScene)');
end;

function tent_use2()
	MessageBox('/Maps/Scenario/C4M4/keymaster.txt');
end;

function teleport(HeroName2)
	Trigger(OBJECT_TOUCH_TRIGGER, 'teleport1' , nil);
	BlockGame();
	SetObjectPosition(HeroName2, 68, 50);
	sleep(12);
	SetObjectEnabled('teleport1', not nil);
	StartDialogScene('/DialogScenes/C4/M4/R2/DialogScene.xdb#xpointer(/DialogScene)');
	sleep ( 6 );
	RemoveObject("del1");
	RemoveObject("del8");
	sleep ( 2 );
	RemoveObject("del2");
	RemoveObject("del7");
	sleep ( 2 );
	RemoveObject("del5");
	RemoveObject("del4");
	sleep ( 2 );
	RemoveObject("del3");
	RemoveObject("del6");
	SetObjectiveState('prim2', OBJECTIVE_ACTIVE);
	sleep(2);
	MoveHeroRealTime('Efion', 81, 51);
	sleep(20);
	SetObjectPosition('Efion', 64, 78, 0);
	UnblockGame();
	startThread(enable_demon);
--	EnableHeroAI('Efion', not nil);
end;

function demon_up()
	sleep(10);
	if GetGameVar("C4M3_Raelag_AFH") == "1" then
		GiveArtefact("Raelag", 13);
	end;
	if GetGameVar("C4M3_Kelodin_AFH") == "1" then
		GiveArtefact("Kelodin", 13);
	end;
	exp = GetHeroStat("Raelag", STAT_EXPERIENCE);
	if dif > 0 then
		exp = exp + GetHeroStat("Kelodin", STAT_EXPERIENCE);
	end;
	ChangeHeroStat('Efion', STAT_EXPERIENCE , exp/(3-dif));
	AddHeroCreatures('Efion',CREATURE_IMP , 50 + dif*50);
	AddHeroCreatures('Efion',CREATURE_HORNED_DEMON ,35 + dif*28);
	AddHeroCreatures('Efion',CREATURE_CERBERI , 25 + dif*18);
	AddHeroCreatures('Efion',CREATURE_INFERNAL_SUCCUBUS , 8 + dif*15);
	AddHeroCreatures('Efion',CREATURE_BALOR , 2 + dif*6);
	AddHeroCreatures('Efion',CREATURE_ARCHDEVIL , 1 + dif);
	for i=1,15 do
		SetRegionBlocked("blok"..i, not nil, 2);
	end;
	SetRegionBlocked("blok20", not nil, 2);
end;

function enable_demon()
	while GetCurrentPlayer == 1 do
		sleep(5);
	end;
	local day = GetDate(DAY) + 13 - 2 * dif + random(5);
	while GetDate(DAY) < day and IsObjectVisible(1, 'Efion') == nil do
		sleep(2);
	end;
	if IsHeroAlive('Efion') then
		EnableHeroAI('Efion', not nil);
	end;
end;

function loose()
	if GetDate(MONTH) == 3 then
		Loose();
	end;
	if GetDate(MONTH) == 2 and GetDate(WEEK) == 4 and GetDate(DAY_OF_WEEK) == 1 then
		MessageBox ("/Maps/Scenario/C4M4/C4M4_1.txt");
	end;
end;

---SCRIPT MAIN PART---
Trigger(OBJECT_TOUCH_TRIGGER , 'Gate','key');
Trigger( PLAYER_REMOVE_HERO_TRIGGER, 1, "LostHero" );
Trigger( PLAYER_REMOVE_HERO_TRIGGER, 2, "LostHero2" );
StartDialogScene('/DialogScenes/C4/M4/R1/DialogScene.xdb#xpointer(/DialogScene)');
EnableHeroAI('Efion', nil);
SetObjectEnabled('tent', nil);
SetObjectEnabled('teleport1', nil);
SetObjectEnabled('Gate', nil);
Trigger(OBJECT_TOUCH_TRIGGER , 'tent','tent_use');
Trigger(OBJECT_TOUCH_TRIGGER, 'teleport1', 'teleport');
Trigger(NEW_DAY_TRIGGER, 'loose');
startThread(demon_up);