123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Threading;
- using log4net;
- using OpenMetaverse;
- using OpenSim.Framework;
- using pCampBot.Interfaces;
- namespace pCampBot
- {
-
-
-
- public class CrossBehaviour : AbstractBehaviour
- {
- private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
- public AutoResetEvent m_regionCrossedMutex = new AutoResetEvent(false);
- public const int m_regionCrossingTimeout = 1000 * 60;
- public CrossBehaviour() { Name = "Cross"; }
- public override void Action()
- {
- GridClient client = Bot.Client;
-
- Simulator currentSim = client.Network.CurrentSim;
- ulong currentHandle = currentSim.Handle;
- uint currentX, currentY;
- Utils.LongToUInts(currentHandle, out currentX, out currentY);
- List<GridRegion> candidateRegions = new List<GridRegion>();
- TryAddRegion(Utils.UIntsToLong(Math.Max(0, currentX - Constants.RegionSize), currentY), candidateRegions);
- TryAddRegion(Utils.UIntsToLong(currentX + Constants.RegionSize, currentY), candidateRegions);
- TryAddRegion(Utils.UIntsToLong(currentX, Math.Max(0, currentY - Constants.RegionSize)), candidateRegions);
- TryAddRegion(Utils.UIntsToLong(currentX, currentY + Constants.RegionSize), candidateRegions);
- if (candidateRegions.Count != 0)
- {
- GridRegion destRegion = candidateRegions[Bot.Manager.Rng.Next(candidateRegions.Count)];
- uint targetX, targetY;
- Utils.LongToUInts(destRegion.RegionHandle, out targetX, out targetY);
- Vector3 pos = client.Self.SimPosition;
- if (targetX < currentX)
- pos.X = -1;
- else if (targetX > currentX)
- pos.X = Constants.RegionSize + 1;
- if (targetY < currentY)
- pos.Y = -1;
- else if (targetY > currentY)
- pos.Y = Constants.RegionSize + 1;
- m_log.DebugFormat(
- "[CROSS BEHAVIOUR]: {0} moving to cross from {1} into {2}, target {3}",
- Bot.Name, currentSim.Name, destRegion.Name, pos);
-
- client.Self.Movement.TurnToward(pos);
-
- Bot.Client.Self.RegionCrossed += Self_RegionCrossed;
-
- Bot.Client.Self.Movement.AtPos = true;
-
- if (!m_regionCrossedMutex.WaitOne(m_regionCrossingTimeout))
- {
- m_log.ErrorFormat(
- "[CROSS BEHAVIOUR]: {0} failed to cross from {1} into {2} with {3}ms",
- Bot.Name, currentSim.Name, destRegion.Name, m_regionCrossingTimeout);
- }
- else
- {
- m_log.DebugFormat(
- "[CROSS BEHAVIOUR]: {0} crossed from {1} into {2}",
- Bot.Name, currentSim.Name, destRegion.Name);
- }
- Bot.Client.Self.RegionCrossed -= Self_RegionCrossed;
-
- Thread.Sleep(6000);
- m_log.DebugFormat(
- "[CROSS BEHAVIOUR]: {0} stopped moving after cross from {1} into {2}",
- Bot.Name, currentSim.Name, destRegion.Name);
- Bot.Client.Self.Movement.AtPos = false;
- }
- else
- {
- m_log.DebugFormat(
- "[CROSS BEHAVIOUR]: No candidate region for {0} to cross into from {1}. Ignoring.",
- Bot.Name, currentSim.Name);
- }
- }
- private bool TryAddRegion(ulong handle, List<GridRegion> regions)
- {
- Dictionary<ulong, GridRegion> knownRegions = Bot.Manager.RegionsKnown;
- lock (knownRegions)
- {
- if (knownRegions.Count == 0)
- return false;
- m_log.DebugFormat("[CROSS BEHAVIOUR]: Looking for region with handle {0} in known regions", handle);
- if (knownRegions.ContainsKey(handle))
- {
- GridRegion region = knownRegions[handle];
- m_log.DebugFormat(
- "[CROSS BEHAVIOUR]: Adding region {0} to crossing candidates for {1}", region.Name, Bot.Name);
- regions.Add(region);
- return true;
- }
- else
- {
- return false;
- }
- }
- }
- internal void Self_RegionCrossed(object o, RegionCrossedEventArgs args)
- {
- m_regionCrossedMutex.Set();
- }
- }
- }
|