SceneObjectGroup.cs 120 KB

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