ScenePresenceAnimator.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  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 m_jumping = false;
  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 = "CROUCH";
  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. // XXX: For some reason, we store all animations and use them with upper case names, but in LSL animations
  95. // are referenced with lower case names!
  96. UUID animID = DefaultAvatarAnimations.GetDefaultAnimation(name.ToUpper());
  97. if (animID == UUID.Zero)
  98. return;
  99. // m_log.DebugFormat("[SCENE PRESENCE ANIMATOR]: Adding animation {0} {1} for {2}", animID, name, m_scenePresence.Name);
  100. AddAnimation(animID, objectID);
  101. }
  102. /// <summary>
  103. /// Remove the specified animation
  104. /// </summary>
  105. /// <param name='animID'></param>
  106. /// <param name='allowNoDefault'>
  107. /// If true, then the default animation can be entirely removed.
  108. /// If false, then removing the default animation will reset it to the simulator default (currently STAND).
  109. /// </param>
  110. public void RemoveAnimation(UUID animID, bool allowNoDefault)
  111. {
  112. if (m_scenePresence.IsChildAgent)
  113. return;
  114. if (m_scenePresence.Scene.DebugAnimations)
  115. m_log.DebugFormat(
  116. "[SCENE PRESENCE ANIMATOR]: Removing animation {0} {1} for {2}",
  117. GetAnimName(animID), animID, m_scenePresence.Name);
  118. if (m_animations.Remove(animID, allowNoDefault))
  119. {
  120. SendAnimPack();
  121. m_scenePresence.TriggerScenePresenceUpdated();
  122. }
  123. }
  124. public void avnChangeAnim(UUID animID, bool addRemove, bool sendPack)
  125. {
  126. if (m_scenePresence.IsChildAgent)
  127. return;
  128. if (animID != UUID.Zero)
  129. {
  130. if (addRemove)
  131. m_animations.Add(animID, m_scenePresence.ControllingClient.NextAnimationSequenceNumber, UUID.Zero);
  132. else
  133. m_animations.Remove(animID, false);
  134. }
  135. if (sendPack)
  136. SendAnimPack();
  137. }
  138. // Called from scripts
  139. public void RemoveAnimation(string name)
  140. {
  141. if (m_scenePresence.IsChildAgent)
  142. return;
  143. // XXX: For some reason, we store all animations and use them with upper case names, but in LSL animations
  144. // are referenced with lower case names!
  145. UUID animID = DefaultAvatarAnimations.GetDefaultAnimation(name.ToUpper());
  146. if (animID == UUID.Zero)
  147. return;
  148. RemoveAnimation(animID, true);
  149. }
  150. public void ResetAnimations()
  151. {
  152. if (m_scenePresence.Scene.DebugAnimations)
  153. m_log.DebugFormat(
  154. "[SCENE PRESENCE ANIMATOR]: Resetting animations for {0} in {1}",
  155. m_scenePresence.Name, m_scenePresence.Scene.RegionInfo.RegionName);
  156. m_animations.Clear();
  157. }
  158. UUID aoSitGndAnim = UUID.Zero;
  159. /// <summary>
  160. /// The movement animation is reserved for "main" animations
  161. /// that are mutually exclusive, e.g. flying and sitting.
  162. /// </summary>
  163. /// <returns>'true' if the animation was updated</returns>
  164. ///
  165. public bool TrySetMovementAnimation(string anim)
  166. {
  167. bool ret = false;
  168. if (!m_scenePresence.IsChildAgent)
  169. {
  170. // m_log.DebugFormat(
  171. // "[SCENE PRESENCE ANIMATOR]: Setting movement animation {0} for {1}",
  172. // anim, m_scenePresence.Name);
  173. if (aoSitGndAnim != UUID.Zero)
  174. {
  175. avnChangeAnim(aoSitGndAnim, false, true);
  176. aoSitGndAnim = UUID.Zero;
  177. }
  178. UUID overridenAnim = m_scenePresence.Overrides.GetOverriddenAnimation(anim);
  179. if (overridenAnim != UUID.Zero)
  180. {
  181. if (anim == "SITGROUND")
  182. {
  183. UUID defsit = DefaultAvatarAnimations.AnimsUUID["SIT_GROUND_CONSTRAINED"];
  184. if (defsit == UUID.Zero)
  185. return false;
  186. m_animations.SetDefaultAnimation(defsit, m_scenePresence.ControllingClient.NextAnimationSequenceNumber, m_scenePresence.UUID);
  187. aoSitGndAnim = overridenAnim;
  188. avnChangeAnim(overridenAnim, true, false);
  189. }
  190. else
  191. {
  192. m_animations.SetDefaultAnimation(overridenAnim, m_scenePresence.ControllingClient.NextAnimationSequenceNumber, m_scenePresence.UUID);
  193. }
  194. m_scenePresence.SendScriptEventToAttachments("changed", new Object[] { (int)Changed.ANIMATION });
  195. SendAnimPack();
  196. ret = true;
  197. }
  198. else
  199. {
  200. // translate sit and sitground state animations
  201. if (anim == "SIT" || anim == "SITGROUND")
  202. anim = m_scenePresence.sitAnimation;
  203. if (m_animations.TrySetDefaultAnimation(
  204. anim, m_scenePresence.ControllingClient.NextAnimationSequenceNumber, m_scenePresence.UUID))
  205. {
  206. // m_log.DebugFormat(
  207. // "[SCENE PRESENCE ANIMATOR]: Updating movement animation to {0} for {1}",
  208. // anim, m_scenePresence.Name);
  209. // 16384 is CHANGED_ANIMATION
  210. m_scenePresence.SendScriptEventToAttachments("changed", new Object[] { (int)Changed.ANIMATION });
  211. SendAnimPack();
  212. ret = true;
  213. }
  214. }
  215. }
  216. else
  217. {
  218. m_log.WarnFormat(
  219. "[SCENE PRESENCE ANIMATOR]: Tried to set movement animation {0} on child presence {1}",
  220. anim, m_scenePresence.Name);
  221. }
  222. return ret;
  223. }
  224. public enum motionControlStates : byte
  225. {
  226. sitted = 0,
  227. flying,
  228. falling,
  229. jumping,
  230. landing,
  231. onsurface
  232. }
  233. public motionControlStates currentControlState = motionControlStates.onsurface;
  234. /// <summary>
  235. /// This method determines the proper movement related animation
  236. /// </summary>
  237. private string DetermineMovementAnimation()
  238. {
  239. const int FALL_DELAY = 800;
  240. const int PREJUMP_DELAY = 200;
  241. const int JUMP_PERIOD = 800;
  242. #region Inputs
  243. if (m_scenePresence.IsInTransit)
  244. return CurrentMovementAnimation;
  245. if (m_scenePresence.SitGround)
  246. {
  247. currentControlState = motionControlStates.sitted;
  248. return "SITGROUND";
  249. }
  250. if (m_scenePresence.ParentID != 0 || m_scenePresence.ParentUUID != UUID.Zero)
  251. {
  252. currentControlState = motionControlStates.sitted;
  253. return "SIT";
  254. }
  255. AgentManager.ControlFlags controlFlags = (AgentManager.ControlFlags)m_scenePresence.AgentControlFlags;
  256. PhysicsActor actor = m_scenePresence.PhysicsActor;
  257. const AgentManager.ControlFlags ANYXYMASK = (
  258. AgentManager.ControlFlags.AGENT_CONTROL_AT_POS | AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_AT_POS |
  259. AgentManager.ControlFlags.AGENT_CONTROL_AT_NEG | AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_AT_NEG |
  260. AgentManager.ControlFlags.AGENT_CONTROL_LEFT_POS | AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_LEFT_POS |
  261. AgentManager.ControlFlags.AGENT_CONTROL_LEFT_NEG | AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_LEFT_NEG
  262. );
  263. // Check control flags
  264. /* not in use
  265. bool heldForward = ((controlFlags & (AgentManager.ControlFlags.AGENT_CONTROL_AT_POS | AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_AT_POS)) != 0);
  266. bool heldBack = ((controlFlags & (AgentManager.ControlFlags.AGENT_CONTROL_AT_NEG | AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_AT_NEG)) != 0);
  267. bool heldLeft = ((controlFlags & (AgentManager.ControlFlags.AGENT_CONTROL_LEFT_POS | AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_LEFT_POS)) != 0);
  268. bool heldRight = ((controlFlags & (AgentManager.ControlFlags.AGENT_CONTROL_LEFT_NEG | AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_LEFT_NEG)) != 0);
  269. */
  270. bool heldTurnLeft = (controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_TURN_LEFT) == AgentManager.ControlFlags.AGENT_CONTROL_TURN_LEFT;
  271. bool heldTurnRight = (controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_TURN_RIGHT) == AgentManager.ControlFlags.AGENT_CONTROL_TURN_RIGHT;
  272. // bool heldUp = ((controlFlags & (AgentManager.ControlFlags.AGENT_CONTROL_UP_POS | AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_UP_POS)) != 0);
  273. // excluded nudge up so it doesn't trigger jump state
  274. bool heldUp = ((controlFlags & (AgentManager.ControlFlags.AGENT_CONTROL_UP_POS)) != 0);
  275. bool heldDown = ((controlFlags & (AgentManager.ControlFlags.AGENT_CONTROL_UP_NEG | AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_UP_NEG)) != 0);
  276. //bool flying = (controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_FLY) == AgentManager.ControlFlags.AGENT_CONTROL_FLY;
  277. //bool mouselook = (controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_MOUSELOOK) == AgentManager.ControlFlags.AGENT_CONTROL_MOUSELOOK;
  278. bool heldOnXY = ((controlFlags & ANYXYMASK) != 0);
  279. if (heldOnXY || heldUp || heldDown)
  280. {
  281. heldTurnLeft = false;
  282. heldTurnRight = false;
  283. }
  284. #endregion Inputs
  285. // no physics actor case
  286. if (actor == null)
  287. {
  288. // well what to do?
  289. currentControlState = motionControlStates.onsurface;
  290. if (heldOnXY)
  291. return "WALK";
  292. return "STAND";
  293. }
  294. #region Flying
  295. bool isColliding = actor.IsColliding;
  296. if (actor.Flying)
  297. {
  298. m_animTickFall = 0;
  299. m_animTickJump = 0;
  300. m_jumping = false;
  301. Falling = false;
  302. currentControlState = motionControlStates.flying;
  303. if (heldOnXY)
  304. {
  305. return (m_scenePresence.Scene.m_useFlySlow ? "FLYSLOW" : "FLY");
  306. }
  307. else if (heldUp)
  308. {
  309. return "HOVER_UP";
  310. }
  311. else if (heldDown)
  312. {
  313. if (isColliding)
  314. {
  315. actor.Flying = false;
  316. currentControlState = motionControlStates.landing;
  317. m_animTickLand = Environment.TickCount;
  318. return "LAND";
  319. }
  320. else
  321. return "HOVER_DOWN";
  322. }
  323. else
  324. {
  325. return "HOVER";
  326. }
  327. }
  328. else
  329. {
  330. if (isColliding && currentControlState == motionControlStates.flying)
  331. {
  332. currentControlState = motionControlStates.landing;
  333. m_animTickLand = Environment.TickCount;
  334. return "LAND";
  335. }
  336. }
  337. #endregion Flying
  338. #region Falling/Floating/Landing
  339. if (!isColliding && currentControlState != motionControlStates.jumping)
  340. {
  341. float fallVelocity = actor.Velocity.Z;
  342. // if stable on Hover assume falling
  343. if(actor.PIDHoverActive && fallVelocity < 0.05f)
  344. {
  345. Falling = true;
  346. currentControlState = motionControlStates.falling;
  347. m_lastFallVelocity = fallVelocity;
  348. return "FALLDOWN";
  349. }
  350. if (fallVelocity < -2.5f)
  351. Falling = true;
  352. if (m_animTickFall == 0 || (fallVelocity >= -0.5f))
  353. {
  354. m_animTickFall = Environment.TickCount;
  355. }
  356. else
  357. {
  358. int fallElapsed = (Environment.TickCount - m_animTickFall);
  359. if ((fallElapsed > FALL_DELAY) && (fallVelocity < -3.0f))
  360. {
  361. currentControlState = motionControlStates.falling;
  362. m_lastFallVelocity = fallVelocity;
  363. // Falling long enough to trigger the animation
  364. return "FALLDOWN";
  365. }
  366. }
  367. // Check if the user has stopped walking just now
  368. if (CurrentMovementAnimation == "WALK" && !heldOnXY && !heldDown && !heldUp)
  369. return "STAND";
  370. return CurrentMovementAnimation;
  371. }
  372. m_animTickFall = 0;
  373. #endregion Falling/Floating/Landing
  374. #region Jumping // section added for jumping...
  375. if (isColliding && heldUp && currentControlState != motionControlStates.jumping && !actor.PIDHoverActive)
  376. {
  377. // Start jumping, prejump
  378. currentControlState = motionControlStates.jumping;
  379. m_jumping = true;
  380. Falling = false;
  381. m_animTickJump = Environment.TickCount;
  382. return "PREJUMP";
  383. }
  384. if (currentControlState == motionControlStates.jumping)
  385. {
  386. int jumptime = Environment.TickCount - m_animTickJump;
  387. if ((jumptime > (JUMP_PERIOD * 1.5f)) && actor.IsColliding)
  388. {
  389. // end jumping
  390. m_jumping = false;
  391. Falling = false;
  392. actor.Selected = false; // borrowed for jumping flag
  393. m_animTickLand = Environment.TickCount;
  394. currentControlState = motionControlStates.landing;
  395. return "LAND";
  396. }
  397. else if (jumptime > JUMP_PERIOD)
  398. {
  399. // jump down
  400. return "JUMP";
  401. }
  402. else if (jumptime > PREJUMP_DELAY)
  403. {
  404. // jump up
  405. m_jumping = true;
  406. return "JUMP";
  407. }
  408. return CurrentMovementAnimation;
  409. }
  410. #endregion Jumping
  411. #region Ground Movement
  412. if (currentControlState == motionControlStates.falling)
  413. {
  414. Falling = false;
  415. currentControlState = motionControlStates.landing;
  416. m_animTickLand = Environment.TickCount;
  417. // TODO: SOFT_LAND support
  418. float fallVsq = m_lastFallVelocity * m_lastFallVelocity;
  419. if (fallVsq > 300f) // aprox 20*h
  420. return "STANDUP";
  421. else if (fallVsq > 160f)
  422. return "SOFT_LAND";
  423. else
  424. return "LAND";
  425. }
  426. if (currentControlState == motionControlStates.landing)
  427. {
  428. Falling = false;
  429. int landElapsed = Environment.TickCount - m_animTickLand;
  430. int limit = 1000;
  431. if (CurrentMovementAnimation == "LAND")
  432. limit = 350;
  433. // NB if the above is set too long a weird anim reset from some place prevents STAND from being sent to client
  434. if ((m_animTickLand != 0) && (landElapsed <= limit))
  435. {
  436. return CurrentMovementAnimation;
  437. }
  438. else
  439. {
  440. currentControlState = motionControlStates.onsurface;
  441. m_animTickLand = 0;
  442. return "STAND";
  443. }
  444. }
  445. // next section moved outside paren. and realigned for jumping
  446. if (heldOnXY)
  447. {
  448. currentControlState = motionControlStates.onsurface;
  449. Falling = false;
  450. // Walking / crouchwalking / running
  451. if (heldDown)
  452. {
  453. return "CROUCHWALK";
  454. }
  455. // We need to prevent these animations if the user tries to make their avatar walk or run whilst
  456. // specifying AGENT_CONTROL_STOP (pressing down space on viewers).
  457. else if (!m_scenePresence.AgentControlStopActive)
  458. {
  459. if (m_scenePresence.SetAlwaysRun)
  460. return "RUN";
  461. else
  462. return "WALK";
  463. }
  464. }
  465. else
  466. {
  467. currentControlState = motionControlStates.onsurface;
  468. Falling = false;
  469. // Not walking
  470. if (heldDown)
  471. return "CROUCH";
  472. else if (heldTurnLeft)
  473. return "TURNLEFT";
  474. else if (heldTurnRight)
  475. return "TURNRIGHT";
  476. else
  477. return "STAND";
  478. }
  479. #endregion Ground Movement
  480. return CurrentMovementAnimation;
  481. }
  482. /// <summary>
  483. /// Update the movement animation of this avatar according to its current state
  484. /// </summary>
  485. /// <returns>'true' if the animation was changed</returns>
  486. public bool UpdateMovementAnimations()
  487. {
  488. // m_log.DebugFormat("[SCENE PRESENCE ANIMATOR]: Updating movement animations for {0}", m_scenePresence.Name);
  489. bool ret = false;
  490. lock (m_animations)
  491. {
  492. string newMovementAnimation = DetermineMovementAnimation();
  493. if (CurrentMovementAnimation != newMovementAnimation)
  494. {
  495. CurrentMovementAnimation = newMovementAnimation;
  496. // m_log.DebugFormat(
  497. // "[SCENE PRESENCE ANIMATOR]: Determined animation {0} for {1} in UpdateMovementAnimations()",
  498. // CurrentMovementAnimation, m_scenePresence.Name);
  499. // Only set it if it's actually changed, give a script
  500. // a chance to stop a default animation
  501. ret = TrySetMovementAnimation(CurrentMovementAnimation);
  502. }
  503. }
  504. return ret;
  505. }
  506. public bool ForceUpdateMovementAnimations()
  507. {
  508. lock (m_animations)
  509. {
  510. CurrentMovementAnimation = DetermineMovementAnimation();
  511. return TrySetMovementAnimation(CurrentMovementAnimation);
  512. }
  513. }
  514. public bool SetMovementAnimations(string motionState)
  515. {
  516. lock (m_animations)
  517. {
  518. CurrentMovementAnimation = motionState;
  519. return TrySetMovementAnimation(CurrentMovementAnimation);
  520. }
  521. }
  522. public UUID[] GetAnimationArray()
  523. {
  524. UUID[] animIDs;
  525. int[] sequenceNums;
  526. UUID[] objectIDs;
  527. m_animations.GetArrays(out animIDs, out sequenceNums, out objectIDs);
  528. return animIDs;
  529. }
  530. public BinBVHAnimation GenerateRandomAnimation()
  531. {
  532. int rnditerations = 3;
  533. BinBVHAnimation anim = new BinBVHAnimation();
  534. List<string> parts = new List<string>();
  535. /// Torso and Head
  536. parts.Add("mPelvis");
  537. parts.Add("mTorso");
  538. parts.Add("mChest");
  539. parts.Add("mNeck");
  540. parts.Add("mHead");
  541. parts.Add("mSkull");
  542. parts.Add("mEyeRight");
  543. parts.Add("mEyeLeft");
  544. /// Arms
  545. parts.Add("mCollarLeft");
  546. parts.Add("mShoulderLeft");
  547. parts.Add("mElbowLeft");
  548. parts.Add("mWristLeft");
  549. parts.Add("mCollarRight");
  550. parts.Add("mShoulderRight");
  551. parts.Add("mElbowRight");
  552. parts.Add("mWristRight");
  553. /// Legs
  554. parts.Add("mHipLeft");
  555. parts.Add("mKneeLeft");
  556. parts.Add("mAnkleLeft");
  557. parts.Add("mFootLeft");
  558. parts.Add("mToeLeft");
  559. parts.Add("mHipRight");
  560. parts.Add("mKneeRight");
  561. parts.Add("mAnkleRight");
  562. parts.Add("mFootRight");
  563. parts.Add("mToeRight");
  564. ///Hands
  565. parts.Add("mHandThumb1Left");
  566. parts.Add("mHandThumb1Right");
  567. parts.Add("mHandThumb2Left");
  568. parts.Add("mHandThumb2Right");
  569. parts.Add("mHandThumb3Left");
  570. parts.Add("mHandThumb3Right");
  571. parts.Add("mHandIndex1Left");
  572. parts.Add("mHandIndex1Right");
  573. parts.Add("mHandIndex2Left");
  574. parts.Add("mHandIndex2Right");
  575. parts.Add("mHandIndex3Left");
  576. parts.Add("mHandIndex3Right");
  577. parts.Add("mHandMiddle1Left");
  578. parts.Add("mHandMiddle1Right");
  579. parts.Add("mHandMiddle2Left");
  580. parts.Add("mHandMiddle2Right");
  581. parts.Add("mHandMiddle3Left");
  582. parts.Add("mHandMiddle3Right");
  583. parts.Add("mHandRing1Left");
  584. parts.Add("mHandRing1Right");
  585. parts.Add("mHandRing2Left");
  586. parts.Add("mHandRing2Right");
  587. parts.Add("mHandRing3Left");
  588. parts.Add("mHandRing3Right");
  589. parts.Add("mHandPinky1Left");
  590. parts.Add("mHandPinky1Right");
  591. parts.Add("mHandPinky2Left");
  592. parts.Add("mHandPinky2Right");
  593. parts.Add("mHandPinky3Left");
  594. parts.Add("mHandPinky3Right");
  595. ///Face
  596. parts.Add("mFaceForeheadLeft");
  597. parts.Add("mFaceForeheadCenter");
  598. parts.Add("mFaceForeheadRight");
  599. parts.Add("mFaceEyebrowOuterLeft");
  600. parts.Add("mFaceEyebrowCenterLeft");
  601. parts.Add("mFaceEyebrowInnerLeft");
  602. parts.Add("mFaceEyebrowOuterRight");
  603. parts.Add("mFaceEyebrowCenterRight");
  604. parts.Add("mFaceEyebrowInnerRight");
  605. parts.Add("mFaceEyeLidUpperLeft");
  606. parts.Add("mFaceEyeLidLowerLeft");
  607. parts.Add("mFaceEyeLidUpperRight");
  608. parts.Add("mFaceEyeLidLowerRight");
  609. parts.Add("mFaceEyeAltLeft");
  610. parts.Add("mFaceEyeAltRight");
  611. parts.Add("mFaceEyecornerInnerLeft");
  612. parts.Add("mFaceEyecornerInnerRight");
  613. parts.Add("mFaceEar1Left");
  614. parts.Add("mFaceEar2Left");
  615. parts.Add("mFaceEar1Right");
  616. parts.Add("mFaceEar2Right");
  617. parts.Add("mFaceNoseLeft");
  618. parts.Add("mFaceNoseCenter");
  619. parts.Add("mFaceNoseRight");
  620. parts.Add("mFaceNoseBase");
  621. parts.Add("mFaceNoseBridge");
  622. parts.Add("mFaceCheekUpperInnerLeft");
  623. parts.Add("mFaceCheekUpperOuterLeft");
  624. parts.Add("mFaceCheekUpperInnerRight");
  625. parts.Add("mFaceCheekUpperOuterRight");
  626. parts.Add("mFaceJaw");
  627. parts.Add("mFaceLipUpperLeft");
  628. parts.Add("mFaceLipUpperCenter");
  629. parts.Add("mFaceLipUpperRight");
  630. parts.Add("mFaceLipCornerLeft");
  631. parts.Add("mFaceLipCornerRight");
  632. parts.Add("mFaceTongueBase");
  633. parts.Add("mFaceTongueTip");
  634. parts.Add("mFaceLipLowerLeft");
  635. parts.Add("mFaceLipLowerCenter");
  636. parts.Add("mFaceLipLowerRight");
  637. parts.Add("mFaceTeethLower");
  638. parts.Add("mFaceTeethUpper");
  639. parts.Add("mFaceChin");
  640. ///Spine
  641. parts.Add("mSpine1");
  642. parts.Add("mSpine2");
  643. parts.Add("mSpine3");
  644. parts.Add("mSpine4");
  645. ///Wings
  646. parts.Add("mWingsRoot");
  647. parts.Add("mWing1Left");
  648. parts.Add("mWing2Left");
  649. parts.Add("mWing3Left");
  650. parts.Add("mWing4Left");
  651. parts.Add("mWing1Right");
  652. parts.Add("mWing2Right");
  653. parts.Add("mWing3Right");
  654. parts.Add("mWing4Right");
  655. parts.Add("mWing4FanRight");
  656. parts.Add("mWing4FanLeft");
  657. ///Hind Limbs
  658. parts.Add("mHindLimbsRoot");
  659. parts.Add("mHindLimb1Left");
  660. parts.Add("mHindLimb2Left");
  661. parts.Add("mHindLimb3Left");
  662. parts.Add("mHindLimb4Left");
  663. parts.Add("mHindLimb1Right");
  664. parts.Add("mHindLimb2Right");
  665. parts.Add("mHindLimb3Right");
  666. parts.Add("mHindLimb4Right");
  667. ///Tail
  668. parts.Add("mTail1");
  669. parts.Add("mTail2");
  670. parts.Add("mTail3");
  671. parts.Add("mTail4");
  672. parts.Add("mTail5");
  673. parts.Add("mTail6");
  674. anim.HandPose = 1;
  675. anim.InPoint = 0;
  676. anim.OutPoint = (rnditerations * .10f);
  677. anim.Priority = 7;
  678. anim.Loop = false;
  679. anim.Length = (rnditerations * .10f);
  680. anim.ExpressionName = "afraid";
  681. anim.EaseInTime = 0;
  682. anim.EaseOutTime = 0;
  683. string[] strjoints = parts.ToArray();
  684. anim.Joints = new binBVHJoint[strjoints.Length];
  685. for (int j = 0; j < strjoints.Length; j++)
  686. {
  687. anim.Joints[j] = new binBVHJoint();
  688. anim.Joints[j].Name = strjoints[j];
  689. anim.Joints[j].Priority = 7;
  690. anim.Joints[j].positionkeys = new binBVHJointKey[rnditerations];
  691. anim.Joints[j].rotationkeys = new binBVHJointKey[rnditerations];
  692. Random rnd = new Random();
  693. for (int i = 0; i < rnditerations; i++)
  694. {
  695. anim.Joints[j].rotationkeys[i] = new binBVHJointKey();
  696. anim.Joints[j].rotationkeys[i].time = (i * .10f);
  697. anim.Joints[j].rotationkeys[i].key_element.X = ((float)rnd.NextDouble() * 2 - 1);
  698. anim.Joints[j].rotationkeys[i].key_element.Y = ((float)rnd.NextDouble() * 2 - 1);
  699. anim.Joints[j].rotationkeys[i].key_element.Z = ((float)rnd.NextDouble() * 2 - 1);
  700. anim.Joints[j].positionkeys[i] = new binBVHJointKey();
  701. anim.Joints[j].positionkeys[i].time = (i * .10f);
  702. anim.Joints[j].positionkeys[i].key_element.X = 0;
  703. anim.Joints[j].positionkeys[i].key_element.Y = 0;
  704. anim.Joints[j].positionkeys[i].key_element.Z = 0;
  705. }
  706. }
  707. AssetBase Animasset = new AssetBase(UUID.Random(), "Random Animation", (sbyte)AssetType.Animation, m_scenePresence.UUID.ToString());
  708. Animasset.Data = anim.ToBytes();
  709. Animasset.Temporary = true;
  710. Animasset.Local = true;
  711. Animasset.Description = "dance";
  712. //BinBVHAnimation bbvhanim = new BinBVHAnimation(Animasset.Data);
  713. m_scenePresence.Scene.AssetService.Store(Animasset);
  714. AddAnimation(Animasset.FullID, m_scenePresence.UUID);
  715. return anim;
  716. }
  717. /// <summary>
  718. ///
  719. /// </summary>
  720. /// <param name="animations"></param>
  721. /// <param name="seqs"></param>
  722. /// <param name="objectIDs"></param>
  723. public void SendAnimPack(UUID[] animations, int[] seqs, UUID[] objectIDs)
  724. {
  725. m_scenePresence.SendAnimPack(animations, seqs, objectIDs);
  726. }
  727. public void GetArrays(out UUID[] animIDs, out int[] sequenceNums, out UUID[] objectIDs)
  728. {
  729. animIDs = null;
  730. sequenceNums = null;
  731. objectIDs = null;
  732. if (m_animations != null)
  733. m_animations.GetArrays(out animIDs, out sequenceNums, out objectIDs);
  734. }
  735. public void SendAnimPackToClient(IClientAPI client)
  736. {
  737. if (m_scenePresence.IsChildAgent)
  738. return;
  739. UUID[] animIDs;
  740. int[] sequenceNums;
  741. UUID[] objectIDs;
  742. m_animations.GetArrays(out animIDs, out sequenceNums, out objectIDs);
  743. client.SendAnimations(animIDs, sequenceNums, m_scenePresence.ControllingClient.AgentId, objectIDs);
  744. }
  745. /// <summary>
  746. /// Send animation information about this avatar to all clients.
  747. /// </summary>
  748. public void SendAnimPack()
  749. {
  750. //m_log.Debug("Sending animation pack to all");
  751. if (m_scenePresence.IsChildAgent)
  752. return;
  753. UUID[] animIDs;
  754. int[] sequenceNums;
  755. UUID[] objectIDs;
  756. m_animations.GetArrays(out animIDs, out sequenceNums, out objectIDs);
  757. // SendAnimPack(animIDs, sequenceNums, objectIDs);
  758. m_scenePresence.SendAnimPack(animIDs, sequenceNums, objectIDs);
  759. }
  760. public string GetAnimName(UUID animId)
  761. {
  762. string animName;
  763. if (!DefaultAvatarAnimations.AnimsNames.TryGetValue(animId, out animName))
  764. {
  765. AssetMetadata amd = m_scenePresence.Scene.AssetService.GetMetadata(animId.ToString());
  766. if (amd != null)
  767. animName = amd.Name;
  768. else
  769. animName = "Unknown";
  770. }
  771. return animName;
  772. }
  773. }
  774. }