1
0

ScenePresenceAnimator.cs 22 KB

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