From 66c60c56a0bac01e0f4f2a9d5c673a48ff94642b Mon Sep 17 00:00:00 2001
From: Justin Clark-Casey (justincc)
Date: Thu, 3 Nov 2011 21:16:24 +0000
Subject: Separate out physics testing actions into a separate PhysicsBehaviour
 class

---
 OpenSim/Tools/pCampBot/BotManager.cs            |  16 ++-
 OpenSim/Tools/pCampBot/Interfaces/IBehaviour.cs |  37 ++++++
 OpenSim/Tools/pCampBot/PhysicsBehaviour.cs      |  97 +++++++++++++++
 OpenSim/Tools/pCampBot/PhysicsBot.cs            | 152 ++++++++++--------------
 OpenSim/Tools/pCampBot/pCampBot.cs              |  14 ++-
 5 files changed, 221 insertions(+), 95 deletions(-)
 create mode 100644 OpenSim/Tools/pCampBot/Interfaces/IBehaviour.cs
 create mode 100644 OpenSim/Tools/pCampBot/PhysicsBehaviour.cs

(limited to 'OpenSim')

diff --git a/OpenSim/Tools/pCampBot/BotManager.cs b/OpenSim/Tools/pCampBot/BotManager.cs
index b05bd6d..0505c97 100644
--- a/OpenSim/Tools/pCampBot/BotManager.cs
+++ b/OpenSim/Tools/pCampBot/BotManager.cs
@@ -37,6 +37,7 @@ using log4net.Repository;
 using Nini.Config;
 using OpenSim.Framework;
 using OpenSim.Framework.Console;
+using pCampBot.Interfaces;
 
 namespace pCampBot
 {
@@ -119,10 +120,14 @@ namespace pCampBot
             string password = cs.GetString("password");
             string loginUri = cs.GetString("loginuri");
 
+            // Hardcoded for new
+            List<IBehaviour> behaviours = new List<IBehaviour>();
+            behaviours.Add(new PhysicsBehaviour());
+
             for (int i = 0; i < botcount; i++)
             {
                 string lastName = string.Format("{0}_{1}", lastNameStem, i);
-                startupBot(i, this, firstName, lastName, password, loginUri);
+                startupBot(i, this, behaviours, firstName, lastName, password, loginUri);
             }
         }
 
@@ -150,14 +155,17 @@ namespace pCampBot
         /// This starts up the bot and stores the thread for the bot in the thread array
         /// </summary>
         /// <param name="pos">The position in the thread array to stick the bot's thread</param>
-        /// <param name="cs">Configuration of the bot</param>
+        /// <param name="bm"></param>
+        /// <param name="behaviours">Behaviours for this bot to perform.</param>
         /// <param name="firstName">First name</param>
         /// <param name="lastName">Last name</param>
         /// <param name="password">Password</param>
         /// <param name="loginUri">Login URI</param>
