bossfight = 0;
cur_spell = 0;
rnd = 0;
count_local = 0;
charge = 0;
charge_count = 0;
wasp = 0;

result = nil;

spells = {};

global_spells = {43, 54, 59, 210, 212, 213, 215, 216, 218, 219, 220, 220, 221, 235};
enemy_spell = {18, 19, 20};
ally_spell = {34, 40};
special_spells = {211, 214};
cleric_spells = {216, 218, 221};

enemy_id = {14, 13, 11, 50, 10, 52, 54, 9, 8, 7, 6, 5, 4, 3, 2};
human_id = {14, 13, 12, 11, 10, 4, 8, 6, 9, 7, 3, 2, 5, 1};

dif = GetGameVar("C6M4_dif") + 0;

function Start()
	units = GetCreatures(ATTACKER);
	for i, unit in units do
		rnd = rnd + GetCreatureNumber(unit);
	end;
	units = GetCreatures(DEFENDER);
	for i, unit in units do
		rnd = rnd + GetCreatureNumber(unit);
	end;
	if dif > 1 and bossfight > 0 then
		print("BOSSFIGHT!");
		EnableDynamicBattleMode(1);
		print("dynamic battle enable!");
		bossfight = 1;
		local k = 0;
		for i=18,235 do
			if GetGameVar("C6M4_Isabell_spell_"..i) == "1" then
				rnd = rnd + 1;
				k = k + 1;
				spells[k] = i;
			end;
		end;
		if GetGameVar("C6M4_Isabell_pray") == "1" then
			spells[k+1] = 54;
		end;
		if GetGameVar("C6M4_Isabell_charge") == "1" then
			charge = 1;
		end;
		if GetGameVar("C6M4_Isabell_encourage") == "1" then
			encourage = 1;
		end;
		if GetGameVar("C6M4_Isabell_wasp") == "1" then
			wasp = 1;
		end;
	end;
end;

function DefenderHeroMove(hero)
	rnd = rnd + 1;
	if bossfight == 1 then
		result = isabell_turn(hero);
	end;
	return result
end

function isabell_turn(hero)
	units = GetCreatures(ATTACKER);
	if GetUnitManaPoints(hero) < 20 and randm(2) == 0 or randm(6-dif) == 0 then
		if encourage == 1 then
			units = GetCreatures(DEFENDER);
			for i, unit in units do
				startThread(cast, hero, 52, unit);
			end;
			return not nil
		end;
	end;
	if charge == 1 and charge_count == 0 and randm(3) == 0 then
		victim = GetTarget(DEFENDER, human_id);
		if victim ~= nil then
			print("Isabell will defend "..victim);
			startThread(cast, hero, 53, victim);
			charge_count = 1;
			return not nil
		end;
	end;
	if wasp == 1 and randm(4) == 0 and GetUnitManaPoints(hero) > 35 then
		for i, unit in units do
			startThread(cast, hero, 39, unit);
		end;
		return not nil
	end;
	if randm(10) > 0 and spells ~= {} and GetUnitManaPoints(hero) > 25 then
		local len = length(spells);
		if len > 0 then
			if len > 1 then
				cur_spell = spells[1+randm(len)];
				else cur_spell = spells[1];
			end;
			print("Isabell want to cast spell "..cur_spell);
			spells = clear_array(spells, cur_spell);
			if IsInArray(cur_spell, global_spells) then
				UnitCastGlobalSpell(hero, cur_spell);
				return not nil
				else if IsInArray(cur_spell, enemy_spell) then
					for i, unit in units do
						if IsUnitInArray(unit, enemy_id) then 
							count_local = count_local + 1;
							startThread(cast, hero, cur_spell, unit);
						end;
						if count_local == 1 and cur_spell == 20 then
							return not nil
						end;
						if count_local > 1 then
							return not nil
						end;
					end;
					else if IsInArray(cur_spell, ally_spell) then
						victim = GetTarget(DEFENDER, human_id);
						if victim ~= nil then
							startThread(cast, hero, cur_spell, victim);
							return not nil
							else return nil
						end;
						else if IsInArray(cur_spell, special_spells) then
							if cur_spell == 211 then
								cur_spell = 13;
							end;
							if cur_spell == 214 then
								cur_spell = 14;
							end;
							for i, unit in units do
								if IsUnitInArray(unit, enemy_id) then 
									count_local = count_local + 1;
									startThread(cast, hero, cur_spell, unit);
								end;
								if count_local > 3 then
									return not nil
								end;
							end;
						end;
					end;
				end;
			end;
		end;
		else print("AI turn");
		return nil
	end;
	return nil
end;

function DefenderCreatureMove(unit)
	rnd = rnd + 1;
	if GetCreatureType(unit) == 10 and bossfight == 1 then
		local len = length(cleric_spells);
		if len > 0 then
			if len > 1 then
				cur_spell = cleric_spells[1+randm(len)];
				else cur_spell = cleric_spells[1];
			end;
			print("Clerics want to cast spell "..cur_spell);
			cleric_spells = clear_array(cleric_spells, cur_spell);
			spells = clear_array(spells, cur_spell);
			UnitCastGlobalSpell(unit, cur_spell);
			SetUnitManaPoints(unit, GetUnitMaxManaPoints(unit));
			return not nil
		end;
	end;
	return nil
end;

function AttackerHeroMove(hero)
	rnd = rnd + 1;
end;

function AttackerCreatureMove(creature)
	rnd = rnd + 1;
end;

function AttackerUnitDeath(creature)
	rnd = rnd + 1;
end;

function DefenderCreatureDeath()
	rnd = rnd + 1;
end

function clear_array(array, elem)
	local a = {};
	local j = 1;
	for i, unit in array do
		if unit ~= elem then
			a[j] = unit;
			j = j + 1;
		end;
	end;
	return a
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;

function IsUnitInArray(unit, array)
	for i, elem in array do
		if GetCreatureType(unit) == elem then
			return not nil;
		end;
	end;
	return nil;
end;

function IsInArray(unit, array)
	for i, elem in array do
		if unit == elem then
			return not nil;
		end;
	end;
	return nil;
end;

function randm(num)
	local rand = rnd;
	if rand >= num then
		rand = mod(rand, num);
	end;
	return rand;
end;

if GetDefenderHero() ~= nil then
	if GetHeroName(GetDefenderHero()) == "Isabell" then
		print("Battle against Isabell!");
		bossfight = 0.5;
	end;
end;