print('mummy combat script started...');

arix = 0;
arixBattle = 0;
gilbert = 0;
attackerCount = 0;
attackerDeathCount = 7;
attackerDeathName = nil;
attackerDeathEnable = 0;
defenderCount = 0;
defenderDeathCount = 7;
defenderDeathName = nil;
defenderDeathEnable = 0;

function Start()
	sleep(5);
	startThread(checkWinAttacker);
	startThread(checkWinDefender);
	if arixBattle == 1 then
		startThread(callPhantoms);
	end;
	sleep(10);
	EnableAutoFinish(nil);
end;

function callPhantoms()
	local manaSave = GetUnitManaPoints(GetDefenderHero());
	combatSetPause(1);
	SetUnitManaPoints(GetDefenderHero(), 500);
	sleep(1);
	startThread(unitCastSpellPhantom);
	sleep(5);
	SetUnitManaPoints(GetDefenderHero(), manaSave);
	combatSetPause(nil);
end;

function unitCastSpellPhantom()
	local creaturesList = GetDefenderCreatures();
	for i, target in creaturesList do
		startThread(unitCastSpellPhantomToTarget, target);
	end;
end;

function unitCastSpellPhantomToTarget(target)
	UnitCastAimedSpell(GetDefenderHero(), SPELL_PHANTOM, target);
end;

function checkWinAttacker()
	repeat sleep(1); until length(GetCreatures(DEFENDER)) == 0;
	
	if gilbert == ATTACKER and attackerDeathCount == 0 then
		AddCreature(ATTACKER, 116, 1);
	end;
	sleep(30);
	Finish(ATTACKER);
end;

function checkWinDefender()
	repeat sleep(1); until length(GetCreatures(ATTACKER)) == 0;
	
	if gilbert == DEFENDER and attackerDeathCount == 0 then
		AddCreature(DEFENDER, 116, 1);
	end;
	sleep(30);
	Finish(DEFENDER);
end;

function checkGilbert()
	if GetHeroName(GetAttackerHero()) == 'Sarge' then
		gilbert = ATTACKER;
		if GetHeroName(GetDefenderHero()) == 'Nur' then
			arix = DEFENDER;
			arixBattle = 1;
			print('Arix is defender');
		end;
		print('Gilbert is attacker');
	else
		gilbert = DEFENDER;
		print('Gilbert is defender');
	end;
end;

function AttackerCreatureDeath(creatureName)
	if gilbert == ATTACKER then
		if GetCreatureType(creatureName) ~= 116 then
			attackerCount = GetCreatureNumber(creatureName);
			attackerDeathCount = attackerDeathCount-1;
			attackerDeathName = creatureName;
			attackerDeathEnable = 1;
		end;
	end;
end;

function DefenderCreatureDeath(creatureName)
	if gilbert == DEFENDER then
		if GetCreatureType(creatureName) ~= 116 then
			defenderCount = GetCreatureNumber(creatureName);
			defenderDeathCount = defenderDeathCount-1;
			defenderDeathName = creatureName;
			defenderDeathEnable = 1;
		end;
	end;
end;

function AttackerCreatureMove(creatures)
	if attackerDeathEnable == 1 or defenderDeathEnable == 1 then
		startThread(summonDeathCreatures);
	end;
end;

function DefenderCreatureMove(creatures)
	if attackerDeathEnable == 1 or defenderDeathEnable == 1 then
		startThread(summonDeathCreatures);
	end;
end;

function summonDeathCreatures()
	if attackerDeathEnable == 1 then
		print('spawn mummy count = '..attackerCount..', and stack count left = '..attackerDeathCount);
		if attackerCount > 0 then
			local x, y = GetUnitPosition(attackerDeathName);
			if IsCombatUnit(attackerDeathName) == not nil then
				removeUnit(attackerDeathName);
			end;
			sleep(5);
			SummonCreature(ATTACKER, 116, attackerCount, x, y);
		end;
		attackerDeathEnable = 0;
		attackerDeathName = nil;
		attackerCount = 0;
	elseif defenderDeathEnable == 1 then
		print('spawn mummy count = '..defenderCount..', and stack count left = '..defenderDeathCount);
		if defenderCount > 0 then
			local x, y = GetUnitPosition(defenderDeathName);
			removeUnit(defenderDeathName);
			sleep(5);
			SummonCreature(ATTACKER, 116, defenderCount, x, y);
		end;
		defenderDeathEnable = 0;
		defenderDeathName = nil;
		defenderCount = 0;
	end;
end;

startThread(checkGilbert);