Avatar.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Copyright (c) Contributors, http://www.openmetaverse.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. */
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Text;
  32. using libsecondlife;
  33. using libsecondlife.Packets;
  34. using OpenSim.Physics.Manager;
  35. using OpenSim.Framework.Inventory;
  36. using OpenSim.Framework.Interfaces;
  37. using OpenSim.Framework.Types;
  38. using Axiom.MathLib;
  39. namespace OpenSim.Region.Scenes
  40. {
  41. public partial class Avatar : Entity
  42. {
  43. public static bool PhysicsEngineFlying = false;
  44. public static AvatarAnimations Animations;
  45. public string firstname;
  46. public string lastname;
  47. public IClientAPI ControllingClient;
  48. public LLUUID current_anim;
  49. public int anim_seq;
  50. private static libsecondlife.Packets.ObjectUpdatePacket.ObjectDataBlock AvatarTemplate;
  51. private bool updateflag = false;
  52. private byte movementflag = 0;
  53. private List<NewForce> forcesList = new List<NewForce>();
  54. private short _updateCount = 0;
  55. private Axiom.MathLib.Quaternion bodyRot;
  56. private LLObject.TextureEntry avatarAppearanceTexture = null;
  57. private byte[] visualParams;
  58. private AvatarWearable[] Wearables;
  59. private LLVector3 positionLastFrame = new LLVector3(0, 0, 0);
  60. private ulong m_regionHandle;
  61. private Dictionary<uint, IClientAPI> m_clientThreads;
  62. private bool childAvatar = false;
  63. private bool newForce = false;
  64. protected RegionInfo m_regionInfo;
  65. /// <summary>
  66. ///
  67. /// </summary>
  68. /// <param name="theClient"></param>
  69. /// <param name="world"></param>
  70. /// <param name="clientThreads"></param>
  71. /// <param name="regionDat"></param>
  72. public Avatar(IClientAPI theClient, Scene world, Dictionary<uint, IClientAPI> clientThreads, RegionInfo reginfo)
  73. {
  74. m_world = world;
  75. m_clientThreads = clientThreads;
  76. this.uuid = theClient.AgentId;
  77. m_regionInfo = reginfo;
  78. m_regionHandle = reginfo.RegionHandle;
  79. OpenSim.Framework.Console.MainConsole.Instance.WriteLine(OpenSim.Framework.Console.LogPriority.LOW, "Avatar.cs - Loading details from grid (DUMMY)");
  80. ControllingClient = theClient;
  81. this.firstname = ControllingClient.FirstName;
  82. this.lastname = ControllingClient.LastName;
  83. localid = this.m_world.NextLocalId;
  84. Pos = ControllingClient.StartPos;
  85. visualParams = new byte[218];
  86. for (int i = 0; i < 218; i++)
  87. {
  88. visualParams[i] = 100;
  89. }
  90. Wearables = AvatarWearable.DefaultWearables;
  91. this.avatarAppearanceTexture = new LLObject.TextureEntry(new LLUUID("00000000-0000-0000-5005-000000000005"));
  92. //register for events
  93. ControllingClient.OnRequestWearables += new GenericCall(this.SendOurAppearance);
  94. //ControllingClient.OnSetAppearance += new SetAppearance(this.SetAppearance);
  95. ControllingClient.OnCompleteMovementToRegion += new GenericCall2(this.CompleteMovement);
  96. ControllingClient.OnCompleteMovementToRegion += new GenericCall2(this.SendInitialPosition);
  97. ControllingClient.OnAgentUpdate += new UpdateAgent(this.HandleAgentUpdate);
  98. // ControllingClient.OnStartAnim += new StartAnim(this.SendAnimPack);
  99. // ControllingClient.OnChildAgentStatus += new StatusChange(this.ChildStatusChange);
  100. //ControllingClient.OnStopMovement += new GenericCall2(this.StopMovement);
  101. }
  102. /// <summary>
  103. ///
  104. /// </summary>
  105. public PhysicsActor PhysActor
  106. {
  107. set
  108. {
  109. this._physActor = value;
  110. }
  111. get
  112. {
  113. return _physActor;
  114. }
  115. }
  116. /// <summary>
  117. ///
  118. /// </summary>
  119. /// <param name="status"></param>
  120. public void ChildStatusChange(bool status)
  121. {
  122. this.childAvatar = status;
  123. if (this.childAvatar == true)
  124. {
  125. this.Velocity = new LLVector3(0, 0, 0);
  126. this.Pos = new LLVector3(128, 128, 70);
  127. }
  128. }
  129. /// <summary>
  130. ///
  131. /// </summary>
  132. /// <param name="pos"></param>
  133. public void UpGradeAvatar(LLVector3 pos)
  134. {
  135. //this.childAvatar = false;
  136. this.Pos = pos;
  137. }
  138. protected void DownGradeAvatar()
  139. {
  140. this.Velocity = new LLVector3(0, 0, 0);
  141. this.Pos = new LLVector3(128, 128, 70);
  142. this.childAvatar = true;
  143. }
  144. /// <summary>
  145. ///
  146. /// </summary>
  147. /// <param name="pos"></param>
  148. public void Teleport(LLVector3 pos)
  149. {
  150. this.Pos = pos;
  151. this.SendTerseUpdateToALLClients();
  152. }
  153. /// <summary>
  154. ///
  155. /// </summary>
  156. public override void addForces()
  157. {
  158. newForce = false;
  159. lock (this.forcesList)
  160. {
  161. if (this.forcesList.Count > 0)
  162. {
  163. for (int i = 0; i < this.forcesList.Count; i++)
  164. {
  165. NewForce force = this.forcesList[i];
  166. this.updateflag = true;
  167. this.Velocity = new LLVector3(force.X, force.Y, force.Z); //shouldn't really be doing this
  168. this.newForce = true;
  169. }
  170. for (int i = 0; i < this.forcesList.Count; i++)
  171. {
  172. this.forcesList.RemoveAt(0);
  173. }
  174. }
  175. }
  176. }
  177. public void SendTerseUpdateToClient(IClientAPI RemoteClient)
  178. {
  179. LLVector3 pos = this.Pos;
  180. LLVector3 vel = this.Velocity;
  181. RemoteClient.SendAvatarTerseUpdate(this.m_regionHandle, 64096, this.localid, new LLVector3(pos.X, pos.Y, pos.Z), new LLVector3(vel.X, vel.Y, vel.Z));
  182. }
  183. /// <summary>
  184. ///
  185. /// </summary>
  186. public void SendTerseUpdateToALLClients()
  187. {
  188. List<Avatar> avatars = this.m_world.RequestAvatarList();
  189. for (int i = 0; i < avatars.Count; i++)
  190. {
  191. this.SendTerseUpdateToClient(avatars[i].ControllingClient);
  192. }
  193. }
  194. /// <summary>
  195. /// Complete Avatar's movement into the region
  196. /// </summary>
  197. public void CompleteMovement()
  198. {
  199. LLVector3 look = this.Velocity;
  200. if ((look.X == 0) && (look.Y == 0) && (look.Z == 0))
  201. {
  202. look = new LLVector3(0.99f, 0.042f, 0);
  203. }
  204. this.ControllingClient.MoveAgentIntoRegion(m_regionInfo, Pos, look);
  205. if (this.childAvatar)
  206. {
  207. this.childAvatar = false;
  208. }
  209. }
  210. /// <summary>
  211. ///
  212. /// </summary>
  213. /// <param name="pack"></param>
  214. public void HandleAgentUpdate(IClientAPI remoteClient, uint flags, LLQuaternion bodyRotation)
  215. {
  216. if ((flags & (uint)MainAvatar.ControlFlags.AGENT_CONTROL_AT_POS) != 0)
  217. {
  218. Axiom.MathLib.Quaternion q = new Axiom.MathLib.Quaternion(bodyRotation.W, bodyRotation.X, bodyRotation.Y, bodyRotation.Z);
  219. if (((movementflag & 1) == 0) || (q != this.bodyRot))
  220. {
  221. //we should add a new force to the list
  222. // but for now we will deal with velocities
  223. NewForce newVelocity = new NewForce();
  224. Axiom.MathLib.Vector3 v3 = new Axiom.MathLib.Vector3(1, 0, 0);
  225. Axiom.MathLib.Vector3 direc = q * v3;
  226. direc.Normalize();
  227. //work out velocity for sim physics system
  228. direc = direc * ((0.03f) * 128f);
  229. if (this._physActor.Flying)
  230. direc *= 4;
  231. newVelocity.X = direc.x;
  232. newVelocity.Y = direc.y;
  233. newVelocity.Z = direc.z;
  234. this.forcesList.Add(newVelocity);
  235. movementflag = 1;
  236. this.bodyRot = q;
  237. }
  238. }
  239. else
  240. {
  241. if ((movementflag) != 0)
  242. {
  243. NewForce newVelocity = new NewForce();
  244. newVelocity.X = 0;
  245. newVelocity.Y = 0;
  246. newVelocity.Z = 0;
  247. this.forcesList.Add(newVelocity);
  248. movementflag = 0;
  249. }
  250. }
  251. }
  252. /// <summary>
  253. ///
  254. /// </summary>
  255. public static void LoadAnims()
  256. {
  257. }
  258. /// <summary>
  259. ///
  260. /// </summary>
  261. public override void LandRenegerated()
  262. {
  263. }
  264. public class NewForce
  265. {
  266. public float X;
  267. public float Y;
  268. public float Z;
  269. public NewForce()
  270. {
  271. }
  272. }
  273. }
  274. }