count = 0;
bossfight = 0;
rnd = 0;
death = 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;
		EnableAutoFinish(nil);
		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;
	if count == 1 then
		SummonCreature(DEFENDER, CREATURE_SKELETON_ARCHER, 40+7*diff, 7, 4);
		SummonCreature(DEFENDER, 32, 52+5*diff, 6, 3);
		print("first summon");
	end
	if count == 3 then
		SummonCreature(DEFENDER, 29, 30+6*diff, 7, 2);
		SummonCreature(DEFENDER, 33, 15+4*diff, 8, 4);
		print("second summon");
	end
	if count == 4 then
		SummonCreature(DEFENDER, CREATURE_SKELETON_ARCHER, 35+8*diff, 7, 7);
		SummonCreature(DEFENDER, 31, 16+5*diff, 9, 3);
		print("second summon");
	end
	if count == 5 then
		SummonCreature(DEFENDER, 29, 38+8*diff, 2, 3);
		SummonCreature(DEFENDER, 32, 12+4*diff, 7, 5);
		print("second summon");
	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;
	local attackers = GetAttackerCreatures();
	if (length(attackers) == 0) then Finish(DEFENDER) end;
end;

function DefenderCreatureDeath()
	death = death + 1
	rnd = rnd + 1;
	if death == 1 then
		sleep(10);
		SummonCreature(DEFENDER, 35, 23+4*diff, 3, 5);
	end
	if death == 3 then
		sleep(10);
		SummonCreature(DEFENDER, 41, 3+diff, 4, 6);
	end
	if death == 5 then
		sleep(10);
		SummonCreature(DEFENDER, 37, 8+3*diff, 9, 6);
	end
	if death == 6 then
		sleep(10);
		SummonCreature(DEFENDER, 39, 6+2*diff, 6, 6);
	end
	local defenders = GetDefenderCreatures();
	if (length(defenders) == 0) then Finish(ATTACKER) end;
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;