SceneObjectPart.cs 84 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423
  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 OpenSim 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.Xml;
  31. using System.Xml.Serialization;
  32. using Axiom.Math;
  33. using libsecondlife;
  34. using libsecondlife.Packets;
  35. using OpenSim.Framework;
  36. using OpenSim.Framework.Console;
  37. using OpenSim.Region.Environment.Interfaces;
  38. using OpenSim.Region.Environment.Scenes.Scripting;
  39. using OpenSim.Region.Physics.Manager;
  40. using System.Runtime.Serialization;
  41. using System.Security.Permissions;
  42. namespace OpenSim.Region.Environment.Scenes
  43. {
  44. // I don't really know where to put this except here.
  45. // Can't access the OpenSim.Region.ScriptEngine.Common.LSL_BaseClass.Changed constants
  46. [Flags]
  47. public enum ExtraParamType
  48. {
  49. Something1 = 1,
  50. Something2 = 2,
  51. Something3 = 4,
  52. Something4 = 8,
  53. Flexible = 16,
  54. Light = 32,
  55. Sculpt = 48,
  56. Something5 = 64,
  57. Something6 = 128
  58. }
  59. [Flags]
  60. public enum Changed : uint
  61. {
  62. INVENTORY = 1,
  63. COLOR = 2,
  64. SHAPE = 4,
  65. SCALE = 8,
  66. TEXTURE = 16,
  67. LINK = 32,
  68. ALLOWED_DROP = 64,
  69. OWNER = 128
  70. }
  71. [Flags]
  72. public enum TextureAnimFlags : byte
  73. {
  74. NONE = 0x00,
  75. ANIM_ON = 0x01,
  76. LOOP = 0x02,
  77. REVERSE = 0x04,
  78. PING_PONG = 0x08,
  79. SMOOTH = 0x10,
  80. ROTATE = 0x20,
  81. SCALE = 0x40
  82. }
  83. [Serializable]
  84. public partial class SceneObjectPart : IScriptHost, ISerializable
  85. {
  86. [XmlIgnore] public PhysicsActor PhysActor = null;
  87. public LLUUID LastOwnerID;
  88. public LLUUID OwnerID;
  89. public LLUUID GroupID;
  90. public int OwnershipCost;
  91. public byte ObjectSaleType;
  92. public int SalePrice;
  93. public uint Category;
  94. public Int32 CreationDate;
  95. public uint ParentID = 0;
  96. private PhysicsVector m_lastRotationalVelocity = PhysicsVector.Zero;
  97. private Vector3 m_sitTargetPosition = new Vector3(0, 0, 0);
  98. private Quaternion m_sitTargetOrientation = new Quaternion(0, 0, 0, 1);
  99. private LLUUID m_sitTargetAvatar = LLUUID.Zero;
  100. #region Permissions
  101. public uint BaseMask = (uint)PermissionMask.All;
  102. public uint OwnerMask = (uint)PermissionMask.All;
  103. public uint GroupMask = (uint)PermissionMask.None;
  104. public uint EveryoneMask = (uint)PermissionMask.None;
  105. public uint NextOwnerMask = (uint)PermissionMask.All;
  106. public LLObject.ObjectFlags Flags = LLObject.ObjectFlags.None;
  107. public uint ObjectFlags
  108. {
  109. get { return (uint)Flags; }
  110. set { Flags = (LLObject.ObjectFlags)value; }
  111. }
  112. #endregion
  113. protected byte[] m_particleSystem = new byte[0];
  114. [XmlIgnore] public uint TimeStampFull = 0;
  115. [XmlIgnore] public uint TimeStampTerse = 0;
  116. [XmlIgnore] public uint TimeStampLastActivity = 0; // Will be used for AutoReturn
  117. /// <summary>
  118. /// Only used internally to schedule client updates.
  119. /// 0 - no update is scheduled
  120. /// 1 - terse update scheduled
  121. /// 2 - full update scheduled
  122. ///
  123. /// TODO - This should be an enumeration
  124. /// </summary>
  125. private byte m_updateFlag;
  126. #region Properties
  127. public LLUUID CreatorID;
  128. public LLUUID ObjectCreator
  129. {
  130. get { return CreatorID; }
  131. }
  132. protected LLUUID m_uuid;
  133. public LLUUID UUID
  134. {
  135. get { return m_uuid; }
  136. set { m_uuid = value; }
  137. }
  138. protected uint m_localId;
  139. public uint LocalId
  140. {
  141. get { return m_localId; }
  142. set { m_localId = value; }
  143. }
  144. protected string m_name;
  145. public virtual string Name
  146. {
  147. get { return m_name; }
  148. set { m_name = value; }
  149. }
  150. protected LLObject.MaterialType m_material = 0;
  151. public byte Material
  152. {
  153. get { return (byte) m_material; }
  154. set { m_material = (LLObject.MaterialType) value; }
  155. }
  156. protected ulong m_regionHandle;
  157. public ulong RegionHandle
  158. {
  159. get { return m_regionHandle; }
  160. set { m_regionHandle = value; }
  161. }
  162. //unkown if this will be kept, added as a way of removing the group position from the group class
  163. protected LLVector3 m_groupPosition;
  164. /// <summary>
  165. /// Method for a prim to get it's world position from the group.
  166. /// Remember, the Group Position simply gives the position of the group itself
  167. /// </summary>
  168. /// <returns>A Linked Child Prim objects position in world</returns>
  169. public LLVector3 GetWorldPosition()
  170. {
  171. Quaternion parentRot = new Quaternion(
  172. ParentGroup.RootPart.RotationOffset.W,
  173. ParentGroup.RootPart.RotationOffset.X,
  174. ParentGroup.RootPart.RotationOffset.Y,
  175. ParentGroup.RootPart.RotationOffset.Z);
  176. Vector3 axPos
  177. = new Vector3(
  178. OffsetPosition.X,
  179. OffsetPosition.Y,
  180. OffsetPosition.Z);
  181. axPos = parentRot * axPos;
  182. LLVector3 translationOffsetPosition = new LLVector3(axPos.x, axPos.y, axPos.z);
  183. return GroupPosition + translationOffsetPosition;
  184. //return (new LLVector3(axiomPos.x, axiomPos.y, axiomPos.z) + AbsolutePosition);
  185. }
  186. /// <summary>
  187. /// Gets the rotation of this prim offset by the group rotation
  188. /// </summary>
  189. /// <returns></returns>
  190. public LLQuaternion GetWorldRotation()
  191. {
  192. Quaternion newRot;
  193. if (this.LinkNum == 0)
  194. {
  195. newRot = new Quaternion(RotationOffset.W,RotationOffset.X,RotationOffset.Y,RotationOffset.Z);
  196. }
  197. else
  198. {
  199. Quaternion parentRot = new Quaternion(
  200. ParentGroup.RootPart.RotationOffset.W,
  201. ParentGroup.RootPart.RotationOffset.X,
  202. ParentGroup.RootPart.RotationOffset.Y,
  203. ParentGroup.RootPart.RotationOffset.Z);
  204. Quaternion oldRot
  205. = new Quaternion(
  206. RotationOffset.W,
  207. RotationOffset.X,
  208. RotationOffset.Y,
  209. RotationOffset.Z);
  210. newRot = parentRot * oldRot;
  211. }
  212. return new LLQuaternion(newRot.x, newRot.y, newRot.z, newRot.w);
  213. //return new LLQuaternion(axiomPartRotation.x, axiomPartRotation.y, axiomPartRotation.z, axiomPartRotation.w);
  214. }
  215. public LLVector3 GroupPosition
  216. {
  217. get
  218. {
  219. // If this is a linkset, we don't want the physics engine mucking up our group position here.
  220. if (PhysActor != null && ParentID == 0)
  221. {
  222. m_groupPosition.X = PhysActor.Position.X;
  223. m_groupPosition.Y = PhysActor.Position.Y;
  224. m_groupPosition.Z = PhysActor.Position.Z;
  225. }
  226. return m_groupPosition;
  227. }
  228. set
  229. {
  230. m_groupPosition = value;
  231. if (PhysActor != null)
  232. {
  233. try
  234. {
  235. // Root prim actually goes at Position
  236. if (ParentID == 0)
  237. {
  238. PhysActor.Position = new PhysicsVector(value.X, value.Y, value.Z);
  239. }
  240. else
  241. {
  242. // To move the child prim in respect to the group position and rotation we have to calculate
  243. LLVector3 resultingposition = GetWorldPosition();
  244. PhysActor.Position = new PhysicsVector(resultingposition.X, resultingposition.Y, resultingposition.Z);
  245. LLQuaternion resultingrot = GetWorldRotation();
  246. PhysActor.Orientation = new Quaternion(resultingrot.W, resultingrot.X, resultingrot.Y, resultingrot.Z);
  247. }
  248. // Tell the physics engines that this prim changed.
  249. m_parentGroup.Scene.PhysicsScene.AddPhysicsActorTaint(PhysActor);
  250. }
  251. catch (Exception e)
  252. {
  253. Console.WriteLine(e.Message);
  254. }
  255. }
  256. }
  257. }
  258. private byte[] m_TextureAnimation;
  259. protected LLVector3 m_offsetPosition;
  260. public LLVector3 OffsetPosition
  261. {
  262. get { return m_offsetPosition; }
  263. set { m_offsetPosition = value;
  264. try
  265. {
  266. // Hack to get the child prim to update world positions in the physics engine
  267. ParentGroup.ResetChildPrimPhysicsPositions();
  268. }
  269. catch (System.NullReferenceException)
  270. {
  271. // Ignore, and skip over.
  272. }
  273. //m_log.Info("[PART]: OFFSET:" + m_offsetPosition.ToString());
  274. }
  275. }
  276. public LLVector3 AbsolutePosition
  277. {
  278. get { return m_offsetPosition + m_groupPosition; }
  279. }
  280. protected LLQuaternion m_rotationOffset;
  281. public LLQuaternion RotationOffset
  282. {
  283. get
  284. {
  285. // We don't want the physics engine mucking up the rotations in a linkset
  286. if (PhysActor != null && ParentID == 0)
  287. {
  288. if (PhysActor.Orientation.x != 0 || PhysActor.Orientation.y != 0
  289. || PhysActor.Orientation.z != 0 || PhysActor.Orientation.w != 0)
  290. {
  291. m_rotationOffset.X = PhysActor.Orientation.x;
  292. m_rotationOffset.Y = PhysActor.Orientation.y;
  293. m_rotationOffset.Z = PhysActor.Orientation.z;
  294. m_rotationOffset.W = PhysActor.Orientation.w;
  295. }
  296. }
  297. return m_rotationOffset;
  298. }
  299. set
  300. {
  301. m_rotationOffset = value;
  302. if (PhysActor != null)
  303. {
  304. try
  305. {
  306. // Root prim gets value directly
  307. if (ParentID == 0)
  308. {
  309. PhysActor.Orientation = new Quaternion(value.W, value.X, value.Y, value.Z);
  310. //m_log.Info("[PART]: RO1:" + PhysActor.Orientation.ToString());
  311. }
  312. else
  313. {
  314. // Child prim we have to calculate it's world rotationwel
  315. LLQuaternion resultingrotation = GetWorldRotation();
  316. PhysActor.Orientation = new Quaternion(resultingrotation.W, resultingrotation.X, resultingrotation.Y, resultingrotation.Z);
  317. //m_log.Info("[PART]: RO2:" + PhysActor.Orientation.ToString());
  318. }
  319. m_parentGroup.Scene.PhysicsScene.AddPhysicsActorTaint(PhysActor);
  320. //}
  321. }
  322. catch (Exception ex)
  323. {
  324. Console.WriteLine(ex.Message);
  325. }
  326. }
  327. }
  328. }
  329. protected LLVector3 m_velocity;
  330. protected LLVector3 m_rotationalvelocity;
  331. /// <summary></summary>
  332. public LLVector3 Velocity
  333. {
  334. get
  335. {
  336. //if (PhysActor.Velocity.x != 0 || PhysActor.Velocity.y != 0
  337. //|| PhysActor.Velocity.z != 0)
  338. //{
  339. if (PhysActor != null)
  340. {
  341. if (PhysActor.IsPhysical)
  342. {
  343. m_velocity.X = PhysActor.Velocity.X;
  344. m_velocity.Y = PhysActor.Velocity.Y;
  345. m_velocity.Z = PhysActor.Velocity.Z;
  346. }
  347. }
  348. return m_velocity;
  349. }
  350. set {
  351. m_velocity = value;
  352. if (PhysActor != null)
  353. {
  354. if (PhysActor.IsPhysical)
  355. {
  356. PhysActor.Velocity = new PhysicsVector(value.X, value.Y, value.Z);
  357. m_parentGroup.Scene.PhysicsScene.AddPhysicsActorTaint(PhysActor);
  358. }
  359. }
  360. }
  361. }
  362. public LLVector3 RotationalVelocity
  363. {
  364. get
  365. {
  366. //if (PhysActor.Velocity.x != 0 || PhysActor.Velocity.y != 0
  367. //|| PhysActor.Velocity.z != 0)
  368. //{
  369. if (PhysActor != null)
  370. {
  371. if (PhysActor.IsPhysical)
  372. {
  373. m_rotationalvelocity.FromBytes(PhysActor.RotationalVelocity.GetBytes(),0);
  374. }
  375. }
  376. return m_rotationalvelocity;
  377. }
  378. set { m_rotationalvelocity = value; }
  379. }
  380. protected LLVector3 m_angularVelocity;
  381. /// <summary></summary>
  382. public LLVector3 AngularVelocity
  383. {
  384. get { return m_angularVelocity; }
  385. set { m_angularVelocity = value; }
  386. }
  387. protected LLVector3 m_acceleration;
  388. /// <summary></summary>
  389. public LLVector3 Acceleration
  390. {
  391. get { return m_acceleration; }
  392. set { m_acceleration = value; }
  393. }
  394. private string m_description = String.Empty;
  395. public string Description
  396. {
  397. get { return m_description; }
  398. set { m_description = value; }
  399. }
  400. private Color m_color = Color.Black;
  401. public Color Color
  402. {
  403. get { return m_color; }
  404. set
  405. {
  406. m_color = value;
  407. TriggerScriptChangedEvent(Changed.COLOR);
  408. /* ScheduleFullUpdate() need not be called b/c after
  409. * setting the color, the text will be set, so then
  410. * ScheduleFullUpdate() will be called. */
  411. //ScheduleFullUpdate();
  412. }
  413. }
  414. private string m_text = String.Empty;
  415. public Vector3 SitTargetPosition
  416. {
  417. get { return m_sitTargetPosition; }
  418. }
  419. public Quaternion SitTargetOrientation
  420. {
  421. get { return m_sitTargetOrientation; }
  422. }
  423. public string Text
  424. {
  425. get { return m_text; }
  426. set
  427. {
  428. m_text = value;
  429. }
  430. }
  431. private string m_sitName = String.Empty;
  432. public string SitName
  433. {
  434. get { return m_sitName; }
  435. set { m_sitName = value; }
  436. }
  437. private string m_touchName = String.Empty;
  438. public string TouchName
  439. {
  440. get { return m_touchName; }
  441. set { m_touchName = value; }
  442. }
  443. private int m_linkNum = 0;
  444. public int LinkNum
  445. {
  446. get { return m_linkNum; }
  447. set
  448. {
  449. m_linkNum = value;
  450. TriggerScriptChangedEvent(Changed.LINK);
  451. }
  452. }
  453. private byte m_clickAction = 0;
  454. public byte ClickAction
  455. {
  456. get { return m_clickAction; }
  457. set
  458. {
  459. m_clickAction = value;
  460. }
  461. }
  462. protected PrimitiveBaseShape m_shape;
  463. /// <summary>
  464. /// hook to the physics scene to apply impulse
  465. /// This is sent up to the group, which then finds the root prim
  466. /// and applies the force on the root prim of the group
  467. /// </summary>
  468. /// <param name="impulsei">Vector force</param>
  469. /// <param name="localGlobalTF">true for the local frame, false for the global frame</param>
  470. public void ApplyImpulse(LLVector3 impulsei, bool localGlobalTF)
  471. {
  472. PhysicsVector impulse = new PhysicsVector(impulsei.X, impulsei.Y, impulsei.Z);
  473. if (localGlobalTF)
  474. {
  475. LLQuaternion grot = GetWorldRotation();
  476. Quaternion AXgrot = new Quaternion(grot.W,grot.X,grot.Y,grot.Z);
  477. Vector3 AXimpulsei = new Vector3(impulsei.X, impulsei.Y, impulsei.Z);
  478. Vector3 newimpulse = AXgrot * AXimpulsei;
  479. impulse = new PhysicsVector(newimpulse.x, newimpulse.y, newimpulse.z);
  480. }
  481. else
  482. {
  483. if (m_parentGroup != null)
  484. {
  485. m_parentGroup.applyImpulse(impulse);
  486. }
  487. }
  488. }
  489. public void MoveToTarget(LLVector3 target, float tau)
  490. {
  491. if (tau > 0)
  492. {
  493. m_parentGroup.moveToTarget(target, tau);
  494. }
  495. else
  496. {
  497. StopMoveToTarget();
  498. }
  499. }
  500. public void StopMoveToTarget()
  501. {
  502. m_parentGroup.stopMoveToTarget();
  503. }
  504. public void TriggerScriptChangedEvent(Changed val)
  505. {
  506. if (m_parentGroup != null)
  507. {
  508. if (m_parentGroup.Scene != null)
  509. m_parentGroup.Scene.TriggerObjectChanged(LocalId, (uint)val);
  510. }
  511. }
  512. public PrimitiveBaseShape Shape
  513. {
  514. get { return m_shape; }
  515. set
  516. {
  517. m_shape = value;
  518. TriggerScriptChangedEvent(Changed.SHAPE);
  519. }
  520. }
  521. public LLVector3 Scale
  522. {
  523. get { return m_shape.Scale; }
  524. set
  525. {
  526. m_shape.Scale = value;
  527. TriggerScriptChangedEvent(Changed.SCALE);
  528. }
  529. }
  530. public bool Stopped
  531. {
  532. get {
  533. double threshold = 0.02;
  534. return (Math.Abs(Velocity.X) < threshold &&
  535. Math.Abs(Velocity.Y) < threshold &&
  536. Math.Abs(Velocity.Z) < threshold &&
  537. Math.Abs(AngularVelocity.X) < threshold &&
  538. Math.Abs(AngularVelocity.Y) < threshold &&
  539. Math.Abs(AngularVelocity.Z) < threshold);
  540. }
  541. }
  542. #endregion
  543. public LLUUID ObjectOwner
  544. {
  545. get { return OwnerID; }
  546. }
  547. // FIXME, TODO, ERROR: 'ParentGroup' can't be in here, move it out.
  548. protected SceneObjectGroup m_parentGroup;
  549. public SceneObjectGroup ParentGroup
  550. {
  551. get { return m_parentGroup; }
  552. }
  553. public byte UpdateFlag
  554. {
  555. get { return m_updateFlag; }
  556. set { m_updateFlag = value; }
  557. }
  558. #region Constructors
  559. /// <summary>
  560. /// No arg constructor called by region restore db code
  561. /// </summary>
  562. public SceneObjectPart()
  563. {
  564. // It's not necessary to persist this
  565. m_inventoryFileName = "taskinventory" + LLUUID.Random().ToString();
  566. m_TextureAnimation = new byte[0];
  567. }
  568. public SceneObjectPart(ulong regionHandle, SceneObjectGroup parent, LLUUID ownerID, uint localID,
  569. PrimitiveBaseShape shape, LLVector3 groupPosition, LLVector3 offsetPosition)
  570. : this(regionHandle, parent, ownerID, localID, shape, groupPosition, LLQuaternion.Identity, offsetPosition)
  571. {
  572. }
  573. /// <summary>
  574. /// Create a completely new SceneObjectPart (prim)
  575. /// </summary>
  576. /// <param name="regionHandle"></param>
  577. /// <param name="parent"></param>
  578. /// <param name="ownerID"></param>
  579. /// <param name="localID"></param>
  580. /// <param name="shape"></param>
  581. /// <param name="position"></param>
  582. public SceneObjectPart(ulong regionHandle, SceneObjectGroup parent, LLUUID ownerID, uint localID,
  583. PrimitiveBaseShape shape, LLVector3 groupPosition, LLQuaternion rotationOffset,
  584. LLVector3 offsetPosition)
  585. {
  586. m_name = "Primitive";
  587. m_regionHandle = regionHandle;
  588. m_parentGroup = parent;
  589. CreationDate = (Int32) (DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
  590. OwnerID = ownerID;
  591. CreatorID = OwnerID;
  592. LastOwnerID = LLUUID.Zero;
  593. UUID = LLUUID.Random();
  594. LocalId = (uint) (localID);
  595. Shape = shape;
  596. // Todo: Add More Object Parameter from above!
  597. OwnershipCost = 0;
  598. ObjectSaleType = (byte) 0;
  599. SalePrice = 0;
  600. Category = (uint) 0;
  601. LastOwnerID = CreatorID;
  602. // End Todo: ///
  603. GroupPosition = groupPosition;
  604. OffsetPosition = offsetPosition;
  605. RotationOffset = rotationOffset;
  606. Velocity = new LLVector3(0, 0, 0);
  607. m_rotationalvelocity = new LLVector3(0, 0, 0);
  608. AngularVelocity = new LLVector3(0, 0, 0);
  609. Acceleration = new LLVector3(0, 0, 0);
  610. m_TextureAnimation = new byte[0];
  611. m_inventoryFileName = "taskinventory" + LLUUID.Random().ToString();
  612. m_folderID = LLUUID.Random();
  613. Flags = 0;
  614. Flags |= LLObject.ObjectFlags.Touch |
  615. LLObject.ObjectFlags.AllowInventoryDrop |
  616. LLObject.ObjectFlags.CreateSelected;
  617. TrimPermissions();
  618. ScheduleFullUpdate();
  619. }
  620. /// <summary>
  621. /// Re/create a SceneObjectPart (prim)
  622. /// currently not used, and maybe won't be
  623. /// </summary>
  624. /// <param name="regionHandle"></param>
  625. /// <param name="parent"></param>
  626. /// <param name="ownerID"></param>
  627. /// <param name="localID"></param>
  628. /// <param name="shape"></param>
  629. /// <param name="position"></param>
  630. public SceneObjectPart(ulong regionHandle, SceneObjectGroup parent, int creationDate, LLUUID ownerID,
  631. LLUUID creatorID, LLUUID lastOwnerID, uint localID, PrimitiveBaseShape shape,
  632. LLVector3 position, LLQuaternion rotation, uint flags)
  633. {
  634. m_regionHandle = regionHandle;
  635. m_parentGroup = parent;
  636. TimeStampTerse = (uint) Util.UnixTimeSinceEpoch();
  637. CreationDate = creationDate;
  638. OwnerID = ownerID;
  639. CreatorID = creatorID;
  640. LastOwnerID = lastOwnerID;
  641. UUID = LLUUID.Random();
  642. LocalId = (uint) (localID);
  643. Shape = shape;
  644. OwnershipCost = 0;
  645. ObjectSaleType = (byte) 0;
  646. SalePrice = 0;
  647. Category = (uint) 0;
  648. LastOwnerID = CreatorID;
  649. OffsetPosition = position;
  650. RotationOffset = rotation;
  651. ObjectFlags = flags;
  652. // Since we don't store script state, this is only a 'temporary' objectflag now
  653. // If the object is scripted, the script will get loaded and this will be set again
  654. ObjectFlags &= ~(uint)LLObject.ObjectFlags.Scripted;
  655. TrimPermissions();
  656. // ApplyPhysics();
  657. ScheduleFullUpdate();
  658. }
  659. #endregion
  660. /// <summary>
  661. ///
  662. /// </summary>
  663. /// <param name="xmlreader"></param>
  664. /// <returns></returns>
  665. public static SceneObjectPart FromXml(XmlReader xmlReader)
  666. {
  667. XmlSerializer serializer = new XmlSerializer(typeof (SceneObjectPart));
  668. SceneObjectPart newobject = (SceneObjectPart) serializer.Deserialize(xmlReader);
  669. return newobject;
  670. }
  671. public void ApplyPhysics(uint rootObjectFlags, bool m_physicalPrim)
  672. {
  673. bool isPhysical = (((rootObjectFlags & (uint) LLObject.ObjectFlags.Physics) != 0) && m_physicalPrim);
  674. bool isPhantom = ((rootObjectFlags & (uint) LLObject.ObjectFlags.Phantom) != 0);
  675. // Added clarification.. since A rigid body is an object that you can kick around, etc.
  676. bool RigidBody = isPhysical && !isPhantom;
  677. // The only time the physics scene shouldn't know about the prim is if it's phantom
  678. if (!isPhantom)
  679. {
  680. PhysActor = m_parentGroup.Scene.PhysicsScene.AddPrimShape(
  681. Name,
  682. Shape,
  683. new PhysicsVector(AbsolutePosition.X, AbsolutePosition.Y,
  684. AbsolutePosition.Z),
  685. new PhysicsVector(Scale.X, Scale.Y, Scale.Z),
  686. new Quaternion(RotationOffset.W, RotationOffset.X,
  687. RotationOffset.Y, RotationOffset.Z), RigidBody);
  688. // Basic Physics returns null.. joy joy joy.
  689. if (PhysActor != null)
  690. {
  691. PhysActor.LocalID = LocalId;
  692. DoPhysicsPropertyUpdate(RigidBody, true);
  693. }
  694. }
  695. }
  696. public void ApplyNextOwnerPermissions()
  697. {
  698. BaseMask = NextOwnerMask;
  699. OwnerMask = NextOwnerMask;
  700. TriggerScriptChangedEvent(Changed.OWNER);
  701. }
  702. public void TrimPermissions()
  703. {
  704. BaseMask &= (uint)PermissionMask.All;
  705. OwnerMask &= (uint)PermissionMask.All;
  706. GroupMask &= (uint)PermissionMask.All;
  707. EveryoneMask &= (uint)PermissionMask.All;
  708. NextOwnerMask &= (uint)PermissionMask.All;
  709. }
  710. /// <summary>
  711. ///
  712. /// </summary>
  713. /// <param name="xmlWriter"></param>
  714. public void ToXml(XmlWriter xmlWriter)
  715. {
  716. XmlSerializer serializer = new XmlSerializer(typeof (SceneObjectPart));
  717. serializer.Serialize(xmlWriter, this);
  718. }
  719. public EntityIntersection TestIntersection(Ray iray, Quaternion parentrot)
  720. {
  721. // In this case we're using a sphere with a radius of the largest dimention of the prim
  722. // TODO: Change to take shape into account
  723. EntityIntersection returnresult = new EntityIntersection();
  724. Vector3 vAbsolutePosition = new Vector3(AbsolutePosition.X, AbsolutePosition.Y, AbsolutePosition.Z);
  725. Vector3 vScale = new Vector3(Scale.X, Scale.Y, Scale.Z);
  726. Quaternion qRotation =
  727. new Quaternion(RotationOffset.W, RotationOffset.X, RotationOffset.Y, RotationOffset.Z);
  728. //Quaternion worldRotation = (qRotation*parentrot);
  729. //Matrix3 worldRotM = worldRotation.ToRotationMatrix();
  730. Vector3 rOrigin = iray.Origin;
  731. Vector3 rDirection = iray.Direction;
  732. //rDirection = rDirection.Normalize();
  733. // Buidling the first part of the Quadratic equation
  734. Vector3 r2ndDirection = rDirection*rDirection;
  735. float itestPart1 = r2ndDirection.x + r2ndDirection.y + r2ndDirection.z;
  736. // Buidling the second part of the Quadratic equation
  737. Vector3 tmVal2 = rOrigin - vAbsolutePosition;
  738. Vector3 r2Direction = rDirection*2.0f;
  739. Vector3 tmVal3 = r2Direction*tmVal2;
  740. float itestPart2 = tmVal3.x + tmVal3.y + tmVal3.z;
  741. // Buidling the third part of the Quadratic equation
  742. Vector3 tmVal4 = rOrigin*rOrigin;
  743. Vector3 tmVal5 = vAbsolutePosition*vAbsolutePosition;
  744. Vector3 tmVal6 = vAbsolutePosition*rOrigin;
  745. // Set Radius to the largest dimention of the prim
  746. float radius = 0f;
  747. if (vScale.x > radius)
  748. radius = vScale.x;
  749. if (vScale.y > radius)
  750. radius = vScale.y;
  751. if (vScale.z > radius)
  752. radius = vScale.z;
  753. // the second part of this is the default prim size
  754. // once we factor in the aabb of the prim we're adding we can
  755. // change this to;
  756. // radius = (radius / 2) - 0.01f;
  757. //
  758. radius = (radius / 2) + (0.5f / 2) - 0.1f;
  759. //radius = radius;
  760. float itestPart3 = tmVal4.x + tmVal4.y + tmVal4.z + tmVal5.x + tmVal5.y + tmVal5.z -
  761. (2.0f*(tmVal6.x + tmVal6.y + tmVal6.z + (radius*radius)));
  762. // Yuk Quadradrics.. Solve first
  763. float rootsqr = (itestPart2*itestPart2) - (4.0f*itestPart1*itestPart3);
  764. if (rootsqr < 0.0f)
  765. {
  766. // No intersection
  767. return returnresult;
  768. }
  769. float root = ((-itestPart2) - (float) Math.Sqrt((double) rootsqr))/(itestPart1*2.0f);
  770. if (root < 0.0f)
  771. {
  772. // perform second quadratic root solution
  773. root = ((-itestPart2) + (float) Math.Sqrt((double) rootsqr))/(itestPart1*2.0f);
  774. // is there any intersection?
  775. if (root < 0.0f)
  776. {
  777. // nope, no intersection
  778. return returnresult;
  779. }
  780. }
  781. // We got an intersection. putting together an EntityIntersection object with the
  782. // intersection information
  783. Vector3 ipoint =
  784. new Vector3(iray.Origin.x + (iray.Direction.x*root), iray.Origin.y + (iray.Direction.y*root),
  785. iray.Origin.z + (iray.Direction.z*root));
  786. returnresult.HitTF = true;
  787. returnresult.ipoint = ipoint;
  788. // Normal is calculated by the difference and then normalizing the result
  789. Vector3 normalpart = ipoint - vAbsolutePosition;
  790. returnresult.normal = normalpart / normalpart.Length;
  791. // It's funny how the LLVector3 object has a Distance function, but the Axiom.Math object doesnt.
  792. // I can write a function to do it.. but I like the fact that this one is Static.
  793. LLVector3 distanceConvert1 = new LLVector3(iray.Origin.x, iray.Origin.y, iray.Origin.z);
  794. LLVector3 distanceConvert2 = new LLVector3(ipoint.x, ipoint.y, ipoint.z);
  795. float distance = (float) Util.GetDistanceTo(distanceConvert1, distanceConvert2);
  796. returnresult.distance = distance;
  797. return returnresult;
  798. }
  799. public double GetDistanceTo(Vector3 a, Vector3 b)
  800. {
  801. float dx = a.x - b.x;
  802. float dy = a.y - b.y;
  803. float dz = a.z - b.z;
  804. return Math.Sqrt(dx * dx + dy * dy + dz * dz);
  805. }
  806. public EntityIntersection TestIntersectionOBB(Ray iray, Quaternion parentrot)
  807. {
  808. // In this case we're using a rectangular prism, which has 6 faces and therefore 6 planes
  809. // This breaks down into the ray---> plane equation.
  810. // TODO: Change to take shape into account
  811. Vector3[] vertexes = new Vector3[8];
  812. float[] distance = new float[6];
  813. Vector3[] FaceA = new Vector3[6]; // vertex A for Facei
  814. Vector3[] FaceB = new Vector3[6]; // vertex B for Facei
  815. Vector3[] FaceC = new Vector3[6]; // vertex C for Facei
  816. Vector3[] FaceD = new Vector3[6]; // vertex D for Facei
  817. Vector3[] normals = new Vector3[6]; // Normal for Facei
  818. Vector3 AmBa = new Vector3(0, 0, 0); // Vertex A - Vertex B
  819. Vector3 AmBb = new Vector3(0, 0, 0); // Vertex B - Vertex C
  820. Vector3 cross = new Vector3();
  821. LLVector3 pos = GetWorldPosition();
  822. LLQuaternion rot = GetWorldRotation();
  823. // Variables prefixed with AX are Axiom.Math copies of the LL variety.
  824. Quaternion AXrot = new Quaternion(rot.W,rot.X,rot.Y,rot.Z);
  825. AXrot.Normalize();
  826. Vector3 AXpos = new Vector3(pos.X, pos.Y, pos.Z);
  827. // tScale is the offset to derive the vertex based on the scale.
  828. // it's different for each vertex because we've got to rotate it
  829. // to get the world position of the vertex to produce the Oriented Bounding Box
  830. Vector3 tScale = new Vector3();
  831. Vector3 AXscale = new Vector3(m_shape.Scale.X * 0.5f, m_shape.Scale.Y * 0.5f, m_shape.Scale.Z * 0.5f);
  832. //Vector3 pScale = (AXscale) - (AXrot.Inverse() * (AXscale));
  833. //Vector3 nScale = (AXscale * -1) - (AXrot.Inverse() * (AXscale * -1));
  834. // rScale is the rotated offset to find a vertex based on the scale and the world rotation.
  835. Vector3 rScale = new Vector3();
  836. // Get Vertexes for Faces Stick them into ABCD for each Face
  837. // Form: Face<vertex>[face] that corresponds to the below diagram
  838. #region ABCD Face Vertex Map Comment Diagram
  839. // A _________ B
  840. // | |
  841. // | 4 top |
  842. // |_________|
  843. // C D
  844. // A _________ B
  845. // | Back |
  846. // | 3 |
  847. // |_________|
  848. // C D
  849. // A _________ B B _________ A
  850. // | Left | | Right |
  851. // | 0 | | 2 |
  852. // |_________| |_________|
  853. // C D D C
  854. // A _________ B
  855. // | Front |
  856. // | 1 |
  857. // |_________|
  858. // C D
  859. // C _________ D
  860. // | |
  861. // | 5 bot |
  862. // |_________|
  863. // A B
  864. #endregion
  865. #region Plane Decomposition of Oriented Bounding Box
  866. tScale = new Vector3(AXscale.x, -AXscale.y, AXscale.z);
  867. rScale = ((AXrot * tScale));
  868. vertexes[0] = (new Vector3((pos.X + rScale.x), (pos.Y + rScale.y), (pos.Z + rScale.z)));
  869. // vertexes[0].x = pos.X + vertexes[0].x;
  870. //vertexes[0].y = pos.Y + vertexes[0].y;
  871. //vertexes[0].z = pos.Z + vertexes[0].z;
  872. FaceA[0] = vertexes[0];
  873. FaceB[3] = vertexes[0];
  874. FaceA[4] = vertexes[0];
  875. tScale = AXscale;
  876. rScale = ((AXrot * tScale));
  877. vertexes[1] = (new Vector3((pos.X + rScale.x), (pos.Y + rScale.y), (pos.Z + rScale.z)));
  878. // vertexes[1].x = pos.X + vertexes[1].x;
  879. // vertexes[1].y = pos.Y + vertexes[1].y;
  880. //vertexes[1].z = pos.Z + vertexes[1].z;
  881. FaceB[0] = vertexes[1];
  882. FaceA[1] = vertexes[1];
  883. FaceC[4] = vertexes[1];
  884. tScale = new Vector3(AXscale.x, -AXscale.y, -AXscale.z);
  885. rScale = ((AXrot * tScale));
  886. vertexes[2] = (new Vector3((pos.X + rScale.x), (pos.Y + rScale.y), (pos.Z + rScale.z)));
  887. //vertexes[2].x = pos.X + vertexes[2].x;
  888. //vertexes[2].y = pos.Y + vertexes[2].y;
  889. //vertexes[2].z = pos.Z + vertexes[2].z;
  890. FaceC[0] = vertexes[2];
  891. FaceD[3] = vertexes[2];
  892. FaceC[5] = vertexes[2];
  893. tScale = new Vector3(AXscale.x, AXscale.y, -AXscale.z);
  894. rScale = ((AXrot * tScale));
  895. vertexes[3] = (new Vector3((pos.X + rScale.x), (pos.Y + rScale.y), (pos.Z + rScale.z)));
  896. //vertexes[3].x = pos.X + vertexes[3].x;
  897. // vertexes[3].y = pos.Y + vertexes[3].y;
  898. // vertexes[3].z = pos.Z + vertexes[3].z;
  899. FaceD[0] = vertexes[3];
  900. FaceC[1] = vertexes[3];
  901. FaceA[5] = vertexes[3];
  902. tScale = new Vector3(-AXscale.x, AXscale.y, AXscale.z);
  903. rScale = ((AXrot * tScale));
  904. vertexes[4] = (new Vector3((pos.X + rScale.x), (pos.Y + rScale.y), (pos.Z + rScale.z)));
  905. // vertexes[4].x = pos.X + vertexes[4].x;
  906. // vertexes[4].y = pos.Y + vertexes[4].y;
  907. // vertexes[4].z = pos.Z + vertexes[4].z;
  908. FaceB[1] = vertexes[4];
  909. FaceA[2] = vertexes[4];
  910. FaceD[4] = vertexes[4];
  911. tScale = new Vector3(-AXscale.x, AXscale.y, -AXscale.z);
  912. rScale = ((AXrot * tScale));
  913. vertexes[5] = (new Vector3((pos.X + rScale.x), (pos.Y + rScale.y), (pos.Z + rScale.z)));
  914. // vertexes[5].x = pos.X + vertexes[5].x;
  915. // vertexes[5].y = pos.Y + vertexes[5].y;
  916. // vertexes[5].z = pos.Z + vertexes[5].z;
  917. FaceD[1] = vertexes[5];
  918. FaceC[2] = vertexes[5];
  919. FaceB[5] = vertexes[5];
  920. tScale = new Vector3(-AXscale.x, -AXscale.y, AXscale.z);
  921. rScale = ((AXrot * tScale));
  922. vertexes[6] = (new Vector3((pos.X + rScale.x), (pos.Y + rScale.y), (pos.Z + rScale.z)));
  923. // vertexes[6].x = pos.X + vertexes[6].x;
  924. // vertexes[6].y = pos.Y + vertexes[6].y;
  925. // vertexes[6].z = pos.Z + vertexes[6].z;
  926. FaceB[2] = vertexes[6];
  927. FaceA[3] = vertexes[6];
  928. FaceB[4] = vertexes[6];
  929. tScale = new Vector3(-AXscale.x, -AXscale.y, -AXscale.z);
  930. rScale = ((AXrot * tScale));
  931. vertexes[7] = (new Vector3((pos.X + rScale.x), (pos.Y + rScale.y), (pos.Z + rScale.z)));
  932. // vertexes[7].x = pos.X + vertexes[7].x;
  933. // vertexes[7].y = pos.Y + vertexes[7].y;
  934. // vertexes[7].z = pos.Z + vertexes[7].z;
  935. FaceD[2] = vertexes[7];
  936. FaceC[3] = vertexes[7];
  937. FaceD[5] = vertexes[7];
  938. #endregion
  939. // Get our plane normals
  940. for (int i = 0; i < 6; i++)
  941. {
  942. //m_log.Info("[FACECALCULATION]: FaceA[" + i + "]=" + FaceA[i] + " FaceB[" + i + "]=" + FaceB[i] + " FaceC[" + i + "]=" + FaceC[i] + " FaceD[" + i + "]=" + FaceD[i]);
  943. // Our Plane direction
  944. AmBa = FaceA[i] - FaceB[i];
  945. AmBb = FaceB[i] - FaceC[i];
  946. cross = AmBb.Cross(AmBa);
  947. // normalize the cross product to get the normal.
  948. normals[i] = cross / cross.Length;
  949. //m_log.Info("[NORMALS]: normals[ " + i + "]" + normals[i].ToString());
  950. //distance[i] = (normals[i].x * AmBa.x + normals[i].y * AmBa.y + normals[i].z * AmBa.z) * -1;
  951. }
  952. EntityIntersection returnresult = new EntityIntersection();
  953. returnresult.distance = 1024;
  954. float c = 0;
  955. float a = 0;
  956. float d = 0;
  957. Vector3 q = new Vector3();
  958. #region OBB Version 2 Experiment
  959. //float fmin = 999999;
  960. //float fmax = -999999;
  961. //float s = 0;
  962. //for (int i=0;i<6;i++)
  963. //{
  964. //s = iray.Direction.Dot(normals[i]);
  965. //d = normals[i].Dot(FaceB[i]);
  966. //if (s == 0)
  967. //{
  968. //if (iray.Origin.Dot(normals[i]) > d)
  969. //{
  970. //return returnresult;
  971. //}
  972. // else
  973. //{
  974. //continue;
  975. //}
  976. //}
  977. //a = (d - iray.Origin.Dot(normals[i])) / s;
  978. //if ( iray.Direction.Dot(normals[i]) < 0)
  979. //{
  980. //if (a > fmax)
  981. //{
  982. //if (a > fmin)
  983. //{
  984. //return returnresult;
  985. //}
  986. //fmax = a;
  987. //}
  988. //}
  989. //else
  990. //{
  991. //if (a < fmin)
  992. //{
  993. //if (a < 0 || a < fmax)
  994. //{
  995. //return returnresult;
  996. //}
  997. //fmin = a;
  998. //}
  999. //}
  1000. //}
  1001. //if (fmax > 0)
  1002. // a= fmax;
  1003. //else
  1004. // a=fmin;
  1005. //q = iray.Origin + a * iray.Direction;
  1006. #endregion
  1007. // Loop over faces (6 of them)
  1008. for (int i = 0; i < 6; i++)
  1009. {
  1010. AmBa = FaceA[i] - FaceB[i];
  1011. AmBb = FaceB[i] - FaceC[i];
  1012. d = normals[i].Dot(FaceB[i]);
  1013. c = iray.Direction.Dot(normals[i]);
  1014. if (c == 0)
  1015. continue;
  1016. a = (d - iray.Origin.Dot(normals[i])) / c;
  1017. if (a < 0)
  1018. continue;
  1019. // If the normal is pointing outside the object
  1020. if (iray.Direction.Dot(normals[i]) < 0)
  1021. {
  1022. q = iray.Origin + a * iray.Direction;
  1023. // Is this the closest hit to the object's origin?
  1024. //float distance2 = (float)GetDistanceTo(q, iray.Origin);
  1025. float distance2 = (float)GetDistanceTo(q, AXpos);
  1026. if (distance2 < returnresult.distance)
  1027. {
  1028. returnresult.distance = distance2;
  1029. returnresult.HitTF = true;
  1030. returnresult.ipoint = q;
  1031. //m_log.Info("[FACE]:" + i.ToString());
  1032. //m_log.Info("[POINT]: " + q.ToString());
  1033. returnresult.normal = normals[i];
  1034. }
  1035. }
  1036. }
  1037. return returnresult;
  1038. }
  1039. /// <summary>
  1040. ///
  1041. /// </summary>
  1042. public void SetParent(SceneObjectGroup parent)
  1043. {
  1044. m_parentGroup = parent;
  1045. }
  1046. public void SetSitTarget(Vector3 offset, Quaternion orientation)
  1047. {
  1048. m_sitTargetPosition = offset;
  1049. m_sitTargetOrientation = orientation;
  1050. }
  1051. public void SetBuoyancy(float fvalue)
  1052. {
  1053. if (PhysActor != null)
  1054. {
  1055. PhysActor.Buoyancy = fvalue;
  1056. }
  1057. }
  1058. public void SetFloatOnWater(int floatYN)
  1059. {
  1060. if (PhysActor != null)
  1061. {
  1062. if (floatYN == 1)
  1063. {
  1064. PhysActor.FloatOnWater = true;
  1065. }
  1066. else
  1067. {
  1068. PhysActor.FloatOnWater = false;
  1069. }
  1070. }
  1071. }
  1072. public LLVector3 GetSitTargetPositionLL()
  1073. {
  1074. return new LLVector3(m_sitTargetPosition.x, m_sitTargetPosition.y, m_sitTargetPosition.z);
  1075. }
  1076. public LLQuaternion GetSitTargetOrientationLL()
  1077. {
  1078. return
  1079. new LLQuaternion(m_sitTargetOrientation.x, m_sitTargetOrientation.y, m_sitTargetOrientation.z,
  1080. m_sitTargetOrientation.w);
  1081. }
  1082. // Utility function so the databases don't have to reference axiom.math
  1083. public void SetSitTargetLL(LLVector3 offset, LLQuaternion orientation)
  1084. {
  1085. if (
  1086. !(offset.X == 0 && offset.Y == 0 && offset.Z == 0 && (orientation.W == 0 || orientation.W == 1) &&
  1087. orientation.X == 0 && orientation.Y == 0 && orientation.Z == 0))
  1088. {
  1089. m_sitTargetPosition = new Vector3(offset.X, offset.Y, offset.Z);
  1090. m_sitTargetOrientation = new Quaternion(orientation.W, orientation.X, orientation.Y, orientation.Z);
  1091. }
  1092. }
  1093. public Vector3 GetSitTargetPosition()
  1094. {
  1095. return m_sitTargetPosition;
  1096. }
  1097. public Quaternion GetSitTargetOrientation()
  1098. {
  1099. return m_sitTargetOrientation;
  1100. }
  1101. public void SetAvatarOnSitTarget(LLUUID avatarID)
  1102. {
  1103. m_sitTargetAvatar = avatarID;
  1104. TriggerScriptChangedEvent(Changed.LINK);
  1105. }
  1106. public LLUUID GetAvatarOnSitTarget()
  1107. {
  1108. return m_sitTargetAvatar;
  1109. }
  1110. public LLUUID GetRootPartUUID()
  1111. {
  1112. if (m_parentGroup != null)
  1113. {
  1114. return m_parentGroup.UUID;
  1115. }
  1116. return LLUUID.Zero;
  1117. }
  1118. public static SceneObjectPart Create()
  1119. {
  1120. SceneObjectPart part = new SceneObjectPart();
  1121. part.UUID = LLUUID.Random();
  1122. PrimitiveBaseShape shape = PrimitiveBaseShape.Create();
  1123. part.Shape = shape;
  1124. part.Name = "Primitive";
  1125. part.OwnerID = LLUUID.Random();
  1126. return part;
  1127. }
  1128. #region Copying
  1129. /// <summary>
  1130. /// Duplicates this part.
  1131. /// </summary>
  1132. /// <returns></returns>
  1133. public SceneObjectPart Copy(uint localID, LLUUID AgentID, LLUUID GroupID, int linkNum)
  1134. {
  1135. SceneObjectPart dupe = (SceneObjectPart) MemberwiseClone();
  1136. dupe.m_shape = m_shape.Copy();
  1137. dupe.m_regionHandle = m_regionHandle;
  1138. dupe.UUID = LLUUID.Random();
  1139. dupe.LocalId = localID;
  1140. dupe.OwnerID = AgentID;
  1141. dupe.GroupID = GroupID;
  1142. dupe.GroupPosition = new LLVector3(GroupPosition.X, GroupPosition.Y, GroupPosition.Z);
  1143. dupe.OffsetPosition = new LLVector3(OffsetPosition.X, OffsetPosition.Y, OffsetPosition.Z);
  1144. dupe.RotationOffset =
  1145. new LLQuaternion(RotationOffset.X, RotationOffset.Y, RotationOffset.Z, RotationOffset.W);
  1146. dupe.Velocity = new LLVector3(0, 0, 0);
  1147. dupe.Acceleration = new LLVector3(0, 0, 0);
  1148. dupe.AngularVelocity = new LLVector3(0, 0, 0);
  1149. dupe.ObjectFlags = ObjectFlags;
  1150. dupe.OwnershipCost = OwnershipCost;
  1151. dupe.ObjectSaleType = ObjectSaleType;
  1152. dupe.SalePrice = SalePrice;
  1153. dupe.Category = Category;
  1154. dupe.TaskInventory = (TaskInventoryDictionary)dupe.TaskInventory.Clone();
  1155. dupe.ResetIDs(linkNum);
  1156. // This may be wrong... it might have to be applied in SceneObjectGroup to the object that's being duplicated.
  1157. dupe.LastOwnerID = ObjectOwner;
  1158. byte[] extraP = new byte[Shape.ExtraParams.Length];
  1159. Array.Copy(Shape.ExtraParams, extraP, extraP.Length);
  1160. dupe.Shape.ExtraParams = extraP;
  1161. bool UsePhysics = ((dupe.ObjectFlags & (uint) LLObject.ObjectFlags.Physics) != 0);
  1162. dupe.DoPhysicsPropertyUpdate(UsePhysics, true);
  1163. return dupe;
  1164. }
  1165. #endregion
  1166. /// <summary>
  1167. /// Reset LLUUIDs for this part. This involves generate this part's own LLUUID and
  1168. /// generating new LLUUIDs for all the items in the inventory.
  1169. /// </summary>
  1170. /// <param name="linkNum">Link number for the part</param>
  1171. public void ResetIDs(int linkNum)
  1172. {
  1173. UUID = LLUUID.Random();
  1174. LinkNum = linkNum;
  1175. ResetInventoryIDs();
  1176. }
  1177. #region Update Scheduling
  1178. /// <summary>
  1179. /// Clear all pending updates
  1180. /// </summary>
  1181. private void ClearUpdateSchedule()
  1182. {
  1183. m_updateFlag = 0;
  1184. }
  1185. /// <summary>
  1186. /// Schedules this prim for a full update
  1187. /// </summary>
  1188. public void ScheduleFullUpdate()
  1189. {
  1190. if (m_parentGroup != null)
  1191. {
  1192. m_parentGroup.HasGroupChanged = true;
  1193. m_parentGroup.QueueForUpdateCheck();
  1194. }
  1195. int timeNow = Util.UnixTimeSinceEpoch();
  1196. // If multiple updates are scheduled on the same second, we still need to perform all of them
  1197. // So we'll force the issue by bumping up the timestamp so that later processing sees these need
  1198. // to be performed.
  1199. if (timeNow <= TimeStampFull)
  1200. {
  1201. TimeStampFull += 1;
  1202. }
  1203. else
  1204. {
  1205. TimeStampFull = (uint)timeNow;
  1206. }
  1207. m_updateFlag = 2;
  1208. // m_log.DebugFormat(
  1209. // "[SCENE OBJECT PART]: Scheduling full update for {0}, {1} at {2}",
  1210. // UUID, Name, TimeStampFull);
  1211. }
  1212. public void AddFlag(LLObject.ObjectFlags flag)
  1213. {
  1214. LLObject.ObjectFlags prevflag = Flags;
  1215. //uint objflags = Flags;
  1216. if ((ObjectFlags & (uint) flag) == 0)
  1217. {
  1218. //Console.WriteLine("Adding flag: " + ((LLObject.ObjectFlags) flag).ToString());
  1219. Flags |= flag;
  1220. }
  1221. //uint currflag = (uint)Flags;
  1222. //System.Console.WriteLine("Aprev: " + prevflag.ToString() + " curr: " + Flags.ToString());
  1223. //ScheduleFullUpdate();
  1224. }
  1225. public void RemFlag(LLObject.ObjectFlags flag)
  1226. {
  1227. LLObject.ObjectFlags prevflag = Flags;
  1228. if ((ObjectFlags & (uint) flag) != 0)
  1229. {
  1230. //Console.WriteLine("Removing flag: " + ((LLObject.ObjectFlags)flag).ToString());
  1231. Flags &= ~flag;
  1232. }
  1233. //System.Console.WriteLine("prev: " + prevflag.ToString() + " curr: " + Flags.ToString());
  1234. //ScheduleFullUpdate();
  1235. }
  1236. /// <summary>
  1237. /// Schedule a terse update for this prim. Terse updates only send position,
  1238. /// rotation, velocity, rotational velocity and shape information.
  1239. /// </summary>
  1240. public void ScheduleTerseUpdate()
  1241. {
  1242. if (m_updateFlag < 1)
  1243. {
  1244. if (m_parentGroup != null)
  1245. {
  1246. m_parentGroup.HasGroupChanged = true;
  1247. m_parentGroup.QueueForUpdateCheck();
  1248. }
  1249. TimeStampTerse = (uint) Util.UnixTimeSinceEpoch();
  1250. m_updateFlag = 1;
  1251. // m_log.DebugFormat(
  1252. // "[SCENE OBJECT PART]: Scheduling terse update for {0}, {1} at {2}",
  1253. // UUID, Name, TimeStampTerse);
  1254. }
  1255. }
  1256. /// <summary>
  1257. /// Tell all the prims which have had updates scheduled
  1258. /// </summary>
  1259. public void SendScheduledUpdates()
  1260. {
  1261. if (m_updateFlag == 1) //some change has been made so update the clients
  1262. {
  1263. AddTerseUpdateToAllAvatars();
  1264. ClearUpdateSchedule();
  1265. // This causes the Scene to 'poll' physical objects every couple of frames
  1266. // bad, so it's been replaced by an event driven method.
  1267. //if ((ObjectFlags & (uint)LLObject.ObjectFlags.Physics) != 0)
  1268. //{
  1269. // Only send the constant terse updates on physical objects!
  1270. //ScheduleTerseUpdate();
  1271. //}
  1272. }
  1273. else
  1274. {
  1275. if (m_updateFlag == 2) // is a new prim, just created/reloaded or has major changes
  1276. {
  1277. AddFullUpdateToAllAvatars();
  1278. ClearUpdateSchedule();
  1279. }
  1280. }
  1281. }
  1282. #endregion
  1283. #region Shape
  1284. /// <summary>
  1285. ///
  1286. /// </summary>
  1287. /// <param name="shapeBlock"></param>
  1288. public void UpdateShape(ObjectShapePacket.ObjectDataBlock shapeBlock)
  1289. {
  1290. m_shape.PathBegin = shapeBlock.PathBegin;
  1291. m_shape.PathEnd = shapeBlock.PathEnd;
  1292. m_shape.PathScaleX = shapeBlock.PathScaleX;
  1293. m_shape.PathScaleY = shapeBlock.PathScaleY;
  1294. m_shape.PathShearX = shapeBlock.PathShearX;
  1295. m_shape.PathShearY = shapeBlock.PathShearY;
  1296. m_shape.PathSkew = shapeBlock.PathSkew;
  1297. m_shape.ProfileBegin = shapeBlock.ProfileBegin;
  1298. m_shape.ProfileEnd = shapeBlock.ProfileEnd;
  1299. m_shape.PathCurve = shapeBlock.PathCurve;
  1300. m_shape.ProfileCurve = shapeBlock.ProfileCurve;
  1301. m_shape.ProfileHollow = shapeBlock.ProfileHollow;
  1302. m_shape.PathRadiusOffset = shapeBlock.PathRadiusOffset;
  1303. m_shape.PathRevolutions = shapeBlock.PathRevolutions;
  1304. m_shape.PathTaperX = shapeBlock.PathTaperX;
  1305. m_shape.PathTaperY = shapeBlock.PathTaperY;
  1306. m_shape.PathTwist = shapeBlock.PathTwist;
  1307. m_shape.PathTwistBegin = shapeBlock.PathTwistBegin;
  1308. if (PhysActor != null)
  1309. {
  1310. PhysActor.Shape = m_shape;
  1311. }
  1312. ScheduleFullUpdate();
  1313. }
  1314. #endregion
  1315. #region ExtraParams
  1316. public void UpdatePrimFlags(ushort type, bool inUse, byte[] data)
  1317. {
  1318. //m_log.Info("TSomething1:" + ((type & (ushort)ExtraParamType.Something1) == (ushort)ExtraParamType.Something1));
  1319. //m_log.Info("TSomething2:" + ((type & (ushort)ExtraParamType.Something2) == (ushort)ExtraParamType.Something2));
  1320. //m_log.Info("TSomething3:" + ((type & (ushort)ExtraParamType.Something3) == (ushort)ExtraParamType.Something3));
  1321. //m_log.Info("TSomething4:" + ((type & (ushort)ExtraParamType.Something4) == (ushort)ExtraParamType.Something4));
  1322. //m_log.Info("TSomething5:" + ((type & (ushort)ExtraParamType.Something5) == (ushort)ExtraParamType.Something5));
  1323. //m_log.Info("TSomething6:" + ((type & (ushort)ExtraParamType.Something6) == (ushort)ExtraParamType.Something6));
  1324. bool usePhysics = false;
  1325. bool IsTemporary = false;
  1326. bool IsPhantom = false;
  1327. bool castsShadows = false;
  1328. bool wasUsingPhysics = ((ObjectFlags & (uint) LLObject.ObjectFlags.Physics) != 0);
  1329. //bool IsLocked = false;
  1330. int i = 0;
  1331. try
  1332. {
  1333. i += 46;
  1334. //IsLocked = (data[i++] != 0) ? true : false;
  1335. usePhysics = ((data[i++] != 0) && m_parentGroup.Scene.m_physicalPrim) ? true : false;
  1336. //System.Console.WriteLine("U" + packet.ToBytes().Length.ToString());
  1337. IsTemporary = (data[i++] != 0) ? true : false;
  1338. IsPhantom = (data[i++] != 0) ? true : false;
  1339. castsShadows = (data[i++] != 0) ? true : false;
  1340. }
  1341. catch (Exception)
  1342. {
  1343. Console.WriteLine("Ignoring invalid Packet:");
  1344. //Silently ignore it - TODO: FIXME Quick
  1345. }
  1346. if (usePhysics)
  1347. {
  1348. AddFlag(LLObject.ObjectFlags.Physics);
  1349. if (!wasUsingPhysics)
  1350. {
  1351. DoPhysicsPropertyUpdate(usePhysics, false);
  1352. }
  1353. }
  1354. else
  1355. {
  1356. RemFlag(LLObject.ObjectFlags.Physics);
  1357. if (wasUsingPhysics)
  1358. {
  1359. DoPhysicsPropertyUpdate(usePhysics, false);
  1360. }
  1361. }
  1362. if (IsPhantom)
  1363. {
  1364. AddFlag(LLObject.ObjectFlags.Phantom);
  1365. if (PhysActor != null)
  1366. {
  1367. m_parentGroup.Scene.PhysicsScene.RemovePrim(PhysActor);
  1368. /// that's not wholesome. Had to make Scene public
  1369. PhysActor = null;
  1370. }
  1371. }
  1372. else
  1373. {
  1374. RemFlag(LLObject.ObjectFlags.Phantom);
  1375. if (PhysActor == null)
  1376. {
  1377. PhysActor = m_parentGroup.Scene.PhysicsScene.AddPrimShape(
  1378. Name,
  1379. Shape,
  1380. new PhysicsVector(AbsolutePosition.X, AbsolutePosition.Y,
  1381. AbsolutePosition.Z),
  1382. new PhysicsVector(Scale.X, Scale.Y, Scale.Z),
  1383. new Quaternion(RotationOffset.W, RotationOffset.X,
  1384. RotationOffset.Y, RotationOffset.Z), usePhysics);
  1385. if (PhysActor != null)
  1386. {
  1387. PhysActor.LocalID = LocalId;
  1388. DoPhysicsPropertyUpdate(usePhysics, true);
  1389. }
  1390. }
  1391. else
  1392. {
  1393. PhysActor.IsPhysical = usePhysics;
  1394. DoPhysicsPropertyUpdate(usePhysics, false);
  1395. }
  1396. }
  1397. if (IsTemporary)
  1398. {
  1399. AddFlag(LLObject.ObjectFlags.TemporaryOnRez);
  1400. }
  1401. else
  1402. {
  1403. RemFlag(LLObject.ObjectFlags.TemporaryOnRez);
  1404. }
  1405. // System.Console.WriteLine("Update: PHY:" + UsePhysics.ToString() + ", T:" + IsTemporary.ToString() + ", PHA:" + IsPhantom.ToString() + " S:" + CastsShadows.ToString());
  1406. ScheduleFullUpdate();
  1407. }
  1408. public void ScriptSetPhysicsStatus(bool UsePhysics)
  1409. {
  1410. if (m_parentGroup != null)
  1411. {
  1412. m_parentGroup.ScriptSetPhysicsStatus(UsePhysics);
  1413. }
  1414. }
  1415. public void ScriptSetPhantomStatus(bool Phantom)
  1416. {
  1417. if (m_parentGroup != null)
  1418. {
  1419. m_parentGroup.ScriptSetPhantomStatus(Phantom);
  1420. }
  1421. }
  1422. public void DoPhysicsPropertyUpdate(bool UsePhysics, bool isNew)
  1423. {
  1424. if (PhysActor != null)
  1425. {
  1426. if (UsePhysics != PhysActor.IsPhysical || isNew)
  1427. {
  1428. if (PhysActor.IsPhysical)
  1429. {
  1430. if (!isNew)
  1431. ParentGroup.Scene.RemovePhysicalPrim(1);
  1432. PhysActor.OnRequestTerseUpdate -= PhysicsRequestingTerseUpdate;
  1433. PhysActor.OnOutOfBounds -= PhysicsOutOfBounds;
  1434. PhysActor.delink();
  1435. }
  1436. PhysActor.IsPhysical = UsePhysics;
  1437. // If we're not what we're supposed to be in the physics scene, recreate ourselves.
  1438. //m_parentGroup.Scene.PhysicsScene.RemovePrim(PhysActor);
  1439. /// that's not wholesome. Had to make Scene public
  1440. //PhysActor = null;
  1441. if ((ObjectFlags & (uint) LLObject.ObjectFlags.Phantom) == 0)
  1442. {
  1443. //PhysActor = m_parentGroup.Scene.PhysicsScene.AddPrimShape(
  1444. //Name,
  1445. //Shape,
  1446. //new PhysicsVector(AbsolutePosition.X, AbsolutePosition.Y,
  1447. //AbsolutePosition.Z),
  1448. //new PhysicsVector(Scale.X, Scale.Y, Scale.Z),
  1449. //new Quaternion(RotationOffset.W, RotationOffset.X,
  1450. //RotationOffset.Y, RotationOffset.Z), UsePhysics);
  1451. if (UsePhysics)
  1452. {
  1453. ParentGroup.Scene.AddPhysicalPrim(1);
  1454. PhysActor.OnRequestTerseUpdate += PhysicsRequestingTerseUpdate;
  1455. PhysActor.OnOutOfBounds += PhysicsOutOfBounds;
  1456. if (ParentID != 0 && ParentID != LocalId)
  1457. {
  1458. if (ParentGroup.RootPart.PhysActor != null)
  1459. {
  1460. PhysActor.link(ParentGroup.RootPart.PhysActor);
  1461. }
  1462. }
  1463. }
  1464. }
  1465. }
  1466. m_parentGroup.Scene.PhysicsScene.AddPhysicsActorTaint(PhysActor);
  1467. }
  1468. }
  1469. public void UpdateExtraParam(ushort type, bool inUse, byte[] data)
  1470. {
  1471. m_shape.ExtraParams = new byte[data.Length + 7];
  1472. int i = 0;
  1473. uint length = (uint) data.Length;
  1474. m_shape.ExtraParams[i++] = 1;
  1475. m_shape.ExtraParams[i++] = (byte) (type%256);
  1476. m_shape.ExtraParams[i++] = (byte) ((type >> 8)%256);
  1477. m_shape.ExtraParams[i++] = (byte) (length%256);
  1478. m_shape.ExtraParams[i++] = (byte) ((length >> 8)%256);
  1479. m_shape.ExtraParams[i++] = (byte) ((length >> 16)%256);
  1480. m_shape.ExtraParams[i++] = (byte) ((length >> 24)%256);
  1481. Array.Copy(data, 0, m_shape.ExtraParams, i, data.Length);
  1482. ScheduleFullUpdate();
  1483. }
  1484. #endregion
  1485. #region Physics
  1486. public float GetMass()
  1487. {
  1488. if (PhysActor != null)
  1489. {
  1490. return PhysActor.Mass;
  1491. }
  1492. else
  1493. {
  1494. return 0;
  1495. }
  1496. }
  1497. public LLVector3 GetGeometricCenter()
  1498. {
  1499. if (PhysActor != null)
  1500. {
  1501. return new LLVector3(PhysActor.CenterOfMass.X, PhysActor.CenterOfMass.Y, PhysActor.CenterOfMass.Z);
  1502. }
  1503. else
  1504. {
  1505. return new LLVector3(0, 0, 0);
  1506. }
  1507. }
  1508. #endregion
  1509. #region Texture
  1510. /// <summary>
  1511. ///
  1512. /// </summary>
  1513. /// <param name="textureEntry"></param>
  1514. public void UpdateTextureEntry(byte[] textureEntry)
  1515. {
  1516. m_shape.TextureEntry = textureEntry;
  1517. TriggerScriptChangedEvent(Changed.TEXTURE);
  1518. ScheduleFullUpdate();
  1519. }
  1520. // Added to handle bug in libsecondlife's TextureEntry.ToBytes()
  1521. // not handling RGBA properly. Cycles through, and "fixes" the color
  1522. // info
  1523. public void UpdateTexture(LLObject.TextureEntry tex)
  1524. {
  1525. //LLColor tmpcolor;
  1526. //for (uint i = 0; i < 32; i++)
  1527. //{
  1528. // if (tex.FaceTextures[i] != null)
  1529. // {
  1530. // tmpcolor = tex.GetFace((uint) i).RGBA;
  1531. // tmpcolor.A = tmpcolor.A*255;
  1532. // tmpcolor.R = tmpcolor.R*255;
  1533. // tmpcolor.G = tmpcolor.G*255;
  1534. // tmpcolor.B = tmpcolor.B*255;
  1535. // tex.FaceTextures[i].RGBA = tmpcolor;
  1536. // }
  1537. //}
  1538. //tmpcolor = tex.DefaultTexture.RGBA;
  1539. //tmpcolor.A = tmpcolor.A*255;
  1540. //tmpcolor.R = tmpcolor.R*255;
  1541. //tmpcolor.G = tmpcolor.G*255;
  1542. //tmpcolor.B = tmpcolor.B*255;
  1543. //tex.DefaultTexture.RGBA = tmpcolor;
  1544. UpdateTextureEntry(tex.ToBytes());
  1545. }
  1546. public byte ConvertScriptUintToByte(uint indata)
  1547. {
  1548. byte outdata = (byte)TextureAnimFlags.NONE;
  1549. if ((indata & 1) != 0) outdata |= (byte)TextureAnimFlags.ANIM_ON;
  1550. if ((indata & 2) != 0) outdata |= (byte)TextureAnimFlags.LOOP;
  1551. if ((indata & 4) != 0) outdata |= (byte)TextureAnimFlags.REVERSE;
  1552. if ((indata & 8) != 0) outdata |= (byte)TextureAnimFlags.PING_PONG;
  1553. if ((indata & 16) != 0) outdata |= (byte)TextureAnimFlags.SMOOTH;
  1554. if ((indata & 32) != 0) outdata |= (byte)TextureAnimFlags.ROTATE;
  1555. if ((indata & 64) != 0) outdata |= (byte)TextureAnimFlags.SCALE;
  1556. return outdata;
  1557. }
  1558. public void AddTextureAnimation(Primitive.TextureAnimation pTexAnim)
  1559. {
  1560. byte[] data = new byte[16];
  1561. int pos = 0;
  1562. // The flags don't like conversion from uint to byte, so we have to do
  1563. // it the crappy way. See the above function :(
  1564. data[pos] = ConvertScriptUintToByte(pTexAnim.Flags); pos++;
  1565. data[pos] = (byte)pTexAnim.Face; pos++;
  1566. data[pos] = (byte)pTexAnim.SizeX; pos++;
  1567. data[pos] = (byte)pTexAnim.SizeX; pos++;
  1568. Helpers.FloatToBytes(pTexAnim.Start).CopyTo(data, pos);
  1569. Helpers.FloatToBytes(pTexAnim.Length ).CopyTo(data, pos + 4);
  1570. Helpers.FloatToBytes(pTexAnim.Rate).CopyTo(data, pos + 8);
  1571. m_TextureAnimation = data;
  1572. }
  1573. #endregion
  1574. #region ParticleSystem
  1575. public void AddNewParticleSystem(Primitive.ParticleSystem pSystem)
  1576. {
  1577. m_particleSystem = pSystem.GetBytes();
  1578. }
  1579. #endregion
  1580. #region Position
  1581. /// <summary>
  1582. ///
  1583. /// </summary>
  1584. /// <param name="pos"></param>
  1585. public void UpdateOffSet(LLVector3 pos)
  1586. {
  1587. LLVector3 newPos = new LLVector3(pos.X, pos.Y, pos.Z);
  1588. OffsetPosition = newPos;
  1589. ScheduleTerseUpdate();
  1590. }
  1591. public void UpdateGroupPosition(LLVector3 pos)
  1592. {
  1593. LLVector3 newPos = new LLVector3(pos.X, pos.Y, pos.Z);
  1594. GroupPosition = newPos;
  1595. ScheduleTerseUpdate();
  1596. }
  1597. #endregion
  1598. #region rotation
  1599. public void UpdateRotation(LLQuaternion rot)
  1600. {
  1601. RotationOffset = new LLQuaternion(rot.X, rot.Y, rot.Z, rot.W);
  1602. ScheduleTerseUpdate();
  1603. }
  1604. #endregion
  1605. #region Sound
  1606. public void PreloadSound(string sound)
  1607. {
  1608. LLUUID ownerID = OwnerID;
  1609. LLUUID objectID = UUID;
  1610. LLUUID soundID = LLUUID.Zero;
  1611. if (!LLUUID.TryParse(sound, out soundID))
  1612. {
  1613. //Trys to fetch sound id from prim's inventory.
  1614. //Prim's inventory doesn't support non script items yet
  1615. SceneObjectPart op = this;
  1616. foreach (KeyValuePair<LLUUID, TaskInventoryItem> item in op.TaskInventory)
  1617. {
  1618. if (item.Value.Name == sound)
  1619. {
  1620. soundID = item.Value.ItemID;
  1621. break;
  1622. }
  1623. }
  1624. }
  1625. List<ScenePresence> avatarts = m_parentGroup.Scene.GetAvatars();
  1626. foreach (ScenePresence p in avatarts)
  1627. {
  1628. // TODO: some filtering by distance of avatar
  1629. p.ControllingClient.SendPreLoadSound(objectID, objectID, soundID);
  1630. }
  1631. }
  1632. public void AdjustSoundGain(double volume)
  1633. {
  1634. if (volume > 1)
  1635. volume = 1;
  1636. if (volume < 0)
  1637. volume = 0;
  1638. List<ScenePresence> avatarts = m_parentGroup.Scene.GetAvatars();
  1639. foreach (ScenePresence p in avatarts)
  1640. {
  1641. p.ControllingClient.SendAttachedSoundGainChange(UUID, (float)volume);
  1642. }
  1643. }
  1644. public void SendSound(string sound, double volume, bool triggered, byte flags)
  1645. {
  1646. if (volume > 1)
  1647. volume = 1;
  1648. if (volume < 0)
  1649. volume = 0;
  1650. LLUUID ownerID = OwnerID;
  1651. LLUUID objectID = UUID;
  1652. LLUUID parentID = GetRootPartUUID();
  1653. LLUUID soundID = LLUUID.Zero;
  1654. LLVector3 position = AbsolutePosition; // region local
  1655. ulong regionHandle = m_parentGroup.Scene.RegionInfo.RegionHandle;
  1656. //byte flags = 0;
  1657. if (!LLUUID.TryParse(sound, out soundID))
  1658. {
  1659. // search sound file from inventory
  1660. SceneObjectPart op = this;
  1661. foreach (KeyValuePair<LLUUID, TaskInventoryItem> item in op.TaskInventory)
  1662. {
  1663. if (item.Value.Name == sound)
  1664. {
  1665. soundID = item.Value.ItemID;
  1666. break;
  1667. }
  1668. }
  1669. }
  1670. List<ScenePresence> avatarts = m_parentGroup.Scene.GetAvatars();
  1671. foreach (ScenePresence p in avatarts)
  1672. {
  1673. // TODO: some filtering by distance of avatar
  1674. if (triggered)
  1675. {
  1676. p.ControllingClient.SendTriggeredSound(soundID, ownerID, objectID, parentID, regionHandle, position, (float)volume);
  1677. }
  1678. else
  1679. {
  1680. p.ControllingClient.SendPlayAttachedSound(soundID, objectID, ownerID, (float)volume, flags);
  1681. }
  1682. }
  1683. }
  1684. #endregion
  1685. #region Resizing/Scale
  1686. /// <summary>
  1687. ///
  1688. /// </summary>
  1689. /// <param name="scale"></param>
  1690. public void Resize(LLVector3 scale)
  1691. {
  1692. m_shape.Scale = scale;
  1693. ScheduleFullUpdate();
  1694. }
  1695. #endregion
  1696. public void UpdatePermissions(LLUUID AgentID, byte field, uint localID, uint mask, byte addRemTF)
  1697. {
  1698. bool set = addRemTF == 1;
  1699. // Are we the owner?
  1700. if (AgentID == OwnerID)
  1701. {
  1702. switch (field)
  1703. {
  1704. case 2:
  1705. OwnerMask = ApplyMask(OwnerMask, set, mask);
  1706. break;
  1707. case 4:
  1708. GroupMask = ApplyMask(GroupMask, set, mask);
  1709. break;
  1710. case 8:
  1711. EveryoneMask = ApplyMask(EveryoneMask, set, mask);
  1712. break;
  1713. case 16:
  1714. NextOwnerMask = ApplyMask(NextOwnerMask, set, mask);
  1715. break;
  1716. }
  1717. SendFullUpdateToAllClients();
  1718. SendObjectPropertiesToClient(AgentID);
  1719. }
  1720. }
  1721. private void SendObjectPropertiesToClient(LLUUID AgentID)
  1722. {
  1723. List<ScenePresence> avatars = m_parentGroup.GetScenePresences();
  1724. for (int i = 0; i < avatars.Count; i++)
  1725. {
  1726. // Ugly reference :(
  1727. if (avatars[i].UUID == AgentID)
  1728. {
  1729. m_parentGroup.GetProperties(avatars[i].ControllingClient);
  1730. }
  1731. }
  1732. }
  1733. private uint ApplyMask(uint val, bool set, uint mask)
  1734. {
  1735. if (set)
  1736. {
  1737. return val |= mask;
  1738. }
  1739. else
  1740. {
  1741. return val &= ~mask;
  1742. }
  1743. }
  1744. #region Client Update Methods
  1745. /// <summary>
  1746. /// Tell all scene presences that they should send updates for this part to their clients
  1747. /// </summary>
  1748. public void AddFullUpdateToAllAvatars()
  1749. {
  1750. List<ScenePresence> avatars = m_parentGroup.GetScenePresences();
  1751. for (int i = 0; i < avatars.Count; i++)
  1752. {
  1753. avatars[i].QueuePartForUpdate(this);
  1754. }
  1755. }
  1756. public void SendFullUpdateToAllClientsExcept(LLUUID agentID)
  1757. {
  1758. List<ScenePresence> avatars = m_parentGroup.GetScenePresences();
  1759. for (int i = 0; i < avatars.Count; i++)
  1760. {
  1761. // Ugly reference :(
  1762. if (avatars[i].UUID != agentID)
  1763. {
  1764. m_parentGroup.SendPartFullUpdate(avatars[i].ControllingClient, this,
  1765. avatars[i].GenerateClientFlags(UUID));
  1766. }
  1767. }
  1768. }
  1769. public void AddFullUpdateToAvatar(ScenePresence presence)
  1770. {
  1771. presence.QueuePartForUpdate(this);
  1772. }
  1773. /// <summary>
  1774. ///
  1775. /// </summary>
  1776. public void SendFullUpdateToAllClients()
  1777. {
  1778. List<ScenePresence> avatars = m_parentGroup.GetScenePresences();
  1779. for (int i = 0; i < avatars.Count; i++)
  1780. {
  1781. // Ugly reference :(
  1782. m_parentGroup.SendPartFullUpdate(avatars[i].ControllingClient, this,
  1783. avatars[i].GenerateClientFlags(UUID));
  1784. }
  1785. }
  1786. /// <summary>
  1787. ///
  1788. /// </summary>
  1789. /// <param name="remoteClient"></param>
  1790. public void SendFullUpdate(IClientAPI remoteClient, uint clientFlags)
  1791. {
  1792. m_parentGroup.SendPartFullUpdate(remoteClient, this, clientFlags);
  1793. }
  1794. /// <summary>
  1795. ///
  1796. /// </summary>
  1797. /// <param name="remoteClient"></param>
  1798. public void SendFullUpdateToClient(IClientAPI remoteClient, uint clientflags)
  1799. {
  1800. LLVector3 lPos;
  1801. lPos = OffsetPosition;
  1802. SendFullUpdateToClient(remoteClient, lPos, clientflags);
  1803. }
  1804. public void SendFullUpdateToClient(IClientAPI remoteClient, LLVector3 lPos, uint clientFlags)
  1805. {
  1806. LLQuaternion lRot;
  1807. lRot = RotationOffset;
  1808. clientFlags &= ~(uint) LLObject.ObjectFlags.CreateSelected;
  1809. if (remoteClient.AgentId == OwnerID)
  1810. {
  1811. if ((uint) (Flags & LLObject.ObjectFlags.CreateSelected) != 0)
  1812. {
  1813. clientFlags |= (uint) LLObject.ObjectFlags.CreateSelected;
  1814. Flags &= ~LLObject.ObjectFlags.CreateSelected;
  1815. }
  1816. }
  1817. byte[] color = new byte[] {m_color.R, m_color.G, m_color.B, m_color.A};
  1818. remoteClient.SendPrimitiveToClient(m_regionHandle, (ushort)(m_parentGroup.GetTimeDilation() * (float)ushort.MaxValue), LocalId, m_shape, lPos, clientFlags, m_uuid,
  1819. OwnerID,
  1820. m_text, color, ParentID, m_particleSystem, lRot, m_clickAction, m_TextureAnimation);
  1821. }
  1822. /// Terse updates
  1823. public void AddTerseUpdateToAllAvatars()
  1824. {
  1825. List<ScenePresence> avatars = m_parentGroup.GetScenePresences();
  1826. for (int i = 0; i < avatars.Count; i++)
  1827. {
  1828. avatars[i].QueuePartForUpdate(this);
  1829. }
  1830. }
  1831. public void AddTerseUpdateToAvatar(ScenePresence presence)
  1832. {
  1833. presence.QueuePartForUpdate(this);
  1834. }
  1835. /// <summary>
  1836. ///
  1837. /// </summary>
  1838. public void SendTerseUpdateToAllClients()
  1839. {
  1840. List<ScenePresence> avatars = m_parentGroup.GetScenePresences();
  1841. for (int i = 0; i < avatars.Count; i++)
  1842. {
  1843. m_parentGroup.SendPartTerseUpdate(avatars[i].ControllingClient, this);
  1844. }
  1845. }
  1846. /// <summary>
  1847. /// Send a terse update to the client.
  1848. /// </summary>
  1849. /// <param name="remoteClient"></param>
  1850. public void SendTerseUpdate(IClientAPI remoteClient)
  1851. {
  1852. m_parentGroup.SendPartTerseUpdate(remoteClient, this);
  1853. }
  1854. public void SendTerseUpdateToClient(IClientAPI remoteClient)
  1855. {
  1856. LLVector3 lPos;
  1857. lPos = OffsetPosition;
  1858. LLQuaternion mRot = RotationOffset;
  1859. if ((ObjectFlags & (uint) LLObject.ObjectFlags.Physics) == 0)
  1860. {
  1861. remoteClient.SendPrimTerseUpdate(m_regionHandle, (ushort)(m_parentGroup.GetTimeDilation() * (float)ushort.MaxValue), LocalId, lPos, mRot, Shape.State);
  1862. }
  1863. else
  1864. {
  1865. remoteClient.SendPrimTerseUpdate(m_regionHandle, (ushort)(m_parentGroup.GetTimeDilation() * (float)ushort.MaxValue), LocalId, lPos, mRot, Velocity,
  1866. RotationalVelocity);
  1867. //System.Console.WriteLine("LID: " + LocalID + " RVel:" + RotationalVelocity.ToString() + " TD: " + ((ushort)(m_parentGroup.Scene.TimeDilation * 500000f)).ToString() + ":" + m_parentGroup.Scene.TimeDilation.ToString());
  1868. }
  1869. }
  1870. public void SendTerseUpdateToClient(IClientAPI remoteClient, LLVector3 lPos)
  1871. {
  1872. LLQuaternion mRot = RotationOffset;
  1873. if ((ObjectFlags & (uint) LLObject.ObjectFlags.Physics) == 0)
  1874. {
  1875. remoteClient.SendPrimTerseUpdate(m_regionHandle, (ushort)(m_parentGroup.GetTimeDilation() * (float)ushort.MaxValue), LocalId, lPos, mRot, Shape.State);
  1876. }
  1877. else
  1878. {
  1879. remoteClient.SendPrimTerseUpdate(m_regionHandle, (ushort)(m_parentGroup.GetTimeDilation() * (float)ushort.MaxValue), LocalId, lPos, mRot, Velocity,
  1880. RotationalVelocity);
  1881. //System.Console.WriteLine("LID: " + LocalID + "RVel:" + RotationalVelocity.ToString() + " TD: " + ((ushort)(m_parentGroup.Scene.TimeDilation * 500000f)).ToString() + ":" + m_parentGroup.Scene.TimeDilation.ToString());
  1882. }
  1883. }
  1884. #endregion
  1885. public virtual void UpdateMovement()
  1886. {
  1887. }
  1888. #region Events
  1889. public void PhysicsRequestingTerseUpdate()
  1890. {
  1891. if (PhysActor != null)
  1892. {
  1893. LLVector3 newpos = new LLVector3(PhysActor.Position.GetBytes(), 0);
  1894. if (newpos.X > 257f || newpos.X < -1f || newpos.Y > 257f || newpos.Y < -1f)
  1895. {
  1896. m_parentGroup.AbsolutePosition = newpos;
  1897. return;
  1898. }
  1899. }
  1900. ScheduleTerseUpdate();
  1901. //SendTerseUpdateToAllClients();
  1902. }
  1903. #endregion
  1904. public void PhysicsOutOfBounds(PhysicsVector pos)
  1905. {
  1906. m_log.Info("[PHYSICS]: Physical Object went out of bounds.");
  1907. RemFlag(LLObject.ObjectFlags.Physics);
  1908. DoPhysicsPropertyUpdate(false, true);
  1909. //m_parentGroup.Scene.PhysicsScene.AddPhysicsActorTaint(PhysActor);
  1910. }
  1911. public virtual void OnGrab(LLVector3 offsetPos, IClientAPI remoteClient)
  1912. {
  1913. }
  1914. public void SetText(string text)
  1915. {
  1916. Text = text;
  1917. ScheduleFullUpdate();
  1918. }
  1919. public void SetText(string text, Vector3 color, double alpha)
  1920. {
  1921. Color = Color.FromArgb(0xff - (int) (alpha*0xff),
  1922. (int) (color.x*0xff),
  1923. (int) (color.y*0xff),
  1924. (int) (color.z*0xff));
  1925. SetText( text );
  1926. }
  1927. protected SceneObjectPart(SerializationInfo info, StreamingContext context)
  1928. {
  1929. //System.Console.WriteLine("SceneObjectPart Deserialize BGN");
  1930. if (info == null)
  1931. {
  1932. throw new System.ArgumentNullException("info");
  1933. }
  1934. /*
  1935. m_queue = (Queue<SceneObjectPart>)info.GetValue("m_queue", typeof(Queue<SceneObjectPart>));
  1936. m_ids = (List<LLUUID>)info.GetValue("m_ids", typeof(List<LLUUID>));
  1937. */
  1938. //System.Console.WriteLine("SceneObjectPart Deserialize END");
  1939. }
  1940. [SecurityPermission(SecurityAction.LinkDemand,
  1941. Flags = SecurityPermissionFlag.SerializationFormatter)]
  1942. public virtual void GetObjectData(
  1943. SerializationInfo info, StreamingContext context)
  1944. {
  1945. if (info == null)
  1946. {
  1947. throw new System.ArgumentNullException("info");
  1948. }
  1949. info.AddValue("m_inventoryFileName", m_inventoryFileName);
  1950. info.AddValue("m_folderID", m_folderID.UUID);
  1951. info.AddValue("PhysActor", PhysActor);
  1952. Dictionary<Guid, TaskInventoryItem> TaskInventory_work = new Dictionary<Guid, TaskInventoryItem>();
  1953. foreach (LLUUID id in TaskInventory.Keys)
  1954. {
  1955. TaskInventory_work.Add(id.UUID, TaskInventory[id]);
  1956. }
  1957. info.AddValue("TaskInventory", TaskInventory_work);
  1958. info.AddValue("LastOwnerID", LastOwnerID.UUID);
  1959. info.AddValue("OwnerID", OwnerID.UUID);
  1960. info.AddValue("GroupID", GroupID.UUID);
  1961. info.AddValue("OwnershipCost", OwnershipCost);
  1962. info.AddValue("ObjectSaleType", ObjectSaleType);
  1963. info.AddValue("SalePrice", SalePrice);
  1964. info.AddValue("Category", Category);
  1965. info.AddValue("CreationDate", CreationDate);
  1966. info.AddValue("ParentID", ParentID);
  1967. info.AddValue("OwnerMask", OwnerMask);
  1968. info.AddValue("NextOwnerMask", NextOwnerMask);
  1969. info.AddValue("GroupMask", GroupMask);
  1970. info.AddValue("EveryoneMask", EveryoneMask);
  1971. info.AddValue("BaseMask", BaseMask);
  1972. info.AddValue("m_particleSystem", m_particleSystem);
  1973. info.AddValue("TimeStampFull", TimeStampFull);
  1974. info.AddValue("TimeStampTerse", TimeStampTerse);
  1975. info.AddValue("TimeStampLastActivity", TimeStampLastActivity);
  1976. info.AddValue("m_updateFlag", m_updateFlag);
  1977. info.AddValue("CreatorID", CreatorID.UUID);
  1978. info.AddValue("m_inventorySerial", m_inventorySerial);
  1979. info.AddValue("m_uuid", m_uuid.UUID);
  1980. info.AddValue("m_localID", m_localId);
  1981. info.AddValue("m_name", m_name);
  1982. info.AddValue("m_flags", Flags);
  1983. info.AddValue("m_material", m_material);
  1984. info.AddValue("m_regionHandle", m_regionHandle);
  1985. info.AddValue("m_groupPosition.X", m_groupPosition.X);
  1986. info.AddValue("m_groupPosition.Y", m_groupPosition.Y);
  1987. info.AddValue("m_groupPosition.Z", m_groupPosition.Z);
  1988. info.AddValue("m_offsetPosition.X", m_offsetPosition.X);
  1989. info.AddValue("m_offsetPosition.Y", m_offsetPosition.Y);
  1990. info.AddValue("m_offsetPosition.Z", m_offsetPosition.Z);
  1991. info.AddValue("m_rotationOffset.W", m_rotationOffset.W);
  1992. info.AddValue("m_rotationOffset.X", m_rotationOffset.X);
  1993. info.AddValue("m_rotationOffset.Y", m_rotationOffset.Y);
  1994. info.AddValue("m_rotationOffset.Z", m_rotationOffset.Z);
  1995. info.AddValue("m_velocity.X", m_velocity.X);
  1996. info.AddValue("m_velocity.Y", m_velocity.Y);
  1997. info.AddValue("m_velocity.Z", m_velocity.Z);
  1998. info.AddValue("m_rotationalvelocity.X", m_rotationalvelocity.X);
  1999. info.AddValue("m_rotationalvelocity.Y", m_rotationalvelocity.Y);
  2000. info.AddValue("m_rotationalvelocity.Z", m_rotationalvelocity.Z);
  2001. info.AddValue("m_angularVelocity.X", m_angularVelocity.X);
  2002. info.AddValue("m_angularVelocity.Y", m_angularVelocity.Y);
  2003. info.AddValue("m_angularVelocity.Z", m_angularVelocity.Z);
  2004. info.AddValue("m_acceleration.X", m_acceleration.X);
  2005. info.AddValue("m_acceleration.Y", m_acceleration.Y);
  2006. info.AddValue("m_acceleration.Z", m_acceleration.Z);
  2007. info.AddValue("m_description", m_description);
  2008. info.AddValue("m_color", m_color);
  2009. info.AddValue("m_text", m_text);
  2010. info.AddValue("m_sitName", m_sitName);
  2011. info.AddValue("m_touchName", m_touchName);
  2012. info.AddValue("m_clickAction", m_clickAction);
  2013. info.AddValue("m_shape", m_shape);
  2014. info.AddValue("m_parentGroup", m_parentGroup);
  2015. }
  2016. }
  2017. }