print("BOSSFIGHT!");

targets_id = {82, 81, 80, 79, 78, 76, 74};

count = 0;
balor_count = 0;
result = nil;

victim = "";

if GetHeroName(GetAttackerHero()) == "Veyer" or GetHeroName(GetDefenderHero()) == "Veyer" then
	veyer = not nil;
	EnableDynamicBattleMode(1);
	print("fight with Veyer");
	else veyer  = nil;
end;

if GetHeroName(GetAttackerHero()) == "Efion" or GetHeroName(GetDefenderHero()) == "Efion" then
	efion = not nil;
	print("fight with Efion");
	else efion  = nil;
end;

if IsHuman(ATTACKER) then
	SetUnitManaPoints(GetDefenderHero(), GetUnitMaxManaPoints(GetDefenderHero()));
	human = ATTACKER
	AI_side = DEFENDER
	else SetUnitManaPoints(GetAttackerHero(), GetUnitMaxManaPoints(GetAttackerHero()));
	human = DEFENDER;
	AI_side = ATTACKER;
end;

function Start()
	if veyer then
		EnableDynamicBattleMode(1);
	end;
end;

function DefenderHeroMove(hero)
	result = nil;
	if IsHuman(ATTACKER) then
		count = count + 1;
		if veyer then
			result = veyer_move(hero);
		end;
		if efion then
			result = efion_move(hero);
		end;
		return result;
	end;
	return nil;
end;

function AttackerHeroMove(hero)
	result = nil;
	if IsHuman(DEFENDER) then
		count = count + 1;
		if veyer then
			result = veyer_move(hero);
		end;
		if efion then
			result = efion_move(hero);
		end;
		return result;
	end;
	return nil;
end;

function veyer_move(hero)
	if GetUnitManaPoints(hero) > 50 then
		elfs = GetCreatures(human);
		if count == 1 then
			for i, unit in elfs do
				startThread(cast, hero, 14, unit);
			end;
			return not nil
		end;
		if count == 2 then
			victim = GetTarget(human);
			if victim ~= nil then
				print("Demon lord hates "..victim);
				startThread(cast, hero, 56, victim);
				return not nil
			end;
		end;
		if count == 3 and length(elfs) > 2 then
			for i, unit in elfs do
				if unit ~= victim then
					startThread(cast, hero, 18, unit);
					i = i + 1;
				end;
			end;
			return not nil
		end;
		if count == 4 then
			UnitCastGlobalSpell(hero, 213);
			return not nil
		end;
		if count == 5 and length(elfs) > 2 then
			for i, unit in elfs do
				startThread(firecast, hero, unit);
			end;
			setATB(hero, 0.25);
			return not nil
		end;
		if count == 6 then
			UnitCastGlobalSpell(hero, 215);
			return not nil
		end;
		if count == 7 then
			for i, unit in elfs do
				startThread(cast, hero, 14, unit);
			end;
			return not nil
		end;
		if count > 9  and count < 13 then
			for i, unit in elfs do
				startThread(cast, hero, 13, unit);
			end;
			setATB(hero, 0.25);
			return not nil
		end;
	end;
	return nil;
end;

function efion_move(hero)
	if GetUnitManaPoints(hero) > 20 then
		if count == 1 then
			UnitCastGlobalSpell(hero, 212);
			return not nil
		end;
		if count == 2 then
			victim = GetTarget(human);
			if victim ~= nil then
				print("Demon lord hates "..victim);
				startThread(cast, hero, 56, victim);
				return not nil
			end;
		end;
		if count == 3 then
			UnitCastGlobalSpell(hero, 213);
			return not nil
		end;
	end;
	return nil;
end;

function DefenderCreatureMove(unit)
	if GetCreatureType(unit) == 26 and veyer then
		balor_count = balor_count + 1;
		elfs = GetCreatures(human);
		if balor_count == 1 and length(elfs) > 2 then
			SetUnitManaPoints(unit, 100);
			for i, cr in elfs do
				startThread(cast, unit, 13, cr);
			end;
			return not nil
		end;
	end;
	return nil
end;

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

function firecast(hero, unit)
	local x,y = GetUnitPosition(unit);
	UnitCastAreaSpell(hero, 5, x, y);
end;

function GetTarget(side)
	local units = GetCreatures(side);
	for i, id in targets_id do
		for j, unit in units do
			if GetCreatureType(unit) == id then
				return unit;
			end;
		end;
	end;
	return nil;
end;