SceneObjectGroup.cs 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998
  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 OpenSim 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.Xml;
  32. using System.Xml.Serialization;
  33. using OpenMetaverse;
  34. using OpenMetaverse.Packets;
  35. using OpenSim.Framework;
  36. using OpenSim.Region.Environment.Interfaces;
  37. using OpenSim.Region.Physics.Manager;
  38. namespace OpenSim.Region.Environment.Scenes
  39. {
  40. [Flags]
  41. public enum scriptEvents
  42. {
  43. None = 0,
  44. attach = 1,
  45. collision = 16,
  46. collision_end = 32,
  47. collision_start = 64,
  48. control = 128,
  49. dataserver = 256,
  50. email = 512,
  51. http_response = 1024,
  52. land_collision = 2048,
  53. land_collision_end = 4096,
  54. land_collision_start = 8192,
  55. at_target = 16384,
  56. listen = 32768,
  57. money = 65536,
  58. moving_end = 131072,
  59. moving_start = 262144,
  60. not_at_rot_target = 524288,
  61. not_at_target = 1048576,
  62. remote_data = 8388608,
  63. run_time_permissions = 268435456,
  64. state_entry = 1073741824,
  65. state_exit = 2,
  66. timer = 4,
  67. touch = 8,
  68. touch_end = 536870912,
  69. touch_start = 2097152,
  70. object_rez = 4194304
  71. }
  72. struct scriptPosTarget
  73. {
  74. public Vector3 targetPos;
  75. public float tolerance;
  76. }
  77. public delegate void PrimCountTaintedDelegate();
  78. /// <summary>
  79. /// A scene object group is conceptually an object in the scene. The object is constituted of SceneObjectParts
  80. /// (often known as prims), one of which is considered the root part.
  81. /// </summary>
  82. public partial class SceneObjectGroup : EntityBase
  83. {
  84. // private PrimCountTaintedDelegate handlerPrimCountTainted = null;
  85. /// <summary>
  86. /// Signal whether the non-inventory attributes of any prims in the group have changed
  87. /// since the group's last persistent backup
  88. /// </summary>
  89. private bool m_hasGroupChanged = false;
  90. private long timeFirstChanged;
  91. private long timeLastChanged;
  92. public bool HasGroupChanged
  93. {
  94. set
  95. {
  96. if (value)
  97. {
  98. timeLastChanged = DateTime.Now.Ticks;
  99. if (!m_hasGroupChanged)
  100. timeFirstChanged = DateTime.Now.Ticks;
  101. }
  102. m_hasGroupChanged = value;
  103. }
  104. get { return m_hasGroupChanged; }
  105. }
  106. private bool isTimeToPersist()
  107. {
  108. if (IsSelected || IsDeleted || IsAttachment)
  109. return false;
  110. if (!m_hasGroupChanged)
  111. return false;
  112. if (m_scene.ShuttingDown)
  113. return true;
  114. long currentTime = DateTime.Now.Ticks;
  115. if (currentTime - timeLastChanged > m_scene.m_dontPersistBefore || currentTime - timeFirstChanged > m_scene.m_persistAfter)
  116. return true;
  117. return false;
  118. }
  119. /// <value>
  120. /// Is this scene object acting as an attachment?
  121. ///
  122. /// We return false if the group has already been deleted.
  123. ///
  124. /// TODO: At the moment set must be done on the part itself. There may be a case for doing it here since I
  125. /// presume either all or no parts in a linkset can be part of an attachment (in which
  126. /// case the value would get proprogated down into all the descendent parts).
  127. /// </value>
  128. public bool IsAttachment
  129. {
  130. get
  131. {
  132. if (!IsDeleted)
  133. return m_rootPart.IsAttachment;
  134. return false;
  135. }
  136. }
  137. public float scriptScore = 0f;
  138. private Vector3 lastPhysGroupPos;
  139. private Quaternion lastPhysGroupRot;
  140. private bool m_isBackedUp = false;
  141. /// <summary>
  142. /// The constituent parts of this group
  143. /// </summary>
  144. protected Dictionary<UUID, SceneObjectPart> m_parts = new Dictionary<UUID, SceneObjectPart>();
  145. protected ulong m_regionHandle;
  146. protected SceneObjectPart m_rootPart;
  147. // private Dictionary<UUID, scriptEvents> m_scriptEvents = new Dictionary<UUID, scriptEvents>();
  148. private Dictionary<uint, scriptPosTarget> m_targets = new Dictionary<uint, scriptPosTarget>();
  149. private bool m_scriptListens_atTarget = false;
  150. private bool m_scriptListens_notAtTarget = false;
  151. #region Properties
  152. /// <summary>
  153. /// The name of an object grouping is always the same as its root part
  154. /// </summary>
  155. public override string Name
  156. {
  157. get {
  158. if (RootPart == null)
  159. return "";
  160. return RootPart.Name;
  161. }
  162. set { RootPart.Name = value; }
  163. }
  164. /// <summary>
  165. /// Added because the Parcel code seems to use it
  166. /// but not sure a object should have this
  167. /// as what does it tell us? that some avatar has selected it (but not what Avatar/user)
  168. /// think really there should be a list (or whatever) in each scenepresence
  169. /// saying what prim(s) that user has selected.
  170. /// </summary>
  171. protected bool m_isSelected = false;
  172. /// <summary>
  173. /// Number of prims in this group
  174. /// </summary>
  175. public int PrimCount
  176. {
  177. get { return m_parts.Count; }
  178. }
  179. public Quaternion GroupRotation
  180. {
  181. get { return m_rootPart.RotationOffset; }
  182. }
  183. public UUID GroupID
  184. {
  185. get { return m_rootPart.GroupID; }
  186. set { m_rootPart.GroupID = value; }
  187. }
  188. public Dictionary<UUID, SceneObjectPart> Children
  189. {
  190. get { return m_parts; }
  191. set { m_parts = value; }
  192. }
  193. /// <value>
  194. /// The root part of this scene object
  195. /// </value>
  196. public SceneObjectPart RootPart
  197. {
  198. get { return m_rootPart; }
  199. }
  200. public ulong RegionHandle
  201. {
  202. get { return m_regionHandle; }
  203. set
  204. {
  205. m_regionHandle = value;
  206. lock (m_parts)
  207. {
  208. foreach (SceneObjectPart part in m_parts.Values)
  209. {
  210. part.RegionHandle = m_regionHandle;
  211. }
  212. }
  213. }
  214. }
  215. /// <summary>
  216. /// The absolute position of this scene object in the scene
  217. /// </summary>
  218. public override Vector3 AbsolutePosition
  219. {
  220. get
  221. {
  222. if (m_rootPart == null)
  223. {
  224. throw new NullReferenceException(
  225. string.Format("[SCENE OBJECT GROUP]: Object {0} has no root part.", m_uuid));
  226. }
  227. return m_rootPart.GroupPosition;
  228. }
  229. set
  230. {
  231. Vector3 val = value;
  232. if ((val.X > 257f || val.X < -1f || val.Y > 257f || val.Y < -1f) && !IsAttachment)
  233. {
  234. m_scene.CrossPrimGroupIntoNewRegion(val, this, true);
  235. }
  236. lock (m_parts)
  237. {
  238. foreach (SceneObjectPart part in m_parts.Values)
  239. {
  240. part.GroupPosition = val;
  241. }
  242. }
  243. //if (m_rootPart.PhysActor != null)
  244. //{
  245. //m_rootPart.PhysActor.Position =
  246. //new PhysicsVector(m_rootPart.GroupPosition.X, m_rootPart.GroupPosition.Y,
  247. //m_rootPart.GroupPosition.Z);
  248. //m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  249. //}
  250. }
  251. }
  252. public override uint LocalId
  253. {
  254. get
  255. {
  256. if (m_rootPart == null)
  257. {
  258. m_log.Error("[SCENE OBJECT GROUP]: Unable to find the rootpart for a LocalId Request!");
  259. return 0;
  260. }
  261. return m_rootPart.LocalId;
  262. }
  263. set { m_rootPart.LocalId = value; }
  264. }
  265. public override UUID UUID
  266. {
  267. get {
  268. if (m_rootPart == null)
  269. {
  270. m_log.Error("Got a null rootpart while requesting UUID. Called from: ", new Exception());
  271. return UUID.Zero;
  272. }
  273. else return m_rootPart.UUID;
  274. }
  275. set { m_rootPart.UUID = value; }
  276. }
  277. public UUID OwnerID
  278. {
  279. get
  280. {
  281. if (m_rootPart == null)
  282. return UUID.Zero;
  283. return m_rootPart.OwnerID;
  284. }
  285. set { m_rootPart.OwnerID = value; }
  286. }
  287. public Color Color
  288. {
  289. get { return m_rootPart.Color; }
  290. set { m_rootPart.Color = value; }
  291. }
  292. public string Text
  293. {
  294. get {
  295. string returnstr = m_rootPart.Text;
  296. if (returnstr.Length > 255)
  297. {
  298. returnstr = returnstr.Substring(0, 255);
  299. }
  300. return returnstr;
  301. }
  302. set { m_rootPart.Text = value; }
  303. }
  304. protected virtual bool InSceneBackup
  305. {
  306. get { return true; }
  307. }
  308. public bool IsSelected
  309. {
  310. get { return m_isSelected; }
  311. set
  312. {
  313. m_isSelected = value;
  314. // Tell physics engine that group is selected
  315. if (m_rootPart != null && m_rootPart.PhysActor != null)
  316. {
  317. m_rootPart.PhysActor.Selected = value;
  318. // Pass it on to the children.
  319. foreach (SceneObjectPart child in Children.Values)
  320. {
  321. if (child.PhysActor != null)
  322. {
  323. child.PhysActor.Selected = value;
  324. }
  325. }
  326. }
  327. }
  328. }
  329. // The UUID for the Region this Object is in.
  330. public UUID RegionUUID
  331. {
  332. get
  333. {
  334. if (m_scene != null)
  335. {
  336. return m_scene.RegionInfo.RegionID;
  337. }
  338. return UUID.Zero;
  339. }
  340. }
  341. #endregion
  342. #region Constructors
  343. /// <summary>
  344. /// Constructor
  345. /// </summary>
  346. public SceneObjectGroup()
  347. {
  348. }
  349. /// <summary>
  350. /// This constructor creates a SceneObjectGroup using a pre-existing SceneObjectPart.
  351. /// The original SceneObjectPart will be used rather than a copy, preserving
  352. /// its existing localID and UUID.
  353. /// </summary>
  354. public SceneObjectGroup(SceneObjectPart part)
  355. {
  356. SetRootPart(part);
  357. }
  358. public SceneObjectGroup(string xmlData, bool isOriginalXmlFormat)
  359. : this(UUID.Zero, xmlData, isOriginalXmlFormat)
  360. {
  361. }
  362. /// <summary>
  363. /// Create an object using serialized data in OpenSim's original xml format.
  364. /// </summary>
  365. /// <param name="fromUserInventoryItemID">
  366. /// If applicable, the user inventory item id from which this object was rezzed. If not applicable then this
  367. /// should be UUID.Zero
  368. /// </param>
  369. /// <param name="xmlData"></param>
  370. /// <param name="isOriginalXmlFormat">
  371. /// This parameter only exists to separate the two different xml constructors. In the future, versions should
  372. /// be specified within the xml itself.
  373. /// </param>
  374. public SceneObjectGroup(UUID fromUserInventoryItemID, string xmlData, bool isOriginalXmlFormat)
  375. {
  376. if (!isOriginalXmlFormat)
  377. throw new Exception("This constructor must specify the xml is in OpenSim's original format");
  378. //m_log.DebugFormat("[SOG]: Starting deserialization of SOG");
  379. int time = System.Environment.TickCount;
  380. // libomv.types changes UUID to Guid
  381. xmlData = xmlData.Replace("<UUID>", "<Guid>");
  382. xmlData = xmlData.Replace("</UUID>", "</Guid>");
  383. // Handle Nested <UUID><UUID> property
  384. xmlData = xmlData.Replace("<Guid><Guid>", "<UUID><Guid>");
  385. xmlData = xmlData.Replace("</Guid></Guid>", "</Guid></UUID>");
  386. StringReader sr = new StringReader(xmlData);
  387. XmlTextReader reader = new XmlTextReader(sr);
  388. try
  389. {
  390. reader.Read();
  391. reader.ReadStartElement("SceneObjectGroup");
  392. reader.ReadStartElement("RootPart");
  393. SetRootPart(SceneObjectPart.FromXml(fromUserInventoryItemID, reader));
  394. reader.ReadEndElement();
  395. while (reader.Read())
  396. {
  397. switch (reader.NodeType)
  398. {
  399. case XmlNodeType.Element:
  400. if (reader.Name == "Part")
  401. {
  402. reader.Read();
  403. SceneObjectPart part = SceneObjectPart.FromXml(reader);
  404. // We reset the link number in order to make sure that the persisted linkset order is
  405. int linkNum = part.LinkNum;
  406. AddPart(part);
  407. part.LinkNum = linkNum;
  408. part.TrimPermissions();
  409. part.StoreUndoState();
  410. }
  411. break;
  412. case XmlNodeType.EndElement:
  413. break;
  414. }
  415. }
  416. }
  417. catch (XmlException e)
  418. {
  419. m_log.ErrorFormat("[SCENE]: Deserialization of xml failed with {0}. xml was {1}", e, xmlData);
  420. }
  421. reader.Close();
  422. sr.Close();
  423. m_log.DebugFormat("[SOG]: Finished deserialization of SOG {0}, {1}ms", Name, System.Environment.TickCount - time);
  424. }
  425. /// <summary>
  426. /// Create an object using serialized data in OpenSim's xml2 format.
  427. /// </summary>
  428. public SceneObjectGroup(string xmlData)
  429. {
  430. SetFromXml(xmlData);
  431. }
  432. protected void SetFromXml(string xmlData)
  433. {
  434. //m_log.DebugFormat("[SOG]: Starting deserialization of SOG");
  435. //int time = System.Environment.TickCount;
  436. // libomv.types changes UUID to Guid
  437. xmlData = xmlData.Replace("<UUID>", "<Guid>");
  438. xmlData = xmlData.Replace("</UUID>", "</Guid>");
  439. // Handle Nested <UUID><UUID> property
  440. xmlData = xmlData.Replace("<Guid><Guid>", "<UUID><Guid>");
  441. xmlData = xmlData.Replace("</Guid></Guid>", "</Guid></UUID>");
  442. StringReader sr = new StringReader(xmlData);
  443. XmlTextReader reader = new XmlTextReader(sr);
  444. reader.Read();
  445. reader.ReadStartElement("SceneObjectGroup");
  446. SetRootPart(CreatePartFromXml(reader));
  447. reader.Read();
  448. bool more = true;
  449. while (more)
  450. {
  451. switch (reader.NodeType)
  452. {
  453. case XmlNodeType.Element:
  454. if (reader.Name == "SceneObjectPart")
  455. {
  456. SceneObjectPart part = CreatePartFromXml(reader);
  457. AddPart(part);
  458. part.StoreUndoState();
  459. }
  460. else
  461. {
  462. Console.WriteLine("found unexpected element: " + reader.Name);
  463. reader.Read();
  464. }
  465. break;
  466. case XmlNodeType.EndElement:
  467. reader.Read();
  468. break;
  469. }
  470. more = !reader.EOF;
  471. }
  472. reader.Close();
  473. sr.Close();
  474. //m_log.DebugFormat("[SOG]: Finished deserialization of SOG {0}, {1}ms", Name, System.Environment.TickCount - time);
  475. }
  476. protected virtual SceneObjectPart CreatePartFromXml(XmlTextReader reader)
  477. {
  478. SceneObjectPart part = SceneObjectPart.FromXml(reader);
  479. return part;
  480. }
  481. /// <summary>
  482. /// Constructor. This object is added to the scene later via AttachToScene()
  483. /// </summary>
  484. public SceneObjectGroup(UUID ownerID, Vector3 pos, Quaternion rot, PrimitiveBaseShape shape)
  485. {
  486. Vector3 rootOffset = new Vector3(0, 0, 0);
  487. SetRootPart(new SceneObjectPart(ownerID, shape, pos, rot, rootOffset));
  488. }
  489. /// <summary>
  490. /// Constructor.
  491. /// </summary>
  492. public SceneObjectGroup(UUID ownerID, Vector3 pos, PrimitiveBaseShape shape)
  493. : this(ownerID, pos, Quaternion.Identity, shape)
  494. {
  495. }
  496. public void SetFromAssetID(UUID AssetId)
  497. {
  498. lock (m_parts)
  499. {
  500. foreach (SceneObjectPart part in m_parts.Values)
  501. {
  502. part.FromAssetID = AssetId;
  503. }
  504. }
  505. }
  506. public UUID GetFromAssetID()
  507. {
  508. if (m_rootPart != null)
  509. {
  510. return m_rootPart.FromAssetID;
  511. }
  512. return UUID.Zero;
  513. }
  514. /// <summary>
  515. /// Hooks this object up to the backup event so that it is persisted to the database when the update thread executes.
  516. /// </summary>
  517. public void AttachToBackup()
  518. {
  519. if (InSceneBackup)
  520. {
  521. //m_log.DebugFormat(
  522. // "[SCENE OBJECT GROUP]: Attaching object {0} {1} to scene presistence sweep", Name, UUID);
  523. if (!m_isBackedUp)
  524. m_scene.EventManager.OnBackup += ProcessBackup;
  525. m_isBackedUp = true;
  526. }
  527. }
  528. /// <summary>
  529. /// Attach this object to a scene. It will also now appear to agents.
  530. /// </summary>
  531. /// <param name="scene"></param>
  532. public void AttachToScene(Scene scene)
  533. {
  534. m_scene = scene;
  535. RegionHandle = m_scene.RegionInfo.RegionHandle;
  536. if (m_rootPart.Shape.PCode != 9 || m_rootPart.Shape.State == 0)
  537. m_rootPart.ParentID = 0;
  538. if (m_rootPart.LocalId==0)
  539. m_rootPart.LocalId = m_scene.AllocateLocalId();
  540. // No need to lock here since the object isn't yet in a scene
  541. foreach (SceneObjectPart part in m_parts.Values)
  542. {
  543. if (Object.ReferenceEquals(part, m_rootPart))
  544. continue;
  545. if (part.LocalId==0)
  546. part.LocalId = m_scene.AllocateLocalId();
  547. part.ParentID = m_rootPart.LocalId;
  548. //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);
  549. }
  550. ApplyPhysics(m_scene.m_physicalPrim);
  551. ScheduleGroupForFullUpdate();
  552. }
  553. public Vector3 GroupScale()
  554. {
  555. Vector3 minScale = new Vector3(Constants.RegionSize,Constants.RegionSize,Constants.RegionSize);
  556. Vector3 maxScale = new Vector3(0f,0f,0f);
  557. Vector3 finalScale = new Vector3(0.5f, 0.5f, 0.5f);
  558. lock (m_parts)
  559. {
  560. foreach (SceneObjectPart part in m_parts.Values)
  561. {
  562. Vector3 partscale = part.Scale;
  563. Vector3 partoffset = part.OffsetPosition;
  564. minScale.X = (partscale.X + partoffset.X < minScale.X) ? partscale.X + partoffset.X : minScale.X;
  565. minScale.Y = (partscale.Y + partoffset.Y < minScale.Y) ? partscale.X + partoffset.Y : minScale.Y;
  566. minScale.Z = (partscale.Z + partoffset.Z < minScale.Z) ? partscale.X + partoffset.Z : minScale.Z;
  567. maxScale.X = (partscale.X + partoffset.X > maxScale.X) ? partscale.X + partoffset.X : maxScale.X;
  568. maxScale.Y = (partscale.Y + partoffset.Y > maxScale.Y) ? partscale.Y + partoffset.Y : maxScale.Y;
  569. maxScale.Z = (partscale.Z + partoffset.Z > maxScale.Z) ? partscale.Z + partoffset.Z : maxScale.Z;
  570. }
  571. }
  572. finalScale.X = (minScale.X > maxScale.X) ? minScale.X : maxScale.X;
  573. finalScale.Y = (minScale.Y > maxScale.Y) ? minScale.Y : maxScale.Y;
  574. finalScale.Z = (minScale.Z > maxScale.Z) ? minScale.Z : maxScale.Z;
  575. return finalScale;
  576. }
  577. public EntityIntersection TestIntersection(Ray hRay, bool frontFacesOnly, bool faceCenters)
  578. {
  579. // We got a request from the inner_scene to raytrace along the Ray hRay
  580. // We're going to check all of the prim in this group for intersection with the ray
  581. // If we get a result, we're going to find the closest result to the origin of the ray
  582. // and send back the intersection information back to the innerscene.
  583. EntityIntersection returnresult = new EntityIntersection();
  584. lock (m_parts)
  585. {
  586. foreach (SceneObjectPart part in m_parts.Values)
  587. {
  588. // Temporary commented to stop compiler warning
  589. //Vector3 partPosition =
  590. // new Vector3(part.AbsolutePosition.X, part.AbsolutePosition.Y, part.AbsolutePosition.Z);
  591. Quaternion parentrotation = GroupRotation;
  592. // Telling the prim to raytrace.
  593. //EntityIntersection inter = part.TestIntersection(hRay, parentrotation);
  594. EntityIntersection inter = part.TestIntersectionOBB(hRay, parentrotation,frontFacesOnly, faceCenters);
  595. // This may need to be updated to the maximum draw distance possible..
  596. // We might (and probably will) be checking for prim creation from other sims
  597. // when the camera crosses the border.
  598. float idist = Constants.RegionSize;
  599. if (inter.HitTF)
  600. {
  601. // We need to find the closest prim to return to the testcaller along the ray
  602. if (inter.distance < idist)
  603. {
  604. returnresult.HitTF = true;
  605. returnresult.ipoint = inter.ipoint;
  606. returnresult.obj = part;
  607. returnresult.normal = inter.normal;
  608. returnresult.distance = inter.distance;
  609. }
  610. }
  611. }
  612. }
  613. return returnresult;
  614. }
  615. #endregion
  616. public string ToXmlString()
  617. {
  618. using (StringWriter sw = new StringWriter())
  619. {
  620. using (XmlTextWriter writer = new XmlTextWriter(sw))
  621. {
  622. ToXml(writer);
  623. }
  624. return sw.ToString();
  625. }
  626. }
  627. public void ToXml(XmlTextWriter writer)
  628. {
  629. m_log.DebugFormat("[SOG]: Starting serialization of {0}", Name);
  630. int time = System.Environment.TickCount;
  631. writer.WriteStartElement(String.Empty, "SceneObjectGroup", String.Empty);
  632. writer.WriteStartElement(String.Empty, "RootPart", String.Empty);
  633. m_rootPart.ToXml(writer);
  634. writer.WriteEndElement();
  635. writer.WriteStartElement(String.Empty, "OtherParts", String.Empty);
  636. lock (m_parts)
  637. {
  638. foreach (SceneObjectPart part in m_parts.Values)
  639. {
  640. if (part.UUID != m_rootPart.UUID)
  641. {
  642. writer.WriteStartElement(String.Empty, "Part", String.Empty);
  643. part.ToXml(writer);
  644. writer.WriteEndElement();
  645. }
  646. }
  647. }
  648. writer.WriteEndElement();
  649. writer.WriteEndElement();
  650. m_log.DebugFormat("[SOG]: Finished serialization of SOG {0}, {1}ms", Name, System.Environment.TickCount - time);
  651. }
  652. public string ToXmlString2()
  653. {
  654. using (StringWriter sw = new StringWriter())
  655. {
  656. using (XmlTextWriter writer = new XmlTextWriter(sw))
  657. {
  658. ToXml2(writer);
  659. }
  660. return sw.ToString();
  661. }
  662. }
  663. public void ToXml2(XmlTextWriter writer)
  664. {
  665. m_log.DebugFormat("[SOG]: Starting serialization of SOG {0} to XML2", Name);
  666. int time = System.Environment.TickCount;
  667. writer.WriteStartElement(String.Empty, "SceneObjectGroup", String.Empty);
  668. m_rootPart.ToXml(writer);
  669. writer.WriteStartElement(String.Empty, "OtherParts", String.Empty);
  670. lock (m_parts)
  671. {
  672. foreach (SceneObjectPart part in m_parts.Values)
  673. {
  674. if (part.UUID != m_rootPart.UUID)
  675. {
  676. part.ToXml(writer);
  677. }
  678. }
  679. }
  680. writer.WriteEndElement();
  681. writer.WriteEndElement();
  682. m_log.DebugFormat("[SOG]: Finished serialization of SOG {0} to XML2, {1}ms", Name, System.Environment.TickCount - time);
  683. }
  684. /// <summary>
  685. /// Attach this scene object to the given avatar.
  686. /// </summary>
  687. /// <param name="agentID"></param>
  688. /// <param name="attachmentpoint"></param>
  689. /// <param name="AttachOffset"></param>
  690. public void AttachToAgent(UUID agentID, uint attachmentpoint, Vector3 AttachOffset, bool silent)
  691. {
  692. ScenePresence avatar = m_scene.GetScenePresence(agentID);
  693. if (avatar != null)
  694. {
  695. // don't attach attachments to child agents
  696. if (avatar.IsChildAgent) return;
  697. DetachFromBackup();
  698. // Remove from database and parcel prim count
  699. //
  700. m_scene.DeleteFromStorage(UUID);
  701. m_scene.EventManager.TriggerParcelPrimCountTainted();
  702. m_rootPart.AttachedAvatar = agentID;
  703. if (m_rootPart.PhysActor != null)
  704. {
  705. m_scene.PhysicsScene.RemovePrim(m_rootPart.PhysActor);
  706. m_rootPart.PhysActor = null;
  707. }
  708. AbsolutePosition = AttachOffset;
  709. m_rootPart.AttachedPos = AttachOffset;
  710. m_rootPart.IsAttachment = true;
  711. m_rootPart.SetParentLocalId(avatar.LocalId);
  712. SetAttachmentPoint(Convert.ToByte(attachmentpoint));
  713. avatar.AddAttachment(this);
  714. if (!silent)
  715. {
  716. // Killing it here will cause the client to deselect it
  717. // It then reappears on the avatar, deselected
  718. // through the full update below
  719. //
  720. if (IsSelected)
  721. {
  722. m_scene.SendKillObject(m_rootPart.LocalId);
  723. }
  724. IsSelected = false; // fudge....
  725. ScheduleGroupForFullUpdate();
  726. }
  727. }
  728. }
  729. public byte GetAttachmentPoint()
  730. {
  731. if (m_rootPart != null)
  732. {
  733. return m_rootPart.Shape.State;
  734. }
  735. return (byte)0;
  736. }
  737. public void ClearPartAttachmentData()
  738. {
  739. SetAttachmentPoint((Byte)0);
  740. }
  741. public void DetachToGround()
  742. {
  743. ScenePresence avatar = m_scene.GetScenePresence(m_rootPart.AttachedAvatar);
  744. if (avatar == null)
  745. return;
  746. avatar.RemoveAttachment(this);
  747. Vector3 detachedpos = new Vector3(127f,127f,127f);
  748. if (avatar == null)
  749. return;
  750. detachedpos = avatar.AbsolutePosition;
  751. AbsolutePosition = detachedpos;
  752. m_rootPart.AttachedAvatar = UUID.Zero;
  753. m_rootPart.SetParentLocalId(0);
  754. SetAttachmentPoint((byte)0);
  755. m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_rootPart.VolumeDetectActive, m_scene.m_physicalPrim);
  756. HasGroupChanged = true;
  757. RootPart.Rezzed = DateTime.Now;
  758. RootPart.RemFlag(PrimFlags.TemporaryOnRez);
  759. AttachToBackup();
  760. m_scene.EventManager.TriggerParcelPrimCountTainted();
  761. m_rootPart.ScheduleFullUpdate();
  762. m_rootPart.ClearUndoState();
  763. }
  764. public void DetachToInventoryPrep()
  765. {
  766. ScenePresence avatar = m_scene.GetScenePresence(m_rootPart.AttachedAvatar);
  767. //Vector3 detachedpos = new Vector3(127f, 127f, 127f);
  768. if (avatar != null)
  769. {
  770. //detachedpos = avatar.AbsolutePosition;
  771. avatar.RemoveAttachment(this);
  772. }
  773. m_rootPart.AttachedAvatar = UUID.Zero;
  774. m_rootPart.SetParentLocalId(0);
  775. //m_rootPart.SetAttachmentPoint((byte)0);
  776. m_rootPart.IsAttachment = false;
  777. AbsolutePosition = m_rootPart.AttachedPos;
  778. //m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_scene.m_physicalPrim);
  779. //AttachToBackup();
  780. //m_rootPart.ScheduleFullUpdate();
  781. }
  782. /// <summary>
  783. ///
  784. /// </summary>
  785. /// <param name="part"></param>
  786. private void SetPartAsNonRoot(SceneObjectPart part)
  787. {
  788. part.ParentID = m_rootPart.LocalId;
  789. part.ClearUndoState();
  790. }
  791. public override void UpdateMovement()
  792. {
  793. lock (m_parts)
  794. {
  795. foreach (SceneObjectPart part in m_parts.Values)
  796. {
  797. part.UpdateMovement();
  798. }
  799. }
  800. }
  801. public float GetTimeDilation()
  802. {
  803. return m_scene.TimeDilation;
  804. }
  805. /// <summary>
  806. /// Added as a way for the storage provider to reset the scene,
  807. /// most likely a better way to do this sort of thing but for now...
  808. /// </summary>
  809. /// <param name="scene"></param>
  810. public void SetScene(Scene scene)
  811. {
  812. m_scene = scene;
  813. }
  814. /// <summary>
  815. /// Set a part to act as the root part for this scene object
  816. /// </summary>
  817. /// <param name="part"></param>
  818. public void SetRootPart(SceneObjectPart part)
  819. {
  820. part.SetParent(this);
  821. m_rootPart = part;
  822. if (!IsAttachment)
  823. part.ParentID = 0;
  824. part.LinkNum = 0;
  825. // No locking required since the SOG should not be in the scene yet - one can't change root parts after
  826. // the scene object has been attached to the scene
  827. m_parts.Add(m_rootPart.UUID, m_rootPart);
  828. }
  829. /// <summary>
  830. /// Add a new part to this scene object. The part must already be correctly configured.
  831. /// </summary>
  832. /// <param name="part"></param>
  833. public void AddPart(SceneObjectPart part)
  834. {
  835. lock (m_parts)
  836. {
  837. part.SetParent(this);
  838. m_parts.Add(part.UUID, part);
  839. part.LinkNum = m_parts.Count;
  840. if (part.LinkNum == 2 && RootPart != null)
  841. RootPart.LinkNum = 1;
  842. }
  843. }
  844. /// <summary>
  845. /// Make sure that every non root part has the proper parent root part local id
  846. /// </summary>
  847. private void UpdateParentIDs()
  848. {
  849. lock (m_parts)
  850. {
  851. foreach (SceneObjectPart part in m_parts.Values)
  852. {
  853. if (part.UUID != m_rootPart.UUID)
  854. {
  855. part.ParentID = m_rootPart.LocalId;
  856. }
  857. }
  858. }
  859. }
  860. public void RegenerateFullIDs()
  861. {
  862. lock (m_parts)
  863. {
  864. foreach (SceneObjectPart part in m_parts.Values)
  865. {
  866. part.UUID = UUID.Random();
  867. }
  868. }
  869. }
  870. // helper provided for parts.
  871. public int GetSceneMaxUndo()
  872. {
  873. if (m_scene != null)
  874. return m_scene.MaxUndoCount;
  875. return 5;
  876. }
  877. // justincc: I don't believe this hack is needed any longer, especially since the physics
  878. // parts of set AbsolutePosition were already commented out. By changing HasGroupChanged to false
  879. // this method was preventing proper reload of scene objects.
  880. // dahlia: I had to uncomment it, without it meshing was failing on some prims and objects
  881. // at region startup
  882. public void ResetChildPrimPhysicsPositions()
  883. {
  884. AbsolutePosition = AbsolutePosition; // could someone in the know please explain how this works?
  885. }
  886. public UUID GetPartsFullID(uint localID)
  887. {
  888. SceneObjectPart part = GetChildPart(localID);
  889. if (part != null)
  890. {
  891. return part.UUID;
  892. }
  893. return UUID.Zero;
  894. }
  895. public void ObjectGrabHandler(uint localId, Vector3 offsetPos, IClientAPI remoteClient)
  896. {
  897. if (m_rootPart.LocalId == localId)
  898. {
  899. OnGrabGroup(offsetPos, remoteClient);
  900. }
  901. else
  902. {
  903. SceneObjectPart part = GetChildPart(localId);
  904. OnGrabPart(part, offsetPos, remoteClient);
  905. }
  906. }
  907. public virtual void OnGrabPart(SceneObjectPart part, Vector3 offsetPos, IClientAPI remoteClient)
  908. {
  909. part.StoreUndoState();
  910. part.OnGrab(offsetPos, remoteClient);
  911. }
  912. public virtual void OnGrabGroup(Vector3 offsetPos, IClientAPI remoteClient)
  913. {
  914. m_scene.EventManager.TriggerGroupGrab(UUID, offsetPos, remoteClient.AgentId);
  915. }
  916. /// <summary>
  917. /// Delete this group from its scene and tell all the scene presences about that deletion.
  918. /// </summary>
  919. public void DeleteGroup(bool silent)
  920. {
  921. // We need to keep track of this state in case this group is still queued for backup.
  922. m_isDeleted = true;
  923. DetachFromBackup();
  924. lock (m_parts)
  925. {
  926. foreach (SceneObjectPart part in m_parts.Values)
  927. {
  928. // part.Inventory.RemoveScriptInstances();
  929. List<ScenePresence> avatars = Scene.GetScenePresences();
  930. for (int i = 0; i < avatars.Count; i++)
  931. {
  932. if (avatars[i].ParentID == LocalId)
  933. {
  934. avatars[i].StandUp();
  935. }
  936. if (!silent)
  937. {
  938. if (m_rootPart != null && part == m_rootPart)
  939. avatars[i].ControllingClient.SendKillObject(m_regionHandle, part.LocalId);
  940. }
  941. }
  942. }
  943. }
  944. }
  945. public void AddScriptLPS(int count)
  946. {
  947. if (scriptScore + count >= float.MaxValue - count)
  948. scriptScore = 0;
  949. scriptScore += (float)count;
  950. SceneGraph d = m_scene.m_sceneGraph;
  951. d.AddToScriptLPS(count);
  952. }
  953. public void AddActiveScriptCount(int count)
  954. {
  955. SceneGraph d = m_scene.m_sceneGraph;
  956. d.AddActiveScripts(count);
  957. }
  958. public void aggregateScriptEvents()
  959. {
  960. uint objectflagupdate=(uint)RootPart.GetEffectiveObjectFlags();
  961. scriptEvents aggregateScriptEvents=0;
  962. lock (m_parts)
  963. {
  964. foreach (SceneObjectPart part in m_parts.Values)
  965. {
  966. if (part == null)
  967. continue;
  968. if (part != RootPart)
  969. part.ObjectFlags = objectflagupdate;
  970. aggregateScriptEvents |= part.AggregateScriptEvents;
  971. }
  972. }
  973. if ((aggregateScriptEvents & scriptEvents.at_target) != 0)
  974. {
  975. m_scriptListens_atTarget = true;
  976. }
  977. else
  978. {
  979. m_scriptListens_atTarget = false;
  980. }
  981. if ((aggregateScriptEvents & scriptEvents.not_at_target) != 0)
  982. {
  983. m_scriptListens_notAtTarget = true;
  984. }
  985. else
  986. {
  987. m_scriptListens_notAtTarget = false;
  988. }
  989. if (m_scriptListens_atTarget || m_scriptListens_notAtTarget)
  990. {
  991. }
  992. else
  993. {
  994. lock (m_targets)
  995. m_targets.Clear();
  996. }
  997. ScheduleGroupForFullUpdate();
  998. }
  999. public override void SetText(string text, Vector3 color, double alpha)
  1000. {
  1001. Color = Color.FromArgb(0xff - (int) (alpha * 0xff),
  1002. (int) (color.X * 0xff),
  1003. (int) (color.Y * 0xff),
  1004. (int) (color.Z * 0xff));
  1005. Text = text;
  1006. HasGroupChanged = true;
  1007. m_rootPart.ScheduleFullUpdate();
  1008. }
  1009. /// <summary>
  1010. /// Apply physics to this group
  1011. /// </summary>
  1012. /// <param name="m_physicalPrim"></param>
  1013. public void ApplyPhysics(bool m_physicalPrim)
  1014. {
  1015. lock (m_parts)
  1016. {
  1017. if (m_parts.Count > 1)
  1018. {
  1019. m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_rootPart.VolumeDetectActive, m_physicalPrim);
  1020. foreach (SceneObjectPart part in m_parts.Values)
  1021. {
  1022. if (part.LocalId != m_rootPart.LocalId)
  1023. {
  1024. part.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), part.VolumeDetectActive, m_physicalPrim);
  1025. }
  1026. }
  1027. // Hack to get the physics scene geometries in the right spot
  1028. ResetChildPrimPhysicsPositions();
  1029. }
  1030. else
  1031. {
  1032. m_rootPart.ApplyPhysics(m_rootPart.GetEffectiveObjectFlags(), m_rootPart.VolumeDetectActive, m_physicalPrim);
  1033. }
  1034. }
  1035. }
  1036. public void SetOwnerId(UUID userId)
  1037. {
  1038. ForEachPart(delegate(SceneObjectPart part) { part.OwnerID = userId; });
  1039. }
  1040. public void ForEachPart(Action<SceneObjectPart> whatToDo)
  1041. {
  1042. lock (m_parts)
  1043. {
  1044. foreach (SceneObjectPart part in m_parts.Values)
  1045. {
  1046. whatToDo(part);
  1047. }
  1048. }
  1049. }
  1050. #region Events
  1051. /// <summary>
  1052. /// Processes backup.
  1053. /// </summary>
  1054. /// <param name="datastore"></param>
  1055. public void ProcessBackup(IRegionDataStore datastore)
  1056. {
  1057. if (!m_isBackedUp)
  1058. return;
  1059. // Since this is the top of the section of call stack for backing up a particular scene object, don't let
  1060. // any exception propogate upwards.
  1061. if (IsDeleted || UUID == UUID.Zero)
  1062. {
  1063. // DetachFromBackup();
  1064. return;
  1065. }
  1066. try
  1067. {
  1068. if (!m_scene.ShuttingDown) // if shutting down then there will be nothing to handle the return so leave till next restart
  1069. {
  1070. ILandObject parcel = m_scene.LandChannel.GetLandObject(
  1071. m_rootPart.GroupPosition.X, m_rootPart.GroupPosition.Y);
  1072. if (parcel != null && parcel.landData != null &&
  1073. parcel.landData.OtherCleanTime != 0)
  1074. {
  1075. if (parcel.landData.OwnerID != OwnerID &&
  1076. (parcel.landData.GroupID != GroupID ||
  1077. parcel.landData.GroupID == UUID.Zero))
  1078. {
  1079. if ((DateTime.Now - RootPart.Rezzed).TotalMinutes >
  1080. parcel.landData.OtherCleanTime)
  1081. {
  1082. DetachFromBackup();
  1083. m_log.InfoFormat("[SCENE]: Returning object {0} due to parcel auto return", RootPart.UUID.ToString());
  1084. m_scene.AddReturn(OwnerID, Name, AbsolutePosition, "parcel auto return");
  1085. m_scene.DeRezObject(null, RootPart.LocalId,
  1086. RootPart.GroupID, DeRezAction.Return, UUID.Zero);
  1087. return;
  1088. }
  1089. }
  1090. }
  1091. }
  1092. if (HasGroupChanged)
  1093. {
  1094. // don't backup while it's selected or you're asking for changes mid stream.
  1095. if (isTimeToPersist())
  1096. {
  1097. m_log.DebugFormat(
  1098. "[SCENE]: Storing {0}, {1} in {2}",
  1099. Name, UUID, m_scene.RegionInfo.RegionName);
  1100. SceneObjectGroup backup_group = Copy(OwnerID, GroupID, false);
  1101. backup_group.RootPart.Velocity = RootPart.Velocity;
  1102. backup_group.RootPart.Acceleration = RootPart.Acceleration;
  1103. backup_group.RootPart.AngularVelocity = RootPart.AngularVelocity;
  1104. backup_group.RootPart.ParticleSystem = RootPart.ParticleSystem;
  1105. HasGroupChanged = false;
  1106. datastore.StoreObject(backup_group, m_scene.RegionInfo.RegionID);
  1107. backup_group.ForEachPart(delegate(SceneObjectPart part) { part.Inventory.ProcessInventoryBackup(datastore); });
  1108. backup_group = null;
  1109. }
  1110. // else
  1111. // {
  1112. // m_log.DebugFormat(
  1113. // "[SCENE]: Did not update persistence of object {0} {1}, selected = {2}",
  1114. // Name, UUID, IsSelected);
  1115. // }
  1116. }
  1117. }
  1118. catch (Exception e)
  1119. {
  1120. m_log.ErrorFormat(
  1121. "[SCENE]: Storing of {0}, {1} in {2} failed with exception {3}\n\t{4}",
  1122. Name, UUID, m_scene.RegionInfo.RegionName, e, e.StackTrace);
  1123. }
  1124. }
  1125. #endregion
  1126. #region Client Updating
  1127. public void SendFullUpdateToClient(IClientAPI remoteClient)
  1128. {
  1129. SendPartFullUpdate(remoteClient, RootPart, m_scene.Permissions.GenerateClientFlags(remoteClient.AgentId, RootPart.UUID));
  1130. lock (m_parts)
  1131. {
  1132. foreach (SceneObjectPart part in m_parts.Values)
  1133. {
  1134. if (part != RootPart)
  1135. SendPartFullUpdate(remoteClient, part, m_scene.Permissions.GenerateClientFlags(remoteClient.AgentId, part.UUID));
  1136. }
  1137. }
  1138. }
  1139. /// <summary>
  1140. /// Send a full update to the client for the given part
  1141. /// </summary>
  1142. /// <param name="remoteClient"></param>
  1143. /// <param name="part"></param>
  1144. internal void SendPartFullUpdate(IClientAPI remoteClient, SceneObjectPart part, uint clientFlags)
  1145. {
  1146. if (m_rootPart != null && m_rootPart.UUID == part.UUID)
  1147. {
  1148. if (IsAttachment)
  1149. {
  1150. part.SendFullUpdateToClient(remoteClient, m_rootPart.AttachedPos, clientFlags);
  1151. }
  1152. else
  1153. {
  1154. part.SendFullUpdateToClient(remoteClient, AbsolutePosition, clientFlags);
  1155. }
  1156. }
  1157. else
  1158. {
  1159. part.SendFullUpdateToClient(remoteClient, clientFlags);
  1160. }
  1161. }
  1162. #endregion
  1163. #region Copying
  1164. /// <summary>
  1165. /// Duplicates this object, including operations such as physics set up and attaching to the backup event.
  1166. /// </summary>
  1167. /// <returns></returns>
  1168. public SceneObjectGroup Copy(UUID cAgentID, UUID cGroupID, bool userExposed)
  1169. {
  1170. SceneObjectGroup dupe = (SceneObjectGroup) MemberwiseClone();
  1171. dupe.m_isBackedUp = false;
  1172. dupe.m_parts = new Dictionary<UUID, SceneObjectPart>();
  1173. dupe.m_parts.Clear();
  1174. //dupe.OwnerID = AgentID;
  1175. //dupe.GroupID = GroupID;
  1176. dupe.AbsolutePosition = new Vector3(AbsolutePosition.X, AbsolutePosition.Y, AbsolutePosition.Z);
  1177. dupe.m_scene = m_scene;
  1178. dupe.m_regionHandle = m_regionHandle;
  1179. dupe.CopyRootPart(m_rootPart, OwnerID, GroupID, userExposed);
  1180. dupe.m_rootPart.LinkNum = m_rootPart.LinkNum;
  1181. if (userExposed)
  1182. dupe.m_rootPart.TrimPermissions();
  1183. /// may need to create a new Physics actor.
  1184. if (dupe.RootPart.PhysActor != null && userExposed)
  1185. {
  1186. PrimitiveBaseShape pbs = dupe.RootPart.Shape;
  1187. dupe.RootPart.PhysActor = m_scene.PhysicsScene.AddPrimShape(
  1188. dupe.RootPart.Name,
  1189. pbs,
  1190. new PhysicsVector(dupe.RootPart.AbsolutePosition.X, dupe.RootPart.AbsolutePosition.Y, dupe.RootPart.AbsolutePosition.Z),
  1191. new PhysicsVector(dupe.RootPart.Scale.X, dupe.RootPart.Scale.Y, dupe.RootPart.Scale.Z),
  1192. dupe.RootPart.RotationOffset,
  1193. dupe.RootPart.PhysActor.IsPhysical);
  1194. dupe.RootPart.PhysActor.LocalID = dupe.RootPart.LocalId;
  1195. dupe.RootPart.DoPhysicsPropertyUpdate(dupe.RootPart.PhysActor.IsPhysical, true);
  1196. }
  1197. // Now we've made a copy that replaces this one, we need to
  1198. // switch the owner to the person who did the copying
  1199. // Second Life copies an object and duplicates the first one in it's place
  1200. // So, we have to make a copy of this one, set it in it's place then set the owner on this one
  1201. if (userExposed)
  1202. {
  1203. SetRootPartOwner(m_rootPart, cAgentID, cGroupID);
  1204. m_rootPart.ScheduleFullUpdate();
  1205. }
  1206. List<SceneObjectPart> partList = new List<SceneObjectPart>(m_parts.Values);
  1207. partList.Sort(delegate(SceneObjectPart p1, SceneObjectPart p2)
  1208. {
  1209. return p1.LinkNum.CompareTo(p2.LinkNum);
  1210. }
  1211. );
  1212. foreach (SceneObjectPart part in partList)
  1213. {
  1214. if (part.UUID != m_rootPart.UUID)
  1215. {
  1216. SceneObjectPart newPart =
  1217. dupe.CopyPart(part, OwnerID, GroupID, userExposed);
  1218. newPart.LinkNum = part.LinkNum;
  1219. if (userExposed)
  1220. {
  1221. SetPartOwner(newPart, cAgentID, cGroupID);
  1222. newPart.ScheduleFullUpdate();
  1223. }
  1224. }
  1225. }
  1226. if (userExposed)
  1227. {
  1228. dupe.UpdateParentIDs();
  1229. dupe.HasGroupChanged = true;
  1230. dupe.AttachToBackup();
  1231. ScheduleGroupForFullUpdate();
  1232. }
  1233. return dupe;
  1234. }
  1235. /// <summary>
  1236. ///
  1237. /// </summary>
  1238. /// <param name="part"></param>
  1239. /// <param name="cAgentID"></param>
  1240. /// <param name="cGroupID"></param>
  1241. public void CopyRootPart(SceneObjectPart part, UUID cAgentID, UUID cGroupID, bool userExposed)
  1242. {
  1243. SetRootPart(part.Copy(m_scene.AllocateLocalId(), OwnerID, GroupID, m_parts.Count, userExposed));
  1244. }
  1245. public void ScriptSetPhysicsStatus(bool UsePhysics)
  1246. {
  1247. bool IsTemporary = ((RootPart.Flags & PrimFlags.TemporaryOnRez) != 0);
  1248. bool IsPhantom = ((RootPart.Flags & PrimFlags.Phantom) != 0);
  1249. bool IsVolumeDetect = RootPart.VolumeDetectActive;
  1250. UpdatePrimFlags(RootPart.LocalId, UsePhysics, IsTemporary, IsPhantom, IsVolumeDetect);
  1251. }
  1252. public void ScriptSetTemporaryStatus(bool TemporaryStatus)
  1253. {
  1254. bool UsePhysics = ((RootPart.Flags & PrimFlags.Physics) != 0);
  1255. bool IsPhantom = ((RootPart.Flags & PrimFlags.Phantom) != 0);
  1256. bool IsVolumeDetect = RootPart.VolumeDetectActive;
  1257. UpdatePrimFlags(RootPart.LocalId, UsePhysics, TemporaryStatus, IsPhantom, IsVolumeDetect);
  1258. }
  1259. public void ScriptSetPhantomStatus(bool PhantomStatus)
  1260. {
  1261. bool UsePhysics = ((RootPart.Flags & PrimFlags.Physics) != 0);
  1262. bool IsTemporary = ((RootPart.Flags & PrimFlags.TemporaryOnRez) != 0);
  1263. bool IsVolumeDetect = RootPart.VolumeDetectActive;
  1264. UpdatePrimFlags(RootPart.LocalId, UsePhysics, IsTemporary, PhantomStatus, IsVolumeDetect);
  1265. }
  1266. public void ScriptSetVolumeDetect(bool VDStatus)
  1267. {
  1268. bool UsePhysics = ((RootPart.Flags & PrimFlags.Physics) != 0);
  1269. bool IsTemporary = ((RootPart.Flags & PrimFlags.TemporaryOnRez) != 0);
  1270. bool IsPhantom = ((RootPart.Flags & PrimFlags.Phantom) != 0);
  1271. UpdatePrimFlags(RootPart.LocalId, UsePhysics, IsTemporary, IsPhantom, VDStatus);
  1272. /*
  1273. ScriptSetPhantomStatus(false); // What ever it was before, now it's not phantom anymore
  1274. if (PhysActor != null) // Should always be the case now
  1275. {
  1276. PhysActor.SetVolumeDetect(param);
  1277. }
  1278. if (param != 0)
  1279. AddFlag(PrimFlags.Phantom);
  1280. ScheduleFullUpdate();
  1281. */
  1282. }
  1283. public void applyImpulse(PhysicsVector impulse)
  1284. {
  1285. // We check if rootpart is null here because scripts don't delete if you delete the host.
  1286. // This means that unfortunately, we can pass a null physics actor to Simulate!
  1287. // Make sure we don't do that!
  1288. SceneObjectPart rootpart = m_rootPart;
  1289. if (rootpart != null)
  1290. {
  1291. if (rootpart.PhysActor != null)
  1292. {
  1293. if (IsAttachment)
  1294. {
  1295. ScenePresence avatar = m_scene.GetScenePresence(rootpart.AttachedAvatar);
  1296. if (avatar != null)
  1297. {
  1298. avatar.PushForce(impulse);
  1299. }
  1300. }
  1301. else
  1302. {
  1303. rootpart.PhysActor.AddForce(impulse,true);
  1304. m_scene.PhysicsScene.AddPhysicsActorTaint(rootpart.PhysActor);
  1305. }
  1306. }
  1307. }
  1308. }
  1309. public void applyAngularImpulse(PhysicsVector impulse)
  1310. {
  1311. // We check if rootpart is null here because scripts don't delete if you delete the host.
  1312. // This means that unfortunately, we can pass a null physics actor to Simulate!
  1313. // Make sure we don't do that!
  1314. SceneObjectPart rootpart = m_rootPart;
  1315. if (rootpart != null)
  1316. {
  1317. if (rootpart.PhysActor != null)
  1318. {
  1319. if (!IsAttachment)
  1320. {
  1321. rootpart.PhysActor.AddAngularForce(impulse, true);
  1322. m_scene.PhysicsScene.AddPhysicsActorTaint(rootpart.PhysActor);
  1323. }
  1324. }
  1325. }
  1326. }
  1327. public void setAngularImpulse(PhysicsVector impulse)
  1328. {
  1329. // We check if rootpart is null here because scripts don't delete if you delete the host.
  1330. // This means that unfortunately, we can pass a null physics actor to Simulate!
  1331. // Make sure we don't do that!
  1332. SceneObjectPart rootpart = m_rootPart;
  1333. if (rootpart != null)
  1334. {
  1335. if (rootpart.PhysActor != null)
  1336. {
  1337. if (!IsAttachment)
  1338. {
  1339. rootpart.PhysActor.Torque = impulse;
  1340. m_scene.PhysicsScene.AddPhysicsActorTaint(rootpart.PhysActor);
  1341. }
  1342. }
  1343. }
  1344. }
  1345. public Vector3 GetTorque()
  1346. {
  1347. // We check if rootpart is null here because scripts don't delete if you delete the host.
  1348. // This means that unfortunately, we can pass a null physics actor to Simulate!
  1349. // Make sure we don't do that!
  1350. SceneObjectPart rootpart = m_rootPart;
  1351. if (rootpart != null)
  1352. {
  1353. if (rootpart.PhysActor != null)
  1354. {
  1355. if (!IsAttachment)
  1356. {
  1357. PhysicsVector torque = rootpart.PhysActor.Torque;
  1358. return new Vector3(torque.X, torque.Y, torque.Z);
  1359. }
  1360. }
  1361. }
  1362. return Vector3.Zero;
  1363. }
  1364. public void moveToTarget(Vector3 target, float tau)
  1365. {
  1366. SceneObjectPart rootpart = m_rootPart;
  1367. if (rootpart != null)
  1368. {
  1369. if (rootpart.PhysActor != null)
  1370. {
  1371. rootpart.PhysActor.PIDTarget = new PhysicsVector(target.X, target.Y, target.Z);
  1372. rootpart.PhysActor.PIDTau = tau;
  1373. rootpart.PhysActor.PIDActive = true;
  1374. }
  1375. }
  1376. }
  1377. public void stopMoveToTarget()
  1378. {
  1379. SceneObjectPart rootpart = m_rootPart;
  1380. if (rootpart != null)
  1381. {
  1382. if (rootpart.PhysActor != null)
  1383. {
  1384. rootpart.PhysActor.PIDActive = false;
  1385. }
  1386. }
  1387. }
  1388. /// <summary>
  1389. /// Set the owner of the root part.
  1390. /// </summary>
  1391. /// <param name="part"></param>
  1392. /// <param name="cAgentID"></param>
  1393. /// <param name="cGroupID"></param>
  1394. public void SetRootPartOwner(SceneObjectPart part, UUID cAgentID, UUID cGroupID)
  1395. {
  1396. part.LastOwnerID = part.OwnerID;
  1397. part.OwnerID = cAgentID;
  1398. part.GroupID = cGroupID;
  1399. if (part.OwnerID != cAgentID)
  1400. {
  1401. // Apply Next Owner Permissions if we're not bypassing permissions
  1402. if (!m_scene.Permissions.BypassPermissions())
  1403. ApplyNextOwnerPermissions();
  1404. }
  1405. part.ScheduleFullUpdate();
  1406. }
  1407. /// <summary>
  1408. /// Make a copy of the given part.
  1409. /// </summary>
  1410. /// <param name="part"></param>
  1411. /// <param name="cAgentID"></param>
  1412. /// <param name="cGroupID"></param>
  1413. public SceneObjectPart CopyPart(SceneObjectPart part, UUID cAgentID, UUID cGroupID, bool userExposed)
  1414. {
  1415. SceneObjectPart newPart = part.Copy(m_scene.AllocateLocalId(), OwnerID, GroupID, m_parts.Count, userExposed);
  1416. newPart.SetParent(this);
  1417. lock (m_parts)
  1418. {
  1419. m_parts.Add(newPart.UUID, newPart);
  1420. }
  1421. SetPartAsNonRoot(newPart);
  1422. return newPart;
  1423. }
  1424. /// <summary>
  1425. /// Reset the UUIDs for all the prims that make up this group.
  1426. ///
  1427. /// This is called by methods which want to add a new group to an existing scene, in order
  1428. /// to ensure that there are no clashes with groups already present.
  1429. /// </summary>
  1430. public void ResetIDs()
  1431. {
  1432. // As this is only ever called for prims which are not currently part of the scene (and hence
  1433. // not accessible by clients), there should be no need to lock
  1434. List<SceneObjectPart> partsList = new List<SceneObjectPart>(m_parts.Values);
  1435. m_parts.Clear();
  1436. foreach (SceneObjectPart part in partsList)
  1437. {
  1438. part.ResetIDs(part.LinkNum); // Don't change link nums
  1439. m_parts.Add(part.UUID, part);
  1440. }
  1441. }
  1442. /// <summary>
  1443. ///
  1444. /// </summary>
  1445. /// <param name="part"></param>
  1446. public void ServiceObjectPropertiesFamilyRequest(IClientAPI remoteClient, UUID AgentID, uint RequestFlags)
  1447. {
  1448. remoteClient.SendObjectPropertiesFamilyData(RequestFlags, RootPart.UUID, RootPart.ObjectOwner, RootPart.GroupID, RootPart.BaseMask,
  1449. RootPart.OwnerMask, RootPart.GroupMask, RootPart.EveryoneMask, RootPart.NextOwnerMask,
  1450. RootPart.OwnershipCost, RootPart.ObjectSaleType, RootPart.SalePrice, RootPart.Category,
  1451. RootPart.CreatorID, RootPart.Name, RootPart.Description);
  1452. }
  1453. public void SetPartOwner(SceneObjectPart part, UUID cAgentID, UUID cGroupID)
  1454. {
  1455. part.OwnerID = cAgentID;
  1456. part.GroupID = cGroupID;
  1457. }
  1458. #endregion
  1459. #region Scheduling
  1460. public override void Update()
  1461. {
  1462. // Check that the group was not deleted before the scheduled update
  1463. // FIXME: This is merely a temporary measure to reduce the incidence of failure when
  1464. // an object has been deleted from a scene before update was processed.
  1465. // A more fundamental overhaul of the update mechanism is required to eliminate all
  1466. // the race conditions.
  1467. if (m_isDeleted)
  1468. return;
  1469. // This is what happens when an orphanced link set child prim's
  1470. // group was queued when it was linked
  1471. //
  1472. if (m_rootPart == null)
  1473. return;
  1474. lock (m_parts)
  1475. {
  1476. bool UsePhysics = ((RootPart.Flags & PrimFlags.Physics) != 0);
  1477. //if (IsAttachment)
  1478. //{
  1479. //foreach (SceneObjectPart part in m_parts.Values)
  1480. //{
  1481. //part.SendScheduledUpdates();
  1482. //}
  1483. //return;
  1484. //}
  1485. if ((Util.GetDistanceTo(lastPhysGroupPos, AbsolutePosition) > 0.02) && UsePhysics)
  1486. {
  1487. m_rootPart.UpdateFlag = 1;
  1488. lastPhysGroupPos = AbsolutePosition;
  1489. }
  1490. //foreach (SceneObjectPart part in m_parts.Values)
  1491. //{
  1492. //if (part.UpdateFlag == 0) part.UpdateFlag = 1;
  1493. //}
  1494. checkAtTargets();
  1495. if (((Math.Abs(lastPhysGroupRot.W - GroupRotation.W) > 0.1)
  1496. || (Math.Abs(lastPhysGroupRot.X - GroupRotation.X) > 0.1)
  1497. || (Math.Abs(lastPhysGroupRot.Y - GroupRotation.Y) > 0.1)
  1498. || (Math.Abs(lastPhysGroupRot.Z - GroupRotation.Z) > 0.1))
  1499. && UsePhysics)
  1500. {
  1501. m_rootPart.UpdateFlag = 1;
  1502. lastPhysGroupRot = GroupRotation;
  1503. }
  1504. foreach (SceneObjectPart part in m_parts.Values)
  1505. {
  1506. part.SendScheduledUpdates();
  1507. }
  1508. }
  1509. }
  1510. public void ScheduleFullUpdateToAvatar(ScenePresence presence)
  1511. {
  1512. RootPart.AddFullUpdateToAvatar(presence);
  1513. lock (m_parts)
  1514. {
  1515. foreach (SceneObjectPart part in m_parts.Values)
  1516. {
  1517. if (part != RootPart)
  1518. part.AddFullUpdateToAvatar(presence);
  1519. }
  1520. }
  1521. }
  1522. public void ScheduleTerseUpdateToAvatar(ScenePresence presence)
  1523. {
  1524. lock (m_parts)
  1525. {
  1526. foreach (SceneObjectPart part in m_parts.Values)
  1527. {
  1528. part.AddTerseUpdateToAvatar(presence);
  1529. }
  1530. }
  1531. }
  1532. /// <summary>
  1533. /// Schedule a full update for this scene object
  1534. /// </summary>
  1535. public void ScheduleGroupForFullUpdate()
  1536. {
  1537. checkAtTargets();
  1538. RootPart.ScheduleFullUpdate();
  1539. lock (m_parts)
  1540. {
  1541. foreach (SceneObjectPart part in m_parts.Values)
  1542. {
  1543. if (part != RootPart)
  1544. part.ScheduleFullUpdate();
  1545. }
  1546. }
  1547. }
  1548. /// <summary>
  1549. /// Schedule a terse update for this scene object
  1550. /// </summary>
  1551. public void ScheduleGroupForTerseUpdate()
  1552. {
  1553. lock (m_parts)
  1554. {
  1555. foreach (SceneObjectPart part in m_parts.Values)
  1556. {
  1557. part.ScheduleTerseUpdate();
  1558. }
  1559. }
  1560. }
  1561. /// <summary>
  1562. /// Immediately send a full update for this scene object.
  1563. /// </summary>
  1564. public void SendGroupFullUpdate()
  1565. {
  1566. if (IsDeleted)
  1567. return;
  1568. RootPart.SendFullUpdateToAllClients();
  1569. lock (m_parts)
  1570. {
  1571. foreach (SceneObjectPart part in m_parts.Values)
  1572. {
  1573. if (part != RootPart)
  1574. part.SendFullUpdateToAllClients();
  1575. }
  1576. }
  1577. }
  1578. public void QueueForUpdateCheck()
  1579. {
  1580. if (m_scene == null) // Need to check here as it's null during object creation
  1581. return;
  1582. m_scene.m_sceneGraph.AddToUpdateList(this);
  1583. }
  1584. /// <summary>
  1585. /// Immediately send a terse update for this scene object.
  1586. /// </summary>
  1587. public void SendGroupTerseUpdate()
  1588. {
  1589. if (IsDeleted)
  1590. return;
  1591. lock (m_parts)
  1592. {
  1593. foreach (SceneObjectPart part in m_parts.Values)
  1594. {
  1595. part.SendTerseUpdateToAllClients();
  1596. }
  1597. }
  1598. }
  1599. #endregion
  1600. #region SceneGroupPart Methods
  1601. /// <summary>
  1602. /// Get the child part by LinkNum
  1603. /// </summary>
  1604. /// <param name="linknum"></param>
  1605. /// <returns>null if no child part with that linknum or child part</returns>
  1606. public SceneObjectPart GetLinkNumPart(int linknum)
  1607. {
  1608. lock (m_parts)
  1609. {
  1610. foreach (SceneObjectPart part in m_parts.Values)
  1611. {
  1612. if (part.LinkNum == linknum)
  1613. {
  1614. return part;
  1615. }
  1616. }
  1617. }
  1618. return null;
  1619. }
  1620. /// <summary>
  1621. /// Get a child part with a given UUID
  1622. /// </summary>
  1623. /// <param name="primID"></param>
  1624. /// <returns>null if a child part with the primID was not found</returns>
  1625. public SceneObjectPart GetChildPart(UUID primID)
  1626. {
  1627. SceneObjectPart childPart = null;
  1628. if (m_parts.ContainsKey(primID))
  1629. {
  1630. childPart = m_parts[primID];
  1631. }
  1632. return childPart;
  1633. }
  1634. /// <summary>
  1635. /// Get a child part with a given local ID
  1636. /// </summary>
  1637. /// <param name="localID"></param>
  1638. /// <returns>null if a child part with the local ID was not found</returns>
  1639. public SceneObjectPart GetChildPart(uint localID)
  1640. {
  1641. //m_log.DebugFormat("Entered looking for {0}", localID);
  1642. lock (m_parts)
  1643. {
  1644. foreach (SceneObjectPart part in m_parts.Values)
  1645. {
  1646. //m_log.DebugFormat("Found {0}", part.LocalId);
  1647. if (part.LocalId == localID)
  1648. {
  1649. return part;
  1650. }
  1651. }
  1652. }
  1653. return null;
  1654. }
  1655. /// <summary>
  1656. /// Does this group contain the child prim
  1657. /// should be able to remove these methods once we have a entity index in scene
  1658. /// </summary>
  1659. /// <param name="primID"></param>
  1660. /// <returns></returns>
  1661. public bool HasChildPrim(UUID primID)
  1662. {
  1663. if (m_parts.ContainsKey(primID))
  1664. {
  1665. return true;
  1666. }
  1667. return false;
  1668. }
  1669. /// <summary>
  1670. /// Does this group contain the child prim
  1671. /// should be able to remove these methods once we have a entity index in scene
  1672. /// </summary>
  1673. /// <param name="localID"></param>
  1674. /// <returns></returns>
  1675. public bool HasChildPrim(uint localID)
  1676. {
  1677. //m_log.DebugFormat("Entered HasChildPrim looking for {0}", localID);
  1678. lock (m_parts)
  1679. {
  1680. foreach (SceneObjectPart part in m_parts.Values)
  1681. {
  1682. //m_log.DebugFormat("Found {0}", part.LocalId);
  1683. if (part.LocalId == localID)
  1684. {
  1685. return true;
  1686. }
  1687. }
  1688. }
  1689. return false;
  1690. }
  1691. #endregion
  1692. #region Packet Handlers
  1693. /// <summary>
  1694. /// Link the prims in a given group to this group
  1695. /// </summary>
  1696. /// <param name="objectGroup">The group of prims which should be linked to this group</param>
  1697. public void LinkToGroup(SceneObjectGroup objectGroup)
  1698. {
  1699. if (objectGroup.RootPart.UpdateFlag > 0)
  1700. {
  1701. // I've never actually seen this happen, though I think it's theoretically possible
  1702. m_log.WarnFormat(
  1703. "[SCENE OBJECT GROUP]: Aborted linking {0}, {1} to {2}, {3} as it has yet to finish delinking",
  1704. objectGroup.RootPart.Name, objectGroup.RootPart.UUID, RootPart.Name, RootPart.UUID);
  1705. return;
  1706. }
  1707. // m_log.DebugFormat(
  1708. // "[SCENE OBJECT GROUP]: Linking group with root part {0}, {1} to group with root part {2}, {3}",
  1709. // objectGroup.RootPart.Name, objectGroup.RootPart.UUID, RootPart.Name, RootPart.UUID);
  1710. SceneObjectPart linkPart = objectGroup.m_rootPart;
  1711. Vector3 oldGroupPosition = linkPart.GroupPosition;
  1712. Quaternion oldRootRotation = linkPart.RotationOffset;
  1713. linkPart.OffsetPosition = linkPart.GroupPosition - AbsolutePosition;
  1714. linkPart.GroupPosition = AbsolutePosition;
  1715. Vector3 axPos = linkPart.OffsetPosition;
  1716. Quaternion parentRot = m_rootPart.RotationOffset;
  1717. axPos *= Quaternion.Inverse(parentRot);
  1718. linkPart.OffsetPosition = axPos;
  1719. Quaternion oldRot = linkPart.RotationOffset;
  1720. Quaternion newRot = Quaternion.Inverse(parentRot) * oldRot;
  1721. linkPart.RotationOffset = newRot;
  1722. linkPart.ParentID = m_rootPart.LocalId;
  1723. if (m_rootPart.LinkNum == 0)
  1724. m_rootPart.LinkNum = 1;
  1725. lock (m_parts)
  1726. {
  1727. m_parts.Add(linkPart.UUID, linkPart);
  1728. // Insert in terms of link numbers, the new links
  1729. // before the current ones (with the exception of
  1730. // the root prim. Shuffle the old ones up
  1731. foreach (KeyValuePair<UUID, SceneObjectPart> kvp in m_parts)
  1732. {
  1733. if (kvp.Value.LinkNum != 1)
  1734. {
  1735. // Don't update root prim link number
  1736. kvp.Value.LinkNum += objectGroup.PrimCount;
  1737. }
  1738. }
  1739. linkPart.LinkNum = 2;
  1740. linkPart.SetParent(this);
  1741. linkPart.AddFlag(PrimFlags.CreateSelected);
  1742. //if (linkPart.PhysActor != null)
  1743. //{
  1744. // m_scene.PhysicsScene.RemovePrim(linkPart.PhysActor);
  1745. //linkPart.PhysActor = null;
  1746. //}
  1747. //TODO: rest of parts
  1748. int linkNum = 3;
  1749. foreach (SceneObjectPart part in objectGroup.Children.Values)
  1750. {
  1751. if (part.UUID != objectGroup.m_rootPart.UUID)
  1752. {
  1753. LinkNonRootPart(part, oldGroupPosition, oldRootRotation, linkNum++);
  1754. }
  1755. part.ClearUndoState();
  1756. }
  1757. }
  1758. m_scene.UnlinkSceneObject(objectGroup.UUID, true);
  1759. // objectGroup.Children.Clear();
  1760. // objectGroup.m_rootPart = null;
  1761. // TODO Deleting the original group object may cause problems later on if they have already
  1762. // made it into the update queue. However, sending out updates for those parts is now
  1763. // spurious, so it would be good not to send them at some point.
  1764. // The traffic caused is always going to be pretty minor, so it's not high priority
  1765. //objectGroup.DeleteGroup();
  1766. AttachToBackup();
  1767. HasGroupChanged = true;
  1768. ScheduleGroupForFullUpdate();
  1769. }
  1770. /// <summary>
  1771. /// Delink the given prim from this group. The delinked prim is established as
  1772. /// an independent SceneObjectGroup.
  1773. /// </summary>
  1774. /// <param name="partID"></param>
  1775. public void DelinkFromGroup(uint partID)
  1776. {
  1777. DelinkFromGroup(partID, true);
  1778. }
  1779. /// <summary>
  1780. /// Delink the given prim from this group. The delinked prim is established as
  1781. /// an independent SceneObjectGroup.
  1782. /// </summary>
  1783. /// <param name="partID"></param>
  1784. /// <param name="sendEvents"></param>
  1785. public void DelinkFromGroup(uint partID, bool sendEvents)
  1786. {
  1787. SceneObjectPart linkPart = GetChildPart(partID);
  1788. if (null != linkPart)
  1789. {
  1790. linkPart.ClearUndoState();
  1791. // m_log.DebugFormat(
  1792. // "[SCENE OBJECT GROUP]: Delinking part {0}, {1} from group with root part {2}, {3}",
  1793. // linkPart.Name, linkPart.UUID, RootPart.Name, RootPart.UUID);
  1794. Quaternion worldRot = linkPart.GetWorldRotation();
  1795. // Remove the part from this object
  1796. lock (m_parts)
  1797. {
  1798. m_parts.Remove(linkPart.UUID);
  1799. }
  1800. if (m_parts.Count == 1 && RootPart != null) //Single prim is left
  1801. RootPart.LinkNum = 0;
  1802. else
  1803. {
  1804. foreach (SceneObjectPart p in m_parts.Values)
  1805. {
  1806. if (p.LinkNum > linkPart.LinkNum)
  1807. p.LinkNum--;
  1808. }
  1809. }
  1810. linkPart.ParentID = 0;
  1811. linkPart.LinkNum = 0;
  1812. if (linkPart.PhysActor != null)
  1813. {
  1814. m_scene.PhysicsScene.RemovePrim(linkPart.PhysActor);
  1815. }
  1816. // We need to reset the child part's position
  1817. // ready for life as a separate object after being a part of another object
  1818. Quaternion parentRot = m_rootPart.RotationOffset;
  1819. Vector3 axPos = linkPart.OffsetPosition;
  1820. axPos *= parentRot;
  1821. linkPart.OffsetPosition = new Vector3(axPos.X, axPos.Y, axPos.Z);
  1822. linkPart.GroupPosition = AbsolutePosition + linkPart.OffsetPosition;
  1823. linkPart.OffsetPosition = new Vector3(0, 0, 0);
  1824. linkPart.RotationOffset = worldRot;
  1825. SceneObjectGroup objectGroup = new SceneObjectGroup(linkPart);
  1826. m_scene.AddNewSceneObject(objectGroup, true);
  1827. if (sendEvents)
  1828. linkPart.TriggerScriptChangedEvent(Changed.LINK);
  1829. linkPart.Rezzed = RootPart.Rezzed;
  1830. HasGroupChanged = true;
  1831. ScheduleGroupForFullUpdate();
  1832. }
  1833. else
  1834. {
  1835. m_log.InfoFormat("[SCENE OBJECT GROUP]: " +
  1836. "DelinkFromGroup(): Child prim {0} not found in object {1}, {2}",
  1837. partID, LocalId, UUID);
  1838. }
  1839. }
  1840. /// <summary>
  1841. /// Stop this object from being persisted over server restarts.
  1842. /// </summary>
  1843. /// <param name="objectGroup"></param>
  1844. public void DetachFromBackup()
  1845. {
  1846. if (m_isBackedUp)
  1847. m_scene.EventManager.OnBackup -= ProcessBackup;
  1848. m_isBackedUp = false;
  1849. }
  1850. private void LinkNonRootPart(SceneObjectPart part, Vector3 oldGroupPosition, Quaternion oldGroupRotation, int linkNum)
  1851. {
  1852. Quaternion parentRot = oldGroupRotation;
  1853. Quaternion oldRot = part.RotationOffset;
  1854. Quaternion worldRot = parentRot * oldRot;
  1855. parentRot = oldGroupRotation;
  1856. Vector3 axPos = part.OffsetPosition;
  1857. axPos *= parentRot;
  1858. part.OffsetPosition = axPos;
  1859. part.GroupPosition = oldGroupPosition + part.OffsetPosition;
  1860. part.OffsetPosition = Vector3.Zero;
  1861. part.RotationOffset = worldRot;
  1862. part.SetParent(this);
  1863. part.ParentID = m_rootPart.LocalId;
  1864. // Caller locks m_parts for us
  1865. m_parts.Add(part.UUID, part);
  1866. part.LinkNum = linkNum;
  1867. part.OffsetPosition = part.GroupPosition - AbsolutePosition;
  1868. Quaternion rootRotation = m_rootPart.RotationOffset;
  1869. Vector3 pos = part.OffsetPosition;
  1870. pos *= Quaternion.Inverse(rootRotation);
  1871. part.OffsetPosition = pos;
  1872. parentRot = m_rootPart.RotationOffset;
  1873. oldRot = part.RotationOffset;
  1874. Quaternion newRot = Quaternion.Inverse(parentRot) * oldRot;
  1875. part.RotationOffset = newRot;
  1876. }
  1877. /// <summary>
  1878. /// If object is physical, apply force to move it around
  1879. /// If object is not physical, just put it at the resulting location
  1880. /// </summary>
  1881. /// <param name="offset">Always seems to be 0,0,0, so ignoring</param>
  1882. /// <param name="pos">New position. We do the math here to turn it into a force</param>
  1883. /// <param name="remoteClient"></param>
  1884. public void GrabMovement(Vector3 offset, Vector3 pos, IClientAPI remoteClient)
  1885. {
  1886. if (m_scene.EventManager.TriggerGroupMove(UUID, pos))
  1887. {
  1888. if (m_rootPart.PhysActor != null)
  1889. {
  1890. if (m_rootPart.PhysActor.IsPhysical)
  1891. {
  1892. Vector3 llmoveforce = pos - AbsolutePosition;
  1893. PhysicsVector grabforce = new PhysicsVector(llmoveforce.X, llmoveforce.Y, llmoveforce.Z);
  1894. grabforce = (grabforce / 10) * m_rootPart.PhysActor.Mass;
  1895. m_rootPart.PhysActor.AddForce(grabforce,true);
  1896. m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  1897. }
  1898. else
  1899. {
  1900. //NonPhysicalGrabMovement(pos);
  1901. }
  1902. }
  1903. else
  1904. {
  1905. //NonPhysicalGrabMovement(pos);
  1906. }
  1907. }
  1908. }
  1909. public void NonPhysicalGrabMovement(Vector3 pos)
  1910. {
  1911. AbsolutePosition = pos;
  1912. m_rootPart.SendTerseUpdateToAllClients();
  1913. }
  1914. /// <summary>
  1915. /// Return metadata about a prim (name, description, sale price, etc.)
  1916. /// </summary>
  1917. /// <param name="client"></param>
  1918. public void GetProperties(IClientAPI client)
  1919. {
  1920. m_rootPart.GetProperties(client);
  1921. }
  1922. /// <summary>
  1923. /// Set the name of a prim
  1924. /// </summary>
  1925. /// <param name="name"></param>
  1926. /// <param name="localID"></param>
  1927. public void SetPartName(string name, uint localID)
  1928. {
  1929. SceneObjectPart part = GetChildPart(localID);
  1930. if (part != null)
  1931. {
  1932. part.Name = name;
  1933. }
  1934. }
  1935. public void SetPartDescription(string des, uint localID)
  1936. {
  1937. SceneObjectPart part = GetChildPart(localID);
  1938. if (part != null)
  1939. {
  1940. part.Description = des;
  1941. }
  1942. }
  1943. public void SetPartText(string text, uint localID)
  1944. {
  1945. SceneObjectPart part = GetChildPart(localID);
  1946. if (part != null)
  1947. {
  1948. part.SetText(text);
  1949. }
  1950. }
  1951. public void SetPartText(string text, UUID partID)
  1952. {
  1953. SceneObjectPart part = GetChildPart(partID);
  1954. if (part != null)
  1955. {
  1956. part.SetText(text);
  1957. }
  1958. }
  1959. public string GetPartName(uint localID)
  1960. {
  1961. SceneObjectPart part = GetChildPart(localID);
  1962. if (part != null)
  1963. {
  1964. return part.Name;
  1965. }
  1966. return String.Empty;
  1967. }
  1968. public string GetPartDescription(uint localID)
  1969. {
  1970. SceneObjectPart part = GetChildPart(localID);
  1971. if (part != null)
  1972. {
  1973. return part.Description;
  1974. }
  1975. return String.Empty;
  1976. }
  1977. /// <summary>
  1978. /// Update prim flags for this group.
  1979. /// </summary>
  1980. /// <param name="localID"></param>
  1981. /// <param name="type"></param>
  1982. /// <param name="inUse"></param>
  1983. /// <param name="data"></param>
  1984. public void UpdatePrimFlags(uint localID, bool UsePhysics, bool IsTemporary, bool IsPhantom, bool IsVolumeDetect)
  1985. {
  1986. SceneObjectPart selectionPart = GetChildPart(localID);
  1987. if (IsTemporary)
  1988. {
  1989. DetachFromBackup();
  1990. // Remove from database and parcel prim count
  1991. //
  1992. m_scene.DeleteFromStorage(UUID);
  1993. m_scene.EventManager.TriggerParcelPrimCountTainted();
  1994. }
  1995. if (selectionPart != null)
  1996. {
  1997. lock (m_parts)
  1998. {
  1999. foreach (SceneObjectPart part in m_parts.Values)
  2000. {
  2001. if (part.Scale.X > 10.0 || part.Scale.Y > 10.0 || part.Scale.Z > 10.0)
  2002. {
  2003. UsePhysics = false; // Reset physics
  2004. break;
  2005. }
  2006. }
  2007. foreach (SceneObjectPart part in m_parts.Values)
  2008. {
  2009. part.UpdatePrimFlags(UsePhysics, IsTemporary, IsPhantom, IsVolumeDetect);
  2010. }
  2011. }
  2012. }
  2013. }
  2014. public void UpdateExtraParam(uint localID, ushort type, bool inUse, byte[] data)
  2015. {
  2016. SceneObjectPart part = GetChildPart(localID);
  2017. if (part != null)
  2018. {
  2019. part.UpdateExtraParam(type, inUse, data);
  2020. }
  2021. }
  2022. /// <summary>
  2023. /// Get the parts of this scene object
  2024. /// </summary>
  2025. /// <returns></returns>
  2026. public SceneObjectPart[] GetParts()
  2027. {
  2028. int numParts = Children.Count;
  2029. SceneObjectPart[] partArray = new SceneObjectPart[numParts];
  2030. Children.Values.CopyTo(partArray, 0);
  2031. return partArray;
  2032. }
  2033. /// <summary>
  2034. /// Update the texture entry for this part
  2035. /// </summary>
  2036. /// <param name="localID"></param>
  2037. /// <param name="textureEntry"></param>
  2038. public void UpdateTextureEntry(uint localID, byte[] textureEntry)
  2039. {
  2040. SceneObjectPart part = GetChildPart(localID);
  2041. if (part != null)
  2042. {
  2043. part.UpdateTextureEntry(textureEntry);
  2044. }
  2045. }
  2046. public void UpdatePermissions(UUID AgentID, byte field, uint localID,
  2047. uint mask, byte addRemTF)
  2048. {
  2049. foreach (SceneObjectPart part in m_parts.Values)
  2050. part.UpdatePermissions(AgentID, field, localID, mask,
  2051. addRemTF);
  2052. HasGroupChanged = true;
  2053. }
  2054. #endregion
  2055. #region Shape
  2056. /// <summary>
  2057. ///
  2058. /// </summary>
  2059. /// <param name="shapeBlock"></param>
  2060. public void UpdateShape(ObjectShapePacket.ObjectDataBlock shapeBlock, uint localID)
  2061. {
  2062. SceneObjectPart part = GetChildPart(localID);
  2063. if (part != null)
  2064. {
  2065. part.UpdateShape(shapeBlock);
  2066. if (part.PhysActor != null)
  2067. m_scene.PhysicsScene.AddPhysicsActorTaint(part.PhysActor);
  2068. }
  2069. }
  2070. #endregion
  2071. #region Resize
  2072. /// <summary>
  2073. /// Resize the given part
  2074. /// </summary>
  2075. /// <param name="scale"></param>
  2076. /// <param name="localID"></param>
  2077. public void Resize(Vector3 scale, uint localID)
  2078. {
  2079. if (scale.X > m_scene.m_maxNonphys)
  2080. scale.X = m_scene.m_maxNonphys;
  2081. if (scale.Y > m_scene.m_maxNonphys)
  2082. scale.Y = m_scene.m_maxNonphys;
  2083. if (scale.Z > m_scene.m_maxNonphys)
  2084. scale.Z = m_scene.m_maxNonphys;
  2085. SceneObjectPart part = GetChildPart(localID);
  2086. if (part != null)
  2087. {
  2088. part.Resize(scale);
  2089. if (part.PhysActor != null)
  2090. {
  2091. if (part.PhysActor.IsPhysical)
  2092. {
  2093. if (scale.X > m_scene.m_maxPhys)
  2094. scale.X = m_scene.m_maxPhys;
  2095. if (scale.Y > m_scene.m_maxPhys)
  2096. scale.Y = m_scene.m_maxPhys;
  2097. if (scale.Z > m_scene.m_maxPhys)
  2098. scale.Z = m_scene.m_maxPhys;
  2099. }
  2100. part.PhysActor.Size =
  2101. new PhysicsVector(scale.X, scale.Y, scale.Z);
  2102. m_scene.PhysicsScene.AddPhysicsActorTaint(part.PhysActor);
  2103. }
  2104. //if (part.UUID != m_rootPart.UUID)
  2105. HasGroupChanged = true;
  2106. ScheduleGroupForFullUpdate();
  2107. //if (part.UUID == m_rootPart.UUID)
  2108. //{
  2109. //if (m_rootPart.PhysActor != null)
  2110. //{
  2111. //m_rootPart.PhysActor.Size =
  2112. //new PhysicsVector(m_rootPart.Scale.X, m_rootPart.Scale.Y, m_rootPart.Scale.Z);
  2113. //m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  2114. //}
  2115. //}
  2116. }
  2117. }
  2118. public void GroupResize(Vector3 scale, uint localID)
  2119. {
  2120. SceneObjectPart part = GetChildPart(localID);
  2121. if (part != null)
  2122. {
  2123. if (scale.X > m_scene.m_maxNonphys)
  2124. scale.X = m_scene.m_maxNonphys;
  2125. if (scale.Y > m_scene.m_maxNonphys)
  2126. scale.Y = m_scene.m_maxNonphys;
  2127. if (scale.Z > m_scene.m_maxNonphys)
  2128. scale.Z = m_scene.m_maxNonphys;
  2129. if (part.PhysActor != null && part.PhysActor.IsPhysical)
  2130. {
  2131. if (scale.X > m_scene.m_maxPhys)
  2132. scale.X = m_scene.m_maxPhys;
  2133. if (scale.Y > m_scene.m_maxPhys)
  2134. scale.Y = m_scene.m_maxPhys;
  2135. if (scale.Z > m_scene.m_maxPhys)
  2136. scale.Z = m_scene.m_maxPhys;
  2137. }
  2138. float x = (scale.X / part.Scale.X);
  2139. float y = (scale.Y / part.Scale.Y);
  2140. float z = (scale.Z / part.Scale.Z);
  2141. lock (m_parts)
  2142. {
  2143. if (x > 1.0f || y > 1.0f || z > 1.0f)
  2144. {
  2145. foreach (SceneObjectPart obPart in m_parts.Values)
  2146. {
  2147. if (obPart.UUID != m_rootPart.UUID)
  2148. {
  2149. Vector3 oldSize = new Vector3(obPart.Scale);
  2150. float f = 1.0f;
  2151. float a = 1.0f;
  2152. if (part.PhysActor != null && part.PhysActor.IsPhysical)
  2153. {
  2154. if (oldSize.X*x > m_scene.m_maxPhys)
  2155. {
  2156. f = m_scene.m_maxPhys / oldSize.X;
  2157. a = f / x;
  2158. x *= a;
  2159. y *= a;
  2160. z *= a;
  2161. }
  2162. if (oldSize.Y*y > m_scene.m_maxPhys)
  2163. {
  2164. f = m_scene.m_maxPhys / oldSize.Y;
  2165. a = f / y;
  2166. x *= a;
  2167. y *= a;
  2168. z *= a;
  2169. }
  2170. if (oldSize.Z*z > m_scene.m_maxPhys)
  2171. {
  2172. f = m_scene.m_maxPhys / oldSize.Z;
  2173. a = f / z;
  2174. x *= a;
  2175. y *= a;
  2176. z *= a;
  2177. }
  2178. }
  2179. else
  2180. {
  2181. if (oldSize.X*x > m_scene.m_maxNonphys)
  2182. {
  2183. f = m_scene.m_maxNonphys / oldSize.X;
  2184. a = f / x;
  2185. x *= a;
  2186. y *= a;
  2187. z *= a;
  2188. }
  2189. if (oldSize.Y*y > m_scene.m_maxNonphys)
  2190. {
  2191. f = m_scene.m_maxNonphys / oldSize.Y;
  2192. a = f / y;
  2193. x *= a;
  2194. y *= a;
  2195. z *= a;
  2196. }
  2197. if (oldSize.Z*z > m_scene.m_maxNonphys)
  2198. {
  2199. f = m_scene.m_maxNonphys / oldSize.Z;
  2200. a = f / z;
  2201. x *= a;
  2202. y *= a;
  2203. z *= a;
  2204. }
  2205. }
  2206. }
  2207. }
  2208. }
  2209. }
  2210. Vector3 prevScale = part.Scale;
  2211. prevScale.X *= x;
  2212. prevScale.Y *= y;
  2213. prevScale.Z *= z;
  2214. part.Resize(prevScale);
  2215. lock (m_parts)
  2216. {
  2217. foreach (SceneObjectPart obPart in m_parts.Values)
  2218. {
  2219. if (obPart.UUID != m_rootPart.UUID)
  2220. {
  2221. Vector3 currentpos = new Vector3(obPart.OffsetPosition);
  2222. currentpos.X *= x;
  2223. currentpos.Y *= y;
  2224. currentpos.Z *= z;
  2225. Vector3 newSize = new Vector3(obPart.Scale);
  2226. newSize.X *= x;
  2227. newSize.Y *= y;
  2228. newSize.Z *= z;
  2229. obPart.Resize(newSize);
  2230. obPart.UpdateOffSet(currentpos);
  2231. }
  2232. }
  2233. }
  2234. if (part.PhysActor != null)
  2235. {
  2236. part.PhysActor.Size =
  2237. new PhysicsVector(prevScale.X, prevScale.Y, prevScale.Z);
  2238. m_scene.PhysicsScene.AddPhysicsActorTaint(part.PhysActor);
  2239. }
  2240. HasGroupChanged = true;
  2241. ScheduleGroupForTerseUpdate();
  2242. }
  2243. }
  2244. #endregion
  2245. #region Position
  2246. /// <summary>
  2247. /// Move this scene object
  2248. /// </summary>
  2249. /// <param name="pos"></param>
  2250. public void UpdateGroupPosition(Vector3 pos)
  2251. {
  2252. if (m_scene.EventManager.TriggerGroupMove(UUID, pos))
  2253. {
  2254. if (IsAttachment)
  2255. {
  2256. m_rootPart.AttachedPos = pos;
  2257. }
  2258. AbsolutePosition = pos;
  2259. HasGroupChanged = true;
  2260. }
  2261. //we need to do a terse update even if the move wasn't allowed
  2262. // so that the position is reset in the client (the object snaps back)
  2263. ScheduleGroupForTerseUpdate();
  2264. }
  2265. /// <summary>
  2266. /// Update the position of a single part of this scene object
  2267. /// </summary>
  2268. /// <param name="pos"></param>
  2269. /// <param name="localID"></param>
  2270. public void UpdateSinglePosition(Vector3 pos, uint localID)
  2271. {
  2272. SceneObjectPart part = GetChildPart(localID);
  2273. if (part != null)
  2274. {
  2275. if (part.UUID == m_rootPart.UUID)
  2276. {
  2277. UpdateRootPosition(pos);
  2278. }
  2279. else
  2280. {
  2281. part.UpdateOffSet(pos);
  2282. }
  2283. HasGroupChanged = true;
  2284. }
  2285. }
  2286. /// <summary>
  2287. ///
  2288. /// </summary>
  2289. /// <param name="pos"></param>
  2290. private void UpdateRootPosition(Vector3 pos)
  2291. {
  2292. Vector3 newPos = new Vector3(pos.X, pos.Y, pos.Z);
  2293. Vector3 oldPos =
  2294. new Vector3(AbsolutePosition.X + m_rootPart.OffsetPosition.X,
  2295. AbsolutePosition.Y + m_rootPart.OffsetPosition.Y,
  2296. AbsolutePosition.Z + m_rootPart.OffsetPosition.Z);
  2297. Vector3 diff = oldPos - newPos;
  2298. Vector3 axDiff = new Vector3(diff.X, diff.Y, diff.Z);
  2299. Quaternion partRotation = m_rootPart.RotationOffset;
  2300. axDiff *= Quaternion.Inverse(partRotation);
  2301. diff = axDiff;
  2302. lock (m_parts)
  2303. {
  2304. foreach (SceneObjectPart obPart in m_parts.Values)
  2305. {
  2306. if (obPart.UUID != m_rootPart.UUID)
  2307. {
  2308. obPart.OffsetPosition = obPart.OffsetPosition + diff;
  2309. }
  2310. }
  2311. }
  2312. AbsolutePosition = newPos;
  2313. HasGroupChanged = true;
  2314. ScheduleGroupForTerseUpdate();
  2315. }
  2316. public void OffsetForNewRegion(Vector3 offset)
  2317. {
  2318. m_rootPart.GroupPosition = offset;
  2319. }
  2320. #endregion
  2321. #region Rotation
  2322. /// <summary>
  2323. ///
  2324. /// </summary>
  2325. /// <param name="rot"></param>
  2326. public void UpdateGroupRotation(Quaternion rot)
  2327. {
  2328. m_rootPart.UpdateRotation(rot);
  2329. if (m_rootPart.PhysActor != null)
  2330. {
  2331. m_rootPart.PhysActor.Orientation = m_rootPart.RotationOffset;
  2332. m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  2333. }
  2334. HasGroupChanged = true;
  2335. ScheduleGroupForTerseUpdate();
  2336. }
  2337. /// <summary>
  2338. ///
  2339. /// </summary>
  2340. /// <param name="pos"></param>
  2341. /// <param name="rot"></param>
  2342. public void UpdateGroupRotation(Vector3 pos, Quaternion rot)
  2343. {
  2344. m_rootPart.UpdateRotation(rot);
  2345. if (m_rootPart.PhysActor != null)
  2346. {
  2347. m_rootPart.PhysActor.Orientation = m_rootPart.RotationOffset;
  2348. m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  2349. }
  2350. AbsolutePosition = pos;
  2351. HasGroupChanged = true;
  2352. ScheduleGroupForTerseUpdate();
  2353. }
  2354. /// <summary>
  2355. ///
  2356. /// </summary>
  2357. /// <param name="rot"></param>
  2358. /// <param name="localID"></param>
  2359. public void UpdateSingleRotation(Quaternion rot, uint localID)
  2360. {
  2361. SceneObjectPart part = GetChildPart(localID);
  2362. if (part != null)
  2363. {
  2364. if (part.UUID == m_rootPart.UUID)
  2365. {
  2366. UpdateRootRotation(rot);
  2367. }
  2368. else
  2369. {
  2370. part.UpdateRotation(rot);
  2371. }
  2372. }
  2373. }
  2374. /// <summary>
  2375. ///
  2376. /// </summary>
  2377. /// <param name="rot"></param>
  2378. private void UpdateRootRotation(Quaternion rot)
  2379. {
  2380. Quaternion axRot = rot;
  2381. Quaternion oldParentRot = m_rootPart.RotationOffset;
  2382. m_rootPart.UpdateRotation(rot);
  2383. if (m_rootPart.PhysActor != null)
  2384. {
  2385. m_rootPart.PhysActor.Orientation = m_rootPart.RotationOffset;
  2386. m_scene.PhysicsScene.AddPhysicsActorTaint(m_rootPart.PhysActor);
  2387. }
  2388. lock (m_parts)
  2389. {
  2390. foreach (SceneObjectPart prim in m_parts.Values)
  2391. {
  2392. if (prim.UUID != m_rootPart.UUID)
  2393. {
  2394. Vector3 axPos = prim.OffsetPosition;
  2395. axPos *= oldParentRot;
  2396. axPos *= Quaternion.Inverse(axRot);
  2397. prim.OffsetPosition = axPos;
  2398. Quaternion primsRot = prim.RotationOffset;
  2399. Quaternion newRot = primsRot * oldParentRot;
  2400. newRot *= Quaternion.Inverse(axRot);
  2401. prim.RotationOffset = newRot;
  2402. prim.ScheduleTerseUpdate();
  2403. }
  2404. }
  2405. }
  2406. m_rootPart.ScheduleTerseUpdate();
  2407. }
  2408. #endregion
  2409. internal void SetAxisRotation(int axis, int rotate10)
  2410. {
  2411. bool setX = false;
  2412. bool setY = false;
  2413. bool setZ = false;
  2414. int xaxis = 2;
  2415. int yaxis = 4;
  2416. int zaxis = 8;
  2417. if (m_rootPart != null)
  2418. {
  2419. setX = ((axis & xaxis) != 0) ? true : false;
  2420. setY = ((axis & yaxis) != 0) ? true : false;
  2421. setZ = ((axis & zaxis) != 0) ? true : false;
  2422. float setval = (rotate10 > 0) ? 1f : 0f;
  2423. if (setX)
  2424. m_rootPart.RotationAxis.X = setval;
  2425. if (setY)
  2426. m_rootPart.RotationAxis.Y = setval;
  2427. if (setZ)
  2428. m_rootPart.RotationAxis.Z = setval;
  2429. if (setX || setY || setZ)
  2430. {
  2431. m_rootPart.SetPhysicsAxisRotation();
  2432. }
  2433. }
  2434. }
  2435. public int registerTargetWaypoint(Vector3 target, float tolerance)
  2436. {
  2437. scriptPosTarget waypoint = new scriptPosTarget();
  2438. waypoint.targetPos = target;
  2439. waypoint.tolerance = tolerance;
  2440. uint handle = m_scene.AllocateLocalId();
  2441. lock (m_targets)
  2442. {
  2443. m_targets.Add(handle, waypoint);
  2444. }
  2445. return (int)handle;
  2446. }
  2447. public void unregisterTargetWaypoint(int handle)
  2448. {
  2449. lock (m_targets)
  2450. {
  2451. if (m_targets.ContainsKey((uint)handle))
  2452. m_targets.Remove((uint)handle);
  2453. }
  2454. }
  2455. private void checkAtTargets()
  2456. {
  2457. if (m_scriptListens_atTarget || m_scriptListens_notAtTarget)
  2458. {
  2459. if (m_targets.Count > 0)
  2460. {
  2461. bool at_target = false;
  2462. //Vector3 targetPos;
  2463. //uint targetHandle;
  2464. Dictionary<uint, scriptPosTarget> atTargets = new Dictionary<uint, scriptPosTarget>();
  2465. lock (m_targets)
  2466. {
  2467. foreach (uint idx in m_targets.Keys)
  2468. {
  2469. scriptPosTarget target = m_targets[idx];
  2470. if (Util.GetDistanceTo(target.targetPos, m_rootPart.GroupPosition) <= target.tolerance)
  2471. {
  2472. // trigger at_target
  2473. if (m_scriptListens_atTarget)
  2474. {
  2475. // Reusing att.tolerance to hold the index of the target in the targets dictionary
  2476. // to avoid deadlocking the sim.
  2477. at_target = true;
  2478. scriptPosTarget att = new scriptPosTarget();
  2479. att.targetPos = target.targetPos;
  2480. att.tolerance = (float)idx;
  2481. atTargets.Add(idx, att);
  2482. }
  2483. }
  2484. }
  2485. }
  2486. if (atTargets.Count > 0)
  2487. {
  2488. uint[] localids = new uint[0];
  2489. lock (m_parts)
  2490. {
  2491. localids = new uint[m_parts.Count];
  2492. int cntr = 0;
  2493. foreach (SceneObjectPart part in m_parts.Values)
  2494. {
  2495. localids[cntr] = part.LocalId;
  2496. cntr++;
  2497. }
  2498. }
  2499. for (int ctr = 0; ctr < localids.Length; ctr++)
  2500. {
  2501. foreach (uint target in atTargets.Keys)
  2502. {
  2503. scriptPosTarget att = atTargets[target];
  2504. // Reusing att.tolerance to hold the index of the target in the targets dictionary
  2505. // to avoid deadlocking the sim.
  2506. m_scene.TriggerAtTargetEvent(localids[ctr], (uint)att.tolerance, att.targetPos, m_rootPart.GroupPosition);
  2507. }
  2508. }
  2509. return;
  2510. }
  2511. if (m_scriptListens_notAtTarget && !at_target)
  2512. {
  2513. //trigger not_at_target
  2514. uint[] localids = new uint[0];
  2515. lock (m_parts)
  2516. {
  2517. localids = new uint[m_parts.Count];
  2518. int cntr = 0;
  2519. foreach (SceneObjectPart part in m_parts.Values)
  2520. {
  2521. localids[cntr] = part.LocalId;
  2522. cntr++;
  2523. }
  2524. }
  2525. for (int ctr = 0; ctr < localids.Length; ctr++)
  2526. {
  2527. m_scene.TriggerNotAtTargetEvent(localids[ctr]);
  2528. }
  2529. }
  2530. }
  2531. }
  2532. }
  2533. public float GetMass()
  2534. {
  2535. float retmass = 0f;
  2536. lock (m_parts)
  2537. {
  2538. foreach (SceneObjectPart part in m_parts.Values)
  2539. {
  2540. retmass += part.GetMass();
  2541. }
  2542. }
  2543. return retmass;
  2544. }
  2545. public void CheckSculptAndLoad()
  2546. {
  2547. lock (m_parts)
  2548. {
  2549. if (!IsDeleted)
  2550. {
  2551. if ((RootPart.GetEffectiveObjectFlags() & (uint)PrimFlags.Phantom) == 0)
  2552. {
  2553. foreach (SceneObjectPart part in m_parts.Values)
  2554. {
  2555. if (part.Shape.SculptEntry && part.Shape.SculptTexture != UUID.Zero)
  2556. {
  2557. m_scene.AssetCache.GetAsset(part.Shape.SculptTexture, part.SculptTextureCallback, true);
  2558. }
  2559. }
  2560. }
  2561. }
  2562. }
  2563. }
  2564. /// <summary>
  2565. /// Set the user group to which this scene object belongs.
  2566. /// </summary>
  2567. /// <param name="GroupID"></param>
  2568. /// <param name="client"></param>
  2569. public void SetGroup(UUID GroupID, IClientAPI client)
  2570. {
  2571. lock (m_parts)
  2572. {
  2573. foreach (SceneObjectPart part in m_parts.Values)
  2574. {
  2575. part.SetGroup(GroupID, client);
  2576. part.Inventory.ChangeInventoryGroup(GroupID);
  2577. }
  2578. HasGroupChanged = true;
  2579. }
  2580. ScheduleGroupForFullUpdate();
  2581. }
  2582. public void TriggerScriptChangedEvent(Changed val)
  2583. {
  2584. foreach (SceneObjectPart part in Children.Values)
  2585. {
  2586. part.TriggerScriptChangedEvent(val);
  2587. }
  2588. }
  2589. public override string ToString()
  2590. {
  2591. return String.Format("{0} {1} ({2})", Name, UUID, AbsolutePosition);
  2592. }
  2593. public void SetAttachmentPoint(byte point)
  2594. {
  2595. lock (m_parts)
  2596. {
  2597. foreach (SceneObjectPart part in m_parts.Values)
  2598. part.SetAttachmentPoint(point);
  2599. }
  2600. }
  2601. }
  2602. }