SceneObjectGroup.cs 123 KB

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