count = 0;
bossfight = 0;
rnd = 0;
count_local = 0;
link = 0;
link_count = 0;

result = nil;

spells = {};

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

enemy_id = {14, 13, 11, 50, 10, 52, 54, 9, 8, 7, 6, 5, 4, 3, 2};
necro_id = {42, 40, 90, 89, 41, 39, 38, 36, 37, 35, 34, 30};

diff = GetGameVar("C6M3_date") + 0;

function Start()
	print("dinamic battle enable!");
	EnableDynamicBattleMode(1);
	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 GetGameVar("C6M3_diff") == "1" then
		print("script battle!");
		bossfight = 1;
		local k = 0;
		for i=18,235 do
			if GetGameVar("C6M3_markel_spell_"..i) == "1" then
				rnd = rnd + 1;
				k = k + 1;
				spells[k] = i;
			end;
		end;
		if GetGameVar("C6M3_markel_scream") == "1" then
			spells[k+1] = 59;
		end;
		if GetGameVar("C6M3_markel_link") == "1" then
			link = 1;
		end;
	end;
end;

function DefenderHeroMove(hero)
	count = count + 1;
	if bossfight == 1 then
		result = markel_turn(hero);
	end;
	return result
end

function markel_turn(hero)
	units = GetCreatures(ATTACKER);
	if GetUnitManaPoints(hero) < 50 and link_count == 0 and link == 1 then
		victim = GetTarget(ATTACKER, enemy_id);
		if victim ~= nil then
			print("Markel wants to gain mana from "..victim);
			startThread(cast, hero, 58, victim);
			link_count = 1;
			return not nil
			else return nil
		end;
	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("Markel 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, necro_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(creature)
	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;