ExtendedPhysics.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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 System.Threading;
  33. using OpenSim.Framework;
  34. using OpenSim.Region.Framework;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. using OpenSim.Region.PhysicsModules.SharedBase;
  38. using Mono.Addins;
  39. using Nini.Config;
  40. using log4net;
  41. using OpenMetaverse;
  42. namespace OpenSim.Region.PhysicsModule.BulletS
  43. {
  44. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule")]
  45. public class ExtendedPhysics : INonSharedRegionModule
  46. {
  47. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  48. private static string LogHeader = "[EXTENDED PHYSICS]";
  49. // =============================================================
  50. // Since BulletSim is a plugin, this these values aren't defined easily in one place.
  51. // This table must correspond to an identical table in BSScene.
  52. // Per scene functions. See BSScene.
  53. // Per avatar functions. See BSCharacter.
  54. // Per prim functions. See BSPrim.
  55. public const string PhysFunctGetLinksetType = "BulletSim.GetLinksetType";
  56. public const string PhysFunctSetLinksetType = "BulletSim.SetLinksetType";
  57. public const string PhysFunctChangeLinkFixed = "BulletSim.ChangeLinkFixed";
  58. public const string PhysFunctChangeLinkType = "BulletSim.ChangeLinkType";
  59. public const string PhysFunctGetLinkType = "BulletSim.GetLinkType";
  60. public const string PhysFunctChangeLinkParams = "BulletSim.ChangeLinkParams";
  61. public const string PhysFunctAxisLockLimits = "BulletSim.AxisLockLimits";
  62. public const string PhysFunctDisableDeactivation = "BulletSim.DisableDeactivation";
  63. // =============================================================
  64. private IConfig Configuration { get; set; }
  65. private bool Enabled { get; set; }
  66. private Scene BaseScene { get; set; }
  67. private IScriptModuleComms Comms { get; set; }
  68. #region INonSharedRegionModule
  69. public string Name { get { return this.GetType().Name; } }
  70. public void Initialise(IConfigSource config)
  71. {
  72. BaseScene = null;
  73. Enabled = false;
  74. Configuration = null;
  75. Comms = null;
  76. try
  77. {
  78. if ((Configuration = config.Configs["ExtendedPhysics"]) != null)
  79. {
  80. //IConfig cStartup = config.Configs["Startup"];
  81. //if (cStartup?.GetString("physics", string.Empty) == "BulletSim")
  82. Enabled = Configuration.GetBoolean("Enabled", Enabled);
  83. }
  84. }
  85. catch (Exception e)
  86. {
  87. m_log.ErrorFormat("{0} Initialization error: {0}", LogHeader, e);
  88. }
  89. m_log.InfoFormat("{0} module {1} enabled", LogHeader, (Enabled ? "is" : "is not"));
  90. }
  91. public void Close()
  92. {
  93. if (BaseScene != null)
  94. {
  95. BaseScene.EventManager.OnObjectAddedToScene -= EventManager_OnObjectAddedToScene;
  96. BaseScene.EventManager.OnSceneObjectPartUpdated -= EventManager_OnSceneObjectPartUpdated;
  97. BaseScene = null;
  98. }
  99. }
  100. public void AddRegion(Scene scene)
  101. {
  102. }
  103. public void RemoveRegion(Scene scene)
  104. {
  105. if (BaseScene != null && BaseScene == scene)
  106. {
  107. Close();
  108. }
  109. }
  110. public void RegionLoaded(Scene scene)
  111. {
  112. if (!Enabled) return;
  113. BaseScene = scene;
  114. Comms = BaseScene.RequestModuleInterface<IScriptModuleComms>();
  115. if (Comms == null)
  116. {
  117. m_log.WarnFormat("{0} ScriptModuleComms interface not defined", LogHeader);
  118. Enabled = false;
  119. return;
  120. }
  121. // Register as LSL functions all the [ScriptInvocation] marked methods.
  122. // NOTE: YEngine keeps functions as region specific but constants are global static
  123. // so if this is used in multiple regions on a simulator, the constants will
  124. // show up as being redefined.
  125. Comms.RegisterScriptInvocations(this);
  126. Comms.RegisterConstants(this);
  127. // When an object is modified, we might need to update its extended physics parameters
  128. BaseScene.EventManager.OnObjectAddedToScene += EventManager_OnObjectAddedToScene;
  129. BaseScene.EventManager.OnSceneObjectPartUpdated += EventManager_OnSceneObjectPartUpdated;
  130. }
  131. public Type ReplaceableInterface { get { return null; } }
  132. #endregion // INonSharedRegionModule
  133. private void EventManager_OnObjectAddedToScene(SceneObjectGroup obj)
  134. {
  135. }
  136. // Event generated when some property of a prim changes.
  137. private void EventManager_OnSceneObjectPartUpdated(SceneObjectPart sop, bool isFullUpdate)
  138. {
  139. }
  140. [ScriptConstant]
  141. public const int PHYS_CENTER_OF_MASS = 1 << 0;
  142. [ScriptInvocation]
  143. public string physGetEngineType(UUID hostID, UUID scriptID)
  144. {
  145. string ret = string.Empty;
  146. if (BaseScene.PhysicsScene != null)
  147. {
  148. ret = BaseScene.PhysicsScene.EngineType;
  149. }
  150. return ret;
  151. }
  152. [ScriptInvocation]
  153. public float physDisableDeactivation(UUID hostId, UUID scriptId, int disable)
  154. {
  155. int ret = -1;
  156. if (!Enabled) return ret;
  157. if (BaseScene.PhysicsScene != null)
  158. {
  159. if (GetRootPhysActor(hostId, out PhysicsActor rootPhysActor))
  160. {
  161. m_log.DebugFormat("{0} physDisableDeactivation: hostId={1}, scriptId={2}, val={3}", LogHeader, hostId, scriptId, disable);
  162. object[] parms2 = { rootPhysActor, null, disable };
  163. ret = MakeIntError(rootPhysActor.Extension(PhysFunctDisableDeactivation, parms2));
  164. }
  165. }
  166. return ret;
  167. }
  168. // Code for specifying params.
  169. // The choice if 14700 is arbitrary and only serves to catch parameter code misuse.
  170. [ScriptConstant]
  171. public const int PHYS_AXIS_LOCK_LINEAR = 14700;
  172. [ScriptConstant]
  173. public const int PHYS_AXIS_LOCK_LINEAR_X = 14701;
  174. [ScriptConstant]
  175. public const int PHYS_AXIS_LIMIT_LINEAR_X = 14702;
  176. [ScriptConstant]
  177. public const int PHYS_AXIS_LOCK_LINEAR_Y = 14703;
  178. [ScriptConstant]
  179. public const int PHYS_AXIS_LIMIT_LINEAR_Y = 14704;
  180. [ScriptConstant]
  181. public const int PHYS_AXIS_LOCK_LINEAR_Z = 14705;
  182. [ScriptConstant]
  183. public const int PHYS_AXIS_LIMIT_LINEAR_Z = 14706;
  184. [ScriptConstant]
  185. public const int PHYS_AXIS_LOCK_ANGULAR = 14707;
  186. [ScriptConstant]
  187. public const int PHYS_AXIS_LOCK_ANGULAR_X = 14708;
  188. [ScriptConstant]
  189. public const int PHYS_AXIS_LIMIT_ANGULAR_X = 14709;
  190. [ScriptConstant]
  191. public const int PHYS_AXIS_LOCK_ANGULAR_Y = 14710;
  192. [ScriptConstant]
  193. public const int PHYS_AXIS_LIMIT_ANGULAR_Y = 14711;
  194. [ScriptConstant]
  195. public const int PHYS_AXIS_LOCK_ANGULAR_Z = 14712;
  196. [ScriptConstant]
  197. public const int PHYS_AXIS_LIMIT_ANGULAR_Z = 14713;
  198. [ScriptConstant]
  199. public const int PHYS_AXIS_UNLOCK_LINEAR = 14714;
  200. [ScriptConstant]
  201. public const int PHYS_AXIS_UNLOCK_LINEAR_X = 14715;
  202. [ScriptConstant]
  203. public const int PHYS_AXIS_UNLOCK_LINEAR_Y = 14716;
  204. [ScriptConstant]
  205. public const int PHYS_AXIS_UNLOCK_LINEAR_Z = 14717;
  206. [ScriptConstant]
  207. public const int PHYS_AXIS_UNLOCK_ANGULAR = 14718;
  208. [ScriptConstant]
  209. public const int PHYS_AXIS_UNLOCK_ANGULAR_X = 14719;
  210. [ScriptConstant]
  211. public const int PHYS_AXIS_UNLOCK_ANGULAR_Y = 14720;
  212. [ScriptConstant]
  213. public const int PHYS_AXIS_UNLOCK_ANGULAR_Z = 14721;
  214. [ScriptConstant]
  215. public const int PHYS_AXIS_UNLOCK = 14722;
  216. // physAxisLockLimits()
  217. [ScriptInvocation]
  218. public int physAxisLock(UUID hostID, UUID scriptID, object[] parms)
  219. {
  220. int ret = -1;
  221. if (!Enabled) return ret;
  222. PhysicsActor rootPhysActor;
  223. if (GetRootPhysActor(hostID, out rootPhysActor))
  224. {
  225. object[] parms2 = AddToBeginningOfArray(rootPhysActor, null, parms);
  226. ret = MakeIntError(rootPhysActor.Extension(PhysFunctAxisLockLimits, parms2));
  227. }
  228. return ret;
  229. }
  230. [ScriptConstant]
  231. public const int PHYS_LINKSET_TYPE_CONSTRAINT = 0;
  232. [ScriptConstant]
  233. public const int PHYS_LINKSET_TYPE_COMPOUND = 1;
  234. [ScriptConstant]
  235. public const int PHYS_LINKSET_TYPE_MANUAL = 2;
  236. [ScriptInvocation]
  237. public int physSetLinksetType(UUID hostID, UUID scriptID, int linksetType)
  238. {
  239. int ret = -1;
  240. if (!Enabled) return ret;
  241. // The part that is requesting the change.
  242. SceneObjectPart requestingPart = BaseScene.GetSceneObjectPart(hostID);
  243. if (requestingPart != null)
  244. {
  245. // The change is always made to the root of a linkset.
  246. SceneObjectGroup containingGroup = requestingPart.ParentGroup;
  247. SceneObjectPart rootPart = containingGroup.RootPart;
  248. if (rootPart != null)
  249. {
  250. PhysicsActor rootPhysActor = rootPart.PhysActor;
  251. if (rootPhysActor != null)
  252. {
  253. if (rootPhysActor.IsPhysical)
  254. {
  255. // Change a physical linkset by making non-physical, waiting for one heartbeat so all
  256. // the prim and linkset state is updated, changing the type and making the
  257. // linkset physical again.
  258. containingGroup.ScriptSetPhysicsStatus(false);
  259. Thread.Sleep(150); // longer than one heartbeat tick
  260. // A kludge for the moment.
  261. // Since compound linksets move the children but don't generate position updates to the
  262. // simulator, it is possible for compound linkset children to have out-of-sync simulator
  263. // and physical positions. The following causes the simulator to push the real child positions
  264. // down into the physics engine to get everything synced.
  265. containingGroup.UpdateGroupPosition(containingGroup.AbsolutePosition);
  266. containingGroup.UpdateGroupRotationR(containingGroup.GroupRotation);
  267. object[] parms2 = { rootPhysActor, null, linksetType };
  268. ret = MakeIntError(rootPhysActor.Extension(PhysFunctSetLinksetType, parms2));
  269. Thread.Sleep(150); // longer than one heartbeat tick
  270. containingGroup.ScriptSetPhysicsStatus(true);
  271. }
  272. else
  273. {
  274. // Non-physical linksets don't have a physical instantiation so there is no state to
  275. // worry about being updated.
  276. object[] parms2 = { rootPhysActor, null, linksetType };
  277. ret = MakeIntError(rootPhysActor.Extension(PhysFunctSetLinksetType, parms2));
  278. }
  279. }
  280. else
  281. {
  282. m_log.WarnFormat("{0} physSetLinksetType: root part does not have a physics actor. rootName={1}, hostID={2}",
  283. LogHeader, rootPart.Name, hostID);
  284. }
  285. }
  286. else
  287. {
  288. m_log.WarnFormat("{0} physSetLinksetType: root part does not exist. RequestingPartName={1}, hostID={2}",
  289. LogHeader, requestingPart.Name, hostID);
  290. }
  291. }
  292. else
  293. {
  294. m_log.WarnFormat("{0} physSetLinsetType: cannot find script object in scene. hostID={1}", LogHeader, hostID);
  295. }
  296. return ret;
  297. }
  298. [ScriptInvocation]
  299. public int physGetLinksetType(UUID hostID, UUID scriptID)
  300. {
  301. int ret = -1;
  302. if (!Enabled) return ret;
  303. PhysicsActor rootPhysActor;
  304. if (GetRootPhysActor(hostID, out rootPhysActor))
  305. {
  306. object[] parms2 = { rootPhysActor, null };
  307. ret = MakeIntError(rootPhysActor.Extension(PhysFunctGetLinksetType, parms2));
  308. }
  309. else
  310. {
  311. m_log.WarnFormat("{0} physGetLinsetType: cannot find script object in scene. hostID={1}", LogHeader, hostID);
  312. }
  313. return ret;
  314. }
  315. [ScriptConstant]
  316. public const int PHYS_LINK_TYPE_FIXED = 1234;
  317. [ScriptConstant]
  318. public const int PHYS_LINK_TYPE_HINGE = 4;
  319. [ScriptConstant]
  320. public const int PHYS_LINK_TYPE_SPRING = 9;
  321. [ScriptConstant]
  322. public const int PHYS_LINK_TYPE_6DOF = 6;
  323. [ScriptConstant]
  324. public const int PHYS_LINK_TYPE_SLIDER = 7;
  325. // physChangeLinkType(integer linkNum, integer typeCode)
  326. [ScriptInvocation]
  327. public int physChangeLinkType(UUID hostID, UUID scriptID, int linkNum, int typeCode)
  328. {
  329. int ret = -1;
  330. if (!Enabled) return ret;
  331. PhysicsActor rootPhysActor;
  332. PhysicsActor childPhysActor;
  333. if (GetRootAndChildPhysActors(hostID, linkNum, out rootPhysActor, out childPhysActor))
  334. {
  335. object[] parms2 = { rootPhysActor, childPhysActor, typeCode };
  336. ret = MakeIntError(rootPhysActor.Extension(PhysFunctChangeLinkType, parms2));
  337. }
  338. return ret;
  339. }
  340. // physGetLinkType(integer linkNum)
  341. [ScriptInvocation]
  342. public int physGetLinkType(UUID hostID, UUID scriptID, int linkNum)
  343. {
  344. int ret = -1;
  345. if (!Enabled) return ret;
  346. PhysicsActor rootPhysActor;
  347. PhysicsActor childPhysActor;
  348. if (GetRootAndChildPhysActors(hostID, linkNum, out rootPhysActor, out childPhysActor))
  349. {
  350. object[] parms2 = { rootPhysActor, childPhysActor };
  351. ret = MakeIntError(rootPhysActor.Extension(PhysFunctGetLinkType, parms2));
  352. }
  353. return ret;
  354. }
  355. // physChangeLinkFixed(integer linkNum)
  356. // Change the link between the root and the linkNum into a fixed, static physical connection.
  357. [ScriptInvocation]
  358. public int physChangeLinkFixed(UUID hostID, UUID scriptID, int linkNum)
  359. {
  360. int ret = -1;
  361. if (!Enabled) return ret;
  362. PhysicsActor rootPhysActor;
  363. PhysicsActor childPhysActor;
  364. if (GetRootAndChildPhysActors(hostID, linkNum, out rootPhysActor, out childPhysActor))
  365. {
  366. object[] parms2 = { rootPhysActor, childPhysActor , PHYS_LINK_TYPE_FIXED };
  367. ret = MakeIntError(rootPhysActor.Extension(PhysFunctChangeLinkType, parms2));
  368. }
  369. return ret;
  370. }
  371. // Code for specifying params.
  372. // The choice if 14400 is arbitrary and only serves to catch parameter code misuse.
  373. public const int PHYS_PARAM_MIN = 14401;
  374. [ScriptConstant]
  375. public const int PHYS_PARAM_FRAMEINA_LOC = 14401;
  376. [ScriptConstant]
  377. public const int PHYS_PARAM_FRAMEINA_ROT = 14402;
  378. [ScriptConstant]
  379. public const int PHYS_PARAM_FRAMEINB_LOC = 14403;
  380. [ScriptConstant]
  381. public const int PHYS_PARAM_FRAMEINB_ROT = 14404;
  382. [ScriptConstant]
  383. public const int PHYS_PARAM_LINEAR_LIMIT_LOW = 14405;
  384. [ScriptConstant]
  385. public const int PHYS_PARAM_LINEAR_LIMIT_HIGH = 14406;
  386. [ScriptConstant]
  387. public const int PHYS_PARAM_ANGULAR_LIMIT_LOW = 14407;
  388. [ScriptConstant]
  389. public const int PHYS_PARAM_ANGULAR_LIMIT_HIGH = 14408;
  390. [ScriptConstant]
  391. public const int PHYS_PARAM_USE_FRAME_OFFSET = 14409;
  392. [ScriptConstant]
  393. public const int PHYS_PARAM_ENABLE_TRANSMOTOR = 14410;
  394. [ScriptConstant]
  395. public const int PHYS_PARAM_TRANSMOTOR_MAXVEL = 14411;
  396. [ScriptConstant]
  397. public const int PHYS_PARAM_TRANSMOTOR_MAXFORCE = 14412;
  398. [ScriptConstant]
  399. public const int PHYS_PARAM_CFM = 14413;
  400. [ScriptConstant]
  401. public const int PHYS_PARAM_ERP = 14414;
  402. [ScriptConstant]
  403. public const int PHYS_PARAM_SOLVER_ITERATIONS = 14415;
  404. [ScriptConstant]
  405. public const int PHYS_PARAM_SPRING_AXIS_ENABLE = 14416;
  406. [ScriptConstant]
  407. public const int PHYS_PARAM_SPRING_DAMPING = 14417;
  408. [ScriptConstant]
  409. public const int PHYS_PARAM_SPRING_STIFFNESS = 14418;
  410. [ScriptConstant]
  411. public const int PHYS_PARAM_LINK_TYPE = 14419;
  412. [ScriptConstant]
  413. public const int PHYS_PARAM_USE_LINEAR_FRAMEA = 14420;
  414. [ScriptConstant]
  415. public const int PHYS_PARAM_SPRING_EQUILIBRIUM_POINT = 14421;
  416. public const int PHYS_PARAM_MAX = 14421;
  417. // Used when specifying a parameter that has settings for the three linear and three angular axis
  418. [ScriptConstant]
  419. public const int PHYS_AXIS_ALL = -1;
  420. [ScriptConstant]
  421. public const int PHYS_AXIS_LINEAR_ALL = -2;
  422. [ScriptConstant]
  423. public const int PHYS_AXIS_ANGULAR_ALL = -3;
  424. [ScriptConstant]
  425. public const int PHYS_AXIS_LINEAR_X = 0;
  426. [ScriptConstant]
  427. public const int PHYS_AXIS_LINEAR_Y = 1;
  428. [ScriptConstant]
  429. public const int PHYS_AXIS_LINEAR_Z = 2;
  430. [ScriptConstant]
  431. public const int PHYS_AXIS_ANGULAR_X = 3;
  432. [ScriptConstant]
  433. public const int PHYS_AXIS_ANGULAR_Y = 4;
  434. [ScriptConstant]
  435. public const int PHYS_AXIS_ANGULAR_Z = 5;
  436. // physChangeLinkParams(integer linkNum, [ PHYS_PARAM_*, value, PHYS_PARAM_*, value, ...])
  437. [ScriptInvocation]
  438. public int physChangeLinkParams(UUID hostID, UUID scriptID, int linkNum, object[] parms)
  439. {
  440. int ret = -1;
  441. if (!Enabled) return ret;
  442. PhysicsActor rootPhysActor;
  443. PhysicsActor childPhysActor;
  444. if (GetRootAndChildPhysActors(hostID, linkNum, out rootPhysActor, out childPhysActor))
  445. {
  446. object[] parms2 = AddToBeginningOfArray(rootPhysActor, childPhysActor, parms);
  447. ret = MakeIntError(rootPhysActor.Extension(PhysFunctChangeLinkParams, parms2));
  448. }
  449. return ret;
  450. }
  451. private bool GetRootPhysActor(UUID hostID, out PhysicsActor rootPhysActor)
  452. {
  453. SceneObjectGroup containingGroup;
  454. SceneObjectPart rootPart;
  455. return GetRootPhysActor(hostID, out containingGroup, out rootPart, out rootPhysActor);
  456. }
  457. private bool GetRootPhysActor(UUID hostID, out SceneObjectGroup containingGroup, out SceneObjectPart rootPart, out PhysicsActor rootPhysActor)
  458. {
  459. bool ret = false;
  460. rootPhysActor = null;
  461. containingGroup = null;
  462. rootPart = null;
  463. SceneObjectPart requestingPart;
  464. requestingPart = BaseScene.GetSceneObjectPart(hostID);
  465. if (requestingPart != null)
  466. {
  467. // The type is is always on the root of a linkset.
  468. containingGroup = requestingPart.ParentGroup;
  469. if (containingGroup != null && !containingGroup.IsDeleted)
  470. {
  471. rootPart = containingGroup.RootPart;
  472. if (rootPart != null)
  473. {
  474. rootPhysActor = rootPart.PhysActor;
  475. if (rootPhysActor != null)
  476. {
  477. ret = true;
  478. }
  479. else
  480. {
  481. m_log.WarnFormat("{0} GetRootAndChildPhysActors: Root part does not have a physics actor. rootName={1}, hostID={2}",
  482. LogHeader, rootPart.Name, hostID);
  483. }
  484. }
  485. else
  486. {
  487. m_log.WarnFormat("{0} GetRootAndChildPhysActors: Root part does not exist. RequestingPartName={1}, hostID={2}",
  488. LogHeader, requestingPart.Name, hostID);
  489. }
  490. }
  491. else
  492. {
  493. m_log.WarnFormat("{0} GetRootAndChildPhysActors: Containing group missing or deleted. hostID={1}", LogHeader, hostID);
  494. }
  495. }
  496. else
  497. {
  498. m_log.WarnFormat("{0} GetRootAndChildPhysActors: cannot find script object in scene. hostID={1}", LogHeader, hostID);
  499. }
  500. return ret;
  501. }
  502. // Find the root and child PhysActors based on the linkNum.
  503. // Return 'true' if both are found and returned.
  504. private bool GetRootAndChildPhysActors(UUID hostID, int linkNum, out PhysicsActor rootPhysActor, out PhysicsActor childPhysActor)
  505. {
  506. bool ret = false;
  507. rootPhysActor = null;
  508. childPhysActor = null;
  509. SceneObjectGroup containingGroup;
  510. SceneObjectPart rootPart;
  511. if (GetRootPhysActor(hostID, out containingGroup, out rootPart, out rootPhysActor))
  512. {
  513. SceneObjectPart linkPart = containingGroup.GetLinkNumPart(linkNum);
  514. if (linkPart != null)
  515. {
  516. childPhysActor = linkPart.PhysActor;
  517. if (childPhysActor != null)
  518. {
  519. ret = true;
  520. }
  521. else
  522. {
  523. m_log.WarnFormat("{0} GetRootAndChildPhysActors: Link part has no physical actor. rootName={1}, hostID={2}, linknum={3}",
  524. LogHeader, rootPart.Name, hostID, linkNum);
  525. }
  526. }
  527. else
  528. {
  529. m_log.WarnFormat("{0} GetRootAndChildPhysActors: Could not find linknum part. rootName={1}, hostID={2}, linknum={3}",
  530. LogHeader, rootPart.Name, hostID, linkNum);
  531. }
  532. }
  533. else
  534. {
  535. m_log.WarnFormat("{0} GetRootAndChildPhysActors: Root part does not have a physics actor. rootName={1}, hostID={2}",
  536. LogHeader, rootPart.Name, hostID);
  537. }
  538. return ret;
  539. }
  540. // Return an array of objects with the passed object as the first object of a new array
  541. private object[] AddToBeginningOfArray(object firstOne, object secondOne, object[] prevArray)
  542. {
  543. object[] newArray = new object[2 + prevArray.Length];
  544. newArray[0] = firstOne;
  545. newArray[1] = secondOne;
  546. prevArray.CopyTo(newArray, 2);
  547. return newArray;
  548. }
  549. // Extension() returns an object. Convert that object into the integer error we expect to return.
  550. private int MakeIntError(object extensionRet)
  551. {
  552. int ret = -1;
  553. if (extensionRet != null)
  554. {
  555. try
  556. {
  557. ret = (int)extensionRet;
  558. }
  559. catch
  560. {
  561. ret = -1;
  562. }
  563. }
  564. return ret;
  565. }
  566. }
  567. }