SceneObjectGroup.cs 122 KB

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