// This script determines if a bot needs to change classes

this.Name = "ClassManager";
this.Parent = "LowLevel";
this.AlwaysRecieveEvents = false;
this.OnlyWhileDead = true; // if set to false, they will /kill for class changes
this.Interval = 6; // time between checks (when OnlyWhileDead is false)
this.CanActivate = false; // delay activation even with maxbots -1

this.GetPriority = function()
{
	if (TestMapOn || DisableClassManager || TestBot || Server.CountedPlayers < Server.MaxBots || !this.CanActivate) {
		sleep(this.Interval);
		this.CanActivate = true;
		return;
	}

	// cs: set a priority so Update can run and subsequently Exit
	// Exit is used in lieu of the death event since the death event
	// has been found to be unreliable (thx mc2718).
	this.Priority = 1.0;
	block(0);
};

this.Update = function()
{
	for(;;)
	{
		// cs: let them /kill in warmup so transitions from snipe or panzer maps wont take forever for correct classes to be used
		if (!this.OnlyWhileDead || GetGameState() != "Playing") {
			this.CheckForClassChange();
		}
		
		sleep(this.Interval);
	}
};

this.Exit = function()
{
	if (this.OnlyWhileDead && GetGameState() == "Playing") {
		this.CheckForClassChange();
	}
};

this.CheckForClassChange = function()
{
	// cs: added temp test for timestamp to limit repeated /kills reported by blindman
	if (this.Bot.crucialClass || (this.Bot.classChangeTime && GetTime() - this.Bot.classChangeTime < 20)) {
		// don't change crucial class which has been set by role manager
		return;
	}

	newClass = ClassManager.EvalClassChange(this.Bot);

	if (newClass) {
		// cs: this must come before the class change call to prevent them from /killing next spawn
		// cs: note: version 0.82 should not require it to come before the class change
		this.Bot.ExecCommand("kill");
		this.Bot.ChangeClass(newClass);
		this.Bot.classChangeTime = GetTime(); // test for blindman
	}
};
