SensorRepeat.cs 29 KB

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