ScenePresenceAnimator.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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 OpenMetaverse;
  30. using OpenSim.Framework;
  31. using OpenSim.Region.Framework.Interfaces;
  32. using OpenSim.Region.Framework.Scenes;
  33. using OpenSim.Region.Physics.Manager;
  34. namespace OpenSim.Region.Framework.Scenes.Animation
  35. {
  36. /// <summary>
  37. /// Handle all animation duties for a scene presence
  38. /// </summary>
  39. public class ScenePresenceAnimator
  40. {
  41. public AnimationSet Animations
  42. {
  43. get { return m_animations; }
  44. }
  45. protected AnimationSet m_animations = new AnimationSet();
  46. /// <value>
  47. /// The current movement animation
  48. /// </value>
  49. public string CurrentMovementAnimation
  50. {
  51. get { return m_movementAnimation; }
  52. }
  53. protected string m_movementAnimation = "DEFAULT";
  54. private int m_animTickFall;
  55. private int m_animTickJump;
  56. /// <value>
  57. /// The scene presence that this animator applies to
  58. /// </value>
  59. protected ScenePresence m_scenePresence;
  60. public ScenePresenceAnimator(ScenePresence sp)
  61. {
  62. m_scenePresence = sp;
  63. }
  64. public void AddAnimation(UUID animID, UUID objectID)
  65. {
  66. if (m_scenePresence.IsChildAgent)
  67. return;
  68. if (m_animations.Add(animID, m_scenePresence.ControllingClient.NextAnimationSequenceNumber, objectID))
  69. SendAnimPack();
  70. }
  71. // Called from scripts
  72. public void AddAnimation(string name, UUID objectID)
  73. {
  74. if (m_scenePresence.IsChildAgent)
  75. return;
  76. UUID animID = m_scenePresence.ControllingClient.GetDefaultAnimation(name);
  77. if (animID == UUID.Zero)
  78. return;
  79. AddAnimation(animID, objectID);
  80. }
  81. public void RemoveAnimation(UUID animID)
  82. {
  83. if (m_scenePresence.IsChildAgent)
  84. return;
  85. if (m_animations.Remove(animID))
  86. SendAnimPack();
  87. }
  88. // Called from scripts
  89. public void RemoveAnimation(string name)
  90. {
  91. if (m_scenePresence.IsChildAgent)
  92. return;
  93. UUID animID = m_scenePresence.ControllingClient.GetDefaultAnimation(name);
  94. if (animID == UUID.Zero)
  95. return;
  96. RemoveAnimation(animID);
  97. }
  98. public void ResetAnimations()
  99. {
  100. m_animations.Clear();
  101. }
  102. /// <summary>
  103. /// The movement animation is reserved for "main" animations
  104. /// that are mutually exclusive, e.g. flying and sitting.
  105. /// </summary>
  106. public void TrySetMovementAnimation(string anim)
  107. {
  108. //m_log.DebugFormat("Updating movement animation to {0}", anim);
  109. if (!m_scenePresence.IsChildAgent)
  110. {
  111. if (m_animations.TrySetDefaultAnimation(
  112. anim, m_scenePresence.ControllingClient.NextAnimationSequenceNumber, m_scenePresence.UUID))
  113. {
  114. // 16384 is CHANGED_ANIMATION
  115. m_scenePresence.SendScriptEventToAttachments("changed", new Object[] { 16384 });
  116. SendAnimPack();
  117. }
  118. }
  119. }
  120. /// <summary>
  121. /// This method determines the proper movement related animation
  122. /// </summary>
  123. public string GetMovementAnimation()
  124. {
  125. const float FALL_DELAY = 0.33f;
  126. const float PREJUMP_DELAY = 0.25f;
  127. #region Inputs
  128. if (m_scenePresence.SitGround)
  129. {
  130. return "SIT_GROUND_CONSTRAINED";
  131. }
  132. AgentManager.ControlFlags controlFlags = (AgentManager.ControlFlags)m_scenePresence.AgentControlFlags;
  133. PhysicsActor actor = m_scenePresence.PhysicsActor;
  134. // Create forward and left vectors from the current avatar rotation
  135. Matrix4 rotMatrix = Matrix4.CreateFromQuaternion(m_scenePresence.Rotation);
  136. Vector3 fwd = Vector3.Transform(Vector3.UnitX, rotMatrix);
  137. Vector3 left = Vector3.Transform(Vector3.UnitY, rotMatrix);
  138. // Check control flags
  139. bool heldForward =
  140. (((controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_AT_POS) == AgentManager.ControlFlags.AGENT_CONTROL_AT_POS) || ((controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_AT_POS) == AgentManager.ControlFlags.AGENT_CONTROL_NUDGE_AT_POS));
  141. bool heldBack = (controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_AT_NEG) == AgentManager.ControlFlags.AGENT_CONTROL_AT_NEG;
  142. bool heldLeft = (controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_LEFT_POS) == AgentManager.ControlFlags.AGENT_CONTROL_LEFT_POS;
  143. bool heldRight = (controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_LEFT_NEG) == AgentManager.ControlFlags.AGENT_CONTROL_LEFT_NEG;
  144. //bool heldTurnLeft = (controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_TURN_LEFT) == AgentManager.ControlFlags.AGENT_CONTROL_TURN_LEFT;
  145. //bool heldTurnRight = (controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_TURN_RIGHT) == AgentManager.ControlFlags.AGENT_CONTROL_TURN_RIGHT;
  146. bool heldUp = (controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_UP_POS) == AgentManager.ControlFlags.AGENT_CONTROL_UP_POS;
  147. bool heldDown = (controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_UP_NEG) == AgentManager.ControlFlags.AGENT_CONTROL_UP_NEG;
  148. //bool flying = (controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_FLY) == AgentManager.ControlFlags.AGENT_CONTROL_FLY;
  149. //bool mouselook = (controlFlags & AgentManager.ControlFlags.AGENT_CONTROL_MOUSELOOK) == AgentManager.ControlFlags.AGENT_CONTROL_MOUSELOOK;
  150. // Direction in which the avatar is trying to move
  151. Vector3 move = Vector3.Zero;
  152. if (heldForward) { move.X += fwd.X; move.Y += fwd.Y; }
  153. if (heldBack) { move.X -= fwd.X; move.Y -= fwd.Y; }
  154. if (heldLeft) { move.X += left.X; move.Y += left.Y; }
  155. if (heldRight) { move.X -= left.X; move.Y -= left.Y; }
  156. if (heldUp) { move.Z += 1; }
  157. if (heldDown) { move.Z -= 1; }
  158. // Is the avatar trying to move?
  159. // bool moving = (move != Vector3.Zero);
  160. bool jumping = m_animTickJump != 0;
  161. #endregion Inputs
  162. #region Flying
  163. if (actor != null && actor.Flying)
  164. {
  165. m_animTickFall = 0;
  166. m_animTickJump = 0;
  167. if (move.X != 0f || move.Y != 0f)
  168. {
  169. return (m_scenePresence.Scene.m_useFlySlow ? "FLYSLOW" : "FLY");
  170. }
  171. else if (move.Z > 0f)
  172. {
  173. return "HOVER_UP";
  174. }
  175. else if (move.Z < 0f)
  176. {
  177. if (actor != null && actor.IsColliding)
  178. return "LAND";
  179. else
  180. return "HOVER_DOWN";
  181. }
  182. else
  183. {
  184. return "HOVER";
  185. }
  186. }
  187. #endregion Flying
  188. #region Falling/Floating/Landing
  189. if (actor == null || !actor.IsColliding)
  190. {
  191. float fallElapsed = (float)(Environment.TickCount - m_animTickFall) / 1000f;
  192. float fallVelocity = (actor != null) ? actor.Velocity.Z : 0.0f;
  193. if (m_animTickFall == 0 || (fallElapsed > FALL_DELAY && fallVelocity >= 0.0f))
  194. {
  195. // Just started falling
  196. m_animTickFall = Environment.TickCount;
  197. }
  198. else if (!jumping && fallElapsed > FALL_DELAY)
  199. {
  200. // Falling long enough to trigger the animation
  201. return "FALLDOWN";
  202. }
  203. return m_movementAnimation;
  204. }
  205. #endregion Falling/Floating/Landing
  206. #region Ground Movement
  207. if (m_movementAnimation == "FALLDOWN")
  208. {
  209. m_animTickFall = Environment.TickCount;
  210. // TODO: SOFT_LAND support
  211. return "LAND";
  212. }
  213. else if (m_movementAnimation == "LAND")
  214. {
  215. float landElapsed = (float)(Environment.TickCount - m_animTickFall) / 1000f;
  216. if ((m_animTickFall != 0) && (landElapsed <= FALL_DELAY))
  217. return "LAND";
  218. }
  219. m_animTickFall = 0;
  220. if (move.Z > 0f)
  221. {
  222. // Jumping
  223. if (!jumping)
  224. {
  225. // Begin prejump
  226. m_animTickJump = Environment.TickCount;
  227. return "PREJUMP";
  228. }
  229. else if (Environment.TickCount - m_animTickJump > PREJUMP_DELAY * 1000.0f)
  230. {
  231. // Start actual jump
  232. if (m_animTickJump == -1)
  233. {
  234. // Already jumping! End the current jump
  235. m_animTickJump = 0;
  236. return "JUMP";
  237. }
  238. m_animTickJump = -1;
  239. return "JUMP";
  240. }
  241. }
  242. else
  243. {
  244. // Not jumping
  245. m_animTickJump = 0;
  246. if (move.X != 0f || move.Y != 0f)
  247. {
  248. // Walking / crouchwalking / running
  249. if (move.Z < 0f)
  250. return "CROUCHWALK";
  251. else if (m_scenePresence.SetAlwaysRun)
  252. return "RUN";
  253. else
  254. return "WALK";
  255. }
  256. else
  257. {
  258. // Not walking
  259. if (move.Z < 0f)
  260. return "CROUCH";
  261. else
  262. return "STAND";
  263. }
  264. }
  265. #endregion Ground Movement
  266. return m_movementAnimation;
  267. }
  268. /// <summary>
  269. /// Update the movement animation of this avatar according to its current state
  270. /// </summary>
  271. public void UpdateMovementAnimations()
  272. {
  273. m_movementAnimation = GetMovementAnimation();
  274. if (m_movementAnimation == "PREJUMP" && !m_scenePresence.Scene.m_usePreJump)
  275. {
  276. // This was the previous behavior before PREJUMP
  277. TrySetMovementAnimation("JUMP");
  278. }
  279. else
  280. {
  281. TrySetMovementAnimation(m_movementAnimation);
  282. }
  283. }
  284. public UUID[] GetAnimationArray()
  285. {
  286. UUID[] animIDs;
  287. int[] sequenceNums;
  288. UUID[] objectIDs;
  289. m_animations.GetArrays(out animIDs, out sequenceNums, out objectIDs);
  290. return animIDs;
  291. }
  292. public BinBVHAnimation GenerateRandomAnimation()
  293. {
  294. int rnditerations = 3;
  295. BinBVHAnimation anim = new BinBVHAnimation();
  296. List<string> parts = new List<string>();
  297. parts.Add("mPelvis");parts.Add("mHead");parts.Add("mTorso");
  298. parts.Add("mHipLeft");parts.Add("mHipRight");parts.Add("mHipLeft");parts.Add("mKneeLeft");
  299. parts.Add("mKneeRight");parts.Add("mCollarLeft");parts.Add("mCollarRight");parts.Add("mNeck");
  300. parts.Add("mElbowLeft");parts.Add("mElbowRight");parts.Add("mWristLeft");parts.Add("mWristRight");
  301. parts.Add("mShoulderLeft");parts.Add("mShoulderRight");parts.Add("mAnkleLeft");parts.Add("mAnkleRight");
  302. parts.Add("mEyeRight");parts.Add("mChest");parts.Add("mToeLeft");parts.Add("mToeRight");
  303. parts.Add("mFootLeft");parts.Add("mFootRight");parts.Add("mEyeLeft");
  304. anim.HandPose = 1;
  305. anim.InPoint = 0;
  306. anim.OutPoint = (rnditerations * .10f);
  307. anim.Priority = 7;
  308. anim.Loop = false;
  309. anim.Length = (rnditerations * .10f);
  310. anim.ExpressionName = "afraid";
  311. anim.EaseInTime = 0;
  312. anim.EaseOutTime = 0;
  313. string[] strjoints = parts.ToArray();
  314. anim.Joints = new binBVHJoint[strjoints.Length];
  315. for (int j = 0; j < strjoints.Length; j++)
  316. {
  317. anim.Joints[j] = new binBVHJoint();
  318. anim.Joints[j].Name = strjoints[j];
  319. anim.Joints[j].Priority = 7;
  320. anim.Joints[j].positionkeys = new binBVHJointKey[rnditerations];
  321. anim.Joints[j].rotationkeys = new binBVHJointKey[rnditerations];
  322. Random rnd = new Random();
  323. for (int i = 0; i < rnditerations; i++)
  324. {
  325. anim.Joints[j].rotationkeys[i] = new binBVHJointKey();
  326. anim.Joints[j].rotationkeys[i].time = (i*.10f);
  327. anim.Joints[j].rotationkeys[i].key_element.X = ((float) rnd.NextDouble()*2 - 1);
  328. anim.Joints[j].rotationkeys[i].key_element.Y = ((float) rnd.NextDouble()*2 - 1);
  329. anim.Joints[j].rotationkeys[i].key_element.Z = ((float) rnd.NextDouble()*2 - 1);
  330. anim.Joints[j].positionkeys[i] = new binBVHJointKey();
  331. anim.Joints[j].positionkeys[i].time = (i*.10f);
  332. anim.Joints[j].positionkeys[i].key_element.X = 0;
  333. anim.Joints[j].positionkeys[i].key_element.Y = 0;
  334. anim.Joints[j].positionkeys[i].key_element.Z = 0;
  335. }
  336. }
  337. AssetBase Animasset = new AssetBase(UUID.Random(), "Random Animation", (sbyte)AssetType.Animation, m_scenePresence.UUID.ToString());
  338. Animasset.Data = anim.ToBytes();
  339. Animasset.Temporary = true;
  340. Animasset.Local = true;
  341. Animasset.Description = "dance";
  342. //BinBVHAnimation bbvhanim = new BinBVHAnimation(Animasset.Data);
  343. m_scenePresence.Scene.AssetService.Store(Animasset);
  344. AddAnimation(Animasset.FullID, m_scenePresence.UUID);
  345. return anim;
  346. }
  347. /// <summary>
  348. ///
  349. /// </summary>
  350. /// <param name="animations"></param>
  351. /// <param name="seqs"></param>
  352. /// <param name="objectIDs"></param>
  353. public void SendAnimPack(UUID[] animations, int[] seqs, UUID[] objectIDs)
  354. {
  355. if (m_scenePresence.IsChildAgent)
  356. return;
  357. m_scenePresence.Scene.ForEachClient(
  358. delegate(IClientAPI client)
  359. {
  360. client.SendAnimations(animations, seqs, m_scenePresence.ControllingClient.AgentId, objectIDs);
  361. });
  362. }
  363. public void SendAnimPackToClient(IClientAPI client)
  364. {
  365. if (m_scenePresence.IsChildAgent)
  366. return;
  367. UUID[] animIDs;
  368. int[] sequenceNums;
  369. UUID[] objectIDs;
  370. m_animations.GetArrays(out animIDs, out sequenceNums, out objectIDs);
  371. client.SendAnimations(animIDs, sequenceNums, m_scenePresence.ControllingClient.AgentId, objectIDs);
  372. }
  373. /// <summary>
  374. /// Send animation information about this avatar to all clients.
  375. /// </summary>
  376. public void SendAnimPack()
  377. {
  378. //m_log.Debug("Sending animation pack to all");
  379. if (m_scenePresence.IsChildAgent)
  380. return;
  381. UUID[] animIDs;
  382. int[] sequenceNums;
  383. UUID[] objectIDs;
  384. m_animations.GetArrays(out animIDs, out sequenceNums, out objectIDs);
  385. SendAnimPack(animIDs, sequenceNums, objectIDs);
  386. }
  387. public void Close()
  388. {
  389. m_animations = null;
  390. m_scenePresence = null;
  391. }
  392. }
  393. }