print("script battle!");

bossfight = 0;
count = 0;
count_local = 0;

targets_id = {48, 50, 49, 47, 46, 44, 45, 54, 52, 53, 51};

blind_id = {47, 48, 49, 50};

victim = "";

function Start()
	print("dinamic battle enable!");
	EnableDynamicBattleMode(1);
	if bossfight == 1 then
		SetUnitManaPoints(Biara, GetUnitMaxManaPoints(Biara));
	end;
end;

function DefenderHeroMove(hero)
	if AI_side == DEFENDER and bossfight == 1 then
		if GetUnitManaPoints(hero) > 50 then
			count = count + 1;
			count_local = 0;
			local mana = GetUnitManaPoints(hero);
			elfs = GetCreatures(human_side);
			if count == 1 or count == 5 then
				for i, unit in elfs do
					if IsUnitInArray(unit, blind_id) then 
						count_local = count_local + 1;
						startThread(cast, hero, 19, unit);
					end;
				end;
				SetUnitManaPoints(hero, mana);
				if count_local > 0 then
					return not nil
					else return nil;
				end;
			end;
			if count == 2 then
				victim = GetTarget(human_side);
				if victim ~= nil then
					print("Demon lord hates "..victim);
					startThread(cast, hero, 56, victim);
					return not nil
					else return nil
				end;
			end;
			if count == 3 then
				for i, unit in elfs do
					if unit ~= victim then
						startThread(cast, hero, 4, unit);
						i = i + 1;
					end;
				end;
				return not nil
			end;
			if count == 4 then
				UnitCastGlobalSpell(hero, 43);
				return not nil
			end;
			if count == 6 then
				for i, unit in elfs do
					if IsUnitInArray(unit, blind_id) then 
						startThread(cast, hero, 7, unit);
						return not nil
					end;
				end;
				startThread(cast, hero, 7, elfs[0]);
				return not nil
			end;
		end;
	end;
	return nil;
end;

function AttackerHeroMove(hero)
	if AI_side == ATTACKER and bossfight == 1 then
		if GetUnitManaPoints(hero) > 50 then
			count = count + 1;
			count_local = 0;
			local mana = GetUnitManaPoints(hero);
			elfs = GetCreatures(human_side);
			if count == 1 or count == 5 then
				for i, unit in elfs do
					if IsUnitInArray(unit, blind_id) then 
						count_local = count_local + 1;
						startThread(cast, hero, 19, unit);
					end;
				end;
				SetUnitManaPoints(hero, mana);
				if count_local > 0 then
					return not nil
					else return nil;
				end;
			end;
			if count == 2 then
				victim = GetTarget(human_side);
				if victim ~= nil then
					print("Demon lord hates "..victim);
					startThread(cast, hero, 56, victim);
					return not nil
					else return nil
				end;
			end;
			if count == 3 then
				for i, unit in elfs do
					if unit ~= victim then
						startThread(cast, hero, 4, unit);
						i = i + 1;
					end;
				end;
				return not nil
			end;
			if count == 4 then
				UnitCastGlobalSpell(hero, 43);
				return not nil
			end;
			if count == 6 or count == 8 then
				for i, unit in elfs do
					if IsUnitInArray(unit, blind_id) then 
						startThread(cast, hero, 7, unit);
						return not nil
					end;
				end;
				startThread(cast, hero, 7, elfs[0]);
				return not nil
			end;
		end;
	end;
	return nil;
end;

function IsUnitInArray(unit, array)
	for i, elem in array do
		if GetCreatureType(unit) == elem then
			return not nil;
		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 GetAttackerHero() ~= nil and GetDefenderHero() ~= nil then
	if GetHeroName(GetAttackerHero()) == "Biara" then
		print("Demon is attacking");
		human_side = DEFENDER;
		AI_side = ATTACKER;
		bossfight = 1;
		Biara = GetAttackerHero();
		else if GetHeroName(GetDefenderHero()) == "Biara" then
			print("Defend yourself, demon!");
			human_side = ATTACKER;
			AI_side = DEFENDER;
			bossfight = 1;
			Biara = GetDefenderHero();
		end;
	end;
end;