Building a bot
From OpenSimulator
Steevithak (Talk | contribs) m (replaced 404 link using wayback archive) |
|||
(9 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
− | [[ | + | __NOTOC__ |
+ | {{Quicklinks}} | ||
+ | |||
+ | [[File:MyPetBot.png|220px|left|thumb|MyPetBot]] | ||
+ | |||
+ | '''Please note, this tutorial refers to building and running a bot using libomv. This technique creates a client that connects to OpenSimulator like any other viewer. For information on other NPC techniques, such as purely server-side NPCs, please see [[NPC]].''' | ||
Bots, or Non-Player Characters (NPCs), represent instances where, instead | Bots, or Non-Player Characters (NPCs), represent instances where, instead | ||
Line 23: | Line 28: | ||
# Specify your region server | # Specify your region server | ||
− | < | + | <source lang ="csharp"> |
// Need to add the following References if using VC# 2008: | // Need to add the following References if using VC# 2008: | ||
// OpenMetaverse, OpenMetaverseTypes, System | // OpenMetaverse, OpenMetaverseTypes, System | ||
Line 59: | Line 64: | ||
(int) startLoc.X,(int) startLoc.Y,(int) startLoc.Z); | (int) startLoc.X,(int) startLoc.Y,(int) startLoc.Z); | ||
client.Network.OnConnected += new NetworkManager.ConnectedCallback(Network_OnConnected); | client.Network.OnConnected += new NetworkManager.ConnectedCallback(Network_OnConnected); | ||
− | + | client.Settings.LOGIN_SERVER = "http://yourserver:port/"; | |
− | + | string[] pointAtt = new string[8]; | |
− | + | if (client.Network.Login(first_name, last_name, password, "My First Bot", startLocation, "Your name")) | |
− | + | { | |
− | + | client.Network.OnConnected += new NetworkManager.ConnectedCallback(Network_OnConnected); | |
− | + | Console.WriteLine("Bot Login Message: " + client.Network.LoginMessage); | |
− | client.Network. | + | client.Appearance.SetPreviousAppearance(false); |
− | + | } | |
− | + | ||
} | } | ||
public static void Objects_OnObjectUpdated(Simulator simulator, ObjectUpdate update, | public static void Objects_OnObjectUpdated(Simulator simulator, ObjectUpdate update, | ||
Line 120: | Line 124: | ||
} | } | ||
} | } | ||
− | </ | + | </source> |
− | This tutorial | + | This tutorial is based on code from [https://web.archive.org/web/20120626125345/http://lib.openmetaverse.org/wiki/Follow_an_avatar Follow An Avatar] |
Latest revision as of 09:39, 15 August 2020
Please note, this tutorial refers to building and running a bot using libomv. This technique creates a client that connects to OpenSimulator like any other viewer. For information on other NPC techniques, such as purely server-side NPCs, please see NPC.
Bots, or Non-Player Characters (NPCs), represent instances where, instead of a user controlling an agent represented as an avatar in-world, a program controls the agent. This facility allows one to program humanoid figures for a variety of tasks. There are two ways that bots can be coded: on a client or on the server where opensim is running. This tutorial covers only the former case: running code on the client, which coincidentally may be the same computer where the server is located.
The following example is called "MyPetBot" and by building this source, and running the resulting bin\MyPetBot executable that results in the bot logging into the world, and then proceeding to follow whatever avatar is specified inside the code.
A short video is shown here To build your pet bot, you will need to copy the following code and make sure to edit the following parts:
- In your C# solution, include the missing references
- Enter the first name, last name, and password of the agent that will serve as a bot
- Specify the start location (X,Y,Z) for the bot
- Specify your region server
// Need to add the following References if using VC# 2008: // OpenMetaverse, OpenMetaverseTypes, System // These can be found in your opensim\bin folder using System; using System.IO; using System.Collections.Generic; using System.Text; using OpenMetaverse; namespace MyPetBot { class MyPetBot { public static GridClient client = new GridClient(); // Enter the name of the agent that you wish to treat // as a bot. For example" Test User" // Also enter the login password for this agent private static string first_name = "Test"; private static string last_name = "User"; private static string password = "test"; // Specify where the bot is to be logged in public static Vector3 startLoc = new Vector3(128, 128, 22); public static Vector3 pos = new Vector3(); public static int turn_count = 0; // How far, in meters, the target AV can go before we start moving private static float followDistance = 3; // A boolean switch to enable or disable following public static bool followon = false; // This is the name of the agent to follow public static string followName = "FirstName LastName"; public static void Main() { // Enter the region name where the bot will log in string startLocation = NetworkManager.StartLocation("Your Region", (int) startLoc.X,(int) startLoc.Y,(int) startLoc.Z); client.Network.OnConnected += new NetworkManager.ConnectedCallback(Network_OnConnected); client.Settings.LOGIN_SERVER = "http://yourserver:port/"; string[] pointAtt = new string[8]; if (client.Network.Login(first_name, last_name, password, "My First Bot", startLocation, "Your name")) { client.Network.OnConnected += new NetworkManager.ConnectedCallback(Network_OnConnected); Console.WriteLine("Bot Login Message: " + client.Network.LoginMessage); client.Appearance.SetPreviousAppearance(false); } } public static void Objects_OnObjectUpdated(Simulator simulator, ObjectUpdate update, ulong regionHandle, ushort timeDilation) { if (followon == true) //Check to make sure we need to be following { //Exit this event if it's not an avatar update if (!update.Avatar) { return; } Avatar av; client.Network.CurrentSim.ObjectsAvatars.TryGetValue(update.LocalID, out av); if (av == null) return; if (av.Name == followName) { pos = av.Position; if (Vector3.Distance(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); turn_count++; if (turn_count%10 == 1) client.Self.Movement.TurnToward(pos); if (pos.Z > 1) { client.Self.AutoPilotLocal(Convert.ToInt32(pos.X), Convert.ToInt32(pos.Y), pos.Z); } else { client.Self.AutoPilotCancel(); } } else { turn_count = 0; client.Self.Movement.TurnToward(pos); } } } } static void Network_OnConnected(object sender) { Console.WriteLine("The bot is connected"); client.Self.Movement.AlwaysRun = false; System.Threading.Thread.Sleep(3000); followon = true; client.Objects.OnObjectUpdated += new ObjectManager.ObjectUpdatedCallback(Objects_OnObjectUpdated); } } }
This tutorial is based on code from Follow An Avatar