-        public void startupBot(int pos, BotManager bm, string firstName, string lastName, string password, string loginUri)
+        public void startupBot(
+             int pos, BotManager bm, List<IBehaviour> behaviours,
+             string firstName, string lastName, string password, string loginUri)
         {
-            PhysicsBot pb = new PhysicsBot(bm, firstName, lastName, password, loginUri);
+            PhysicsBot pb = new PhysicsBot(bm, behaviours, firstName, lastName, password, loginUri);
 
             pb.OnConnected += handlebotEvent;
             pb.OnDisconnected += handlebotEvent;
diff --git a/OpenSim/Tools/pCampBot/Interfaces/IBehaviour.cs b/OpenSim/Tools/pCampBot/Interfaces/IBehaviour.cs
new file mode 100644
index 0000000..f9eaa1c
--- /dev/null
+++ b/OpenSim/Tools/pCampBot/Interfaces/IBehaviour.cs
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) Contributors, http://opensimulator.org/
+ * See CONTRIBUTORS.TXT for a full list of copyright holders.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in the
+ *       documentation and/or other materials provided with the distribution.
+ *     * Neither the name of the OpenSimulator Project nor the
+ *       names of its contributors may be used to endorse or promote products
+ *       derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+using pCampBot;
+using System;
+
+namespace pCampBot.Interfaces
+{
+    public interface IBehaviour
+    {
+        void Action(PhysicsBot bot);
+    }
+}
\ No newline at end of file
diff --git a/OpenSim/Tools/pCampBot/PhysicsBehaviour.cs b/OpenSim/Tools/pCampBot/PhysicsBehaviour.cs
new file mode 100644
index 0000000..e76c0b3
--- /dev/null
+++ b/OpenSim/Tools/pCampBot/PhysicsBehaviour.cs
@@ -0,0 +1,97 @@
+/*
+ * Copyright (c) Contributors, http://opensimulator.org/
+ * See CONTRIBUTORS.TXT for a full list of copyright holders.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *     * Redistributions of source code must retain the above copyright
+ *       notice, this list of conditions and the following disclaimer.
+ *     * Redistributions in binary form must reproduce the above copyright
+ *       notice, this list of conditions and the following disclaimer in the
+ *       documentation and/or other materials provided with the distribution.
+ *     * Neither the name of the OpenSimulator Project nor the
+ *       names of its contributors may be used to endorse or promote products
+ *       derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
+ * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+using System;
+using System.IO;
+using System.Threading;
+using OpenMetaverse;
+using OpenSim.Framework;
+using pCampBot.Interfaces;
+
+namespace pCampBot
+{
+    /// <summary>
+    /// This is a behaviour designed to stress physics by moving and bouncing around bots a whole lot.
+    /// </summary>
+    /// <remarks>
+    /// TODO: talkarray should be in a separate behaviour.
+    /// </remarks>
+    public class PhysicsBehaviour : IBehaviour
+    {
+        private string[] talkarray;
+
+        public PhysicsBehaviour()
+        {
+            talkarray = readexcuses();
+        }
+
+        public void Action(PhysicsBot bot)
+        {
+            int walkorrun = bot.Random.Next(4); // Randomize between walking and running. The greater this number,
+                                                // the greater the bot's chances to walk instead of run.
+            bot.Client.Self.Jump(false);
+            if (walkorrun == 0)
+            {
+                bot.Client.Self.Movement.AlwaysRun = true;
+            }
+            else
+            {
+                bot.Client.Self.Movement.AlwaysRun = false;
+            }
+
+            // TODO: unused: Vector3 pos = client.Self.SimPosition;
+            Vector3 newpos = new Vector3(bot.Random.Next(1, 254), bot.Random.Next(1, 254), bot.Random.Next(1, 254));
+            bot.Client.Self.Movement.TurnToward(newpos);
+
+            bot.Client.Self.Movement.AtPos = true;
+            Thread.Sleep(bot.Random.Next(3000, 13000));
+            bot.Client.Self.Movement.AtPos = false;
+            bot.Client.Self.Jump(true);
+
+            string randomf = talkarray[bot.Random.Next(talkarray.Length)];
+            if (talkarray.Length > 1 && randomf.Length > 1)
+                bot.Client.Self.Chat(randomf, 0, ChatType.Normal);
+
+            Thread.Sleep(bot.Random.Next(1000, 10000));
+        }
+
+        private string[] readexcuses()
+        {
+            string allexcuses = "";
+
+            string file = Path.Combine(Util.configDir(), "pCampBotSentences.txt");
+            if (File.Exists(file))
+            {
+                StreamReader csr = File.OpenText(file);
+                allexcuses = csr.ReadToEnd();
+                csr.Close();
+            }
+
+            return allexcuses.Split(Environment.NewLine.ToCharArray());
+        }
+    }
+}
\ No newline at end of file
diff --git a/OpenSim/Tools/pCampBot/PhysicsBot.cs b/OpenSim/Tools/pCampBot/PhysicsBot.cs
index a8b2426..c1bd49f 100644
--- a/OpenSim/Tools/pCampBot/PhysicsBot.cs
+++ b/OpenSim/Tools/pCampBot/PhysicsBot.cs
@@ -36,6 +36,7 @@ using log4net;
 using OpenMetaverse;
 using OpenMetaverse.Assets;
 using Nini.Config;
+using pCampBot.Interfaces;
 using OpenSim.Framework;
 using OpenSim.Framework.Console;
 using Timer = System.Timers.Timer;
@@ -52,6 +53,11 @@ namespace pCampBot
         private IConfig startupConfig; // bot config, passed from BotManager
 
         /// <summary>
+        /// Behaviours implemented by this bot.
+        /// </summary>
+        public List<IBehaviour> Behaviours { get; private set; }
+
+        /// <summary>
         /// Is this bot connected to the grid?
         /// </summary>
         public bool IsConnected { get; private set; }
@@ -74,25 +80,33 @@ namespace pCampBot
 
         protected List<uint> objectIDs = new List<uint>();
 
-        protected Random somthing = new Random(Environment.TickCount);// We do stuff randomly here
+        /// <summary>
+        /// Random number generator.
+        /// </summary>
+        public Random Random { get; private set; }
 
         /// <summary>
         /// New instance of a SecondLife client
         /// </summary>
-        public GridClient client = new GridClient();
-
-        protected string[] talkarray;
+        public GridClient Client { get; private set; }
 
         /// <summary>
         /// Constructor
         /// </summary>
         /// <param name="bm"></param>
+        /// <param name="behaviours">Behaviours for this bot to perform</param>
         /// <param name="firstName"></param>
         /// <param name="lastName"></param>
         /// <param name="password"></param>
         /// <param name="loginUri"></param>
-        public PhysicsBot(BotManager bm, string firstName, string lastName, string password, string loginUri)
+        /// <param name="behaviours"></param>
+        public PhysicsBot(
+            BotManager bm, List<IBehaviour> behaviours,
+            string firstName, string lastName, string password, string loginUri)
         {
+            Client = new GridClient();
+            
+            Random = new Random(Environment.TickCount);// We do stuff randomly here
             FirstName = firstName;
             LastName = lastName;
             Name = string.Format("{0} {1}", FirstName, LastName);
@@ -102,7 +116,8 @@ namespace pCampBot
             BotManager = bm;
             startupConfig = bm.Config;
             readconfig();
-            talkarray = readexcuses();
+
+            Behaviours = behaviours;
         }
 
         //We do our actions here.  This is where one would
@@ -110,34 +125,7 @@ namespace pCampBot
         private void Action()
         {
             while (true)
-            {
-                int walkorrun = somthing.Next(4); // Randomize between walking and running. The greater this number,
-                                                  // the greater the bot's chances to walk instead of run.
-                client.Self.Jump(false);
-                if (walkorrun == 0)
-                {
-                    client.Self.Movement.AlwaysRun = true;
-                }
-                else
-                {
-                    client.Self.Movement.AlwaysRun = false;
-                }
-
-                // TODO: unused: Vector3 pos = client.Self.SimPosition;
-                Vector3 newpos = new Vector3(somthing.Next(1, 254), somthing.Next(1, 254), somthing.Next(1, 254));
-                client.Self.Movement.TurnToward(newpos);
-
-                client.Self.Movement.AtPos = true;
-                Thread.Sleep(somthing.Next(3000, 13000));
-                client.Self.Movement.AtPos = false;
-                client.Self.Jump(true);
-
-                string randomf = talkarray[somthing.Next(talkarray.Length)];
-                if (talkarray.Length > 1 && randomf.Length > 1)
-                    client.Self.Chat(randomf, 0, ChatType.Normal);
-
-                Thread.Sleep(somthing.Next(1000, 10000));
-            }
+                Behaviours.ForEach(b => b.Action(this));
         }
 
         /// <summary>
@@ -145,7 +133,7 @@ namespace pCampBot
         /// </summary>
         public void readconfig()
         {
-            wear = startupConfig.GetString("wear","no");
+            wear = startupConfig.GetString("wear", "no");
         }
 
         /// <summary>
@@ -156,7 +144,7 @@ namespace pCampBot
             if (m_actionThread != null)
                 m_actionThread.Abort();
 
-            client.Network.Logout();
+            Client.Network.Logout();
         }
 
         /// <summary>
@@ -164,50 +152,50 @@ namespace pCampBot
         /// </summary>
         public void startup()
         {
-            client.Settings.LOGIN_SERVER = LoginUri;
-            client.Settings.ALWAYS_DECODE_OBJECTS = false;
-            client.Settings.AVATAR_TRACKING = false;
-            client.Settings.OBJECT_TRACKING = false;
-            client.Settings.SEND_AGENT_THROTTLE = true;
-            client.Settings.SEND_PINGS = true;
-            client.Settings.STORE_LAND_PATCHES = false;
-            client.Settings.USE_ASSET_CACHE = false;
-            client.Settings.MULTIPLE_SIMS = true;
-            client.Throttle.Asset = 100000;
-            client.Throttle.Land = 100000;
-            client.Throttle.Task = 100000;
-            client.Throttle.Texture = 100000;
-            client.Throttle.Wind = 100000;
-            client.Throttle.Total = 400000;
-            client.Network.LoginProgress += this.Network_LoginProgress;
-            client.Network.SimConnected += this.Network_SimConnected;
-            client.Network.Disconnected += this.Network_OnDisconnected;
-            client.Objects.ObjectUpdate += Objects_NewPrim;
-
-            if (client.Network.Login(FirstName, LastName, Password, "pCampBot", "Your name"))
+            Client.Settings.LOGIN_SERVER = LoginUri;
+            Client.Settings.ALWAYS_DECODE_OBJECTS = false;
+            Client.Settings.AVATAR_TRACKING = false;
+            Client.Settings.OBJECT_TRACKING = false;
+            Client.Settings.SEND_AGENT_THROTTLE = true;
+            Client.Settings.SEND_PINGS = true;
+            Client.Settings.STORE_LAND_PATCHES = false;
+            Client.Settings.USE_ASSET_CACHE = false;
+            Client.Settings.MULTIPLE_SIMS = true;
+            Client.Throttle.Asset = 100000;
+            Client.Throttle.Land = 100000;
+            Client.Throttle.Task = 100000;
+            Client.Throttle.Texture = 100000;
+            Client.Throttle.Wind = 100000;
+            Client.Throttle.Total = 400000;
+            Client.Network.LoginProgress += this.Network_LoginProgress;
+            Client.Network.SimConnected += this.Network_SimConnected;
+            Client.Network.Disconnected += this.Network_OnDisconnected;
+            Client.Objects.ObjectUpdate += Objects_NewPrim;
+
+            if (Client.Network.Login(FirstName, LastName, Password, "pCampBot", "Your name"))
             {
                 IsConnected = true;
 
-                Thread.Sleep(somthing.Next(1000, 10000));
+                Thread.Sleep(Random.Next(1000, 10000));
                 m_actionThread = new Thread(Action);
                 m_actionThread.Start();
 
 //                    OnConnected(this, EventType.CONNECTED);
                 if (wear == "save")
                 {
-                    client.Appearance.SetPreviousAppearance();
+                    Client.Appearance.SetPreviousAppearance();
                     SaveDefaultAppearance();
                 }
                 else if (wear != "no")
                 {
                     MakeDefaultAppearance(wear);
                 }
-                client.Self.Jump(true);
+                Client.Self.Jump(true);
             }
             else
             {
                 MainConsole.Instance.OutputFormat(
-                    "{0} {1} cannot login: {2}", FirstName, LastName, client.Network.LoginMessage);
+                    "{0} {1} cannot login: {2}", FirstName, LastName, Client.Network.LoginMessage);
 
                 if (OnDisconnected != null)
                 {
@@ -227,11 +215,11 @@ namespace pCampBot
             Array wtypes = Enum.GetValues(typeof(WearableType));
             foreach (WearableType wtype in wtypes)
             {
-                UUID wearable = client.Appearance.GetWearableAsset(wtype);
+                UUID wearable = Client.Appearance.GetWearableAsset(wtype);
                 if (wearable != UUID.Zero)
                 {
-                    client.Assets.RequestAsset(wearable, AssetType.Clothing, false, Asset_ReceivedCallback);
-                    client.Assets.RequestAsset(wearable, AssetType.Bodypart, false, Asset_ReceivedCallback);
+                    Client.Assets.RequestAsset(wearable, AssetType.Clothing, false, Asset_ReceivedCallback);
+                    Client.Assets.RequestAsset(wearable, AssetType.Bodypart, false, Asset_ReceivedCallback);
                 }
             }
         }
@@ -306,11 +294,11 @@ namespace pCampBot
                     UUID assetID = UUID.Random();
                     AssetClothing asset = new AssetClothing(assetID, File.ReadAllBytes(clothing[i]));
                     asset.Decode();
-                    asset.Owner = client.Self.AgentID;
+                    asset.Owner = Client.Self.AgentID;
                     asset.WearableType = GetWearableType(clothing[i]);
                     asset.Encode();
-                    transid = client.Assets.RequestUpload(asset,true);
-                    client.Inventory.RequestCreateItem(clothfolder.UUID, "MyClothing" + i.ToString(), "MyClothing", AssetType.Clothing,
+                    transid = Client.Assets.RequestUpload(asset,true);
+                    Client.Inventory.RequestCreateItem(clothfolder.UUID, "MyClothing" + i.ToString(), "MyClothing", AssetType.Clothing,
                          transid, InventoryType.Wearable, asset.WearableType, PermissionMask.All, delegate(bool success, InventoryItem item)
                     {
                         if (success)
@@ -328,11 +316,11 @@ namespace pCampBot
                     UUID assetID = UUID.Random();
                     AssetBodypart asset = new AssetBodypart(assetID, File.ReadAllBytes(bodyparts[i]));
                     asset.Decode();
-                    asset.Owner = client.Self.AgentID;
+                    asset.Owner = Client.Self.AgentID;
                     asset.WearableType = GetWearableType(bodyparts[i]);
                     asset.Encode();
-                    transid = client.Assets.RequestUpload(asset,true);
-                    client.Inventory.RequestCreateItem(clothfolder.UUID, "MyBodyPart" + i.ToString(), "MyBodyPart", AssetType.Bodypart,
+                    transid = Client.Assets.RequestUpload(asset,true);
+                    Client.Inventory.RequestCreateItem(clothfolder.UUID, "MyBodyPart" + i.ToString(), "MyBodyPart", AssetType.Bodypart,
                          transid, InventoryType.Wearable, asset.WearableType, PermissionMask.All, delegate(bool success, InventoryItem item)
                     {
                         if (success)
@@ -352,7 +340,7 @@ namespace pCampBot
                 else
                 {
                     MainConsole.Instance.Output(String.Format("Sending {0} wearables...",listwearables.Count));
-                    client.Appearance.WearOutfit(listwearables, false);
+                    Client.Appearance.WearOutfit(listwearables, false);
                 }
             }
             catch (Exception ex)
@@ -363,8 +351,8 @@ namespace pCampBot
 
         public InventoryFolder FindClothingFolder()
         {
-            UUID rootfolder = client.Inventory.Store.RootFolder.UUID;
-            List<InventoryBase> listfolders = client.Inventory.Store.GetContents(rootfolder);
+            UUID rootfolder = Client.Inventory.Store.RootFolder.UUID;
+            List<InventoryBase> listfolders = Client.Inventory.Store.GetContents(rootfolder);
             InventoryFolder clothfolder = new InventoryFolder(UUID.Random());
             foreach (InventoryBase folder in listfolders)
             {
@@ -453,10 +441,9 @@ namespace pCampBot
                     return;
 
                 BotManager.AssetsReceived[textureID] = false;
-                client.Assets.RequestImage(textureID, ImageType.Normal, Asset_TextureCallback_Texture);
+                Client.Assets.RequestImage(textureID, ImageType.Normal, Asset_TextureCallback_Texture);
             }
         }
-
         
         public void Asset_TextureCallback_Texture(TextureRequestState state, AssetTexture assetTexture)
         {
@@ -473,20 +460,5 @@ namespace pCampBot
 //                SaveAsset((AssetWearable) asset);
 //            }
         }
-
-        public string[] readexcuses()
-        {
-            string allexcuses = "";
-
-            string file = Path.Combine(Util.configDir(), "pCampBotSentences.txt");
-            if (File.Exists(file))
-            {
-                StreamReader csr = File.OpenText(file);
-                allexcuses = csr.ReadToEnd();
-                csr.Close();
-            }
-
-            return allexcuses.Split(Environment.NewLine.ToCharArray());
-        }
     }
 }
diff --git a/OpenSim/Tools/pCampBot/pCampBot.cs b/OpenSim/Tools/pCampBot/pCampBot.cs
index a69fbf0..cae96e1 100644
--- a/OpenSim/Tools/pCampBot/pCampBot.cs
+++ b/OpenSim/Tools/pCampBot/pCampBot.cs
@@ -26,6 +26,8 @@
  */
 
 using System;
+using System.Reflection;
+using log4net;
 using Nini.Config;
 using OpenSim.Framework;
 using OpenSim.Framework.Console;
@@ -44,6 +46,8 @@ namespace pCampBot
 
     public class pCampBot
     {
+        private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
+
         [STAThread]
         public static void Main(string[] args)
         {
@@ -60,9 +64,17 @@ namespace pCampBot
 
                 //startup specified number of bots.  1 is the default
                 bm.dobotStartup(botcount, config);
+
                 while (true)
                 {
-                    MainConsole.Instance.Prompt();
+                    try
+                    {
+                        MainConsole.Instance.Prompt();
+                    }
+                    catch (Exception e)
+                    {
+                        m_log.ErrorFormat("Command error: {0}", e);
+                    }
                 }
             }
         }
-- 
cgit v1.1