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