SceneObjectGroup.cs 122 KB

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