On pCampBot, add bot as a property on behaviours instead of passing it in every time
parent
cbbd992df4
commit
2ae5b40ca6
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -39,20 +39,20 @@ namespace pCampBot
|
|||
/// <remarks>
|
||||
/// The viewer itself does not give the option of grabbing objects that haven't been signalled as grabbable.
|
||||
/// </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
|
||||
bot.Client.Self.Grab(prim.LocalID);
|
||||
bot.Client.Self.GrabUpdate(prim.ID, Vector3.Zero);
|
||||
bot.Client.Self.DeGrab(prim.LocalID);
|
||||
Bot.Client.Self.Grab(prim.LocalID);
|
||||
Bot.Client.Self.GrabUpdate(prim.ID, Vector3.Zero);
|
||||
Bot.Client.Self.DeGrab(prim.LocalID);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -40,42 +40,41 @@ namespace pCampBot
|
|||
/// <remarks>
|
||||
/// TODO: talkarray should be in a separate behaviour.
|
||||
/// </remarks>
|
||||
public class PhysicsBehaviour : IBehaviour
|
||||
public class PhysicsBehaviour : AbstractBehaviour
|
||||
{
|
||||
public string Name { get { return "Physics"; } }
|
||||
|
||||
private string[] talkarray;
|
||||
|
||||
public PhysicsBehaviour()
|
||||
{
|
||||
Name = "Physics";
|
||||
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.
|
||||
bot.Client.Self.Jump(false);
|
||||
Bot.Client.Self.Jump(false);
|
||||
if (walkorrun == 0)
|
||||
{
|
||||
bot.Client.Self.Movement.AlwaysRun = true;
|
||||
Bot.Client.Self.Movement.AlwaysRun = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
bot.Client.Self.Movement.AlwaysRun = false;
|
||||
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);
|
||||
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)];
|
||||
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);
|
||||
Bot.Client.Self.Chat(randomf, 0, ChatType.Normal);
|
||||
}
|
||||
|
||||
private string[] readexcuses()
|
||||
|
|
|
@ -38,38 +38,38 @@ namespace pCampBot
|
|||
/// <summary>
|
||||
/// Teleport to a random region on the grid.
|
||||
/// </summary>
|
||||
public class TeleportBehaviour : IBehaviour
|
||||
public class TeleportBehaviour : AbstractBehaviour
|
||||
{
|
||||
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;
|
||||
|
||||
lock (bot.Manager.RegionsKnown)
|
||||
lock (Bot.Manager.RegionsKnown)
|
||||
{
|
||||
if (bot.Manager.RegionsKnown.Count == 0)
|
||||
if (Bot.Manager.RegionsKnown.Count == 0)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
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)];
|
||||
Vector3 destPosition = new Vector3(rng.Next(255), rng.Next(255), 50);
|
||||
|
||||
m_log.DebugFormat(
|
||||
"[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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -130,6 +130,8 @@ namespace pCampBot
|
|||
BotManager bm, List<IBehaviour> behaviours,
|
||||
string firstName, string lastName, string password, string loginUri)
|
||||
{
|
||||
behaviours.ForEach(b => b.Initialize(this));
|
||||
|
||||
Client = new GridClient();
|
||||
|
||||
Random = new Random(Environment.TickCount);// We do stuff randomly here
|
||||
|
@ -156,7 +158,7 @@ namespace pCampBot
|
|||
b =>
|
||||
{
|
||||
// m_log.DebugFormat("[pCAMPBOT]: For {0} performing action {1}", Name, b.GetType());
|
||||
b.Action(this);
|
||||
b.Action();
|
||||
|
||||
Thread.Sleep(Random.Next(1000, 10000));
|
||||
}
|
||||
|
|
|
@ -151,29 +151,29 @@ namespace pCampBot
|
|||
Array.ForEach<string>(
|
||||
cs.GetString("behaviours", "p").Split(new char[] { ',' }), b => behaviourSwitches.Add(b));
|
||||
|
||||
List<IBehaviour> behaviours = new List<IBehaviour>();
|
||||
|
||||
// Hard-coded for now
|
||||
if (behaviourSwitches.Contains("p"))
|
||||
behaviours.Add(new PhysicsBehaviour());
|
||||
|
||||
if (behaviourSwitches.Contains("g"))
|
||||
behaviours.Add(new GrabbingBehaviour());
|
||||
|
||||
if (behaviourSwitches.Contains("t"))
|
||||
behaviours.Add(new TeleportBehaviour());
|
||||
|
||||
// if (behaviourSwitches.Contains("c"))
|
||||
// behaviours.Add(new CrossBehaviour());
|
||||
|
||||
MainConsole.Instance.OutputFormat(
|
||||
"[BOT MANAGER]: Bots configured for behaviours {0}",
|
||||
string.Join(",", behaviours.ConvertAll<string>(b => b.Name).ToArray()));
|
||||
|
||||
for (int i = 0; i < botcount; i++)
|
||||
{
|
||||
string lastName = string.Format("{0}_{1}", lastNameStem, i);
|
||||
|
||||
List<IBehaviour> behaviours = new List<IBehaviour>();
|
||||
|
||||
// Hard-coded for now
|
||||
if (behaviourSwitches.Contains("p"))
|
||||
behaviours.Add(new PhysicsBehaviour());
|
||||
|
||||
if (behaviourSwitches.Contains("g"))
|
||||
behaviours.Add(new GrabbingBehaviour());
|
||||
|
||||
if (behaviourSwitches.Contains("t"))
|
||||
behaviours.Add(new TeleportBehaviour());
|
||||
|
||||
if (behaviourSwitches.Contains("c"))
|
||||
behaviours.Add(new CrossBehaviour());
|
||||
|
||||
MainConsole.Instance.OutputFormat(
|
||||
"[BOT MANAGER]: Bot {0} {1} configured for behaviours {2}",
|
||||
firstName, lastName, string.Join(",", behaviours.ConvertAll<string>(b => b.Name).ToArray()));
|
||||
|
||||
StartBot(this, behaviours, firstName, lastName, password, loginUri);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -36,10 +36,19 @@ namespace pCampBot.Interfaces
|
|||
/// </summary>
|
||||
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>
|
||||
/// Action to take when this behaviour is invoked.
|
||||
/// </summary>
|
||||
/// <param name="bot"></param>
|
||||
void Action(Bot bot);
|
||||
void Action();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue