1
0

SceneObjectGroup.cs 148 KB

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