#include "scripts/common.txt"
#include "scripts/BP_common.txt"
#include "scripts/BP_MapCommon.txt"

//Tag IDs
//2018: Start Warp/Checkpoint
//1000: Bot 1
//1001: Bot 2
//1002: Bot 3
//1003: Bot 4
//1004:
//1005:
//1006: 

array<kActor@> bots;
int botsKilled;
int maxBots;
//------------------------------------------------------------------------------------------------------------------------
// Start Map
//------------------------------------------------------------------------------------------------------------------------
$script 0
{
	StartMap();
	botsKilled = 0;
	maxBots = 0;
	
	AddBot(1000, "BotSpiritGreen");
	AddBot(1001, "BotSpiritBlue");
	AddBot(1002, "BotSpiritRed");
	AddBot(1003, "BotSpiritBlack");
	maxBots = int(bots.length());

	Game.CallDelayedMapScript(1, instigator, 0); //Update Map
	if (maxBots == 0)
	{
		Game.CallDelayedMapScript(2, instigator, 0); //Start Victory
	}
	else
	{
		Game.PlaySound("sounds/shaders/spirit_intro.ksnd");
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Update Map
//------------------------------------------------------------------------------------------------------------------------
$script 1
{
	TickMap();
	PlayWavMusic("music_spirit.ksnd");
	$restart;
}
//------------------------------------------------------------------------------------------------------------------------
void AddBot(const int TID, const kStr &in fxEvent)
{
	kActor@ bot = World.GetActorByTID(TID);
	bot.RunFxEvent(fxEvent);
	if (bot.IsPersistentMarked() || (bot.TID() == 1002 && Game.GetDifficulty() < DIFFICULTY_HARD) || (bot.TID() == 1003 && Game.GetDifficulty() < DIFFICULTY_HARDCORE))
	{
		bot.Remove();
	}
	else
	{
		bots.insertLast(bot);
	}
}
//------------------------------------------------------------------------------------------------------------------------
// Start Victory
//------------------------------------------------------------------------------------------------------------------------
$script 2
{
	Game.PlaySound("sounds/shaders/event01.ksnd");
	delay(2.0f);
	SetProgress(PROGESS_BEATSPIRITS, true);
	PlayLoop.StartWarp(instigator, 1000, 125);
}
//------------------------------------------------------------------------------------------------------------------------
// Enemy Killed
//------------------------------------------------------------------------------------------------------------------------
$script 9999
{	
	//if killed spirit bot
	if (instigator.Type() == 6093)
	{
		int botIndex = bots.findByRef(instigator);
		if (botIndex >= 0)
		{
			botsKilled++;
			bots.removeAt(botIndex);
			if (botsKilled >= maxBots)
			{
				Game.PlaySound("sounds/shaders/spirit_defeated.ksnd");
				Game.CallDelayedMapScript(2, instigator, 0); //Start Victory
			}
		}
	}
}
//------------------------------------------------------------------------------------------------------------------------
