ScenePresenceAnimator.cs 34 KB

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