SceneObjectGroup.cs 132 KB

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