ExtendedPhysics.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 copyrightD
  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 System.Linq;
  30. using System.Reflection;
  31. using System.Text;
  32. using OpenSim.Framework;
  33. using OpenSim.Region.CoreModules;
  34. using OpenSim.Region.Framework;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. using Mono.Addins;
  38. using Nini.Config;
  39. using log4net;
  40. using OpenMetaverse;
  41. namespace OpenSim.Region.OptionalModules.Scripting
  42. {
  43. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")]
  44. public class ExtendedPhysics : INonSharedRegionModule
  45. {
  46. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  47. private static string LogHeader = "[EXTENDED PHYSICS]";
  48. // =============================================================
  49. // Since BulletSim is a plugin, this these values aren't defined easily in one place.
  50. // This table must correspond to an identical table in BSScene.
  51. // Per scene functions. See BSScene.
  52. // Per avatar functions. See BSCharacter.
  53. // Per prim functions. See BSPrim.
  54. public const string PhysFunctGetLinksetType = "BulletSim.GetLinksetType";
  55. public const string PhysFunctSetLinksetType = "BulletSim.SetLinksetType";
  56. // =============================================================
  57. private IConfig Configuration { get; set; }
  58. private bool Enabled { get; set; }
  59. private Scene BaseScene { get; set; }
  60. private IScriptModuleComms Comms { get; set; }
  61. #region INonSharedRegionModule
  62. public string Name { get { return this.GetType().Name; } }
  63. public void Initialise(IConfigSource config)
  64. {
  65. BaseScene = null;
  66. Enabled = false;
  67. Configuration = null;
  68. Comms = null;
  69. try
  70. {
  71. if ((Configuration = config.Configs["ExtendedPhysics"]) != null)
  72. {
  73. Enabled = Configuration.GetBoolean("Enabled", Enabled);
  74. }
  75. }
  76. catch (Exception e)
  77. {
  78. m_log.ErrorFormat("{0} Initialization error: {0}", LogHeader, e);
  79. }
  80. m_log.InfoFormat("{0} module {1} enabled", LogHeader, (Enabled ? "is" : "is not"));
  81. }
  82. public void Close()
  83. {
  84. if (BaseScene != null)
  85. {
  86. BaseScene.EventManager.OnObjectAddedToScene -= EventManager_OnObjectAddedToScene;
  87. BaseScene.EventManager.OnSceneObjectPartUpdated -= EventManager_OnSceneObjectPartUpdated;
  88. BaseScene = null;
  89. }
  90. }
  91. public void AddRegion(Scene scene)
  92. {
  93. }
  94. public void RemoveRegion(Scene scene)
  95. {
  96. if (BaseScene != null && BaseScene == scene)
  97. {
  98. Close();
  99. }
  100. }
  101. public void RegionLoaded(Scene scene)
  102. {
  103. if (!Enabled) return;
  104. BaseScene = scene;
  105. Comms = BaseScene.RequestModuleInterface<IScriptModuleComms>();
  106. if (Comms == null)
  107. {
  108. m_log.WarnFormat("{0} ScriptModuleComms interface not defined", LogHeader);
  109. Enabled = false;
  110. return;
  111. }
  112. // Register as LSL functions all the [ScriptInvocation] marked methods.
  113. Comms.RegisterScriptInvocations(this);
  114. Comms.RegisterConstants(this);
  115. // When an object is modified, we might need to update its extended physics parameters
  116. BaseScene.EventManager.OnObjectAddedToScene += EventManager_OnObjectAddedToScene;
  117. BaseScene.EventManager.OnSceneObjectPartUpdated += EventManager_OnSceneObjectPartUpdated;
  118. }
  119. public Type ReplaceableInterface { get { return null; } }
  120. #endregion // INonSharedRegionModule
  121. private void EventManager_OnObjectAddedToScene(SceneObjectGroup obj)
  122. {
  123. }
  124. // Event generated when some property of a prim changes.
  125. private void EventManager_OnSceneObjectPartUpdated(SceneObjectPart sop, bool isFullUpdate)
  126. {
  127. }
  128. [ScriptConstant]
  129. public static int PHYS_CENTER_OF_MASS = 1 << 0;
  130. [ScriptInvocation]
  131. public string physGetEngineType(UUID hostID, UUID scriptID)
  132. {
  133. string ret = string.Empty;
  134. if (BaseScene.PhysicsScene != null)
  135. {
  136. ret = BaseScene.PhysicsScene.EngineType;
  137. }
  138. return ret;
  139. }
  140. [ScriptConstant]
  141. public static int PHYS_LINKSET_TYPE_CONSTRAINT = 0;
  142. [ScriptConstant]
  143. public static int PHYS_LINKSET_TYPE_COMPOUND = 1;
  144. [ScriptConstant]
  145. public static int PHYS_LINKSET_TYPE_MANUAL = 2;
  146. [ScriptInvocation]
  147. public int physSetLinksetType(UUID hostID, UUID scriptID, int linksetType)
  148. {
  149. int ret = -1;
  150. if (!Enabled) return ret;
  151. // The part that is requesting the change.
  152. SceneObjectPart requestingPart = BaseScene.GetSceneObjectPart(hostID);
  153. if (requestingPart != null)
  154. {
  155. // The change is always made to the root of a linkset.
  156. SceneObjectGroup containingGroup = requestingPart.ParentGroup;
  157. SceneObjectPart rootPart = containingGroup.RootPart;
  158. if (rootPart != null)
  159. {
  160. Physics.Manager.PhysicsActor rootPhysActor = rootPart.PhysActor;
  161. if (rootPhysActor != null)
  162. {
  163. ret = (int)rootPhysActor.Extension(PhysFunctSetLinksetType, linksetType);
  164. }
  165. else
  166. {
  167. m_log.WarnFormat("{0} physSetLinksetType: root part does not have a physics actor. rootName={1}, hostID={2}",
  168. LogHeader, rootPart.Name, hostID);
  169. }
  170. }
  171. else
  172. {
  173. m_log.WarnFormat("{0} physSetLinksetType: root part does not exist. RequestingPartName={1}, hostID={2}",
  174. LogHeader, requestingPart.Name, hostID);
  175. }
  176. }
  177. else
  178. {
  179. m_log.WarnFormat("{0} physSetLinsetType: cannot find script object in scene. hostID={1}", LogHeader, hostID);
  180. }
  181. return ret;
  182. }
  183. [ScriptInvocation]
  184. public int physGetLinksetType(UUID hostID, UUID scriptID)
  185. {
  186. int ret = -1;
  187. if (!Enabled) return ret;
  188. // The part that is requesting the change.
  189. SceneObjectPart requestingPart = BaseScene.GetSceneObjectPart(hostID);
  190. if (requestingPart != null)
  191. {
  192. // The type is is always on the root of a linkset.
  193. SceneObjectGroup containingGroup = requestingPart.ParentGroup;
  194. SceneObjectPart rootPart = containingGroup.RootPart;
  195. if (rootPart != null)
  196. {
  197. Physics.Manager.PhysicsActor rootPhysActor = rootPart.PhysActor;
  198. if (rootPhysActor != null)
  199. {
  200. ret = (int)rootPhysActor.Extension(PhysFunctGetLinksetType);
  201. }
  202. else
  203. {
  204. m_log.WarnFormat("{0} physGetLinksetType: root part does not have a physics actor. rootName={1}, hostID={2}",
  205. LogHeader, rootPart.Name, hostID);
  206. }
  207. }
  208. else
  209. {
  210. m_log.WarnFormat("{0} physGetLinksetType: root part does not exist. RequestingPartName={1}, hostID={2}",
  211. LogHeader, requestingPart.Name, hostID);
  212. }
  213. }
  214. else
  215. {
  216. m_log.WarnFormat("{0} physGetLinsetType: cannot find script object in scene. hostID={1}", LogHeader, hostID);
  217. }
  218. return ret;
  219. }
  220. }
  221. }