Follow an avatar

From libopenmetaverse - libomv - Developer Wiki

Jump to: navigation, search

Contents

Prerequisites

The following prerequisites are required in order for you to create a following bot by using this tutorial.

Real World

  • This tutorial assumes that you were able to successfully build libsecondlife! If you have not compiled libsl, please follow the instructions on the Getting Started wikipage.
  • Also, if you have no experience with the C# (c-sharp) programming language, I highly recommend that you stop now and please follow the list of links to csharp tutorials located here.

The Code

Note: We are assuming that our main "SecondLife" variable is named "client." If you have a different name for this variable, please substitute the name accordingly

 
 
        ////Follow code vars///
        private static float followDistance = 0; //How far, in meters, the target AV can go before we start moving
        public static bool followon = true; //A boolean switch to enable or disable following
        public static string followName = "Test User"; //This is the name of the target avatar
 
        public static void Main() //Main event, entry point
        {
             client.Objects.OnObjectUpdated +=new ObjectManager.ObjectUpdatedCallback(Objects_OnObjectUpdated); //Callback for sims object update event, this is where we grab the coordinates.
, 
        }
 
        private static void Objects_OnObjectUpdated(Simulator simulator, ObjectUpdate update, ulong regionHandle, ushort timeDilation) //Callback for object updates in simulator
        {
            if (followon == true) //Check to make sure we need to be following
            {
                if (!update.Avatar){return;}//Exit this event if it's not an avatar update
                Avatar av;
                client.Network.CurrentSim.ObjectsAvatars.TryGetValue(update.LocalID, out av);
                if (av == null) return;
                if (av.Name == followName)
                {
                    LLVector3 pos = av.Position;
 
                    if (LLVector3.Dist(pos, client.Self.SimPosition) > followDistance)
                    {
                        int followRegionX = (int)(regionHandle >> 32);
                        int followRegionY = (int)(regionHandle & 0xFFFFFFFF);
                        int followRegionZ = (int)(regionHandle);
                        ulong x = (ulong)(pos.X + followRegionX);
                        ulong y = (ulong)(pos.Y + followRegionY);
 
                        if (pos.Z > 1)
                        {
                            client.Self.AutoPilotLocal(Convert.ToInt32(pos.X), Convert.ToInt32(pos.Y), pos.Z);
                        }
                        else
                        {
                            client.Self.AutoPilotCancel();
                        }
                    }
                }
            }
 

Notes

  • There seems to be a problem with the bot following you on the Z axis
  • The live performance of the bots follow coordinates updating seems to be based on the simulator's current performance.
  • Inexplicably, the followRegion values are computed but never used in this example.
  • == this code is outdated and does not compile ==