CrossBehaviour.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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()
  46. {
  47. AbbreviatedName = "c";
  48. Name = "Cross";
  49. }
  50. public override void Action()
  51. {
  52. GridClient client = Bot.Client;
  53. // // Fly to make the border cross easier.
  54. // client.Self.Movement.Fly = true;
  55. // client.Self.Movement.Fly = false;
  56. // Seek out neighbouring region
  57. Simulator currentSim = client.Network.CurrentSim;
  58. ulong currentHandle = currentSim.Handle;
  59. uint currentX, currentY;
  60. Utils.LongToUInts(currentHandle, out currentX, out currentY);
  61. List<GridRegion> candidateRegions = new List<GridRegion>();
  62. TryAddRegion(Utils.UIntsToLong(Math.Max(0, currentX - Constants.RegionSize), currentY), candidateRegions); // West
  63. TryAddRegion(Utils.UIntsToLong(currentX + Constants.RegionSize, currentY), candidateRegions); // East
  64. TryAddRegion(Utils.UIntsToLong(currentX, Math.Max(0, currentY - Constants.RegionSize)), candidateRegions); // South
  65. TryAddRegion(Utils.UIntsToLong(currentX, currentY + Constants.RegionSize), candidateRegions); // North
  66. if (candidateRegions.Count != 0)
  67. {
  68. GridRegion destRegion = candidateRegions[Bot.Manager.Rng.Next(candidateRegions.Count)];
  69. uint targetX, targetY;
  70. Utils.LongToUInts(destRegion.RegionHandle, out targetX, out targetY);
  71. Vector3 pos = client.Self.SimPosition;
  72. if (targetX < currentX)
  73. pos.X = -1;
  74. else if (targetX > currentX)
  75. pos.X = Constants.RegionSize + 1;
  76. if (targetY < currentY)
  77. pos.Y = -1;
  78. else if (targetY > currentY)
  79. pos.Y = Constants.RegionSize + 1;
  80. m_log.DebugFormat(
  81. "[CROSS BEHAVIOUR]: {0} moving to cross from {1} into {2}, target {3}",
  82. Bot.Name, currentSim.Name, destRegion.Name, pos);
  83. // Face in the direction of the candidate region
  84. client.Self.Movement.TurnToward(pos);
  85. // Listen for event so that we know when we've crossed the region boundary
  86. Bot.Client.Self.RegionCrossed += Self_RegionCrossed;
  87. // Start moving
  88. Bot.Client.Self.Movement.AtPos = true;
  89. // Stop when reach region target or border cross detected
  90. if (!m_regionCrossedMutex.WaitOne(m_regionCrossingTimeout))
  91. {
  92. m_log.ErrorFormat(
  93. "[CROSS BEHAVIOUR]: {0} failed to cross from {1} into {2} with {3}ms",
  94. Bot.Name, currentSim.Name, destRegion.Name, m_regionCrossingTimeout);
  95. }
  96. else
  97. {
  98. m_log.DebugFormat(
  99. "[CROSS BEHAVIOUR]: {0} crossed from {1} into {2}",
  100. Bot.Name, currentSim.Name, destRegion.Name);
  101. }
  102. Bot.Client.Self.RegionCrossed -= Self_RegionCrossed;
  103. // We will hackishly carry on travelling into the region for a little bit.
  104. Thread.Sleep(6000);
  105. m_log.DebugFormat(
  106. "[CROSS BEHAVIOUR]: {0} stopped moving after cross from {1} into {2}",
  107. Bot.Name, currentSim.Name, destRegion.Name);
  108. Bot.Client.Self.Movement.AtPos = false;
  109. }
  110. else
  111. {
  112. m_log.DebugFormat(
  113. "[CROSS BEHAVIOUR]: No candidate region for {0} to cross into from {1}. Ignoring.",
  114. Bot.Name, currentSim.Name);
  115. }
  116. }
  117. private bool TryAddRegion(ulong handle, List<GridRegion> regions)
  118. {
  119. Dictionary<ulong, GridRegion> knownRegions = Bot.Manager.RegionsKnown;
  120. lock (knownRegions)
  121. {
  122. if (knownRegions.Count == 0)
  123. return false;
  124. m_log.DebugFormat("[CROSS BEHAVIOUR]: Looking for region with handle {0} in known regions", handle);
  125. if (knownRegions.ContainsKey(handle))
  126. {
  127. GridRegion region = knownRegions[handle];
  128. m_log.DebugFormat(
  129. "[CROSS BEHAVIOUR]: Adding region {0} to crossing candidates for {1}", region.Name, Bot.Name);
  130. regions.Add(region);
  131. return true;
  132. }
  133. else
  134. {
  135. return false;
  136. }
  137. }
  138. }
  139. internal void Self_RegionCrossed(object o, RegionCrossedEventArgs args)
  140. {
  141. m_regionCrossedMutex.Set();
  142. }
  143. }
  144. }