diff options
Diffstat (limited to 'OpenSim/Tools/pCampBot/Bot.cs')
-rw-r--r-- | OpenSim/Tools/pCampBot/Bot.cs | 77 |
1 files changed, 63 insertions, 14 deletions
diff --git a/OpenSim/Tools/pCampBot/Bot.cs b/OpenSim/Tools/pCampBot/Bot.cs index d418288..d0a4ef3 100644 --- a/OpenSim/Tools/pCampBot/Bot.cs +++ b/OpenSim/Tools/pCampBot/Bot.cs | |||
@@ -72,9 +72,10 @@ namespace pCampBot | |||
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. |
@@ -165,8 +166,6 @@ namespace pCampBot | |||
165 | { | 166 | { |
166 | ConnectionState = ConnectionState.Disconnected; | 167 | ConnectionState = ConnectionState.Disconnected; |
167 | 168 | ||
168 | behaviours.ForEach(b => b.Initialize(this)); | ||
169 | |||
170 | Random = new Random(Environment.TickCount);// We do stuff randomly here | 169 | Random = new Random(Environment.TickCount);// We do stuff randomly here |
171 | FirstName = firstName; | 170 | FirstName = firstName; |
172 | LastName = lastName; | 171 | LastName = lastName; |
@@ -176,12 +175,53 @@ namespace pCampBot | |||
176 | StartLocation = startLocation; | 175 | StartLocation = startLocation; |
177 | 176 | ||
178 | Manager = bm; | 177 | Manager = bm; |
179 | Behaviours = behaviours; | 178 | |
179 | Behaviours = new Dictionary<string, IBehaviour>(); | ||
180 | foreach (IBehaviour behaviour in behaviours) | ||
181 | AddBehaviour(behaviour); | ||
180 | 182 | ||
181 | // Only calling for use as a template. | 183 | // Only calling for use as a template. |
182 | CreateLibOmvClient(); | 184 | CreateLibOmvClient(); |
183 | } | 185 | } |
184 | 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 | |||
185 | private void CreateLibOmvClient() | 225 | private void CreateLibOmvClient() |
186 | { | 226 | { |
187 | GridClient newClient = new GridClient(); | 227 | GridClient newClient = new GridClient(); |
@@ -237,16 +277,25 @@ namespace pCampBot | |||
237 | private void Action() | 277 | private void Action() |
238 | { | 278 | { |
239 | while (ConnectionState != ConnectionState.Disconnecting) | 279 | while (ConnectionState != ConnectionState.Disconnecting) |
280 | { | ||
240 | lock (Behaviours) | 281 | lock (Behaviours) |
241 | Behaviours.ForEach( | 282 | { |
242 | b => | 283 | foreach (IBehaviour behaviour in Behaviours.Values) |
243 | { | 284 | { |
244 | Thread.Sleep(Random.Next(3000, 10000)); | 285 | // Thread.Sleep(Random.Next(3000, 10000)); |
245 | 286 | ||
246 | // 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()); |
247 | b.Action(); | 288 | behaviour.Action(); |
248 | } | 289 | } |
249 | ); | 290 | } |
291 | |||
292 | // XXX: This is a really shitty way of yielding so that behaviours can be added/removed | ||
293 | Thread.Sleep(100); | ||
294 | } | ||
295 | |||
296 | lock (Behaviours) | ||
297 | foreach (IBehaviour b in Behaviours.Values) | ||
298 | b.Close(); | ||
250 | } | 299 | } |
251 | 300 | ||
252 | /// <summary> | 301 | /// <summary> |