SceneObjectGroup.cs 103 KB

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