Separate out physics testing actions into a separate PhysicsBehaviour class

remove-scene-viewer
Justin Clark-Casey (justincc) 2011-11-03 21:16:24 +00:00
parent a62ccb5c4c
commit 66c60c56a0
5 changed files with 220 additions and 94 deletions

View File

@ -37,6 +37,7 @@ using log4net.Repository;
using Nini.Config; using Nini.Config;
using OpenSim.Framework; using OpenSim.Framework;
using OpenSim.Framework.Console; using OpenSim.Framework.Console;
using pCampBot.Interfaces;
namespace pCampBot namespace pCampBot
{ {
@ -119,10 +120,14 @@ namespace pCampBot
string password = cs.GetString("password"); string password = cs.GetString("password");
string loginUri = cs.GetString("loginuri"); 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++) for (int i = 0; i < botcount; i++)
{ {
string lastName = string.Format("{0}_{1}", lastNameStem, 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 /// This starts up the bot and stores the thread for the bot in the thread array
/// </summary> /// </summary>
/// <param name="pos">The position in the thread array to stick the bot's thread</param> /// <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="firstName">First name</param>
/// <param name="lastName">Last name</param> /// <param name="lastName">Last name</param>
/// <param name="password">Password</param> /// <param name="password">Password</param>
/// <param name="loginUri">Login URI</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.OnConnected += handlebotEvent;
pb.OnDisconnected += handlebotEvent; pb.OnDisconnected += handlebotEvent;

View File

@ -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);
}
}

View File

@ -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());
}
}
}

View File

