SceneObjectGroup.cs 122 KB

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