SensorRepeat.cs 28 KB

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