SensorRepeat.cs 13 KB

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