PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/OpenSim/Region/ScriptEngine/Shared/Api/Implementation/Plugins/SensorRepeat.cs

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