SensorRepeat.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 OpenSim 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. //#define SPAM
  28. using System;
  29. using System.Collections.Generic;
  30. using libsecondlife;
  31. using OpenSim.Framework;
  32. using OpenSim.Region.Environment.Scenes;
  33. namespace OpenSim.Region.ScriptEngine.Common.ScriptEngineBase.AsyncCommandPlugins
  34. {
  35. public class SensorRepeat
  36. {
  37. public AsyncCommandManager m_CmdManager;
  38. public SensorRepeat(AsyncCommandManager CmdManager)
  39. {
  40. m_CmdManager = CmdManager;
  41. }
  42. public Dictionary<uint, Dictionary<LLUUID, LSL_Types.list>> SenseEvents =
  43. new Dictionary<uint, Dictionary<LLUUID, LSL_Types.list>>();
  44. private Object SenseLock = new Object();
  45. //
  46. // SenseRepeater and Sensors
  47. //
  48. private class SenseRepeatClass
  49. {
  50. public uint localID;
  51. public LLUUID itemID;
  52. public double interval;
  53. public DateTime next;
  54. public string name;
  55. public LLUUID keyID;
  56. public int type;
  57. public double range;
  58. public double arc;
  59. public SceneObjectPart host;
  60. }
  61. private List<SenseRepeatClass> SenseRepeaters = new List<SenseRepeatClass>();
  62. private object SenseRepeatListLock = new object();
  63. public void SetSenseRepeatEvent(uint m_localID, LLUUID m_itemID,
  64. string name, LLUUID keyID, int type, double range, double arc, double sec, SceneObjectPart host)
  65. {
  66. #if SPAM
  67. Console.WriteLine("SetSensorEvent");
  68. #endif
  69. // Always remove first, in case this is a re-set
  70. UnSetSenseRepeaterEvents(m_localID, m_itemID);
  71. if (sec == 0) // Disabling timer
  72. return;
  73. // Add to timer
  74. SenseRepeatClass ts = new SenseRepeatClass();
  75. ts.localID = m_localID;
  76. ts.itemID = m_itemID;
  77. ts.interval = sec;
  78. ts.name = name;
  79. ts.keyID = keyID;
  80. ts.type = type;
  81. ts.range = range;
  82. ts.arc = arc;
  83. ts.host = host;
  84. ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
  85. lock (SenseRepeatListLock)
  86. {
  87. SenseRepeaters.Add(ts);
  88. }
  89. }
  90. public void UnSetSenseRepeaterEvents(uint m_localID, LLUUID m_itemID)
  91. {
  92. // Remove from timer
  93. lock (SenseRepeatListLock)
  94. {
  95. List<SenseRepeatClass> NewSensors = new List<SenseRepeatClass>();
  96. foreach (SenseRepeatClass ts in SenseRepeaters)
  97. {
  98. if (ts.localID != m_localID && ts.itemID != m_itemID)
  99. {
  100. NewSensors.Add(ts);
  101. }
  102. }
  103. SenseRepeaters.Clear();
  104. SenseRepeaters = NewSensors;
  105. }
  106. }
  107. public void CheckSenseRepeaterEvents()
  108. {
  109. // Nothing to do here?
  110. if (SenseRepeaters.Count == 0)
  111. return;
  112. lock (SenseRepeatListLock)
  113. {
  114. // Go through all timers
  115. foreach (SenseRepeatClass ts in SenseRepeaters)
  116. {
  117. // Time has passed?
  118. if (ts.next.ToUniversalTime() < DateTime.Now.ToUniversalTime())
  119. {
  120. SensorSweep(ts);
  121. // set next interval
  122. ts.next = DateTime.Now.ToUniversalTime().AddSeconds(ts.interval);
  123. }
  124. }
  125. } // lock
  126. }
  127. public void SenseOnce(uint m_localID, LLUUID m_itemID,
  128. string name, LLUUID keyID, int type,
  129. double range, double arc, SceneObjectPart host)
  130. {
  131. // Add to timer
  132. SenseRepeatClass ts = new SenseRepeatClass();
  133. ts.localID = m_localID;
  134. ts.itemID = m_itemID;
  135. ts.interval = 0;
  136. ts.name = name;
  137. ts.keyID = keyID;
  138. ts.type = type;
  139. ts.range = range;
  140. ts.arc = arc;
  141. ts.host = host;
  142. SensorSweep(ts);
  143. }
  144. public LSL_Types.list GetSensorList(uint m_localID, LLUUID m_itemID)
  145. {
  146. lock (SenseLock)
  147. {
  148. Dictionary<LLUUID, LSL_Types.list> Obj = null;
  149. if (!SenseEvents.TryGetValue(m_localID, out Obj))
  150. {
  151. #if SPAM
  152. m_CmdManager.m_ScriptEngine.Log.Info("[AsyncLSL]: GetSensorList missing localID: " + m_localID);
  153. #endif
  154. return null;
  155. }
  156. lock (Obj)
  157. {
  158. // Get script
  159. LSL_Types.list SenseList = null;
  160. if (!Obj.TryGetValue(m_itemID, out SenseList))
  161. {
  162. #if SPAM
  163. m_CmdManager.m_ScriptEngine.Log.Info("[AsyncLSL]: GetSensorList missing itemID: " + m_itemID);
  164. #endif
  165. return null;
  166. }
  167. return SenseList;
  168. }
  169. }
  170. }
  171. private void SensorSweep(SenseRepeatClass ts)
  172. {
  173. //m_ScriptEngine.Log.Info("[AsyncLSL]:Enter SensorSweep");
  174. SceneObjectPart SensePoint = ts.host;
  175. if (SensePoint == null)
  176. {
  177. #if SPAM
  178. //m_ScriptEngine.Log.Info("[AsyncLSL]: Enter SensorSweep (SensePoint == null) for "+ts.itemID.ToString());
  179. #endif
  180. return;
  181. }
  182. //m_ScriptEngine.Log.Info("[AsyncLSL]: Enter SensorSweep Scan");
  183. LLVector3 sensorPos = SensePoint.AbsolutePosition;
  184. LLVector3 regionPos = new LLVector3(m_CmdManager.m_ScriptEngine.World.RegionInfo.RegionLocX * Constants.RegionSize, m_CmdManager.m_ScriptEngine.World.RegionInfo.RegionLocY * Constants.RegionSize, 0);
  185. LLVector3 fromRegionPos = sensorPos + regionPos;
  186. LLQuaternion q = SensePoint.RotationOffset;
  187. LSL_Types.Quaternion r = new LSL_Types.Quaternion(q.X, q.Y, q.Z, q.W);
  188. LSL_Types.Vector3 forward_dir = (new LSL_Types.Vector3(1, 0, 0) * r);
  189. double mag_fwd = LSL_Types.Vector3.Mag(forward_dir);
  190. // Here we should do some smart culling ...
  191. // math seems quicker than strings so try that first
  192. LSL_Types.list SensedObjects = new LSL_Types.list();
  193. LSL_Types.Vector3 ZeroVector = new LSL_Types.Vector3(0, 0, 0);
  194. foreach (EntityBase ent in m_CmdManager.m_ScriptEngine.World.Entities.Values)
  195. {
  196. LLVector3 toRegionPos = ent.AbsolutePosition + regionPos;
  197. double dis = Math.Abs((double)Util.GetDistanceTo(toRegionPos, fromRegionPos));
  198. if (dis <= ts.range)
  199. {
  200. // In Range, is it the right Type ?
  201. int objtype = 0;
  202. if (m_CmdManager.m_ScriptEngine.World.GetScenePresence(ent.UUID) != null) objtype |= 0x01; // actor
  203. if (ent.Velocity.Equals(ZeroVector))
  204. objtype |= 0x04; // passive non-moving
  205. else
  206. objtype |= 0x02; // active moving
  207. if (ent is IScript) objtype |= 0x08; // Scripted. It COULD have one hidden ...
  208. if (((ts.type & objtype) != 0) || ((ts.type & objtype) == ts.type))
  209. {
  210. // docs claim AGENT|ACTIVE should find agent objects OR active objects
  211. // so the bitwise AND with object type should be non-zero
  212. // Right type too, what about the other params , key and name ?
  213. bool keep = true;
  214. if (ts.arc < Math.PI)
  215. {
  216. // not omni-directional. Can you see it ?
  217. // vec forward_dir = llRot2Fwd(llGetRot())
  218. // vec obj_dir = toRegionPos-fromRegionPos
  219. // dot=dot(forward_dir,obj_dir)
  220. // mag_fwd = mag(forward_dir)
  221. // mag_obj = mag(obj_dir)
  222. // ang = acos(dot /(mag_fwd*mag_obj))
  223. double ang_obj = 0;
  224. try
  225. {
  226. LLVector3 diff = toRegionPos - fromRegionPos;
  227. LSL_Types.Vector3 obj_dir = new LSL_Types.Vector3(diff.X, diff.Y, diff.Z);
  228. double dot = LSL_Types.Vector3.Dot(forward_dir, obj_dir);
  229. double mag_obj = LSL_Types.Vector3.Mag(obj_dir);
  230. ang_obj = Math.Acos(dot / (mag_fwd * mag_obj));
  231. }
  232. catch
  233. {
  234. }
  235. if (ang_obj > ts.arc) keep = false;
  236. }
  237. if (keep && (ts.keyID != LLUUID.Zero) && (ts.keyID != ent.UUID))
  238. {
  239. keep = false;
  240. }
  241. if (keep && (ts.name.Length > 0))
  242. {
  243. string avatarname=null;
  244. string objectname=null;
  245. string entname =ent.Name;
  246. // try avatar username surname
  247. UserProfileData profile = m_CmdManager.m_ScriptEngine.World.CommsManager.UserService.GetUserProfile(ent.UUID);
  248. if (profile != null)
  249. {
  250. avatarname = profile.FirstName + " " + profile.SurName;
  251. }
  252. // try an scene object
  253. SceneObjectPart SOP = m_CmdManager.m_ScriptEngine.World.GetSceneObjectPart(ent.UUID);
  254. if (SOP != null)
  255. {
  256. objectname = SOP.Name;
  257. }
  258. if ((ts.name != entname) && (ts.name != avatarname) && (ts.name != objectname))
  259. {
  260. keep = false;
  261. }
  262. }
  263. if (keep == true) SensedObjects.Add(ent.UUID);
  264. }
  265. }
  266. }
  267. #if SPAM
  268. //m_ScriptEngine.Log.Info("[AsyncLSL]: Enter SensorSweep SenseLock");
  269. #endif
  270. lock (SenseLock)
  271. {
  272. // Create object if it doesn't exist
  273. if (SenseEvents.ContainsKey(ts.localID) == false)
  274. {
  275. SenseEvents.Add(ts.localID, new Dictionary<LLUUID, LSL_Types.list>());
  276. }
  277. // clear if previous traces exist
  278. Dictionary<LLUUID, LSL_Types.list> Obj;
  279. SenseEvents.TryGetValue(ts.localID, out Obj);
  280. if (Obj.ContainsKey(ts.itemID) == true)
  281. Obj.Remove(ts.itemID);
  282. // note list may be zero length
  283. Obj.Add(ts.itemID, SensedObjects);
  284. if (SensedObjects.Length == 0)
  285. {
  286. // send a "no_sensor"
  287. // Add it to queue
  288. m_CmdManager.m_ScriptEngine.m_EventQueueManager.AddToScriptQueue(ts.localID, ts.itemID, "no_sensor", EventQueueManager.llDetectNull,
  289. new object[] { });
  290. }
  291. else
  292. {
  293. m_CmdManager.m_ScriptEngine.m_EventQueueManager.AddToScriptQueue(ts.localID, ts.itemID, "sensor", EventQueueManager.llDetectNull,
  294. new object[] { new LSL_Types.LSLInteger(SensedObjects.Length) });
  295. }
  296. m_CmdManager.m_ScriptEngine.World.EventManager.TriggerTimerEvent(ts.localID, ts.interval);
  297. }
  298. }
  299. }
  300. }