ScenePresence.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  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 Axiom.Math;
  32. using libsecondlife;
  33. using libsecondlife.Packets;
  34. using OpenSim.Framework.Console;
  35. using OpenSim.Framework.Interfaces;
  36. using OpenSim.Framework.Types;
  37. using OpenSim.Physics.Manager;
  38. namespace OpenSim.Region.Environment.Scenes
  39. {
  40. public partial class ScenePresence : Entity
  41. {
  42. public static bool PhysicsEngineFlying = false;
  43. public static AvatarAnimations Animations;
  44. public static byte[] DefaultTexture;
  45. public string firstname;
  46. public string lastname;
  47. public IClientAPI ControllingClient;
  48. public LLUUID current_anim;
  49. public int anim_seq;
  50. private bool updateflag = false;
  51. private byte movementflag = 0;
  52. private List<NewForce> forcesList = new List<NewForce>();
  53. private short _updateCount = 0;
  54. private Quaternion bodyRot;
  55. private LLObject.TextureEntry avatarAppearanceTexture = null;
  56. private byte[] visualParams;
  57. private AvatarWearable[] Wearables;
  58. private LLVector3 positionLastFrame = new LLVector3(0, 0, 0);
  59. private ulong m_regionHandle;
  60. private bool childAgent = false;
  61. private bool newForce = false;
  62. private bool newAvatar = false;
  63. private IScenePresenceBody m_body;
  64. protected RegionInfo m_regionInfo;
  65. private Vector3[] Dir_Vectors = new Vector3[6];
  66. private enum Dir_ControlFlags
  67. {
  68. DIR_CONTROL_FLAG_FOWARD = MainAvatar.ControlFlags.AGENT_CONTROL_AT_POS,
  69. DIR_CONTROL_FLAG_BACK = MainAvatar.ControlFlags.AGENT_CONTROL_AT_NEG,
  70. DIR_CONTROL_FLAG_LEFT = MainAvatar.ControlFlags.AGENT_CONTROL_LEFT_POS,
  71. DIR_CONTROL_FLAG_RIGHT = MainAvatar.ControlFlags.AGENT_CONTROL_LEFT_NEG,
  72. DIR_CONTROL_FLAG_UP = MainAvatar.ControlFlags.AGENT_CONTROL_UP_POS,
  73. DIR_CONTROL_FLAG_DOWN = MainAvatar.ControlFlags.AGENT_CONTROL_UP_NEG
  74. }
  75. #region Properties
  76. /// <summary>
  77. ///
  78. /// </summary>
  79. public PhysicsActor PhysActor
  80. {
  81. set
  82. {
  83. this._physActor = value;
  84. }
  85. get
  86. {
  87. return _physActor;
  88. }
  89. }
  90. public ulong RegionHandle
  91. {
  92. get { return m_regionHandle; }
  93. }
  94. #endregion
  95. #region Constructor(s)
  96. /// <summary>
  97. ///
  98. /// </summary>
  99. /// <param name="theClient"></param>
  100. /// <param name="world"></param>
  101. /// <param name="clientThreads"></param>
  102. /// <param name="regionDat"></param>
  103. public ScenePresence(IClientAPI theClient, Scene world, RegionInfo reginfo)
  104. {
  105. m_world = world;
  106. this.uuid = theClient.AgentId;
  107. m_regionInfo = reginfo;
  108. m_regionHandle = reginfo.RegionHandle;
  109. MainLog.Instance.Verbose("Avatar.cs ");
  110. ControllingClient = theClient;
  111. this.firstname = ControllingClient.FirstName;
  112. this.lastname = ControllingClient.LastName;
  113. m_localId = m_world.NextLocalId;
  114. Pos = ControllingClient.StartPos;
  115. visualParams = new byte[218];
  116. for (int i = 0; i < 218; i++)
  117. {
  118. visualParams[i] = 100;
  119. }
  120. Wearables = AvatarWearable.DefaultWearables;
  121. Animations = new ScenePresence.AvatarAnimations();
  122. Animations.LoadAnims();
  123. this.avatarAppearanceTexture = new LLObject.TextureEntry(new LLUUID("00000000-0000-0000-5005-000000000005"));
  124. //register for events
  125. ControllingClient.OnRequestWearables += this.SendOurAppearance;
  126. //ControllingClient.OnSetAppearance += new SetAppearance(this.SetAppearance);
  127. ControllingClient.OnCompleteMovementToRegion += this.CompleteMovement;
  128. ControllingClient.OnCompleteMovementToRegion += this.SendInitialData;
  129. ControllingClient.OnAgentUpdate += this.HandleAgentUpdate;
  130. // ControllingClient.OnStartAnim += new StartAnim(this.SendAnimPack);
  131. // ControllingClient.OnChildAgentStatus += new StatusChange(this.ChildStatusChange);
  132. //ControllingClient.OnStopMovement += new GenericCall2(this.StopMovement);
  133. Dir_Vectors[0] = new Vector3(1, 0, 0); //FOWARD
  134. Dir_Vectors[1] = new Vector3(-1, 0, 0); //BACK
  135. Dir_Vectors[2] = new Vector3(0, 1, 0); //LEFT
  136. Dir_Vectors[3] = new Vector3(0, -1, 0); //RIGHT
  137. Dir_Vectors[4] = new Vector3(0, 0, 1); //UP
  138. Dir_Vectors[5] = new Vector3(0, 0, -1); //DOWN
  139. }
  140. #endregion
  141. #region Status Methods
  142. /// <summary>
  143. /// Not Used, most likely can be deleted
  144. /// </summary>
  145. /// <param name="status"></param>
  146. public void ChildStatusChange(bool status)
  147. {
  148. this.childAgent = status;
  149. if (this.childAgent == true)
  150. {
  151. this.Velocity = new LLVector3(0, 0, 0);
  152. this.Pos = new LLVector3(128, 128, 70);
  153. }
  154. }
  155. /// <summary>
  156. ///
  157. /// </summary>
  158. /// <param name="pos"></param>
  159. public void MakeAvatar(LLVector3 pos)
  160. {
  161. //this.childAvatar = false;
  162. this.Pos = pos;
  163. this.newAvatar = true;
  164. this.childAgent = false;
  165. }
  166. protected void MakeChildAgent()
  167. {
  168. this.Velocity = new LLVector3(0, 0, 0);
  169. this.Pos = new LLVector3(128, 128, 70);
  170. this.childAgent = true;
  171. }
  172. /// <summary>
  173. ///
  174. /// </summary>
  175. /// <param name="pos"></param>
  176. public void Teleport(LLVector3 pos)
  177. {
  178. this.Pos = pos;
  179. this.SendTerseUpdateToALLClients();
  180. }
  181. /// <summary>
  182. ///
  183. /// </summary>
  184. public void StopMovement()
  185. {
  186. }
  187. #endregion
  188. #region Event Handlers
  189. /// <summary>
  190. ///
  191. /// </summary>
  192. /// <param name="texture"></param>
  193. /// <param name="visualParam"></param>
  194. public void SetAppearance(byte[] texture, AgentSetAppearancePacket.VisualParamBlock[] visualParam)
  195. {
  196. }
  197. /// <summary>
  198. /// Complete Avatar's movement into the region
  199. /// </summary>
  200. public void CompleteMovement()
  201. {
  202. LLVector3 look = this.Velocity;
  203. if ((look.X == 0) && (look.Y == 0) && (look.Z == 0))
  204. {
  205. look = new LLVector3(0.99f, 0.042f, 0);
  206. }
  207. this.ControllingClient.MoveAgentIntoRegion(m_regionInfo, Pos, look);
  208. if (this.childAgent)
  209. {
  210. this.childAgent = false;
  211. }
  212. }
  213. /// <summary>
  214. ///
  215. /// </summary>
  216. /// <param name="pack"></param>
  217. public void HandleAgentUpdate(IClientAPI remoteClient, uint flags, LLQuaternion bodyRotation)
  218. {
  219. int i = 0;
  220. bool update_movementflag = false;
  221. bool update_rotation = false;
  222. bool DCFlagKeyPressed = false;
  223. Vector3 agent_control_v3 = new Vector3(0, 0, 0);
  224. Quaternion q = new Quaternion(bodyRotation.W, bodyRotation.X, bodyRotation.Y, bodyRotation.Z);
  225. this.PhysActor.Flying = ((flags & (uint)MainAvatar.ControlFlags.AGENT_CONTROL_FLY) != 0);
  226. if (q != this.bodyRot)
  227. {
  228. this.bodyRot = q;
  229. update_rotation = true;
  230. }
  231. foreach (Dir_ControlFlags DCF in Enum.GetValues(typeof(Dir_ControlFlags)))
  232. {
  233. if ((flags & (uint)DCF) != 0)
  234. {
  235. DCFlagKeyPressed = true;
  236. agent_control_v3 += Dir_Vectors[i];
  237. if ((movementflag & (uint)DCF) == 0)
  238. {
  239. movementflag += (byte)(uint)DCF;
  240. update_movementflag = true;
  241. }
  242. }
  243. else
  244. {
  245. if ((movementflag & (uint)DCF) != 0)
  246. {
  247. movementflag -= (byte)(uint)DCF;
  248. update_movementflag = true;
  249. }
  250. }
  251. i++;
  252. }
  253. if ((update_movementflag) || (update_rotation && DCFlagKeyPressed))
  254. {
  255. this.AddNewMovement(agent_control_v3, q);
  256. }
  257. UpdateMovementAnimations(update_movementflag);
  258. }
  259. protected void UpdateMovementAnimations(bool update_movementflag)
  260. {
  261. if (update_movementflag)
  262. {
  263. if (movementflag != 0)
  264. {
  265. if (this._physActor.Flying)
  266. {
  267. this.SendAnimPack(Animations.AnimsLLUUID["FLY"], 1);
  268. }
  269. else
  270. {
  271. this.SendAnimPack(Animations.AnimsLLUUID["WALK"], 1);
  272. }
  273. }
  274. else
  275. {
  276. this.SendAnimPack(Animations.AnimsLLUUID["STAND"], 1);
  277. }
  278. }
  279. }
  280. protected void AddNewMovement(Vector3 vec, Quaternion rotation)
  281. {
  282. NewForce newVelocity = new NewForce();
  283. Vector3 direc = rotation * vec;
  284. direc.Normalize();
  285. direc = direc * ((0.03f) * 128f);
  286. if (this._physActor.Flying)
  287. direc *= 4;
  288. newVelocity.X = direc.x;
  289. newVelocity.Y = direc.y;
  290. newVelocity.Z = direc.z;
  291. this.forcesList.Add(newVelocity);
  292. }
  293. #endregion
  294. #region Overridden Methods
  295. /// <summary>
  296. ///
  297. /// </summary>
  298. public override void LandRenegerated()
  299. {
  300. }
  301. /// <summary>
  302. ///
  303. /// </summary>
  304. public override void update()
  305. {
  306. if (this.childAgent == false)
  307. {
  308. if (this.newForce)
  309. {
  310. this.SendTerseUpdateToALLClients();
  311. _updateCount = 0;
  312. }
  313. else if (movementflag != 0)
  314. {
  315. _updateCount++;
  316. if (_updateCount > 3)
  317. {
  318. this.SendTerseUpdateToALLClients();
  319. _updateCount = 0;
  320. }
  321. }
  322. this.CheckForBorderCrossing();
  323. }
  324. }
  325. #endregion
  326. #region Update Client(s)
  327. /// <summary>
  328. ///
  329. /// </summary>
  330. /// <param name="RemoteClient"></param>
  331. public void SendTerseUpdateToClient(IClientAPI RemoteClient)
  332. {
  333. LLVector3 pos = this.Pos;
  334. LLVector3 vel = this.Velocity;
  335. RemoteClient.SendAvatarTerseUpdate(this.m_regionHandle, 64096, this.LocalId, new LLVector3(pos.X, pos.Y, pos.Z), new LLVector3(vel.X, vel.Y, vel.Z));
  336. }
  337. /// <summary>
  338. ///
  339. /// </summary>
  340. public void SendTerseUpdateToALLClients()
  341. {
  342. List<ScenePresence> avatars = this.m_world.RequestAvatarList();
  343. for (int i = 0; i < avatars.Count; i++)
  344. {
  345. this.SendTerseUpdateToClient(avatars[i].ControllingClient);
  346. }
  347. }
  348. /// <summary>
  349. ///
  350. /// </summary>
  351. /// <param name="remoteAvatar"></param>
  352. public void SendFullUpdateToOtherClient(ScenePresence remoteAvatar)
  353. {
  354. remoteAvatar.ControllingClient.SendAvatarData(m_regionInfo.RegionHandle, this.firstname, this.lastname, this.uuid, this.LocalId, this.Pos, DefaultTexture);
  355. }
  356. public void SendFullUpdateToALLClients()
  357. {
  358. List<ScenePresence> avatars = this.m_world.RequestAvatarList();
  359. foreach (ScenePresence avatar in this.m_world.RequestAvatarList())
  360. {
  361. this.SendFullUpdateToOtherClient(avatar);
  362. avatar.SendFullUpdateToOtherClient(this);
  363. }
  364. }
  365. /// <summary>
  366. ///
  367. /// </summary>
  368. public void SendInitialData()
  369. {
  370. this.ControllingClient.SendAvatarData(m_regionInfo.RegionHandle, this.firstname, this.lastname, this.uuid, this.LocalId, this.Pos, DefaultTexture);
  371. if (this.newAvatar)
  372. {
  373. this.m_world.InformClientOfNeighbours(this.ControllingClient);
  374. this.newAvatar = false;
  375. }
  376. }
  377. /// <summary>
  378. ///
  379. /// </summary>
  380. /// <param name="OurClient"></param>
  381. public void SendOurAppearance(IClientAPI OurClient)
  382. {
  383. this.ControllingClient.SendWearables(this.Wearables);
  384. this.SendFullUpdateToALLClients();
  385. this.m_world.SendAllSceneObjectsToClient(this.ControllingClient);
  386. }
  387. /// <summary>
  388. ///
  389. /// </summary>
  390. /// <param name="avatarInfo"></param>
  391. public void SendAppearanceToOtherAgent(ScenePresence avatarInfo)
  392. {
  393. }
  394. /// <summary>
  395. ///
  396. /// </summary>
  397. /// <param name="animID"></param>
  398. /// <param name="seq"></param>
  399. public void SendAnimPack(LLUUID animID, int seq)
  400. {
  401. this.current_anim = animID;
  402. this.anim_seq = seq;
  403. List<ScenePresence> avatars = this.m_world.RequestAvatarList();
  404. for (int i = 0; i < avatars.Count; i++)
  405. {
  406. avatars[i].ControllingClient.SendAnimation(animID, seq, this.ControllingClient.AgentId);
  407. }
  408. }
  409. /// <summary>
  410. ///
  411. /// </summary>
  412. public void SendAnimPack()
  413. {
  414. this.SendAnimPack(this.current_anim, this.anim_seq);
  415. }
  416. #endregion
  417. #region Border Crossing Methods
  418. /// <summary>
  419. ///
  420. /// </summary>
  421. protected void CheckForBorderCrossing()
  422. {
  423. LLVector3 pos2 = this.Pos;
  424. LLVector3 vel = this.Velocity;
  425. float timeStep = 0.2f;
  426. pos2.X = pos2.X + (vel.X * timeStep);
  427. pos2.Y = pos2.Y + (vel.Y * timeStep);
  428. pos2.Z = pos2.Z + (vel.Z * timeStep);
  429. if ((pos2.X < 0) || (pos2.X > 256))
  430. {
  431. this.CrossToNewRegion();
  432. }
  433. if ((pos2.Y < 0) || (pos2.Y > 256))
  434. {
  435. this.CrossToNewRegion();
  436. }
  437. }
  438. /// <summary>
  439. ///
  440. /// </summary>
  441. protected void CrossToNewRegion()
  442. {
  443. LLVector3 pos = this.Pos;
  444. LLVector3 newpos = new LLVector3(pos.X, pos.Y, pos.Z);
  445. uint neighbourx = this.m_regionInfo.RegionLocX;
  446. uint neighboury = this.m_regionInfo.RegionLocY;
  447. if (pos.X < 2)
  448. {
  449. neighbourx -= 1;
  450. newpos.X = 254;
  451. }
  452. if (pos.X > 253)
  453. {
  454. neighbourx += 1;
  455. newpos.X = 1;
  456. }
  457. if (pos.Y < 2)
  458. {
  459. neighboury -= 1;
  460. newpos.Y = 254;
  461. }
  462. if (pos.Y > 253)
  463. {
  464. neighboury += 1;
  465. newpos.Y = 1;
  466. }
  467. LLVector3 vel = this.m_velocity;
  468. ulong neighbourHandle = Helpers.UIntsToLong((uint)(neighbourx * 256), (uint)(neighboury * 256));
  469. RegionInfo neighbourRegion = this.m_world.RequestNeighbouringRegionInfo(neighbourHandle);
  470. if (neighbourRegion != null)
  471. {
  472. bool res = this.m_world.InformNeighbourOfCrossing(neighbourHandle, this.ControllingClient.AgentId, newpos);
  473. if (res)
  474. {
  475. this.MakeChildAgent();
  476. this.ControllingClient.CrossRegion(neighbourHandle, newpos, vel, neighbourRegion.InternalEndPoint);
  477. }
  478. }
  479. }
  480. #endregion
  481. /// <summary>
  482. ///
  483. /// </summary>
  484. public static void LoadAnims()
  485. {
  486. }
  487. /// <summary>
  488. ///
  489. /// </summary>
  490. public override void updateMovement()
  491. {
  492. newForce = false;
  493. lock (this.forcesList)
  494. {
  495. if (this.forcesList.Count > 0)
  496. {
  497. for (int i = 0; i < this.forcesList.Count; i++)
  498. {
  499. NewForce force = this.forcesList[i];
  500. this.updateflag = true;
  501. this.Velocity = new LLVector3(force.X, force.Y, force.Z);
  502. this.newForce = true;
  503. }
  504. for (int i = 0; i < this.forcesList.Count; i++)
  505. {
  506. this.forcesList.RemoveAt(0);
  507. }
  508. }
  509. }
  510. }
  511. public static void LoadTextureFile(string name)
  512. {
  513. FileInfo fInfo = new FileInfo(name);
  514. long numBytes = fInfo.Length;
  515. FileStream fStream = new FileStream(name, FileMode.Open, FileAccess.Read);
  516. BinaryReader br = new BinaryReader(fStream);
  517. byte[] data1 = br.ReadBytes((int)numBytes);
  518. br.Close();
  519. fStream.Close();
  520. DefaultTexture = data1;
  521. }
  522. public class NewForce
  523. {
  524. public float X;
  525. public float Y;
  526. public float Z;
  527. public NewForce()
  528. {
  529. }
  530. }
  531. }
  532. }