SensorRepeat.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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.Reflection;
  29. using System.Collections.Generic;
  30. using OpenMetaverse;
  31. using OpenSim.Framework;
  32. using log4net;
  33. using OpenSim.Region.Framework.Interfaces;
  34. using OpenSim.Region.Framework.Scenes;
  35. using OpenSim.Region.ScriptEngine.Shared;
  36. using OpenSim.Region.ScriptEngine.Shared.Api;
  37. namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins
  38. {
  39. public class SensorRepeat
  40. {
  41. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. /// <summary>
  43. /// Used by one-off and repeated sensors
  44. /// </summary>
  45. public class SensorInfo
  46. {
  47. public uint localID;
  48. public UUID itemID;
  49. public double interval;
  50. public DateTime next;
  51. public string name;
  52. public UUID keyID;
  53. public int type;
  54. public double range;
  55. public double arc;
  56. public SceneObjectPart host;
  57. public SensorInfo Clone()
  58. {
  59. return (SensorInfo)this.MemberwiseClone();
  60. }
  61. }
  62. public AsyncCommandManager m_CmdManager;
  63. /// <summary>
  64. /// Number of sensors active.
  65. /// </summary>
  66. public int SensorsCount
  67. {
  68. get
  69. {
  70. return SenseRepeaters.Count;
  71. }
  72. }
  73. public SensorRepeat(AsyncCommandManager CmdManager)
  74. {
  75. m_CmdManager = CmdManager;
  76. maximumRange = CmdManager.m_ScriptEngine.Config.GetDouble("SensorMaxRange", 96.0d);
  77. maximumToReturn = CmdManager.m_ScriptEngine.Config.GetInt("SensorMaxResults", 16);
  78. m_npcModule = m_CmdManager.m_ScriptEngine.World.RequestModuleInterface<INPCModule>();
  79. }
  80. private INPCModule m_npcModule;
  81. private Object SenseLock = new Object();
  82. private const int AGENT = 1;
  83. private const int AGENT_BY_USERNAME = 0x10;
  84. private const int NPC = 0x20;
  85. private const int OS_NPC = 0x01000000;
  86. private const int ACTIVE = 2;
  87. private const int PASSIVE = 4;
  88. private const int SCRIPTED = 8;
  89. private double maximumRange = 96.0;
  90. private int maximumToReturn = 16;
  91. //
  92. // Sensed entity
  93. //
  94. private class SensedEntity : IComparable
  95. {
  96. public SensedEntity(double detectedDistance, UUID detectedID)
  97. {
  98. distance = detectedDistance;
  99. itemID = detectedID;
  100. }
  101. public int CompareTo(object obj)
  102. {
  103. if (!(obj is SensedEntity)) throw new InvalidOperationException();
  104. SensedEntity ent = (SensedEntity)obj;
  105. if (ent == null || ent.distance < distance) return 1;
  106. if (ent.distance > distance) return -1;
  107. return 0;
  108. }
  109. public UUID itemID;
  110. public double distance;
  111. }
  112. /// <summary>
  113. /// Sensors to process.
  114. /// </summary>
  115. /// <remarks>
  116. /// Do not add or remove sensors from this list directly. Instead, copy the list and substitute the updated
  117. /// copy. This is to avoid locking the list for the duration of the sensor sweep, which increases the danger
  118. /// of deadlocks with future code updates.
  119. ///
  120. /// Always lock SenseRepeatListLock when updating this list.
  121. /// </remarks>
  122. private List<SensorInfo> SenseRepeaters = new List<SensorInfo>();
  123. private object SenseRepeatListLock = new object();
  124. public void SetSenseRepeatEvent(uint m_localID, UUID m_itemID,
  125. string name, UUID keyID, int type, double range,
  126. double arc, double sec, SceneObjectPart host)
  127. {
  128. // Always remove first, in case this is a re-set
  129. UnSetSenseRepeaterEvents(m_localID, m_itemID);
  130. if (sec == 0) // Disabling timer
  131. return;
  132. // Add to timer
  133. SensorInfo ts = new SensorInfo();
  134. ts.localID = m_localID;
  135. ts.itemID = m_itemID;
  136. ts.interval = sec;
  137. ts.name = name;
  138. ts.keyID = keyID;
  139. ts.type = type;
  140. if (range > maximumRange)
  141. ts.range = maximumRange;
  142. else
  143. ts.range = range;
  144. ts.arc = arc;
  145. ts.host = host;
  146. ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
  147. AddSenseRepeater(ts);
  148. }
  149. private void AddSenseRepeater(SensorInfo senseRepeater)
  150. {
  151. lock (SenseRepeatListLock)
  152. {
  153. List<SensorInfo> newSenseRepeaters = new List<SensorInfo>(SenseRepeaters);
  154. newSenseRepeaters.Add(senseRepeater);
  155. SenseRepeaters = newSenseRepeaters;
  156. }
  157. }
  158. public void UnSetSenseRepeaterEvents(uint m_localID, UUID m_itemID)
  159. {
  160. // Remove from timer
  161. lock (SenseRepeatListLock)
  162. {
  163. List<SensorInfo> newSenseRepeaters = new List<SensorInfo>();
  164. foreach (SensorInfo ts in SenseRepeaters)
  165. {
  166. if (ts.localID != m_localID || ts.itemID != m_itemID)
  167. {
  168. newSenseRepeaters.Add(ts);
  169. }
  170. }
  171. SenseRepeaters = newSenseRepeaters;
  172. }
  173. }
  174. public void CheckSenseRepeaterEvents()
  175. {
  176. // Go through all timers
  177. foreach (SensorInfo ts in SenseRepeaters)
  178. {
  179. // Time has passed?
  180. if (ts.next.ToUniversalTime() < DateTime.Now.ToUniversalTime())
  181. {
  182. SensorSweep(ts);
  183. // set next interval
  184. ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
  185. }
  186. }
  187. }
  188. public void SenseOnce(uint m_localID, UUID m_itemID,
  189. string name, UUID keyID, int type,
  190. double range, double arc, SceneObjectPart host)
  191. {
  192. // Add to timer
  193. SensorInfo ts = new SensorInfo();
  194. ts.localID = m_localID;
  195. ts.itemID = m_itemID;
  196. ts.interval = 0;
  197. ts.name = name;
  198. ts.keyID = keyID;
  199. ts.type = type;
  200. if (range > maximumRange)
  201. ts.range = maximumRange;
  202. else
  203. ts.range = range;
  204. ts.arc = arc;
  205. ts.host = host;
  206. SensorSweep(ts);
  207. }
  208. private void SensorSweep(SensorInfo ts)
  209. {
  210. if (ts.host == null)
  211. {
  212. return;
  213. }
  214. List<SensedEntity> sensedEntities = new List<SensedEntity>();
  215. // Is the sensor type is AGENT and not SCRIPTED then include agents
  216. if ((ts.type & (AGENT | AGENT_BY_USERNAME | NPC | OS_NPC)) != 0 && (ts.type & SCRIPTED) == 0)
  217. {
  218. sensedEntities.AddRange(doAgentSensor(ts));
  219. }
  220. // If SCRIPTED or PASSIVE or ACTIVE check objects
  221. if ((ts.type & SCRIPTED) != 0 || (ts.type & PASSIVE) != 0 || (ts.type & ACTIVE) != 0)
  222. {
  223. sensedEntities.AddRange(doObjectSensor(ts));
  224. }
  225. lock (SenseLock)
  226. {
  227. if (sensedEntities.Count == 0)
  228. {
  229. // send a "no_sensor"
  230. // Add it to queue
  231. m_CmdManager.m_ScriptEngine.PostScriptEvent(ts.itemID,
  232. new EventParams("no_sensor", new Object[0],
  233. new DetectParams[0]));
  234. }
  235. else
  236. {
  237. // Sort the list to get everything ordered by distance
  238. sensedEntities.Sort();
  239. int count = sensedEntities.Count;
  240. int idx;
  241. List<DetectParams> detected = new List<DetectParams>();
  242. for (idx = 0; idx < count; idx++)
  243. {
  244. try
  245. {
  246. DetectParams detect = new DetectParams();
  247. detect.Key = sensedEntities[idx].itemID;
  248. detect.Populate(m_CmdManager.m_ScriptEngine.World);
  249. detected.Add(detect);
  250. }
  251. catch (Exception)
  252. {
  253. // Ignore errors, the object has been deleted or the avatar has gone and
  254. // there was a problem in detect.Populate so nothing added to the list
  255. }
  256. if (detected.Count == maximumToReturn)
  257. break;
  258. }
  259. if (detected.Count == 0)
  260. {
  261. // To get here with zero in the list there must have been some sort of problem
  262. // like the object being deleted or the avatar leaving to have caused some
  263. // difficulty during the Populate above so fire a no_sensor event
  264. m_CmdManager.m_ScriptEngine.PostScriptEvent(ts.itemID,
  265. new EventParams("no_sensor", new Object[0],
  266. new DetectParams[0]));
  267. }
  268. else
  269. {
  270. m_CmdManager.m_ScriptEngine.PostScriptEvent(ts.itemID,
  271. new EventParams("sensor",
  272. new Object[] {new LSL_Types.LSLInteger(detected.Count) },
  273. detected.ToArray()));
  274. }
  275. }
  276. }
  277. }
  278. private List<SensedEntity> doObjectSensor(SensorInfo ts)
  279. {
  280. List<EntityBase> Entities;
  281. List<SensedEntity> sensedEntities = new List<SensedEntity>();
  282. // If this is an object sense by key try to get it directly
  283. // rather than getting a list to scan through
  284. if (ts.keyID != UUID.Zero)
  285. {
  286. EntityBase e = null;
  287. m_CmdManager.m_ScriptEngine.World.Entities.TryGetValue(ts.keyID, out e);
  288. if (e == null)
  289. return sensedEntities;
  290. Entities = new List<EntityBase>();
  291. Entities.Add(e);
  292. }
  293. else
  294. {
  295. Entities = new List<EntityBase>(m_CmdManager.m_ScriptEngine.World.GetEntities());
  296. }
  297. SceneObjectPart SensePoint = ts.host;
  298. Vector3 fromRegionPos = SensePoint.GetWorldPosition();
  299. // pre define some things to avoid repeated definitions in the loop body
  300. Vector3 toRegionPos;
  301. double dis;
  302. int objtype;
  303. SceneObjectPart part;
  304. float dx;
  305. float dy;
  306. float dz;
  307. Quaternion q = SensePoint.GetWorldRotation();
  308. if (SensePoint.ParentGroup.IsAttachment)
  309. {
  310. // In attachments, rotate the sensor cone with the
  311. // avatar rotation. This may include a nonzero elevation if
  312. // in mouselook.
  313. // This will not include the rotation and position of the
  314. // attachment point (e.g. your head when a sensor is in your
  315. // hair attached to your scull. Your hair will turn with
  316. // your head but the sensor will stay with your (global)
  317. // avatar rotation and position.
  318. // Position of a sensor in a child prim attached to an avatar
  319. // will be still wrong.
  320. ScenePresence avatar = m_CmdManager.m_ScriptEngine.World.GetScenePresence(SensePoint.ParentGroup.AttachedAvatar);
  321. // Don't proceed if the avatar for this attachment has since been removed from the scene.
  322. if (avatar == null)
  323. return sensedEntities;
  324. q = avatar.GetWorldRotation() * q;
  325. }
  326. LSL_Types.Quaternion r = new LSL_Types.Quaternion(q);
  327. LSL_Types.Vector3 forward_dir = (new LSL_Types.Vector3(1, 0, 0) * r);
  328. double mag_fwd = LSL_Types.Vector3.Mag(forward_dir);
  329. Vector3 ZeroVector = new Vector3(0, 0, 0);
  330. bool nameSearch = (ts.name != null && ts.name != "");
  331. foreach (EntityBase ent in Entities)
  332. {
  333. bool keep = true;
  334. if (nameSearch && ent.Name != ts.name) // Wrong name and it is a named search
  335. continue;
  336. if (ent.IsDeleted) // taken so long to do this it has gone from the scene
  337. continue;
  338. if (!(ent is SceneObjectGroup)) // dont bother if it is a pesky avatar
  339. continue;
  340. toRegionPos = ent.AbsolutePosition;
  341. // Calculation is in line for speed
  342. dx = toRegionPos.X - fromRegionPos.X;
  343. dy = toRegionPos.Y - fromRegionPos.Y;
  344. dz = toRegionPos.Z - fromRegionPos.Z;
  345. // Weed out those that will not fit in a cube the size of the range
  346. // no point calculating if they are within a sphere the size of the range
  347. // if they arent even in the cube
  348. if (Math.Abs(dx) > ts.range || Math.Abs(dy) > ts.range || Math.Abs(dz) > ts.range)
  349. dis = ts.range + 1.0;
  350. else
  351. dis = Math.Sqrt(dx * dx + dy * dy + dz * dz);
  352. if (keep && dis <= ts.range && ts.host.UUID != ent.UUID)
  353. {
  354. // In Range and not the object containing the script, is it the right Type ?
  355. objtype = 0;
  356. part = ((SceneObjectGroup)ent).RootPart;
  357. if (part.ParentGroup.AttachmentPoint != 0) // Attached so ignore
  358. continue;
  359. if (part.Inventory.ContainsScripts())
  360. {
  361. objtype |= ACTIVE | SCRIPTED; // Scripted and active. It COULD have one hidden ...
  362. }
  363. else
  364. {
  365. if (ent.Velocity.Equals(ZeroVector))
  366. {
  367. objtype |= PASSIVE; // Passive non-moving
  368. }
  369. else
  370. {
  371. objtype |= ACTIVE; // moving so active
  372. }
  373. }
  374. // If any of the objects attributes match any in the requested scan type
  375. if (((ts.type & objtype) != 0))
  376. {
  377. // Right type too, what about the other params , key and name ?
  378. if (ts.arc < Math.PI)
  379. {
  380. // not omni-directional. Can you see it ?
  381. // vec forward_dir = llRot2Fwd(llGetRot())
  382. // vec obj_dir = toRegionPos-fromRegionPos
  383. // dot=dot(forward_dir,obj_dir)
  384. // mag_fwd = mag(forward_dir)
  385. // mag_obj = mag(obj_dir)
  386. // ang = acos(dot /(mag_fwd*mag_obj))
  387. double ang_obj = 0;
  388. try
  389. {
  390. Vector3 diff = toRegionPos - fromRegionPos;
  391. double dot = LSL_Types.Vector3.Dot(forward_dir, diff);
  392. double mag_obj = LSL_Types.Vector3.Mag(diff);
  393. ang_obj = Math.Acos(dot / (mag_fwd * mag_obj));
  394. }
  395. catch
  396. {
  397. }
  398. if (ang_obj > ts.arc) keep = false;
  399. }
  400. if (keep == true)
  401. {
  402. // add distance for sorting purposes later
  403. sensedEntities.Add(new SensedEntity(dis, ent.UUID));
  404. }
  405. }
  406. }
  407. }
  408. return sensedEntities;
  409. }
  410. private List<SensedEntity> doAgentSensor(SensorInfo ts)
  411. {
  412. List<SensedEntity> sensedEntities = new List<SensedEntity>();
  413. // If nobody about quit fast
  414. if (m_CmdManager.m_ScriptEngine.World.GetRootAgentCount() == 0)
  415. return sensedEntities;
  416. SceneObjectPart SensePoint = ts.host;
  417. Vector3 fromRegionPos = SensePoint.GetWorldPosition();
  418. Quaternion q = SensePoint.GetWorldRotation();
  419. if (SensePoint.ParentGroup.IsAttachment)
  420. {
  421. // In attachments, rotate the sensor cone with the
  422. // avatar rotation. This may include a nonzero elevation if
  423. // in mouselook.
  424. // This will not include the rotation and position of the
  425. // attachment point (e.g. your head when a sensor is in your
  426. // hair attached to your scull. Your hair will turn with
  427. // your head but the sensor will stay with your (global)
  428. // avatar rotation and position.
  429. // Position of a sensor in a child prim attached to an avatar
  430. // will be still wrong.
  431. ScenePresence avatar = m_CmdManager.m_ScriptEngine.World.GetScenePresence(SensePoint.ParentGroup.AttachedAvatar);
  432. // Don't proceed if the avatar for this attachment has since been removed from the scene.
  433. if (avatar == null)
  434. return sensedEntities;
  435. q = avatar.GetWorldRotation() * q;
  436. }
  437. LSL_Types.Quaternion r = new LSL_Types.Quaternion(q);
  438. LSL_Types.Vector3 forward_dir = (new LSL_Types.Vector3(1, 0, 0) * r);
  439. double mag_fwd = LSL_Types.Vector3.Mag(forward_dir);
  440. bool attached = (SensePoint.ParentGroup.AttachmentPoint != 0);
  441. Vector3 toRegionPos;
  442. double dis;
  443. Action<ScenePresence> senseEntity = new Action<ScenePresence>(presence =>
  444. {
  445. // m_log.DebugFormat(
  446. // "[SENSOR REPEAT]: Inspecting scene presence {0}, type {1} on sensor sweep for {2}, type {3}",
  447. // presence.Name, presence.PresenceType, ts.name, ts.type);
  448. if ((ts.type & NPC) == 0 && (ts.type & OS_NPC) == 0 && presence.PresenceType == PresenceType.Npc)
  449. {
  450. INPC npcData = m_npcModule.GetNPC(presence.UUID, presence.Scene);
  451. if (npcData == null || !npcData.SenseAsAgent)
  452. {
  453. // m_log.DebugFormat(
  454. // "[SENSOR REPEAT]: Discarding NPC {0} from agent sense sweep for script item id {1}",
  455. // presence.Name, ts.itemID);
  456. return;
  457. }
  458. }
  459. if ((ts.type & AGENT) == 0)
  460. {
  461. if (presence.PresenceType == PresenceType.User)
  462. {
  463. return;
  464. }
  465. else
  466. {
  467. INPC npcData = m_npcModule.GetNPC(presence.UUID, presence.Scene);
  468. if (npcData != null && npcData.SenseAsAgent)
  469. {
  470. // m_log.DebugFormat(
  471. // "[SENSOR REPEAT]: Discarding NPC {0} from non-agent sense sweep for script item id {1}",
  472. // presence.Name, ts.itemID);
  473. return;
  474. }
  475. }
  476. }
  477. if (presence.IsDeleted || presence.IsChildAgent || presence.GodLevel > 0.0)
  478. return;
  479. // if the object the script is in is attached and the avatar is the owner
  480. // then this one is not wanted
  481. if (attached && presence.UUID == SensePoint.OwnerID)
  482. return;
  483. toRegionPos = presence.AbsolutePosition;
  484. dis = Math.Abs(Util.GetDistanceTo(toRegionPos, fromRegionPos));
  485. // Disabled for now since all osNpc* methods check for appropriate ownership permission.
  486. // Perhaps could be re-enabled as an NPC setting at some point since being able to make NPCs not
  487. // sensed might be useful.
  488. // if (presence.PresenceType == PresenceType.Npc && npcModule != null)
  489. // {
  490. // UUID npcOwner = npcModule.GetOwner(presence.UUID);
  491. // if (npcOwner != UUID.Zero && npcOwner != SensePoint.OwnerID)
  492. // return;
  493. // }
  494. // are they in range
  495. if (dis <= ts.range)
  496. {
  497. // Are they in the required angle of view
  498. if (ts.arc < Math.PI)
  499. {
  500. // not omni-directional. Can you see it ?
  501. // vec forward_dir = llRot2Fwd(llGetRot())
  502. // vec obj_dir = toRegionPos-fromRegionPos
  503. // dot=dot(forward_dir,obj_dir)
  504. // mag_fwd = mag(forward_dir)
  505. // mag_obj = mag(obj_dir)
  506. // ang = acos(dot /(mag_fwd*mag_obj))
  507. double ang_obj = 0;
  508. try
  509. {
  510. LSL_Types.Vector3 obj_dir = new LSL_Types.Vector3(
  511. toRegionPos - fromRegionPos);
  512. double dot = LSL_Types.Vector3.Dot(forward_dir, obj_dir);
  513. double mag_obj = LSL_Types.Vector3.Mag(obj_dir);
  514. ang_obj = Math.Acos(dot / (mag_fwd * mag_obj));
  515. }
  516. catch
  517. {
  518. }
  519. if (ang_obj <= ts.arc)
  520. {
  521. sensedEntities.Add(new SensedEntity(dis, presence.UUID));
  522. }
  523. }
  524. else
  525. {
  526. sensedEntities.Add(new SensedEntity(dis, presence.UUID));
  527. }
  528. }
  529. });
  530. // If this is an avatar sense by key try to get them directly
  531. // rather than getting a list to scan through
  532. if (ts.keyID != UUID.Zero)
  533. {
  534. ScenePresence sp;
  535. // Try direct lookup by UUID
  536. if (!m_CmdManager.m_ScriptEngine.World.TryGetScenePresence(ts.keyID, out sp))
  537. return sensedEntities;
  538. senseEntity(sp);
  539. }
  540. else if (ts.name != null && ts.name != "")
  541. {
  542. ScenePresence sp;
  543. // Try lookup by name will return if/when found
  544. if (((ts.type & AGENT) != 0) && m_CmdManager.m_ScriptEngine.World.TryGetAvatarByName(ts.name, out sp))
  545. senseEntity(sp);
  546. if ((ts.type & AGENT_BY_USERNAME) != 0)
  547. {
  548. m_CmdManager.m_ScriptEngine.World.ForEachRootScenePresence(
  549. delegate (ScenePresence ssp)
  550. {
  551. if (ssp.Lastname == "Resident")
  552. {
  553. if (ssp.Firstname.ToLower() == ts.name)
  554. senseEntity(ssp);
  555. return;
  556. }
  557. if (ssp.Name.Replace(" ", ".").ToLower() == ts.name)
  558. senseEntity(ssp);
  559. }
  560. );
  561. }
  562. return sensedEntities;
  563. }
  564. else
  565. {
  566. m_CmdManager.m_ScriptEngine.World.ForEachRootScenePresence(senseEntity);
  567. }
  568. return sensedEntities;
  569. }
  570. public Object[] GetSerializationData(UUID itemID)
  571. {
  572. List<Object> data = new List<Object>();
  573. foreach (SensorInfo ts in SenseRepeaters)
  574. {
  575. if (ts.itemID == itemID)
  576. {
  577. data.Add(ts.interval);
  578. data.Add(ts.name);
  579. data.Add(ts.keyID);
  580. data.Add(ts.type);
  581. data.Add(ts.range);
  582. data.Add(ts.arc);
  583. }
  584. }
  585. return data.ToArray();
  586. }
  587. public void CreateFromData(uint localID, UUID itemID, UUID objectID,
  588. Object[] data)
  589. {
  590. SceneObjectPart part =
  591. m_CmdManager.m_ScriptEngine.World.GetSceneObjectPart(
  592. objectID);
  593. if (part == null)
  594. return;
  595. int idx = 0;
  596. while (idx < data.Length)
  597. {
  598. SensorInfo ts = new SensorInfo();
  599. ts.localID = localID;
  600. ts.itemID = itemID;
  601. ts.interval = (double)data[idx];
  602. ts.name = (string)data[idx+1];
  603. ts.keyID = (UUID)data[idx+2];
  604. ts.type = (int)data[idx+3];
  605. ts.range = (double)data[idx+4];
  606. ts.arc = (double)data[idx+5];
  607. ts.host = part;
  608. ts.next =
  609. DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
  610. AddSenseRepeater(ts);
  611. idx += 6;
  612. }
  613. }
  614. public List<SensorInfo> GetSensorInfo()
  615. {
  616. List<SensorInfo> retList = new List<SensorInfo>();
  617. lock (SenseRepeatListLock)
  618. {
  619. foreach (SensorInfo i in SenseRepeaters)
  620. retList.Add(i.Clone());
  621. }
  622. return retList;
  623. }
  624. }
  625. }