print("Script battle!");

count = 0;
rnd = 0;

elfs = {};
victim = nil;
cur_spell = 0;

gles_cast = {210, 215, 59};
gless_strike = 0;

nemor_cast = {43, 59, 212, 213, 215};
nemor_strike = 0;

tent = nil;
tent_count = 0;
last_aid_id = {48, 50, 49, 47, 56, 55, 44, 52, 51, 45, 54, 53};

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

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

if IsHuman(ATTACKER) then
	human = ATTACKER
	AI_side = DEFENDER
	SetUnitManaPoints(GetDefenderHero(), GetUnitMaxManaPoints(GetDefenderHero()));
	else
	human = DEFENDER;
	AI_side = ATTACKER;
	SetUnitManaPoints(GetAttackerHero(), GetUnitMaxManaPoints(GetAttackerHero()));
end;

function Start()
	elfs = GetCreatures(AI_side);
	for i, unit in elfs do
		rnd = rnd + GetCreatureNumber(unit);
	end;
	elfs = GetCreatures(human);
	for i, unit in elfs do
		rnd = rnd + GetCreatureNumber(unit);
	end;
	if GetDefenderBuilding(BUILDING_GATE) ~= nil then
		print("siege!");
		nemor_cast = {41, 43, 59, 212, 215};
	end;
	if gles then
		tent = GetWarMachine(AI_side, WAR_MACHINE_FIRST_AID_TENT);
		rnd = rnd + 1;
		print("Gles has evil first aid tent, prepare your anus!");
	end;
	print(rnd);
	if randm(4) == 1 then
		EnableDynamicBattleMode(1);
	end;
end;

function DefenderHeroMove(hero)
	rnd = rnd + 1;
	result = nil;
	if IsHuman(ATTACKER) then
		count = count + 1;
		if gles then
			result = gles_move(hero);
		end;
		if nemor then
			result = nemor_move(hero);
		end;
		return result;
	end;
	return nil;
end;

function AttackerHeroMove(hero)
	rnd = rnd + 1;
	result = nil;
	if IsHuman(DEFENDER) then
		count = count + 1;
		if gles then
			result = gles_move(hero);
		end;
		if nemor then
			result = nemor_move(hero);
		end;
		return result;
	end;
	return nil;
end;

function AttackerWarMachineMove(unit)
	rnd = rnd + 1;
	if unit == tent and tent_count < 3 then
		tent_count = tent_count + 1;
		elfs = GetCreatures(human);
		victim = elfs[randm(length(elfs))];
		if victim ~= nil then
			local x,y = GetUnitPosition(victim);
			commandDoSpecial(unit, 189, x, y);
			return not nil
		end;
	end;
	return nil
end;

function DefenderWarMachineMove(unit)
	rnd = rnd + 1;
	if unit == tent and tent_count < 3 then
		tent_count = tent_count + 1;
		elfs = GetCreatures(human);
		victim = elfs[randm(length(elfs))];
		if victim ~= nil then
			local x,y = GetUnitPosition(victim);
			commandDoSpecial(unit, 189, x, y);
			return not nil
		end;
	end;
	return nil
end;

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

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

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

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

function gles_move(hero)
	if GetUnitManaPoints(hero) > 15 then
		if randm(4) == 2 and gless_strike == 0 then
			gless_strike = 1;
			UnitCastGlobalSpell(hero, 21);
			print("Gless use superstrike!");
			return not nil
			else if randm(3) > 0 then
				local len = length(gles_cast);
				if len > 1 then
					cur_spell = gles_cast[1+randm(len)];
					UnitCastGlobalSpell(hero, cur_spell);
					print("Gles cast spell "..cur_spell);
					else UnitCastGlobalSpell(hero, gles_cast[1]);
				end;
				gles_cast = clear_array(gles_cast, cur_spell);
				return not nil
			end;
		end;
	end;
	return nil;
end;

function nemor_move(hero)
	if GetUnitManaPoints(hero) > 15 then
		elfs = GetCreatures(human);
		if randm(4) == 1 and nemor_strike == 0 then
			nemor_strike = 1;
			for i, unit in elfs do
				if IsUnitInArray(unit, last_aid_id) then 
					startThread(cast, hero, 7, unit);
					print("Nemor use superstrike!");
					return not nil
				end;
			end;
			return nil
			else if randm(3) > 0 then
				local len = length(nemor_cast);
				if len > 1 then
					cur_spell = nemor_cast[1+randm(len)];
					print("Nemor cast spell "..cur_spell);
					UnitCastGlobalSpell(hero, cur_spell);
					else UnitCastGlobalSpell(hero, nemor_cast[1]);
				end;
				nemor_cast = clear_array(nemor_cast, cur_spell);
				return not nil
			end;
		end;
	end;
	return nil;
end;

function clear_array(array, elem)
	local a = {};
	local j = 1;
	for i, unit in array do
		if unit ~= elem then
			a[j] = cast;
			j = j + 1;
		end;
	end;
	return a
end;

function cast(hero, spell, target)
	UnitCastAimedSpell(hero, spell, target);
	print("casting spell "..spell);
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 randm(num)
	local rand = rnd;
	if rand >= num then
		rand = mod(rand, num);
	end;
	return rand;
end;