SceneObjectGroup.cs 146 KB

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