ScenePresenceAnimator.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Linq;
  30. using System.Reflection;
  31. using System.Threading;
  32. using log4net;
  33. using OpenMetaverse;
  34. using OpenSim.Framework;
  35. using OpenSim.Region.Framework.Interfaces;
  36. using OpenSim.Region.Framework.Scenes;
  37. using OpenSim.Region.PhysicsModules.SharedBase;
  38. namespace OpenSim.Region.Framework.Scenes.Animation
  39. {
  40. /// <summary>
  41. /// Handle all animation duties for a scene presence
  42. /// </summary>
  43. public class ScenePresenceAnimator
  44. {
  45. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  46. public AnimationSet Animations
  47. {
  48. get { return m_animations; }
  49. }
  50. protected AnimationSet m_animations = new AnimationSet();
  51. /// <value>
  52. /// The current movement animation
  53. /// </value>
  54. public string CurrentMovementAnimation { get; private set; }
  55. private int m_animTickFall;
  56. private int m_animTickLand;
  57. private int m_animTickJump;
  58. public bool isJumping;
  59. // private int m_landing = 0;
  60. /// <summary>
  61. /// Is the avatar falling?
  62. /// </summary>
  63. public bool Falling { get; private set; }
  64. private float m_lastFallVelocity;
  65. /// <value>
  66. /// The scene presence that this animator applies to
  67. /// </value>
  68. protected ScenePresence m_scenePresence;
  69. public ScenePresenceAnimator(ScenePresence sp)
  70. {
  71. m_scenePresence = sp;
  72. CurrentMovementAnimation = "STAND";
  73. }
  74. public void AddAnimation(UUID animID, UUID objectID)
  75. {
  76. if (m_scenePresence.IsChildAgent)
  77. return;
  78. // m_log.DebugFormat("[SCENE PRESENCE ANIMATOR]: Adding animation {0} for {1}", animID, m_scenePresence.Name);
  79. if (m_scenePresence.Scene.DebugAnimations)
  80. m_log.DebugFormat(
  81. "[SCENE PRESENCE ANIMATOR]: Adding animation {0} {1} for {2}",
  82. GetAnimName(animID), animID, m_scenePresence.Name);
  83. if (m_animations.Add(animID, m_scenePresence.ControllingClient.NextAnimationSequenceNumber, objectID))
  84. {
  85. SendAnimPack();
  86. m_scenePresence.TriggerScenePresenceUpdated();
  87. }
  88. }
  89. // Called from scripts
  90. public void AddAnimation(string name, UUID objectID)
  91. {
  92. if (m_scenePresence.IsChildAgent)
  93. return;
  94. UUID animID = DefaultAvatarAnimations.GetDefaultAnimation(name);
  95. if (animID.IsZero())
  96. return;
  97. // m_log.DebugFormat("[SCENE PRESENCE ANIMATOR]: Adding animation {0} {1} for {2}", animID, name, m_scenePresence.Name);
  98. AddAnimation(animID, objectID);
  99. }
  100. /// <summary>
  101. /// Remove the specified animation
  102. /// </summary>
  103. /// <param name='animID'></param>
  104. /// <param name='allowNoDefault'>
  105. /// If true, then the default animation can be entirely removed.
  106. /// If false, then removing the default animation will reset it to the simulator default (currently STAND).
  107. /// </param>
  108. public void RemoveAnimation(UUID animID, bool allowNoDefault)
  109. {
  110. if (m_scenePresence.IsChildAgent)
  111. return;
  112. if (m_scenePresence.Scene.DebugAnimations)
  113. m_log.DebugFormat(
  114. "[SCENE PRESENCE ANIMATOR]: Removing animation {0} {1} for {2}",
  115. GetAnimName(animID), animID, m_scenePresence.Name);
  116. if (m_animations.Remove(animID, allowNoDefault))
  117. {
  118. SendAnimPack();
  119. m_scenePresence.TriggerScenePresenceUpdated();
  120. }
  121. }
  122. public void avnChangeAnim(UUID animID, bool addRemove, bool sendPack)
  123. {
  124. if (m_scenePresence.IsChildAgent)
  125. return;
  126. if (!animID.IsZero())
  127. {
  128. if (addRemove)
  129. m_animations.Add(animID, m_scenePresence.ControllingClient.NextAnimationSequenceNumber, UUID.Zero);
  130. else
  131. m_animations.Remove(animID, false);
  132. }
  133. if (sendPack)
  134. SendAnimPack();
  135. }
  136. // Called from scripts
  137. public void RemoveAnimation(string name)
  138. {
  139. if (m_scenePresence.IsChildAgent)
  140. return;
  141. UUID animID = DefaultAvatarAnimations.GetDefaultAnimation(name);
  142. if (animID.IsZero())
  143. return;
  144. RemoveAnimation(animID, true);
  145. }
  146. public void ResetAnimations()
  147. {
  148. if (m_scenePresence.Scene.DebugAnimations)
  149. m_log.DebugFormat(
  150. "[SCENE PRESENCE ANIMATOR]: Resetting animations for {0} in {1}",
  151. m_scenePresence.Name, m_scenePresence.Scene.RegionInfo.RegionName);
  152. m_animations.Clear();
  153. }
  154. UUID aoSitGndAnim = UUID.Zero;
  155. /// <summary>
  156. /// The movement animation is reserved for "main" animations
  157. /// that are mutually exclusive, e.g. flying and sitting.
  158. /// </summary>
  159. /// <returns>'true' if the animation was updated</returns>
  160. ///
  161. public bool TrySetMovementAnimation(string anim)
  162. {
  163. if (m_scenePresence.IsChildAgent)
  164. {
  165. m_log.WarnFormat(
  166. "[SCENE PRESENCE ANIMATOR]: Tried to set movement animation {0} on child presence {1}",
  167. anim, m_scenePresence.Name);
  168. return false;
  169. }
  170. //m_log.DebugFormat(
  171. // "[SCENE PRESENCE ANIMATOR]: Setting movement animation {0} for {1}",
  172. // anim, m_scenePresence.Name);
  173. if (!aoSitGndAnim.IsZero())
  174. {
  175. avnChangeAnim(aoSitGndAnim, false, true);
  176. aoSitGndAnim = UUID.Zero;
  177. }
  178. if (m_scenePresence.Overrides.TryGetOverriddenAnimation(anim, out UUID overridenAnim))
  179. {
  180. if (anim.Equals("SITGROUND"))
  181. {
  182. UUID defsit = DefaultAvatarAnimations.AnimsUUIDbyName["SIT_GROUND_CONSTRAINED"];
  183. if (defsit.IsZero())
  184. return false;
  185. m_animations.SetDefaultAnimation(defsit, m_scenePresence.ControllingClient.NextAnimationSequenceNumber, m_scenePresence.UUID);
  186. aoSitGndAnim = overridenAnim;
  187. avnChangeAnim(overridenAnim, true, false);
  188. }
  189. else
  190. {
  191. m_animations.SetDefaultAnimation(overridenAnim, m_scenePresence.ControllingClient.NextAnimationSequenceNumber, m_scenePresence.UUID);
  192. }
  193. m_scenePresence.SendScriptChangedEventToAttachments(Changed.ANIMATION);
  194. SendAnimPack();
  195. return true;
  196. }
  197. // translate sit and sitground state animations
  198. if (anim.Equals("SIT") || anim.Equals("SITGROUND"))
  199. anim = m_scenePresence.sitAnimation;
  200. if (m_animations.TrySetDefaultAnimation(anim, m_scenePresence.ControllingClient.NextAnimationSequenceNumber, m_scenePresence.UUID))
  201. {
  202. //m_log.DebugFormat(
  203. // "[SCENE PRESENCE ANIMATOR]: Updating movement animation to {0} for {1}",
  204. // anim, m_scenePresence.Name);
  205. m_scenePresence.SendScriptChangedEventToAttachments(Changed.ANIMATION);
  206. SendAnimPack();
  207. return true;
  208. }
  209. return false;
  210. }
  211. public enum motionControlStates : byte
  212. {
  213. sitted = 0,
  214. flying,
  215. falling,
  216. prejumping,
  217. jumping,
  218. landing,
  219. onsurface
  220. }
  221. public motionControlStates currentControlState = motionControlStates.onsurface;
  222. /// <summary>
  223. /// This method determines the proper movement related animation
  224. /// </summary>
  225. private string DetermineMovementAnimation()
  226. {
  227. const int FALL_DELAY = 800;
  228. const int PREJUMP_DELAY = 450;
  229. const int JUMP_PERIOD = 1050;
  230. #region Inputs
  231. if (m_scenePresence.IsInTransit)
  232. return CurrentMovementAnimation;
  233. if (m_scenePresence.SitGround)
  234. {
  235. currentControlState = motionControlStates.sitted;
  236. isJumping = false;
  237. Falling = false;
  238. return "SITGROUND";
  239. }
  240. if (m_scenePresence.ParentID != 0 || !m_scenePresence.ParentUUID.IsZero())
  241. {
  242. currentControlState = motionControlStates.sitted;
  243. isJumping = false;
  244. Falling = false;
  245. return "SIT";
  246. }
  247. AgentManager.ControlFlags controlFlags = (AgentManager.ControlFlags)m_scenePresence.AgentControlFlags;
  248. const AgentManager.ControlFlags ANYXYMASK = (
  249. AgentManager.ControlFlags.AGENT_CONTROL_AT_POS | AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_AT_POS |
  250. AgentManager.ControlFlags.AGENT_CONTROL_AT_NEG | AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_AT_NEG |
  251. AgentManager.ControlFlags.AGENT_CONTROL_LEFT_POS | AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_LEFT_POS |
  252. AgentManager.ControlFlags.AGENT_CONTROL_LEFT_NEG | AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_LEFT_NEG
  253. );
  254. const AgentManager.ControlFlags ANYXYZMASK = (ANYXYMASK |
  255. AgentManager.ControlFlags.AGENT_CONTROL_UP_POS | AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_UP_POS |
  256. AgentManager.ControlFlags.AGENT_CONTROL_UP_NEG | AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_UP_NEG
  257. );
  258. // excluded nudge up so it doesn't trigger jump state
  259. bool heldUp = ((controlFlags & (AgentManager.ControlFlags.AGENT_CONTROL_UP_POS)) != 0);
  260. bool heldDown = ((controlFlags & (AgentManager.ControlFlags.AGENT_CONTROL_UP_NEG | AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_UP_NEG)) != 0);
  261. //bool flying = (controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_FLY) == AgentManager.ControlFlags.AGENT_CONTROL_FLY;
  262. //bool mouselook = (controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_MOUSELOOK) == AgentManager.ControlFlags.AGENT_CONTROL_MOUSELOOK;
  263. bool heldOnXY = ((controlFlags & ANYXYMASK) != 0);
  264. bool heldTurnLeft;
  265. bool heldTurnRight;
  266. if ((controlFlags & ANYXYZMASK) != 0)
  267. {
  268. heldTurnLeft = false;
  269. heldTurnRight = false;
  270. }
  271. else
  272. {
  273. heldTurnLeft = (controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_TURN_LEFT) != 0;
  274. heldTurnRight = (controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_TURN_RIGHT) != 0;
  275. //heldTurnLeft = (controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_YAW_POS) != 0;
  276. //heldTurnRight = (controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_YAW_NEG) != 0;
  277. }
  278. #endregion Inputs
  279. PhysicsActor actor = m_scenePresence.PhysicsActor;
  280. // no physics actor case
  281. if (actor == null)
  282. {
  283. isJumping = false;
  284. Falling = false;
  285. // well what to do?
  286. currentControlState = motionControlStates.onsurface;
  287. if (heldOnXY)
  288. return "WALK";
  289. return "STAND";
  290. }
  291. bool isColliding = actor.IsColliding;
  292. #region Flying
  293. if (actor.Flying)
  294. {
  295. m_animTickFall = 0;
  296. m_animTickJump = 0;
  297. isJumping = false;
  298. Falling = false;
  299. currentControlState = motionControlStates.flying;
  300. if (heldOnXY)
  301. {
  302. return (m_scenePresence.Scene.m_useFlySlow ? "FLYSLOW" : "FLY");
  303. }
  304. else if (heldUp)
  305. {
  306. return "HOVER_UP";
  307. }
  308. else if (heldDown)
  309. {
  310. if (isColliding)
  311. {
  312. actor.Flying = false;
  313. currentControlState = motionControlStates.landing;
  314. m_animTickLand = Environment.TickCount;
  315. return "LAND";
  316. }
  317. else
  318. return "HOVER_DOWN";
  319. }
  320. else
  321. {
  322. return "HOVER";
  323. }
  324. }
  325. else
  326. {
  327. if (isColliding && currentControlState == motionControlStates.flying)
  328. {
  329. currentControlState = motionControlStates.landing;
  330. m_animTickLand = Environment.TickCount;
  331. return "LAND";
  332. }
  333. }
  334. #endregion Flying
  335. #region Falling/Floating/Landing
  336. if (!isColliding && currentControlState != motionControlStates.jumping)
  337. {
  338. float fallVelocity = actor.Velocity.Z;
  339. // if stable on Hover assume falling
  340. if(actor.PIDHoverActive && fallVelocity < 0.05f)
  341. {
  342. Falling = true;
  343. currentControlState = motionControlStates.falling;
  344. m_lastFallVelocity = fallVelocity;
  345. return "FALLDOWN";
  346. }
  347. if (fallVelocity < -2.5f)
  348. Falling = true;
  349. if (m_animTickFall == 0 || (fallVelocity >= -0.5f))
  350. {
  351. m_animTickFall = Environment.TickCount;
  352. }
  353. else
  354. {
  355. int fallElapsed = (Environment.TickCount - m_animTickFall);
  356. if ((fallElapsed > FALL_DELAY) && (fallVelocity < -3.0f))
  357. {
  358. currentControlState = motionControlStates.falling;
  359. m_lastFallVelocity = fallVelocity;
  360. // Falling long enough to trigger the animation
  361. return "FALLDOWN";
  362. }
  363. }
  364. // Check if the user has stopped walking just now
  365. if (CurrentMovementAnimation == "WALK" && !heldOnXY && !heldDown && !heldUp)
  366. return "STAND";
  367. return CurrentMovementAnimation;
  368. }
  369. m_animTickFall = 0;
  370. #endregion Falling/Floating/Landing
  371. #region Jumping // section added for jumping...
  372. if (isColliding && heldUp && !isJumping && !actor.PIDHoverActive)
  373. {
  374. isJumping = true;
  375. Falling = false;
  376. if ((controlFlags & (AgentManager.ControlFlags.AGENT_CONTROL_FINISH_ANIM)) == 0)
  377. {
  378. currentControlState = motionControlStates.prejumping;
  379. m_animTickJump = Environment.TickCount;
  380. // Start jumping, prejump
  381. return "PREJUMP";
  382. }
  383. m_animTickJump = Environment.TickCount - PREJUMP_DELAY - 1;
  384. currentControlState = motionControlStates.jumping;
  385. m_scenePresence.Jump(9.4f);
  386. return "JUMP";
  387. }
  388. if (currentControlState == motionControlStates.prejumping)
  389. {
  390. if((controlFlags & (AgentManager.ControlFlags.AGENT_CONTROL_FINISH_ANIM)) == 0)
  391. {
  392. int jumptime = Environment.TickCount - m_animTickJump;
  393. if (jumptime < PREJUMP_DELAY)
  394. return CurrentMovementAnimation;
  395. }
  396. // jump up
  397. isJumping = true;
  398. currentControlState = motionControlStates.jumping;
  399. m_scenePresence.Jump(9.4f);
  400. return "JUMP";
  401. }
  402. if (currentControlState == motionControlStates.jumping)
  403. {
  404. int jumptime = Environment.TickCount - m_animTickJump;
  405. if ((jumptime > (JUMP_PERIOD * 1.5f)) && actor.IsColliding)
  406. {
  407. // end jumping
  408. isJumping = false;
  409. Falling = false;
  410. actor.Selected = false; // borrowed for jumping flag
  411. m_animTickLand = Environment.TickCount;
  412. currentControlState = motionControlStates.landing;
  413. return "LAND";
  414. }
  415. else if (jumptime > JUMP_PERIOD)
  416. {
  417. // jump down
  418. return "JUMP";
  419. }
  420. return CurrentMovementAnimation;
  421. }
  422. #endregion Jumping
  423. #region Ground Movement
  424. if (currentControlState == motionControlStates.falling)
  425. {
  426. Falling = false;
  427. currentControlState = motionControlStates.landing;
  428. if ((controlFlags & (AgentManager.ControlFlags.AGENT_CONTROL_FINISH_ANIM)) == 0)
  429. {
  430. float fallVsq = m_lastFallVelocity * m_lastFallVelocity;
  431. if (fallVsq > 300f) // aprox 20*h
  432. {
  433. m_animTickLand = Environment.TickCount + 3000;
  434. return "STANDUP";
  435. }
  436. if (fallVsq > 160f)
  437. {
  438. m_animTickLand = Environment.TickCount + 1500;
  439. return "SOFT_LAND";
  440. }
  441. }
  442. m_animTickLand = Environment.TickCount + 600;
  443. return "LAND";
  444. }
  445. if (currentControlState == motionControlStates.landing)
  446. {
  447. Falling = false;
  448. if ((controlFlags & (AgentManager.ControlFlags.AGENT_CONTROL_FINISH_ANIM)) == 0)
  449. {
  450. if ((m_animTickLand != 0) && (m_animTickLand > Environment.TickCount))
  451. return CurrentMovementAnimation;
  452. }
  453. currentControlState = motionControlStates.onsurface;
  454. m_animTickLand = 0;
  455. return "STAND";
  456. }
  457. // next section moved outside paren. and realigned for jumping
  458. if (heldOnXY)
  459. {
  460. currentControlState = motionControlStates.onsurface;
  461. Falling = false;
  462. // Walking / crouchwalking / running
  463. if (heldDown)
  464. {
  465. return "CROUCHWALK";
  466. }
  467. // We need to prevent these animations if the user tries to make their avatar walk or run whilst
  468. // specifying AGENT_CONTROL_STOP (pressing down space on viewers).
  469. else if (!m_scenePresence.AgentControlStopActive)
  470. {
  471. if (m_scenePresence.SetAlwaysRun)
  472. return "RUN";
  473. else
  474. return "WALK";
  475. }
  476. }
  477. else
  478. {
  479. currentControlState = motionControlStates.onsurface;
  480. Falling = false;
  481. // Not walking
  482. if (heldDown)
  483. return "CROUCH";
  484. else if (heldTurnLeft)
  485. return "TURNLEFT";
  486. else if (heldTurnRight)
  487. return "TURNRIGHT";
  488. else
  489. return "STAND";
  490. }
  491. #endregion Ground Movement
  492. return CurrentMovementAnimation;
  493. }
  494. /// <summary>
  495. /// Update the movement animation of this avatar according to its current state
  496. /// </summary>
  497. /// <returns>'true' if the animation was changed</returns>
  498. public bool UpdateMovementAnimations()
  499. {
  500. // m_log.DebugFormat("[SCENE PRESENCE ANIMATOR]: Updating movement animations for {0}", m_scenePresence.Name);
  501. lock (m_animations)
  502. {
  503. string newMovementAnimation = DetermineMovementAnimation();
  504. if (CurrentMovementAnimation.Equals(newMovementAnimation))
  505. return false;
  506. CurrentMovementAnimation = newMovementAnimation;
  507. //m_log.DebugFormat(
  508. // "[SCENE PRESENCE ANIMATOR]: Determined animation {0} for {1} {2} {3} in UpdateMovementAnimations()",
  509. // CurrentMovementAnimation, m_scenePresence.Name, isJumping, Falling);
  510. // Only set it if it's actually changed, give a script
  511. // a chance to stop a default animation
  512. return TrySetMovementAnimation(CurrentMovementAnimation);
  513. }
  514. }
  515. public bool ForceUpdateMovementAnimations()
  516. {
  517. lock (m_animations)
  518. {
  519. CurrentMovementAnimation = DetermineMovementAnimation();
  520. return TrySetMovementAnimation(CurrentMovementAnimation);
  521. }
  522. }
  523. public bool SetMovementAnimations(string motionState)
  524. {
  525. lock (m_animations)
  526. {
  527. CurrentMovementAnimation = motionState;
  528. return TrySetMovementAnimation(CurrentMovementAnimation);
  529. }
  530. }
  531. public UUID[] GetAnimationArray()
  532. {
  533. m_animations.GetAnimationIDsArray(out UUID[] animIDs);
  534. return animIDs;
  535. }
  536. public bool HasAnimation(UUID animID)
  537. {
  538. return m_animations.HasAnimation(animID);
  539. }
  540. public BinBVHAnimation GenerateRandomAnimation()
  541. {
  542. int rnditerations = 3;
  543. BinBVHAnimation anim = new BinBVHAnimation();
  544. List<string> parts = new List<string>();
  545. /// Torso and Head
  546. parts.Add("mPelvis");
  547. parts.Add("mTorso");
  548. parts.Add("mChest");
  549. parts.Add("mNeck");
  550. parts.Add("mHead");
  551. parts.Add("mSkull");
  552. parts.Add("mEyeRight");
  553. parts.Add("mEyeLeft");
  554. /// Arms
  555. parts.Add("mCollarLeft");
  556. parts.Add("mShoulderLeft");
  557. parts.Add("mElbowLeft");
  558. parts.Add("mWristLeft");
  559. parts.Add("mCollarRight");
  560. parts.Add("mShoulderRight");
  561. parts.Add("mElbowRight");
  562. parts.Add("mWristRight");
  563. /// Legs
  564. parts.Add("mHipLeft");
  565. parts.Add("mKneeLeft");
  566. parts.Add("mAnkleLeft");
  567. parts.Add("mFootLeft");
  568. parts.Add("mToeLeft");
  569. parts.Add("mHipRight");
  570. parts.Add("mKneeRight");
  571. parts.Add("mAnkleRight");
  572. parts.Add("mFootRight");
  573. parts.Add("mToeRight");
  574. ///Hands
  575. parts.Add("mHandThumb1Left");
  576. parts.Add("mHandThumb1Right");
  577. parts.Add("mHandThumb2Left");
  578. parts.Add("mHandThumb2Right");
  579. parts.Add("mHandThumb3Left");
  580. parts.Add("mHandThumb3Right");
  581. parts.Add("mHandIndex1Left");
  582. parts.Add("mHandIndex1Right");
  583. parts.Add("mHandIndex2Left");
  584. parts.Add("mHandIndex2Right");
  585. parts.Add("mHandIndex3Left");
  586. parts.Add("mHandIndex3Right");
  587. parts.Add("mHandMiddle1Left");
  588. parts.Add("mHandMiddle1Right");
  589. parts.Add("mHandMiddle2Left");
  590. parts.Add("mHandMiddle2Right");
  591. parts.Add("mHandMiddle3Left");
  592. parts.Add("mHandMiddle3Right");
  593. parts.Add("mHandRing1Left");
  594. parts.Add("mHandRing1Right");
  595. parts.Add("mHandRing2Left");
  596. parts.Add("mHandRing2Right");
  597. parts.Add("mHandRing3Left");
  598. parts.Add("mHandRing3Right");
  599. parts.Add("mHandPinky1Left");
  600. parts.Add("mHandPinky1Right");
  601. parts.Add("mHandPinky2Left");
  602. parts.Add("mHandPinky2Right");
  603. parts.Add("mHandPinky3Left");
  604. parts.Add("mHandPinky3Right");
  605. ///Face
  606. parts.Add("mFaceForeheadLeft");
  607. parts.Add("mFaceForeheadCenter");
  608. parts.Add("mFaceForeheadRight");
  609. parts.Add("mFaceEyebrowOuterLeft");
  610. parts.Add("mFaceEyebrowCenterLeft");
  611. parts.Add("mFaceEyebrowInnerLeft");
  612. parts.Add("mFaceEyebrowOuterRight");
  613. parts.Add("mFaceEyebrowCenterRight");
  614. parts.Add("mFaceEyebrowInnerRight");
  615. parts.Add("mFaceEyeLidUpperLeft");
  616. parts.Add("mFaceEyeLidLowerLeft");
  617. parts.Add("mFaceEyeLidUpperRight");
  618. parts.Add("mFaceEyeLidLowerRight");
  619. parts.Add("mFaceEyeAltLeft");
  620. parts.Add("mFaceEyeAltRight");
  621. parts.Add("mFaceEyecornerInnerLeft");
  622. parts.Add("mFaceEyecornerInnerRight");
  623. parts.Add("mFaceEar1Left");
  624. parts.Add("mFaceEar2Left");
  625. parts.Add("mFaceEar1Right");
  626. parts.Add("mFaceEar2Right");
  627. parts.Add("mFaceNoseLeft");
  628. parts.Add("mFaceNoseCenter");
  629. parts.Add("mFaceNoseRight");
  630. parts.Add("mFaceNoseBase");
  631. parts.Add("mFaceNoseBridge");
  632. parts.Add("mFaceCheekUpperInnerLeft");
  633. parts.Add("mFaceCheekUpperOuterLeft");
  634. parts.Add("mFaceCheekUpperInnerRight");
  635. parts.Add("mFaceCheekUpperOuterRight");
  636. parts.Add("mFaceJaw");
  637. parts.Add("mFaceLipUpperLeft");
  638. parts.Add("mFaceLipUpperCenter");
  639. parts.Add("mFaceLipUpperRight");
  640. parts.Add("mFaceLipCornerLeft");
  641. parts.Add("mFaceLipCornerRight");
  642. parts.Add("mFaceTongueBase");
  643. parts.Add("mFaceTongueTip");
  644. parts.Add("mFaceLipLowerLeft");
  645. parts.Add("mFaceLipLowerCenter");
  646. parts.Add("mFaceLipLowerRight");
  647. parts.Add("mFaceTeethLower");
  648. parts.Add("mFaceTeethUpper");
  649. parts.Add("mFaceChin");
  650. ///Spine
  651. parts.Add("mSpine1");
  652. parts.Add("mSpine2");
  653. parts.Add("mSpine3");
  654. parts.Add("mSpine4");
  655. ///Wings
  656. parts.Add("mWingsRoot");
  657. parts.Add("mWing1Left");
  658. parts.Add("mWing2Left");
  659. parts.Add("mWing3Left");
  660. parts.Add("mWing4Left");
  661. parts.Add("mWing1Right");
  662. parts.Add("mWing2Right");
  663. parts.Add("mWing3Right");
  664. parts.Add("mWing4Right");
  665. parts.Add("mWing4FanRight");
  666. parts.Add("mWing4FanLeft");
  667. ///Hind Limbs
  668. parts.Add("mHindLimbsRoot");
  669. parts.Add("mHindLimb1Left");
  670. parts.Add("mHindLimb2Left");
  671. parts.Add("mHindLimb3Left");
  672. parts.Add("mHindLimb4Left");
  673. parts.Add("mHindLimb1Right");
  674. parts.Add("mHindLimb2Right");
  675. parts.Add("mHindLimb3Right");
  676. parts.Add("mHindLimb4Right");
  677. ///Tail
  678. parts.Add("mTail1");
  679. parts.Add("mTail2");
  680. parts.Add("mTail3");
  681. parts.Add("mTail4");
  682. parts.Add("mTail5");
  683. parts.Add("mTail6");
  684. anim.HandPose = 1;
  685. anim.InPoint = 0;
  686. anim.OutPoint = (rnditerations * .10f);
  687. anim.Priority = 7;
  688. anim.Loop = false;
  689. anim.Length = (rnditerations * .10f);
  690. anim.ExpressionName = "afraid";
  691. anim.EaseInTime = 0;
  692. anim.EaseOutTime = 0;
  693. string[] strjoints = parts.ToArray();
  694. anim.Joints = new binBVHJoint[strjoints.Length];
  695. for (int j = 0; j < strjoints.Length; j++)
  696. {
  697. anim.Joints[j] = new binBVHJoint();
  698. anim.Joints[j].Name = strjoints[j];
  699. anim.Joints[j].Priority = 7;
  700. anim.Joints[j].positionkeys = new binBVHJointKey[rnditerations];
  701. anim.Joints[j].rotationkeys = new binBVHJointKey[rnditerations];
  702. for (int i = 0; i < rnditerations; i++)
  703. {
  704. anim.Joints[j].rotationkeys[i] = new binBVHJointKey();
  705. anim.Joints[j].rotationkeys[i].time = (i * .10f);
  706. anim.Joints[j].rotationkeys[i].key_element.X = ((float)Random.Shared.NextDouble() * 2 - 1);
  707. anim.Joints[j].rotationkeys[i].key_element.Y = ((float)Random.Shared.NextDouble() * 2 - 1);
  708. anim.Joints[j].rotationkeys[i].key_element.Z = ((float)Random.Shared.NextDouble() * 2 - 1);
  709. anim.Joints[j].positionkeys[i] = new binBVHJointKey();
  710. anim.Joints[j].positionkeys[i].time = (i * .10f);
  711. anim.Joints[j].positionkeys[i].key_element.X = 0;
  712. anim.Joints[j].positionkeys[i].key_element.Y = 0;
  713. anim.Joints[j].positionkeys[i].key_element.Z = 0;
  714. }
  715. }
  716. AssetBase Animasset = new AssetBase(UUID.Random(), "Random Animation", (sbyte)AssetType.Animation, m_scenePresence.UUID.ToString());
  717. Animasset.Data = anim.ToBytes();
  718. Animasset.Temporary = true;
  719. Animasset.Local = true;
  720. Animasset.Description = "dance";
  721. //BinBVHAnimation bbvhanim = new BinBVHAnimation(Animasset.Data);
  722. m_scenePresence.Scene.AssetService.Store(Animasset);
  723. AddAnimation(Animasset.FullID, m_scenePresence.UUID);
  724. return anim;
  725. }
  726. /// <summary>
  727. ///
  728. /// </summary>
  729. /// <param name="animations"></param>
  730. /// <param name="seqs"></param>
  731. /// <param name="objectIDs"></param>
  732. public void SendAnimPack(UUID[] animations, int[] seqs, UUID[] objectIDs)
  733. {
  734. m_scenePresence.SendAnimPack(animations, seqs, objectIDs);
  735. }
  736. public void GetArrays(out UUID[] animIDs, out int[] sequenceNums, out UUID[] objectIDs)
  737. {
  738. animIDs = null;
  739. sequenceNums = null;
  740. objectIDs = null;
  741. if (m_animations != null)
  742. m_animations.GetArrays(out animIDs, out sequenceNums, out objectIDs);
  743. }
  744. public void SendAnimPackToClient(IClientAPI client)
  745. {
  746. if (m_scenePresence.IsChildAgent)
  747. return;
  748. UUID[] animIDs;
  749. int[] sequenceNums;
  750. UUID[] objectIDs;
  751. m_animations.GetArrays(out animIDs, out sequenceNums, out objectIDs);
  752. client.SendAnimations(animIDs, sequenceNums, m_scenePresence.ControllingClient.AgentId, objectIDs);
  753. }
  754. /// <summary>
  755. /// Send animation information about this avatar to all clients.
  756. /// </summary>
  757. public void SendAnimPack()
  758. {
  759. //m_log.Debug("Sending animation pack to all");
  760. if (m_scenePresence.IsChildAgent)
  761. return;
  762. UUID[] animIDs;
  763. int[] sequenceNums;
  764. UUID[] objectIDs;
  765. m_animations.GetArrays(out animIDs, out sequenceNums, out objectIDs);
  766. m_scenePresence.SendAnimPack(animIDs, sequenceNums, objectIDs);
  767. }
  768. public string GetAnimName(UUID animId)
  769. {
  770. string animName;
  771. if (!DefaultAvatarAnimations.AnimsNamesbyUUID.TryGetValue(animId, out animName))
  772. {
  773. AssetMetadata amd = m_scenePresence.Scene.AssetService.GetMetadata(animId.ToString());
  774. if (amd != null)
  775. animName = amd.Name;
  776. else
  777. animName = "Unknown";
  778. }
  779. return animName;
  780. }
  781. }
  782. }