Avatar.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using libsecondlife;
  6. using libsecondlife.Packets;
  7. using OpenSim.Physics.Manager;
  8. using OpenSim.Framework.Inventory;
  9. using Axiom.MathLib;
  10. namespace OpenSim.world
  11. {
  12. public partial class Avatar : Entity
  13. {
  14. public static bool PhysicsEngineFlying = false;
  15. public static AvatarAnimations Animations;
  16. public string firstname;
  17. public string lastname;
  18. public SimClient ControllingClient;
  19. public LLUUID current_anim;
  20. public int anim_seq;
  21. private static libsecondlife.Packets.ObjectUpdatePacket.ObjectDataBlock AvatarTemplate;
  22. private bool updateflag = false;
  23. private byte movementflag = 0;
  24. private List<NewForce> forcesList = new List<NewForce>();
  25. private short _updateCount = 0;
  26. private Axiom.MathLib.Quaternion bodyRot;
  27. private LLObject.TextureEntry avatarAppearanceTexture = null;
  28. private byte[] visualParams;
  29. private AvatarWearable[] Wearables;
  30. private LLVector3 positionLastFrame = new LLVector3(0, 0, 0);
  31. private ulong m_regionHandle;
  32. private Dictionary<uint, SimClient> m_clientThreads;
  33. private string m_regionName;
  34. private ushort m_regionWaterHeight;
  35. private bool m_regionTerraform;
  36. //private bool childShadowAvatar = false;
  37. public Avatar(SimClient TheClient, World world, string regionName, Dictionary<uint, SimClient> clientThreads, ulong regionHandle, bool regionTerraform, ushort regionWater)
  38. {
  39. m_world = world;
  40. m_clientThreads = clientThreads;
  41. m_regionName = regionName;
  42. m_regionHandle = regionHandle;
  43. m_regionTerraform = regionTerraform;
  44. m_regionWaterHeight = regionWater;
  45. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Avatar.cs - Loading details from grid (DUMMY)");
  46. ControllingClient = TheClient;
  47. localid = 8880000 + (this.m_world._localNumber++);
  48. Pos = ControllingClient.startpos;
  49. visualParams = new byte[218];
  50. for (int i = 0; i < 218; i++)
  51. {
  52. visualParams[i] = 100;
  53. }
  54. Wearables = new AvatarWearable[13]; //should be 13 of these
  55. for (int i = 0; i < 13; i++)
  56. {
  57. Wearables[i] = new AvatarWearable();
  58. }
  59. this.Wearables[0].AssetID = new LLUUID("66c41e39-38f9-f75a-024e-585989bfab73");
  60. this.Wearables[0].ItemID = LLUUID.Random();
  61. this.avatarAppearanceTexture = new LLObject.TextureEntry(new LLUUID("00000000-0000-0000-5005-000000000005"));
  62. }
  63. public PhysicsActor PhysActor
  64. {
  65. set
  66. {
  67. this._physActor = value;
  68. }
  69. }
  70. public override void addForces()
  71. {
  72. lock (this.forcesList)
  73. {
  74. if (this.forcesList.Count > 0)
  75. {
  76. for (int i = 0; i < this.forcesList.Count; i++)
  77. {
  78. NewForce force = this.forcesList[i];
  79. PhysicsVector phyVector = new PhysicsVector(force.X, force.Y, force.Z);
  80. lock (m_world.LockPhysicsEngine)
  81. {
  82. this._physActor.Velocity = phyVector;
  83. }
  84. this.updateflag = true;
  85. this.velocity = new LLVector3(force.X, force.Y, force.Z); //shouldn't really be doing this
  86. // but as we are setting the velocity (rather than using real forces) at the moment it is okay.
  87. }
  88. for (int i = 0; i < this.forcesList.Count; i++)
  89. {
  90. this.forcesList.RemoveAt(0);
  91. }
  92. }
  93. }
  94. }
  95. public static void SetupTemplate(string name)
  96. {
  97. FileInfo fInfo = new FileInfo(name);
  98. long numBytes = fInfo.Length;
  99. FileStream fStream = new FileStream(name, FileMode.Open, FileAccess.Read);
  100. BinaryReader br = new BinaryReader(fStream);
  101. byte[] data1 = br.ReadBytes((int)numBytes);
  102. br.Close();
  103. fStream.Close();
  104. libsecondlife.Packets.ObjectUpdatePacket.ObjectDataBlock objdata = new ObjectUpdatePacket.ObjectDataBlock(); // new libsecondlife.Packets.ObjectUpdatePacket.ObjectDataBlock(data1, ref i);
  105. SetDefaultPacketValues(objdata);
  106. objdata.TextureEntry = data1;
  107. objdata.UpdateFlags = 61 + (9 << 8) + (130 << 16) + (16 << 24);
  108. objdata.PathCurve = 16;
  109. objdata.ProfileCurve = 1;
  110. objdata.PathScaleX = 100;
  111. objdata.PathScaleY = 100;
  112. objdata.ParentID = 0;
  113. objdata.OwnerID = LLUUID.Zero;
  114. objdata.Scale = new LLVector3(1, 1, 1);
  115. objdata.PCode = 47;
  116. System.Text.Encoding enc = System.Text.Encoding.ASCII;
  117. libsecondlife.LLVector3 pos = new LLVector3(objdata.ObjectData, 16);
  118. pos.X = 100f;
  119. objdata.ID = 8880000;
  120. objdata.NameValue = enc.GetBytes("FirstName STRING RW SV Test \nLastName STRING RW SV User \0");
  121. libsecondlife.LLVector3 pos2 = new LLVector3(100f, 100f, 23f);
  122. //objdata.FullID=user.AgentID;
  123. byte[] pb = pos.GetBytes();
  124. Array.Copy(pb, 0, objdata.ObjectData, 16, pb.Length);
  125. Avatar.AvatarTemplate = objdata;
  126. }
  127. protected static void SetDefaultPacketValues(ObjectUpdatePacket.ObjectDataBlock objdata)
  128. {
  129. objdata.PSBlock = new byte[0];
  130. objdata.ExtraParams = new byte[1];
  131. objdata.MediaURL = new byte[0];
  132. objdata.NameValue = new byte[0];
  133. objdata.Text = new byte[0];
  134. objdata.TextColor = new byte[4];
  135. objdata.JointAxisOrAnchor = new LLVector3(0, 0, 0);
  136. objdata.JointPivot = new LLVector3(0, 0, 0);
  137. objdata.Material = 4;
  138. objdata.TextureAnim = new byte[0];
  139. objdata.Sound = LLUUID.Zero;
  140. LLObject.TextureEntry ntex = new LLObject.TextureEntry(new LLUUID("00000000-0000-0000-5005-000000000005"));
  141. objdata.TextureEntry = ntex.ToBytes();
  142. objdata.State = 0;
  143. objdata.Data = new byte[0];
  144. objdata.ObjectData = new byte[76];
  145. objdata.ObjectData[15] = 128;
  146. objdata.ObjectData[16] = 63;
  147. objdata.ObjectData[56] = 128;
  148. objdata.ObjectData[61] = 102;
  149. objdata.ObjectData[62] = 40;
  150. objdata.ObjectData[63] = 61;
  151. objdata.ObjectData[64] = 189;
  152. }
  153. public void CompleteMovement(World RegionInfo)
  154. {
  155. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Avatar.cs:CompleteMovement() - Constructing AgentMovementComplete packet");
  156. AgentMovementCompletePacket mov = new AgentMovementCompletePacket();
  157. mov.AgentData.SessionID = this.ControllingClient.SessionID;
  158. mov.AgentData.AgentID = this.ControllingClient.AgentID;
  159. mov.Data.RegionHandle = this.m_regionHandle;
  160. // TODO - dynamicalise this stuff
  161. mov.Data.Timestamp = 1172750370;
  162. mov.Data.Position = this.ControllingClient.startpos;
  163. mov.Data.LookAt = new LLVector3(0.99f, 0.042f, 0);
  164. ControllingClient.OutPacket(mov);
  165. }
  166. public void HandleUpdate(AgentUpdatePacket pack)
  167. {
  168. if (((uint)pack.AgentData.ControlFlags & (uint)MainAvatar.ControlFlags.AGENT_CONTROL_FLY) != 0)
  169. {
  170. if (this._physActor.Flying == false)
  171. {
  172. this.current_anim = Animations.AnimsLLUUID["ANIM_AGENT_FLY"];
  173. this.anim_seq = 1;
  174. this.SendAnimPack();
  175. }
  176. this._physActor.Flying = true;
  177. }
  178. else
  179. {
  180. if (this._physActor.Flying == true)
  181. {
  182. this.current_anim = Animations.AnimsLLUUID["ANIM_AGENT_STAND"];
  183. this.anim_seq = 1;
  184. this.SendAnimPack();
  185. }
  186. this._physActor.Flying = false;
  187. }
  188. if (((uint)pack.AgentData.ControlFlags & (uint)MainAvatar.ControlFlags.AGENT_CONTROL_AT_POS) != 0)
  189. {
  190. Axiom.MathLib.Quaternion q = new Axiom.MathLib.Quaternion(pack.AgentData.BodyRotation.W, pack.AgentData.BodyRotation.X, pack.AgentData.BodyRotation.Y, pack.AgentData.BodyRotation.Z);
  191. if (((movementflag & 1) == 0) || (q != this.bodyRot))
  192. {
  193. if (((movementflag & 1) == 0) && (!this._physActor.Flying))
  194. {
  195. this.current_anim = Animations.AnimsLLUUID["ANIM_AGENT_WALK"];
  196. this.anim_seq = 1;
  197. this.SendAnimPack();
  198. }
  199. //we should add a new force to the list
  200. // but for now we will deal with velocities
  201. NewForce newVelocity = new NewForce();
  202. Axiom.MathLib.Vector3 v3 = new Axiom.MathLib.Vector3(1, 0, 0);
  203. Axiom.MathLib.Vector3 direc = q * v3;
  204. direc.Normalize();
  205. //work out velocity for sim physics system
  206. direc = direc * ((0.03f) * 128f);
  207. if (this._physActor.Flying)
  208. direc *= 2;
  209. newVelocity.X = direc.x;
  210. newVelocity.Y = direc.y;
  211. newVelocity.Z = direc.z;
  212. this.forcesList.Add(newVelocity);
  213. movementflag = 1;
  214. this.bodyRot = q;
  215. }
  216. }
  217. else if ((((uint)pack.AgentData.ControlFlags & (uint)MainAvatar.ControlFlags.AGENT_CONTROL_UP_POS) != 0) && (PhysicsEngineFlying))
  218. {
  219. if (((movementflag & 2) == 0) && this._physActor.Flying)
  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(0, 0, 1);
  225. Axiom.MathLib.Vector3 direc = v3;
  226. direc.Normalize();
  227. //work out velocity for sim physics system
  228. direc = direc * ((0.03f) * 128f * 2);
  229. newVelocity.X = direc.x;
  230. newVelocity.Y = direc.y;
  231. newVelocity.Z = direc.z;
  232. this.forcesList.Add(newVelocity);
  233. movementflag = 2;
  234. }
  235. }
  236. else if ((((uint)pack.AgentData.ControlFlags & (uint)MainAvatar.ControlFlags.AGENT_CONTROL_UP_NEG) != 0) && (PhysicsEngineFlying))
  237. {
  238. if (((movementflag & 4) == 0) && this._physActor.Flying)
  239. {
  240. //we should add a new force to the list
  241. // but for now we will deal with velocities
  242. NewForce newVelocity = new NewForce();
  243. Axiom.MathLib.Vector3 v3 = new Axiom.MathLib.Vector3(0, 0, -1);
  244. //Axiom.MathLib.Quaternion q = new Axiom.MathLib.Quaternion(pack.AgentData.BodyRotation.W, pack.AgentData.BodyRotation.X, pack.AgentData.BodyRotation.Y, pack.AgentData.BodyRotation.Z);
  245. Axiom.MathLib.Vector3 direc = v3;
  246. direc.Normalize();
  247. //work out velocity for sim physics system
  248. direc = direc * ((0.03f) * 128f * 2);
  249. newVelocity.X = direc.x;
  250. newVelocity.Y = direc.y;
  251. newVelocity.Z = direc.z;
  252. this.forcesList.Add(newVelocity);
  253. movementflag = 4;
  254. }
  255. }
  256. else if (((uint)pack.AgentData.ControlFlags & (uint)MainAvatar.ControlFlags.AGENT_CONTROL_AT_NEG) != 0)
  257. {
  258. Axiom.MathLib.Quaternion q = new Axiom.MathLib.Quaternion(pack.AgentData.BodyRotation.W, pack.AgentData.BodyRotation.X, pack.AgentData.BodyRotation.Y, pack.AgentData.BodyRotation.Z);
  259. if (((movementflag & 8) == 0) || (q != this.bodyRot))
  260. {
  261. //we should add a new force to the list
  262. // but for now we will deal with velocities
  263. NewForce newVelocity = new NewForce();
  264. Axiom.MathLib.Vector3 v3 = new Axiom.MathLib.Vector3(-1, 0, 0);
  265. Axiom.MathLib.Vector3 direc = q * v3;
  266. direc.Normalize();
  267. //work out velocity for sim physics system
  268. direc = direc * ((0.03f) * 128f);
  269. if (this._physActor.Flying)
  270. direc *= 2;
  271. newVelocity.X = direc.x;
  272. newVelocity.Y = direc.y;
  273. newVelocity.Z = direc.z;
  274. this.forcesList.Add(newVelocity);
  275. movementflag = 8;
  276. this.bodyRot = q;
  277. }
  278. }
  279. else
  280. {
  281. if (movementflag == 16)
  282. {
  283. movementflag = 0;
  284. }
  285. if ((movementflag) != 0)
  286. {
  287. NewForce newVelocity = new NewForce();
  288. newVelocity.X = 0;
  289. newVelocity.Y = 0;
  290. newVelocity.Z = 0;
  291. this.forcesList.Add(newVelocity);
  292. movementflag = 0;
  293. // We're standing still, so make it show!
  294. if (this._physActor.Flying == false)
  295. {
  296. this.current_anim = Animations.AnimsLLUUID["ANIM_AGENT_STAND"];
  297. this.anim_seq = 1;
  298. this.SendAnimPack();
  299. }
  300. this.movementflag = 16;
  301. }
  302. }
  303. }
  304. //really really should be moved somewhere else (RegionInfo.cs ?)
  305. public void SendRegionHandshake(World RegionInfo)
  306. {
  307. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Avatar.cs:SendRegionHandshake() - Creating empty RegionHandshake packet");
  308. System.Text.Encoding _enc = System.Text.Encoding.ASCII;
  309. RegionHandshakePacket handshake = new RegionHandshakePacket();
  310. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Avatar.cs:SendRegionhandshake() - Filling in RegionHandshake details");
  311. handshake.RegionInfo.BillableFactor = 0;
  312. handshake.RegionInfo.IsEstateManager = false;
  313. handshake.RegionInfo.TerrainHeightRange00 = 60;
  314. handshake.RegionInfo.TerrainHeightRange01 = 60;
  315. handshake.RegionInfo.TerrainHeightRange10 = 60;
  316. handshake.RegionInfo.TerrainHeightRange11 = 60;
  317. handshake.RegionInfo.TerrainStartHeight00 = 10;
  318. handshake.RegionInfo.TerrainStartHeight01 = 10;
  319. handshake.RegionInfo.TerrainStartHeight10 = 10;
  320. handshake.RegionInfo.TerrainStartHeight11 = 10;
  321. handshake.RegionInfo.SimAccess = 13;
  322. handshake.RegionInfo.WaterHeight = m_regionWaterHeight;
  323. uint regionFlags = 72458694;
  324. if (this.m_regionTerraform)
  325. {
  326. regionFlags -= 64;
  327. }
  328. handshake.RegionInfo.RegionFlags = regionFlags;
  329. handshake.RegionInfo.SimName = _enc.GetBytes(m_regionName + "\0");
  330. handshake.RegionInfo.SimOwner = new LLUUID("00000000-0000-0000-0000-000000000000");
  331. handshake.RegionInfo.TerrainBase0 = new LLUUID("b8d3965a-ad78-bf43-699b-bff8eca6c975");
  332. handshake.RegionInfo.TerrainBase1 = new LLUUID("abb783e6-3e93-26c0-248a-247666855da3");
  333. handshake.RegionInfo.TerrainBase2 = new LLUUID("179cdabd-398a-9b6b-1391-4dc333ba321f");
  334. handshake.RegionInfo.TerrainBase3 = new LLUUID("beb169c7-11ea-fff2-efe5-0f24dc881df2");
  335. handshake.RegionInfo.TerrainDetail0 = new LLUUID("00000000-0000-0000-0000-000000000000");
  336. handshake.RegionInfo.TerrainDetail1 = new LLUUID("00000000-0000-0000-0000-000000000000");
  337. handshake.RegionInfo.TerrainDetail2 = new LLUUID("00000000-0000-0000-0000-000000000000");
  338. handshake.RegionInfo.TerrainDetail3 = new LLUUID("00000000-0000-0000-0000-000000000000");
  339. handshake.RegionInfo.CacheID = new LLUUID("545ec0a5-5751-1026-8a0b-216e38a7ab37");
  340. OpenSim.Framework.Console.MainConsole.Instance.WriteLine("Avatar.cs:SendRegionHandshake() - Sending RegionHandshake packet");
  341. this.ControllingClient.OutPacket(handshake);
  342. }
  343. public static void LoadAnims()
  344. {
  345. Avatar.Animations = new AvatarAnimations();
  346. Avatar.Animations.LoadAnims();
  347. }
  348. public override void LandRenegerated()
  349. {
  350. Pos = new LLVector3(100.0f, 100.0f, m_world.Terrain[(int)Pos.X, (int)Pos.Y] + 50.0f);
  351. }
  352. }
  353. public class NewForce
  354. {
  355. public float X;
  356. public float Y;
  357. public float Z;
  358. public NewForce()
  359. {
  360. }
  361. }
  362. }