aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Tools/pCampBot/Bot.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Tools/pCampBot/Bot.cs')
-rw-r--r--OpenSim/Tools/pCampBot/Bot.cs250
1 files changed, 192 insertions, 58 deletions
diff --git a/OpenSim/Tools/pCampBot/Bot.cs b/OpenSim/Tools/pCampBot/Bot.cs
index 9821180..d0a4ef3 100644
--- a/OpenSim/Tools/pCampBot/Bot.cs
+++ b/OpenSim/Tools/pCampBot/Bot.cs
@@ -59,22 +59,23 @@ namespace pCampBot
59 public delegate void AnEvent(Bot callbot, EventType someevent); // event delegate for bot events 59 public delegate void AnEvent(Bot callbot, EventType someevent); // event delegate for bot events
60 60
61 /// <summary> 61 /// <summary>
62 /// Bot manager. 62 /// Controls whether bots request textures for the object information they receive
63 /// </summary> 63 /// </summary>
64 public BotManager Manager { get; private set; } 64 public bool RequestObjectTextures { get; set; }
65 65
66 /// <summary> 66 /// <summary>
67 /// Bot config, passed from BotManager. 67 /// Bot manager.
68 /// </summary> 68 /// </summary>
69 private IConfig startupConfig; 69 public BotManager Manager { get; private set; }
70 70
71 /// <summary> 71 /// <summary>
72 /// Behaviours implemented by this bot. 72 /// Behaviours implemented by this bot.
73 /// </summary> 73 /// </summary>
74 /// <remarks> 74 /// <remarks>
75 /// Lock this list before manipulating it. 75 /// Indexed by abbreviated name. There can only be one instance of a particular behaviour.
76 /// Lock this structure before manipulating it.
76 /// </remarks> 77 /// </remarks>
77 public List<IBehaviour> Behaviours { get; private set; } 78 public Dictionary<string, IBehaviour> Behaviours { get; private set; }
78 79
79 /// <summary> 80 /// <summary>
80 /// Objects that the bot has discovered. 81 /// Objects that the bot has discovered.
@@ -97,11 +98,35 @@ namespace pCampBot
97 /// </summary> 98 /// </summary>
98 public ConnectionState ConnectionState { get; private set; } 99 public ConnectionState ConnectionState { get; private set; }
99 100
101 public List<Simulator> Simulators
102 {
103 get
104 {
105 lock (Client.Network.Simulators)
106 return new List<Simulator>(Client.Network.Simulators);
107 }
108 }
109
110 /// <summary>
111 /// The number of connections that this bot has to different simulators.
112 /// </summary>
113 /// <value>Includes both root and child connections.</value>
114 public int SimulatorsCount
115 {
116 get
117 {
118 lock (Client.Network.Simulators)
119 return Client.Network.Simulators.Count;
120 }
121 }
122
100 public string FirstName { get; private set; } 123 public string FirstName { get; private set; }
101 public string LastName { get; private set; } 124 public string LastName { get; private set; }
102 public string Name { get; private set; } 125 public string Name { get; private set; }
103 public string Password { get; private set; } 126 public string Password { get; private set; }
104 public string LoginUri { get; private set; } 127 public string LoginUri { get; private set; }
128 public string StartLocation { get; private set; }
129
105 public string saveDir; 130 public string saveDir;
106 public string wear; 131 public string wear;
107 132
@@ -137,94 +162,177 @@ namespace pCampBot
137 /// <param name="behaviours"></param> 162 /// <param name="behaviours"></param>
138 public Bot( 163 public Bot(
139 BotManager bm, List<IBehaviour> behaviours, 164 BotManager bm, List<IBehaviour> behaviours,
140 string firstName, string lastName, string password, string loginUri) 165 string firstName, string lastName, string password, string startLocation, string loginUri)
141 { 166 {
142 ConnectionState = ConnectionState.Disconnected; 167 ConnectionState = ConnectionState.Disconnected;
143 168
144 behaviours.ForEach(b => b.Initialize(this));
145
146 Client = new GridClient();
147
148 Random = new Random(Environment.TickCount);// We do stuff randomly here 169 Random = new Random(Environment.TickCount);// We do stuff randomly here
149 FirstName = firstName; 170 FirstName = firstName;
150 LastName = lastName; 171 LastName = lastName;
151 Name = string.Format("{0} {1}", FirstName, LastName); 172 Name = string.Format("{0} {1}", FirstName, LastName);
152 Password = password; 173 Password = password;
153 LoginUri = loginUri; 174 LoginUri = loginUri;
175 StartLocation = startLocation;
154 176
155 Manager = bm; 177 Manager = bm;
156 startupConfig = bm.Config;
157 readconfig();
158 178
159 Behaviours = behaviours; 179 Behaviours = new Dictionary<string, IBehaviour>();
180 foreach (IBehaviour behaviour in behaviours)
181 AddBehaviour(behaviour);
182
183 // Only calling for use as a template.
184 CreateLibOmvClient();
185 }
186
187 public bool TryGetBehaviour(string abbreviatedName, out IBehaviour behaviour)
188 {
189 lock (Behaviours)
190 return Behaviours.TryGetValue(abbreviatedName, out behaviour);
191 }
192
193 public bool AddBehaviour(IBehaviour behaviour)
194 {
195 lock (Behaviours)
196 {
197 if (!Behaviours.ContainsKey(behaviour.AbbreviatedName))
198 {
199 behaviour.Initialize(this);
200 Behaviours.Add(behaviour.AbbreviatedName, behaviour);
201
202 return true;
203 }
204 }
205
206 return false;
207 }
208
209 public bool RemoveBehaviour(string abbreviatedName)
210 {
211 lock (Behaviours)
212 {
213 IBehaviour behaviour;
214
215 if (!Behaviours.TryGetValue(abbreviatedName, out behaviour))
216 return false;
217
218 behaviour.Close();
219 Behaviours.Remove(abbreviatedName);
220
221 return true;
222 }
223 }
224
225 private void CreateLibOmvClient()
226 {
227 GridClient newClient = new GridClient();
228
229 if (Client != null)
230 {
231 newClient.Settings.LOGIN_SERVER = Client.Settings.LOGIN_SERVER;
232 newClient.Settings.ALWAYS_DECODE_OBJECTS = Client.Settings.ALWAYS_DECODE_OBJECTS;
233 newClient.Settings.AVATAR_TRACKING = Client.Settings.AVATAR_TRACKING;
234 newClient.Settings.OBJECT_TRACKING = Client.Settings.OBJECT_TRACKING;
235 newClient.Settings.SEND_AGENT_THROTTLE = Client.Settings.SEND_AGENT_THROTTLE;
236 newClient.Settings.SEND_AGENT_UPDATES = Client.Settings.SEND_AGENT_UPDATES;
237 newClient.Settings.SEND_PINGS = Client.Settings.SEND_PINGS;
238 newClient.Settings.STORE_LAND_PATCHES = Client.Settings.STORE_LAND_PATCHES;
239 newClient.Settings.USE_ASSET_CACHE = Client.Settings.USE_ASSET_CACHE;
240 newClient.Settings.MULTIPLE_SIMS = Client.Settings.MULTIPLE_SIMS;
241 newClient.Throttle.Asset = Client.Throttle.Asset;
242 newClient.Throttle.Land = Client.Throttle.Land;
243 newClient.Throttle.Task = Client.Throttle.Task;
244 newClient.Throttle.Texture = Client.Throttle.Texture;
245 newClient.Throttle.Wind = Client.Throttle.Wind;
246 newClient.Throttle.Total = Client.Throttle.Total;
247 }
248 else
249 {
250 newClient.Settings.LOGIN_SERVER = LoginUri;
251 newClient.Settings.ALWAYS_DECODE_OBJECTS = false;
252 newClient.Settings.AVATAR_TRACKING = false;
253 newClient.Settings.OBJECT_TRACKING = false;
254 newClient.Settings.SEND_AGENT_THROTTLE = true;
255 newClient.Settings.SEND_PINGS = true;
256 newClient.Settings.STORE_LAND_PATCHES = false;
257 newClient.Settings.USE_ASSET_CACHE = false;
258 newClient.Settings.MULTIPLE_SIMS = true;
259 newClient.Throttle.Asset = 100000;
260 newClient.Throttle.Land = 100000;
261 newClient.Throttle.Task = 100000;
262 newClient.Throttle.Texture = 100000;
263 newClient.Throttle.Wind = 100000;
264 newClient.Throttle.Total = 400000;
265 }
266
267 newClient.Network.LoginProgress += this.Network_LoginProgress;
268 newClient.Network.SimConnected += this.Network_SimConnected;
269 newClient.Network.Disconnected += this.Network_OnDisconnected;
270 newClient.Objects.ObjectUpdate += Objects_NewPrim;
271
272 Client = newClient;
160 } 273 }
161 274
162 //We do our actions here. This is where one would 275 //We do our actions here. This is where one would
163 //add additional steps and/or things the bot should do 276 //add additional steps and/or things the bot should do
164 private void Action() 277 private void Action()
165 { 278 {
166 while (true) 279 while (ConnectionState != ConnectionState.Disconnecting)
280 {
167 lock (Behaviours) 281 lock (Behaviours)
168 Behaviours.ForEach( 282 {
169 b => 283 foreach (IBehaviour behaviour in Behaviours.Values)
170 { 284 {
171 Thread.Sleep(Random.Next(3000, 10000)); 285// Thread.Sleep(Random.Next(3000, 10000));
172 286
173 // m_log.DebugFormat("[pCAMPBOT]: For {0} performing action {1}", Name, b.GetType()); 287 // m_log.DebugFormat("[pCAMPBOT]: For {0} performing action {1}", Name, b.GetType());
174 b.Action(); 288 behaviour.Action();
175 } 289 }
176 ); 290 }
177 }
178 291
179 /// <summary> 292 // XXX: This is a really shitty way of yielding so that behaviours can be added/removed
180 /// Read the Nini config and initialize 293 Thread.Sleep(100);
181 /// </summary> 294 }
182 public void readconfig() 295
183 { 296 lock (Behaviours)
184 wear = startupConfig.GetString("wear", "no"); 297 foreach (IBehaviour b in Behaviours.Values)
298 b.Close();
185 } 299 }
186 300
187 /// <summary> 301 /// <summary>
188 /// Tells LibSecondLife to logout and disconnect. Raises the disconnect events once it finishes. 302 /// Tells LibSecondLife to logout and disconnect. Raises the disconnect events once it finishes.
189 /// </summary> 303 /// </summary>
190 public void shutdown() 304 public void Disconnect()
191 { 305 {
192 ConnectionState = ConnectionState.Disconnecting; 306 ConnectionState = ConnectionState.Disconnecting;
193 307
194 if (m_actionThread != null) 308// if (m_actionThread != null)
195 m_actionThread.Abort(); 309// m_actionThread.Abort();
196 310
197 Client.Network.Logout(); 311 Client.Network.Logout();
198 } 312 }
199 313
314 public void Connect()
315 {
316 Thread connectThread = new Thread(ConnectInternal);
317 connectThread.Name = Name;
318 connectThread.IsBackground = true;
319
320 connectThread.Start();
321 }
322
200 /// <summary> 323 /// <summary>
201 /// This is the bot startup loop. 324 /// This is the bot startup loop.
202 /// </summary> 325 /// </summary>
203 public void startup() 326 private void ConnectInternal()
204 { 327 {
205 Client.Settings.LOGIN_SERVER = LoginUri;
206 Client.Settings.ALWAYS_DECODE_OBJECTS = false;
207 Client.Settings.AVATAR_TRACKING = false;
208 Client.Settings.OBJECT_TRACKING = false;
209 Client.Settings.SEND_AGENT_THROTTLE = true;
210 Client.Settings.SEND_PINGS = true;
211 Client.Settings.STORE_LAND_PATCHES = false;
212 Client.Settings.USE_ASSET_CACHE = false;
213 Client.Settings.MULTIPLE_SIMS = true;
214 Client.Throttle.Asset = 100000;
215 Client.Throttle.Land = 100000;
216 Client.Throttle.Task = 100000;
217 Client.Throttle.Texture = 100000;
218 Client.Throttle.Wind = 100000;
219 Client.Throttle.Total = 400000;
220 Client.Network.LoginProgress += this.Network_LoginProgress;
221 Client.Network.SimConnected += this.Network_SimConnected;
222 Client.Network.Disconnected += this.Network_OnDisconnected;
223 Client.Objects.ObjectUpdate += Objects_NewPrim;
224
225 ConnectionState = ConnectionState.Connecting; 328 ConnectionState = ConnectionState.Connecting;
226 329
227 if (Client.Network.Login(FirstName, LastName, Password, "pCampBot", "Your name")) 330 // Current create a new client on each connect. libomv doesn't seem to process new sim
331 // information (e.g. EstablishAgentCommunication events) if connecting after a disceonnect with the same
332 // client
333 CreateLibOmvClient();
334
335 if (Client.Network.Login(FirstName, LastName, Password, "pCampBot", StartLocation, "Your name"))
228 { 336 {
229 ConnectionState = ConnectionState.Connected; 337 ConnectionState = ConnectionState.Connected;
230 338
@@ -268,6 +376,30 @@ namespace pCampBot
268 } 376 }
269 } 377 }
270 378
379 /// <summary>
380 /// Sit this bot on the ground.
381 /// </summary>
382 public void SitOnGround()
383 {
384 if (ConnectionState == ConnectionState.Connected)
385 Client.Self.SitOnGround();
386 }
387
388 /// <summary>
389 /// Stand this bot
390 /// </summary>
391 public void Stand()
392 {
393 if (ConnectionState == ConnectionState.Connected)
394 {
395 // Unlike sit on ground, here libomv checks whether we have SEND_AGENT_UPDATES enabled.
396 bool prevUpdatesSetting = Client.Settings.SEND_AGENT_UPDATES;
397 Client.Settings.SEND_AGENT_UPDATES = true;
398 Client.Self.Stand();
399 Client.Settings.SEND_AGENT_UPDATES = prevUpdatesSetting;
400 }
401 }
402
271 public void SaveDefaultAppearance() 403 public void SaveDefaultAppearance()
272 { 404 {
273 saveDir = "MyAppearance/" + FirstName + "_" + LastName; 405 saveDir = "MyAppearance/" + FirstName + "_" + LastName;
@@ -468,6 +600,8 @@ namespace pCampBot
468// || args.Reason == NetworkManager.DisconnectType.NetworkTimeout) 600// || args.Reason == NetworkManager.DisconnectType.NetworkTimeout)
469// && OnDisconnected != null) 601// && OnDisconnected != null)
470 602
603
604
471 if ( 605 if (
472 (args.Reason == NetworkManager.DisconnectType.ClientInitiated 606 (args.Reason == NetworkManager.DisconnectType.ClientInitiated
473 || args.Reason == NetworkManager.DisconnectType.ServerInitiated 607 || args.Reason == NetworkManager.DisconnectType.ServerInitiated
@@ -481,8 +615,8 @@ namespace pCampBot
481 615
482 public void Objects_NewPrim(object sender, PrimEventArgs args) 616 public void Objects_NewPrim(object sender, PrimEventArgs args)
483 { 617 {
484// if (Name.EndsWith("4")) 618 if (!RequestObjectTextures)
485// throw new Exception("Aaargh"); 619 return;
486 620
487 Primitive prim = args.Prim; 621 Primitive prim = args.Prim;
488 622