SceneObjectGroup.cs 120 KB

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