boss_fight = 0;
count = 0;
death = 0;
data_l = GetGameVar("C2M4_date");
diff = GetGameVar("C2M4_diff");
data = 0;
mana = 0;

function Start()
	if boss_fight == 1 then
		setATB(GetDefenderHero(), 0.66);
	end;
end;

function DefenderHeroMove(hero)
	if boss_fight == 1 and GetUnitManaPoints(GetDefenderHero()) > 50 then
		count = count + 1;
		creatures = GetCreatures(DEFENDER);
		mana = GetUnitManaPoints(GetDefenderHero());
		if count == 1 then
			UnitCastGlobalSpell(GetDefenderHero(), 221);
			return not nil
		end;
		if count == 2 then
			SetUnitManaPoints(GetDefenderHero(), 500);
			for i, creature in creatures do
				startThread(cast, GetDefenderHero(), 34, creature);
			end;
			sleep(1);
			SetUnitManaPoints(GetDefenderHero(), mana);
			return not nil
		end;
		if count == 3 then
			UnitCastGlobalSpell(GetDefenderHero(), 219);
			return not nil
		end;
		if count == 4 then
			UnitCastGlobalSpell(GetDefenderHero(), 220);
			return not nil
		end;
		if count == 5 then
			UnitCastGlobalSpell(GetDefenderHero(), 218);
			return not nil
		end;
		if count == 6 then
			UnitCastGlobalSpell(GetDefenderHero(), 216);
			return not nil
		end;
		return nil
	end;
	return nil
end;

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

function DefenderCreatureDeath()
	if boss_fight == 1 then
		death = death + 1;
		data = data_l + 0;
		creatures = GetCreatures(DEFENDER);
		local new_creatures = {};
		SetUnitManaPoints(GetDefenderHero(), 5 + GetUnitManaPoints(GetDefenderHero()));
		if death == 1 then
			sleep(3);
			AddCreature(DEFENDER, 44, 50 + 10 * data, 12, 2);
			AddCreature(DEFENDER, 44, 100 + 15 * data, 12, 8);
			AddCreature(DEFENDER, 44, 50 + 10 * data, 12, 13);
			sleep(1);
		end
		if death == 4 then
			sleep(3);
			AddCreature(DEFENDER, 54, 7 + 2 * data, 12, 11);
			AddCreature(DEFENDER, 54, 7 + 2 * data, 12, 4);
			sleep(1);
		end;
		if death == 7 then
			sleep(3);
			AddCreature(DEFENDER, 85, 10 + 5 * data, 14, 14);
			AddCreature(DEFENDER, 86, 10 + 5 * data, 14, 1);
			AddCreature(DEFENDER, 87, 9 + 4 * data, 12, 11);
			AddCreature(DEFENDER, 88, 10 + 6 * data, 12, 3);
			sleep(1);
		end;
		if death == 10 then
			sleep(1);
			AddCreature(DEFENDER, 91, 2 + data, 11, 8);
			AddCreature(DEFENDER, 52, 10 + 2 * data, 12, 2);
			AddCreature(DEFENDER, 52, 10 + 2 * data, 13, 13);
			sleep(1);
		end;
		local defenders = GetDefenderCreatures();
		if (length(defenders) == 0) then Finish(ATTACKER) end;
	end;
end;

function AttackerCreatureDeath()
	local attackers = GetAttackerCreatures();
	if (length(attackers) == 0) then Finish(DEFENDER) end;
	if boss_fight == 1 then
		SetUnitManaPoints(GetDefenderHero(), 10 + GetUnitManaPoints(GetDefenderHero()));
	end;
end;

if GetDefenderHero() ~= nil then
	if GetHeroName(GetDefenderHero()) == "Elleshar" and diff == "1" then
		print("Boss fight!");
		boss_fight = 1;
		EnableAutoFinish(nil);
	end;
end;