SceneObjectGroup.cs 148 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891
  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 OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.ComponentModel;
  29. using System.Collections.Generic;
  30. using System.Drawing;
  31. using System.IO;
  32. using System.Linq;
  33. using System.Threading;
  34. using System.Xml;
  35. using System.Xml.Serialization;
  36. using OpenMetaverse;
  37. using OpenMetaverse.Packets;
  38. using OpenSim.Framework;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Region.Physics.Manager;
  41. using OpenSim.Region.Framework.Scenes.Serialization;
  42. using PermissionMask = OpenSim.Framework.PermissionMask;
  43. namespace OpenSim.Region.Framework.Scenes
  44. {
  45. [Flags]
  46. public enum scriptEvents
  47. {
  48. None = 0,
  49. attach = 1,
  50. collision = 16,
  51. collision_end = 32,
  52. collision_start = 64,
  53. control = 128,
  54. dataserver = 256,
  55. email = 512,
  56. http_response = 1024,
  57. land_collision = 2048,
  58. land_collision_end = 4096,
  59. land_collision_start = 8192,
  60. at_target = 16384,
  61. at_rot_target = 16777216,
  62. listen = 32768,
  63. money = 65536,
  64. moving_end = 131072,
  65. moving_start = 262144,
  66. not_at_rot_target = 524288,
  67. not_at_target = 1048576,
  68. remote_data = 8388608,
  69. run_time_permissions = 268435456,
  70. state_entry = 1073741824,
  71. state_exit = 2,
  72. timer = 4,
  73. touch = 8,
  74. touch_end = 536870912,
  75. touch_start = 2097152,
  76. object_rez = 4194304
  77. }
  78. struct scriptPosTarget
  79. {
  80. public Vector3 targetPos;
  81. public float tolerance;
  82. public uint handle;
  83. }
  84. struct scriptRotTarget
  85. {
  86. public Quaternion targetRot;
  87. public float tolerance;
  88. public uint handle;
  89. }
  90. public delegate void PrimCountTaintedDelegate();
  91. /// <summary>
  92. /// A scene object group is conceptually an object in the scene. The object is constituted of SceneObjectParts
  93. /// (often known as prims), one of which is considered the root part.
  94. /// </summary>
  95. public partial class SceneObjectGroup : EntityBase, ISceneObject
  96. {
  97. // Axis selection bitmask used by SetAxisRotation()
  98. // Just happen to be the same bits used by llSetStatus() and defined in ScriptBaseClass.
  99. public enum axisSelect : int
  100. {
  101. STATUS_ROTATE_X = 0x002,
  102. STATUS_ROTATE_Y = 0x004,
  103. STATUS_ROTATE_Z = 0x008,
  104. }
  105. // This flag has the same purpose as InventoryItemFlags.ObjectSlamPerm
  106. public static readonly uint SLAM = 16;
  107. // private PrimCountTaintedDelegate handlerPrimCountTainted = null;
  108. /// <summary>
  109. /// Signal whether the non-inventory attributes of any prims in the group have changed
  110. /// since the group's last persistent backup
  111. /// </summary>
  112. private bool m_hasGroupChanged = false;
  113. private long timeFirstChanged;
  114. private long timeLastChanged;
  115. /// <summary>
  116. /// This indicates whether the object has changed such that it needs to be repersisted to permenant storage
  117. /// (the database).
  118. /// </summary>
  119. /// <remarks>
  120. /// Ultimately, this should be managed such that region modules can change it at the end of a set of operations
  121. /// so that either all changes are preserved or none at all. However, currently, a large amount of internal
  122. /// code will set this anyway when some object properties are changed.
  123. /// </remarks>
  124. public bool HasGroupChanged
  125. {
  126. set
  127. {
  128. if (value)
  129. {
  130. timeLastChanged = DateTime.Now.Ticks;
  131. if (!m_hasGroupChanged)
  132. timeFirstChanged = DateTime.Now.Ticks;
  133. }
  134. m_hasGroupChanged = value;
  135. // m_log.DebugFormat(
  136. // "[SCENE OBJECT GROUP]: HasGroupChanged set to {0} for {1} {2}", m_hasGroupChanged, Name, LocalId);
  137. }
  138. get { return m_hasGroupChanged; }
  139. }
  140. private bool m_groupContainsForeignPrims = false;
  141. /// <summary>
  142. /// Whether the group contains prims that came from a different group. This happens when
  143. /// linking or delinking groups. The implication is that until the group is persisted,
  144. /// the prims in the database still use the old SceneGroupID. That's a problem if the group
  145. /// is deleted, because we delete groups by searching for prims by their SceneGroupID.
  146. /// </summary>
  147. public bool GroupContainsForeignPrims
  148. {
  149. private set
  150. {
  151. m_groupContainsForeignPrims = value;
  152. if (m_groupContainsForeignPrims)
  153. HasGroupChanged = true;
  154. }
  155. get { return m_groupContainsForeignPrims; }
  156. }
  157. private bool isTimeToPersist()
  158. {
  159. if (IsSelected || IsDeleted || IsAttachment)
  160. return false;
  161. if (!m_hasGroupChanged)
  162. return false;
  163. if (m_scene.ShuttingDown)
  164. return true;
  165. long currentTime = DateTime.Now.Ticks;
  166. if (currentTime - timeLastChanged > m_scene.m_dontPersistBefore || currentTime - timeFirstChanged > m_scene.m_persistAfter)
  167. return true;
  168. return false;
  169. }
  170. /// <summary>
  171. /// Is this scene object acting as an attachment?
  172. /// </summary>
  173. public bool IsAttachment { get; set; }
  174. /// <summary>
  175. /// The avatar to which this scene object is attached.
  176. /// </summary>
  177. /// <remarks>
  178. /// If we're not attached to an avatar then this is UUID.Zero
  179. /// </remarks>
  180. public UUID AttachedAvatar { get; set; }
  181. /// <summary>
  182. /// Attachment point of this scene object to an avatar.
  183. /// </summary>
  184. /// <remarks>
  185. /// 0 if we're not attached to anything
  186. /// </remarks>
  187. public uint AttachmentPoint
  188. {
  189. get
  190. {
  191. return m_rootPart.Shape.State;
  192. }
  193. set
  194. {
  195. IsAttachment = value != 0;
  196. m_rootPart.Shape.State = (byte)value;
  197. }
  198. }
  199. /// <summary>
  200. /// If this scene object has an attachment point then indicate whether there is a point where
  201. /// attachments are perceivable by avatars other than the avatar to which this object is attached.
  202. /// </summary>
  203. /// <remarks>
  204. /// HUDs are not perceivable by other avatars.
  205. /// </remarks>
  206. public bool HasPrivateAttachmentPoint
  207. {
  208. get
  209. {
  210. return AttachmentPoint >= (uint)OpenMetaverse.AttachmentPoint.HUDCenter2
  211. && AttachmentPoint <= (uint)OpenMetaverse.AttachmentPoint.HUDBottomRight;
  212. }
  213. }
  214. public void ClearPartAttachmentData()
  215. {
  216. AttachmentPoint = 0;
  217. // Even though we don't use child part state parameters for attachments any more, we still need to set
  218. // these to zero since having them non-zero in rezzed scene objects will crash some clients. Even if
  219. // we store them correctly, scene objects that we receive from elsewhere might not.
  220. foreach (SceneObjectPart part in Parts)
  221. part.Shape.State = 0;
  222. }
  223. /// <summary>
  224. /// Is this scene object phantom?
  225. /// </summary>
  226. /// <remarks>
  227. /// Updating must currently take place through UpdatePrimFlags()
  228. /// </remarks>
  229. public bool IsPhantom
  230. {
  231. get { return (RootPart.Flags & PrimFlags.Phantom) != 0; }
  232. }
  233. /// <summary>
  234. /// Does this scene object use physics?
  235. /// </summary>
  236. /// <remarks>
  237. /// Updating must currently take place through UpdatePrimFlags()
  238. /// </remarks>
  239. public bool UsesPhysics
  240. {
  241. get { return (RootPart.Flags & PrimFlags.Physics) != 0; }
  242. }
  243. /// <summary>
  244. /// Is this scene object temporary?
  245. /// </summary>
  246. /// <remarks>
  247. /// Updating must currently take place through UpdatePrimFlags()
  248. /// </remarks>
  249. public bool IsTemporary
  250. {
  251. get { return (RootPart.Flags & PrimFlags.TemporaryOnRez) != 0; }
  252. }
  253. public bool IsVolumeDetect
  254. {
  255. get { return RootPart.VolumeDetectActive; }
  256. }
  257. private Vector3 lastPhysGroupPos;
  258. private Quaternion lastPhysGroupRot;
  259. /// <summary>
  260. /// Is this entity set to be saved in persistent storage?
  261. /// </summary>
  262. public bool Backup { get; private set; }
  263. protected MapAndArray<UUID, SceneObjectPart> m_parts = new MapAndArray<UUID, SceneObjectPart>();
  264. protected ulong m_regionHandle;
  265. protected SceneObjectPart m_rootPart;
  266. // private Dictionary<UUID, scriptEvents> m_scriptEvents = new Dictionary<UUID, scriptEvents>();
  267. private Dictionary<uint, scriptPosTarget> m_targets = new Dictionary<uint, scriptPosTarget>();
  268. private Dictionary<uint, scriptRotTarget> m_rotTargets = new Dictionary<uint, scriptRotTarget>();
  269. private bool m_scriptListens_atTarget;
  270. private bool m_scriptListens_notAtTarget;
  271. private bool m_scriptListens_atRotTarget;
  272. private bool m_scriptListens_notAtRotTarget;
  273. internal Dictionary<UUID, string> m_savedScriptState;
  274. #region Properties
  275. /// <summary>
  276. /// The name of an object grouping is always the same as its root part
  277. /// </summary>
  278. public override string Name
  279. {
  280. get { return RootPart.Name; }
  281. set { RootPart.Name = value; }
  282. }
  283. public string Description
  284. {
  285. get { return RootPart.Description; }
  286. set { RootPart.Description = value; }
  287. }
  288. /// <summary>
  289. /// Added because the Parcel code seems to use it
  290. /// but not sure a object should have this
  291. /// as what does it tell us? that some avatar has selected it (but not what Avatar/user)
  292. /// think really there should be a list (or whatever) in each scenepresence
  293. /// saying what prim(s) that user has selected.
  294. /// </summary>
  295. protected bool m_isSelected = false;
  296. /// <summary>
  297. /// Number of prims in this group
  298. /// </summary>
  299. public int PrimCount
  300. {
  301. get { return m_parts.Count; }
  302. }
  303. public Quaternion GroupRotation
  304. {
  305. get { return m_rootPart.RotationOffset; }
  306. }
  307. public Vector3 GroupScale
  308. {
  309. get
  310. {
  311. Vector3 minScale = new Vector3(Constants.MaximumRegionSize, Constants.MaximumRegionSize, Constants.MaximumRegionSize);
  312. Vector3 maxScale = Vector3.Zero;
  313. Vector3 finalScale = new Vector3(0.5f, 0.5f, 0.5f);
  314. SceneObjectPart[] parts = m_parts.GetArray();
  315. for (int i = 0; i < parts.Length; i++)
  316. {
  317. SceneObjectPart part = parts[i];
  318. Vector3 partscale = part.Scale;
  319. Vector3 partoffset = part.OffsetPosition;
  320. minScale.X = (partscale.X + partoffset.X < minScale.X) ? partscale.X + partoffset.X : minScale.X;
  321. minScale.Y = (partscale.Y + partoffset.Y < minScale.Y) ? partscale.Y + partoffset.Y : minScale.Y;
  322. minScale.Z = (partscale.Z + partoffset.Z < minScale.Z) ? partscale.Z + partoffset.Z : minScale.Z;
  323. maxScale.X = (partscale.X + partoffset.X > maxScale.X) ? partscale.X + partoffset.X : maxScale.X;
  324. maxScale.Y = (partscale.Y + partoffset.Y > maxScale.Y) ? partscale.Y + partoffset.Y : maxScale.Y;
  325. maxScale.Z = (partscale.Z + partoffset.Z > maxScale.Z) ? partscale.Z + partoffset.Z : maxScale.Z;
  326. }
  327. finalScale.X = (minScale.X > maxScale.X) ? minScale.X : maxScale.X;
  328. finalScale.Y = (minScale.Y > maxScale.Y) ? minScale.Y : maxScale.Y;
  329. finalScale.Z = (minScale.Z > maxScale.Z) ? minScale.Z : maxScale.Z;
  330. return finalScale;
  331. }
  332. }
  333. public UUID GroupID
  334. {
  335. get { return m_rootPart.GroupID; }
  336. set { m_rootPart.GroupID = value; }
  337. }
  338. public SceneObjectPart[] Parts
  339. {
  340. get { return m_parts.GetArray(); }
  341. }
  342. public bool ContainsPart(UUID partID)
  343. {
  344. return m_parts.ContainsKey(partID);
  345. }
  346. /// <summary>
  347. /// Does this group contain the given part?
  348. /// should be able to remove these methods once we have a entity index in scene
  349. /// </summary>
  350. /// <param name="localID"></param>
  351. /// <returns></returns>
  352. public bool ContainsPart(uint localID)
  353. {
  354. SceneObjectPart[] parts = m_parts.GetArray();
  355. for (int i = 0; i < parts.Length; i++)
  356. {
  357. if (parts[i].LocalId == localID)
  358. return true;
  359. }
  360. return false;
  361. }
  362. /// <value>
  363. /// The root part of this scene object
  364. /// </value>
  365. public SceneObjectPart RootPart
  366. {
  367. get { return m_rootPart; }
  368. }
  369. public ulong RegionHandle
  370. {
  371. get { return m_regionHandle; }
  372. set
  373. {
  374. m_regionHandle = value;
  375. SceneObjectPart[] parts = m_parts.GetArray();
  376. for (int i = 0; i < parts.Length; i++)
  377. parts[i].RegionHandle = value;
  378. }
  379. }
  380. /// <summary>
  381. /// Check both the attachment property and the relevant properties of the underlying root part.
  382. /// </summary>
  383. /// <remarks>
  384. /// This is necessary in some cases, particularly when a scene object has just crossed into a region and doesn't
  385. /// have the IsAttachment property yet checked.
  386. ///
  387. /// FIXME: However, this should be fixed so that this property
  388. /// propertly reflects the underlying status.
  389. /// </remarks>
  390. /// <returns></returns>
  391. public bool IsAttachmentCheckFull()
  392. {
  393. return (IsAttachment || (m_rootPart.Shape.PCode == (byte)PCodeEnum.Primitive && m_rootPart.Shape.State != 0));
  394. }
  395. private struct avtocrossInfo
  396. {
  397. public ScenePresence av;
  398. public uint ParentID;
  399. }
  400. /// <summary>
  401. /// The absolute position of this scene object in the scene
  402. /// </summary>
  403. public override Vector3 AbsolutePosition
  404. {
  405. get { return m_rootPart.GroupPosition; }
  406. set
  407. {
  408. Vector3 val = value;
  409. if (Scene != null)
  410. {
  411. if (
  412. !Scene.PositionIsInCurrentRegion(val)
  413. && !IsAttachmentCheckFull()
  414. && (!Scene.LoadingPrims)
  415. )
  416. {
  417. IEntityTransferModule entityTransfer = m_scene.RequestModuleInterface<IEntityTransferModule>();
  418. string version = String.Empty;
  419. Vector3 newpos = Vector3.Zero;
  420. string failureReason = String.Empty;
  421. OpenSim.Services.Interfaces.GridRegion destination = null;
  422. if (m_rootPart.KeyframeMotion != null)
  423. m_rootPart.KeyframeMotion.StartCrossingCheck();
  424. bool canCross = true;
  425. foreach (ScenePresence av in GetSittingAvatars())
  426. {
  427. // We need to cross these agents. First, let's find
  428. // out if any of them can't cross for some reason.
  429. // We have to deny the crossing entirely if any
  430. // of them are banned. Alternatively, we could
  431. // unsit banned agents....
  432. // We set the avatar position as being the object
  433. // position to get the region to send to
  434. if ((destination = entityTransfer.GetDestination(m_scene, av.UUID, val, out version, out newpos, out failureReason)) == null)
  435. {
  436. canCross = false;
  437. break;
  438. }
  439. m_log.DebugFormat("[SCENE OBJECT]: Avatar {0} needs to be crossed to {1}", av.Name, destination.RegionName);
  440. }
  441. if (canCross)
  442. {
  443. // We unparent the SP quietly so that it won't
  444. // be made to stand up
  445. List<avtocrossInfo> avsToCross = new List<avtocrossInfo>();
  446. foreach (ScenePresence av in GetSittingAvatars())
  447. {
  448. avtocrossInfo avinfo = new avtocrossInfo();
  449. SceneObjectPart parentPart = m_scene.GetSceneObjectPart(av.ParentID);
  450. if (parentPart != null)
  451. av.ParentUUID = parentPart.UUID;
  452. avinfo.av = av;
  453. avinfo.ParentID = av.ParentID;
  454. avsToCross.Add(avinfo);
  455. av.PrevSitOffset = av.OffsetPosition;
  456. av.ParentID = 0;
  457. }
  458. m_scene.CrossPrimGroupIntoNewRegion(val, this, true);
  459. // Normalize
  460. if (val.X >= m_scene.RegionInfo.RegionSizeX)
  461. val.X -= m_scene.RegionInfo.RegionSizeX;
  462. if (val.Y >= m_scene.RegionInfo.RegionSizeY)
  463. val.Y -= m_scene.RegionInfo.RegionSizeY;
  464. if (val.X < 0)
  465. val.X += m_scene.RegionInfo.RegionSizeX;
  466. if (val.Y < 0)
  467. val.Y += m_scene.RegionInfo.RegionSizeY;
  468. // If it's deleted, crossing was successful
  469. if (IsDeleted)
  470. {
  471. foreach (avtocrossInfo avinfo in avsToCross)
  472. {
  473. ScenePresence av = avinfo.av;
  474. if (!av.IsInTransit) // just in case...
  475. {
  476. m_log.DebugFormat("[SCENE OBJECT]: Crossing avatar {0} to {1}", av.Name, val);
  477. av.IsInTransit = true;
  478. // A temporary measure to allow regression tests to work.
  479. // Quite possibly, all BeginInvoke() calls should be replaced by Util.FireAndForget
  480. // or similar since BeginInvoke() always uses the system threadpool to launch
  481. // threads rather than any replace threadpool that we might be using.
  482. if (Util.FireAndForgetMethod == FireAndForgetMethod.RegressionTest)
  483. {
  484. entityTransfer.CrossAgentToNewRegionAsync(av, val, destination, av.Flying, version);
  485. CrossAgentToNewRegionCompleted(av);
  486. }
  487. else
  488. {
  489. CrossAgentToNewRegionDelegate d = entityTransfer.CrossAgentToNewRegionAsync;
  490. d.BeginInvoke(
  491. av, val, destination, av.Flying, version,
  492. ar => CrossAgentToNewRegionCompleted(d.EndInvoke(ar)), null);
  493. }
  494. }
  495. else
  496. {
  497. m_log.DebugFormat("[SCENE OBJECT]: Not crossing avatar {0} to {1} because it's already in transit", av.Name, val);
  498. }
  499. }
  500. return;
  501. }
  502. else // cross failed, put avas back ??
  503. {
  504. foreach (avtocrossInfo avinfo in avsToCross)
  505. {
  506. ScenePresence av = avinfo.av;
  507. av.ParentUUID = UUID.Zero;
  508. av.ParentID = avinfo.ParentID;
  509. }
  510. }
  511. }
  512. else
  513. {
  514. if (m_rootPart.KeyframeMotion != null)
  515. m_rootPart.KeyframeMotion.CrossingFailure();
  516. if (RootPart.PhysActor != null)
  517. {
  518. RootPart.PhysActor.CrossingFailure();
  519. }
  520. }
  521. Vector3 oldp = AbsolutePosition;
  522. val.X = Util.Clamp<float>(oldp.X, 0.5f, (float)m_scene.RegionInfo.RegionSizeX - 0.5f);
  523. val.Y = Util.Clamp<float>(oldp.Y, 0.5f, (float)m_scene.RegionInfo.RegionSizeY - 0.5f);
  524. val.Z = Util.Clamp<float>(oldp.Z, 0.5f, Constants.RegionHeight);
  525. }
  526. }
  527. if (RootPart.GetStatusSandbox())
  528. {
  529. if (Util.GetDistanceTo(RootPart.StatusSandboxPos, value) > 10)
  530. {
  531. RootPart.ScriptSetPhysicsStatus(false);
  532. if (Scene != null)
  533. Scene.SimChat(Utils.StringToBytes("Hit Sandbox Limit"),
  534. ChatTypeEnum.DebugChannel, 0x7FFFFFFF, RootPart.AbsolutePosition, Name, UUID, false);
  535. return;
  536. }
  537. }
  538. // Restuff the new GroupPosition into each SOP of the linkset.
  539. // This has the affect of resetting and tainting the physics actors.
  540. SceneObjectPart[] parts = m_parts.GetArray();
  541. for (int i = 0; i < parts.Length; i++)
  542. parts[i].GroupPosition = val;
  543. //if (m_rootPart.PhysActor != null)
  544. //{
  545. //m_rootPart.PhysActor.Position =
  546. //new PhysicsVector(m_rootPart.GroupPosition.X, m_rootPart.GroupPosition.Y,
  547. //m_rootPart.GroupPosition.Z);
  548. //m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  549. //}
  550. if (Scene != null)
  551. Scene.EventManager.TriggerParcelPrimCountTainted();
  552. }
  553. }
  554. public override Vector3 Velocity
  555. {
  556. get { return RootPart.Velocity; }
  557. set { RootPart.Velocity = value; }
  558. }
  559. private void CrossAgentToNewRegionCompleted(ScenePresence agent)
  560. {
  561. //// If the cross was successful, this agent is a child agent
  562. if (agent.IsChildAgent)
  563. {
  564. if (agent.ParentUUID != UUID.Zero)
  565. {
  566. agent.ParentPart = null;
  567. // agent.ParentPosition = Vector3.Zero;
  568. // agent.ParentUUID = UUID.Zero;
  569. }
  570. }
  571. agent.ParentUUID = UUID.Zero;
  572. // agent.Reset();
  573. // else // Not successful
  574. // agent.RestoreInCurrentScene();
  575. // In any case
  576. agent.IsInTransit = false;
  577. m_log.DebugFormat("[SCENE OBJECT]: Crossing agent {0} {1} completed.", agent.Firstname, agent.Lastname);
  578. }
  579. public override uint LocalId
  580. {
  581. get { return m_rootPart.LocalId; }
  582. set { m_rootPart.LocalId = value; }
  583. }
  584. public override UUID UUID
  585. {
  586. get { return m_rootPart.UUID; }
  587. set
  588. {
  589. lock (m_parts.SyncRoot)
  590. {
  591. m_parts.Remove(m_rootPart.UUID);
  592. m_rootPart.UUID = value;
  593. m_parts.Add(value, m_rootPart);
  594. }
  595. }
  596. }
  597. public UUID LastOwnerID
  598. {
  599. get { return m_rootPart.LastOwnerID; }
  600. set { m_rootPart.LastOwnerID = value; }
  601. }
  602. public UUID OwnerID
  603. {
  604. get { return m_rootPart.OwnerID; }
  605. set { m_rootPart.OwnerID = value; }
  606. }
  607. public float Damage
  608. {
  609. get { return m_rootPart.Damage; }
  610. set { m_rootPart.Damage = value; }
  611. }
  612. public Color Color
  613. {
  614. get { return m_rootPart.Color; }
  615. set { m_rootPart.Color = value; }
  616. }
  617. public string Text
  618. {
  619. get {
  620. string returnstr = m_rootPart.Text;
  621. if (returnstr.Length > 255)
  622. {
  623. returnstr = returnstr.Substring(0, 255);
  624. }
  625. return returnstr;
  626. }
  627. set { m_rootPart.Text = value; }
  628. }
  629. /// <summary>
  630. /// If set to true then the scene object can be backed up in principle, though this will only actually occur
  631. /// if Backup is set. If false then the scene object will never be backed up, Backup will always be false.
  632. /// </summary>
  633. protected virtual bool CanBeBackedUp
  634. {
  635. get { return true; }
  636. }
  637. public bool IsSelected
  638. {
  639. get { return m_isSelected; }
  640. set
  641. {
  642. m_isSelected = value;
  643. // Tell physics engine that group is selected
  644. PhysicsActor pa = m_rootPart.PhysActor;
  645. if (pa != null)
  646. {
  647. pa.Selected = value;
  648. // Pass it on to the children.
  649. SceneObjectPart[] parts = m_parts.GetArray();
  650. for (int i = 0; i < parts.Length; i++)
  651. {
  652. SceneObjectPart child = parts[i];
  653. PhysicsActor childPa = child.PhysActor;
  654. if (childPa != null)
  655. childPa.Selected = value;
  656. }
  657. }
  658. if (RootPart.KeyframeMotion != null)
  659. RootPart.KeyframeMotion.Selected = value;
  660. }
  661. }
  662. private SceneObjectPart m_PlaySoundMasterPrim = null;
  663. public SceneObjectPart PlaySoundMasterPrim
  664. {
  665. get { return m_PlaySoundMasterPrim; }
  666. set { m_PlaySoundMasterPrim = value; }
  667. }
  668. private List<SceneObjectPart> m_PlaySoundSlavePrims = new List<SceneObjectPart>();
  669. public List<SceneObjectPart> PlaySoundSlavePrims
  670. {
  671. get { return m_PlaySoundSlavePrims; }
  672. set { m_PlaySoundSlavePrims = value; }
  673. }
  674. private SceneObjectPart m_LoopSoundMasterPrim = null;
  675. public SceneObjectPart LoopSoundMasterPrim
  676. {
  677. get { return m_LoopSoundMasterPrim; }
  678. set { m_LoopSoundMasterPrim = value; }
  679. }
  680. private List<SceneObjectPart> m_LoopSoundSlavePrims = new List<SceneObjectPart>();
  681. public List<SceneObjectPart> LoopSoundSlavePrims
  682. {
  683. get { return m_LoopSoundSlavePrims; }
  684. set { m_LoopSoundSlavePrims = value; }
  685. }
  686. /// <summary>
  687. /// The UUID for the region this object is in.
  688. /// </summary>
  689. public UUID RegionUUID
  690. {
  691. get
  692. {
  693. if (m_scene != null)
  694. {
  695. return m_scene.RegionInfo.RegionID;
  696. }
  697. return UUID.Zero;
  698. }
  699. }
  700. /// <summary>
  701. /// The item ID that this object was rezzed from, if applicable.
  702. /// </summary>
  703. /// <remarks>
  704. /// If not applicable will be UUID.Zero
  705. /// </remarks>
  706. public UUID FromItemID { get; set; }
  707. /// <summary>
  708. /// Refers to the SceneObjectPart.UUID property of the object that this object was rezzed from, if applicable.
  709. /// </summary>
  710. /// <remarks>
  711. /// If not applicable will be UUID.Zero
  712. /// </remarks>
  713. public UUID FromPartID { get; set; }
  714. /// <summary>
  715. /// The folder ID that this object was rezzed from, if applicable.
  716. /// </summary>
  717. /// <remarks>
  718. /// If not applicable will be UUID.Zero
  719. /// </remarks>
  720. public UUID FromFolderID { get; set; }
  721. /// <summary>
  722. /// If true then grabs are blocked no matter what the individual part BlockGrab setting.
  723. /// </summary>
  724. /// <value><c>true</c> if block grab override; otherwise, <c>false</c>.</value>
  725. public bool BlockGrabOverride { get; set; }
  726. /// <summary>
  727. /// IDs of all avatars sat on this scene object.
  728. /// </summary>
  729. /// <remarks>
  730. /// We need this so that we can maintain a linkset wide ordering of avatars sat on different parts.
  731. /// This must be locked before it is read or written.
  732. /// SceneObjectPart sitting avatar add/remove code also locks on this object to avoid race conditions.
  733. /// No avatar should appear more than once in this list.
  734. /// Do not manipulate this list directly - use the Add/Remove sitting avatar methods on SceneObjectPart.
  735. /// </remarks>
  736. protected internal List<ScenePresence> m_sittingAvatars = new List<ScenePresence>();
  737. #endregion
  738. // ~SceneObjectGroup()
  739. // {
  740. // //m_log.DebugFormat("[SCENE OBJECT GROUP]: Destructor called for {0}, local id {1}", Name, LocalId);
  741. // Console.WriteLine("Destructor called for {0}, local id {1}", Name, LocalId);
  742. // }
  743. #region Constructors
  744. /// <summary>
  745. /// Constructor
  746. /// </summary>
  747. public SceneObjectGroup()
  748. {
  749. }
  750. /// <summary>
  751. /// This constructor creates a SceneObjectGroup using a pre-existing SceneObjectPart.
  752. /// The original SceneObjectPart will be used rather than a copy, preserving
  753. /// its existing localID and UUID.
  754. /// </summary>
  755. /// <param name='part'>Root part for this scene object.</param>
  756. public SceneObjectGroup(SceneObjectPart part) : this()
  757. {
  758. SetRootPart(part);
  759. }
  760. /// <summary>
  761. /// Constructor. This object is added to the scene later via AttachToScene()
  762. /// </summary>
  763. public SceneObjectGroup(UUID ownerID, Vector3 pos, Quaternion rot, PrimitiveBaseShape shape)
  764. :this(new SceneObjectPart(ownerID, shape, pos, rot, Vector3.Zero))
  765. {
  766. }
  767. /// <summary>
  768. /// Constructor.
  769. /// </summary>
  770. public SceneObjectGroup(UUID ownerID, Vector3 pos, PrimitiveBaseShape shape)
  771. : this(ownerID, pos, Quaternion.Identity, shape)
  772. {
  773. }
  774. public void LoadScriptState(XmlDocument doc)
  775. {
  776. XmlNodeList nodes = doc.GetElementsByTagName("SavedScriptState");
  777. if (nodes.Count > 0)
  778. {
  779. if (m_savedScriptState == null)
  780. m_savedScriptState = new Dictionary<UUID, string>();
  781. foreach (XmlNode node in nodes)
  782. {
  783. if (node.Attributes["UUID"] != null)
  784. {
  785. UUID itemid = new UUID(node.Attributes["UUID"].Value);
  786. if (itemid != UUID.Zero)
  787. m_savedScriptState[itemid] = node.InnerXml;
  788. }
  789. }
  790. }
  791. }
  792. public void LoadScriptState(XmlReader reader)
  793. {
  794. // m_log.DebugFormat("[SCENE OBJECT GROUP]: Looking for script state for {0} in {1}", Name);
  795. while (reader.ReadToFollowing("SavedScriptState"))
  796. {
  797. // m_log.DebugFormat("[SCENE OBJECT GROUP]: Loading script state for {0}", Name);
  798. if (m_savedScriptState == null)
  799. m_savedScriptState = new Dictionary<UUID, string>();
  800. string uuid = reader.GetAttribute("UUID");
  801. if (uuid != null)
  802. {
  803. // m_log.DebugFormat("[SCENE OBJECT GROUP]: Found state for item ID {0} in object {1}", uuid, Name);
  804. UUID itemid = new UUID(uuid);
  805. if (itemid != UUID.Zero)
  806. m_savedScriptState[itemid] = reader.ReadInnerXml();
  807. }
  808. else
  809. {
  810. m_log.WarnFormat("[SCENE OBJECT GROUP]: SavedScriptState element had no UUID in object {0}", Name);
  811. }
  812. }
  813. }
  814. /// <summary>
  815. /// Hooks this object up to the backup event so that it is persisted to the database when the update thread executes.
  816. /// </summary>
  817. public virtual void AttachToBackup()
  818. {
  819. if (CanBeBackedUp)
  820. {
  821. // m_log.DebugFormat(
  822. // "[SCENE OBJECT GROUP]: Attaching object {0} {1} to scene presistence sweep", Name, UUID);
  823. if (!Backup)
  824. m_scene.EventManager.OnBackup += ProcessBackup;
  825. Backup = true;
  826. }
  827. }
  828. /// <summary>
  829. /// Attach this object to a scene. It will also now appear to agents.
  830. /// </summary>
  831. /// <param name="scene"></param>
  832. public void AttachToScene(Scene scene)
  833. {
  834. m_scene = scene;
  835. RegionHandle = m_scene.RegionInfo.RegionHandle;
  836. if (m_rootPart.Shape.PCode != 9 || m_rootPart.Shape.State == 0)
  837. m_rootPart.ParentID = 0;
  838. if (m_rootPart.LocalId == 0)
  839. m_rootPart.LocalId = m_scene.AllocateLocalId();
  840. SceneObjectPart[] parts = m_parts.GetArray();
  841. for (int i = 0; i < parts.Length; i++)
  842. {
  843. SceneObjectPart part = parts[i];
  844. if (part.KeyframeMotion != null)
  845. {
  846. part.KeyframeMotion.UpdateSceneObject(this);
  847. }
  848. if (Object.ReferenceEquals(part, m_rootPart))
  849. continue;
  850. if (part.LocalId == 0)
  851. part.LocalId = m_scene.AllocateLocalId();
  852. part.ParentID = m_rootPart.LocalId;
  853. //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);
  854. }
  855. ApplyPhysics();
  856. // Don't trigger the update here - otherwise some client issues occur when multiple updates are scheduled
  857. // for the same object with very different properties. The caller must schedule the update.
  858. //ScheduleGroupForFullUpdate();
  859. }
  860. public EntityIntersection TestIntersection(Ray hRay, bool frontFacesOnly, bool faceCenters)
  861. {
  862. // We got a request from the inner_scene to raytrace along the Ray hRay
  863. // We're going to check all of the prim in this group for intersection with the ray
  864. // If we get a result, we're going to find the closest result to the origin of the ray
  865. // and send back the intersection information back to the innerscene.
  866. EntityIntersection result = new EntityIntersection();
  867. SceneObjectPart[] parts = m_parts.GetArray();
  868. for (int i = 0; i < parts.Length; i++)
  869. {
  870. SceneObjectPart part = parts[i];
  871. // Temporary commented to stop compiler warning
  872. //Vector3 partPosition =
  873. // new Vector3(part.AbsolutePosition.X, part.AbsolutePosition.Y, part.AbsolutePosition.Z);
  874. Quaternion parentrotation = GroupRotation;
  875. // Telling the prim to raytrace.
  876. //EntityIntersection inter = part.TestIntersection(hRay, parentrotation);
  877. EntityIntersection inter = part.TestIntersectionOBB(hRay, parentrotation, frontFacesOnly, faceCenters);
  878. // This may need to be updated to the maximum draw distance possible..
  879. // We might (and probably will) be checking for prim creation from other sims
  880. // when the camera crosses the border.
  881. float idist = Constants.RegionSize;
  882. if (inter.HitTF)
  883. {
  884. // We need to find the closest prim to return to the testcaller along the ray
  885. if (inter.distance < idist)
  886. {
  887. result.HitTF = true;
  888. result.ipoint = inter.ipoint;
  889. result.obj = part;
  890. result.normal = inter.normal;
  891. result.distance = inter.distance;
  892. }
  893. }
  894. }
  895. return result;
  896. }
  897. /// <summary>
  898. /// Gets a vector representing the size of the bounding box containing all the prims in the group
  899. /// Treats all prims as rectangular, so no shape (cut etc) is taken into account
  900. /// offsetHeight is the offset in the Z axis from the centre of the bounding box to the centre of the root prim
  901. /// </summary>
  902. /// <returns></returns>
  903. public void GetAxisAlignedBoundingBoxRaw(out float minX, out float maxX, out float minY, out float maxY, out float minZ, out float maxZ)
  904. {
  905. maxX = -256f;
  906. maxY = -256f;
  907. maxZ = -256f;
  908. minX = 10000f;
  909. minY = 10000f;
  910. minZ = 10000f;
  911. SceneObjectPart[] parts = m_parts.GetArray();
  912. for (int i = 0; i < parts.Length; i++)
  913. {
  914. SceneObjectPart part = parts[i];
  915. Vector3 worldPos = part.GetWorldPosition();
  916. Vector3 offset = worldPos - AbsolutePosition;
  917. Quaternion worldRot;
  918. if (part.ParentID == 0)
  919. worldRot = part.RotationOffset;
  920. else
  921. worldRot = part.GetWorldRotation();
  922. Vector3 frontTopLeft;
  923. Vector3 frontTopRight;
  924. Vector3 frontBottomLeft;
  925. Vector3 frontBottomRight;
  926. Vector3 backTopLeft;
  927. Vector3 backTopRight;
  928. Vector3 backBottomLeft;
  929. Vector3 backBottomRight;
  930. Vector3 orig = Vector3.Zero;
  931. frontTopLeft.X = orig.X - (part.Scale.X / 2);
  932. frontTopLeft.Y = orig.Y - (part.Scale.Y / 2);
  933. frontTopLeft.Z = orig.Z + (part.Scale.Z / 2);
  934. frontTopRight.X = orig.X - (part.Scale.X / 2);
  935. frontTopRight.Y = orig.Y + (part.Scale.Y / 2);
  936. frontTopRight.Z = orig.Z + (part.Scale.Z / 2);
  937. frontBottomLeft.X = orig.X - (part.Scale.X / 2);
  938. frontBottomLeft.Y = orig.Y - (part.Scale.Y / 2);
  939. frontBottomLeft.Z = orig.Z - (part.Scale.Z / 2);
  940. frontBottomRight.X = orig.X - (part.Scale.X / 2);
  941. frontBottomRight.Y = orig.Y + (part.Scale.Y / 2);
  942. frontBottomRight.Z = orig.Z - (part.Scale.Z / 2);
  943. backTopLeft.X = orig.X + (part.Scale.X / 2);
  944. backTopLeft.Y = orig.Y - (part.Scale.Y / 2);
  945. backTopLeft.Z = orig.Z + (part.Scale.Z / 2);
  946. backTopRight.X = orig.X + (part.Scale.X / 2);
  947. backTopRight.Y = orig.Y + (part.Scale.Y / 2);
  948. backTopRight.Z = orig.Z + (part.Scale.Z / 2);
  949. backBottomLeft.X = orig.X + (part.Scale.X / 2);
  950. backBottomLeft.Y = orig.Y - (part.Scale.Y / 2);
  951. backBottomLeft.Z = orig.Z - (part.Scale.Z / 2);
  952. backBottomRight.X = orig.X + (part.Scale.X / 2);
  953. backBottomRight.Y = orig.Y + (part.Scale.Y / 2);
  954. backBottomRight.Z = orig.Z - (part.Scale.Z / 2);
  955. frontTopLeft = frontTopLeft * worldRot;
  956. frontTopRight = frontTopRight * worldRot;
  957. frontBottomLeft = frontBottomLeft * worldRot;
  958. frontBottomRight = frontBottomRight * worldRot;
  959. backBottomLeft = backBottomLeft * worldRot;
  960. backBottomRight = backBottomRight * worldRot;
  961. backTopLeft = backTopLeft * worldRot;
  962. backTopRight = backTopRight * worldRot;
  963. frontTopLeft += offset;
  964. frontTopRight += offset;
  965. frontBottomLeft += offset;
  966. frontBottomRight += offset;
  967. backBottomLeft += offset;
  968. backBottomRight += offset;
  969. backTopLeft += offset;
  970. backTopRight += offset;
  971. if (frontTopRight.X > maxX)
  972. maxX = frontTopRight.X;
  973. if (frontTopLeft.X > maxX)
  974. maxX = frontTopLeft.X;
  975. if (frontBottomRight.X > maxX)
  976. maxX = frontBottomRight.X;
  977. if (frontBottomLeft.X > maxX)
  978. maxX = frontBottomLeft.X;
  979. if (backTopRight.X > maxX)
  980. maxX = backTopRight.X;
  981. if (backTopLeft.X > maxX)
  982. maxX = backTopLeft.X;
  983. if (backBottomRight.X > maxX)
  984. maxX = backBottomRight.X;
  985. if (backBottomLeft.X > maxX)
  986. maxX = backBottomLeft.X;
  987. if (frontTopRight.X < minX)
  988. minX = frontTopRight.X;
  989. if (frontTopLeft.X < minX)
  990. minX = frontTopLeft.X;
  991. if (frontBottomRight.X < minX)
  992. minX = frontBottomRight.X;
  993. if (frontBottomLeft.X < minX)
  994. minX = frontBottomLeft.X;
  995. if (backTopRight.X < minX)
  996. minX = backTopRight.X;
  997. if (backTopLeft.X < minX)
  998. minX = backTopLeft.X;
  999. if (backBottomRight.X < minX)
  1000. minX = backBottomRight.X;
  1001. if (backBottomLeft.X < minX)
  1002. minX = backBottomLeft.X;
  1003. //
  1004. if (frontTopRight.Y > maxY)
  1005. maxY = frontTopRight.Y;
  1006. if (frontTopLeft.Y > maxY)
  1007. maxY = frontTopLeft.Y;
  1008. if (frontBottomRight.Y > maxY)
  1009. maxY = frontBottomRight.Y;
  1010. if (frontBottomLeft.Y > maxY)
  1011. maxY = frontBottomLeft.Y;
  1012. if (backTopRight.Y > maxY)
  1013. maxY = backTopRight.Y;
  1014. if (backTopLeft.Y > maxY)
  1015. maxY = backTopLeft.Y;
  1016. if (backBottomRight.Y > maxY)
  1017. maxY = backBottomRight.Y;
  1018. if (backBottomLeft.Y > maxY)
  1019. maxY = backBottomLeft.Y;
  1020. if (frontTopRight.Y < minY)
  1021. minY = frontTopRight.Y;
  1022. if (frontTopLeft.Y < minY)
  1023. minY = frontTopLeft.Y;
  1024. if (frontBottomRight.Y < minY)
  1025. minY = frontBottomRight.Y;
  1026. if (frontBottomLeft.Y < minY)
  1027. minY = frontBottomLeft.Y;
  1028. if (backTopRight.Y < minY)
  1029. minY = backTopRight.Y;
  1030. if (backTopLeft.Y < minY)
  1031. minY = backTopLeft.Y;
  1032. if (backBottomRight.Y < minY)
  1033. minY = backBottomRight.Y;
  1034. if (backBottomLeft.Y < minY)
  1035. minY = backBottomLeft.Y;
  1036. //
  1037. if (frontTopRight.Z > maxZ)
  1038. maxZ = frontTopRight.Z;
  1039. if (frontTopLeft.Z > maxZ)
  1040. maxZ = frontTopLeft.Z;
  1041. if (frontBottomRight.Z > maxZ)
  1042. maxZ = frontBottomRight.Z;
  1043. if (frontBottomLeft.Z > maxZ)
  1044. maxZ = frontBottomLeft.Z;
  1045. if (backTopRight.Z > maxZ)
  1046. maxZ = backTopRight.Z;
  1047. if (backTopLeft.Z > maxZ)
  1048. maxZ = backTopLeft.Z;
  1049. if (backBottomRight.Z > maxZ)
  1050. maxZ = backBottomRight.Z;
  1051. if (backBottomLeft.Z > maxZ)
  1052. maxZ = backBottomLeft.Z;
  1053. if (frontTopRight.Z < minZ)
  1054. minZ = frontTopRight.Z;
  1055. if (frontTopLeft.Z < minZ)
  1056. minZ = frontTopLeft.Z;
  1057. if (frontBottomRight.Z < minZ)
  1058. minZ = frontBottomRight.Z;
  1059. if (frontBottomLeft.Z < minZ)
  1060. minZ = frontBottomLeft.Z;
  1061. if (backTopRight.Z < minZ)
  1062. minZ = backTopRight.Z;
  1063. if (backTopLeft.Z < minZ)
  1064. minZ = backTopLeft.Z;
  1065. if (backBottomRight.Z < minZ)
  1066. minZ = backBottomRight.Z;
  1067. if (backBottomLeft.Z < minZ)
  1068. minZ = backBottomLeft.Z;
  1069. }
  1070. }
  1071. public Vector3 GetAxisAlignedBoundingBox(out float offsetHeight)
  1072. {
  1073. float minX;
  1074. float maxX;
  1075. float minY;
  1076. float maxY;
  1077. float minZ;
  1078. float maxZ;
  1079. GetAxisAlignedBoundingBoxRaw(out minX, out maxX, out minY, out maxY, out minZ, out maxZ);
  1080. Vector3 boundingBox = new Vector3(maxX - minX, maxY - minY, maxZ - minZ);
  1081. offsetHeight = 0;
  1082. float lower = (minZ * -1);
  1083. if (lower > maxZ)
  1084. {
  1085. offsetHeight = lower - (boundingBox.Z / 2);
  1086. }
  1087. else if (maxZ > lower)
  1088. {
  1089. offsetHeight = maxZ - (boundingBox.Z / 2);
  1090. offsetHeight *= -1;
  1091. }
  1092. // m_log.InfoFormat("BoundingBox is {0} , {1} , {2} ", boundingBox.X, boundingBox.Y, boundingBox.Z);
  1093. return boundingBox;
  1094. }
  1095. #endregion
  1096. public void SaveScriptedState(XmlTextWriter writer)
  1097. {
  1098. XmlDocument doc = new XmlDocument();
  1099. Dictionary<UUID,string> states = new Dictionary<UUID,string>();
  1100. SceneObjectPart[] parts = m_parts.GetArray();
  1101. for (int i = 0; i < parts.Length; i++)
  1102. {
  1103. Dictionary<UUID, string> pstates = parts[i].Inventory.GetScriptStates();
  1104. foreach (KeyValuePair<UUID, string> kvp in pstates)
  1105. states.Add(kvp.Key, kvp.Value);
  1106. }
  1107. if (states.Count > 0)
  1108. {
  1109. // Now generate the necessary XML wrappings
  1110. writer.WriteStartElement(String.Empty, "GroupScriptStates", String.Empty);
  1111. foreach (UUID itemid in states.Keys)
  1112. {
  1113. doc.LoadXml(states[itemid]);
  1114. writer.WriteStartElement(String.Empty, "SavedScriptState", String.Empty);
  1115. writer.WriteAttributeString(String.Empty, "UUID", String.Empty, itemid.ToString());
  1116. writer.WriteRaw(doc.DocumentElement.OuterXml); // Writes ScriptState element
  1117. writer.WriteEndElement(); // End of SavedScriptState
  1118. }
  1119. writer.WriteEndElement(); // End of GroupScriptStates
  1120. }
  1121. }
  1122. /// <summary>
  1123. ///
  1124. /// </summary>
  1125. /// <param name="part"></param>
  1126. private void SetPartAsNonRoot(SceneObjectPart part)
  1127. {
  1128. part.ParentID = m_rootPart.LocalId;
  1129. part.ClearUndoState();
  1130. }
  1131. public ushort GetTimeDilation()
  1132. {
  1133. return Utils.FloatToUInt16(m_scene.TimeDilation, 0.0f, 1.0f);
  1134. }
  1135. /// <summary>
  1136. /// Set a part to act as the root part for this scene object
  1137. /// </summary>
  1138. /// <param name="part"></param>
  1139. public void SetRootPart(SceneObjectPart part)
  1140. {
  1141. if (part == null)
  1142. throw new ArgumentNullException("Cannot give SceneObjectGroup a null root SceneObjectPart");
  1143. part.SetParent(this);
  1144. m_rootPart = part;
  1145. if (!IsAttachment)
  1146. part.ParentID = 0;
  1147. part.LinkNum = 0;
  1148. m_parts.Add(m_rootPart.UUID, m_rootPart);
  1149. }
  1150. /// <summary>
  1151. /// Add a new part to this scene object. The part must already be correctly configured.
  1152. /// </summary>
  1153. /// <param name="part"></param>
  1154. public void AddPart(SceneObjectPart part)
  1155. {
  1156. part.SetParent(this);
  1157. part.LinkNum = m_parts.Add(part.UUID, part);
  1158. if (part.LinkNum == 2)
  1159. RootPart.LinkNum = 1;
  1160. }
  1161. /// <summary>
  1162. /// Make sure that every non root part has the proper parent root part local id
  1163. /// </summary>
  1164. private void UpdateParentIDs()
  1165. {
  1166. SceneObjectPart[] parts = m_parts.GetArray();
  1167. for (int i = 0; i < parts.Length; i++)
  1168. {
  1169. SceneObjectPart part = parts[i];
  1170. if (part.UUID != m_rootPart.UUID)
  1171. part.ParentID = m_rootPart.LocalId;
  1172. }
  1173. }
  1174. public void RegenerateFullIDs()
  1175. {
  1176. SceneObjectPart[] parts = m_parts.GetArray();
  1177. for (int i = 0; i < parts.Length; i++)
  1178. parts[i].UUID = UUID.Random();
  1179. }
  1180. // justincc: I don't believe this hack is needed any longer, especially since the physics
  1181. // parts of set AbsolutePosition were already commented out. By changing HasGroupChanged to false
  1182. // this method was preventing proper reload of scene objects.
  1183. // dahlia: I had to uncomment it, without it meshing was failing on some prims and objects
  1184. // at region startup
  1185. // teravus: After this was removed from the linking algorithm, Linked prims no longer collided
  1186. // properly when non-physical if they havn't been moved. This breaks ALL builds.
  1187. // see: http://opensimulator.org/mantis/view.php?id=3108
  1188. // Here's the deal, this is ABSOLUTELY CRITICAL so the physics scene gets the update about the
  1189. // position of linkset prims. IF YOU CHANGE THIS, YOU MUST TEST colliding with just linked and
  1190. // unmoved prims! As soon as you move a Prim/group, it will collide properly because Absolute
  1191. // Position has been set!
  1192. public void ResetChildPrimPhysicsPositions()
  1193. {
  1194. // Setting this SOG's absolute position also loops through and sets the positions
  1195. // of the SOP's in this SOG's linkset. This has the side affect of making sure
  1196. // the physics world matches the simulated world.
  1197. AbsolutePosition = AbsolutePosition; // could someone in the know please explain how this works?
  1198. // teravus: AbsolutePosition is NOT a normal property!
  1199. // the code in the getter of AbsolutePosition is significantly different then the code in the setter!
  1200. // jhurliman: Then why is it a property instead of two methods?
  1201. }
  1202. public UUID GetPartsFullID(uint localID)
  1203. {
  1204. SceneObjectPart part = GetPart(localID);
  1205. if (part != null)
  1206. {
  1207. return part.UUID;
  1208. }
  1209. return UUID.Zero;
  1210. }
  1211. public void ObjectGrabHandler(uint localId, Vector3 offsetPos, IClientAPI remoteClient)
  1212. {
  1213. if (m_rootPart.LocalId == localId)
  1214. {
  1215. OnGrabGroup(offsetPos, remoteClient);
  1216. }
  1217. else
  1218. {
  1219. SceneObjectPart part = GetPart(localId);
  1220. OnGrabPart(part, offsetPos, remoteClient);
  1221. }
  1222. }
  1223. public virtual void OnGrabPart(SceneObjectPart part, Vector3 offsetPos, IClientAPI remoteClient)
  1224. {
  1225. // m_log.DebugFormat(
  1226. // "[SCENE OBJECT GROUP]: Processing OnGrabPart for {0} on {1} {2}, offsetPos {3}",
  1227. // remoteClient.Name, part.Name, part.LocalId, offsetPos);
  1228. part.StoreUndoState();
  1229. part.OnGrab(offsetPos, remoteClient);
  1230. }
  1231. public virtual void OnGrabGroup(Vector3 offsetPos, IClientAPI remoteClient)
  1232. {
  1233. m_scene.EventManager.TriggerGroupGrab(UUID, offsetPos, remoteClient.AgentId);
  1234. }
  1235. /// <summary>
  1236. /// Delete this group from its scene.
  1237. /// </summary>
  1238. /// <remarks>
  1239. /// This only handles the in-world consequences of deletion (e.g. any avatars sitting on it are forcibly stood
  1240. /// up and all avatars receive notification of its removal. Removal of the scene object from database backup
  1241. /// must be handled by the caller.
  1242. /// </remarks>
  1243. /// <param name="silent">If true then deletion is not broadcast to clients</param>
  1244. public void DeleteGroupFromScene(bool silent)
  1245. {
  1246. SceneObjectPart[] parts = m_parts.GetArray();
  1247. for (int i = 0; i < parts.Length; i++)
  1248. {
  1249. SceneObjectPart part = parts[i];
  1250. Scene.ForEachScenePresence(sp =>
  1251. {
  1252. if (!sp.IsChildAgent && sp.ParentID == part.LocalId)
  1253. sp.StandUp();
  1254. if (!silent)
  1255. {
  1256. part.ClearUpdateSchedule();
  1257. if (part == m_rootPart)
  1258. {
  1259. if (!IsAttachment
  1260. || AttachedAvatar == sp.UUID
  1261. || !HasPrivateAttachmentPoint)
  1262. sp.ControllingClient.SendKillObject(new List<uint> { part.LocalId });
  1263. }
  1264. }
  1265. });
  1266. }
  1267. }
  1268. public void AddScriptLPS(int count)
  1269. {
  1270. m_scene.SceneGraph.AddToScriptLPS(count);
  1271. }
  1272. public void AddActiveScriptCount(int count)
  1273. {
  1274. SceneGraph d = m_scene.SceneGraph;
  1275. d.AddActiveScripts(count);
  1276. }
  1277. public void aggregateScriptEvents()
  1278. {
  1279. PrimFlags objectflagupdate = (PrimFlags)RootPart.GetEffectiveObjectFlags();
  1280. scriptEvents aggregateScriptEvents = 0;
  1281. SceneObjectPart[] parts = m_parts.GetArray();
  1282. for (int i = 0; i < parts.Length; i++)
  1283. {
  1284. SceneObjectPart part = parts[i];
  1285. if (part == null)
  1286. continue;
  1287. if (part != RootPart)
  1288. part.Flags = objectflagupdate;
  1289. aggregateScriptEvents |= part.AggregateScriptEvents;
  1290. }
  1291. m_scriptListens_atTarget = ((aggregateScriptEvents & scriptEvents.at_target) != 0);
  1292. m_scriptListens_notAtTarget = ((aggregateScriptEvents & scriptEvents.not_at_target) != 0);
  1293. if (!m_scriptListens_atTarget && !m_scriptListens_notAtTarget)
  1294. {
  1295. lock (m_targets)
  1296. m_targets.Clear();
  1297. m_scene.RemoveGroupTarget(this);
  1298. }
  1299. m_scriptListens_atRotTarget = ((aggregateScriptEvents & scriptEvents.at_rot_target) != 0);
  1300. m_scriptListens_notAtRotTarget = ((aggregateScriptEvents & scriptEvents.not_at_rot_target) != 0);
  1301. if (!m_scriptListens_atRotTarget && !m_scriptListens_notAtRotTarget)
  1302. {
  1303. lock (m_rotTargets)
  1304. m_rotTargets.Clear();
  1305. m_scene.RemoveGroupTarget(this);
  1306. }
  1307. ScheduleGroupForFullUpdate();
  1308. }
  1309. public void SetText(string text, Vector3 color, double alpha)
  1310. {
  1311. Color = Color.FromArgb(0xff - (int) (alpha * 0xff),
  1312. (int) (color.X * 0xff),
  1313. (int) (color.Y * 0xff),
  1314. (int) (color.Z * 0xff));
  1315. Text = text;
  1316. HasGroupChanged = true;
  1317. m_rootPart.ScheduleFullUpdate();
  1318. }
  1319. /// <summary>
  1320. /// Apply physics to this group
  1321. /// </summary>
  1322. public void ApplyPhysics()
  1323. {
  1324. // Apply physics to the root prim
  1325. m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_rootPart.VolumeDetectActive);
  1326. // Apply physics to child prims
  1327. SceneObjectPart[] parts = m_parts.GetArray();
  1328. if (parts.Length > 1)
  1329. {
  1330. for (int i = 0; i < parts.Length; i++)
  1331. {
  1332. SceneObjectPart part = parts[i];
  1333. if (part.LocalId != m_rootPart.LocalId)
  1334. part.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), part.VolumeDetectActive);
  1335. }
  1336. // Hack to get the physics scene geometries in the right spot
  1337. ResetChildPrimPhysicsPositions();
  1338. }
  1339. }
  1340. public void SetOwnerId(UUID userId)
  1341. {
  1342. ForEachPart(delegate(SceneObjectPart part) { part.OwnerID = userId; });
  1343. }
  1344. public void ForEachPart(Action<SceneObjectPart> whatToDo)
  1345. {
  1346. SceneObjectPart[] parts = m_parts.GetArray();
  1347. for (int i = 0; i < parts.Length; i++)
  1348. whatToDo(parts[i]);
  1349. }
  1350. #region Events
  1351. /// <summary>
  1352. /// Processes backup.
  1353. /// </summary>
  1354. /// <param name="datastore"></param>
  1355. public virtual void ProcessBackup(ISimulationDataService datastore, bool forcedBackup)
  1356. {
  1357. if (!Backup)
  1358. {
  1359. // m_log.DebugFormat(
  1360. // "[WATER WARS]: Ignoring backup of {0} {1} since object is not marked to be backed up", Name, UUID);
  1361. return;
  1362. }
  1363. if (IsDeleted || UUID == UUID.Zero)
  1364. {
  1365. // m_log.DebugFormat(
  1366. // "[WATER WARS]: Ignoring backup of {0} {1} since object is marked as already deleted", Name, UUID);
  1367. return;
  1368. }
  1369. // Since this is the top of the section of call stack for backing up a particular scene object, don't let
  1370. // any exception propogate upwards.
  1371. try
  1372. {
  1373. if (!m_scene.ShuttingDown) // if shutting down then there will be nothing to handle the return so leave till next restart
  1374. {
  1375. ILandObject parcel = m_scene.LandChannel.GetLandObject(
  1376. m_rootPart.GroupPosition.X, m_rootPart.GroupPosition.Y);
  1377. if (parcel != null && parcel.LandData != null &&
  1378. parcel.LandData.OtherCleanTime != 0)
  1379. {
  1380. if (parcel.LandData.OwnerID != OwnerID &&
  1381. (parcel.LandData.GroupID != GroupID ||
  1382. parcel.LandData.GroupID == UUID.Zero))
  1383. {
  1384. if ((DateTime.UtcNow - RootPart.Rezzed).TotalMinutes >
  1385. parcel.LandData.OtherCleanTime)
  1386. {
  1387. DetachFromBackup();
  1388. m_log.DebugFormat(
  1389. "[SCENE OBJECT GROUP]: Returning object {0} due to parcel autoreturn",
  1390. RootPart.UUID);
  1391. m_scene.AddReturn(OwnerID == GroupID ? LastOwnerID : OwnerID, Name, AbsolutePosition, "parcel autoreturn");
  1392. m_scene.DeRezObjects(null, new List<uint>() { RootPart.LocalId }, UUID.Zero,
  1393. DeRezAction.Return, UUID.Zero);
  1394. return;
  1395. }
  1396. }
  1397. }
  1398. }
  1399. if (m_scene.UseBackup && HasGroupChanged)
  1400. {
  1401. // don't backup while it's selected or you're asking for changes mid stream.
  1402. if (isTimeToPersist() || forcedBackup)
  1403. {
  1404. // m_log.DebugFormat(
  1405. // "[SCENE]: Storing {0}, {1} in {2}",
  1406. // Name, UUID, m_scene.RegionInfo.RegionName);
  1407. SceneObjectGroup backup_group = Copy(false);
  1408. backup_group.RootPart.Velocity = RootPart.Velocity;
  1409. backup_group.RootPart.Acceleration = RootPart.Acceleration;
  1410. backup_group.RootPart.AngularVelocity = RootPart.AngularVelocity;
  1411. backup_group.RootPart.ParticleSystem = RootPart.ParticleSystem;
  1412. HasGroupChanged = false;
  1413. GroupContainsForeignPrims = false;
  1414. m_scene.EventManager.TriggerOnSceneObjectPreSave(backup_group, this);
  1415. datastore.StoreObject(backup_group, m_scene.RegionInfo.RegionID);
  1416. backup_group.ForEachPart(delegate(SceneObjectPart part)
  1417. {
  1418. part.Inventory.ProcessInventoryBackup(datastore);
  1419. });
  1420. backup_group = null;
  1421. }
  1422. // else
  1423. // {
  1424. // m_log.DebugFormat(
  1425. // "[SCENE]: Did not update persistence of object {0} {1}, selected = {2}",
  1426. // Name, UUID, IsSelected);
  1427. // }
  1428. }
  1429. }
  1430. catch (Exception e)
  1431. {
  1432. m_log.ErrorFormat(
  1433. "[SCENE]: Storing of {0}, {1} in {2} failed with exception {3}{4}",
  1434. Name, UUID, m_scene.RegionInfo.RegionName, e.Message, e.StackTrace);
  1435. }
  1436. }
  1437. #endregion
  1438. /// <summary>
  1439. /// Send the parts of this SOG to a single client
  1440. /// </summary>
  1441. /// <remarks>
  1442. /// Used when the client initially connects and when client sends RequestPrim packet
  1443. /// </remarks>
  1444. /// <param name="remoteClient"></param>
  1445. public void SendFullUpdateToClient(IClientAPI remoteClient)
  1446. {
  1447. RootPart.SendFullUpdate(remoteClient);
  1448. SceneObjectPart[] parts = m_parts.GetArray();
  1449. for (int i = 0; i < parts.Length; i++)
  1450. {
  1451. SceneObjectPart part = parts[i];
  1452. if (part != RootPart)
  1453. part.SendFullUpdate(remoteClient);
  1454. }
  1455. }
  1456. #region Copying
  1457. /// <summary>
  1458. /// Duplicates this object, including operations such as physics set up and attaching to the backup event.
  1459. /// </summary>
  1460. /// <param name="userExposed">True if the duplicate will immediately be in the scene, false otherwise</param>
  1461. /// <returns></returns>
  1462. public SceneObjectGroup Copy(bool userExposed)
  1463. {
  1464. // FIXME: This is dangerous since it's easy to forget to reset some references when necessary and end up
  1465. // with bugs that only occur in some circumstances (e.g. crossing between regions on the same simulator
  1466. // but not between regions on different simulators). Really, all copying should be done explicitly.
  1467. SceneObjectGroup dupe = (SceneObjectGroup)MemberwiseClone();
  1468. dupe.Backup = false;
  1469. dupe.m_parts = new MapAndArray<OpenMetaverse.UUID, SceneObjectPart>();
  1470. dupe.m_sittingAvatars = new List<ScenePresence>();
  1471. dupe.CopyRootPart(m_rootPart, OwnerID, GroupID, userExposed);
  1472. dupe.m_rootPart.LinkNum = m_rootPart.LinkNum;
  1473. if (userExposed)
  1474. dupe.m_rootPart.TrimPermissions();
  1475. List<SceneObjectPart> partList = new List<SceneObjectPart>(m_parts.GetArray());
  1476. partList.Sort(delegate(SceneObjectPart p1, SceneObjectPart p2)
  1477. {
  1478. return p1.LinkNum.CompareTo(p2.LinkNum);
  1479. }
  1480. );
  1481. foreach (SceneObjectPart part in partList)
  1482. {
  1483. SceneObjectPart newPart;
  1484. if (part.UUID != m_rootPart.UUID)
  1485. {
  1486. newPart = dupe.CopyPart(part, OwnerID, GroupID, userExposed);
  1487. newPart.LinkNum = part.LinkNum;
  1488. }
  1489. else
  1490. {
  1491. newPart = dupe.m_rootPart;
  1492. }
  1493. // Need to duplicate the physics actor as well
  1494. PhysicsActor originalPartPa = part.PhysActor;
  1495. if (originalPartPa != null && userExposed)
  1496. {
  1497. PrimitiveBaseShape pbs = newPart.Shape;
  1498. newPart.PhysActor
  1499. = m_scene.PhysicsScene.AddPrimShape(
  1500. string.Format("{0}/{1}", newPart.Name, newPart.UUID),
  1501. pbs,
  1502. newPart.AbsolutePosition,
  1503. newPart.Scale,
  1504. newPart.RotationOffset,
  1505. originalPartPa.IsPhysical,
  1506. newPart.LocalId);
  1507. newPart.DoPhysicsPropertyUpdate(originalPartPa.IsPhysical, true);
  1508. }
  1509. if (part.KeyframeMotion != null)
  1510. newPart.KeyframeMotion = part.KeyframeMotion.Copy(dupe);
  1511. }
  1512. if (userExposed)
  1513. {
  1514. dupe.UpdateParentIDs();
  1515. dupe.HasGroupChanged = true;
  1516. dupe.AttachToBackup();
  1517. ScheduleGroupForFullUpdate();
  1518. }
  1519. return dupe;
  1520. }
  1521. /// <summary>
  1522. /// Copy the given part as the root part of this scene object.
  1523. /// </summary>
  1524. /// <param name="part"></param>
  1525. /// <param name="cAgentID"></param>
  1526. /// <param name="cGroupID"></param>
  1527. public void CopyRootPart(SceneObjectPart part, UUID cAgentID, UUID cGroupID, bool userExposed)
  1528. {
  1529. SetRootPart(part.Copy(m_scene.AllocateLocalId(), OwnerID, GroupID, 0, userExposed));
  1530. }
  1531. public void ScriptSetPhysicsStatus(bool usePhysics)
  1532. {
  1533. if (usePhysics)
  1534. {
  1535. if (RootPart.KeyframeMotion != null)
  1536. RootPart.KeyframeMotion.Stop();
  1537. RootPart.KeyframeMotion = null;
  1538. }
  1539. UpdatePrimFlags(RootPart.LocalId, usePhysics, IsTemporary, IsPhantom, IsVolumeDetect);
  1540. }
  1541. public void ScriptSetTemporaryStatus(bool makeTemporary)
  1542. {
  1543. UpdatePrimFlags(RootPart.LocalId, UsesPhysics, makeTemporary, IsPhantom, IsVolumeDetect);
  1544. }
  1545. public void ScriptSetPhantomStatus(bool makePhantom)
  1546. {
  1547. UpdatePrimFlags(RootPart.LocalId, UsesPhysics, IsTemporary, makePhantom, IsVolumeDetect);
  1548. }
  1549. public void ScriptSetVolumeDetect(bool makeVolumeDetect)
  1550. {
  1551. UpdatePrimFlags(RootPart.LocalId, UsesPhysics, IsTemporary, IsPhantom, makeVolumeDetect);
  1552. /*
  1553. ScriptSetPhantomStatus(false); // What ever it was before, now it's not phantom anymore
  1554. if (PhysActor != null) // Should always be the case now
  1555. {
  1556. PhysActor.SetVolumeDetect(param);
  1557. }
  1558. if (param != 0)
  1559. AddFlag(PrimFlags.Phantom);
  1560. ScheduleFullUpdate();
  1561. */
  1562. }
  1563. public void applyImpulse(Vector3 impulse)
  1564. {
  1565. if (IsAttachment)
  1566. {
  1567. ScenePresence avatar = m_scene.GetScenePresence(AttachedAvatar);
  1568. if (avatar != null)
  1569. {
  1570. avatar.PushForce(impulse);
  1571. }
  1572. }
  1573. else
  1574. {
  1575. PhysicsActor pa = RootPart.PhysActor;
  1576. if (pa != null)
  1577. {
  1578. pa.AddForce(impulse, true);
  1579. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  1580. }
  1581. }
  1582. }
  1583. public void applyAngularImpulse(Vector3 impulse)
  1584. {
  1585. PhysicsActor pa = RootPart.PhysActor;
  1586. if (pa != null)
  1587. {
  1588. if (!IsAttachment)
  1589. {
  1590. pa.AddAngularForce(impulse, true);
  1591. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  1592. }
  1593. }
  1594. }
  1595. public void setAngularImpulse(Vector3 impulse)
  1596. {
  1597. PhysicsActor pa = RootPart.PhysActor;
  1598. if (pa != null)
  1599. {
  1600. if (!IsAttachment)
  1601. {
  1602. pa.Torque = impulse;
  1603. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  1604. }
  1605. }
  1606. }
  1607. public Vector3 GetTorque()
  1608. {
  1609. PhysicsActor pa = RootPart.PhysActor;
  1610. if (pa != null)
  1611. {
  1612. if (!IsAttachment)
  1613. {
  1614. Vector3 torque = pa.Torque;
  1615. return torque;
  1616. }
  1617. }
  1618. return Vector3.Zero;
  1619. }
  1620. public void moveToTarget(Vector3 target, float tau)
  1621. {
  1622. if (IsAttachment)
  1623. {
  1624. ScenePresence avatar = m_scene.GetScenePresence(AttachedAvatar);
  1625. if (avatar != null)
  1626. {
  1627. avatar.MoveToTarget(target, false, false);
  1628. }
  1629. }
  1630. else
  1631. {
  1632. PhysicsActor pa = RootPart.PhysActor;
  1633. if (pa != null)
  1634. {
  1635. pa.PIDTarget = target;
  1636. pa.PIDTau = tau;
  1637. pa.PIDActive = true;
  1638. }
  1639. }
  1640. }
  1641. public void stopMoveToTarget()
  1642. {
  1643. PhysicsActor pa = RootPart.PhysActor;
  1644. if (pa != null)
  1645. pa.PIDActive = false;
  1646. }
  1647. /// <summary>
  1648. /// Uses a PID to attempt to clamp the object on the Z axis at the given height over tau seconds.
  1649. /// </summary>
  1650. /// <param name="height">Height to hover. Height of zero disables hover.</param>
  1651. /// <param name="hoverType">Determines what the height is relative to </param>
  1652. /// <param name="tau">Number of seconds over which to reach target</param>
  1653. public void SetHoverHeight(float height, PIDHoverType hoverType, float tau)
  1654. {
  1655. PhysicsActor pa = RootPart.PhysActor;
  1656. if (pa != null)
  1657. {
  1658. if (height != 0f)
  1659. {
  1660. pa.PIDHoverHeight = height;
  1661. pa.PIDHoverType = hoverType;
  1662. pa.PIDTau = tau;
  1663. pa.PIDHoverActive = true;
  1664. }
  1665. else
  1666. {
  1667. pa.PIDHoverActive = false;
  1668. }
  1669. }
  1670. }
  1671. /// <summary>
  1672. /// Set the owner of the root part.
  1673. /// </summary>
  1674. /// <param name="part"></param>
  1675. /// <param name="cAgentID"></param>
  1676. /// <param name="cGroupID"></param>
  1677. public void SetRootPartOwner(SceneObjectPart part, UUID cAgentID, UUID cGroupID)
  1678. {
  1679. part.LastOwnerID = part.OwnerID;
  1680. part.OwnerID = cAgentID;
  1681. part.GroupID = cGroupID;
  1682. if (part.OwnerID != cAgentID)
  1683. {
  1684. // Apply Next Owner Permissions if we're not bypassing permissions
  1685. if (!m_scene.Permissions.BypassPermissions())
  1686. ApplyNextOwnerPermissions();
  1687. }
  1688. part.ScheduleFullUpdate();
  1689. }
  1690. /// <summary>
  1691. /// Make a copy of the given part.
  1692. /// </summary>
  1693. /// <param name="part"></param>
  1694. /// <param name="cAgentID"></param>
  1695. /// <param name="cGroupID"></param>
  1696. public SceneObjectPart CopyPart(SceneObjectPart part, UUID cAgentID, UUID cGroupID, bool userExposed)
  1697. {
  1698. SceneObjectPart newPart = part.Copy(m_scene.AllocateLocalId(), OwnerID, GroupID, m_parts.Count, userExposed);
  1699. AddPart(newPart);
  1700. SetPartAsNonRoot(newPart);
  1701. return newPart;
  1702. }
  1703. /// <summary>
  1704. /// Reset the UUIDs for all the prims that make up this group.
  1705. /// </summary>
  1706. /// <remarks>
  1707. /// This is called by methods which want to add a new group to an existing scene, in order
  1708. /// to ensure that there are no clashes with groups already present.
  1709. /// </remarks>
  1710. public void ResetIDs()
  1711. {
  1712. lock (m_parts.SyncRoot)
  1713. {
  1714. List<SceneObjectPart> partsList = new List<SceneObjectPart>(m_parts.GetArray());
  1715. m_parts.Clear();
  1716. foreach (SceneObjectPart part in partsList)
  1717. {
  1718. part.ResetIDs(part.LinkNum); // Don't change link nums
  1719. m_parts.Add(part.UUID, part);
  1720. }
  1721. }
  1722. }
  1723. /// <summary>
  1724. ///
  1725. /// </summary>
  1726. /// <param name="part"></param>
  1727. public void ServiceObjectPropertiesFamilyRequest(IClientAPI remoteClient, UUID AgentID, uint RequestFlags)
  1728. {
  1729. remoteClient.SendObjectPropertiesFamilyData(RootPart, RequestFlags);
  1730. // remoteClient.SendObjectPropertiesFamilyData(RequestFlags, RootPart.UUID, RootPart.OwnerID, RootPart.GroupID, RootPart.BaseMask,
  1731. // RootPart.OwnerMask, RootPart.GroupMask, RootPart.EveryoneMask, RootPart.NextOwnerMask,
  1732. // RootPart.OwnershipCost, RootPart.ObjectSaleType, RootPart.SalePrice, RootPart.Category,
  1733. // RootPart.CreatorID, RootPart.Name, RootPart.Description);
  1734. }
  1735. public void SetPartOwner(SceneObjectPart part, UUID cAgentID, UUID cGroupID)
  1736. {
  1737. part.OwnerID = cAgentID;
  1738. part.GroupID = cGroupID;
  1739. }
  1740. #endregion
  1741. public override void Update()
  1742. {
  1743. // Check that the group was not deleted before the scheduled update
  1744. // FIXME: This is merely a temporary measure to reduce the incidence of failure when
  1745. // an object has been deleted from a scene before update was processed.
  1746. // A more fundamental overhaul of the update mechanism is required to eliminate all
  1747. // the race conditions.
  1748. if (IsDeleted)
  1749. return;
  1750. // Even temporary objects take part in physics (e.g. temp-on-rez bullets)
  1751. //if ((RootPart.Flags & PrimFlags.TemporaryOnRez) != 0)
  1752. // return;
  1753. // If we somehow got here to updating the SOG and its root part is not scheduled for update,
  1754. // check to see if the physical position or rotation warrant an update.
  1755. if (m_rootPart.UpdateFlag == UpdateRequired.NONE)
  1756. {
  1757. bool UsePhysics = ((RootPart.Flags & PrimFlags.Physics) != 0);
  1758. if (UsePhysics && !AbsolutePosition.ApproxEquals(lastPhysGroupPos, 0.02f))
  1759. {
  1760. m_rootPart.UpdateFlag = UpdateRequired.TERSE;
  1761. lastPhysGroupPos = AbsolutePosition;
  1762. }
  1763. if (UsePhysics && !GroupRotation.ApproxEquals(lastPhysGroupRot, 0.1f))
  1764. {
  1765. m_rootPart.UpdateFlag = UpdateRequired.TERSE;
  1766. lastPhysGroupRot = GroupRotation;
  1767. }
  1768. }
  1769. SceneObjectPart[] parts = m_parts.GetArray();
  1770. for (int i = 0; i < parts.Length; i++)
  1771. {
  1772. SceneObjectPart part = parts[i];
  1773. if (!IsSelected)
  1774. part.UpdateLookAt();
  1775. part.SendScheduledUpdates();
  1776. }
  1777. }
  1778. /// <summary>
  1779. /// Schedule a full update for this scene object to all interested viewers.
  1780. /// </summary>
  1781. /// <remarks>
  1782. /// Ultimately, this should be managed such that region modules can invoke it at the end of a set of operations
  1783. /// so that either all changes are sent at once. However, currently, a large amount of internal
  1784. /// code will set this anyway when some object properties are changed.
  1785. /// </remarks>
  1786. public void ScheduleGroupForFullUpdate()
  1787. {
  1788. // if (IsAttachment)
  1789. // m_log.DebugFormat("[SOG]: Scheduling full update for {0} {1}", Name, LocalId);
  1790. checkAtTargets();
  1791. RootPart.ScheduleFullUpdate();
  1792. SceneObjectPart[] parts = m_parts.GetArray();
  1793. for (int i = 0; i < parts.Length; i++)
  1794. {
  1795. SceneObjectPart part = parts[i];
  1796. if (part != RootPart)
  1797. part.ScheduleFullUpdate();
  1798. }
  1799. }
  1800. /// <summary>
  1801. /// Schedule a terse update for this scene object to all interested viewers.
  1802. /// </summary>
  1803. /// <remarks>
  1804. /// Ultimately, this should be managed such that region modules can invoke it at the end of a set of operations
  1805. /// so that either all changes are sent at once. However, currently, a large amount of internal
  1806. /// code will set this anyway when some object properties are changed.
  1807. /// </remarks>
  1808. public void ScheduleGroupForTerseUpdate()
  1809. {
  1810. // m_log.DebugFormat("[SOG]: Scheduling terse update for {0} {1}", Name, UUID);
  1811. SceneObjectPart[] parts = m_parts.GetArray();
  1812. for (int i = 0; i < parts.Length; i++)
  1813. parts[i].ScheduleTerseUpdate();
  1814. }
  1815. /// <summary>
  1816. /// Immediately send a full update for this scene object.
  1817. /// </summary>
  1818. public void SendGroupFullUpdate()
  1819. {
  1820. if (IsDeleted)
  1821. return;
  1822. // m_log.DebugFormat("[SOG]: Sending immediate full group update for {0} {1}", Name, UUID);
  1823. RootPart.SendFullUpdateToAllClients();
  1824. SceneObjectPart[] parts = m_parts.GetArray();
  1825. for (int i = 0; i < parts.Length; i++)
  1826. {
  1827. SceneObjectPart part = parts[i];
  1828. if (part != RootPart)
  1829. part.SendFullUpdateToAllClients();
  1830. }
  1831. }
  1832. /// <summary>
  1833. /// Immediately send an update for this scene object's root prim only.
  1834. /// This is for updates regarding the object as a whole, and none of its parts in particular.
  1835. /// Note: this may not be used by opensim (it probably should) but it's used by
  1836. /// external modules.
  1837. /// </summary>
  1838. public void SendGroupRootTerseUpdate()
  1839. {
  1840. if (IsDeleted)
  1841. return;
  1842. RootPart.SendTerseUpdateToAllClients();
  1843. }
  1844. public void QueueForUpdateCheck()
  1845. {
  1846. if (m_scene == null) // Need to check here as it's null during object creation
  1847. return;
  1848. m_scene.SceneGraph.AddToUpdateList(this);
  1849. }
  1850. /// <summary>
  1851. /// Immediately send a terse update for this scene object.
  1852. /// </summary>
  1853. public void SendGroupTerseUpdate()
  1854. {
  1855. if (IsDeleted)
  1856. return;
  1857. SceneObjectPart[] parts = m_parts.GetArray();
  1858. for (int i = 0; i < parts.Length; i++)
  1859. parts[i].SendTerseUpdateToAllClients();
  1860. }
  1861. /// <summary>
  1862. /// Send metadata about the root prim (name, description, sale price, permissions, etc.) to a client.
  1863. /// </summary>
  1864. /// <param name="client"></param>
  1865. public void SendPropertiesToClient(IClientAPI client)
  1866. {
  1867. m_rootPart.SendPropertiesToClient(client);
  1868. }
  1869. #region SceneGroupPart Methods
  1870. /// <summary>
  1871. /// Get the child part by LinkNum
  1872. /// </summary>
  1873. /// <param name="linknum"></param>
  1874. /// <returns>null if no child part with that linknum or child part</returns>
  1875. public SceneObjectPart GetLinkNumPart(int linknum)
  1876. {
  1877. SceneObjectPart[] parts = m_parts.GetArray();
  1878. for (int i = 0; i < parts.Length; i++)
  1879. {
  1880. if (parts[i].LinkNum == linknum)
  1881. return parts[i];
  1882. }
  1883. return null;
  1884. }
  1885. /// <summary>
  1886. /// Get a part with a given UUID
  1887. /// </summary>
  1888. /// <param name="primID"></param>
  1889. /// <returns>null if a part with the primID was not found</returns>
  1890. public SceneObjectPart GetPart(UUID primID)
  1891. {
  1892. SceneObjectPart childPart;
  1893. m_parts.TryGetValue(primID, out childPart);
  1894. return childPart;
  1895. }
  1896. /// <summary>
  1897. /// Get a part with a given local ID
  1898. /// </summary>
  1899. /// <param name="localID"></param>
  1900. /// <returns>null if a part with the local ID was not found</returns>
  1901. public SceneObjectPart GetPart(uint localID)
  1902. {
  1903. SceneObjectPart[] parts = m_parts.GetArray();
  1904. for (int i = 0; i < parts.Length; i++)
  1905. {
  1906. if (parts[i].LocalId == localID)
  1907. return parts[i];
  1908. }
  1909. return null;
  1910. }
  1911. #endregion
  1912. #region Packet Handlers
  1913. /// <summary>
  1914. /// Link the prims in a given group to this group
  1915. /// </summary>
  1916. /// <remarks>
  1917. /// Do not call this method directly - use Scene.LinkObjects() instead to avoid races between threads.
  1918. /// FIXME: There are places where scripts call these methods directly without locking. This is a potential race condition.
  1919. /// </remarks>
  1920. /// <param name="objectGroup">The group of prims which should be linked to this group</param>
  1921. public void LinkToGroup(SceneObjectGroup objectGroup)
  1922. {
  1923. LinkToGroup(objectGroup, false);
  1924. }
  1925. // Link an existing group to this group.
  1926. // The group being linked need not be a linkset -- it can have just one prim.
  1927. public void LinkToGroup(SceneObjectGroup objectGroup, bool insert)
  1928. {
  1929. // m_log.DebugFormat(
  1930. // "[SCENE OBJECT GROUP]: Linking group with root part {0}, {1} to group with root part {2}, {3}",
  1931. // objectGroup.RootPart.Name, objectGroup.RootPart.UUID, RootPart.Name, RootPart.UUID);
  1932. // Linking to ourselves is not a valid operation.
  1933. if (objectGroup == this)
  1934. return;
  1935. // If the configured linkset capacity is greater than zero,
  1936. // and the new linkset would have a prim count higher than this
  1937. // value, do not link it.
  1938. if (m_scene.m_linksetCapacity > 0 &&
  1939. (PrimCount + objectGroup.PrimCount) >
  1940. m_scene.m_linksetCapacity)
  1941. {
  1942. m_log.DebugFormat(
  1943. "[SCENE OBJECT GROUP]: Cannot link group with root" +
  1944. " part {0}, {1} ({2} prims) to group with root part" +
  1945. " {3}, {4} ({5} prims) because the new linkset" +
  1946. " would exceed the configured maximum of {6}",
  1947. objectGroup.RootPart.Name, objectGroup.RootPart.UUID,
  1948. objectGroup.PrimCount, RootPart.Name, RootPart.UUID,
  1949. PrimCount, m_scene.m_linksetCapacity);
  1950. return;
  1951. }
  1952. // 'linkPart' == the root of the group being linked into this group
  1953. SceneObjectPart linkPart = objectGroup.m_rootPart;
  1954. // physics flags from group to be applied to linked parts
  1955. bool grpusephys = UsesPhysics;
  1956. bool grptemporary = IsTemporary;
  1957. // Remember where the group being linked thought it was
  1958. Vector3 oldGroupPosition = linkPart.GroupPosition;
  1959. Quaternion oldRootRotation = linkPart.RotationOffset;
  1960. // A linked SOP remembers its location and rotation relative to the root of a group.
  1961. // Convert the root of the group being linked to be relative to the
  1962. // root of the group being linked to.
  1963. // Note: Some of the assignments have complex side effects.
  1964. // First move the new group's root SOP's position to be relative to ours
  1965. // (radams1: Not sure if the multiple setting of OffsetPosition is required. If not,
  1966. // this code can be reordered to have a more logical flow.)
  1967. linkPart.OffsetPosition = linkPart.GroupPosition - AbsolutePosition;
  1968. // Assign the new parent to the root of the old group
  1969. linkPart.ParentID = m_rootPart.LocalId;
  1970. // Now that it's a child, it's group position is our root position
  1971. linkPart.GroupPosition = AbsolutePosition;
  1972. Vector3 axPos = linkPart.OffsetPosition;
  1973. // Rotate the linking root SOP's position to be relative to the new root prim
  1974. Quaternion parentRot = m_rootPart.RotationOffset;
  1975. axPos *= Quaternion.Inverse(parentRot);
  1976. linkPart.OffsetPosition = axPos;
  1977. // Make the linking root SOP's rotation relative to the new root prim
  1978. Quaternion oldRot = linkPart.RotationOffset;
  1979. Quaternion newRot = Quaternion.Inverse(parentRot) * oldRot;
  1980. linkPart.RotationOffset = newRot;
  1981. // If there is only one SOP in a SOG, the LinkNum is zero. I.e., not a linkset.
  1982. // Now that we know this SOG has at least two SOPs in it, the new root
  1983. // SOP becomes the first in the linkset.
  1984. if (m_rootPart.LinkNum == 0)
  1985. m_rootPart.LinkNum = 1;
  1986. lock (m_parts.SyncRoot)
  1987. {
  1988. // Calculate the new link number for the old root SOP
  1989. int linkNum;
  1990. if (insert)
  1991. {
  1992. linkNum = 2;
  1993. foreach (SceneObjectPart part in Parts)
  1994. {
  1995. if (part.LinkNum > 1)
  1996. part.LinkNum++;
  1997. }
  1998. }
  1999. else
  2000. {
  2001. linkNum = PrimCount + 1;
  2002. }
  2003. // Add the old root SOP as a part in our group's list
  2004. m_parts.Add(linkPart.UUID, linkPart);
  2005. linkPart.SetParent(this);
  2006. linkPart.CreateSelected = true;
  2007. // let physics know preserve part volume dtc messy since UpdatePrimFlags doesn't look to parent changes for now
  2008. linkPart.UpdatePrimFlags(grpusephys, grptemporary, (IsPhantom || (linkPart.Flags & PrimFlags.Phantom) != 0), linkPart.VolumeDetectActive);
  2009. // If the added SOP is physical, also tell the physics engine about the link relationship.
  2010. if (linkPart.PhysActor != null && m_rootPart.PhysActor != null && m_rootPart.PhysActor.IsPhysical)
  2011. {
  2012. linkPart.PhysActor.link(m_rootPart.PhysActor);
  2013. this.Scene.PhysicsScene.AddPhysicsActorTaint(linkPart.PhysActor);
  2014. }
  2015. linkPart.LinkNum = linkNum++;
  2016. // Get a list of the SOP's in the old group in order of their linknum's.
  2017. SceneObjectPart[] ogParts = objectGroup.Parts;
  2018. Array.Sort(ogParts, delegate(SceneObjectPart a, SceneObjectPart b)
  2019. {
  2020. return a.LinkNum - b.LinkNum;
  2021. });
  2022. // Add each of the SOP's from the old linkset to our linkset
  2023. for (int i = 0; i < ogParts.Length; i++)
  2024. {
  2025. SceneObjectPart part = ogParts[i];
  2026. if (part.UUID != objectGroup.m_rootPart.UUID)
  2027. {
  2028. LinkNonRootPart(part, oldGroupPosition, oldRootRotation, linkNum++);
  2029. // Update the physics flags for the newly added SOP
  2030. // (Is this necessary? LinkNonRootPart() has already called UpdatePrimFlags but with different flags!??)
  2031. part.UpdatePrimFlags(grpusephys, grptemporary, (IsPhantom || (part.Flags & PrimFlags.Phantom) != 0), part.VolumeDetectActive);
  2032. // If the added SOP is physical, also tell the physics engine about the link relationship.
  2033. if (part.PhysActor != null && m_rootPart.PhysActor != null && m_rootPart.PhysActor.IsPhysical)
  2034. {
  2035. part.PhysActor.link(m_rootPart.PhysActor);
  2036. this.Scene.PhysicsScene.AddPhysicsActorTaint(part.PhysActor);
  2037. }
  2038. }
  2039. part.ClearUndoState();
  2040. }
  2041. }
  2042. // Now that we've aquired all of the old SOG's parts, remove the old SOG from the scene.
  2043. m_scene.UnlinkSceneObject(objectGroup, true);
  2044. objectGroup.IsDeleted = true;
  2045. objectGroup.m_parts.Clear();
  2046. // Can't do this yet since backup still makes use of the root part without any synchronization
  2047. // objectGroup.m_rootPart = null;
  2048. // If linking prims with different permissions, fix them
  2049. AdjustChildPrimPermissions(false);
  2050. GroupContainsForeignPrims = true;
  2051. AttachToBackup();
  2052. // Here's the deal, this is ABSOLUTELY CRITICAL so the physics scene gets the update about the
  2053. // position of linkset prims. IF YOU CHANGE THIS, YOU MUST TEST colliding with just linked and
  2054. // unmoved prims!
  2055. ResetChildPrimPhysicsPositions();
  2056. //HasGroupChanged = true;
  2057. //ScheduleGroupForFullUpdate();
  2058. }
  2059. /// <summary>
  2060. /// Delink the given prim from this group. The delinked prim is established as
  2061. /// an independent SceneObjectGroup.
  2062. /// </summary>
  2063. /// <remarks>
  2064. /// FIXME: This method should not be called directly since it bypasses update locking, allowing a potential race
  2065. /// condition. But currently there is no
  2066. /// alternative method that does take a lonk to delink a single prim.
  2067. /// </remarks>
  2068. /// <param name="partID"></param>
  2069. /// <returns>The object group of the newly delinked prim. Null if part could not be found</returns>
  2070. public SceneObjectGroup DelinkFromGroup(uint partID)
  2071. {
  2072. return DelinkFromGroup(partID, true);
  2073. }
  2074. /// <summary>
  2075. /// Delink the given prim from this group. The delinked prim is established as
  2076. /// an independent SceneObjectGroup.
  2077. /// </summary>
  2078. /// <remarks>
  2079. /// FIXME: This method should not be called directly since it bypasses update locking, allowing a potential race
  2080. /// condition. But currently there is no
  2081. /// alternative method that does take a lonk to delink a single prim.
  2082. /// </remarks>
  2083. /// <param name="partID"></param>
  2084. /// <param name="sendEvents"></param>
  2085. /// <returns>The object group of the newly delinked prim. Null if part could not be found</returns>
  2086. public SceneObjectGroup DelinkFromGroup(uint partID, bool sendEvents)
  2087. {
  2088. SceneObjectPart linkPart = GetPart(partID);
  2089. if (linkPart != null)
  2090. {
  2091. return DelinkFromGroup(linkPart, sendEvents);
  2092. }
  2093. else
  2094. {
  2095. m_log.WarnFormat("[SCENE OBJECT GROUP]: " +
  2096. "DelinkFromGroup(): Child prim {0} not found in object {1}, {2}",
  2097. partID, LocalId, UUID);
  2098. return null;
  2099. }
  2100. }
  2101. /// <summary>
  2102. /// Delink the given prim from this group. The delinked prim is established as
  2103. /// an independent SceneObjectGroup.
  2104. /// </summary>
  2105. /// <remarks>
  2106. /// FIXME: This method should not be called directly since it bypasses update locking, allowing a potential race
  2107. /// condition. But currently there is no
  2108. /// alternative method that does take a lock to delink a single prim.
  2109. /// </remarks>
  2110. /// <param name="partID"></param>
  2111. /// <param name="sendEvents"></param>
  2112. /// <returns>The object group of the newly delinked prim.</returns>
  2113. public SceneObjectGroup DelinkFromGroup(SceneObjectPart linkPart, bool sendEvents)
  2114. {
  2115. // m_log.DebugFormat(
  2116. // "[SCENE OBJECT GROUP]: Delinking part {0}, {1} from group with root part {2}, {3}",
  2117. // linkPart.Name, linkPart.UUID, RootPart.Name, RootPart.UUID);
  2118. linkPart.ClearUndoState();
  2119. Vector3 worldPos = linkPart.GetWorldPosition();
  2120. Quaternion worldRot = linkPart.GetWorldRotation();
  2121. // Remove the part from this object
  2122. lock (m_parts.SyncRoot)
  2123. {
  2124. m_parts.Remove(linkPart.UUID);
  2125. SceneObjectPart[] parts = m_parts.GetArray();
  2126. // Rejigger the linknum's of the remaining SOP's to fill any gap
  2127. if (parts.Length == 1 && RootPart != null)
  2128. {
  2129. // Single prim left
  2130. RootPart.LinkNum = 0;
  2131. }
  2132. else
  2133. {
  2134. for (int i = 0; i < parts.Length; i++)
  2135. {
  2136. SceneObjectPart part = parts[i];
  2137. if (part.LinkNum > linkPart.LinkNum)
  2138. part.LinkNum--;
  2139. }
  2140. }
  2141. }
  2142. linkPart.ParentID = 0;
  2143. linkPart.LinkNum = 0;
  2144. PhysicsActor linkPartPa = linkPart.PhysActor;
  2145. // Remove the SOP from the physical scene.
  2146. // If the new SOG is physical, it is re-created later.
  2147. // (There is a problem here in that we have not yet told the physics
  2148. // engine about the delink. Someday, linksets should be made first
  2149. // class objects in the physics engine interface).
  2150. if (linkPartPa != null)
  2151. m_scene.PhysicsScene.RemovePrim(linkPartPa);
  2152. // We need to reset the child part's position
  2153. // ready for life as a separate object after being a part of another object
  2154. /* This commented out code seems to recompute what GetWorldPosition already does.
  2155. * Replace with a call to GetWorldPosition (before unlinking)
  2156. Quaternion parentRot = m_rootPart.RotationOffset;
  2157. Vector3 axPos = linkPart.OffsetPosition;
  2158. axPos *= parentRot;
  2159. linkPart.OffsetPosition = new Vector3(axPos.X, axPos.Y, axPos.Z);
  2160. linkPart.GroupPosition = AbsolutePosition + linkPart.OffsetPosition;
  2161. linkPart.OffsetPosition = new Vector3(0, 0, 0);
  2162. */
  2163. linkPart.GroupPosition = worldPos;
  2164. linkPart.OffsetPosition = Vector3.Zero;
  2165. linkPart.RotationOffset = worldRot;
  2166. // Create a new SOG to go around this unlinked and unattached SOP
  2167. SceneObjectGroup objectGroup = new SceneObjectGroup(linkPart);
  2168. m_scene.AddNewSceneObject(objectGroup, true);
  2169. if (sendEvents)
  2170. linkPart.TriggerScriptChangedEvent(Changed.LINK);
  2171. linkPart.Rezzed = RootPart.Rezzed;
  2172. // We must persist the delinked group to the database immediately, for safety. The problem
  2173. // is that although in memory the new group has a new SceneGroupID, in the database it
  2174. // still has the parent group's SceneGroupID (until the next backup). This means that if the
  2175. // parent group is deleted then the delinked group will also be deleted from the database.
  2176. // This problem will disappear if the region remains alive long enough for another backup,
  2177. // since at that time the delinked group's new SceneGroupID will be written to the database.
  2178. // But if the region crashes before that then the prims will be permanently gone, and this must
  2179. // not happen. (We can't use a just-in-time trick like GroupContainsForeignPrims in this case
  2180. // because the delinked group doesn't know when the source group is deleted.)
  2181. m_scene.ForceSceneObjectBackup(objectGroup);
  2182. return objectGroup;
  2183. }
  2184. /// <summary>
  2185. /// Stop this object from being persisted over server restarts.
  2186. /// </summary>
  2187. /// <param name="objectGroup"></param>
  2188. public virtual void DetachFromBackup()
  2189. {
  2190. if (Backup && Scene != null)
  2191. m_scene.EventManager.OnBackup -= ProcessBackup;
  2192. Backup = false;
  2193. }
  2194. // This links an SOP from a previous linkset into my linkset.
  2195. // The trick is that the SOP's position and rotation are relative to the old root SOP's
  2196. // so we are passed in the position and rotation of the old linkset so this can
  2197. // unjigger this SOP's position and rotation from the previous linkset and
  2198. // then make them relative to my linkset root.
  2199. private void LinkNonRootPart(SceneObjectPart part, Vector3 oldGroupPosition, Quaternion oldGroupRotation, int linkNum)
  2200. {
  2201. Quaternion parentRot = oldGroupRotation;
  2202. Quaternion oldRot = part.RotationOffset;
  2203. // Move our position to not be relative to the old parent
  2204. Vector3 axPos = part.OffsetPosition;
  2205. axPos *= parentRot;
  2206. part.OffsetPosition = axPos;
  2207. part.GroupPosition = oldGroupPosition + part.OffsetPosition;
  2208. part.OffsetPosition = Vector3.Zero;
  2209. // Compution our rotation to be not relative to the old parent
  2210. Quaternion worldRot = parentRot * oldRot;
  2211. part.RotationOffset = worldRot;
  2212. // Add this SOP to our linkset
  2213. part.SetParent(this);
  2214. part.ParentID = m_rootPart.LocalId;
  2215. m_parts.Add(part.UUID, part);
  2216. part.LinkNum = linkNum;
  2217. // Compute the new position of this SOP relative to the group position
  2218. part.OffsetPosition = part.GroupPosition - AbsolutePosition;
  2219. // (radams1 20120711: I don't know why part.OffsetPosition is set multiple times.
  2220. // It would have the affect of setting the physics engine position multiple
  2221. // times. In theory, that is not necessary but I don't have a good linkset
  2222. // test to know that cleaning up this code wouldn't break things.)
  2223. // Rotate the relative position by the rotation of the group
  2224. Quaternion rootRotation = m_rootPart.RotationOffset;
  2225. Vector3 pos = part.OffsetPosition;
  2226. pos *= Quaternion.Inverse(rootRotation);
  2227. part.OffsetPosition = pos;
  2228. // Compute the SOP's rotation relative to the rotation of the group.
  2229. parentRot = m_rootPart.RotationOffset;
  2230. oldRot = part.RotationOffset;
  2231. Quaternion newRot = Quaternion.Inverse(parentRot) * oldRot;
  2232. part.RotationOffset = newRot;
  2233. // Since this SOP's state has changed, push those changes into the physics engine
  2234. // and the simulator.
  2235. part.UpdatePrimFlags(UsesPhysics, IsTemporary, IsPhantom, IsVolumeDetect);
  2236. }
  2237. /// <summary>
  2238. /// If object is physical, apply force to move it around
  2239. /// If object is not physical, just put it at the resulting location
  2240. /// </summary>
  2241. /// <param name="partID">Part ID to check for grab</param>
  2242. /// <param name="offset">Always seems to be 0,0,0, so ignoring</param>
  2243. /// <param name="pos">New position. We do the math here to turn it into a force</param>
  2244. /// <param name="remoteClient"></param>
  2245. public void GrabMovement(UUID partID, Vector3 offset, Vector3 pos, IClientAPI remoteClient)
  2246. {
  2247. if (m_scene.EventManager.TriggerGroupMove(UUID, pos))
  2248. {
  2249. SceneObjectPart part = GetPart(partID);
  2250. if (part == null)
  2251. return;
  2252. PhysicsActor pa = m_rootPart.PhysActor;
  2253. if (pa != null)
  2254. {
  2255. if (pa.IsPhysical)
  2256. {
  2257. if (!BlockGrabOverride && !part.BlockGrab)
  2258. {
  2259. Vector3 llmoveforce = pos - AbsolutePosition;
  2260. Vector3 grabforce = llmoveforce;
  2261. grabforce = (grabforce / 10) * pa.Mass;
  2262. pa.AddForce(grabforce, true);
  2263. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  2264. }
  2265. }
  2266. else
  2267. {
  2268. //NonPhysicalGrabMovement(pos);
  2269. }
  2270. }
  2271. else
  2272. {
  2273. //NonPhysicalGrabMovement(pos);
  2274. }
  2275. }
  2276. }
  2277. public void NonPhysicalGrabMovement(Vector3 pos)
  2278. {
  2279. AbsolutePosition = pos;
  2280. m_rootPart.SendTerseUpdateToAllClients();
  2281. }
  2282. /// <summary>
  2283. /// If object is physical, prepare for spinning torques (set flag to save old orientation)
  2284. /// </summary>
  2285. /// <param name="rotation">Rotation. We do the math here to turn it into a torque</param>
  2286. /// <param name="remoteClient"></param>
  2287. public void SpinStart(IClientAPI remoteClient)
  2288. {
  2289. if (m_scene.EventManager.TriggerGroupSpinStart(UUID))
  2290. {
  2291. PhysicsActor pa = m_rootPart.PhysActor;
  2292. if (pa != null)
  2293. {
  2294. if (pa.IsPhysical)
  2295. {
  2296. m_rootPart.IsWaitingForFirstSpinUpdatePacket = true;
  2297. }
  2298. }
  2299. }
  2300. }
  2301. /// <summary>
  2302. /// If object is physical, apply torque to spin it around
  2303. /// </summary>
  2304. /// <param name="rotation">Rotation. We do the math here to turn it into a torque</param>
  2305. /// <param name="remoteClient"></param>
  2306. public void SpinMovement(Quaternion newOrientation, IClientAPI remoteClient)
  2307. {
  2308. // The incoming newOrientation, sent by the client, "seems" to be the
  2309. // desired target orientation. This needs further verification; in particular,
  2310. // one would expect that the initial incoming newOrientation should be
  2311. // fairly close to the original prim's physical orientation,
  2312. // m_rootPart.PhysActor.Orientation. This however does not seem to be the
  2313. // case (might just be an issue with different quaternions representing the
  2314. // same rotation, or it might be a coordinate system issue).
  2315. //
  2316. // Since it's not clear what the relationship is between the PhysActor.Orientation
  2317. // and the incoming orientations sent by the client, we take an alternative approach
  2318. // of calculating the delta rotation between the orientations being sent by the
  2319. // client. (Since a spin is invoked by ctrl+shift+drag in the client, we expect
  2320. // a steady stream of several new orientations coming in from the client.)
  2321. // This ensures that the delta rotations are being calculated from self-consistent
  2322. // pairs of old/new rotations. Given the delta rotation, we apply a torque around
  2323. // the delta rotation axis, scaled by the object mass times an arbitrary scaling
  2324. // factor (to ensure the resulting torque is not "too strong" or "too weak").
  2325. //
  2326. // Ideally we need to calculate (probably iteratively) the exact torque or series
  2327. // of torques needed to arrive exactly at the destination orientation. However, since
  2328. // it is not yet clear how to map the destination orientation (provided by the viewer)
  2329. // into PhysActor orientations (needed by the physics engine), we omit this step.
  2330. // This means that the resulting torque will at least be in the correct direction,
  2331. // but it will result in over-shoot or under-shoot of the target orientation.
  2332. // For the end user, this means that ctrl+shift+drag can be used for relative,
  2333. // but not absolute, adjustments of orientation for physical prims.
  2334. if (m_scene.EventManager.TriggerGroupSpin(UUID, newOrientation))
  2335. {
  2336. PhysicsActor pa = m_rootPart.PhysActor;
  2337. if (pa != null)
  2338. {
  2339. if (pa.IsPhysical)
  2340. {
  2341. if (m_rootPart.IsWaitingForFirstSpinUpdatePacket)
  2342. {
  2343. // first time initialization of "old" orientation for calculation of delta rotations
  2344. m_rootPart.SpinOldOrientation = newOrientation;
  2345. m_rootPart.IsWaitingForFirstSpinUpdatePacket = false;
  2346. }
  2347. else
  2348. {
  2349. // save and update old orientation
  2350. Quaternion old = m_rootPart.SpinOldOrientation;
  2351. m_rootPart.SpinOldOrientation = newOrientation;
  2352. //m_log.Error("[SCENE OBJECT GROUP]: Old orientation is " + old);
  2353. //m_log.Error("[SCENE OBJECT GROUP]: Incoming new orientation is " + newOrientation);
  2354. // compute difference between previous old rotation and new incoming rotation
  2355. Quaternion minimalRotationFromQ1ToQ2 = Quaternion.Inverse(old) * newOrientation;
  2356. float rotationAngle;
  2357. Vector3 rotationAxis;
  2358. minimalRotationFromQ1ToQ2.GetAxisAngle(out rotationAxis, out rotationAngle);
  2359. rotationAxis.Normalize();
  2360. //m_log.Error("SCENE OBJECT GROUP]: rotation axis is " + rotationAxis);
  2361. Vector3 spinforce = new Vector3(rotationAxis.X, rotationAxis.Y, rotationAxis.Z);
  2362. spinforce = (spinforce/8) * pa.Mass; // 8 is an arbitrary torque scaling factor
  2363. pa.AddAngularForce(spinforce,true);
  2364. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  2365. }
  2366. }
  2367. else
  2368. {
  2369. //NonPhysicalSpinMovement(pos);
  2370. }
  2371. }
  2372. else
  2373. {
  2374. //NonPhysicalSpinMovement(pos);
  2375. }
  2376. }
  2377. }
  2378. /// <summary>
  2379. /// Set the name of a prim
  2380. /// </summary>
  2381. /// <param name="name"></param>
  2382. /// <param name="localID"></param>
  2383. public void SetPartName(string name, uint localID)
  2384. {
  2385. SceneObjectPart part = GetPart(localID);
  2386. if (part != null)
  2387. {
  2388. part.Name = name;
  2389. }
  2390. }
  2391. public void SetPartDescription(string des, uint localID)
  2392. {
  2393. SceneObjectPart part = GetPart(localID);
  2394. if (part != null)
  2395. {
  2396. part.Description = des;
  2397. }
  2398. }
  2399. public void SetPartText(string text, uint localID)
  2400. {
  2401. SceneObjectPart part = GetPart(localID);
  2402. if (part != null)
  2403. {
  2404. part.SetText(text);
  2405. }
  2406. }
  2407. public void SetPartText(string text, UUID partID)
  2408. {
  2409. SceneObjectPart part = GetPart(partID);
  2410. if (part != null)
  2411. {
  2412. part.SetText(text);
  2413. }
  2414. }
  2415. public string GetPartName(uint localID)
  2416. {
  2417. SceneObjectPart part = GetPart(localID);
  2418. if (part != null)
  2419. {
  2420. return part.Name;
  2421. }
  2422. return String.Empty;
  2423. }
  2424. public string GetPartDescription(uint localID)
  2425. {
  2426. SceneObjectPart part = GetPart(localID);
  2427. if (part != null)
  2428. {
  2429. return part.Description;
  2430. }
  2431. return String.Empty;
  2432. }
  2433. /// <summary>
  2434. /// Update prim flags for this group.
  2435. /// </summary>
  2436. /// <param name="localID"></param>
  2437. /// <param name="UsePhysics"></param>
  2438. /// <param name="SetTemporary"></param>
  2439. /// <param name="SetPhantom"></param>
  2440. /// <param name="SetVolumeDetect"></param>
  2441. public void UpdatePrimFlags(uint localID, bool UsePhysics, bool SetTemporary, bool SetPhantom, bool SetVolumeDetect)
  2442. {
  2443. SceneObjectPart selectionPart = GetPart(localID);
  2444. if (Scene != null)
  2445. {
  2446. if (SetTemporary)
  2447. {
  2448. DetachFromBackup();
  2449. // Remove from database and parcel prim count
  2450. //
  2451. m_scene.DeleteFromStorage(UUID);
  2452. }
  2453. else if (!Backup)
  2454. {
  2455. // Previously been temporary now switching back so make it
  2456. // available for persisting again
  2457. AttachToBackup();
  2458. }
  2459. m_scene.EventManager.TriggerParcelPrimCountTainted();
  2460. }
  2461. if (selectionPart != null)
  2462. {
  2463. SceneObjectPart[] parts = m_parts.GetArray();
  2464. if (Scene != null)
  2465. {
  2466. for (int i = 0; i < parts.Length; i++)
  2467. {
  2468. SceneObjectPart part = parts[i];
  2469. if (part.Scale.X > m_scene.m_maxPhys ||
  2470. part.Scale.Y > m_scene.m_maxPhys ||
  2471. part.Scale.Z > m_scene.m_maxPhys )
  2472. {
  2473. UsePhysics = false; // Reset physics
  2474. break;
  2475. }
  2476. }
  2477. }
  2478. for (int i = 0; i < parts.Length; i++)
  2479. parts[i].UpdatePrimFlags(UsePhysics, SetTemporary, SetPhantom, SetVolumeDetect);
  2480. }
  2481. }
  2482. public void UpdateExtraParam(uint localID, ushort type, bool inUse, byte[] data)
  2483. {
  2484. SceneObjectPart part = GetPart(localID);
  2485. if (part != null)
  2486. {
  2487. part.UpdateExtraParam(type, inUse, data);
  2488. }
  2489. }
  2490. /// <summary>
  2491. /// Update the texture entry for this part
  2492. /// </summary>
  2493. /// <param name="localID"></param>
  2494. /// <param name="textureEntry"></param>
  2495. public void UpdateTextureEntry(uint localID, byte[] textureEntry)
  2496. {
  2497. SceneObjectPart part = GetPart(localID);
  2498. if (part != null)
  2499. {
  2500. part.UpdateTextureEntry(textureEntry);
  2501. }
  2502. }
  2503. public void AdjustChildPrimPermissions(bool forceTaskInventoryPermissive)
  2504. {
  2505. uint newOwnerMask = (uint)(PermissionMask.All | PermissionMask.Export) & 0xfffffff8; // Mask folded bits
  2506. uint foldedPerms = RootPart.OwnerMask & 3;
  2507. ForEachPart(part =>
  2508. {
  2509. newOwnerMask &= part.BaseMask;
  2510. if (part != RootPart)
  2511. part.ClonePermissions(RootPart);
  2512. if (forceTaskInventoryPermissive)
  2513. part.Inventory.ApplyGodPermissions(part.BaseMask);
  2514. });
  2515. uint lockMask = ~(uint)(PermissionMask.Move | PermissionMask.Modify);
  2516. uint lockBit = RootPart.OwnerMask & (uint)(PermissionMask.Move | PermissionMask.Modify);
  2517. RootPart.OwnerMask = (RootPart.OwnerMask & lockBit) | ((newOwnerMask | foldedPerms) & lockMask);
  2518. RootPart.ScheduleFullUpdate();
  2519. }
  2520. public void UpdatePermissions(UUID AgentID, byte field, uint localID,
  2521. uint mask, byte addRemTF)
  2522. {
  2523. RootPart.UpdatePermissions(AgentID, field, localID, mask, addRemTF);
  2524. AdjustChildPrimPermissions(Scene.Permissions.IsGod(AgentID));
  2525. HasGroupChanged = true;
  2526. // Send the group's properties to all clients once all parts are updated
  2527. IClientAPI client;
  2528. if (Scene.TryGetClient(AgentID, out client))
  2529. SendPropertiesToClient(client);
  2530. }
  2531. #endregion
  2532. #region Shape
  2533. /// <summary>
  2534. ///
  2535. /// </summary>
  2536. /// <param name="shapeBlock"></param>
  2537. public void UpdateShape(ObjectShapePacket.ObjectDataBlock shapeBlock, uint localID)
  2538. {
  2539. SceneObjectPart part = GetPart(localID);
  2540. if (part != null)
  2541. {
  2542. part.UpdateShape(shapeBlock);
  2543. PhysicsActor pa = m_rootPart.PhysActor;
  2544. if (pa != null)
  2545. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  2546. }
  2547. }
  2548. #endregion
  2549. #region Resize
  2550. /// <summary>
  2551. /// Resize the entire group of prims.
  2552. /// </summary>
  2553. /// <param name="scale"></param>
  2554. public void GroupResize(Vector3 scale)
  2555. {
  2556. // m_log.DebugFormat(
  2557. // "[SCENE OBJECT GROUP]: Group resizing {0} {1} from {2} to {3}", Name, LocalId, RootPart.Scale, scale);
  2558. PhysicsActor pa = m_rootPart.PhysActor;
  2559. RootPart.StoreUndoState(true);
  2560. if (Scene != null)
  2561. {
  2562. scale.X = Math.Max(Scene.m_minNonphys, Math.Min(Scene.m_maxNonphys, scale.X));
  2563. scale.Y = Math.Max(Scene.m_minNonphys, Math.Min(Scene.m_maxNonphys, scale.Y));
  2564. scale.Z = Math.Max(Scene.m_minNonphys, Math.Min(Scene.m_maxNonphys, scale.Z));
  2565. if (pa != null && pa.IsPhysical)
  2566. {
  2567. scale.X = Math.Max(Scene.m_minPhys, Math.Min(Scene.m_maxPhys, scale.X));
  2568. scale.Y = Math.Max(Scene.m_minPhys, Math.Min(Scene.m_maxPhys, scale.Y));
  2569. scale.Z = Math.Max(Scene.m_minPhys, Math.Min(Scene.m_maxPhys, scale.Z));
  2570. }
  2571. }
  2572. float x = (scale.X / RootPart.Scale.X);
  2573. float y = (scale.Y / RootPart.Scale.Y);
  2574. float z = (scale.Z / RootPart.Scale.Z);
  2575. SceneObjectPart[] parts = m_parts.GetArray();
  2576. if (Scene != null & (x > 1.0f || y > 1.0f || z > 1.0f))
  2577. {
  2578. for (int i = 0; i < parts.Length; i++)
  2579. {
  2580. SceneObjectPart obPart = parts[i];
  2581. if (obPart.UUID != m_rootPart.UUID)
  2582. {
  2583. // obPart.IgnoreUndoUpdate = true;
  2584. Vector3 oldSize = new Vector3(obPart.Scale);
  2585. float f = 1.0f;
  2586. float a = 1.0f;
  2587. if (pa != null && pa.IsPhysical)
  2588. {
  2589. if (oldSize.X * x > Scene.m_maxPhys)
  2590. {
  2591. f = m_scene.m_maxPhys / oldSize.X;
  2592. a = f / x;
  2593. x *= a;
  2594. y *= a;
  2595. z *= a;
  2596. }
  2597. else if (oldSize.X * x < Scene.m_minPhys)
  2598. {
  2599. f = m_scene.m_minPhys / oldSize.X;
  2600. a = f / x;
  2601. x *= a;
  2602. y *= a;
  2603. z *= a;
  2604. }
  2605. if (oldSize.Y * y > Scene.m_maxPhys)
  2606. {
  2607. f = m_scene.m_maxPhys / oldSize.Y;
  2608. a = f / y;
  2609. x *= a;
  2610. y *= a;
  2611. z *= a;
  2612. }
  2613. else if (oldSize.Y * y < Scene.m_minPhys)
  2614. {
  2615. f = m_scene.m_minPhys / oldSize.Y;
  2616. a = f / y;
  2617. x *= a;
  2618. y *= a;
  2619. z *= a;
  2620. }
  2621. if (oldSize.Z * z > Scene.m_maxPhys)
  2622. {
  2623. f = m_scene.m_maxPhys / oldSize.Z;
  2624. a = f / z;
  2625. x *= a;
  2626. y *= a;
  2627. z *= a;
  2628. }
  2629. else if (oldSize.Z * z < Scene.m_minPhys)
  2630. {
  2631. f = m_scene.m_minPhys / oldSize.Z;
  2632. a = f / z;
  2633. x *= a;
  2634. y *= a;
  2635. z *= a;
  2636. }
  2637. }
  2638. else
  2639. {
  2640. if (oldSize.X * x > Scene.m_maxNonphys)
  2641. {
  2642. f = m_scene.m_maxNonphys / oldSize.X;
  2643. a = f / x;
  2644. x *= a;
  2645. y *= a;
  2646. z *= a;
  2647. }
  2648. else if (oldSize.X * x < Scene.m_minNonphys)
  2649. {
  2650. f = m_scene.m_minNonphys / oldSize.X;
  2651. a = f / x;
  2652. x *= a;
  2653. y *= a;
  2654. z *= a;
  2655. }
  2656. if (oldSize.Y * y > Scene.m_maxNonphys)
  2657. {
  2658. f = m_scene.m_maxNonphys / oldSize.Y;
  2659. a = f / y;
  2660. x *= a;
  2661. y *= a;
  2662. z *= a;
  2663. }
  2664. else if (oldSize.Y * y < Scene.m_minNonphys)
  2665. {
  2666. f = m_scene.m_minNonphys / oldSize.Y;
  2667. a = f / y;
  2668. x *= a;
  2669. y *= a;
  2670. z *= a;
  2671. }
  2672. if (oldSize.Z * z > Scene.m_maxNonphys)
  2673. {
  2674. f = m_scene.m_maxNonphys / oldSize.Z;
  2675. a = f / z;
  2676. x *= a;
  2677. y *= a;
  2678. z *= a;
  2679. }
  2680. else if (oldSize.Z * z < Scene.m_minNonphys)
  2681. {
  2682. f = m_scene.m_minNonphys / oldSize.Z;
  2683. a = f / z;
  2684. x *= a;
  2685. y *= a;
  2686. z *= a;
  2687. }
  2688. }
  2689. // obPart.IgnoreUndoUpdate = false;
  2690. }
  2691. }
  2692. }
  2693. Vector3 prevScale = RootPart.Scale;
  2694. prevScale.X *= x;
  2695. prevScale.Y *= y;
  2696. prevScale.Z *= z;
  2697. // RootPart.IgnoreUndoUpdate = true;
  2698. RootPart.Resize(prevScale);
  2699. // RootPart.IgnoreUndoUpdate = false;
  2700. for (int i = 0; i < parts.Length; i++)
  2701. {
  2702. SceneObjectPart obPart = parts[i];
  2703. if (obPart.UUID != m_rootPart.UUID)
  2704. {
  2705. obPart.IgnoreUndoUpdate = true;
  2706. Vector3 currentpos = new Vector3(obPart.OffsetPosition);
  2707. currentpos.X *= x;
  2708. currentpos.Y *= y;
  2709. currentpos.Z *= z;
  2710. Vector3 newSize = new Vector3(obPart.Scale);
  2711. newSize.X *= x;
  2712. newSize.Y *= y;
  2713. newSize.Z *= z;
  2714. obPart.Resize(newSize);
  2715. obPart.UpdateOffSet(currentpos);
  2716. obPart.IgnoreUndoUpdate = false;
  2717. }
  2718. // obPart.IgnoreUndoUpdate = false;
  2719. // obPart.StoreUndoState();
  2720. }
  2721. // m_log.DebugFormat(
  2722. // "[SCENE OBJECT GROUP]: Finished group resizing {0} {1} to {2}", Name, LocalId, RootPart.Scale);
  2723. }
  2724. #endregion
  2725. #region Position
  2726. /// <summary>
  2727. /// Move this scene object
  2728. /// </summary>
  2729. /// <param name="pos"></param>
  2730. public void UpdateGroupPosition(Vector3 pos)
  2731. {
  2732. // m_log.DebugFormat("[SCENE OBJECT GROUP]: Updating group position on {0} {1} to {2}", Name, LocalId, pos);
  2733. RootPart.StoreUndoState(true);
  2734. // SceneObjectPart[] parts = m_parts.GetArray();
  2735. // for (int i = 0; i < parts.Length; i++)
  2736. // parts[i].StoreUndoState();
  2737. if (m_scene.EventManager.TriggerGroupMove(UUID, pos))
  2738. {
  2739. if (IsAttachment)
  2740. {
  2741. m_rootPart.AttachedPos = pos;
  2742. }
  2743. if (RootPart.GetStatusSandbox())
  2744. {
  2745. if (Util.GetDistanceTo(RootPart.StatusSandboxPos, pos) > 10)
  2746. {
  2747. RootPart.ScriptSetPhysicsStatus(false);
  2748. pos = AbsolutePosition;
  2749. Scene.SimChat(Utils.StringToBytes("Hit Sandbox Limit"),
  2750. ChatTypeEnum.DebugChannel, 0x7FFFFFFF, RootPart.AbsolutePosition, Name, UUID, false);
  2751. }
  2752. }
  2753. AbsolutePosition = pos;
  2754. HasGroupChanged = true;
  2755. }
  2756. //we need to do a terse update even if the move wasn't allowed
  2757. // so that the position is reset in the client (the object snaps back)
  2758. RootPart.ScheduleTerseUpdate();
  2759. }
  2760. /// <summary>
  2761. /// Update the position of a single part of this scene object
  2762. /// </summary>
  2763. /// <param name="pos"></param>
  2764. /// <param name="localID"></param>
  2765. public void UpdateSinglePosition(Vector3 pos, uint localID)
  2766. {
  2767. SceneObjectPart part = GetPart(localID);
  2768. // SceneObjectPart[] parts = m_parts.GetArray();
  2769. // for (int i = 0; i < parts.Length; i++)
  2770. // parts[i].StoreUndoState();
  2771. if (part != null)
  2772. {
  2773. // m_log.DebugFormat(
  2774. // "[SCENE OBJECT GROUP]: Updating single position of {0} {1} to {2}", part.Name, part.LocalId, pos);
  2775. part.StoreUndoState(false);
  2776. part.IgnoreUndoUpdate = true;
  2777. if (part.UUID == m_rootPart.UUID)
  2778. {
  2779. UpdateRootPosition(pos);
  2780. }
  2781. else
  2782. {
  2783. part.UpdateOffSet(pos);
  2784. }
  2785. HasGroupChanged = true;
  2786. part.IgnoreUndoUpdate = false;
  2787. }
  2788. }
  2789. /// <summary>
  2790. /// Update just the root prim position in a linkset
  2791. /// </summary>
  2792. /// <param name="newPos"></param>
  2793. public void UpdateRootPosition(Vector3 newPos)
  2794. {
  2795. // m_log.DebugFormat(
  2796. // "[SCENE OBJECT GROUP]: Updating root position of {0} {1} to {2}", Name, LocalId, pos);
  2797. // SceneObjectPart[] parts = m_parts.GetArray();
  2798. // for (int i = 0; i < parts.Length; i++)
  2799. // parts[i].StoreUndoState();
  2800. Vector3 oldPos;
  2801. if (IsAttachment)
  2802. oldPos = m_rootPart.AttachedPos + m_rootPart.OffsetPosition; // OffsetPosition should always be 0 in an attachments's root prim
  2803. else
  2804. oldPos = AbsolutePosition + m_rootPart.OffsetPosition;
  2805. Vector3 diff = oldPos - newPos;
  2806. Quaternion partRotation = m_rootPart.RotationOffset;
  2807. diff *= Quaternion.Inverse(partRotation);
  2808. SceneObjectPart[] parts = m_parts.GetArray();
  2809. for (int i = 0; i < parts.Length; i++)
  2810. {
  2811. SceneObjectPart obPart = parts[i];
  2812. if (obPart.UUID != m_rootPart.UUID)
  2813. obPart.OffsetPosition = obPart.OffsetPosition + diff;
  2814. }
  2815. AbsolutePosition = newPos;
  2816. if (IsAttachment)
  2817. m_rootPart.AttachedPos = newPos;
  2818. HasGroupChanged = true;
  2819. ScheduleGroupForTerseUpdate();
  2820. }
  2821. #endregion
  2822. #region Rotation
  2823. /// <summary>
  2824. /// Update the rotation of the group.
  2825. /// </summary>
  2826. /// <param name="rot"></param>
  2827. public void UpdateGroupRotationR(Quaternion rot)
  2828. {
  2829. // m_log.DebugFormat(
  2830. // "[SCENE OBJECT GROUP]: Updating group rotation R of {0} {1} to {2}", Name, LocalId, rot);
  2831. // SceneObjectPart[] parts = m_parts.GetArray();
  2832. // for (int i = 0; i < parts.Length; i++)
  2833. // parts[i].StoreUndoState();
  2834. m_rootPart.StoreUndoState(true);
  2835. m_rootPart.UpdateRotation(rot);
  2836. PhysicsActor actor = m_rootPart.PhysActor;
  2837. if (actor != null)
  2838. {
  2839. actor.Orientation = m_rootPart.RotationOffset;
  2840. m_scene.PhysicsScene.AddPhysicsActorTaint(actor);
  2841. }
  2842. HasGroupChanged = true;
  2843. ScheduleGroupForTerseUpdate();
  2844. }
  2845. /// <summary>
  2846. /// Update the position and rotation of a group simultaneously.
  2847. /// </summary>
  2848. /// <param name="pos"></param>
  2849. /// <param name="rot"></param>
  2850. public void UpdateGroupRotationPR(Vector3 pos, Quaternion rot)
  2851. {
  2852. // m_log.DebugFormat(
  2853. // "[SCENE OBJECT GROUP]: Updating group rotation PR of {0} {1} to {2}", Name, LocalId, rot);
  2854. // SceneObjectPart[] parts = m_parts.GetArray();
  2855. // for (int i = 0; i < parts.Length; i++)
  2856. // parts[i].StoreUndoState();
  2857. RootPart.StoreUndoState(true);
  2858. RootPart.IgnoreUndoUpdate = true;
  2859. m_rootPart.UpdateRotation(rot);
  2860. PhysicsActor actor = m_rootPart.PhysActor;
  2861. if (actor != null)
  2862. {
  2863. actor.Orientation = m_rootPart.RotationOffset;
  2864. m_scene.PhysicsScene.AddPhysicsActorTaint(actor);
  2865. }
  2866. if (IsAttachment)
  2867. {
  2868. m_rootPart.AttachedPos = pos;
  2869. }
  2870. AbsolutePosition = pos;
  2871. HasGroupChanged = true;
  2872. ScheduleGroupForTerseUpdate();
  2873. RootPart.IgnoreUndoUpdate = false;
  2874. }
  2875. /// <summary>
  2876. /// Update the rotation of a single prim within the group.
  2877. /// </summary>
  2878. /// <param name="rot"></param>
  2879. /// <param name="localID"></param>
  2880. public void UpdateSingleRotation(Quaternion rot, uint localID)
  2881. {
  2882. SceneObjectPart part = GetPart(localID);
  2883. SceneObjectPart[] parts = m_parts.GetArray();
  2884. for (int i = 0; i < parts.Length; i++)
  2885. parts[i].StoreUndoState();
  2886. if (part != null)
  2887. {
  2888. // m_log.DebugFormat(
  2889. // "[SCENE OBJECT GROUP]: Updating single rotation of {0} {1} to {2}", part.Name, part.LocalId, rot);
  2890. if (part.UUID == m_rootPart.UUID)
  2891. {
  2892. UpdateRootRotation(rot);
  2893. }
  2894. else
  2895. {
  2896. part.UpdateRotation(rot);
  2897. }
  2898. }
  2899. }
  2900. /// <summary>
  2901. /// Update the position and rotation simultaneously of a single prim within the group.
  2902. /// </summary>
  2903. /// <param name="rot"></param>
  2904. /// <param name="localID"></param>
  2905. public void UpdateSingleRotation(Quaternion rot, Vector3 pos, uint localID)
  2906. {
  2907. SceneObjectPart part = GetPart(localID);
  2908. if (part != null)
  2909. {
  2910. // m_log.DebugFormat(
  2911. // "[SCENE OBJECT GROUP]: Updating single position and rotation of {0} {1} to {2}",
  2912. // part.Name, part.LocalId, rot);
  2913. part.StoreUndoState();
  2914. part.IgnoreUndoUpdate = true;
  2915. if (part.UUID == m_rootPart.UUID)
  2916. {
  2917. UpdateRootRotation(rot);
  2918. AbsolutePosition = pos;
  2919. }
  2920. else
  2921. {
  2922. part.UpdateRotation(rot);
  2923. part.OffsetPosition = pos;
  2924. }
  2925. part.IgnoreUndoUpdate = false;
  2926. }
  2927. }
  2928. /// <summary>
  2929. /// Update the rotation of just the root prim of a linkset.
  2930. /// </summary>
  2931. /// <param name="rot"></param>
  2932. public void UpdateRootRotation(Quaternion rot)
  2933. {
  2934. // m_log.DebugFormat(
  2935. // "[SCENE OBJECT GROUP]: Updating root rotation of {0} {1} to {2}",
  2936. // Name, LocalId, rot);
  2937. Quaternion axRot = rot;
  2938. Quaternion oldParentRot = m_rootPart.RotationOffset;
  2939. m_rootPart.StoreUndoState();
  2940. m_rootPart.UpdateRotation(rot);
  2941. PhysicsActor pa = m_rootPart.PhysActor;
  2942. if (pa != null)
  2943. {
  2944. pa.Orientation = m_rootPart.RotationOffset;
  2945. m_scene.PhysicsScene.AddPhysicsActorTaint(pa);
  2946. }
  2947. SceneObjectPart[] parts = m_parts.GetArray();
  2948. for (int i = 0; i < parts.Length; i++)
  2949. {
  2950. SceneObjectPart prim = parts[i];
  2951. if (prim.UUID != m_rootPart.UUID)
  2952. {
  2953. prim.IgnoreUndoUpdate = true;
  2954. Vector3 axPos = prim.OffsetPosition;
  2955. axPos *= oldParentRot;
  2956. axPos *= Quaternion.Inverse(axRot);
  2957. prim.OffsetPosition = axPos;
  2958. Quaternion primsRot = prim.RotationOffset;
  2959. Quaternion newRot = oldParentRot * primsRot;
  2960. newRot = Quaternion.Inverse(axRot) * newRot;
  2961. prim.RotationOffset = newRot;
  2962. prim.ScheduleTerseUpdate();
  2963. prim.IgnoreUndoUpdate = false;
  2964. }
  2965. }
  2966. // for (int i = 0; i < parts.Length; i++)
  2967. // {
  2968. // SceneObjectPart childpart = parts[i];
  2969. // if (childpart != m_rootPart)
  2970. // {
  2971. //// childpart.IgnoreUndoUpdate = false;
  2972. //// childpart.StoreUndoState();
  2973. // }
  2974. // }
  2975. m_rootPart.ScheduleTerseUpdate();
  2976. // m_log.DebugFormat(
  2977. // "[SCENE OBJECT GROUP]: Updated root rotation of {0} {1} to {2}",
  2978. // Name, LocalId, rot);
  2979. }
  2980. #endregion
  2981. internal void SetAxisRotation(int axis, int rotate10)
  2982. {
  2983. bool setX = false;
  2984. bool setY = false;
  2985. bool setZ = false;
  2986. int xaxis = 2;
  2987. int yaxis = 4;
  2988. int zaxis = 8;
  2989. setX = ((axis & xaxis) != 0) ? true : false;
  2990. setY = ((axis & yaxis) != 0) ? true : false;
  2991. setZ = ((axis & zaxis) != 0) ? true : false;
  2992. float setval = (rotate10 > 0) ? 1f : 0f;
  2993. if (setX)
  2994. RootPart.RotationAxis.X = setval;
  2995. if (setY)
  2996. RootPart.RotationAxis.Y = setval;
  2997. if (setZ)
  2998. RootPart.RotationAxis.Z = setval;
  2999. if (setX || setY || setZ)
  3000. RootPart.SetPhysicsAxisRotation();
  3001. }
  3002. public int registerRotTargetWaypoint(Quaternion target, float tolerance)
  3003. {
  3004. scriptRotTarget waypoint = new scriptRotTarget();
  3005. waypoint.targetRot = target;
  3006. waypoint.tolerance = tolerance;
  3007. uint handle = m_scene.AllocateLocalId();
  3008. waypoint.handle = handle;
  3009. lock (m_rotTargets)
  3010. {
  3011. m_rotTargets.Add(handle, waypoint);
  3012. }
  3013. m_scene.AddGroupTarget(this);
  3014. return (int)handle;
  3015. }
  3016. public void unregisterRotTargetWaypoint(int handle)
  3017. {
  3018. lock (m_targets)
  3019. {
  3020. m_rotTargets.Remove((uint)handle);
  3021. if (m_targets.Count == 0)
  3022. m_scene.RemoveGroupTarget(this);
  3023. }
  3024. }
  3025. public int registerTargetWaypoint(Vector3 target, float tolerance)
  3026. {
  3027. scriptPosTarget waypoint = new scriptPosTarget();
  3028. waypoint.targetPos = target;
  3029. waypoint.tolerance = tolerance;
  3030. uint handle = m_scene.AllocateLocalId();
  3031. waypoint.handle = handle;
  3032. lock (m_targets)
  3033. {
  3034. m_targets.Add(handle, waypoint);
  3035. }
  3036. m_scene.AddGroupTarget(this);
  3037. return (int)handle;
  3038. }
  3039. public void unregisterTargetWaypoint(int handle)
  3040. {
  3041. lock (m_targets)
  3042. {
  3043. m_targets.Remove((uint)handle);
  3044. if (m_targets.Count == 0)
  3045. m_scene.RemoveGroupTarget(this);
  3046. }
  3047. }
  3048. public void checkAtTargets()
  3049. {
  3050. if (m_scriptListens_atTarget || m_scriptListens_notAtTarget)
  3051. {
  3052. if (m_targets.Count > 0)
  3053. {
  3054. bool at_target = false;
  3055. //Vector3 targetPos;
  3056. //uint targetHandle;
  3057. Dictionary<uint, scriptPosTarget> atTargets = new Dictionary<uint, scriptPosTarget>();
  3058. lock (m_targets)
  3059. {
  3060. foreach (uint idx in m_targets.Keys)
  3061. {
  3062. scriptPosTarget target = m_targets[idx];
  3063. if (Util.GetDistanceTo(target.targetPos, m_rootPart.GroupPosition) <= target.tolerance)
  3064. {
  3065. // trigger at_target
  3066. if (m_scriptListens_atTarget)
  3067. {
  3068. at_target = true;
  3069. scriptPosTarget att = new scriptPosTarget();
  3070. att.targetPos = target.targetPos;
  3071. att.tolerance = target.tolerance;
  3072. att.handle = target.handle;
  3073. atTargets.Add(idx, att);
  3074. }
  3075. }
  3076. }
  3077. }
  3078. if (atTargets.Count > 0)
  3079. {
  3080. SceneObjectPart[] parts = m_parts.GetArray();
  3081. uint[] localids = new uint[parts.Length];
  3082. for (int i = 0; i < parts.Length; i++)
  3083. localids[i] = parts[i].LocalId;
  3084. for (int ctr = 0; ctr < localids.Length; ctr++)
  3085. {
  3086. foreach (uint target in atTargets.Keys)
  3087. {
  3088. scriptPosTarget att = atTargets[target];
  3089. m_scene.EventManager.TriggerAtTargetEvent(
  3090. localids[ctr], att.handle, att.targetPos, m_rootPart.GroupPosition);
  3091. }
  3092. }
  3093. return;
  3094. }
  3095. if (m_scriptListens_notAtTarget && !at_target)
  3096. {
  3097. //trigger not_at_target
  3098. SceneObjectPart[] parts = m_parts.GetArray();
  3099. uint[] localids = new uint[parts.Length];
  3100. for (int i = 0; i < parts.Length; i++)
  3101. localids[i] = parts[i].LocalId;
  3102. for (int ctr = 0; ctr < localids.Length; ctr++)
  3103. {
  3104. m_scene.EventManager.TriggerNotAtTargetEvent(localids[ctr]);
  3105. }
  3106. }
  3107. }
  3108. }
  3109. if (m_scriptListens_atRotTarget || m_scriptListens_notAtRotTarget)
  3110. {
  3111. if (m_rotTargets.Count > 0)
  3112. {
  3113. bool at_Rottarget = false;
  3114. Dictionary<uint, scriptRotTarget> atRotTargets = new Dictionary<uint, scriptRotTarget>();
  3115. lock (m_rotTargets)
  3116. {
  3117. foreach (uint idx in m_rotTargets.Keys)
  3118. {
  3119. scriptRotTarget target = m_rotTargets[idx];
  3120. double angle
  3121. = Math.Acos(
  3122. target.targetRot.X * m_rootPart.RotationOffset.X
  3123. + target.targetRot.Y * m_rootPart.RotationOffset.Y
  3124. + target.targetRot.Z * m_rootPart.RotationOffset.Z
  3125. + target.targetRot.W * m_rootPart.RotationOffset.W)
  3126. * 2;
  3127. if (angle < 0) angle = -angle;
  3128. if (angle > Math.PI) angle = (Math.PI * 2 - angle);
  3129. if (angle <= target.tolerance)
  3130. {
  3131. // trigger at_rot_target
  3132. if (m_scriptListens_atRotTarget)
  3133. {
  3134. at_Rottarget = true;
  3135. scriptRotTarget att = new scriptRotTarget();
  3136. att.targetRot = target.targetRot;
  3137. att.tolerance = target.tolerance;
  3138. att.handle = target.handle;
  3139. atRotTargets.Add(idx, att);
  3140. }
  3141. }
  3142. }
  3143. }
  3144. if (atRotTargets.Count > 0)
  3145. {
  3146. SceneObjectPart[] parts = m_parts.GetArray();
  3147. uint[] localids = new uint[parts.Length];
  3148. for (int i = 0; i < parts.Length; i++)
  3149. localids[i] = parts[i].LocalId;
  3150. for (int ctr = 0; ctr < localids.Length; ctr++)
  3151. {
  3152. foreach (uint target in atRotTargets.Keys)
  3153. {
  3154. scriptRotTarget att = atRotTargets[target];
  3155. m_scene.EventManager.TriggerAtRotTargetEvent(
  3156. localids[ctr], att.handle, att.targetRot, m_rootPart.RotationOffset);
  3157. }
  3158. }
  3159. return;
  3160. }
  3161. if (m_scriptListens_notAtRotTarget && !at_Rottarget)
  3162. {
  3163. //trigger not_at_target
  3164. SceneObjectPart[] parts = m_parts.GetArray();
  3165. uint[] localids = new uint[parts.Length];
  3166. for (int i = 0; i < parts.Length; i++)
  3167. localids[i] = parts[i].LocalId;
  3168. for (int ctr = 0; ctr < localids.Length; ctr++)
  3169. {
  3170. m_scene.EventManager.TriggerNotAtRotTargetEvent(localids[ctr]);
  3171. }
  3172. }
  3173. }
  3174. }
  3175. }
  3176. public float GetMass()
  3177. {
  3178. float retmass = 0f;
  3179. SceneObjectPart[] parts = m_parts.GetArray();
  3180. for (int i = 0; i < parts.Length; i++)
  3181. retmass += parts[i].GetMass();
  3182. return retmass;
  3183. }
  3184. /// <summary>
  3185. /// If the object is a sculpt/mesh, retrieve the mesh data for each part and reinsert it into each shape so that
  3186. /// the physics engine can use it.
  3187. /// </summary>
  3188. /// <remarks>
  3189. /// When the physics engine has finished with it, the sculpt data is discarded to save memory.
  3190. /// </remarks>
  3191. /*
  3192. public void CheckSculptAndLoad()
  3193. {
  3194. if (IsDeleted)
  3195. return;
  3196. if ((RootPart.GetEffectiveObjectFlags() & (uint)PrimFlags.Phantom) != 0)
  3197. return;
  3198. // m_log.Debug("Processing CheckSculptAndLoad for {0} {1}", Name, LocalId);
  3199. SceneObjectPart[] parts = m_parts.GetArray();
  3200. for (int i = 0; i < parts.Length; i++)
  3201. parts[i].CheckSculptAndLoad();
  3202. }
  3203. */
  3204. /// <summary>
  3205. /// Set the user group to which this scene object belongs.
  3206. /// </summary>
  3207. /// <param name="GroupID"></param>
  3208. /// <param name="client"></param>
  3209. public void SetGroup(UUID GroupID, IClientAPI client)
  3210. {
  3211. SceneObjectPart[] parts = m_parts.GetArray();
  3212. for (int i = 0; i < parts.Length; i++)
  3213. {
  3214. SceneObjectPart part = parts[i];
  3215. part.SetGroup(GroupID, client);
  3216. part.Inventory.ChangeInventoryGroup(GroupID);
  3217. }
  3218. HasGroupChanged = true;
  3219. // Don't trigger the update here - otherwise some client issues occur when multiple updates are scheduled
  3220. // for the same object with very different properties. The caller must schedule the update.
  3221. //ScheduleGroupForFullUpdate();
  3222. }
  3223. public void TriggerScriptChangedEvent(Changed val)
  3224. {
  3225. SceneObjectPart[] parts = m_parts.GetArray();
  3226. for (int i = 0; i < parts.Length; i++)
  3227. parts[i].TriggerScriptChangedEvent(val);
  3228. }
  3229. /// <summary>
  3230. /// Returns a count of the number of scripts in this groups parts.
  3231. /// </summary>
  3232. public int ScriptCount()
  3233. {
  3234. int count = 0;
  3235. SceneObjectPart[] parts = m_parts.GetArray();
  3236. for (int i = 0; i < parts.Length; i++)
  3237. count += parts[i].Inventory.ScriptCount();
  3238. return count;
  3239. }
  3240. /// <summary>
  3241. /// A float the value is a representative execution time in milliseconds of all scripts in the link set.
  3242. /// </summary>
  3243. public float ScriptExecutionTime()
  3244. {
  3245. IScriptModule[] engines = Scene.RequestModuleInterfaces<IScriptModule>();
  3246. if (engines.Length == 0) // No engine at all
  3247. return 0.0f;
  3248. float time = 0.0f;
  3249. // get all the scripts in all parts
  3250. SceneObjectPart[] parts = m_parts.GetArray();
  3251. List<TaskInventoryItem> scripts = new List<TaskInventoryItem>();
  3252. for (int i = 0; i < parts.Length; i++)
  3253. {
  3254. scripts.AddRange(parts[i].Inventory.GetInventoryItems(InventoryType.LSL));
  3255. }
  3256. // extract the UUIDs
  3257. List<UUID> ids = new List<UUID>(scripts.Count);
  3258. foreach (TaskInventoryItem script in scripts)
  3259. {
  3260. if (!ids.Contains(script.ItemID))
  3261. {
  3262. ids.Add(script.ItemID);
  3263. }
  3264. }
  3265. // Offer the list of script UUIDs to each engine found and accumulate the time
  3266. foreach (IScriptModule e in engines)
  3267. {
  3268. if (e != null)
  3269. {
  3270. time += e.GetScriptExecutionTime(ids);
  3271. }
  3272. }
  3273. return time;
  3274. }
  3275. /// <summary>
  3276. /// Returns a count of the number of running scripts in this groups parts.
  3277. /// </summary>
  3278. public int RunningScriptCount()
  3279. {
  3280. int count = 0;
  3281. SceneObjectPart[] parts = m_parts.GetArray();
  3282. for (int i = 0; i < parts.Length; i++)
  3283. count += parts[i].Inventory.RunningScriptCount();
  3284. return count;
  3285. }
  3286. /// <summary>
  3287. /// Get a copy of the list of sitting avatars on all prims of this object.
  3288. /// </summary>
  3289. /// <remarks>
  3290. /// This is sorted by the order in which avatars sat down. If an avatar stands up then all avatars that sat
  3291. /// down after it move one place down the list.
  3292. /// </remarks>
  3293. /// <returns>A list of the sitting avatars. Returns an empty list if there are no sitting avatars.</returns>
  3294. public List<ScenePresence> GetSittingAvatars()
  3295. {
  3296. lock (m_sittingAvatars)
  3297. return new List<ScenePresence>(m_sittingAvatars);
  3298. }
  3299. /// <summary>
  3300. /// Gets the number of sitting avatars.
  3301. /// </summary>
  3302. /// <remarks>This applies to all sitting avatars whether there is a sit target set or not.</remarks>
  3303. /// <returns></returns>
  3304. public int GetSittingAvatarsCount()
  3305. {
  3306. lock (m_sittingAvatars)
  3307. return m_sittingAvatars.Count;
  3308. }
  3309. public override string ToString()
  3310. {
  3311. return String.Format("{0} {1} ({2})", Name, UUID, AbsolutePosition);
  3312. }
  3313. #region ISceneObject
  3314. public virtual ISceneObject CloneForNewScene()
  3315. {
  3316. SceneObjectGroup sog = Copy(false);
  3317. sog.IsDeleted = false;
  3318. return sog;
  3319. }
  3320. public virtual string ToXml2()
  3321. {
  3322. return SceneObjectSerializer.ToXml2Format(this);
  3323. }
  3324. public virtual string ExtraToXmlString()
  3325. {
  3326. return "<ExtraFromItemID>" + FromItemID.ToString() + "</ExtraFromItemID>";
  3327. }
  3328. public virtual void ExtraFromXmlString(string xmlstr)
  3329. {
  3330. string id = xmlstr.Substring(xmlstr.IndexOf("<ExtraFromItemID>"));
  3331. id = xmlstr.Replace("<ExtraFromItemID>", "");
  3332. id = id.Replace("</ExtraFromItemID>", "");
  3333. UUID uuid = UUID.Zero;
  3334. UUID.TryParse(id, out uuid);
  3335. FromItemID = uuid;
  3336. }
  3337. #endregion
  3338. }
  3339. }