diff options
Diffstat (limited to 'OpenSim')
-rw-r--r-- | OpenSim/Tools/pCampBot/Behaviours/CrossBehaviour.cs | 169 |
1 files changed, 169 insertions, 0 deletions
diff --git a/OpenSim/Tools/pCampBot/Behaviours/CrossBehaviour.cs b/OpenSim/Tools/pCampBot/Behaviours/CrossBehaviour.cs new file mode 100644 index 0000000..1e01c64 --- /dev/null +++ b/OpenSim/Tools/pCampBot/Behaviours/CrossBehaviour.cs | |||
@@ -0,0 +1,169 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using System.Reflection; | ||
31 | using System.Threading; | ||
32 | using log4net; | ||
33 | using OpenMetaverse; | ||
34 | using OpenSim.Framework; | ||
35 | using pCampBot.Interfaces; | ||
36 | |||
37 | namespace pCampBot | ||
38 | { | ||
39 | /// <summary> | ||
40 | /// Get the bot to make a region crossing. | ||
41 | /// </summary> | ||
42 | public class CrossBehaviour : AbstractBehaviour | ||
43 | { | ||
44 | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
45 | |||
46 | public AutoResetEvent m_regionCrossedMutex = new AutoResetEvent(false); | ||
47 | |||
48 | public const int m_regionCrossingTimeout = 1000 * 60; | ||
49 | |||
50 | public CrossBehaviour() { Name = "Cross"; } | ||
51 | |||
52 | public override void Action() | ||
53 | { | ||
54 | GridClient client = Bot.Client; | ||
55 | |||
56 | // // Fly to make the border cross easier. | ||
57 | // client.Self.Movement.Fly = true; | ||
58 | // client.Self.Movement.Fly = false; | ||
59 | |||
60 | // Seek out neighbouring region | ||
61 | Simulator currentSim = client.Network.CurrentSim; | ||
62 | ulong currentHandle = currentSim.Handle; | ||
63 | uint currentX, currentY; | ||
64 | Utils.LongToUInts(currentHandle, out currentX, out currentY); | ||
65 | |||
66 | List<GridRegion> candidateRegions = new List<GridRegion>(); | ||
67 | TryAddRegion(Utils.UIntsToLong(Math.Max(0, currentX - Constants.RegionSize), currentY), candidateRegions); // West | ||
68 | TryAddRegion(Utils.UIntsToLong(currentX + Constants.RegionSize, currentY), candidateRegions); // East | ||
69 | TryAddRegion(Utils.UIntsToLong(currentX, Math.Max(0, currentY - Constants.RegionSize)), candidateRegions); // South | ||
70 | TryAddRegion(Utils.UIntsToLong(currentX, currentY + Constants.RegionSize), candidateRegions); // North | ||
71 | |||
72 | if (candidateRegions.Count != 0) | ||
73 | { | ||
74 | GridRegion destRegion = candidateRegions[Bot.Manager.Rng.Next(candidateRegions.Count)]; | ||
75 | |||
76 | uint targetX, targetY; | ||
77 | Utils.LongToUInts(destRegion.RegionHandle, out targetX, out targetY); | ||
78 | |||
79 | Vector3 pos = client.Self.SimPosition; | ||
80 | if (targetX < currentX) | ||
81 | pos.X = -1; | ||
82 | else if (targetX > currentX) | ||
83 | pos.X = Constants.RegionSize + 1; | ||
84 | |||
85 | if (targetY < currentY) | ||
86 | pos.Y = -1; | ||
87 | else if (targetY > currentY) | ||
88 | pos.Y = Constants.RegionSize + 1; | ||
89 | |||
90 | m_log.DebugFormat( | ||
91 | "[CROSS BEHAVIOUR]: {0} moving to cross from {1} into {2}, target {3}", | ||
92 | Bot.Name, currentSim.Name, destRegion.Name, pos); | ||
93 | |||
94 | // Face in the direction of the candidate region | ||
95 | client.Self.Movement.TurnToward(pos); | ||
96 | |||
97 | // Listen for event so that we know when we've crossed the region boundary | ||
98 | Bot.Client.Self.RegionCrossed += Self_RegionCrossed; | ||
99 | |||
100 | // Start moving | ||
101 | Bot.Client.Self.Movement.AtPos = true; | ||
102 | |||
103 | // Stop when reach region target or border cross detected | ||
104 | if (!m_regionCrossedMutex.WaitOne(m_regionCrossingTimeout)) | ||
105 | { | ||
106 | m_log.ErrorFormat( | ||
107 | "[CROSS BEHAVIOUR]: {0} failed to cross from {1} into {2} with {3}ms", | ||
108 | Bot.Name, currentSim.Name, destRegion.Name, m_regionCrossingTimeout); | ||
109 | } | ||
110 | else | ||
111 | { | ||
112 | m_log.DebugFormat( | ||
113 | "[CROSS BEHAVIOUR]: {0} crossed from {1} into {2}", | ||
114 | Bot.Name, currentSim.Name, destRegion.Name); | ||
115 | } | ||
116 | |||
117 | Bot.Client.Self.RegionCrossed -= Self_RegionCrossed; | ||
118 | |||
119 | // We will hackishly carry on travelling into the region for a little bit. | ||
120 | Thread.Sleep(6000); | ||
121 | |||
122 | m_log.DebugFormat( | ||
123 | "[CROSS BEHAVIOUR]: {0} stopped moving after cross from {1} into {2}", | ||
124 | Bot.Name, currentSim.Name, destRegion.Name); | ||
125 | |||
126 | Bot.Client.Self.Movement.AtPos = false; | ||
127 | } | ||
128 | else | ||
129 | { | ||
130 | m_log.DebugFormat( | ||
131 | "[CROSS BEHAVIOUR]: No candidate region for {0} to cross into from {1}. Ignoring.", | ||
132 | Bot.Name, currentSim.Name); | ||
133 | } | ||
134 | } | ||
135 | |||
136 | private bool TryAddRegion(ulong handle, List<GridRegion> regions) | ||
137 | { | ||
138 | Dictionary<ulong, GridRegion> knownRegions = Bot.Manager.RegionsKnown; | ||
139 | |||
140 | lock (knownRegions) | ||
141 | { | ||
142 | if (knownRegions.Count == 0) | ||
143 | return false; | ||
144 | |||
145 | m_log.DebugFormat("[CROSS BEHAVIOUR]: Looking for region with handle {0} in known regions", handle); | ||
146 | |||
147 | if (knownRegions.ContainsKey(handle)) | ||
148 | { | ||
149 | GridRegion region = knownRegions[handle]; | ||
150 | m_log.DebugFormat( | ||
151 | "[CROSS BEHAVIOUR]: Adding region {0} to crossing candidates for {1}", region.Name, Bot.Name); | ||
152 | |||
153 | regions.Add(region); | ||
154 | |||
155 | return true; | ||
156 | } | ||
157 | else | ||
158 | { | ||
159 | return false; | ||
160 | } | ||
161 | } | ||
162 | } | ||
163 | |||
164 | internal void Self_RegionCrossed(object o, RegionCrossedEventArgs args) | ||
165 | { | ||
166 | m_regionCrossedMutex.Set(); | ||
167 | } | ||
168 | } | ||
169 | } \ No newline at end of file | ||