2007-01-19 10:43:30 +00:00
|
|
|
using System;
|
2021-07-25 11:10:52 -05:00
|
|
|
using System.Linq;
|
2008-07-21 21:12:59 +00:00
|
|
|
using OpenMetaverse.Packets;
|
2007-01-19 10:43:30 +00:00
|
|
|
|
2008-07-21 21:12:59 +00:00
|
|
|
namespace OpenMetaverse.TestClient
|
2007-01-19 10:43:30 +00:00
|
|
|
{
|
|
|
|
|
public class FollowCommand: Command
|
|
|
|
|
{
|
2008-10-08 19:08:10 +00:00
|
|
|
const float DISTANCE_BUFFER = 3.0f;
|
|
|
|
|
uint targetLocalID = 0;
|
|
|
|
|
|
2007-01-19 10:43:30 +00:00
|
|
|
public FollowCommand(TestClient testClient)
|
|
|
|
|
{
|
|
|
|
|
Name = "follow";
|
2008-10-08 19:08:10 +00:00
|
|
|
Description = "Follow another avatar. Usage: follow [FirstName LastName]/off.";
|
2008-07-25 08:55:36 +00:00
|
|
|
Category = CommandCategory.Movement;
|
2007-08-10 20:16:19 +00:00
|
|
|
|
2009-10-28 08:01:52 +00:00
|
|
|
testClient.Network.RegisterCallback(PacketType.AlertMessage, AlertMessageHandler);
|
2007-01-19 10:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
2008-07-25 05:15:05 +00:00
|
|
|
public override string Execute(string[] args, UUID fromAgentID)
|
2007-01-19 10:43:30 +00:00
|
|
|
{
|
2008-10-08 19:08:10 +00:00
|
|
|
// Construct the target name from the passed arguments
|
2022-02-25 19:38:11 -06:00
|
|
|
string target = args.Aggregate(string.Empty, (current, t) => current + t + " ");
|
2021-07-25 11:10:52 -05:00
|
|
|
target = target.TrimEnd();
|
2007-01-19 10:43:30 +00:00
|
|
|
|
2008-10-08 19:08:10 +00:00
|
|
|
if (target.Length == 0 || target == "off")
|
2007-01-19 10:43:30 +00:00
|
|
|
{
|
2008-10-08 19:08:10 +00:00
|
|
|
Active = false;
|
|
|
|
|
targetLocalID = 0;
|
|
|
|
|
Client.Self.AutoPilotCancel();
|
|
|
|
|
return "Following is off";
|
2007-01-19 10:43:30 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-07-01 12:17:07 -05:00
|
|
|
return Follow(target)
|
|
|
|
|
? $"Following {target}."
|
|
|
|
|
: $"Unable to follow {target}. Client may not be able to see the target avatar.";
|
2007-01-19 10:43:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool Follow(string name)
|
|
|
|
|
{
|
2007-08-20 09:26:21 +00:00
|
|
|
lock (Client.Network.Simulators)
|
2007-01-19 10:43:30 +00:00
|
|
|
{
|
2021-07-25 11:10:52 -05:00
|
|
|
foreach (var sim in Client.Network.Simulators)
|
2007-08-20 09:26:21 +00:00
|
|
|
{
|
2021-07-25 11:10:52 -05:00
|
|
|
Avatar target = sim.ObjectsAvatars.Find(
|
2020-05-09 12:59:06 -05:00
|
|
|
avatar => avatar.Name == name
|
2007-08-29 08:55:53 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (target != null)
|
|
|
|
|
{
|
|
|
|
|
targetLocalID = target.LocalID;
|
|
|
|
|
Active = true;
|
|
|
|
|
return true;
|
2007-08-20 09:26:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-01-19 10:43:30 +00:00
|
|
|
}
|
2007-08-10 20:16:19 +00:00
|
|
|
|
2008-10-08 19:08:10 +00:00
|
|
|
if (Active)
|
|
|
|
|
{
|
|
|
|
|
Client.Self.AutoPilotCancel();
|
|
|
|
|
Active = false;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-19 10:43:30 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Think()
|
|
|
|
|
{
|
2008-10-08 19:08:10 +00:00
|
|
|
if (Active)
|
2007-01-19 10:43:30 +00:00
|
|
|
{
|
2008-10-08 19:08:10 +00:00
|
|
|
// Find the target position
|
|
|
|
|
lock (Client.Network.Simulators)
|
2007-08-10 20:16:19 +00:00
|
|
|
{
|
2021-07-25 11:10:52 -05:00
|
|
|
foreach (var t in Client.Network.Simulators)
|
2007-08-20 09:26:21 +00:00
|
|
|
{
|
2008-10-08 19:08:10 +00:00
|
|
|
Avatar targetAv;
|
2007-08-20 09:26:21 +00:00
|
|
|
|
2021-07-25 11:10:52 -05:00
|
|
|
if (t.ObjectsAvatars.TryGetValue(targetLocalID, out targetAv))
|
2007-08-20 09:26:21 +00:00
|
|
|
{
|
2008-10-08 19:08:10 +00:00
|
|
|
float distance = 0.0f;
|
|
|
|
|
|
2021-07-25 11:10:52 -05:00
|
|
|
if (t == Client.Network.CurrentSim)
|
2008-10-08 19:08:10 +00:00
|
|
|
{
|
|
|
|
|
distance = Vector3.Distance(targetAv.Position, Client.Self.SimPosition);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// FIXME: Calculate global distances
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (distance > DISTANCE_BUFFER)
|
|
|
|
|
{
|
|
|
|
|
uint regionX, regionY;
|
2021-07-25 11:10:52 -05:00
|
|
|
Utils.LongToUInts(t.Handle, out regionX, out regionY);
|
2008-10-08 19:08:10 +00:00
|
|
|
|
|
|
|
|
double xTarget = (double)targetAv.Position.X + (double)regionX;
|
|
|
|
|
double yTarget = (double)targetAv.Position.Y + (double)regionY;
|
|
|
|
|
double zTarget = targetAv.Position.Z - 2f;
|
|
|
|
|
|
2022-02-25 19:38:11 -06:00
|
|
|
Logger.DebugLog(
|
|
|
|
|
$"[Autopilot] {distance} meters away from the target, starting autopilot to <{xTarget},{yTarget},{zTarget}>", Client);
|
2008-10-08 19:08:10 +00:00
|
|
|
|
|
|
|
|
Client.Self.AutoPilot(xTarget, yTarget, zTarget);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// We are in range of the target and moving, stop moving
|
|
|
|
|
Client.Self.AutoPilotCancel();
|
|
|
|
|
}
|
2007-08-20 09:26:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-08-10 20:16:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-01-19 10:43:30 +00:00
|
|
|
|
|
|
|
|
base.Think();
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-28 08:01:52 +00:00
|
|
|
private void AlertMessageHandler(object sender, PacketReceivedEventArgs e)
|
2007-08-10 20:16:19 +00:00
|
|
|
{
|
2009-10-28 08:01:52 +00:00
|
|
|
Packet packet = e.Packet;
|
|
|
|
|
|
2007-08-10 20:16:19 +00:00
|
|
|
AlertMessagePacket alert = (AlertMessagePacket)packet;
|
2021-09-11 09:21:35 -05:00
|
|
|
if (alert.AlertInfo.Length > 0)
|
2007-08-10 20:16:19 +00:00
|
|
|
{
|
2021-09-11 09:21:35 -05:00
|
|
|
string id = Utils.BytesToString(alert.AlertInfo[0].Message);
|
2021-09-11 09:37:03 -05:00
|
|
|
if (id == "AutopilotCanceled")
|
2021-09-11 09:21:35 -05:00
|
|
|
{
|
|
|
|
|
Logger.Log("FollowCommand: " + Utils.BytesToString(alert.AlertData.Message),
|
|
|
|
|
Helpers.LogLevel.Info, Client);
|
|
|
|
|
}
|
2007-08-10 20:16:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-01-19 10:43:30 +00:00
|
|
|
}
|
|
|
|
|
}
|