HERO_NAME = "Christian"

demons = {"HOUND", "CERBERI", "NIGHTMARE", "FIEND", "BALOR", "DEVIL"};

function DifficultyDependency()
	sleep(1);
	SetPlayerStartResource(1,ORE,0);
	SetPlayerStartResource(1,WOOD,0);
	SetPlayerStartResource(1,CRYSTAL,0);
	SetPlayerStartResource(1,GEM,0);
	SetPlayerStartResource(1,SULFUR,0);
	SetPlayerStartResource(1,MERCURY,0);
	if GetDifficulty() == DIFFICULTY_EASY then
		print ("easy");
		SetPlayerStartResource(1,GOLD,20000);
	end;
	if GetDifficulty() == DIFFICULTY_NORMAL then 
		SetPlayerStartResource(1,GOLD,10000);
		print("Difficulty level is normal.");
	else
		if GetDifficulty() == DIFFICULTY_HARD then 
			SetPlayerStartResource(1,GOLD,5000);
			startThread(add_demons, 0.33);
			print("Difficulty level is hard.");
		else
		if GetDifficulty() == DIFFICULTY_HEROIC then 
				SetPlayerStartResource(1,GOLD,1000);
				startThread(add_demons, 0.5);
				print("Difficulty level is heroic.");
			end;
		end;
	end;
end;

function add_army(mon, ind)
	if IsObjectExists(mon) then
		for i = 15, 28 do
			if GetObjectCreatures(mon, i) > 0 then
				AddObjectCreatures(mon, i, ind * GetObjectCreatures(mon, i));
			end;
		end;
	end;
end;

function add_demons(coef)
	local creatures = {};
	for i, name in demons do
		creatures = GetObjectNamesByType(name);
		if creatures ~= {} then
			for j, mon in creatures do
				startThread(add_army, mon, coef);
			end;
		end;
	end;
end;

function PlayerWin()
	while 1 do
		sleep(10);
		if (IsObjectInRegion(HERO_NAME,"end") == true) then
			SetObjectiveState("HeroSurvive", OBJECTIVE_COMPLETED);
			sleep(5);
			SetObjectiveState("PlayerWin", OBJECTIVE_COMPLETED);
			sleep(5);
			Win(0);
			break;
		end;
	end;
end;

function LostHero( HeroName )
	if ( HeroName == HERO_NAME ) then
		SetObjectiveState("HeroSurvive", OBJECTIVE_FAILED);
		sleep (10);
		Loose();
	end;
end;

function redkey()
	SetObjectiveState('first', OBJECTIVE_ACTIVE);
	Trigger(OBJECT_TOUCH_TRIGGER,"red" , "redquest");
end;

function bluekey()
	SetObjectiveState('second', OBJECTIVE_ACTIVE);
	Trigger(OBJECT_TOUCH_TRIGGER,"blue" , "bluequest");
end;

function greenkey()
	SetObjectiveState('third', OBJECTIVE_ACTIVE);
	Trigger(OBJECT_TOUCH_TRIGGER,"green" , "greenquest");
end;

function redquest()
	print("Thread redquest has been started...");
	if GetHeroCreatures(HERO_NAME,CREATURE_PEASANT) >= 20 then
		if GetHeroCreatures(HERO_NAME,CREATURE_PEASANT) >= 2 * GetHeroCreatures(HERO_NAME,CREATURE_FAMILIAR) then
			if GetHeroCreatures(HERO_NAME,CREATURE_PEASANT) >= 4 * GetHeroCreatures(HERO_NAME,CREATURE_WOOD_ELF) then
			if GetHeroCreatures(HERO_NAME,CREATURE_PEASANT) >= 20 * GetHeroCreatures(HERO_NAME,CREATURE_GREMLIN) then
			SetObjectiveState("first",OBJECTIVE_COMPLETED);
			ObjectiveExp(HERO_NAME);
			Trigger(OBJECT_TOUCH_TRIGGER, "red" , nil);
			GiveBorderguardKey(1 , RED_KEY);
			else
				print("Peasant >= 20 * Gremlin not success");
			end;
			else
				print("Peasant >= 4 * Wood Elf not success");
			end;
		else
			print("Peasants >= 2 * Familiar not success");
		end;
		else
		print("not enough peasants");
	end;
end;

