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

function Start()
	sleep(5);
	startThread(checkWinAttacker);
	startThread(checkWinDefender);
	startThread(callPhantoms);
	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(GetDefenderCreatures()) == 0;
	
	playAnimation(GetAttackerHero(), "stir02", ONESHOT_STILL);
	sleep(30);
	Finish(ATTACKER);
end;

function checkWinDefender()
	repeat sleep(1); until length(GetAttackerCreatures()) == 0;
	
	sleep(30);
	Finish(DEFENDER);
end;