SceneObjectGroup.cs 86 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567
  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 System.Drawing;
  30. using System.IO;
  31. using System.Xml;
  32. using System.Xml.Serialization;
  33. using Axiom.Math;
  34. using libsecondlife;
  35. using libsecondlife.Packets;
  36. using OpenSim.Framework;
  37. using OpenSim.Region.Environment.Interfaces;
  38. using OpenSim.Region.Physics.Manager;
  39. namespace OpenSim.Region.Environment.Scenes
  40. {
  41. [Flags]
  42. public enum scriptEvents
  43. {
  44. None = 0,
  45. attach = 1,
  46. collision = 15,
  47. collision_end = 32,
  48. collision_start = 64,
  49. control = 128,
  50. dataserver = 256,
  51. email = 512,
  52. http_response = 1024,
  53. land_collision = 2048,
  54. land_collision_end = 4096,
  55. land_collision_start = 8192,
  56. at_target = 16384,
  57. listen = 32768,
  58. money = 65536,
  59. moving_end = 131072,
  60. moving_start = 262144,
  61. not_at_rot_target = 524288,
  62. not_at_target = 1048576,
  63. remote_data = 8388608,
  64. run_time_permissions = 268435456,
  65. state_entry = 1073741824,
  66. state_exit = 2,
  67. timer = 4,
  68. touch = 8,
  69. touch_end = 536870912,
  70. touch_start = 2097152,
  71. object_rez = 4194304
  72. }
  73. struct scriptPosTarget
  74. {
  75. public LLVector3 targetPos;
  76. public float tolerance;
  77. }
  78. public delegate void PrimCountTaintedDelegate();
  79. /// <summary>
  80. /// A scene object group is conceptually an object in the scene. The object is constituted of SceneObjectParts
  81. /// (often known as prims), one of which is considered the root part.
  82. /// </summary>
  83. public partial class SceneObjectGroup : EntityBase
  84. {
  85. private PrimCountTaintedDelegate handlerPrimCountTainted = null;
  86. /// <summary>
  87. /// Signal whether the non-inventory attributes of any prims in the group have changed
  88. /// since the group's last persistent backup
  89. /// </summary>
  90. public bool HasGroupChanged = false;
  91. public float scriptScore = 0f;
  92. private LLVector3 lastPhysGroupPos;
  93. private LLQuaternion lastPhysGroupRot;
  94. /// <summary>
  95. /// The constituent parts of this group
  96. /// </summary>
  97. protected Dictionary<LLUUID, SceneObjectPart> m_parts = new Dictionary<LLUUID, SceneObjectPart>();
  98. protected ulong m_regionHandle;
  99. protected SceneObjectPart m_rootPart;
  100. private Dictionary<LLUUID, scriptEvents> m_scriptEvents = new Dictionary<LLUUID, scriptEvents>();
  101. private Dictionary<uint, scriptPosTarget> m_targets = new Dictionary<uint, scriptPosTarget>();
  102. private bool m_scriptListens_atTarget = false;
  103. private bool m_scriptListens_notAtTarget = false;
  104. private int m_startparameter = 0;
  105. #region Properties
  106. /// <summary>
  107. /// Added because the Parcel code seems to use it
  108. /// but not sure a object should have this
  109. /// as what does it tell us? that some avatar has selected it (but not what Avatar/user)
  110. /// think really there should be a list (or whatever) in each scenepresence
  111. /// saying what prim(s) that user has selected.
  112. /// </summary>
  113. protected bool m_isSelected = false;
  114. /// <summary>
  115. /// Set start parameter for on_rez event and llGetStartParameter()
  116. /// </summary>
  117. [XmlIgnore]
  118. public int StartParameter
  119. {
  120. get { return m_startparameter; }
  121. set { m_startparameter = value; }
  122. }
  123. /// <summary>
  124. ///
  125. /// </summary>
  126. public int PrimCount
  127. {
  128. get { return m_parts.Count; }
  129. }
  130. public LLQuaternion GroupRotation
  131. {
  132. get { return m_rootPart.RotationOffset; }
  133. }
  134. public LLUUID GroupID
  135. {
  136. get { return m_rootPart.GroupID; }
  137. set { m_rootPart.GroupID = value; }
  138. }
  139. /// <summary>
  140. ///
  141. /// </summary>
  142. public LLVector3 GroupCentrePoint
  143. {
  144. get { return new LLVector3(0, 0, 0); }
  145. }
  146. public Dictionary<LLUUID, SceneObjectPart> Children
  147. {
  148. get { return m_parts; }
  149. set { m_parts = value; }
  150. }
  151. public SceneObjectPart RootPart
  152. {
  153. get { return m_rootPart; }
  154. set { m_rootPart = value; }
  155. }
  156. public ulong RegionHandle
  157. {
  158. get { return m_regionHandle; }
  159. set
  160. {
  161. m_regionHandle = value;
  162. lock (m_parts)
  163. {
  164. foreach (SceneObjectPart part in m_parts.Values)
  165. {
  166. part.RegionHandle = m_regionHandle;
  167. }
  168. }
  169. }
  170. }
  171. public override LLVector3 AbsolutePosition
  172. {
  173. get
  174. {
  175. if (m_rootPart == null)
  176. {
  177. throw new NullReferenceException(
  178. string.Format("[SCENE OBJECT GROUP]: Object {0} has no root part.", m_uuid));
  179. }
  180. return m_rootPart.GroupPosition;
  181. }
  182. set
  183. {
  184. LLVector3 val = value;
  185. if ((val.X > 257f || val.X < -1f || val.Y > 257f || val.Y < -1f) && !m_rootPart.m_IsAttachment)
  186. {
  187. m_scene.CrossPrimGroupIntoNewRegion(val, this);
  188. }
  189. lock (m_parts)
  190. {
  191. foreach (SceneObjectPart part in m_parts.Values)
  192. {
  193. part.GroupPosition = val;
  194. }
  195. }
  196. //if (m_rootPart.PhysActor != null)
  197. //{
  198. //m_rootPart.PhysActor.Position =
  199. //new PhysicsVector(m_rootPart.GroupPosition.X, m_rootPart.GroupPosition.Y,
  200. //m_rootPart.GroupPosition.Z);
  201. //m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  202. //}
  203. }
  204. }
  205. public override uint LocalId
  206. {
  207. get
  208. {
  209. if (m_rootPart == null)
  210. {
  211. m_log.Error("[SCENE OBJECT GROUP]: Unable to find the rootpart for a LocalId Request!");
  212. return 0;
  213. }
  214. return m_rootPart.LocalId;
  215. }
  216. set { m_rootPart.LocalId = value; }
  217. }
  218. public override LLUUID UUID
  219. {
  220. get { return m_rootPart.UUID; }
  221. set { m_rootPart.UUID = value; }
  222. }
  223. public LLUUID OwnerID
  224. {
  225. get
  226. {
  227. if (m_rootPart == null)
  228. return LLUUID.Zero;
  229. return m_rootPart.OwnerID;
  230. }
  231. set { m_rootPart.OwnerID = value; }
  232. }
  233. public Color Color
  234. {
  235. get { return m_rootPart.Color; }
  236. set { m_rootPart.Color = value; }
  237. }
  238. public string Text
  239. {
  240. get {
  241. string returnstr = m_rootPart.Text;
  242. if (returnstr.Length > 255)
  243. {
  244. returnstr = returnstr.Substring(0, 255);
  245. }
  246. return returnstr;
  247. }
  248. set { m_rootPart.Text = value; }
  249. }
  250. protected virtual bool InSceneBackup
  251. {
  252. get { return true; }
  253. }
  254. public bool IsSelected
  255. {
  256. get { return m_isSelected; }
  257. set
  258. {
  259. m_isSelected = value;
  260. // Tell physics engine that group is selected
  261. if (m_rootPart.PhysActor != null)
  262. {
  263. m_rootPart.PhysActor.Selected = value;
  264. // Pass it on to the children.
  265. foreach (SceneObjectPart child in Children.Values)
  266. {
  267. if (child.PhysActor != null)
  268. {
  269. child.PhysActor.Selected = value;
  270. }
  271. }
  272. }
  273. }
  274. }
  275. // The UUID for the Region this Object is in.
  276. public LLUUID RegionUUID
  277. {
  278. get
  279. {
  280. if (m_scene != null)
  281. {
  282. return m_scene.RegionInfo.RegionID;
  283. }
  284. return LLUUID.Zero;
  285. }
  286. }
  287. #endregion
  288. #region Constructors
  289. /// <summary>
  290. ///
  291. /// </summary>
  292. public SceneObjectGroup()
  293. {
  294. }
  295. /// <summary>
  296. /// This constructor creates a SceneObjectGroup using a pre-existing SceneObjectPart.
  297. /// The original SceneObjectPart will be used rather than a copy, preserving
  298. /// its existing localID and UUID.
  299. /// </summary>
  300. public SceneObjectGroup(Scene scene, ulong regionHandle, SceneObjectPart part)
  301. {
  302. m_scene = scene;
  303. part.SetParent(this);
  304. part.ParentID = 0;
  305. part.LinkNum = 0;
  306. m_parts.Add(part.UUID, part);
  307. SetPartAsRoot(part);
  308. RegionHandle = regionHandle;
  309. AttachToBackup();
  310. ApplyPhysics(scene.m_physicalPrim);
  311. ScheduleGroupForFullUpdate();
  312. }
  313. /// <summary>
  314. /// Restore the object from its serialized xml representation.
  315. /// </summary>
  316. public SceneObjectGroup(Scene scene, ulong regionHandle, string xmlData)
  317. {
  318. m_scene = scene;
  319. m_regionHandle = regionHandle;
  320. StringReader sr = new StringReader(xmlData);
  321. XmlTextReader reader = new XmlTextReader(sr);
  322. try
  323. {
  324. reader.Read();
  325. reader.ReadStartElement("SceneObjectGroup");
  326. reader.ReadStartElement("RootPart");
  327. m_rootPart = SceneObjectPart.FromXml(reader);
  328. AddPart(m_rootPart);
  329. m_log.DebugFormat("[SCENE OBJECT GROUP]: Current node {0}", reader.Name);
  330. reader.ReadEndElement();
  331. while (reader.Read())
  332. {
  333. switch (reader.NodeType)
  334. {
  335. case XmlNodeType.Element:
  336. if (reader.Name == "Part")
  337. {
  338. reader.Read();
  339. SceneObjectPart part = SceneObjectPart.FromXml(reader);
  340. part.LocalId = m_scene.PrimIDAllocate();
  341. AddPart(part);
  342. part.RegionHandle = m_regionHandle;
  343. part.TrimPermissions();
  344. part.StoreUndoState();
  345. }
  346. break;
  347. case XmlNodeType.EndElement:
  348. break;
  349. }
  350. }
  351. }
  352. catch (XmlException)
  353. {
  354. m_log.ErrorFormat("[SCENE OBJECT GROUP]: Deserialization of following xml failed, {0}", xmlData);
  355. // Let's see if carrying on does anything for us
  356. }
  357. reader.Close();
  358. sr.Close();
  359. m_rootPart.LocalId = m_scene.PrimIDAllocate();
  360. m_rootPart.ParentID = 0;
  361. m_rootPart.RegionHandle = m_regionHandle;
  362. UpdateParentIDs();
  363. AttachToBackup();
  364. ApplyPhysics(scene.m_physicalPrim);
  365. ScheduleGroupForFullUpdate();
  366. }
  367. /// <summary>
  368. ///
  369. /// </summary>
  370. public SceneObjectGroup(string xmlData)
  371. {
  372. StringReader sr = new StringReader(xmlData);
  373. XmlTextReader reader = new XmlTextReader(sr);
  374. reader.Read();
  375. reader.ReadStartElement("SceneObjectGroup");
  376. m_rootPart = SceneObjectPart.FromXml(reader);
  377. m_rootPart.SetParent(this);
  378. m_parts.Add(m_rootPart.UUID, m_rootPart);
  379. m_rootPart.ParentID = 0;
  380. m_rootPart.LinkNum = 0;
  381. reader.Read();
  382. bool more = true;
  383. while (more)
  384. {
  385. switch (reader.NodeType)
  386. {
  387. case XmlNodeType.Element:
  388. if (reader.Name == "SceneObjectPart")
  389. {
  390. SceneObjectPart Part = SceneObjectPart.FromXml(reader);
  391. AddPart(Part);
  392. Part.StoreUndoState();
  393. }
  394. else
  395. {
  396. Console.WriteLine("found unexpected element: " + reader.Name);
  397. reader.Read();
  398. }
  399. break;
  400. case XmlNodeType.EndElement:
  401. reader.Read();
  402. break;
  403. }
  404. more = !reader.EOF;
  405. }
  406. reader.Close();
  407. sr.Close();
  408. UpdateParentIDs();
  409. }
  410. /// <summary>
  411. ///
  412. /// </summary>
  413. public SceneObjectGroup(Scene scene, ulong regionHandle, LLUUID ownerID, uint localID, LLVector3 pos,
  414. LLQuaternion rot, PrimitiveBaseShape shape)
  415. {
  416. m_regionHandle = regionHandle;
  417. m_scene = scene;
  418. // this.Pos = pos;
  419. LLVector3 rootOffset = new LLVector3(0, 0, 0);
  420. SceneObjectPart newPart =
  421. new SceneObjectPart(m_regionHandle, this, ownerID, localID, shape, pos, rot, rootOffset);
  422. newPart.LinkNum = m_parts.Count;
  423. m_parts.Add(newPart.UUID, newPart);
  424. SetPartAsRoot(newPart);
  425. AttachToBackup();
  426. //ApplyPhysics(scene.m_physicalPrim);
  427. }
  428. /// <summary>
  429. ///
  430. /// </summary>
  431. public SceneObjectGroup(Scene scene, ulong regionHandle, LLUUID ownerID, uint localID, LLVector3 pos,
  432. PrimitiveBaseShape shape)
  433. : this(scene, regionHandle, ownerID, localID, pos, LLQuaternion.Identity, shape)
  434. {
  435. }
  436. public void SetFromAssetID(LLUUID AssetId)
  437. {
  438. lock (m_parts)
  439. {
  440. foreach (SceneObjectPart part in m_parts.Values)
  441. {
  442. part.fromAssetID = AssetId;
  443. }
  444. }
  445. }
  446. public LLUUID GetFromAssetID()
  447. {
  448. if (m_rootPart != null)
  449. {
  450. return m_rootPart.fromAssetID;
  451. }
  452. return LLUUID.Zero;
  453. }
  454. /// <summary>
  455. /// Hooks this object up to the backup event so that it is persisted to the database when the update thread executes.
  456. /// </summary>
  457. private void AttachToBackup()
  458. {
  459. if (InSceneBackup)
  460. {
  461. // m_log.DebugFormat(
  462. // "[SCENE OBJECT GROUP]: Attaching object {0} to scene presistence sweep", UUID);
  463. m_scene.EventManager.OnBackup += ProcessBackup;
  464. }
  465. }
  466. public LLVector3 GroupScale()
  467. {
  468. LLVector3 minScale = new LLVector3(Constants.RegionSize,Constants.RegionSize,Constants.RegionSize);
  469. LLVector3 maxScale = new LLVector3(0f,0f,0f);
  470. LLVector3 finalScale = new LLVector3(0.5f, 0.5f, 0.5f);
  471. lock (m_parts)
  472. {
  473. foreach (SceneObjectPart part in m_parts.Values)
  474. {
  475. LLVector3 partscale = part.Scale;
  476. LLVector3 partoffset = part.OffsetPosition;
  477. minScale.X = (partscale.X + partoffset.X < minScale.X) ? partscale.X + partoffset.X : minScale.X;
  478. minScale.Y = (partscale.Y + partoffset.Y < minScale.Y) ? partscale.X + partoffset.Y : minScale.Y;
  479. minScale.Z = (partscale.Z + partoffset.Z < minScale.Z) ? partscale.X + partoffset.Z : minScale.Z;
  480. maxScale.X = (partscale.X + partoffset.X > maxScale.X) ? partscale.X + partoffset.X : maxScale.X;
  481. maxScale.Y = (partscale.Y + partoffset.Y > maxScale.Y) ? partscale.Y + partoffset.Y : maxScale.Y;
  482. maxScale.Z = (partscale.Z + partoffset.Z > maxScale.Z) ? partscale.Z + partoffset.Z : maxScale.Z;
  483. }
  484. }
  485. finalScale.X = (minScale.X > maxScale.X) ? minScale.X : maxScale.X;
  486. finalScale.Y = (minScale.Y > maxScale.Y) ? minScale.Y : maxScale.Y;
  487. finalScale.Z = (minScale.Z > maxScale.Z) ? minScale.Z : maxScale.Z;
  488. return finalScale;
  489. }
  490. public EntityIntersection TestIntersection(Ray hRay, bool frontFacesOnly, bool faceCenters)
  491. {
  492. // We got a request from the inner_scene to raytrace along the Ray hRay
  493. // We're going to check all of the prim in this group for intersection with the ray
  494. // If we get a result, we're going to find the closest result to the origin of the ray
  495. // and send back the intersection information back to the innerscene.
  496. EntityIntersection returnresult = new EntityIntersection();
  497. lock (m_parts)
  498. {
  499. foreach (SceneObjectPart part in m_parts.Values)
  500. {
  501. // Temporary commented to stop compiler warning
  502. //Vector3 partPosition =
  503. // new Vector3(part.AbsolutePosition.X, part.AbsolutePosition.Y, part.AbsolutePosition.Z);
  504. Quaternion parentrotation =
  505. new Quaternion(GroupRotation.W, GroupRotation.X, GroupRotation.Y, GroupRotation.Z);
  506. // Telling the prim to raytrace.
  507. //EntityIntersection inter = part.TestIntersection(hRay, parentrotation);
  508. EntityIntersection inter = part.TestIntersectionOBB(hRay, parentrotation,frontFacesOnly, faceCenters);
  509. // This may need to be updated to the maximum draw distance possible..
  510. // We might (and probably will) be checking for prim creation from other sims
  511. // when the camera crosses the border.
  512. float idist = Constants.RegionSize;
  513. if (inter.HitTF)
  514. {
  515. // We need to find the closest prim to return to the testcaller along the ray
  516. if (inter.distance < idist)
  517. {
  518. returnresult.HitTF = true;
  519. returnresult.ipoint = inter.ipoint;
  520. returnresult.obj = part;
  521. returnresult.normal = inter.normal;
  522. returnresult.distance = inter.distance;
  523. }
  524. }
  525. }
  526. }
  527. return returnresult;
  528. }
  529. #endregion
  530. public string ToXmlString()
  531. {
  532. using (StringWriter sw = new StringWriter())
  533. {
  534. using (XmlTextWriter writer = new XmlTextWriter(sw))
  535. {
  536. ToXml(writer);
  537. }
  538. return sw.ToString();
  539. }
  540. }
  541. public void ToXml(XmlTextWriter writer)
  542. {
  543. writer.WriteStartElement(String.Empty, "SceneObjectGroup", String.Empty);
  544. writer.WriteStartElement(String.Empty, "RootPart", String.Empty);
  545. m_rootPart.ToXml(writer);
  546. writer.WriteEndElement();
  547. writer.WriteStartElement(String.Empty, "OtherParts", String.Empty);
  548. lock (m_parts)
  549. {
  550. foreach (SceneObjectPart part in m_parts.Values)
  551. {
  552. if (part.UUID != m_rootPart.UUID)
  553. {
  554. writer.WriteStartElement(String.Empty, "Part", String.Empty);
  555. part.ToXml(writer);
  556. writer.WriteEndElement();
  557. }
  558. }
  559. }
  560. writer.WriteEndElement();
  561. writer.WriteEndElement();
  562. }
  563. public string ToXmlString2()
  564. {
  565. using (StringWriter sw = new StringWriter())
  566. {
  567. using (XmlTextWriter writer = new XmlTextWriter(sw))
  568. {
  569. ToXml2(writer);
  570. }
  571. return sw.ToString();
  572. }
  573. }
  574. public void ToXml2(XmlTextWriter writer)
  575. {
  576. writer.WriteStartElement(String.Empty, "SceneObjectGroup", String.Empty);
  577. m_rootPart.ToXml(writer);
  578. writer.WriteStartElement(String.Empty, "OtherParts", String.Empty);
  579. lock (m_parts)
  580. {
  581. foreach (SceneObjectPart part in m_parts.Values)
  582. {
  583. if (part.UUID != m_rootPart.UUID)
  584. {
  585. part.ToXml(writer);
  586. }
  587. }
  588. }
  589. writer.WriteEndElement();
  590. writer.WriteEndElement();
  591. }
  592. /// <summary>
  593. ///
  594. /// </summary>
  595. /// <param name="part"></param>
  596. private void SetPartAsRoot(SceneObjectPart part)
  597. {
  598. m_rootPart = part;
  599. }
  600. public void AttachToAgent(LLUUID agentID, uint attachmentpoint, LLVector3 AttachOffset)
  601. {
  602. ScenePresence avatar = m_scene.GetScenePresence(agentID);
  603. if (avatar != null)
  604. {
  605. DetachFromBackup(this);
  606. m_rootPart.m_attachedAvatar = agentID;
  607. if (m_rootPart.PhysActor != null)
  608. {
  609. m_scene.PhysicsScene.RemovePrim(m_rootPart.PhysActor);
  610. m_rootPart.PhysActor = null;
  611. }
  612. AbsolutePosition = AttachOffset;
  613. m_rootPart.m_attachedPos = AttachOffset;
  614. m_rootPart.m_IsAttachment = true;
  615. m_rootPart.SetParentLocalId(avatar.LocalId);
  616. lock (m_parts)
  617. {
  618. foreach (SceneObjectPart part in m_parts.Values)
  619. {
  620. part.SetAttachmentPoint(attachmentpoint);
  621. }
  622. }
  623. avatar.AddAttachment(this);
  624. m_rootPart.ScheduleFullUpdate();
  625. }
  626. }
  627. public byte GetAttachmentPoint()
  628. {
  629. if (m_rootPart != null)
  630. {
  631. return m_rootPart.Shape.State;
  632. }
  633. return (byte)0;
  634. }
  635. public void ClearPartAttachmentData()
  636. {
  637. foreach (SceneObjectPart part in m_parts.Values)
  638. {
  639. part.SetAttachmentPoint((Byte)0);
  640. }
  641. }
  642. public void DetachToGround()
  643. {
  644. ScenePresence avatar = m_scene.GetScenePresence(m_rootPart.m_attachedAvatar);
  645. LLVector3 detachedpos = new LLVector3(127f,127f,127f);
  646. if (avatar != null)
  647. {
  648. detachedpos = avatar.AbsolutePosition;
  649. avatar.RemoveAttachment(this);
  650. }
  651. AbsolutePosition = detachedpos;
  652. m_rootPart.m_attachedAvatar = LLUUID.Zero;
  653. m_rootPart.SetParentLocalId(0);
  654. m_rootPart.SetAttachmentPoint((byte)0);
  655. m_rootPart.m_IsAttachment = false;
  656. m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_scene.m_physicalPrim);
  657. AttachToBackup();
  658. m_rootPart.ScheduleFullUpdate();
  659. m_rootPart.ClearUndoState();
  660. }
  661. public void DetachToInventoryPrep()
  662. {
  663. ScenePresence avatar = m_scene.GetScenePresence(m_rootPart.m_attachedAvatar);
  664. //LLVector3 detachedpos = new LLVector3(127f, 127f, 127f);
  665. if (avatar != null)
  666. {
  667. //detachedpos = avatar.AbsolutePosition;
  668. avatar.RemoveAttachment(this);
  669. }
  670. m_rootPart.m_attachedAvatar = LLUUID.Zero;
  671. m_rootPart.SetParentLocalId(0);
  672. //m_rootPart.SetAttachmentPoint((byte)0);
  673. m_rootPart.m_IsAttachment = false;
  674. AbsolutePosition = m_rootPart.m_attachedPos;
  675. //m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_scene.m_physicalPrim);
  676. //AttachToBackup();
  677. //m_rootPart.ScheduleFullUpdate();
  678. }
  679. /// <summary>
  680. ///
  681. /// </summary>
  682. /// <param name="part"></param>
  683. private void SetPartAsNonRoot(SceneObjectPart part)
  684. {
  685. part.ParentID = m_rootPart.LocalId;
  686. part.ClearUndoState();
  687. }
  688. public override void UpdateMovement()
  689. {
  690. lock (m_parts)
  691. {
  692. foreach (SceneObjectPart part in m_parts.Values)
  693. {
  694. part.UpdateMovement();
  695. }
  696. }
  697. }
  698. public float GetTimeDilation()
  699. {
  700. return m_scene.TimeDilation;
  701. }
  702. /// <summary>
  703. /// Added as a way for the storage provider to reset the scene,
  704. /// most likely a better way to do this sort of thing but for now...
  705. /// </summary>
  706. /// <param name="scene"></param>
  707. public void SetScene(Scene scene)
  708. {
  709. m_scene = scene;
  710. AttachToBackup();
  711. }
  712. /// <summary>
  713. ///
  714. /// </summary>
  715. /// <param name="part"></param>
  716. public void AddPart(SceneObjectPart part)
  717. {
  718. lock (m_parts)
  719. {
  720. part.SetParent(this);
  721. part.LinkNum = m_parts.Count;
  722. try
  723. {
  724. m_parts.Add(part.UUID, part);
  725. }
  726. catch (Exception e)
  727. {
  728. m_log.Error("Failed to add scened object part", e);
  729. }
  730. }
  731. }
  732. /// <summary>
  733. ///
  734. /// </summary>
  735. public void UpdateParentIDs()
  736. {
  737. lock (m_parts)
  738. {
  739. foreach (SceneObjectPart part in m_parts.Values)
  740. {
  741. if (part.UUID != m_rootPart.UUID)
  742. {
  743. part.ParentID = m_rootPart.LocalId;
  744. }
  745. }
  746. }
  747. }
  748. public void RegenerateFullIDs()
  749. {
  750. lock (m_parts)
  751. {
  752. foreach (SceneObjectPart part in m_parts.Values)
  753. {
  754. part.UUID = LLUUID.Random();
  755. }
  756. }
  757. }
  758. // helper provided for parts.
  759. public int GetSceneMaxUndo()
  760. {
  761. if (m_scene != null)
  762. return m_scene.MaxUndoCount;
  763. return 5;
  764. }
  765. public void ResetChildPrimPhysicsPositions()
  766. {
  767. AbsolutePosition = AbsolutePosition;
  768. HasGroupChanged = false;
  769. }
  770. public LLUUID GetPartsFullID(uint localID)
  771. {
  772. SceneObjectPart part = GetChildPart(localID);
  773. if (part != null)
  774. {
  775. return part.UUID;
  776. }
  777. return null;
  778. }
  779. public void ObjectGrabHandler(uint localId, LLVector3 offsetPos, IClientAPI remoteClient)
  780. {
  781. if (m_rootPart.LocalId == localId)
  782. {
  783. OnGrabGroup(offsetPos, remoteClient);
  784. }
  785. else
  786. {
  787. SceneObjectPart part = GetChildPart(localId);
  788. OnGrabPart(part, offsetPos, remoteClient);
  789. }
  790. }
  791. public virtual void OnGrabPart(SceneObjectPart part, LLVector3 offsetPos, IClientAPI remoteClient)
  792. {
  793. part.StoreUndoState();
  794. part.OnGrab(offsetPos, remoteClient);
  795. }
  796. public virtual void OnGrabGroup(LLVector3 offsetPos, IClientAPI remoteClient)
  797. {
  798. m_scene.EventManager.TriggerGroupGrab(UUID, offsetPos, remoteClient.AgentId);
  799. }
  800. /// <summary>
  801. /// Completely delete this group and tell all the scene presences about that deletion.
  802. /// </summary>
  803. public void DeleteGroup()
  804. {
  805. DetachFromBackup(this);
  806. lock (m_parts)
  807. {
  808. foreach (SceneObjectPart part in m_parts.Values)
  809. {
  810. List<ScenePresence> avatars = Scene.GetScenePresences();
  811. for (int i = 0; i < avatars.Count; i++)
  812. {
  813. if (avatars[i].ParentID == LocalId)
  814. {
  815. avatars[i].StandUp();
  816. }
  817. avatars[i].ControllingClient.SendKillObject(m_regionHandle, part.LocalId);
  818. }
  819. }
  820. }
  821. }
  822. /// <summary>
  823. /// Delete all the parts in this group.
  824. /// </summary>
  825. public void DeleteParts()
  826. {
  827. lock (m_parts)
  828. {
  829. foreach (SceneObjectPart part in m_parts.Values)
  830. {
  831. part.StopScripts();
  832. }
  833. m_rootPart = null;
  834. m_parts.Clear();
  835. }
  836. }
  837. public void AddScriptLPS(int count)
  838. {
  839. if (scriptScore + count >= float.MaxValue - count)
  840. scriptScore = 0;
  841. scriptScore += (float)count;
  842. InnerScene d = m_scene.m_innerScene;
  843. d.AddToScriptLPS(count);
  844. }
  845. public void AddActiveScriptCount(int count)
  846. {
  847. InnerScene d = m_scene.m_innerScene;
  848. d.AddActiveScripts(count);
  849. }
  850. public void aggregateScriptEvents()
  851. {
  852. uint objectflagupdate=(uint)RootPart.GetEffectiveObjectFlags();
  853. scriptEvents aggregateScriptEvents=0;
  854. lock (m_parts)
  855. {
  856. foreach (SceneObjectPart part in m_parts.Values)
  857. {
  858. if (part == null)
  859. continue;
  860. if (part != RootPart)
  861. part.ObjectFlags = objectflagupdate;
  862. aggregateScriptEvents |= part.m_aggregateScriptEvents;
  863. }
  864. }
  865. if ((aggregateScriptEvents & scriptEvents.at_target) != 0)
  866. {
  867. m_scriptListens_atTarget = true;
  868. }
  869. else
  870. {
  871. m_scriptListens_atTarget = false;
  872. }
  873. if ((aggregateScriptEvents & scriptEvents.not_at_target) != 0)
  874. {
  875. m_scriptListens_notAtTarget = true;
  876. }
  877. else
  878. {
  879. m_scriptListens_notAtTarget = false;
  880. }
  881. if (m_scriptListens_atTarget || m_scriptListens_notAtTarget)
  882. {
  883. }
  884. else
  885. {
  886. lock (m_targets)
  887. m_targets.Clear();
  888. }
  889. ScheduleGroupForFullUpdate();
  890. }
  891. public override void SetText(string text, Vector3 color, double alpha)
  892. {
  893. Color = Color.FromArgb(0xff - (int) (alpha * 0xff),
  894. (int) (color.x * 0xff),
  895. (int) (color.y * 0xff),
  896. (int) (color.z * 0xff));
  897. Text = text;
  898. m_rootPart.ScheduleFullUpdate();
  899. }
  900. public void ApplyPhysics(bool m_physicalPrim)
  901. {
  902. lock (m_parts)
  903. {
  904. if (m_parts.Count > 1)
  905. {
  906. m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_physicalPrim);
  907. foreach (SceneObjectPart part in m_parts.Values)
  908. {
  909. if (part.LocalId != m_rootPart.LocalId)
  910. {
  911. part.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_physicalPrim);
  912. }
  913. // Hack to get the physics scene geometries in the right spot
  914. ResetChildPrimPhysicsPositions();
  915. }
  916. }
  917. else
  918. {
  919. m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_physicalPrim);
  920. }
  921. }
  922. }
  923. public void SetOwnerId(LLUUID userId)
  924. {
  925. ForEachPart(delegate(SceneObjectPart part) { part.OwnerID = userId; });
  926. }
  927. public void ForEachPart(Action<SceneObjectPart> whatToDo)
  928. {
  929. lock (m_parts)
  930. {
  931. foreach (SceneObjectPart part in m_parts.Values)
  932. {
  933. whatToDo(part);
  934. }
  935. }
  936. }
  937. #region Events
  938. /// <summary>
  939. /// Processes backup
  940. /// </summary>
  941. /// <param name="datastore"></param>
  942. public void ProcessBackup(IRegionDataStore datastore)
  943. {
  944. // don't backup while it's selected or you're asking for changes mid stream.
  945. if (HasGroupChanged)
  946. {
  947. if ((!IsSelected) && (RootPart != null))
  948. {
  949. m_log.InfoFormat("[SCENE OBJECT GROUP]: Storing object {0}", UUID);
  950. SceneObjectGroup backup_group = Copy(OwnerID, GroupID, false);
  951. datastore.StoreObject(backup_group, m_scene.RegionInfo.RegionID);
  952. HasGroupChanged = false;
  953. backup_group.ForEachPart(delegate(SceneObjectPart part) { part.ProcessInventoryBackup(datastore); });
  954. backup_group = null;
  955. }
  956. // else
  957. // {
  958. // m_log.DebugFormat(
  959. // "[SCENE OBJECT GROUP]: Did not update persistence of object {0} since it was still selected by an avatar during the backup sweep", UUID);
  960. // }
  961. }
  962. // Why is storing the inventory outside of HasGroupChanged?
  963. //ForEachPart(delegate(SceneObjectPart part) { part.ProcessInventoryBackup(datastore); });
  964. }
  965. #endregion
  966. #region Client Updating
  967. public void SendFullUpdateToClient(IClientAPI remoteClient, uint clientFlags)
  968. {
  969. lock (m_parts)
  970. {
  971. foreach (SceneObjectPart part in m_parts.Values)
  972. {
  973. SendPartFullUpdate(remoteClient, part, clientFlags);
  974. }
  975. }
  976. }
  977. /// <summary>
  978. /// Send a full update to the client for the given part
  979. /// </summary>
  980. /// <param name="remoteClient"></param>
  981. /// <param name="part"></param>
  982. internal void SendPartFullUpdate(IClientAPI remoteClient, SceneObjectPart part, uint clientFlags)
  983. {
  984. if (m_rootPart.UUID == part.UUID)
  985. {
  986. if (m_rootPart.m_IsAttachment)
  987. {
  988. part.SendFullUpdateToClient(remoteClient, m_rootPart.m_attachedPos, clientFlags);
  989. }
  990. else
  991. {
  992. part.SendFullUpdateToClient(remoteClient, AbsolutePosition, clientFlags);
  993. }
  994. }
  995. else
  996. {
  997. part.SendFullUpdateToClient(remoteClient, clientFlags);
  998. }
  999. }
  1000. /// <summary>
  1001. /// Send a terse update to the client for the given part
  1002. /// </summary>
  1003. /// <param name="remoteClient"></param>
  1004. /// <param name="part"></param>
  1005. internal void SendPartTerseUpdate(IClientAPI remoteClient, SceneObjectPart part)
  1006. {
  1007. if (m_rootPart.UUID == part.UUID)
  1008. {
  1009. if (m_rootPart.m_IsAttachment)
  1010. {
  1011. part.SendTerseUpdateToClient(remoteClient, m_rootPart.m_attachedPos);
  1012. }
  1013. else
  1014. {
  1015. part.SendTerseUpdateToClient(remoteClient, AbsolutePosition);
  1016. }
  1017. }
  1018. else
  1019. {
  1020. part.SendTerseUpdateToClient(remoteClient);
  1021. }
  1022. }
  1023. #endregion
  1024. #region Copying
  1025. /// <summary>
  1026. /// Duplicates this object, including operations such as physics set up and attaching to the backup event.
  1027. /// </summary>
  1028. /// <returns></returns>
  1029. public SceneObjectGroup Copy(LLUUID cAgentID, LLUUID cGroupID, bool userExposed)
  1030. {
  1031. SceneObjectGroup dupe = (SceneObjectGroup) MemberwiseClone();
  1032. dupe.m_parts = new Dictionary<LLUUID, SceneObjectPart>();
  1033. dupe.m_parts.Clear();
  1034. //dupe.OwnerID = AgentID;
  1035. //dupe.GroupID = GroupID;
  1036. dupe.AbsolutePosition = new LLVector3(AbsolutePosition.X, AbsolutePosition.Y, AbsolutePosition.Z);
  1037. dupe.m_scene = m_scene;
  1038. dupe.m_regionHandle = m_regionHandle;
  1039. dupe.CopyRootPart(m_rootPart, OwnerID, GroupID, userExposed);
  1040. if (userExposed)
  1041. dupe.m_rootPart.TrimPermissions();
  1042. /// may need to create a new Physics actor.
  1043. if (dupe.RootPart.PhysActor != null && userExposed)
  1044. {
  1045. PrimitiveBaseShape pbs = dupe.RootPart.Shape;
  1046. dupe.RootPart.PhysActor = m_scene.PhysicsScene.AddPrimShape(
  1047. dupe.RootPart.Name,
  1048. pbs,
  1049. new PhysicsVector(dupe.RootPart.AbsolutePosition.X, dupe.RootPart.AbsolutePosition.Y,
  1050. dupe.RootPart.AbsolutePosition.Z),
  1051. new PhysicsVector(dupe.RootPart.Scale.X, dupe.RootPart.Scale.Y, dupe.RootPart.Scale.Z),
  1052. new Quaternion(dupe.RootPart.RotationOffset.W, dupe.RootPart.RotationOffset.X,
  1053. dupe.RootPart.RotationOffset.Y, dupe.RootPart.RotationOffset.Z),
  1054. dupe.RootPart.PhysActor.IsPhysical);
  1055. dupe.RootPart.PhysActor.LocalID = dupe.RootPart.LocalId;
  1056. dupe.RootPart.DoPhysicsPropertyUpdate(dupe.RootPart.PhysActor.IsPhysical, true);
  1057. }
  1058. // Now we've made a copy that replaces this one, we need to
  1059. // switch the owner to the person who did the copying
  1060. // Second Life copies an object and duplicates the first one in it's place
  1061. // So, we have to make a copy of this one, set it in it's place then set the owner on this one
  1062. if (userExposed)
  1063. {
  1064. SetRootPartOwner(m_rootPart, cAgentID, cGroupID);
  1065. m_rootPart.ScheduleFullUpdate();
  1066. }
  1067. List<SceneObjectPart> partList = new List<SceneObjectPart>(m_parts.Values);
  1068. foreach (SceneObjectPart part in partList)
  1069. {
  1070. if (part.UUID != m_rootPart.UUID)
  1071. {
  1072. dupe.CopyPart(part, OwnerID, GroupID, userExposed);
  1073. if (userExposed)
  1074. {
  1075. SetPartOwner(part, cAgentID, cGroupID);
  1076. part.ScheduleFullUpdate();
  1077. }
  1078. }
  1079. }
  1080. if (userExposed)
  1081. {
  1082. dupe.UpdateParentIDs();
  1083. dupe.AttachToBackup();
  1084. ScheduleGroupForFullUpdate();
  1085. }
  1086. return dupe;
  1087. }
  1088. /// <summary>
  1089. ///
  1090. /// </summary>
  1091. /// <param name="part"></param>
  1092. /// <param name="cAgentID"></param>
  1093. /// <param name="cGroupID"></param>
  1094. public void CopyRootPart(SceneObjectPart part, LLUUID cAgentID, LLUUID cGroupID, bool userExposed)
  1095. {
  1096. SceneObjectPart newPart = part.Copy(m_scene.PrimIDAllocate(), OwnerID, GroupID, m_parts.Count, userExposed);
  1097. newPart.SetParent(this);
  1098. lock (m_parts)
  1099. {
  1100. m_parts.Add(newPart.UUID, newPart);
  1101. }
  1102. SetPartAsRoot(newPart);
  1103. }
  1104. public void ScriptSetPhysicsStatus(bool UsePhysics)
  1105. {
  1106. if (m_scene.m_physicalPrim)
  1107. {
  1108. lock (m_parts)
  1109. {
  1110. foreach (SceneObjectPart part in m_parts.Values)
  1111. {
  1112. if (UsePhysics)
  1113. part.AddFlag(LLObject.ObjectFlags.Physics);
  1114. else
  1115. part.RemFlag(LLObject.ObjectFlags.Physics);
  1116. part.DoPhysicsPropertyUpdate(UsePhysics, false);
  1117. IsSelected = false;
  1118. }
  1119. }
  1120. }
  1121. }
  1122. public void ScriptSetPhantomStatus(bool PhantomStatus)
  1123. {
  1124. lock (m_parts)
  1125. {
  1126. foreach (SceneObjectPart part in m_parts.Values)
  1127. {
  1128. if (PhantomStatus)
  1129. {
  1130. part.AddFlag(LLObject.ObjectFlags.Phantom);
  1131. if (part.PhysActor != null)
  1132. {
  1133. m_scene.PhysicsScene.RemovePrim(part.PhysActor);
  1134. }
  1135. }
  1136. else
  1137. {
  1138. part.RemFlag(LLObject.ObjectFlags.Phantom);
  1139. if ((part.GetEffectiveObjectFlags() & (int) LLObject.ObjectFlags.Physics) != 0)
  1140. {
  1141. part.DoPhysicsPropertyUpdate(true, false);
  1142. }
  1143. }
  1144. }
  1145. }
  1146. }
  1147. public void applyImpulse(PhysicsVector impulse)
  1148. {
  1149. // We check if rootpart is null here because scripts don't delete if you delete the host.
  1150. // This means that unfortunately, we can pass a null physics actor to Simulate!
  1151. // Make sure we don't do that!
  1152. SceneObjectPart rootpart = m_rootPart;
  1153. if (rootpart != null)
  1154. {
  1155. if (rootpart.PhysActor != null)
  1156. {
  1157. if (rootpart.m_IsAttachment)
  1158. {
  1159. ScenePresence avatar = m_scene.GetScenePresence(rootpart.m_attachedAvatar);
  1160. if (avatar != null)
  1161. {
  1162. avatar.PushForce(impulse);
  1163. }
  1164. }
  1165. else
  1166. {
  1167. rootpart.PhysActor.AddForce(impulse,true);
  1168. m_scene.PhysicsScene.AddPhysicsActorTaint(rootpart.PhysActor);
  1169. }
  1170. }
  1171. }
  1172. }
  1173. public void moveToTarget(LLVector3 target, float tau)
  1174. {
  1175. SceneObjectPart rootpart = m_rootPart;
  1176. if (rootpart != null)
  1177. {
  1178. if (rootpart.PhysActor != null)
  1179. {
  1180. rootpart.PhysActor.PIDTarget = new PhysicsVector(target.X, target.Y, target.Z);
  1181. rootpart.PhysActor.PIDTau = tau;
  1182. rootpart.PhysActor.PIDActive = true;
  1183. }
  1184. }
  1185. }
  1186. public void stopMoveToTarget()
  1187. {
  1188. SceneObjectPart rootpart = m_rootPart;
  1189. if (rootpart != null)
  1190. {
  1191. rootpart.PhysActor.PIDActive = false;
  1192. }
  1193. }
  1194. public void SetRootPartOwner(SceneObjectPart part, LLUUID cAgentID, LLUUID cGroupID)
  1195. {
  1196. part.LastOwnerID = part.OwnerID;
  1197. part.OwnerID = cAgentID;
  1198. part.GroupID = cGroupID;
  1199. if (part.OwnerID != cAgentID)
  1200. {
  1201. // Apply Next Owner Permissions if we're not bypassing permissions
  1202. if (!m_scene.ExternalChecks.ExternalChecksBypassPermissions())
  1203. ApplyNextOwnerPermissions();
  1204. }
  1205. part.ScheduleFullUpdate();
  1206. }
  1207. /// <summary>
  1208. ///
  1209. /// </summary>
  1210. /// <param name="part"></param>
  1211. /// <param name="cAgentID"></param>
  1212. /// <param name="cGroupID"></param>
  1213. public void CopyPart(SceneObjectPart part, LLUUID cAgentID, LLUUID cGroupID, bool userExposed)
  1214. {
  1215. SceneObjectPart newPart = part.Copy(m_scene.PrimIDAllocate(), OwnerID, GroupID, m_parts.Count, userExposed);
  1216. newPart.SetParent(this);
  1217. lock (m_parts)
  1218. {
  1219. m_parts.Add(newPart.UUID, newPart);
  1220. }
  1221. SetPartAsNonRoot(newPart);
  1222. }
  1223. /// <summary>
  1224. /// Reset the LLUUIDs for all the prims that make up this group.
  1225. ///
  1226. /// This is called by methods which want to add a new group to an existing scene, in order
  1227. /// to ensure that there are no clashes with groups already present.
  1228. /// </summary>
  1229. public void ResetIDs()
  1230. {
  1231. // As this is only ever called for prims which are not currently part of the scene (and hence
  1232. // not accessible by clients), there should be no need to lock
  1233. List<SceneObjectPart> partsList = new List<SceneObjectPart>(m_parts.Values);
  1234. m_parts.Clear();
  1235. foreach (SceneObjectPart part in partsList)
  1236. {
  1237. part.ResetIDs(m_parts.Count);
  1238. m_parts.Add(part.UUID, part);
  1239. }
  1240. }
  1241. /// <summary>
  1242. ///
  1243. /// </summary>
  1244. /// <param name="part"></param>
  1245. public void ServiceObjectPropertiesFamilyRequest(IClientAPI remoteClient, LLUUID AgentID, uint RequestFlags)
  1246. {
  1247. remoteClient.SendObjectPropertiesFamilyData(RequestFlags, RootPart.UUID, RootPart.ObjectOwner, RootPart.GroupID, RootPart.BaseMask,
  1248. RootPart.OwnerMask, RootPart.GroupMask, RootPart.EveryoneMask, RootPart.NextOwnerMask,
  1249. RootPart.OwnershipCost, RootPart.ObjectSaleType, RootPart.SalePrice, RootPart.Category,
  1250. RootPart.CreatorID, RootPart.Name, RootPart.Description);
  1251. }
  1252. public void SetPartOwner(SceneObjectPart part, LLUUID cAgentID, LLUUID cGroupID)
  1253. {
  1254. part.OwnerID = cAgentID;
  1255. part.GroupID = cGroupID;
  1256. }
  1257. #endregion
  1258. #region Scheduling
  1259. /// <summary>
  1260. /// Examine this object's parts to see if they've changed sufficiently to warrant an update
  1261. /// </summary>
  1262. public override void Update()
  1263. {
  1264. lock (m_parts)
  1265. {
  1266. //if (m_rootPart.m_IsAttachment)
  1267. //{
  1268. //foreach (SceneObjectPart part in m_parts.Values)
  1269. //{
  1270. //part.SendScheduledUpdates();
  1271. //}
  1272. //return;
  1273. //}
  1274. if (Util.GetDistanceTo(lastPhysGroupPos, AbsolutePosition) > 0.02)
  1275. {
  1276. m_rootPart.UpdateFlag = 1;
  1277. lastPhysGroupPos = AbsolutePosition;
  1278. }
  1279. //foreach (SceneObjectPart part in m_parts.Values)
  1280. //{
  1281. //if (part.UpdateFlag == 0) part.UpdateFlag = 1;
  1282. //}
  1283. checkAtTargets();
  1284. if ((Math.Abs(lastPhysGroupRot.W - GroupRotation.W) > 0.1)
  1285. || (Math.Abs(lastPhysGroupRot.X - GroupRotation.X) > 0.1)
  1286. || (Math.Abs(lastPhysGroupRot.Y - GroupRotation.Y) > 0.1)
  1287. || (Math.Abs(lastPhysGroupRot.Z - GroupRotation.Z) > 0.1))
  1288. {
  1289. m_rootPart.UpdateFlag = 1;
  1290. lastPhysGroupRot = GroupRotation;
  1291. }
  1292. foreach (SceneObjectPart part in m_parts.Values)
  1293. {
  1294. part.SendScheduledUpdates();
  1295. }
  1296. }
  1297. }
  1298. public void ScheduleFullUpdateToAvatar(ScenePresence presence)
  1299. {
  1300. lock (m_parts)
  1301. {
  1302. foreach (SceneObjectPart part in m_parts.Values)
  1303. {
  1304. part.AddFullUpdateToAvatar(presence);
  1305. }
  1306. }
  1307. }
  1308. public void ScheduleTerseUpdateToAvatar(ScenePresence presence)
  1309. {
  1310. lock (m_parts)
  1311. {
  1312. foreach (SceneObjectPart part in m_parts.Values)
  1313. {
  1314. part.AddTerseUpdateToAvatar(presence);
  1315. }
  1316. }
  1317. }
  1318. /// <summary>
  1319. /// Schedule a full update for every part in this object
  1320. /// </summary>
  1321. public void ScheduleGroupForFullUpdate()
  1322. {
  1323. HasGroupChanged = true;
  1324. checkAtTargets();
  1325. lock (m_parts)
  1326. {
  1327. foreach (SceneObjectPart part in m_parts.Values)
  1328. {
  1329. part.ScheduleFullUpdate();
  1330. }
  1331. }
  1332. }
  1333. /// <summary>
  1334. ///
  1335. /// </summary>
  1336. public void ScheduleGroupForTerseUpdate()
  1337. {
  1338. HasGroupChanged = true;
  1339. lock (m_parts)
  1340. {
  1341. foreach (SceneObjectPart part in m_parts.Values)
  1342. {
  1343. part.ScheduleTerseUpdate();
  1344. }
  1345. }
  1346. }
  1347. /// <summary>
  1348. ///
  1349. /// </summary>
  1350. public void SendGroupFullUpdate()
  1351. {
  1352. HasGroupChanged = true;
  1353. lock (m_parts)
  1354. {
  1355. foreach (SceneObjectPart part in m_parts.Values)
  1356. {
  1357. part.SendFullUpdateToAllClients();
  1358. }
  1359. }
  1360. }
  1361. public void QueueForUpdateCheck()
  1362. {
  1363. m_scene.m_innerScene.AddToUpdateList(this);
  1364. }
  1365. /// <summary>
  1366. ///
  1367. /// </summary>
  1368. public void SendGroupTerseUpdate()
  1369. {
  1370. HasGroupChanged = true;
  1371. lock (m_parts)
  1372. {
  1373. foreach (SceneObjectPart part in m_parts.Values)
  1374. {
  1375. part.SendTerseUpdateToAllClients();
  1376. }
  1377. }
  1378. }
  1379. #endregion
  1380. #region SceneGroupPart Methods
  1381. /// <summary>
  1382. /// Get the child part by LinkNum
  1383. /// </summary>
  1384. /// <param name="linknum"></param>
  1385. /// <returns>null if no child part with that linknum or child part</returns>
  1386. public SceneObjectPart GetLinkNumPart(int linknum)
  1387. {
  1388. lock (m_parts)
  1389. {
  1390. foreach (SceneObjectPart part in m_parts.Values)
  1391. {
  1392. if (part.LinkNum == linknum)
  1393. {
  1394. return part;
  1395. }
  1396. }
  1397. }
  1398. return null;
  1399. }
  1400. /// <summary>
  1401. /// Get a child part with a given UUID
  1402. /// </summary>
  1403. /// <param name="primID"></param>
  1404. /// <returns>null if a child part with the primID was not found</returns>
  1405. public SceneObjectPart GetChildPart(LLUUID primID)
  1406. {
  1407. SceneObjectPart childPart = null;
  1408. if (m_parts.ContainsKey(primID))
  1409. {
  1410. childPart = m_parts[primID];
  1411. }
  1412. return childPart;
  1413. }
  1414. /// <summary>
  1415. /// Get a child part with a given local ID
  1416. /// </summary>
  1417. /// <param name="localID"></param>
  1418. /// <returns>null if a child part with the local ID was not found</returns>
  1419. public SceneObjectPart GetChildPart(uint localID)
  1420. {
  1421. lock (m_parts)
  1422. {
  1423. foreach (SceneObjectPart part in m_parts.Values)
  1424. {
  1425. if (part.LocalId == localID)
  1426. {
  1427. return part;
  1428. }
  1429. }
  1430. }
  1431. return null;
  1432. }
  1433. /// <summary>
  1434. /// Does this group contain the child prim
  1435. /// should be able to remove these methods once we have a entity index in scene
  1436. /// </summary>
  1437. /// <param name="primID"></param>
  1438. /// <returns></returns>
  1439. public bool HasChildPrim(LLUUID primID)
  1440. {
  1441. if (m_parts.ContainsKey(primID))
  1442. {
  1443. return true;
  1444. }
  1445. return false;
  1446. }
  1447. /// <summary>
  1448. /// Does this group contain the child prim
  1449. /// should be able to remove these methods once we have a entity index in scene
  1450. /// </summary>
  1451. /// <param name="localID"></param>
  1452. /// <returns></returns>
  1453. public bool HasChildPrim(uint localID)
  1454. {
  1455. lock (m_parts)
  1456. {
  1457. foreach (SceneObjectPart part in m_parts.Values)
  1458. {
  1459. if (part.LocalId == localID)
  1460. {
  1461. return true;
  1462. }
  1463. }
  1464. }
  1465. return false;
  1466. }
  1467. #endregion
  1468. #region Packet Handlers
  1469. /// <summary>
  1470. /// Link the prims in a given group to this group
  1471. /// </summary>
  1472. /// <param name="objectGroup">The group of prims which should be linked to this group</param>
  1473. public void LinkToGroup(SceneObjectGroup objectGroup)
  1474. {
  1475. if (objectGroup.RootPart.UpdateFlag > 0)
  1476. {
  1477. // I've never actually seen this happen, though I think it's theoretically possible
  1478. m_log.WarnFormat(
  1479. "[SCENE OBJECT GROUP]: Aborted linking {0}, {1} to {2}, {3} as it has yet to finish delinking",
  1480. objectGroup.RootPart.Name, objectGroup.RootPart.UUID, RootPart.Name, RootPart.UUID);
  1481. return;
  1482. }
  1483. // m_log.DebugFormat(
  1484. // "[SCENE OBJECT GROUP]: Linking group with root part {0}, {1} to group with root part {2}, {3}",
  1485. // objectGroup.RootPart.Name, objectGroup.RootPart.UUID, RootPart.Name, RootPart.UUID);
  1486. SceneObjectPart linkPart = objectGroup.m_rootPart;
  1487. Vector3 oldGroupPosition =
  1488. new Vector3(linkPart.GroupPosition.X, linkPart.GroupPosition.Y, linkPart.GroupPosition.Z);
  1489. Quaternion oldRootRotation =
  1490. new Quaternion(linkPart.RotationOffset.W, linkPart.RotationOffset.X, linkPart.RotationOffset.Y,
  1491. linkPart.RotationOffset.Z);
  1492. linkPart.OffsetPosition = linkPart.GroupPosition - AbsolutePosition;
  1493. linkPart.GroupPosition = AbsolutePosition;
  1494. Vector3 axPos = new Vector3(linkPart.OffsetPosition.X, linkPart.OffsetPosition.Y, linkPart.OffsetPosition.Z);
  1495. Quaternion parentRot =
  1496. new Quaternion(m_rootPart.RotationOffset.W, m_rootPart.RotationOffset.X, m_rootPart.RotationOffset.Y,
  1497. m_rootPart.RotationOffset.Z);
  1498. axPos = parentRot.Inverse() * axPos;
  1499. linkPart.OffsetPosition = new LLVector3(axPos.x, axPos.y, axPos.z);
  1500. Quaternion oldRot =
  1501. new Quaternion(linkPart.RotationOffset.W, linkPart.RotationOffset.X, linkPart.RotationOffset.Y,
  1502. linkPart.RotationOffset.Z);
  1503. Quaternion newRot = parentRot.Inverse() * oldRot;
  1504. linkPart.RotationOffset = new LLQuaternion(newRot.x, newRot.y, newRot.z, newRot.w);
  1505. linkPart.ParentID = m_rootPart.LocalId;
  1506. linkPart.LinkNum = m_parts.Count;
  1507. lock (m_parts)
  1508. {
  1509. m_parts.Add(linkPart.UUID, linkPart);
  1510. }
  1511. linkPart.SetParent(this);
  1512. //if (linkPart.PhysActor != null)
  1513. //{
  1514. // m_scene.PhysicsScene.RemovePrim(linkPart.PhysActor);
  1515. //linkPart.PhysActor = null;
  1516. //}
  1517. //TODO: rest of parts
  1518. foreach (SceneObjectPart part in objectGroup.Children.Values)
  1519. {
  1520. if (part.UUID != objectGroup.m_rootPart.UUID)
  1521. {
  1522. LinkNonRootPart(part, oldGroupPosition, oldRootRotation);
  1523. }
  1524. part.ClearUndoState();
  1525. }
  1526. m_scene.UnlinkSceneObject(objectGroup.UUID);
  1527. // TODO Deleting the parts may cause problems later on if they have already
  1528. // made it into the update queue. However, sending out updates for those parts is now
  1529. // spurious, so it would be good not to send them at some point.
  1530. // The traffic caused is always going to be pretty minor, so it's not high priority
  1531. //objectGroup.DeleteParts();
  1532. ScheduleGroupForFullUpdate();
  1533. }
  1534. /// <summary>
  1535. /// Delink the given prim from this group. The delinked prim is established as
  1536. /// an independent SceneObjectGroup.
  1537. /// </summary>
  1538. /// <param name="partID"></param>
  1539. public void DelinkFromGroup(uint partID)
  1540. {
  1541. SceneObjectPart linkPart = GetChildPart(partID);
  1542. if (null != linkPart)
  1543. {
  1544. linkPart.ClearUndoState();
  1545. // m_log.DebugFormat(
  1546. // "[SCENE OBJECT GROUP]: Delinking part {0}, {1} from group with root part {2}, {3}",
  1547. // linkPart.Name, linkPart.UUID, RootPart.Name, RootPart.UUID);
  1548. LLQuaternion worldRot = linkPart.GetWorldRotation();
  1549. // Remove the part from this object
  1550. lock (m_parts)
  1551. {
  1552. m_parts.Remove(linkPart.UUID);
  1553. }
  1554. linkPart.ParentID = 0;
  1555. if (linkPart.PhysActor != null)
  1556. {
  1557. m_scene.PhysicsScene.RemovePrim(linkPart.PhysActor);
  1558. }
  1559. // We need to reset the child part's position
  1560. // ready for life as a separate object after being a part of another object
  1561. Quaternion parentRot
  1562. = new Quaternion(
  1563. m_rootPart.RotationOffset.W,
  1564. m_rootPart.RotationOffset.X,
  1565. m_rootPart.RotationOffset.Y,
  1566. m_rootPart.RotationOffset.Z);
  1567. Vector3 axPos
  1568. = new Vector3(
  1569. linkPart.OffsetPosition.X,
  1570. linkPart.OffsetPosition.Y,
  1571. linkPart.OffsetPosition.Z);
  1572. axPos = parentRot * axPos;
  1573. linkPart.OffsetPosition = new LLVector3(axPos.x, axPos.y, axPos.z);
  1574. linkPart.GroupPosition = AbsolutePosition + linkPart.OffsetPosition;
  1575. linkPart.OffsetPosition = new LLVector3(0, 0, 0);
  1576. linkPart.RotationOffset = worldRot;
  1577. // This chunk is probably unnecesary now - delete later on
  1578. /*
  1579. Quaternion oldRot
  1580. = new Quaternion(
  1581. linkPart.RotationOffset.W,
  1582. linkPart.RotationOffset.X,
  1583. linkPart.RotationOffset.Y,
  1584. linkPart.RotationOffset.Z);
  1585. Quaternion newRot = parentRot*oldRot;
  1586. linkPart.RotationOffset = new LLQuaternion(newRot.x, newRot.y, newRot.z, newRot.w);
  1587. */
  1588. // Add physics information back to delinked part if appropriate
  1589. // XXX This is messy and should be refactorable with the similar section in
  1590. // SceneObjectPart.UpdatePrimFlags()
  1591. //if (m_rootPart.PhysActor != null)
  1592. //{
  1593. //linkPart.PhysActor = m_scene.PhysicsScene.AddPrimShape(
  1594. //linkPart.Name,
  1595. //linkPart.Shape,
  1596. //new PhysicsVector(linkPart.AbsolutePosition.X, linkPart.AbsolutePosition.Y,
  1597. //linkPart.AbsolutePosition.Z),
  1598. //new PhysicsVector(linkPart.Scale.X, linkPart.Scale.Y, linkPart.Scale.Z),
  1599. //new Quaternion(linkPart.RotationOffset.W, linkPart.RotationOffset.X,
  1600. //linkPart.RotationOffset.Y, linkPart.RotationOffset.Z),
  1601. //m_rootPart.PhysActor.IsPhysical);
  1602. //m_rootPart.DoPhysicsPropertyUpdate(m_rootPart.PhysActor.IsPhysical, true);
  1603. //}
  1604. SceneObjectGroup objectGroup = new SceneObjectGroup(m_scene, m_regionHandle, linkPart);
  1605. m_scene.AddSceneObject(objectGroup);
  1606. ScheduleGroupForFullUpdate();
  1607. }
  1608. else
  1609. {
  1610. m_log.InfoFormat("[SCENE OBJECT GROUP]: " +
  1611. "DelinkFromGroup(): Child prim {0} not found in object {1}, {2}",
  1612. partID, LocalId, UUID);
  1613. }
  1614. }
  1615. private void DetachFromBackup(SceneObjectGroup objectGroup)
  1616. {
  1617. m_scene.EventManager.OnBackup -= objectGroup.ProcessBackup;
  1618. }
  1619. private void LinkNonRootPart(SceneObjectPart part, Vector3 oldGroupPosition, Quaternion oldGroupRotation)
  1620. {
  1621. part.SetParent(this);
  1622. part.ParentID = m_rootPart.LocalId;
  1623. part.LinkNum = m_parts.Count;
  1624. lock (m_parts)
  1625. {
  1626. m_parts.Add(part.UUID, part);
  1627. }
  1628. Vector3 axiomOldPos = new Vector3(part.OffsetPosition.X, part.OffsetPosition.Y, part.OffsetPosition.Z);
  1629. axiomOldPos = oldGroupRotation * axiomOldPos;
  1630. axiomOldPos += oldGroupPosition;
  1631. LLVector3 oldAbsolutePosition = new LLVector3(axiomOldPos.x, axiomOldPos.y, axiomOldPos.z);
  1632. part.OffsetPosition = oldAbsolutePosition - AbsolutePosition;
  1633. Quaternion axiomRootRotation =
  1634. new Quaternion(m_rootPart.RotationOffset.W, m_rootPart.RotationOffset.X, m_rootPart.RotationOffset.Y,
  1635. m_rootPart.RotationOffset.Z);
  1636. Vector3 axiomPos = new Vector3(part.OffsetPosition.X, part.OffsetPosition.Y, part.OffsetPosition.Z);
  1637. axiomPos = axiomRootRotation.Inverse() * axiomPos;
  1638. part.OffsetPosition = new LLVector3(axiomPos.x, axiomPos.y, axiomPos.z);
  1639. Quaternion axiomPartRotation =
  1640. new Quaternion(part.RotationOffset.W, part.RotationOffset.X, part.RotationOffset.Y,
  1641. part.RotationOffset.Z);
  1642. axiomPartRotation = oldGroupRotation * axiomPartRotation;
  1643. axiomPartRotation = axiomRootRotation.Inverse() * axiomPartRotation;
  1644. part.RotationOffset =
  1645. new LLQuaternion(axiomPartRotation.x, axiomPartRotation.y, axiomPartRotation.z, axiomPartRotation.w);
  1646. }
  1647. /// <summary>
  1648. /// If object is physical, apply force to move it around
  1649. /// If object is not physical, just put it at the resulting location
  1650. /// </summary>
  1651. /// <param name="offset">Always seems to be 0,0,0, so ignoring</param>
  1652. /// <param name="pos">New position. We do the math here to turn it into a force</param>
  1653. /// <param name="remoteClient"></param>
  1654. public void GrabMovement(LLVector3 offset, LLVector3 pos, IClientAPI remoteClient)
  1655. {
  1656. if (m_scene.EventManager.TriggerGroupMove(UUID, pos))
  1657. {
  1658. if (m_rootPart.PhysActor != null)
  1659. {
  1660. if (m_rootPart.PhysActor.IsPhysical)
  1661. {
  1662. LLVector3 llmoveforce = pos - AbsolutePosition;
  1663. PhysicsVector grabforce = new PhysicsVector(llmoveforce.X, llmoveforce.Y, llmoveforce.Z);
  1664. grabforce = (grabforce / 10) * m_rootPart.PhysActor.Mass;
  1665. m_rootPart.PhysActor.AddForce(grabforce,true);
  1666. m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  1667. }
  1668. else
  1669. {
  1670. //NonPhysicalGrabMovement(pos);
  1671. }
  1672. }
  1673. else
  1674. {
  1675. //NonPhysicalGrabMovement(pos);
  1676. }
  1677. }
  1678. }
  1679. public void NonPhysicalGrabMovement(LLVector3 pos)
  1680. {
  1681. AbsolutePosition = pos;
  1682. m_rootPart.SendTerseUpdateToAllClients();
  1683. }
  1684. /// <summary>
  1685. /// Return metadata about a prim (name, description, sale price, etc.)
  1686. /// </summary>
  1687. /// <param name="client"></param>
  1688. public void GetProperties(IClientAPI client)
  1689. {
  1690. m_rootPart.GetProperties(client);
  1691. }
  1692. /// <summary>
  1693. /// Set the name of a prim
  1694. /// </summary>
  1695. /// <param name="name"></param>
  1696. /// <param name="localID"></param>
  1697. public void SetPartName(string name, uint localID)
  1698. {
  1699. SceneObjectPart part = GetChildPart(localID);
  1700. if (part != null)
  1701. {
  1702. part.Name = name;
  1703. }
  1704. }
  1705. public void SetPartDescription(string des, uint localID)
  1706. {
  1707. SceneObjectPart part = GetChildPart(localID);
  1708. if (part != null)
  1709. {
  1710. part.Description = des;
  1711. }
  1712. }
  1713. public void SetPartText(string text, uint localID)
  1714. {
  1715. SceneObjectPart part = GetChildPart(localID);
  1716. if (part != null)
  1717. {
  1718. part.SetText(text);
  1719. }
  1720. }
  1721. public void SetPartText(string text, LLUUID partID)
  1722. {
  1723. SceneObjectPart part = GetChildPart(partID);
  1724. if (part != null)
  1725. {
  1726. part.SetText(text);
  1727. }
  1728. }
  1729. public string GetPartName(uint localID)
  1730. {
  1731. SceneObjectPart part = GetChildPart(localID);
  1732. if (part != null)
  1733. {
  1734. return part.Name;
  1735. }
  1736. return String.Empty;
  1737. }
  1738. public string GetPartDescription(uint localID)
  1739. {
  1740. SceneObjectPart part = GetChildPart(localID);
  1741. if (part != null)
  1742. {
  1743. return part.Description;
  1744. }
  1745. return String.Empty;
  1746. }
  1747. /// <summary>
  1748. ///
  1749. /// </summary>
  1750. /// <param name="localID"></param>
  1751. /// <param name="type"></param>
  1752. /// <param name="inUse"></param>
  1753. /// <param name="data"></param>
  1754. ///
  1755. public void UpdatePrimFlags(uint localID, ushort type, bool inUse, byte[] data)
  1756. {
  1757. SceneObjectPart part = GetChildPart(localID);
  1758. if (part != null)
  1759. {
  1760. // If we have children
  1761. lock (m_parts)
  1762. {
  1763. if (m_parts.Count > 1)
  1764. {
  1765. foreach (SceneObjectPart parts in m_parts.Values)
  1766. {
  1767. parts.UpdatePrimFlags(type, inUse, data);
  1768. }
  1769. }
  1770. else
  1771. {
  1772. part.UpdatePrimFlags(type, inUse, data);
  1773. }
  1774. }
  1775. }
  1776. }
  1777. public void UpdateExtraParam(uint localID, ushort type, bool inUse, byte[] data)
  1778. {
  1779. SceneObjectPart part = GetChildPart(localID);
  1780. if (part != null)
  1781. {
  1782. part.UpdateExtraParam(type, inUse, data);
  1783. }
  1784. }
  1785. /// <summary>
  1786. /// Get the parts of this scene object
  1787. /// </summary>
  1788. /// <returns></returns>
  1789. public SceneObjectPart[] GetParts()
  1790. {
  1791. int numParts = Children.Count;
  1792. SceneObjectPart[] partArray = new SceneObjectPart[numParts];
  1793. Children.Values.CopyTo(partArray, 0);
  1794. return partArray;
  1795. }
  1796. /// <summary>
  1797. /// Update the texture entry for this part
  1798. /// </summary>
  1799. /// <param name="localID"></param>
  1800. /// <param name="textureEntry"></param>
  1801. public void UpdateTextureEntry(uint localID, byte[] textureEntry)
  1802. {
  1803. SceneObjectPart part = GetChildPart(localID);
  1804. if (part != null)
  1805. {
  1806. part.UpdateTextureEntry(textureEntry);
  1807. }
  1808. }
  1809. public void UpdatePermissions(LLUUID AgentID, byte field, uint localID, uint mask, byte addRemTF)
  1810. {
  1811. SceneObjectPart updatePart = GetChildPart(localID);
  1812. updatePart.UpdatePermissions(AgentID, field, localID, mask, addRemTF);
  1813. HasGroupChanged = true;
  1814. }
  1815. #endregion
  1816. #region Shape
  1817. /// <summary>
  1818. ///
  1819. /// </summary>
  1820. /// <param name="shapeBlock"></param>
  1821. public void UpdateShape(ObjectShapePacket.ObjectDataBlock shapeBlock, uint localID)
  1822. {
  1823. SceneObjectPart part = GetChildPart(localID);
  1824. if (part != null)
  1825. {
  1826. part.UpdateShape(shapeBlock);
  1827. if (part.PhysActor != null)
  1828. m_scene.PhysicsScene.AddPhysicsActorTaint(part.PhysActor);
  1829. }
  1830. }
  1831. #endregion
  1832. #region Resize
  1833. /// <summary>
  1834. ///
  1835. /// </summary>
  1836. /// <param name="scale"></param>
  1837. /// <param name="localID"></param>
  1838. public void Resize(LLVector3 scale, uint localID)
  1839. {
  1840. SceneObjectPart part = GetChildPart(localID);
  1841. if (part != null)
  1842. {
  1843. part.Resize(scale);
  1844. if (part.PhysActor != null)
  1845. {
  1846. part.PhysActor.Size =
  1847. new PhysicsVector(scale.X, scale.Y, scale.Z);
  1848. m_scene.PhysicsScene.AddPhysicsActorTaint(part.PhysActor);
  1849. }
  1850. //if (part.UUID != m_rootPart.UUID)
  1851. ScheduleGroupForFullUpdate();
  1852. //if (part.UUID == m_rootPart.UUID)
  1853. //{
  1854. //if (m_rootPart.PhysActor != null)
  1855. //{
  1856. //m_rootPart.PhysActor.Size =
  1857. //new PhysicsVector(m_rootPart.Scale.X, m_rootPart.Scale.Y, m_rootPart.Scale.Z);
  1858. //m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  1859. //}
  1860. //}
  1861. }
  1862. }
  1863. public void GroupResize(LLVector3 scale, uint localID)
  1864. {
  1865. SceneObjectPart part = GetChildPart(localID);
  1866. if (part != null)
  1867. {
  1868. float x = (scale.X / part.Scale.X);
  1869. float y = (scale.Y / part.Scale.Y);
  1870. float z = (scale.Z / part.Scale.Z);
  1871. part.Resize(scale);
  1872. lock (m_parts)
  1873. {
  1874. foreach (SceneObjectPart obPart in m_parts.Values)
  1875. {
  1876. if (obPart.UUID != m_rootPart.UUID)
  1877. {
  1878. LLVector3 currentpos = new LLVector3(obPart.OffsetPosition);
  1879. currentpos.X *= x;
  1880. currentpos.Y *= y;
  1881. currentpos.Z *= z;
  1882. LLVector3 newSize = new LLVector3(obPart.Scale);
  1883. newSize.X *= x;
  1884. newSize.Y *= y;
  1885. newSize.Z *= z;
  1886. obPart.Resize(newSize);
  1887. obPart.UpdateOffSet(currentpos);
  1888. }
  1889. }
  1890. }
  1891. if (part.PhysActor != null)
  1892. {
  1893. part.PhysActor.Size =
  1894. new PhysicsVector(scale.X, scale.Y, scale.Z);
  1895. m_scene.PhysicsScene.AddPhysicsActorTaint(part.PhysActor);
  1896. }
  1897. ScheduleGroupForTerseUpdate();
  1898. }
  1899. }
  1900. #endregion
  1901. #region Position
  1902. /// <summary>
  1903. ///
  1904. /// </summary>
  1905. /// <param name="pos"></param>
  1906. public void UpdateGroupPosition(LLVector3 pos)
  1907. {
  1908. if (m_scene.EventManager.TriggerGroupMove(UUID, pos))
  1909. {
  1910. if (m_rootPart.m_IsAttachment)
  1911. {
  1912. m_rootPart.m_attachedPos = pos;
  1913. }
  1914. AbsolutePosition = pos;
  1915. }
  1916. //we need to do a terse update even if the move wasn't allowed
  1917. // so that the position is reset in the client (the object snaps back)
  1918. ScheduleGroupForTerseUpdate();
  1919. }
  1920. /// <summary>
  1921. ///
  1922. /// </summary>
  1923. /// <param name="pos"></param>
  1924. /// <param name="localID"></param>
  1925. public void UpdateSinglePosition(LLVector3 pos, uint localID)
  1926. {
  1927. SceneObjectPart part = GetChildPart(localID);
  1928. if (part != null)
  1929. {
  1930. if (part.UUID == m_rootPart.UUID)
  1931. {
  1932. UpdateRootPosition(pos);
  1933. }
  1934. else
  1935. {
  1936. part.UpdateOffSet(pos);
  1937. }
  1938. }
  1939. }
  1940. /// <summary>
  1941. ///
  1942. /// </summary>
  1943. /// <param name="pos"></param>
  1944. private void UpdateRootPosition(LLVector3 pos)
  1945. {
  1946. LLVector3 newPos = new LLVector3(pos.X, pos.Y, pos.Z);
  1947. LLVector3 oldPos =
  1948. new LLVector3(AbsolutePosition.X + m_rootPart.OffsetPosition.X,
  1949. AbsolutePosition.Y + m_rootPart.OffsetPosition.Y,
  1950. AbsolutePosition.Z + m_rootPart.OffsetPosition.Z);
  1951. LLVector3 diff = oldPos - newPos;
  1952. Vector3 axDiff = new Vector3(diff.X, diff.Y, diff.Z);
  1953. Quaternion partRotation =
  1954. new Quaternion(m_rootPart.RotationOffset.W, m_rootPart.RotationOffset.X, m_rootPart.RotationOffset.Y,
  1955. m_rootPart.RotationOffset.Z);
  1956. axDiff = partRotation.Inverse() * axDiff;
  1957. diff.X = axDiff.x;
  1958. diff.Y = axDiff.y;
  1959. diff.Z = axDiff.z;
  1960. lock (m_parts)
  1961. {
  1962. foreach (SceneObjectPart obPart in m_parts.Values)
  1963. {
  1964. if (obPart.UUID != m_rootPart.UUID)
  1965. {
  1966. obPart.OffsetPosition = obPart.OffsetPosition + diff;
  1967. }
  1968. }
  1969. }
  1970. AbsolutePosition = newPos;
  1971. ScheduleGroupForTerseUpdate();
  1972. }
  1973. public void OffsetForNewRegion(LLVector3 offset)
  1974. {
  1975. m_rootPart.GroupPosition = offset;
  1976. }
  1977. #endregion
  1978. #region Rotation
  1979. /// <summary>
  1980. ///
  1981. /// </summary>
  1982. /// <param name="rot"></param>
  1983. public void UpdateGroupRotation(LLQuaternion rot)
  1984. {
  1985. m_rootPart.UpdateRotation(rot);
  1986. if (m_rootPart.PhysActor != null)
  1987. {
  1988. m_rootPart.PhysActor.Orientation =
  1989. new Quaternion(m_rootPart.RotationOffset.W, m_rootPart.RotationOffset.X, m_rootPart.RotationOffset.Y,
  1990. m_rootPart.RotationOffset.Z);
  1991. m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  1992. }
  1993. ScheduleGroupForTerseUpdate();
  1994. }
  1995. /// <summary>
  1996. ///
  1997. /// </summary>
  1998. /// <param name="pos"></param>
  1999. /// <param name="rot"></param>
  2000. public void UpdateGroupRotation(LLVector3 pos, LLQuaternion rot)
  2001. {
  2002. m_rootPart.UpdateRotation(rot);
  2003. if (m_rootPart.PhysActor != null)
  2004. {
  2005. m_rootPart.PhysActor.Orientation =
  2006. new Quaternion(m_rootPart.RotationOffset.W, m_rootPart.RotationOffset.X, m_rootPart.RotationOffset.Y,
  2007. m_rootPart.RotationOffset.Z);
  2008. m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  2009. }
  2010. AbsolutePosition = pos;
  2011. ScheduleGroupForTerseUpdate();
  2012. }
  2013. /// <summary>
  2014. ///
  2015. /// </summary>
  2016. /// <param name="rot"></param>
  2017. /// <param name="localID"></param>
  2018. public void UpdateSingleRotation(LLQuaternion rot, uint localID)
  2019. {
  2020. SceneObjectPart part = GetChildPart(localID);
  2021. if (part != null)
  2022. {
  2023. if (part.UUID == m_rootPart.UUID)
  2024. {
  2025. UpdateRootRotation(rot);
  2026. }
  2027. else
  2028. {
  2029. part.UpdateRotation(rot);
  2030. }
  2031. }
  2032. }
  2033. /// <summary>
  2034. ///
  2035. /// </summary>
  2036. /// <param name="rot"></param>
  2037. private void UpdateRootRotation(LLQuaternion rot)
  2038. {
  2039. Quaternion axRot = new Quaternion(rot.W, rot.X, rot.Y, rot.Z);
  2040. Quaternion oldParentRot =
  2041. new Quaternion(m_rootPart.RotationOffset.W, m_rootPart.RotationOffset.X, m_rootPart.RotationOffset.Y,
  2042. m_rootPart.RotationOffset.Z);
  2043. m_rootPart.UpdateRotation(rot);
  2044. if (m_rootPart.PhysActor != null)
  2045. {
  2046. m_rootPart.PhysActor.Orientation =
  2047. new Quaternion(m_rootPart.RotationOffset.W, m_rootPart.RotationOffset.X, m_rootPart.RotationOffset.Y,
  2048. m_rootPart.RotationOffset.Z);
  2049. m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  2050. }
  2051. lock (m_parts)
  2052. {
  2053. foreach (SceneObjectPart prim in m_parts.Values)
  2054. {
  2055. if (prim.UUID != m_rootPart.UUID)
  2056. {
  2057. Vector3 axPos = new Vector3(prim.OffsetPosition.X, prim.OffsetPosition.Y, prim.OffsetPosition.Z);
  2058. axPos = oldParentRot * axPos;
  2059. axPos = axRot.Inverse() * axPos;
  2060. prim.OffsetPosition = new LLVector3(axPos.x, axPos.y, axPos.z);
  2061. Quaternion primsRot =
  2062. new Quaternion(prim.RotationOffset.W, prim.RotationOffset.X, prim.RotationOffset.Y,
  2063. prim.RotationOffset.Z);
  2064. Quaternion newRot = oldParentRot * primsRot;
  2065. newRot = axRot.Inverse() * newRot;
  2066. prim.RotationOffset = new LLQuaternion(newRot.x, newRot.y, newRot.z, newRot.w);
  2067. prim.ScheduleTerseUpdate();
  2068. }
  2069. }
  2070. }
  2071. m_rootPart.ScheduleTerseUpdate();
  2072. }
  2073. #endregion
  2074. internal void SetAxisRotation(int axis, int rotate10)
  2075. {
  2076. bool setX = false;
  2077. bool setY = false;
  2078. bool setZ = false;
  2079. int xaxis = 2;
  2080. int yaxis = 4;
  2081. int zaxis = 8;
  2082. if (m_rootPart != null)
  2083. {
  2084. setX = ((axis & xaxis) != 0) ? true : false;
  2085. setY = ((axis & yaxis) != 0) ? true : false;
  2086. setZ = ((axis & zaxis) != 0) ? true : false;
  2087. float setval = (rotate10 > 0) ? 1f : 0f;
  2088. if (setX)
  2089. m_rootPart.m_rotationAxis.X = setval;
  2090. if (setY)
  2091. m_rootPart.m_rotationAxis.Y = setval;
  2092. if (setZ)
  2093. m_rootPart.m_rotationAxis.Z = setval;
  2094. if (setX || setY || setZ)
  2095. {
  2096. m_rootPart.SetPhysicsAxisRotation();
  2097. }
  2098. }
  2099. }
  2100. public int registerTargetWaypoint(LLVector3 target, float tolerance)
  2101. {
  2102. scriptPosTarget waypoint = new scriptPosTarget();
  2103. waypoint.targetPos = target;
  2104. waypoint.tolerance = tolerance;
  2105. uint handle = m_scene.PrimIDAllocate();
  2106. lock (m_targets)
  2107. {
  2108. m_targets.Add(handle, waypoint);
  2109. }
  2110. return (int)handle;
  2111. }
  2112. public void unregisterTargetWaypoint(int handle)
  2113. {
  2114. lock (m_targets)
  2115. {
  2116. if (m_targets.ContainsKey((uint)handle))
  2117. m_targets.Remove((uint)handle);
  2118. }
  2119. }
  2120. private void checkAtTargets()
  2121. {
  2122. if (m_scriptListens_atTarget || m_scriptListens_notAtTarget)
  2123. {
  2124. if (m_targets.Count > 0)
  2125. {
  2126. bool at_target = false;
  2127. //LLVector3 targetPos;
  2128. //uint targetHandle;
  2129. Dictionary<uint, scriptPosTarget> atTargets = new Dictionary<uint, scriptPosTarget>();
  2130. lock (m_targets)
  2131. {
  2132. foreach (uint idx in m_targets.Keys)
  2133. {
  2134. scriptPosTarget target = m_targets[idx];
  2135. if (Util.GetDistanceTo(target.targetPos, m_rootPart.GroupPosition) <= target.tolerance)
  2136. {
  2137. // trigger at_target
  2138. if (m_scriptListens_atTarget)
  2139. {
  2140. // Reusing att.tolerance to hold the index of the target in the targets dictionary
  2141. // to avoid deadlocking the sim.
  2142. at_target = true;
  2143. scriptPosTarget att = new scriptPosTarget();
  2144. att.targetPos = target.targetPos;
  2145. att.tolerance = (float)idx;
  2146. atTargets.Add(idx, att);
  2147. }
  2148. }
  2149. }
  2150. }
  2151. if (atTargets.Count > 0)
  2152. {
  2153. uint[] localids = new uint[0];
  2154. lock (m_parts)
  2155. {
  2156. localids = new uint[m_parts.Count];
  2157. int cntr = 0;
  2158. foreach (SceneObjectPart part in m_parts.Values)
  2159. {
  2160. localids[cntr] = part.LocalId;
  2161. cntr++;
  2162. }
  2163. }
  2164. for (int ctr = 0; ctr < localids.Length; ctr++)
  2165. {
  2166. foreach (uint target in atTargets.Keys)
  2167. {
  2168. scriptPosTarget att = atTargets[target];
  2169. // Reusing att.tolerance to hold the index of the target in the targets dictionary
  2170. // to avoid deadlocking the sim.
  2171. m_scene.TriggerAtTargetEvent(localids[ctr], (uint)att.tolerance, att.targetPos, m_rootPart.GroupPosition);
  2172. }
  2173. }
  2174. return;
  2175. }
  2176. if (m_scriptListens_notAtTarget && !at_target)
  2177. {
  2178. //trigger not_at_target
  2179. uint[] localids = new uint[0];
  2180. lock (m_parts)
  2181. {
  2182. localids = new uint[m_parts.Count];
  2183. int cntr = 0;
  2184. foreach (SceneObjectPart part in m_parts.Values)
  2185. {
  2186. localids[cntr] = part.LocalId;
  2187. cntr++;
  2188. }
  2189. }
  2190. for (int ctr = 0; ctr < localids.Length; ctr++)
  2191. {
  2192. m_scene.TriggerNotAtTargetEvent(localids[ctr]);
  2193. }
  2194. }
  2195. }
  2196. }
  2197. }
  2198. public float GetMass()
  2199. {
  2200. float retmass = 0f;
  2201. lock (m_parts)
  2202. {
  2203. foreach (SceneObjectPart part in m_parts.Values)
  2204. {
  2205. retmass += part.GetMass();
  2206. }
  2207. }
  2208. return retmass;
  2209. }
  2210. public void CheckSculptAndLoad()
  2211. {
  2212. lock (m_parts)
  2213. {
  2214. if (RootPart != null)
  2215. {
  2216. if ((RootPart.GetEffectiveObjectFlags() & (uint)LLObject.ObjectFlags.Phantom) == 0)
  2217. {
  2218. foreach (SceneObjectPart part in m_parts.Values)
  2219. {
  2220. if (part.Shape.SculptEntry && part.Shape.SculptTexture != LLUUID.Zero)
  2221. {
  2222. m_scene.AssetCache.GetAsset(part.Shape.SculptTexture, part.SculptTextureCallback, true);
  2223. }
  2224. }
  2225. }
  2226. }
  2227. }
  2228. }
  2229. public void SetGroup(LLUUID GroupID, IClientAPI client)
  2230. {
  2231. lock (m_parts)
  2232. {
  2233. foreach (SceneObjectPart part in m_parts.Values)
  2234. {
  2235. part.SetGroup(GroupID, client);
  2236. }
  2237. }
  2238. ScheduleGroupForFullUpdate();
  2239. }
  2240. }
  2241. }