function bluequest()
	Zombie = GetHeroCreatures(HERO_NAME,CREATURE_ZOMBIE);
	Walking_dead = GetHeroCreatures(HERO_NAME,CREATURE_WALKING_DEAD);
	Vampire = GetHeroCreatures(HERO_NAME,CREATURE_VAMPIRE);
	Vampire_lord = GetHeroCreatures(HERO_NAME,CREATURE_VAMPIRE_LORD);
	Lich = GetHeroCreatures(HERO_NAME,CREATURE_LICH);
	Demilich = GetHeroCreatures(HERO_NAME,CREATURE_DEMILICH);
	if (Zombie + Walking_dead == 25 and Vampire + Vampire_lord == 20 and Lich + Demilich == 16) then
		SetObjectiveState("second",OBJECTIVE_COMPLETED);
		ObjectiveExp(HERO_NAME);
		Trigger(OBJECT_TOUCH_TRIGGER, "blue" , nil);
		GiveBorderguardKey(1 , BLUE_KEY);
	end;
end;

function greenquest()
	if (GetHeroCreatures(HERO_NAME,CREATURE_GIANT) == 1
		and GetHeroCreatures(HERO_NAME,CREATURE_DEVIL) == 2 
		and GetHeroCreatures(HERO_NAME,CREATURE_BONE_DRAGON) == 2
		and GetHeroCreatures(HERO_NAME,CREATURE_ANGEL) == 5) then
		SetObjectiveState("third",OBJECTIVE_COMPLETED);
		ObjectiveExp(HERO_NAME);
		Trigger(OBJECT_TOUCH_TRIGGER, "green" , nil);
		GiveBorderguardKey(1 , GREEN_KEY);
		startThread(PlayerWin);
	end;
end;

--DEBUG FUNCTIONS ---------
function GiveMeQuest1Creatures()
	AddHeroCreatures(HERO_NAME, CREATURE_PEASANT, 10);
	AddHeroCreatures(HERO_NAME, CREATURE_WOOD_ELF, 10);
	AddHeroCreatures(HERO_NAME, CREATURE_FAMILIAR, 20);
	AddHeroCreatures(HERO_NAME, CREATURE_GREMLIN, 2);
end;

function GiveMeQuest1CreaturesNew()
	AddHeroCreatures(HERO_NAME, CREATURE_PEASANT, 20);
	AddHeroCreatures(HERO_NAME, CREATURE_WOOD_ELF, 5);
	AddHeroCreatures(HERO_NAME, CREATURE_FAMILIAR, 10);
	AddHeroCreatures(HERO_NAME, CREATURE_GREMLIN, 1);
end;

function GiveMeQuest2Creatures()
	AddHeroCreatures(HERO_NAME, CREATURE_ZOMBIE, 25);
	AddHeroCreatures(HERO_NAME, CREATURE_VAMPIRE, 20);
	AddHeroCreatures(HERO_NAME, CREATURE_LICH, 16);
end;

function GiveMeQuest3Creatures()
	AddHeroCreatures(HERO_NAME, CREATURE_GIANT, 1);
	AddHeroCreatures(HERO_NAME, CREATURE_DEVIL, 2);
	AddHeroCreatures(HERO_NAME, CREATURE_BONE_DRAGON, 2);
	AddHeroCreatures(HERO_NAME, CREATURE_ANGEL, 5);
end;

function ObjectiveExp(HeroName)
	local ToLevel = GetExpToLevel(GetHeroLevel(HeroName)+1);
	local delta = (ToLevel - GetHeroStat(HeroName, STAT_EXPERIENCE)) / 2;
	ChangeHeroStat(HeroName, STAT_EXPERIENCE,delta);
	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;

SetObjectEnabled("red", nil);
SetObjectEnabled("blue", nil);
SetObjectEnabled("green", nil);
SetObjectiveState('HeroSurvive', OBJECTIVE_ACTIVE);
SetObjectiveState('PlayerWin', OBJECTIVE_ACTIVE);
Trigger( PLAYER_REMOVE_HERO_TRIGGER, 1, "LostHero" );
Trigger( OBJECT_TOUCH_TRIGGER , "red" , "redkey" );
Trigger( OBJECT_TOUCH_TRIGGER , "blue" , "bluekey" );
Trigger( OBJECT_TOUCH_TRIGGER , "green" , "greenkey" );
DifficultyDependency();