opensim – Blame information for rev 1

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