From 2ae5b40ca6bb3d33310f0f66569bf150548a2de6 Mon Sep 17 00:00:00 2001 From: Justin Clark-Casey (justincc) Date: Thu, 24 Nov 2011 22:36:45 +0000 Subject: On pCampBot, add bot as a property on behaviours instead of passing it in every time --- .../Tools/pCampBot/Behaviours/AbstractBehaviour.cs | 49 ++++++++++++++++++++++ .../Tools/pCampBot/Behaviours/GrabbingBehaviour.cs | 16 +++---- .../Tools/pCampBot/Behaviours/PhysicsBehaviour.cs | 31 +++++++------- .../Tools/pCampBot/Behaviours/TeleportBehaviour.cs | 22 +++++----- 4 files changed, 83 insertions(+), 35 deletions(-) create mode 100644 OpenSim/Tools/pCampBot/Behaviours/AbstractBehaviour.cs (limited to 'OpenSim/Tools/pCampBot/Behaviours') diff --git a/OpenSim/Tools/pCampBot/Behaviours/AbstractBehaviour.cs b/OpenSim/Tools/pCampBot/Behaviours/AbstractBehaviour.cs new file mode 100644 index 0000000..9a9371d --- /dev/null +++ b/OpenSim/Tools/pCampBot/Behaviours/AbstractBehaviour.cs @@ -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; + } + } +} diff --git a/OpenSim/Tools/pCampBot/Behaviours/GrabbingBehaviour.cs b/OpenSim/Tools/pCampBot/Behaviours/GrabbingBehaviour.cs index 0a6d5d2..6ad02b2 100644 --- a/OpenSim/Tools/pCampBot/Behaviours/GrabbingBehaviour.cs +++ b/OpenSim/Tools/pCampBot/Behaviours/GrabbingBehaviour.cs @@ -39,20 +39,20 @@ namespace pCampBot /// /// The viewer itself does not give the option of grabbing objects that haven't been signalled as grabbable. /// - 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 objects = bot.Objects; + Dictionary 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); } } } \ No newline at end of file diff --git a/OpenSim/Tools/pCampBot/Behaviours/PhysicsBehaviour.cs b/OpenSim/Tools/pCampBot/Behaviours/PhysicsBehaviour.cs index 25529d0..daa7485 100644 --- a/OpenSim/Tools/pCampBot/Behaviours/PhysicsBehaviour.cs +++ b/OpenSim/Tools/pCampBot/Behaviours/PhysicsBehaviour.cs @@ -40,42 +40,41 @@ namespace pCampBot /// /// TODO: talkarray should be in a separate behaviour. /// - 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() diff --git a/OpenSim/Tools/pCampBot/Behaviours/TeleportBehaviour.cs b/OpenSim/Tools/pCampBot/Behaviours/TeleportBehaviour.cs index fc2ed2c..fbb4e96 100644 --- a/OpenSim/Tools/pCampBot/Behaviours/TeleportBehaviour.cs +++ b/OpenSim/Tools/pCampBot/Behaviours/TeleportBehaviour.cs @@ -38,38 +38,38 @@ namespace pCampBot /// /// Teleport to a random region on the grid. /// - 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); } } } \ No newline at end of file -- cgit v1.1