print("BOSSFIGHT!");

count = 0;
count_local = 0;

result = nil;
victim = "";
elfs = {};

targets_id = {48, 50, 49, 47, 46, 44, 45, 54, 52, 53, 51};
strike_id = {56, 55, 54, 53, 52, 50, 48, 49, 47, 51, 46, 44};
shield_id = {20, 26, 28, 27, 24};
phantom_id = {20, 26, 24, 28, 27};

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

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

if GetHeroName(GetAttackerHero()) == "Oddrema" or GetHeroName(GetDefenderHero()) == "Oddrema" then
	oddrema = not nil;
	print("fight with Oddrema");
	else oddrema  = 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 oddrema then
		if AI_side == ATTACKER then
			setATB(GetAttackerHero(), 0.65);
			else setATB(GetDefenderHero(), 0.65);
		end;
	end;
end;

function DefenderHeroMove(hero)
	result = nil;
	if IsHuman(ATTACKER) then
		count = count + 1;
		if jazaz then
			result = jazaz_move(hero);
		end;
		if oddrema then
			result = oddrema_move(hero);
		end;
		if biara then
			result = biara_move(hero);
		end;
		return result;
	end;
	return nil;
end;

function AttackerHeroMove(hero)
	result = nil;
	if IsHuman(DEFENDER) then
		count = count + 1;
		if jazaz then
			result = jazaz_move(hero);
		end;
		if oddrema then
			result = oddrema_move(hero);
		end;
		if biara then
			result = biara_move(hero);
		end;
		return result;
	end;
	return nil;
end;

function jazaz_move(hero)
	if GetUnitManaPoints(hero) > 30 then
		if count == 1 then
			UnitCastGlobalSpell(hero, 213);
			return not nil;
		end;
		if count == 2 then
			victim = GetTarget(AI_side, shield_id);
			if victim ~= nil then
				startThread(cast, hero, 34, victim);
				return not nil
				else return nil
			end;
			return not nil;
		end;
		if count == 3 then
			UnitCastGlobalSpell(hero, 235);
			return not nil;
		end;
		if count == 4 then
			victim = GetTarget(AI_side, phantom_id);
			if victim ~= nil then
				startThread(cast, hero, 40, victim);
				return not nil
				else return nil
			end;
			return not nil;
		end;
		if count == 5 then
			UnitCastGlobalSpell(hero, 43);
			return not nil;
		end;
	end;
	return nil;
end;

function oddrema_move(hero)
	if GetUnitManaPoints(hero) > 30 then
		if count == 1 then
			victim = GetTarget(human, targets_id);
			if victim ~= nil then
				startThread(cast, hero, 20, victim);
				return not nil
				else return nil
			end;
		end;
		if count == 2 then
			victim = GetTarget(human, strike_id);
			if victim ~= nil then
				startThread(cast, hero, 56, victim);
				return not nil
				else return nil
			end;
		end;
		if count == 3 then
			UnitCastGlobalSpell(hero, 212);
			return not nil;
		end;
		if count == 4 then
			UnitCastGlobalSpell(hero, 213);
			return not nil;
		end;
	end;
	return nil;
end;

function biara_move(hero)
	if GetUnitManaPoints(hero) > 50 then
		elfs = GetCreatures(human);
		if count == 1 then
			for i, unit in elfs do
				if IsUnitInArray(unit, strike_id) then 
					count_local = count_local + 1;
					startThread(cast, hero, 18, unit);
				end;
				if count_local > 1 then
					return not nil
				end;
			end;
			if count_local > 0 then
				return not nil
				else return nil;
			end;
		end;
		if count == 2 then
			victim = GetTarget(human, strike_id);
			if victim ~= nil then
				startThread(cast, hero, 56, victim);
				return not nil
				else return nil
			end;
		end;
		if count == 3 then
			UnitCastGlobalSpell(hero, 212);
			return not nil;
		end;
		if count == 3 then
			UnitCastGlobalSpell(hero, 221);
			return not nil;
		end;
		if count == 4 then
			UnitCastGlobalSpell(hero, 213);
			return not nil;
		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, array)
	local units = GetCreatures(side);
	for i, id in array do
		for j, unit in units do
			if GetCreatureType(unit) == id then
				return unit;
			end;
		end;
	end;
	return nil;
end;
