2007-01-19 10:43:30 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
2008-07-21 21:12:59 +00:00
|
|
|
using OpenMetaverse;
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
public FollowCommand(TestClient testClient)
|
|
|
|
|
{
|
|
|
|
|
Name = "follow";
|
|
|
|
|
Description = "Follow another avatar. (usage: follow [FirstName LastName]) If no target is set then will follow master.";
|
2008-07-25 08:55:36 +00:00
|
|
|
Category = CommandCategory.Movement;
|
2007-08-10 20:16:19 +00:00
|
|
|
|
|
|
|
|
testClient.Network.RegisterCallback(PacketType.AlertMessage, new NetworkManager.PacketCallback(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
|
|
|
{
|
|
|
|
|
string target = String.Empty;
|
|
|
|
|
for (int ct = 0; ct < args.Length; ct++)
|
|
|
|
|
target = target + args[ct] + " ";
|
|
|
|
|
target = target.TrimEnd();
|
|
|
|
|
|
|
|
|
|
if (target.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
if (Follow(target))
|
|
|
|
|
return "Following " + target;
|
|
|
|
|
else
|
|
|
|
|
return "Unable to follow " + target + ". Client may not be able to see that avatar.";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2008-07-30 06:40:47 +00:00
|
|
|
if (Client.MasterKey != UUID.Zero)
|
|
|
|
|
{
|
|
|
|
|
if (Follow(Client.MasterKey))
|
|
|
|
|
return "Following UUID " + Client.MasterKey;
|
|
|
|
|
else
|
|
|
|
|
return "Unable to follow UUID " + Client.MasterKey;
|
|
|
|
|
}
|
|
|
|
|
else if (Client.MasterName != String.Empty)
|
|
|
|
|
{
|
|
|
|
|
if (Follow(Client.MasterName))
|
|
|
|
|
return "Following " + Client.MasterName;
|
|
|
|
|
else
|
|
|
|
|
return "Unable to follow " + Client.MasterName;
|
|
|
|
|
}
|
2007-04-23 01:39:14 +00:00
|
|
|
else
|
2008-07-30 06:40:47 +00:00
|
|
|
{
|
|
|
|
|
return "No master specified. Usage: follow <target>";
|
|
|
|
|
}
|
2007-01-19 10:43:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const float DISTANCE_BUFFER = 3.0f;
|
2007-08-10 20:16:19 +00:00
|
|
|
uint targetLocalID = 0;
|
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
|
|
|
{
|
2007-08-20 09:26:21 +00:00
|
|
|
for (int i = 0; i < Client.Network.Simulators.Count; i++)
|
|
|
|
|
{
|
2008-01-03 21:55:49 +00:00
|
|
|
Avatar target = Client.Network.Simulators[i].ObjectsAvatars.Find(
|
2007-08-29 08:55:53 +00:00
|
|
|
delegate(Avatar avatar)
|
2007-08-20 09:26:21 +00:00
|
|
|
{
|
2007-08-29 08:55:53 +00:00
|
|
|
return avatar.Name == name;
|
2007-08-20 09:26:21 +00:00
|
|
|
}
|
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
|
|
|
|
|
|
|
|
Active = false;
|
2007-01-19 10:43:30 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-25 05:15:05 +00:00
|
|
|
bool Follow(UUID id)
|
2007-04-23 01:39:14 +00:00
|
|
|
{
|
2007-08-20 09:26:21 +00:00
|
|
|
lock (Client.Network.Simulators)
|
2007-04-23 01:39:14 +00:00
|
|
|
{
|
2007-08-20 09:26:21 +00:00
|
|
|
for (int i = 0; i < Client.Network.Simulators.Count; i++)
|
2007-04-23 01:39:14 +00:00
|
|
|
{
|
2008-01-03 21:55:49 +00:00
|
|
|
Avatar target = Client.Network.Simulators[i].ObjectsAvatars.Find(
|
2007-08-29 08:55:53 +00:00
|
|
|
delegate(Avatar avatar)
|
2007-08-20 09:26:21 +00:00
|
|
|
{
|
2007-08-29 08:55:53 +00:00
|
|
|
return avatar.ID == id;
|
2007-08-20 09:26:21 +00:00
|
|
|
}
|
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-04-23 01:39:14 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-08-10 20:16:19 +00:00
|
|
|
|
|
|
|
|
Active = false;
|
2007-04-23 01:39:14 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2007-01-19 10:43:30 +00:00
|
|
|
public override void Think()
|
|
|
|
|
{
|
2007-08-10 20:16:19 +00:00
|
|
|
// Find the target position
|
2007-08-20 09:26:21 +00:00
|
|
|
lock (Client.Network.Simulators)
|
2007-01-19 10:43:30 +00:00
|
|
|
{
|
2007-08-20 09:26:21 +00:00
|
|
|
for (int i = 0; i < Client.Network.Simulators.Count; i++)
|
2007-08-10 20:16:19 +00:00
|
|
|
{
|
2007-08-29 08:55:53 +00:00
|
|
|
Avatar targetAv;
|
|
|
|
|
|
2008-01-03 21:55:49 +00:00
|
|
|
if (Client.Network.Simulators[i].ObjectsAvatars.TryGetValue(targetLocalID, out targetAv))
|
2007-08-20 09:26:21 +00:00
|
|
|
{
|
|
|
|
|
float distance = 0.0f;
|
|
|
|
|
|
|
|
|
|
if (Client.Network.Simulators[i] == Client.Network.CurrentSim)
|
|
|
|
|
{
|
2008-08-01 20:22:22 +00:00
|
|
|
distance = Vector3.Distance(targetAv.Position, Client.Self.SimPosition);
|
2007-08-20 09:26:21 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// FIXME: Calculate global distances
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (distance > DISTANCE_BUFFER)
|
|
|
|
|
{
|
|
|
|
|
uint regionX, regionY;
|
|
|
|
|
Helpers.LongToUInts(Client.Network.Simulators[i].Handle, out regionX, out regionY);
|
|
|
|
|
|
|
|
|
|
double xTarget = (double)targetAv.Position.X + (double)regionX;
|
|
|
|
|
double yTarget = (double)targetAv.Position.Y + (double)regionY;
|
|
|
|
|
double zTarget = targetAv.Position.Z - 2f;
|
|
|
|
|
|
2008-05-06 23:57:26 +00:00
|
|
|
Logger.DebugLog(String.Format("[Autopilot] {0} meters away from the target, starting autopilot to <{1},{2},{3}>",
|
|
|
|
|
distance, xTarget, yTarget, zTarget), Client);
|
2007-08-20 09:26:21 +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-10 20:16:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-01-19 10:43:30 +00:00
|
|
|
|
|
|
|
|
base.Think();
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-10 20:16:19 +00:00
|
|
|
private void AlertMessageHandler(Packet packet, Simulator simulator)
|
|
|
|
|
{
|
|
|
|
|
AlertMessagePacket alert = (AlertMessagePacket)packet;
|
|
|
|
|
string message = Helpers.FieldToUTF8String(alert.AlertData.Message);
|
|
|
|
|
|
|
|
|
|
if (message.Contains("Autopilot cancel"))
|
|
|
|
|
{
|
2008-05-06 23:57:26 +00:00
|
|
|
Logger.Log("Server cancelled the autopilot", Helpers.LogLevel.Info, Client);
|
2007-08-10 20:16:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-01-19 10:43:30 +00:00
|
|
|
}
|
|
|
|
|
}
|