ScenePresenceAnimator.cs 33 KB

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