CrossBehaviour.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Threading;
  31. using log4net;
  32. using OpenMetaverse;
  33. using OpenSim.Framework;
  34. using pCampBot.Interfaces;
  35. namespace pCampBot
  36. {
  37. /// <summary>
  38. /// Get the bot to make a region crossing.
  39. /// </summary>
  40. public class CrossBehaviour : AbstractBehaviour
  41. {
  42. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  43. public AutoResetEvent m_regionCrossedMutex = new AutoResetEvent(false);
  44. public const int m_regionCrossingTimeout = 1000 * 60;
  45. public CrossBehaviour() { Name = "Cross"; }
  46. public override void Action()
  47. {
  48. GridClient client = Bot.Client;
  49. // // Fly to make the border cross easier.
  50. // client.Self.Movement.Fly = true;
  51. // client.Self.Movement.Fly = false;
  52. // Seek out neighbouring region
  53. Simulator currentSim = client.Network.CurrentSim;
  54. ulong currentHandle = currentSim.Handle;
  55. uint currentX, currentY;
  56. Utils.LongToUInts(currentHandle, out currentX, out currentY);
  57. List<GridRegion> candidateRegions = new List<GridRegion>();
  58. TryAddRegion(Utils.UIntsToLong(Math.Max(0, currentX - Constants.RegionSize), currentY), candidateRegions); // West
  59. TryAddRegion(Utils.UIntsToLong(currentX + Constants.RegionSize, currentY), candidateRegions); // East
  60. TryAddRegion(Utils.UIntsToLong(currentX, Math.Max(0, currentY - Constants.RegionSize)), candidateRegions); // South
  61. TryAddRegion(Utils.UIntsToLong(currentX, currentY + Constants.RegionSize), candidateRegions); // North
  62. if (candidateRegions.Count != 0)
  63. {
  64. GridRegion destRegion = candidateRegions[Bot.Manager.Rng.Next(candidateRegions.Count)];
  65. uint targetX, targetY;
  66. Utils.LongToUInts(destRegion.RegionHandle, out targetX, out targetY);
  67. Vector3 pos = client.Self.SimPosition;
  68. if (targetX < currentX)
  69. pos.X = -1;
  70. else if (targetX > currentX)
  71. pos.X = Constants.RegionSize + 1;
  72. if (targetY < currentY)
  73. pos.Y = -1;
  74. else if (targetY > currentY)
  75. pos.Y = Constants.RegionSize + 1;
  76. m_log.DebugFormat(
  77. "[CROSS BEHAVIOUR]: {0} moving to cross from {1} into {2}, target {3}",
  78. Bot.Name, currentSim.Name, destRegion.Name, pos);
  79. // Face in the direction of the candidate region
  80. client.Self.Movement.TurnToward(pos);
  81. // Listen for event so that we know when we've crossed the region boundary
  82. Bot.Client.Self.RegionCrossed += Self_RegionCrossed;
  83. // Start moving
  84. Bot.Client.Self.Movement.AtPos = true;
  85. // Stop when reach region target or border cross detected
  86. if (!m_regionCrossedMutex.WaitOne(m_regionCrossingTimeout))
  87. {
  88. m_log.ErrorFormat(
  89. "[CROSS BEHAVIOUR]: {0} failed to cross from {1} into {2} with {3}ms",
  90. Bot.Name, currentSim.Name, destRegion.Name, m_regionCrossingTimeout);
  91. }
  92. else
  93. {
  94. m_log.DebugFormat(
  95. "[CROSS BEHAVIOUR]: {0} crossed from {1} into {2}",
  96. Bot.Name, currentSim.Name, destRegion.Name);
  97. }
  98. Bot.Client.Self.RegionCrossed -= Self_RegionCrossed;
  99. // We will hackishly carry on travelling into the region for a little bit.
  100. Thread.Sleep(6000);
  101. m_log.DebugFormat(
  102. "[CROSS BEHAVIOUR]: {0} stopped moving after cross from {1} into {2}",
  103. Bot.Name, currentSim.Name, destRegion.Name);
  104. Bot.Client.Self.Movement.AtPos = false;
  105. }
  106. else
  107. {
  108. m_log.DebugFormat(
  109. "[CROSS BEHAVIOUR]: No candidate region for {0} to cross into from {1}. Ignoring.",
  110. Bot.Name, currentSim.Name);
  111. }
  112. }
  113. private bool TryAddRegion(ulong handle, List<GridRegion> regions)
  114. {
  115. Dictionary<ulong, GridRegion> knownRegions = Bot.Manager.RegionsKnown;
  116. lock (knownRegions)
  117. {
  118. if (knownRegions.Count == 0)
  119. return false;
  120. m_log.DebugFormat("[CROSS BEHAVIOUR]: Looking for region with handle {0} in known regions", handle);
  121. if (knownRegions.ContainsKey(handle))
  122. {
  123. GridRegion region = knownRegions[handle];
  124. m_log.DebugFormat(
  125. "[CROSS BEHAVIOUR]: Adding region {0} to crossing candidates for {1}", region.Name, Bot.Name);
  126. regions.Add(region);
  127. return true;
  128. }
  129. else
  130. {
  131. return false;
  132. }
  133. }
  134. }
  135. internal void Self_RegionCrossed(object o, RegionCrossedEventArgs args)
  136. {
  137. m_regionCrossedMutex.Set();
  138. }
  139. }
  140. }