SceneObjectGroup.cs 131 KB

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