SceneObjectGroup.cs 146 KB

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