ScenePresenceAnimator.cs 18 KB

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