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
|
|
|
|
|
{
|
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
|
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();
|
|
|
|
|
|
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
|
|
|
|
|
{
|
2008-10-08 19:08:10 +00:00
|
|
|
if (Follow(target))
|
|
|
|
|
return "Following " + target;
|
2007-04-23 01:39:14 +00:00
|
|
|
else
|
2008-10-08 19:08:10 +00:00
|
|
|
return "Unable to follow " + target + ". Client may not be able to see that 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
|
|
|
{
|
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(
|
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
|
|
|
{
|
2008-10-08 19:08:10 +00:00
|
|
|
for (int i = 0; i < Client.Network.Simulators.Count; i++)
|
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
|
|
|
|
2008-10-08 19:08:10 +00:00
|
|
|
if (Client.Network.Simulators[i].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;
|
|
|
|
|
|
|
|
|
|
if (Client.Network.Simulators[i] == Client.Network.CurrentSim)
|
|
|
|
|
{
|
|
|
|
|
distance = Vector3.Distance(targetAv.Position, Client.Self.SimPosition);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// FIXME: Calculate global distances
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (distance > DISTANCE_BUFFER)
|
|
|
|
|
{
|
|
|
|
|
uint regionX, regionY;
|
|
|
|
|
Utils.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;
|
|
|
|
|
|
|
|
|
|
Logger.DebugLog(String.Format("[Autopilot] {0} meters away from the target, starting autopilot to <{1},{2},{3}>",
|
|
|
|
|
distance, xTarget, yTarget, zTarget), Client);
|
|
|
|
|
|
|
|
|
|
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;
|
2008-08-12 22:38:02 +00:00
|
|
|
string message = Utils.BytesToString(alert.AlertData.Message);
|
2007-08-10 20:16:19 +00:00
|
|
|
|
|
|
|
|
if (message.Contains("Autopilot cancel"))
|
|
|
|
|
{
|
2008-10-08 19:08:10 +00:00
|
|
|
Logger.Log("FollowCommand: " + message, Helpers.LogLevel.Info, Client);
|
2007-08-10 20:16:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-01-19 10:43:30 +00:00
|
|
|
}
|
|
|
|
|
}
|