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

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

this.Initialize = function()
{
	disable = true;
	if(Map.Switches)
	{
		foreach (switchTable in Map.Switches)
		{
			if (IsNull(switchTable.Priority))
				{ switchTable.Priority = 0.65; }
			else if (switchTable.Priority == 0)
				{ continue; } //paththrough

			// If not otherwise defined, set a high bot limit for the switches.
			if (IsNull(switchTable.LimitBots))
				{ switchTable.LimitBots = Util.AllTeamPlayers; }

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

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

			disable = false;
		}
	}
	if(disable)
	{
		// Disable this script goal if there are no switches or if there are only paththrough switches.
		this.Disable = true;
	}
	else
	{
		// This goal uses the global blackboard, so let's register a key for it.
		if(!BB.SWITCHUSER)
		{
			BB.SWITCHUSER = Blackboard.MakeKey();
		}
	}
};

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()
{
	Blackboard.PostRecord(BB.SWITCHUSER, {
		Owner = this.Bot.GetGameId(),
		Target = this.BlackboardId,
	});
};

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); }
};
