SensorRepeat.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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.Collections.Generic;
  29. using OpenMetaverse;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.Framework.Scenes;
  32. using OpenSim.Region.ScriptEngine.Shared;
  33. using OpenSim.Region.ScriptEngine.Shared.Api;
  34. namespace OpenSim.Region.ScriptEngine.Shared.Api.Plugins
  35. {
  36. public class SensorRepeat
  37. {
  38. public AsyncCommandManager m_CmdManager;
  39. public SensorRepeat(AsyncCommandManager CmdManager)
  40. {
  41. m_CmdManager = CmdManager;
  42. maximumRange = CmdManager.m_ScriptEngine.Config.GetDouble("SensorMaxRange", 96.0d);
  43. maximumToReturn = CmdManager.m_ScriptEngine.Config.GetInt("SensorMaxResults", 16);
  44. }
  45. private Object SenseLock = new Object();
  46. private const int AGENT = 1;
  47. private const int ACTIVE = 2;
  48. private const int PASSIVE = 4;
  49. private const int SCRIPTED = 8;
  50. private double maximumRange = 96.0;
  51. private int maximumToReturn = 16;
  52. //
  53. // SenseRepeater and Sensors
  54. //
  55. private class SenseRepeatClass
  56. {
  57. public uint localID;
  58. public UUID itemID;
  59. public double interval;
  60. public DateTime next;
  61. public string name;
  62. public UUID keyID;
  63. public int type;
  64. public double range;
  65. public double arc;
  66. public SceneObjectPart host;
  67. }
  68. //
  69. // Sensed entity
  70. //
  71. private class SensedEntity : IComparable
  72. {
  73. public SensedEntity(double detectedDistance, UUID detectedID)
  74. {
  75. distance = detectedDistance;
  76. itemID = detectedID;
  77. }
  78. public int CompareTo(object obj)
  79. {
  80. if (!(obj is SensedEntity)) throw new InvalidOperationException();
  81. SensedEntity ent = (SensedEntity)obj;
  82. if (ent == null || ent.distance < distance) return 1;
  83. if (ent.distance > distance) return -1;
  84. return 0;
  85. }
  86. public UUID itemID;
  87. public double distance;
  88. }
  89. private List<SenseRepeatClass> SenseRepeaters = new List<SenseRepeatClass>();
  90. private object SenseRepeatListLock = new object();
  91. public void SetSenseRepeatEvent(uint m_localID, UUID m_itemID,
  92. string name, UUID keyID, int type, double range,
  93. double arc, double sec, SceneObjectPart host)
  94. {
  95. // Always remove first, in case this is a re-set
  96. UnSetSenseRepeaterEvents(m_localID, m_itemID);
  97. if (sec == 0) // Disabling timer
  98. return;
  99. // Add to timer
  100. SenseRepeatClass ts = new SenseRepeatClass();
  101. ts.localID = m_localID;
  102. ts.itemID = m_itemID;
  103. ts.interval = sec;
  104. ts.name = name;
  105. ts.keyID = keyID;
  106. ts.type = type;
  107. if (range > maximumRange)
  108. ts.range = maximumRange;
  109. else
  110. ts.range = range;
  111. ts.arc = arc;
  112. ts.host = host;
  113. ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
  114. lock (SenseRepeatListLock)
  115. {
  116. SenseRepeaters.Add(ts);
  117. }
  118. }
  119. public void UnSetSenseRepeaterEvents(uint m_localID, UUID m_itemID)
  120. {
  121. // Remove from timer
  122. lock (SenseRepeatListLock)
  123. {
  124. List<SenseRepeatClass> NewSensors = new List<SenseRepeatClass>();
  125. foreach (SenseRepeatClass ts in SenseRepeaters)
  126. {
  127. if (ts.localID != m_localID && ts.itemID != m_itemID)
  128. {
  129. NewSensors.Add(ts);
  130. }
  131. }
  132. SenseRepeaters.Clear();
  133. SenseRepeaters = NewSensors;
  134. }
  135. }
  136. public void CheckSenseRepeaterEvents()
  137. {
  138. // Nothing to do here?
  139. if (SenseRepeaters.Count == 0)
  140. return;
  141. lock (SenseRepeatListLock)
  142. {
  143. // Go through all timers
  144. foreach (SenseRepeatClass ts in SenseRepeaters)
  145. {
  146. // Time has passed?
  147. if (ts.next.ToUniversalTime() < DateTime.Now.ToUniversalTime())
  148. {
  149. SensorSweep(ts);
  150. // set next interval
  151. ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
  152. }
  153. }
  154. } // lock
  155. }
  156. public void SenseOnce(uint m_localID, UUID m_itemID,
  157. string name, UUID keyID, int type,
  158. double range, double arc, SceneObjectPart host)
  159. {
  160. // Add to timer
  161. SenseRepeatClass ts = new SenseRepeatClass();
  162. ts.localID = m_localID;
  163. ts.itemID = m_itemID;
  164. ts.interval = 0;
  165. ts.name = name;
  166. ts.keyID = keyID;
  167. ts.type = type;
  168. if (range > maximumRange)
  169. ts.range = maximumRange;
  170. else
  171. ts.range = range;
  172. ts.arc = arc;
  173. ts.host = host;
  174. SensorSweep(ts);
  175. }
  176. private void SensorSweep(SenseRepeatClass ts)
  177. {
  178. if (ts.host == null)
  179. {
  180. return;
  181. }
  182. List<SensedEntity> sensedEntities = new List<SensedEntity>();
  183. // Is the sensor type is AGENT and not SCRIPTED then include agents
  184. if ((ts.type & AGENT) != 0 && (ts.type & SCRIPTED) == 0)
  185. {
  186. sensedEntities.AddRange(doAgentSensor(ts));
  187. }
  188. // If SCRIPTED or PASSIVE or ACTIVE check objects
  189. if ((ts.type & SCRIPTED) != 0 || (ts.type & PASSIVE) != 0 || (ts.type & ACTIVE) != 0)
  190. {
  191. sensedEntities.AddRange(doObjectSensor(ts));
  192. }
  193. lock (SenseLock)
  194. {
  195. if (sensedEntities.Count == 0)
  196. {
  197. // send a "no_sensor"
  198. // Add it to queue
  199. m_CmdManager.m_ScriptEngine.PostScriptEvent(ts.itemID,
  200. new EventParams("no_sensor", new Object[0],
  201. new DetectParams[0]));
  202. }
  203. else
  204. {
  205. // Sort the list to get everything ordered by distance
  206. sensedEntities.Sort();
  207. int count = sensedEntities.Count;
  208. int idx;
  209. List<DetectParams> detected = new List<DetectParams>();
  210. for (idx = 0; idx < count; idx++)
  211. {
  212. try
  213. {
  214. DetectParams detect = new DetectParams();
  215. detect.Key = sensedEntities[idx].itemID;
  216. detect.Populate(m_CmdManager.m_ScriptEngine.World);
  217. detected.Add(detect);
  218. }
  219. catch (Exception)
  220. {
  221. // Ignore errors, the object has been deleted or the avatar has gone and
  222. // there was a problem in detect.Populate so nothing added to the list
  223. }
  224. if (detected.Count == maximumToReturn)
  225. break;
  226. }
  227. if (detected.Count == 0)
  228. {
  229. // To get here with zero in the list there must have been some sort of problem
  230. // like the object being deleted or the avatar leaving to have caused some
  231. // difficulty during the Populate above so fire a no_sensor event
  232. m_CmdManager.m_ScriptEngine.PostScriptEvent(ts.itemID,
  233. new EventParams("no_sensor", new Object[0],
  234. new DetectParams[0]));
  235. }
  236. else
  237. {
  238. m_CmdManager.m_ScriptEngine.PostScriptEvent(ts.itemID,
  239. new EventParams("sensor",
  240. new Object[] {new LSL_Types.LSLInteger(detected.Count) },
  241. detected.ToArray()));
  242. }
  243. }
  244. }
  245. }
  246. private List<SensedEntity> doObjectSensor(SenseRepeatClass ts)
  247. {
  248. List<EntityBase> Entities;
  249. List<SensedEntity> sensedEntities = new List<SensedEntity>();
  250. // If this is an object sense by key try to get it directly
  251. // rather than getting a list to scan through
  252. if (ts.keyID != UUID.Zero)
  253. {
  254. EntityBase e = null;
  255. m_CmdManager.m_ScriptEngine.World.Entities.TryGetValue(ts.keyID, out e);
  256. if (e == null)
  257. return sensedEntities;
  258. Entities = new List<EntityBase>();
  259. Entities.Add(e);
  260. }
  261. else
  262. {
  263. Entities = m_CmdManager.m_ScriptEngine.World.GetEntities();
  264. }
  265. SceneObjectPart SensePoint = ts.host;
  266. Vector3 fromRegionPos = SensePoint.AbsolutePosition;
  267. // pre define some things to avoid repeated definitions in the loop body
  268. Vector3 toRegionPos;
  269. double dis;
  270. int objtype;
  271. SceneObjectPart part;
  272. float dx;
  273. float dy;
  274. float dz;
  275. Quaternion q = SensePoint.RotationOffset;
  276. if (SensePoint.ParentGroup.RootPart.IsAttachment)
  277. {
  278. // In attachments, the sensor cone always orients with the
  279. // avatar rotation. This may include a nonzero elevation if
  280. // in mouselook.
  281. ScenePresence avatar = m_CmdManager.m_ScriptEngine.World.GetScenePresence(SensePoint.ParentGroup.RootPart.AttachedAvatar);
  282. q = avatar.Rotation;
  283. }
  284. LSL_Types.Quaternion r = new LSL_Types.Quaternion(q.X, q.Y, q.Z, q.W);
  285. LSL_Types.Vector3 forward_dir = (new LSL_Types.Vector3(1, 0, 0) * r);
  286. double mag_fwd = LSL_Types.Vector3.Mag(forward_dir);
  287. Vector3 ZeroVector = new Vector3(0, 0, 0);
  288. bool nameSearch = (ts.name != null && ts.name != "");
  289. foreach (EntityBase ent in Entities)
  290. {
  291. bool keep = true;
  292. if (nameSearch && ent.Name != ts.name) // Wrong name and it is a named search
  293. continue;
  294. if (ent.IsDeleted) // taken so long to do this it has gone from the scene
  295. continue;
  296. if (!(ent is SceneObjectGroup)) // dont bother if it is a pesky avatar
  297. continue;
  298. toRegionPos = ent.AbsolutePosition;
  299. // Calculation is in line for speed
  300. dx = toRegionPos.X - fromRegionPos.X;
  301. dy = toRegionPos.Y - fromRegionPos.Y;
  302. dz = toRegionPos.Z - fromRegionPos.Z;
  303. // Weed out those that will not fit in a cube the size of the range
  304. // no point calculating if they are within a sphere the size of the range
  305. // if they arent even in the cube
  306. if (Math.Abs(dx) > ts.range || Math.Abs(dy) > ts.range || Math.Abs(dz) > ts.range)
  307. dis = ts.range + 1.0;
  308. else
  309. dis = Math.Sqrt(dx * dx + dy * dy + dz * dz);
  310. if (keep && dis <= ts.range && ts.host.UUID != ent.UUID)
  311. {
  312. // In Range and not the object containing the script, is it the right Type ?
  313. objtype = 0;
  314. part = ((SceneObjectGroup)ent).RootPart;
  315. if (part.AttachmentPoint != 0) // Attached so ignore
  316. continue;
  317. if (part.Inventory.ContainsScripts())
  318. {
  319. objtype |= ACTIVE | SCRIPTED; // Scripted and active. It COULD have one hidden ...
  320. }
  321. else
  322. {
  323. if (ent.Velocity.Equals(ZeroVector))
  324. {
  325. objtype |= PASSIVE; // Passive non-moving
  326. }
  327. else
  328. {
  329. objtype |= ACTIVE; // moving so active
  330. }
  331. }
  332. // If any of the objects attributes match any in the requested scan type
  333. if (((ts.type & objtype) != 0))
  334. {
  335. // Right type too, what about the other params , key and name ?
  336. if (ts.arc < Math.PI)
  337. {
  338. // not omni-directional. Can you see it ?
  339. // vec forward_dir = llRot2Fwd(llGetRot())
  340. // vec obj_dir = toRegionPos-fromRegionPos
  341. // dot=dot(forward_dir,obj_dir)
  342. // mag_fwd = mag(forward_dir)
  343. // mag_obj = mag(obj_dir)
  344. // ang = acos(dot /(mag_fwd*mag_obj))
  345. double ang_obj = 0;
  346. try
  347. {
  348. Vector3 diff = toRegionPos - fromRegionPos;
  349. LSL_Types.Vector3 obj_dir = new LSL_Types.Vector3(diff.X, diff.Y, diff.Z);
  350. double dot = LSL_Types.Vector3.Dot(forward_dir, obj_dir);
  351. double mag_obj = LSL_Types.Vector3.Mag(obj_dir);
  352. ang_obj = Math.Acos(dot / (mag_fwd * mag_obj));
  353. }
  354. catch
  355. {
  356. }
  357. if (ang_obj > ts.arc) keep = false;
  358. }
  359. if (keep == true)
  360. {
  361. // add distance for sorting purposes later
  362. sensedEntities.Add(new SensedEntity(dis, ent.UUID));
  363. }
  364. }
  365. }
  366. }
  367. return sensedEntities;
  368. }
  369. private List<SensedEntity> doAgentSensor(SenseRepeatClass ts)
  370. {
  371. List<SensedEntity> sensedEntities = new List<SensedEntity>();
  372. // If nobody about quit fast
  373. if(m_CmdManager.m_ScriptEngine.World.GetRootAgentCount() == 0)
  374. return sensedEntities;
  375. SceneObjectPart SensePoint = ts.host;
  376. Vector3 fromRegionPos = SensePoint.AbsolutePosition;
  377. Quaternion q = SensePoint.RotationOffset;
  378. LSL_Types.Quaternion r = new LSL_Types.Quaternion(q.X, q.Y, q.Z, q.W);
  379. LSL_Types.Vector3 forward_dir = (new LSL_Types.Vector3(1, 0, 0) * r);
  380. double mag_fwd = LSL_Types.Vector3.Mag(forward_dir);
  381. bool attached = (SensePoint.AttachmentPoint != 0);
  382. Vector3 toRegionPos;
  383. double dis;
  384. Action<ScenePresence> senseEntity = new Action<ScenePresence>(delegate(ScenePresence presence)
  385. {
  386. if (presence.IsDeleted || presence.IsChildAgent || presence.GodLevel > 0.0)
  387. return;
  388. // if the object the script is in is attached and the avatar is the owner
  389. // then this one is not wanted
  390. if (attached && presence.UUID == SensePoint.OwnerID)
  391. return;
  392. toRegionPos = presence.AbsolutePosition;
  393. dis = Math.Abs(Util.GetDistanceTo(toRegionPos, fromRegionPos));
  394. // are they in range
  395. if (dis <= ts.range)
  396. {
  397. // Are they in the required angle of view
  398. if (ts.arc < Math.PI)
  399. {
  400. // not omni-directional. Can you see it ?
  401. // vec forward_dir = llRot2Fwd(llGetRot())
  402. // vec obj_dir = toRegionPos-fromRegionPos
  403. // dot=dot(forward_dir,obj_dir)
  404. // mag_fwd = mag(forward_dir)
  405. // mag_obj = mag(obj_dir)
  406. // ang = acos(dot /(mag_fwd*mag_obj))
  407. double ang_obj = 0;
  408. try
  409. {
  410. Vector3 diff = toRegionPos - fromRegionPos;
  411. LSL_Types.Vector3 obj_dir = new LSL_Types.Vector3(diff.X, diff.Y, diff.Z);
  412. double dot = LSL_Types.Vector3.Dot(forward_dir, obj_dir);
  413. double mag_obj = LSL_Types.Vector3.Mag(obj_dir);
  414. ang_obj = Math.Acos(dot / (mag_fwd * mag_obj));
  415. }
  416. catch
  417. {
  418. }
  419. if (ang_obj <= ts.arc)
  420. {
  421. sensedEntities.Add(new SensedEntity(dis, presence.UUID));
  422. }
  423. }
  424. else
  425. {
  426. sensedEntities.Add(new SensedEntity(dis, presence.UUID));
  427. }
  428. }
  429. });
  430. // If this is an avatar sense by key try to get them directly
  431. // rather than getting a list to scan through
  432. if (ts.keyID != UUID.Zero)
  433. {
  434. ScenePresence sp;
  435. // Try direct lookup by UUID
  436. if(!m_CmdManager.m_ScriptEngine.World.TryGetScenePresence(ts.keyID, out sp))
  437. return sensedEntities;
  438. senseEntity(sp);
  439. }
  440. else if (ts.name != null && ts.name != "")
  441. {
  442. ScenePresence sp;
  443. // Try lookup by name will return if/when found
  444. if (!m_CmdManager.m_ScriptEngine.World.TryGetAvatarByName(ts.name, out sp))
  445. return sensedEntities;
  446. senseEntity(sp);
  447. }
  448. else
  449. {
  450. m_CmdManager.m_ScriptEngine.World.ForEachScenePresence(senseEntity);
  451. }
  452. return sensedEntities;
  453. }
  454. public Object[] GetSerializationData(UUID itemID)
  455. {
  456. List<Object> data = new List<Object>();
  457. lock (SenseRepeatListLock)
  458. {
  459. foreach (SenseRepeatClass ts in SenseRepeaters)
  460. {
  461. if (ts.itemID == itemID)
  462. {
  463. data.Add(ts.interval);
  464. data.Add(ts.name);
  465. data.Add(ts.keyID);
  466. data.Add(ts.type);
  467. data.Add(ts.range);
  468. data.Add(ts.arc);
  469. }
  470. }
  471. }
  472. return data.ToArray();
  473. }
  474. public void CreateFromData(uint localID, UUID itemID, UUID objectID,
  475. Object[] data)
  476. {
  477. SceneObjectPart part =
  478. m_CmdManager.m_ScriptEngine.World.GetSceneObjectPart(
  479. objectID);
  480. if (part == null)
  481. return;
  482. int idx = 0;
  483. while (idx < data.Length)
  484. {
  485. SenseRepeatClass ts = new SenseRepeatClass();
  486. ts.localID = localID;
  487. ts.itemID = itemID;
  488. ts.interval = (double)data[idx];
  489. ts.name = (string)data[idx+1];
  490. ts.keyID = (UUID)data[idx+2];
  491. ts.type = (int)data[idx+3];
  492. ts.range = (double)data[idx+4];
  493. ts.arc = (double)data[idx+5];
  494. ts.host = part;
  495. ts.next =
  496. DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
  497. SenseRepeaters.Add(ts);
  498. idx += 6;
  499. }
  500. }
  501. }
  502. }