print("BOSSFIGHT!");

count = 0;
targets_id = {84, 83, 82, 81, 80, 79, 74, 72};

function Start()
	EnableDynamicBattleMode(1);
end;

function DefenderHeroMove(hero)
	if AI_side == DEFENDER then
		if GetUnitManaPoints(hero) > 50 then
			count = count + 1;
			if count == 1 then
				UnitCastGlobalSpell(hero, 212);
				return not nil
			end;
			if count == 2 then
				UnitCastGlobalSpell(hero, 213);
				return not nil
			end;
			if count == 3 then
				local victim = GetTarget(human_side);
				if victim ~= nil then
					print("Demon lord hates "..victim);
					startThread(cast, hero, 56, victim);
					return not nil
				end;
			end;
		end;
	end;
	return nil;
end;

function AttackerHeroMove(hero)
	if AI_side == ATTACKER then
		if GetUnitManaPoints(hero) > 50 then
			count = count + 1;
			if count == 1 then
				UnitCastGlobalSpell(hero, 212);
				return not nil
			end;
			if count == 2 then
				UnitCastGlobalSpell(hero, 213);
				return not nil
			end;
			if count == 3 then
				local victim = GetTarget(human_side);
				if victim ~= nil then
					print("Demon lord hates "..victim);
					startThread(cast, hero, 56, victim);
					return not nil
				end;
			end;
		end;
	end;
	return nil;
end;

function cast(hero, spell, target)
	UnitCastAimedSpell(hero, spell, target);
	print("casting spell "..spell);
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;

if GetHeroName(GetAttackerHero()) == "Raelag" or GetHeroName(GetAttackerHero()) == "Kelodin" then
	print("Defend yourself, demon!");
	human_side = ATTACKER;
	AI_side = DEFENDER;
	else
	print("Demon is attacking");
	human_side = DEFENDER;
	AI_side = ATTACKER;
end;
