SceneObjectGroup.cs 121 KB

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