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

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

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

this.Initialize = function()
{
	// If there is no global Map or RideTram table this script goal is useless,
	// so let's disable it from consideration.
	if(!Map.RideTram)
	{
		this.Disable = true;
	}
};

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

	if(!this.IsActive())
	{
		// Look for a tram we can go for.
		vehicleIndex = 0;

		foreach (i and vehTable in Map.RideTram)
		{
			if((vehTable.Enabled) &&
				(vehTable.LimitTeam & myTeamMask) &&
				(vehTable.LimitClass & myClassMask))
			{
				vehGoal = GetGoal(vehTable.MoverName);
				vehicleEnt = vehGoal.GetEntity();
				enterPos = Util.GetWpNamePosition(vehTable.EnterWaypointName);
				maxBots = vehTable.MaxUsers;

				if(!maxBots) {maxBots = 2;}

				if(vehicleEnt && enterPos)
				{
					bbId = vehicleIndex;

					numBotsWithGoal = Blackboard.GetNumRecords(BB.RIDETRAM, bbId);
					if(numBotsWithGoal < maxBots)
					{
						this.CurrentVehicle = i;
						this.CurrentVehicleEntity = vehicleEnt;
						this.enterPos = enterPos;
						this.waitTimeout = vehTable.WaitTimeout;
						this.rideWaypointName = vehTable.RideWaypointName;
						this.ridePosition = Util.GetWpNamePosition(this.rideWaypointName);
						this.targetWaypointName = vehTable.TargetWaypointName;
						this.targetPos = Util.GetWpNamePosition(this.targetWaypointName);
						this.SeatRadius = vehTable.SeatRadius;

						this.BlackboardId = bbId;

						this.Priority = vehTable.Priority;

						//print(this.Bot.Name, "going for tram", i);

						return;
					}
				}
			}
			vehicleIndex = vehicleIndex+1;
		}
	}
	else
	{
		// Abort if the goal is disabled
		vehicle = Map.RideTram[ this.CurrentVehicle ];
		if(vehicle && !vehicle.Enabled)
		{
			this.Priority = 0;
		}
	}
};

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

this.Exit = function()
{
	this.CurrentVehicle = null;
	this.CurrentVehicleEntity = null;
	this.BlackboardId = null;
	this.enterPos = null;
	this.waitTimeout = null;
	this.rideWaypointName = null;
	this.ridePosition = null;
	this.targetWaypointName = null;
	this.targetPos = null;
	this.SeatRadius = null;

	// Remove myself from the blackboard for using this tram
	Blackboard.RemoveByPoster(this.Bot.GetGameId(), BB.RIDETRAM);
};

this.Update = function()
{
	vehicle = Map.RideTram[ this.CurrentVehicle ];

	if(this.Goto(this.enterPos) == EVENT.PATH_SUCCESS)
	{
		timeout = 0;
		seconds_waited = 0;
		boardingfailed = false;
		//print(this.Bot.Name, "waiting for tram.");

		while(this.Bot.DistanceTo(GetEntPosition(this.CurrentVehicleEntity)) > 300)
		{
			sleep(1);
			++seconds_waited;
			if (seconds_waited == this.waitTimeout)
			{
				boardingfailed = true;
				//print("timeout expired");
				this.Finished();
				break;
			}
		}
		// Attempt to board the vehicle

		//this.Bot.Say("Geronimo!");
		while(!this.Bot.MoveTowards(this.ridePosition))
		{
			if (timeout == 5000)
			{
				boardingfailed = true;
				break;
			}

			timeout = timeout + 50;

			myPos = this.Bot.GetPosition();
			if(timeout > 200 && myPos.z < this.ridePosition.z)
			{
				this.Bot.HoldButton(BTN.JUMP, 0.1);
			}

			yield();
		}

		if(boardingfailed)
		{
			// Finish the state.
			this.Finished();
		}

		// First increase priority so the bots won't abandon the goal in the middle of a ride
		this.Priority = 1.5;

		// Aim someplace logical
		this.AddAimRequest(Priority.Low);

		// Ride cowboy ride
		for(;;)
		{
			if(!this.Bot.HasTarget())
			{
				this.Bot.MoveTowards(GetEntPosition(this.CurrentVehicleEntity));
			}
			if (GetEntVelocity(this.CurrentVehicleEntity).IsZero() && this.Bot.DistanceTo(this.targetPos) < 200)
			{
				break;
			}
			if (this.Bot.DistanceTo(GetEntPosition(this.CurrentVehicleEntity)) > this.SeatRadius)
			{
				break;
			}
			yield();
		}
		//this.Bot.Say("Vehicle stopped, or whatever.");
		this.Finished();
	}
	else
	{
		// Finish the state.
		this.Finished();
	}
};