SceneObjectGroup.cs 124 KB

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