@ -36,6 +36,7 @@ using log4net;
using OpenMetaverse; using OpenMetaverse;
using OpenMetaverse.Assets; using OpenMetaverse.Assets;
using Nini.Config; using Nini.Config;
using pCampBot.Interfaces;
using OpenSim.Framework; using OpenSim.Framework;
using OpenSim.Framework.Console; using OpenSim.Framework.Console;
using Timer = System.Timers.Timer; using Timer = System.Timers.Timer;
@ -51,6 +52,11 @@ namespace pCampBot
public BotManager BotManager { get; private set; } public BotManager BotManager { get; private set; }
private IConfig startupConfig; // bot config, passed from BotManager private IConfig startupConfig; // bot config, passed from BotManager
/// <summary>
/// Behaviours implemented by this bot.
/// </summary>
public List<IBehaviour> Behaviours { get; private set; }
/// <summary> /// <summary>
/// Is this bot connected to the grid? /// Is this bot connected to the grid?
/// </summary> /// </summary>
@ -74,25 +80,33 @@ namespace pCampBot
protected List<uint> objectIDs = new List<uint>(); 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> /// <summary>
/// New instance of a SecondLife client /// New instance of a SecondLife client
/// </summary> /// </summary>
public GridClient client = new GridClient(); public GridClient Client { get; private set; }
protected string[] talkarray;
/// <summary> /// <summary>
/// Constructor /// Constructor
/// </summary> /// </summary>
/// <param name="bm"></param> /// <param name="bm"></param>
/// <param name="behaviours">Behaviours for this bot to perform</param>
/// <param name="firstName"></param> /// <param name="firstName"></param>
/// <param name="lastName"></param> /// <param name="lastName"></param>
/// <param name="password"></param> /// <param name="password"></param>
/// <param name="loginUri"></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; FirstName = firstName;
LastName = lastName; LastName = lastName;
Name = string.Format("{0} {1}", FirstName, LastName); Name = string.Format("{0} {1}", FirstName, LastName);
@ -102,7 +116,8 @@ namespace pCampBot
BotManager = bm; BotManager = bm;
startupConfig = bm.Config; startupConfig = bm.Config;
readconfig(); readconfig();
talkarray = readexcuses();
Behaviours = behaviours;
} }
//We do our actions here. This is where one would //We do our actions here. This is where one would
@ -110,34 +125,7 @@ namespace pCampBot
private void Action() private void Action()
{ {
while (true) while (true)
{ Behaviours.ForEach(b => b.Action(this));
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));
}
} }
/// <summary> /// <summary>
@ -156,7 +144,7 @@ namespace pCampBot
if (m_actionThread != null) if (m_actionThread != null)
m_actionThread.Abort(); m_actionThread.Abort();
client.Network.Logout(); Client.Network.Logout();
} }
/// <summary> /// <summary>
@ -164,50 +152,50 @@ namespace pCampBot
/// </summary> /// </summary>
public void startup() public void startup()
{ {
client.Settings.LOGIN_SERVER = LoginUri; Client.Settings.LOGIN_SERVER = LoginUri;
client.Settings.ALWAYS_DECODE_OBJECTS = false; Client.Settings.ALWAYS_DECODE_OBJECTS = false;
client.Settings.AVATAR_TRACKING = false; Client.Settings.AVATAR_TRACKING = false;
client.Settings.OBJECT_TRACKING = false; Client.Settings.OBJECT_TRACKING = false;
client.Settings.SEND_AGENT_THROTTLE = true; Client.Settings.SEND_AGENT_THROTTLE = true;
client.Settings.SEND_PINGS = true; Client.Settings.SEND_PINGS = true;
client.Settings.STORE_LAND_PATCHES = false; Client.Settings.STORE_LAND_PATCHES = false;
client.Settings.USE_ASSET_CACHE = false; Client.Settings.USE_ASSET_CACHE = false;
client.Settings.MULTIPLE_SIMS = true; Client.Settings.MULTIPLE_SIMS = true;
client.Throttle.Asset = 100000; Client.Throttle.Asset = 100000;
client.Throttle.Land = 100000; Client.Throttle.Land = 100000;
client.Throttle.Task = 100000; Client.Throttle.Task = 100000;
client.Throttle.Texture = 100000; Client.Throttle.Texture = 100000;
client.Throttle.Wind = 100000; Client.Throttle.Wind = 100000;
client.Throttle.Total = 400000; Client.Throttle.Total = 400000;
client.Network.LoginProgress += this.Network_LoginProgress; Client.Network.LoginProgress += this.Network_LoginProgress;
client.Network.SimConnected += this.Network_SimConnected; Client.Network.SimConnected += this.Network_SimConnected;
client.Network.Disconnected += this.Network_OnDisconnected; Client.Network.Disconnected += this.Network_OnDisconnected;
client.Objects.ObjectUpdate += Objects_NewPrim; Client.Objects.ObjectUpdate += Objects_NewPrim;
if (client.Network.Login(FirstName, LastName, Password, "pCampBot", "Your name")) if (Client.Network.Login(FirstName, LastName, Password, "pCampBot", "Your name"))
{ {
IsConnected = true; IsConnected = true;
Thread.Sleep(somthing.Next(1000, 10000)); Thread.Sleep(Random.Next(1000, 10000));
m_actionThread = new Thread(Action); m_actionThread = new Thread(Action);
m_actionThread.Start(); m_actionThread.Start();
// OnConnected(this, EventType.CONNECTED); // OnConnected(this, EventType.CONNECTED);
if (wear == "save") if (wear == "save")
{ {
client.Appearance.SetPreviousAppearance(); Client.Appearance.SetPreviousAppearance();
SaveDefaultAppearance(); SaveDefaultAppearance();
} }
else if (wear != "no") else if (wear != "no")
{ {
MakeDefaultAppearance(wear); MakeDefaultAppearance(wear);
} }
client.Self.Jump(true); Client.Self.Jump(true);
} }
else else
{ {
MainConsole.Instance.OutputFormat( 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) if (OnDisconnected != null)
{ {
@ -227,11 +215,11 @@ namespace pCampBot
Array wtypes = Enum.GetValues(typeof(WearableType)); Array wtypes = Enum.GetValues(typeof(WearableType));
foreach (WearableType wtype in wtypes) foreach (WearableType wtype in wtypes)
{ {
UUID wearable = client.Appearance.GetWearableAsset(wtype); UUID wearable = Client.Appearance.GetWearableAsset(wtype);
if (wearable != UUID.Zero) if (wearable != UUID.Zero)
{ {
client.Assets.RequestAsset(wearable, AssetType.Clothing, false, Asset_ReceivedCallback); Client.Assets.RequestAsset(wearable, AssetType.Clothing, false, Asset_ReceivedCallback);
client.Assets.RequestAsset(wearable, AssetType.Bodypart, false, Asset_ReceivedCallback); Client.Assets.RequestAsset(wearable, AssetType.Bodypart, false, Asset_ReceivedCallback);
} }
} }
} }
@ -306,11 +294,11 @@ namespace pCampBot
UUID assetID = UUID.Random(); UUID assetID = UUID.Random();
AssetClothing asset = new AssetClothing(assetID, File.ReadAllBytes(clothing[i])); AssetClothing asset = new AssetClothing(assetID, File.ReadAllBytes(clothing[i]));
asset.Decode(); asset.Decode();
asset.Owner = client.Self.AgentID; asset.Owner = Client.Self.AgentID;
asset.WearableType = GetWearableType(clothing[i]); asset.WearableType = GetWearableType(clothing[i]);
asset.Encode(); asset.Encode();
transid = client.Assets.RequestUpload(asset,true); transid = Client.Assets.RequestUpload(asset,true);
client.Inventory.RequestCreateItem(clothfolder.UUID, "MyClothing" + i.ToString(), "MyClothing", AssetType.Clothing, Client.Inventory.RequestCreateItem(clothfolder.UUID, "MyClothing" + i.ToString(), "MyClothing", AssetType.Clothing,
transid, InventoryType.Wearable, asset.WearableType, PermissionMask.All, delegate(bool success, InventoryItem item) transid, InventoryType.Wearable, asset.WearableType, PermissionMask.All, delegate(bool success, InventoryItem item)
{ {
if (success) if (success)
@ -328,11 +316,11 @@ namespace pCampBot
UUID assetID = UUID.Random(); UUID assetID = UUID.Random();
AssetBodypart asset = new AssetBodypart(assetID, File.ReadAllBytes(bodyparts[i])); AssetBodypart asset = new AssetBodypart(assetID, File.ReadAllBytes(bodyparts[i]));
asset.Decode(); asset.Decode();
asset.Owner = client.Self.AgentID; asset.Owner = Client.Self.AgentID;
asset.WearableType = GetWearableType(bodyparts[i]); asset.WearableType = GetWearableType(bodyparts[i]);
asset.Encode(); asset.Encode();
transid = client.Assets.RequestUpload(asset,true); transid = Client.Assets.RequestUpload(asset,true);
client.Inventory.RequestCreateItem(clothfolder.UUID, "MyBodyPart" + i.ToString(), "MyBodyPart", AssetType.Bodypart, Client.Inventory.RequestCreateItem(clothfolder.UUID, "MyBodyPart" + i.ToString(), "MyBodyPart", AssetType.Bodypart,
transid, InventoryType.Wearable, asset.WearableType, PermissionMask.All, delegate(bool success, InventoryItem item) transid, InventoryType.Wearable, asset.WearableType, PermissionMask.All, delegate(bool success, InventoryItem item)
{ {
if (success) if (success)
@ -352,7 +340,7 @@ namespace pCampBot
else else
{ {
MainConsole.Instance.Output(String.Format("Sending {0} wearables...",listwearables.Count)); MainConsole.Instance.Output(String.Format("Sending {0} wearables...",listwearables.Count));
client.Appearance.WearOutfit(listwearables, false); Client.Appearance.WearOutfit(listwearables, false);
} }
} }
catch (Exception ex) catch (Exception ex)
@ -363,8 +351,8 @@ namespace pCampBot
public InventoryFolder FindClothingFolder() public InventoryFolder FindClothingFolder()
{ {
UUID rootfolder = client.Inventory.Store.RootFolder.UUID; UUID rootfolder = Client.Inventory.Store.RootFolder.UUID;
List<InventoryBase> listfolders = client.Inventory.Store.GetContents(rootfolder); List<InventoryBase> listfolders = Client.Inventory.Store.GetContents(rootfolder);
InventoryFolder clothfolder = new InventoryFolder(UUID.Random()); InventoryFolder clothfolder = new InventoryFolder(UUID.Random());
foreach (InventoryBase folder in listfolders) foreach (InventoryBase folder in listfolders)
{ {
@ -453,11 +441,10 @@ namespace pCampBot
return; return;
BotManager.AssetsReceived[textureID] = false; 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) public void Asset_TextureCallback_Texture(TextureRequestState state, AssetTexture assetTexture)
{ {
//TODO: Implement texture saving and applying //TODO: Implement texture saving and applying
@ -473,20 +460,5 @@ namespace pCampBot
// SaveAsset((AssetWearable) asset); // 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());
}
} }
} }

View File

@ -26,6 +26,8 @@
*/ */
using System; using System;
using System.Reflection;
using log4net;
using Nini.Config; using Nini.Config;
using OpenSim.Framework; using OpenSim.Framework;
using OpenSim.Framework.Console; using OpenSim.Framework.Console;
@ -44,6 +46,8 @@ namespace pCampBot
public class pCampBot public class pCampBot
{ {
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
[STAThread] [STAThread]
public static void Main(string[] args) public static void Main(string[] args)
{ {
@ -60,10 +64,18 @@ namespace pCampBot
//startup specified number of bots. 1 is the default //startup specified number of bots. 1 is the default
bm.dobotStartup(botcount, config); bm.dobotStartup(botcount, config);
while (true) while (true)
{
try
{ {
MainConsole.Instance.Prompt(); MainConsole.Instance.Prompt();
} }
catch (Exception e)
{
m_log.ErrorFormat("Command error: {0}", e);
}
}
} }
} }