army_array = {};
creatures = {};

hard = 0;
bossfight = 0;
reinforcement = 1;

count = 0;
jin_count = 0;

function DefenderHeroMove(hero)
	count = count + 1;
	if reinforcement == 1 and bossfight == 1 then
		reinforcement = 2;
		count = 0;
	end;
	if GetUnitManaPoints(hero) > 30 and hard == 1 and bossfight == 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()
	while combatStarted() == nil do
		sleep(1);
	end;
	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 GetAttackerHero() ~= nil and GetDefenderHero() ~= nil then
	if GetHeroName(GetDefenderHero()) == "Godric" then
		print("BOSS FIGHT!");
		bossfight = 1;
		reinforcement = 0;
		k = 1;
		number = 0;
		EnableAutoFinish(nil);
		for i=1,6 do
			number = 0 + GetGameVar("C3M5_creatures"..(2*i)) + GetGameVar("C3M5_creatures"..(2*i-1));
			if number > 0 then
				army_array[k] = {};
				army_array[k][1] = 2*i;
				army_array[k][2] = number;
				k = k + 1;
			end;
		end;
		print(army_array);
		sleep(1);
		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
					army_array[i][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
						army_array[i][2] = 2 * mon[2];
					end;
				end;
			end;	
		end;
		SetUnitManaPoints(GetDefenderHero(), GetUnitMaxManaPoints(GetDefenderHero()));
		sleep(1);
		startThread(GodricsReinforcements);
	end;
end;