On pCampBot, add bot as a property on behaviours instead of passing it in every time

iar_mods
Justin Clark-Casey (justincc) 2011-11-24 22:36:45 +00:00
parent cbbd992df4
commit 2ae5b40ca6
7 changed files with 115 additions and 56 deletions

View File

@ -0,0 +1,49 @@
/*
* 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 OpenMetaverse;
using System;
using System.Collections.Generic;
using System.Linq;
using pCampBot.Interfaces;
namespace pCampBot
{
public class AbstractBehaviour : IBehaviour
{
public string Name { get; protected set; }
public Bot Bot { get; protected set; }
public virtual void Action() {}
public virtual void Initialize(Bot bot)
{
Bot = bot;
}
}
}

View File

@ -39,20 +39,20 @@ namespace pCampBot
/// <remarks> /// <remarks>
/// The viewer itself does not give the option of grabbing objects that haven't been signalled as grabbable. /// The viewer itself does not give the option of grabbing objects that haven't been signalled as grabbable.
/// </remarks> /// </remarks>
public class GrabbingBehaviour : IBehaviour public class GrabbingBehaviour : AbstractBehaviour
{ {
public string Name { get { return "Grabbing"; } } public GrabbingBehaviour() { Name = "Grabbing"; }
public void Action(Bot bot) public override void Action()
{ {
Dictionary<UUID, Primitive> objects = bot.Objects; Dictionary<UUID, Primitive> objects = Bot.Objects;
Primitive prim = objects.ElementAt(bot.Random.Next(0, objects.Count)).Value; Primitive prim = objects.ElementAt(Bot.Random.Next(0, objects.Count)).Value;
// This appears to be a typical message sent when a viewer user clicks a clickable object // This appears to be a typical message sent when a viewer user clicks a clickable object
bot.Client.Self.Grab(prim.LocalID); Bot.Client.Self.Grab(prim.LocalID);
bot.Client.Self.GrabUpdate(prim.ID, Vector3.Zero); Bot.Client.Self.GrabUpdate(prim.ID, Vector3.Zero);
bot.Client.Self.DeGrab(prim.LocalID); Bot.Client.Self.DeGrab(prim.LocalID);
} }
} }
} }

View File

@ -40,42 +40,41 @@ namespace pCampBot
/// <remarks> /// <remarks>
/// TODO: talkarray should be in a separate behaviour. /// TODO: talkarray should be in a separate behaviour.
/// </remarks> /// </remarks>
public class PhysicsBehaviour : IBehaviour public class PhysicsBehaviour : AbstractBehaviour
{ {
public string Name { get { return "Physics"; } }
private string[] talkarray; private string[] talkarray;
public PhysicsBehaviour() public PhysicsBehaviour()
{ {
Name = "Physics";
talkarray = readexcuses(); talkarray = readexcuses();
} }
public void Action(Bot bot) public override void Action()
{ {
int walkorrun = bot.Random.Next(4); // Randomize between walking and running. The greater this number, 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. // the greater the bot's chances to walk instead of run.
bot.Client.Self.Jump(false); Bot.Client.Self.Jump(false);
if (walkorrun == 0) if (walkorrun == 0)
{ {
bot.Client.Self.Movement.AlwaysRun = true; Bot.Client.Self.Movement.AlwaysRun = true;
} }
else else
{ {
bot.Client.Self.Movement.AlwaysRun = false; Bot.Client.Self.Movement.AlwaysRun = false;
} }
// TODO: unused: Vector3 pos = client.Self.SimPosition; // 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)); 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.TurnToward(newpos);
bot.Client.Self.Movement.AtPos = true; Bot.Client.Self.Movement.AtPos = true;
Thread.Sleep(bot.Random.Next(3000, 13000)); Thread.Sleep(Bot.Random.Next(3000, 13000));
bot.Client.Self.Movement.AtPos = false; Bot.Client.Self.Movement.AtPos = false;
bot.Client.Self.Jump(true); Bot.Client.Self.Jump(true);
string randomf = talkarray[bot.Random.Next(talkarray.Length)]; string randomf = talkarray[Bot.Random.Next(talkarray.Length)];
if (talkarray.Length > 1 && randomf.Length > 1) if (talkarray.Length > 1 && randomf.Length > 1)
bot.Client.Self.Chat(randomf, 0, ChatType.Normal); Bot.Client.Self.Chat(randomf, 0, ChatType.Normal);
} }
private string[] readexcuses() private string[] readexcuses()

View File

@ -38,38 +38,38 @@ namespace pCampBot
/// <summary> /// <summary>
/// Teleport to a random region on the grid. /// Teleport to a random region on the grid.
/// </summary> /// </summary>
public class TeleportBehaviour : IBehaviour public class TeleportBehaviour : AbstractBehaviour
{ {
private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
public string Name { get { return "Teleport"; } } public TeleportBehaviour() { Name = "Teleport"; }
public void Action(Bot bot) public override void Action()
{ {
Random rng = bot.Manager.Rng; Random rng = Bot.Manager.Rng;
GridRegion[] knownRegions; GridRegion[] knownRegions;
lock (bot.Manager.RegionsKnown) lock (Bot.Manager.RegionsKnown)
{ {
if (bot.Manager.RegionsKnown.Count == 0) if (Bot.Manager.RegionsKnown.Count == 0)
{ {
m_log.DebugFormat( m_log.DebugFormat(
"[TELEPORT BEHAVIOUR]: Ignoring teleport action for {0} since no regions are known yet", bot.Name); "[TELEPORT BEHAVIOUR]: Ignoring teleport action for {0} since no regions are known yet", Bot.Name);
return; return;
} }
knownRegions = bot.Manager.RegionsKnown.Values.ToArray(); knownRegions = Bot.Manager.RegionsKnown.Values.ToArray();
} }
Simulator sourceRegion = bot.Client.Network.CurrentSim; Simulator sourceRegion = Bot.Client.Network.CurrentSim;
GridRegion destRegion = knownRegions[rng.Next(knownRegions.Length)]; GridRegion destRegion = knownRegions[rng.Next(knownRegions.Length)];
Vector3 destPosition = new Vector3(rng.Next(255), rng.Next(255), 50); Vector3 destPosition = new Vector3(rng.Next(255), rng.Next(255), 50);
m_log.DebugFormat( m_log.DebugFormat(
"[TELEPORT BEHAVIOUR]: Teleporting {0} from {1} {2} to {3} {4}", "[TELEPORT BEHAVIOUR]: Teleporting {0} from {1} {2} to {3} {4}",
bot.Name, sourceRegion.Name, bot.Client.Self.SimPosition, destRegion.Name, destPosition); Bot.Name, sourceRegion.Name, Bot.Client.Self.SimPosition, destRegion.Name, destPosition);
bot.Client.Self.Teleport(destRegion.RegionHandle, destPosition); Bot.Client.Self.Teleport(destRegion.RegionHandle, destPosition);
} }
} }
} }

View File

@ -130,6 +130,8 @@ namespace pCampBot
BotManager bm, List<IBehaviour> behaviours, BotManager bm, List<IBehaviour> behaviours,
string firstName, string lastName, string password, string loginUri) string firstName, string lastName, string password, string loginUri)
{ {
behaviours.ForEach(b => b.Initialize(this));
Client = new GridClient(); Client = new GridClient();
Random = new Random(Environment.TickCount);// We do stuff randomly here Random = new Random(Environment.TickCount);// We do stuff randomly here
@ -156,7 +158,7 @@ namespace pCampBot
b => b =>
{ {
// m_log.DebugFormat("[pCAMPBOT]: For {0} performing action {1}", Name, b.GetType()); // m_log.DebugFormat("[pCAMPBOT]: For {0} performing action {1}", Name, b.GetType());
b.Action(this); b.Action();
Thread.Sleep(Random.Next(1000, 10000)); Thread.Sleep(Random.Next(1000, 10000));
} }

View File

@ -151,6 +151,10 @@ namespace pCampBot
Array.ForEach<string>( Array.ForEach<string>(
cs.GetString("behaviours", "p").Split(new char[] { ',' }), b => behaviourSwitches.Add(b)); cs.GetString("behaviours", "p").Split(new char[] { ',' }), b => behaviourSwitches.Add(b));
for (int i = 0; i < botcount; i++)
{
string lastName = string.Format("{0}_{1}", lastNameStem, i);
List<IBehaviour> behaviours = new List<IBehaviour>(); List<IBehaviour> behaviours = new List<IBehaviour>();
// Hard-coded for now // Hard-coded for now
@ -163,16 +167,12 @@ namespace pCampBot
if (behaviourSwitches.Contains("t")) if (behaviourSwitches.Contains("t"))
behaviours.Add(new TeleportBehaviour()); behaviours.Add(new TeleportBehaviour());
// if (behaviourSwitches.Contains("c")) if (behaviourSwitches.Contains("c"))
// behaviours.Add(new CrossBehaviour()); behaviours.Add(new CrossBehaviour());
MainConsole.Instance.OutputFormat( MainConsole.Instance.OutputFormat(
"[BOT MANAGER]: Bots configured for behaviours {0}", "[BOT MANAGER]: Bot {0} {1} configured for behaviours {2}",
string.Join(",", behaviours.ConvertAll<string>(b => b.Name).ToArray())); firstName, lastName, string.Join(",", behaviours.ConvertAll<string>(b => b.Name).ToArray()));
for (int i = 0; i < botcount; i++)
{
string lastName = string.Format("{0}_{1}", lastNameStem, i);
StartBot(this, behaviours, firstName, lastName, password, loginUri); StartBot(this, behaviours, firstName, lastName, password, loginUri);
} }

View File

@ -36,10 +36,19 @@ namespace pCampBot.Interfaces
/// </summary> /// </summary>
string Name { get; } string Name { get; }
/// <summary>
/// Initialize the behaviour for this bot.
/// </summary>
/// <remarks>
/// This must be invoked before Action() is called.
/// </remarks>
/// <param name="bot"></param>
void Initialize(Bot bot);
/// <summary> /// <summary>
/// Action to take when this behaviour is invoked. /// Action to take when this behaviour is invoked.
/// </summary> /// </summary>
/// <param name="bot"></param> /// <param name="bot"></param>
void Action(Bot bot); void Action();
} }
} }