army_array = {};
creatures = {};

hard = 0;
k = 1;
reinforcement = 0;

count = 0;
jin_count = 0;

for i=1,12 do
	if GetGameVar("C3M5_creatures"..i) ~= "0" then
		if mod(i,2) == 1 then
			army_array[k] = {};
			army_array[k][1] = i+1;
			army_array[k][2] = 0 + GetGameVar("C3M5_creatures"..i);
			else 
			army_array[k][2] = army_array[k][2] + GetGameVar("C3M5_creatures"..i);
			k = k + 1;
		end;
	end;
end;

function DefenderHeroMove(hero)
	count = count + 1;
	if reinforcement == 1 then
		reinforcement = 2;
		count = 0;
	end;
	if GetUnitManaPoints(hero) > 30 and hard == 1 then
		creatures = GetCreatures(DEFENDER);
		if count == 0 then
			UnitCastGlobalSpell(hero, 54);
			setATB(hero, 0.66);
			return not nil
		end;
		if count == 1 then
			for i, creature in creatures do
				startThread(cast, hero, 52, creature);
			end;
			return not nil
		end;
		if count == 2 then
			UnitCastGlobalSpell(hero, 221);
			return not nil
		end;
		if count == 3 then
			UnitCastGlobalSpell(hero, 219);
			return not nil
		end;
		if count == 4 then
			UnitCastGlobalSpell(hero, 220);
			return not nil
		end;
		if count == 5 then
			UnitCastGlobalSpell(hero, 218);
			return not nil
		end;
		if count == 6 then
			for i, creature in creatures do
				startThread(cast, hero, 52, creature);
			end;
			return not nil
		end;
		if count == 8 then
			UnitCastGlobalSpell(hero, 216);
			return not nil
		end;
		if count == 12 then
			for i, creature in creatures do
				startThread(cast, hero, 52, creature);
			end;
			return not nil
		end;
		if count == 16 then
			for i, creature in creatures do
				startThread(cast, hero, 52, creature);
			end;
			return not nil
		end;
		return nil
	end;
	return nil
end;

function DefenderCreatureMove(unit)
	if GetCreatureType(unit) == 66 then
		jin_count = jin_count + 1;
		if jin_count == 1 and hard == 1 then
			local creatures = GetCreatures(DEFENDER);
			for i, creature in creatures do
				if GetCreatureType(creature) == 64 or GetCreatureType(creature) == 68 or GetCreatureType(creature) == 70 then
					startThread(cast, unit, 175, creature);
				end;
			end;
			return not nil
		end;
	end;
end;

function cast(hero, spell, target)
	UnitCastAimedSpell(hero, spell, target);
	print("casting spell "..spell);
end;

function GodricsReinforcements()
	print("Thread GodricsReinforcements has been started...");	
	repeat
		sleep(10);
		len = length(GetDefenderCreatures());
	until len == 0;
	print("Godric does not have any creatures.");
	if army_array == {} then
		print("Godric does not have any reinforcements. You won!");
		Finish(ATTACKER);
		return
	else
		print("Godric has reinforcements!");
		reinforcement = 1;
		j = 0;
		for i, mon in army_array do
			if mon[2] ~= 0 and j<=5 then
				if k < 5 then
					AddCreature(DEFENDER, mon[1],mon[2],15,3*j);
					else
					AddCreature(DEFENDER, mon[1],mon[2],15,1+2*j);
				end;
				j = j + 1;
				if mon[1] == 12 then
					AddCreature(DEFENDER, mon[1]-1,mon[2],13,8);
				end;
			end;
		end;
	end;
end;

function DefenderCreatureDeath()
	local defenders = GetDefenderCreatures();
	if (length(defenders) == 0) and reinforcement > 0 then Finish(ATTACKER) end;
end;

function AttackerCreatureDeath()
	local attackers = GetAttackerCreatures();
	if (length(attackers) == 0) then Finish(DEFENDER) end;
end;

if GetGameVar("C3M5_Difficulty") == "normal" then
	print("Godric has reinforcemets for NORMAL difficulty level");
	else
	if GetGameVar("C3M5_Difficulty") == "hard" then
		print("Godric has reinforcemets for HARD difficulty level");
		for i, mon in army_array do
			mon[2] = 1.5 * mon[2] - 0.5 * mod(mon[2],2);
		end;
		else
		if GetGameVar("C3M5_Difficulty") == "heroic" then
			print("Godric has reinforcemets for heroic difficulty level");
			hard = 1;
			for i, mon in army_array do
				mon[2] = 2 * mon[2];
			end;
		end;
	end;	
end;

EnableAutoFinish(nil);
SetUnitManaPoints(GetDefenderHero(), GetUnitMaxManaPoints(GetDefenderHero()));

while combatStarted() == nil do
	sleep(1);
end;
sleep(1);
print("BOSS FIGHT!");
startThread(GodricsReinforcements);
