// This script is deprecated. You should use the SWITCH goal instead.

// This goal uses the global blackboard, so let's register a key for it.
if(!BB.SWITCHUSER)
{
	BB.SWITCHUSER = Blackboard.MakeKey();
}

this.Name = "UseSwitch";
this.Parent = "HighLevel";
this.GetPriorityDelay = 5;		// Delay between calls to GetPriority

this.Initialize = function()
{
	// Internal variables

	// If there is no global map or switches table this script goal is useless,
	// so lets disable it from consideration.
	if(!Map.Switches)
	{
		this.Disable = true;
	}
	else
	{
		// If not otherwise defined, set a high bot limit for the switches.
		foreach ( switchTable in Map.Switches )
		{
			if ( typeId( switchTable.Priority ) == 0 )
				{ switchTable.Priority = 0.65; }

			if ( typeId( switchTable.LimitBots ) == 0 )
				{ switchTable.LimitBots = Util.AllTeamPlayers; }

			if ( typeId( switchTable.LimitTeam ) == 0 )
				{ switchTable.LimitTeam = Util.AllTeams; }

			if ( typeId( switchTable.LimitClass ) == 0 )
				{ switchTable.LimitClass = Util.AllClasses; }

			if ( typeId( switchTable.Timeout ) == 0 )
				{ switchTable.Timeout = 2000; }
		}
	}
};

this.GetPriority = function()
{
	myTeamMask = (1<<this.Bot.GetTeam());
	myClassMask = (1<<this.Bot.GetClass());

	if(!this.IsActive())
	{
		this.CurrentPriority = 0;
		// Look for a switch we can go for.
		foreach ( i and currentswitch in Map.Switches )
		{
			if (currentswitch.Priority == 0){ continue; } //ignore paththrough switch
			
			numBotsOnSwitch = Blackboard.GetNumRecords(BB.SWITCHUSER, currentswitch.Serial);

			if ( currentswitch.Debug )
			{
				print("currentswitch.Enabled:", currentswitch.Enabled);
				print("currentswitch.LimitTeam:", currentswitch.LimitTeam);
				print("currentswitch.LimitClass:", currentswitch.LimitClass);
				print("numBotsOnSwitch:", numBotsOnSwitch);
				print("currentswitch.LimitBots:", currentswitch.LimitBots);
				print("!this.BlackboardIsDelayed:", !this.BlackboardIsDelayed(currentswitch.Serial) );

				if ( currentswitch.AimPosition )
				{
					print("AimPosition", currentswitch.AimPosition );
				}
			}

			if ( currentswitch.LimitDistance && currentswitch.LimitDistance > 0  )
			{
				dist = this.Bot.DistanceTo(currentswitch.Waypoint.position);

				if ( currentswitch.Debug )
					{ print(this.Bot.Name, "^2is", dist, "away from", currentswitch.WaypointName); }

				if ( dist > currentswitch.LimitDistance )
					{ continue; }
			}

			if( 	( currentswitch.Enabled ) &&
				( currentswitch.LimitTeam & myTeamMask ) &&
				( currentswitch.LimitClass & myClassMask ) &&
				( numBotsOnSwitch < currentswitch.LimitBots ) &&
				( !this.BlackboardIsDelayed(currentswitch.Serial) ))
			{
				if ( currentswitch.Priority > this.CurrentPriority )
				{
					this.CurrentSwitch = i;
					this.CurrentPriority = currentswitch.Priority;
					this.DebugString = currentswitch.WaypointName;
					this.BlackboardId = currentswitch.Serial;
				}
			}
		}

		this.Priority = this.CurrentPriority;
	}
	else
	{
		// Abort if the goal disabled
		currentswitch = Map.Switches[this.CurrentSwitch];
		if(currentswitch && !currentswitch.Enabled)
		{
			this.Priority = 0;
		}
	}
};

this.Enter = function()
{
	bbRecord =
	{
		Owner = this.Bot.GetGameId(),
		Target = this.BlackboardId,
	};
	Blackboard.PostRecord(BB.SWITCHUSER, bbRecord);
};

this.Exit = function()
{
	// Remove myself from the blackboard for using this switch
	Blackboard.RemoveByPoster(this.Bot.GetGameId(), BB.SWITCHUSER);
};

this.Update = function()
{
	currentswitch = Map.Switches[this.CurrentSwitch];

	 if ( currentswitch.Debug )
			{ print( this.Bot.Name, " heading to ", currentswitch.WaypointName ); }

	pSuccess = false;
	
	if( this.Goto(currentswitch.Waypoint.position, currentswitch.Waypoint.radius) == EVENT.PATH_SUCCESS )
		{ pSuccess = true; }

	if ( pSuccess )
		{ this.RunSwitch(); }
	else
		{ this.BlackboardDelay(30, currentswitch.Serial); }

    // Finish the state.
	this.Finished();
};

this.RunSwitch = function()
{
	currentswitch = Map.Switches[this.CurrentSwitch];

	if ( !currentswitch.AimPosition )
	{
		this.AddAimRequest(Priority.High, "facing", currentswitch.Waypoint.facing);
	}
	else
	{
		this.AddAimRequest(Priority.High, "position", currentswitch.AimPosition);
	}

	if( !currentswitch.Timeout )
	{
		currentswitch.Timeout = 2000;
	}

	myTeamMask = (1<<this.Bot.GetTeam());
	myClassMask = (1<<this.Bot.GetClass());

	exitCondition = false;
	timeout = 0;

	while( !exitCondition && currentswitch.Enabled && ( currentswitch.LimitTeam & myTeamMask )
		&& ( currentswitch.LimitClass & myClassMask) && timeout < currentswitch.Timeout )
	{
		timeout += 250;

		this.Bot.MoveTowards(currentswitch.Waypoint.position);
		this.Bot.PressButton( BTN.USE);

		if ( currentswitch.ExitConditions )
		{
			foreach ( id and func in currentswitch.ExitConditions )
			{
				if ( func(this.Bot) == true )
				{
					if ( currentswitch.Debug )
						{ print(this.Bot.Name, "^2exit condition met for", currentswitch.WaypointName); }

					exitCondition = true;
					break;
				}
			}
		}

		sleep(0.25);
	}

	// timed out, give enough time to reposition
	if ( timeout >= currentswitch.Timeout )
		{ this.BlackboardDelay(5, currentswitch.Serial); }
};
