PageRenderTime 81ms CodeModel.GetById 15ms app.highlight 58ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcharacter/llmotioncontroller.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 1127 lines | 729 code | 133 blank | 265 comment | 154 complexity | b1572596f6843de0d3ccae63f81e6219 MD5 | raw file
   1/** 
   2 * @file llmotioncontroller.cpp
   3 * @brief Implementation of LLMotionController class.
   4 *
   5 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
   6 * Second Life Viewer Source Code
   7 * Copyright (C) 2010, Linden Research, Inc.
   8 * 
   9 * This library is free software; you can redistribute it and/or
  10 * modify it under the terms of the GNU Lesser General Public
  11 * License as published by the Free Software Foundation;
  12 * version 2.1 of the License only.
  13 * 
  14 * This library is distributed in the hope that it will be useful,
  15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  17 * Lesser General Public License for more details.
  18 * 
  19 * You should have received a copy of the GNU Lesser General Public
  20 * License along with this library; if not, write to the Free Software
  21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  22 * 
  23 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
  24 * $/LicenseInfo$
  25 */
  26
  27//-----------------------------------------------------------------------------
  28// Header Files
  29//-----------------------------------------------------------------------------
  30#include "linden_common.h"
  31
  32#include "llmemtype.h"
  33
  34#include "llmotioncontroller.h"
  35#include "llkeyframemotion.h"
  36#include "llmath.h"
  37#include "lltimer.h"
  38#include "llanimationstates.h"
  39#include "llstl.h"
  40
  41const S32 NUM_JOINT_SIGNATURE_STRIDES = LL_CHARACTER_MAX_JOINTS / 4;
  42const U32 MAX_MOTION_INSTANCES = 32;
  43
  44//-----------------------------------------------------------------------------
  45// Constants and statics
  46//-----------------------------------------------------------------------------
  47LLMotionRegistry LLMotionController::sRegistry;
  48
  49//-----------------------------------------------------------------------------
  50//-----------------------------------------------------------------------------
  51// LLMotionRegistry class
  52//-----------------------------------------------------------------------------
  53//-----------------------------------------------------------------------------
  54
  55//-----------------------------------------------------------------------------
  56// LLMotionRegistry()
  57// Class Constructor
  58//-----------------------------------------------------------------------------
  59LLMotionRegistry::LLMotionRegistry()
  60{
  61	
  62}
  63
  64
  65//-----------------------------------------------------------------------------
  66// ~LLMotionRegistry()
  67// Class Destructor
  68//-----------------------------------------------------------------------------
  69LLMotionRegistry::~LLMotionRegistry()
  70{
  71	mMotionTable.clear();
  72}
  73
  74
  75//-----------------------------------------------------------------------------
  76// addMotion()
  77//-----------------------------------------------------------------------------
  78BOOL LLMotionRegistry::registerMotion( const LLUUID& id, LLMotionConstructor constructor )
  79{
  80	//	llinfos << "Registering motion: " << name << llendl;
  81	if (!is_in_map(mMotionTable, id))
  82	{
  83		mMotionTable[id] = constructor;
  84		return TRUE;
  85	}
  86	
  87	return FALSE;
  88}
  89
  90//-----------------------------------------------------------------------------
  91// markBad()
  92//-----------------------------------------------------------------------------
  93void LLMotionRegistry::markBad( const LLUUID& id )
  94{
  95	mMotionTable[id] = LLMotionConstructor(NULL);
  96}
  97
  98//-----------------------------------------------------------------------------
  99// createMotion()
 100//-----------------------------------------------------------------------------
 101LLMotion *LLMotionRegistry::createMotion( const LLUUID &id )
 102{
 103	LLMotionConstructor constructor = get_if_there(mMotionTable, id, LLMotionConstructor(NULL));
 104	LLMotion* motion = NULL;
 105
 106	if ( constructor == NULL )
 107	{
 108		// *FIX: need to replace with a better default scheme. RN
 109		motion = LLKeyframeMotion::create(id);
 110	}
 111	else
 112	{
 113		motion = constructor(id);
 114	}
 115
 116	return motion;
 117}
 118
 119//-----------------------------------------------------------------------------
 120//-----------------------------------------------------------------------------
 121// LLMotionController class
 122//-----------------------------------------------------------------------------
 123//-----------------------------------------------------------------------------
 124
 125//-----------------------------------------------------------------------------
 126// LLMotionController()
 127// Class Constructor
 128//-----------------------------------------------------------------------------
 129LLMotionController::LLMotionController()
 130	: mTimeFactor(1.f),
 131	  mCharacter(NULL),
 132	  mAnimTime(0.f),
 133	  mPrevTimerElapsed(0.f),
 134	  mLastTime(0.0f),
 135	  mHasRunOnce(FALSE),
 136	  mPaused(FALSE),
 137	  mPauseTime(0.f),
 138	  mTimeStep(0.f),
 139	  mTimeStepCount(0),
 140	  mLastInterp(0.f),
 141	  mIsSelf(FALSE)
 142{
 143}
 144
 145
 146//-----------------------------------------------------------------------------
 147// ~LLMotionController()
 148// Class Destructor
 149//-----------------------------------------------------------------------------
 150LLMotionController::~LLMotionController()
 151{
 152	deleteAllMotions();
 153}
 154
 155void LLMotionController::incMotionCounts(S32& num_motions, S32& num_loading_motions, S32& num_loaded_motions, S32& num_active_motions, S32& num_deprecated_motions)
 156{
 157	num_motions += mAllMotions.size();
 158	num_loading_motions += mLoadingMotions.size();
 159	num_loaded_motions += mLoadedMotions.size();
 160	num_active_motions += mActiveMotions.size();
 161	num_deprecated_motions += mDeprecatedMotions.size();
 162}
 163
 164//-----------------------------------------------------------------------------
 165// deleteAllMotions()
 166//-----------------------------------------------------------------------------
 167void LLMotionController::deleteAllMotions()
 168{
 169	mLoadingMotions.clear();
 170	mLoadedMotions.clear();
 171	mActiveMotions.clear();
 172
 173	for_each(mAllMotions.begin(), mAllMotions.end(), DeletePairedPointer());
 174	mAllMotions.clear();
 175}
 176
 177//-----------------------------------------------------------------------------
 178// purgeExcessMotion()
 179//-----------------------------------------------------------------------------
 180void LLMotionController::purgeExcessMotions()
 181{
 182	if (mLoadedMotions.size() > MAX_MOTION_INSTANCES)
 183	{
 184		// clean up deprecated motions
 185		for (motion_set_t::iterator deprecated_motion_it = mDeprecatedMotions.begin(); 
 186			 deprecated_motion_it != mDeprecatedMotions.end(); )
 187		{
 188			motion_set_t::iterator cur_iter = deprecated_motion_it++;
 189			LLMotion* cur_motionp = *cur_iter;
 190			if (!isMotionActive(cur_motionp))
 191			{
 192				// Motion is deprecated so we know it's not cannonical,
 193				//  we can safely remove the instance
 194				removeMotionInstance(cur_motionp); // modifies mDeprecatedMotions
 195				mDeprecatedMotions.erase(cur_iter);
 196			}
 197		}
 198	}
 199
 200	std::set<LLUUID> motions_to_kill;
 201	if (mLoadedMotions.size() > MAX_MOTION_INSTANCES)
 202	{
 203		// too many motions active this frame, kill all blenders
 204		mPoseBlender.clearBlenders();
 205		for (motion_set_t::iterator loaded_motion_it = mLoadedMotions.begin(); 
 206			 loaded_motion_it != mLoadedMotions.end(); 
 207			 ++loaded_motion_it)
 208		{
 209			LLMotion* cur_motionp = *loaded_motion_it;
 210			// motion isn't playing, delete it
 211			if (!isMotionActive(cur_motionp))
 212			{
 213				motions_to_kill.insert(cur_motionp->getID());
 214			}
 215		}
 216	}
 217	
 218	// clean up all inactive, loaded motions
 219	for (std::set<LLUUID>::iterator motion_it = motions_to_kill.begin();
 220		motion_it != motions_to_kill.end();
 221		++motion_it)
 222	{
 223		// look up the motion again by ID to get canonical instance
 224		// and kill it only if that one is inactive
 225		LLUUID motion_id = *motion_it;
 226		LLMotion* motionp = findMotion(motion_id);
 227		if (motionp && !isMotionActive(motionp))
 228		{
 229			removeMotion(motion_id);
 230		}
 231	}
 232
 233	if (mLoadedMotions.size() > 2*MAX_MOTION_INSTANCES)
 234	{
 235		LL_WARNS_ONCE("Animation") << "> " << 2*MAX_MOTION_INSTANCES << " Loaded Motions" << llendl;
 236	}
 237}
 238
 239//-----------------------------------------------------------------------------
 240// deactivateStoppedMotions()
 241//-----------------------------------------------------------------------------
 242void LLMotionController::deactivateStoppedMotions()
 243{
 244	// Since we're hidden, deactivate any stopped motions.
 245	for (motion_list_t::iterator iter = mActiveMotions.begin();
 246		 iter != mActiveMotions.end(); )
 247	{
 248		motion_list_t::iterator curiter = iter++;
 249		LLMotion* motionp = *curiter;
 250		if (motionp->isStopped())
 251		{
 252			deactivateMotionInstance(motionp);
 253		}
 254	}
 255}
 256
 257//-----------------------------------------------------------------------------
 258// setTimeStep()
 259//-----------------------------------------------------------------------------
 260void LLMotionController::setTimeStep(F32 step)
 261{
 262	mTimeStep = step;
 263
 264	if (step != 0.f)
 265	{
 266		// make sure timestamps conform to new quantum
 267		for (motion_list_t::iterator iter = mActiveMotions.begin();
 268			 iter != mActiveMotions.end(); ++iter)
 269		{
 270			LLMotion* motionp = *iter;
 271			F32 activation_time = motionp->mActivationTimestamp;
 272			motionp->mActivationTimestamp = (F32)(llfloor(activation_time / step)) * step;
 273			BOOL stopped = motionp->isStopped();
 274			motionp->setStopTime((F32)(llfloor(motionp->getStopTime() / step)) * step);
 275			motionp->setStopped(stopped);
 276			motionp->mSendStopTimestamp = (F32)llfloor(motionp->mSendStopTimestamp / step) * step;
 277		}
 278	}
 279}
 280
 281//-----------------------------------------------------------------------------
 282// setTimeFactor()
 283//-----------------------------------------------------------------------------
 284void LLMotionController::setTimeFactor(F32 time_factor)
 285{ 
 286	mTimeFactor = time_factor; 
 287}
 288
 289//-----------------------------------------------------------------------------
 290// setCharacter()
 291//-----------------------------------------------------------------------------
 292void LLMotionController::setCharacter(LLCharacter *character)
 293{
 294	mCharacter = character;
 295}
 296
 297
 298//-----------------------------------------------------------------------------
 299// registerMotion()
 300//-----------------------------------------------------------------------------
 301BOOL LLMotionController::registerMotion( const LLUUID& id, LLMotionConstructor constructor )
 302{
 303	return sRegistry.registerMotion(id, constructor);
 304}
 305
 306//-----------------------------------------------------------------------------
 307// removeMotion()
 308//-----------------------------------------------------------------------------
 309void LLMotionController::removeMotion( const LLUUID& id)
 310{
 311	LLMotion* motionp = findMotion(id);
 312	mAllMotions.erase(id);
 313	removeMotionInstance(motionp);
 314}
 315
 316// removes instance of a motion from all runtime structures, but does
 317// not erase entry by ID, as this could be a duplicate instance
 318// use removeMotion(id) to remove all references to a given motion by id.
 319void LLMotionController::removeMotionInstance(LLMotion* motionp)
 320{
 321	if (motionp)
 322	{
 323		llassert(findMotion(motionp->getID()) != motionp);
 324		if (motionp->isActive())
 325			motionp->deactivate();
 326		mLoadingMotions.erase(motionp);
 327		mLoadedMotions.erase(motionp);
 328		mActiveMotions.remove(motionp);
 329		delete motionp;
 330	}
 331}
 332
 333//-----------------------------------------------------------------------------
 334// createMotion()
 335//-----------------------------------------------------------------------------
 336LLMotion* LLMotionController::createMotion( const LLUUID &id )
 337{
 338	LLMemType mt(LLMemType::MTYPE_ANIMATION);
 339	// do we have an instance of this motion for this character?
 340	LLMotion *motion = findMotion(id);
 341
 342	// if not, we need to create one
 343	if (!motion)
 344	{
 345		// look up constructor and create it
 346		motion = sRegistry.createMotion(id);
 347		if (!motion)
 348		{
 349			return NULL;
 350		}
 351
 352		// look up name for default motions
 353		const char* motion_name = gAnimLibrary.animStateToString(id);
 354		if (motion_name)
 355		{
 356			motion->setName(motion_name);
 357		}
 358
 359		// initialize the new instance
 360		LLMotion::LLMotionInitStatus stat = motion->onInitialize(mCharacter);
 361		switch(stat)
 362		{
 363		case LLMotion::STATUS_FAILURE:
 364			llinfos << "Motion " << id << " init failed." << llendl;
 365			sRegistry.markBad(id);
 366			delete motion;
 367			return NULL;
 368		case LLMotion::STATUS_HOLD:
 369			mLoadingMotions.insert(motion);
 370			break;
 371		case LLMotion::STATUS_SUCCESS:
 372		    // add motion to our list
 373		    mLoadedMotions.insert(motion);
 374			break;
 375		default:
 376			llerrs << "Invalid initialization status" << llendl;
 377			break;
 378		}
 379
 380		mAllMotions[id] = motion;
 381	}
 382	return motion;
 383}
 384
 385//-----------------------------------------------------------------------------
 386// startMotion()
 387//-----------------------------------------------------------------------------
 388BOOL LLMotionController::startMotion(const LLUUID &id, F32 start_offset)
 389{
 390	// do we have an instance of this motion for this character?
 391	LLMotion *motion = findMotion(id);
 392
 393	// motion that is stopping will be allowed to stop but
 394	// replaced by a new instance of that motion
 395	if (motion
 396		&& !mPaused
 397		&& motion->canDeprecate()
 398		&& motion->getFadeWeight() > 0.01f // not LOD-ed out
 399		&& (motion->isBlending() || motion->getStopTime() != 0.f))
 400	{
 401		deprecateMotionInstance(motion);
 402		// force creation of new instance
 403		motion = NULL;
 404	}
 405
 406	// create new motion instance
 407	if (!motion)
 408	{
 409		motion = createMotion(id);
 410	}
 411
 412	if (!motion)
 413	{
 414		return FALSE;
 415	}
 416	//if the motion is already active and allows deprecation, then let it keep playing
 417	else if (motion->canDeprecate() && isMotionActive(motion))
 418	{	
 419		return TRUE;
 420	}
 421
 422//	llinfos << "Starting motion " << name << llendl;
 423	return activateMotionInstance(motion, mAnimTime - start_offset);
 424}
 425
 426
 427//-----------------------------------------------------------------------------
 428// stopMotionLocally()
 429//-----------------------------------------------------------------------------
 430BOOL LLMotionController::stopMotionLocally(const LLUUID &id, BOOL stop_immediate)
 431{
 432	// if already inactive, return false
 433	LLMotion *motion = findMotion(id);
 434	return stopMotionInstance(motion, stop_immediate);
 435}
 436
 437BOOL LLMotionController::stopMotionInstance(LLMotion* motion, BOOL stop_immediate)
 438{
 439	if (!motion)
 440	{
 441		return FALSE;
 442	}
 443
 444	
 445	// If on active list, stop it
 446	if (isMotionActive(motion) && !motion->isStopped())
 447	{
 448		motion->setStopTime(mAnimTime);
 449		if (stop_immediate)
 450		{
 451			deactivateMotionInstance(motion);
 452		}
 453		return TRUE;
 454	}
 455	else if (isMotionLoading(motion))
 456	{
 457		motion->setStopped(TRUE);
 458		return TRUE;
 459	}
 460
 461	return FALSE;
 462}
 463
 464//-----------------------------------------------------------------------------
 465// updateRegularMotions()
 466//-----------------------------------------------------------------------------
 467void LLMotionController::updateRegularMotions()
 468{
 469	updateMotionsByType(LLMotion::NORMAL_BLEND);
 470}
 471
 472//-----------------------------------------------------------------------------
 473// updateAdditiveMotions()
 474//-----------------------------------------------------------------------------
 475void LLMotionController::updateAdditiveMotions()
 476{
 477	updateMotionsByType(LLMotion::ADDITIVE_BLEND);
 478}
 479
 480//-----------------------------------------------------------------------------
 481// resetJointSignatures()
 482//-----------------------------------------------------------------------------
 483void LLMotionController::resetJointSignatures()
 484{
 485	memset(&mJointSignature[0][0], 0, sizeof(U8) * LL_CHARACTER_MAX_JOINTS);
 486	memset(&mJointSignature[1][0], 0, sizeof(U8) * LL_CHARACTER_MAX_JOINTS);
 487}
 488
 489//-----------------------------------------------------------------------------
 490// updateIdleMotion()
 491// minimal updates for active motions
 492//-----------------------------------------------------------------------------
 493void LLMotionController::updateIdleMotion(LLMotion* motionp)
 494{
 495	if (motionp->isStopped() && mAnimTime > motionp->getStopTime() + motionp->getEaseOutDuration())
 496	{
 497		deactivateMotionInstance(motionp);
 498	}
 499	else if (motionp->isStopped() && mAnimTime > motionp->getStopTime())
 500	{
 501		// is this the first iteration in the ease out phase?
 502		if (mLastTime <= motionp->getStopTime())
 503		{
 504			// store residual weight for this motion
 505			motionp->mResidualWeight = motionp->getPose()->getWeight();
 506		}
 507	}
 508	else if (mAnimTime > motionp->mSendStopTimestamp)
 509	{
 510		// notify character of timed stop event on first iteration past sendstoptimestamp
 511		// this will only be called when an animation stops itself (runs out of time)
 512		if (mLastTime <= motionp->mSendStopTimestamp)
 513		{
 514			mCharacter->requestStopMotion( motionp );
 515			stopMotionInstance(motionp, FALSE);
 516		}
 517	}
 518	else if (mAnimTime >= motionp->mActivationTimestamp)
 519	{
 520		if (mLastTime < motionp->mActivationTimestamp)
 521		{
 522			motionp->mResidualWeight = motionp->getPose()->getWeight();
 523		}
 524	}
 525}
 526
 527//-----------------------------------------------------------------------------
 528// updateIdleActiveMotions()
 529// Call this instead of updateMotionsByType for hidden avatars
 530//-----------------------------------------------------------------------------
 531void LLMotionController::updateIdleActiveMotions()
 532{
 533	for (motion_list_t::iterator iter = mActiveMotions.begin();
 534		 iter != mActiveMotions.end(); )
 535	{
 536		motion_list_t::iterator curiter = iter++;
 537		LLMotion* motionp = *curiter;
 538		updateIdleMotion(motionp);
 539	}
 540}
 541
 542//-----------------------------------------------------------------------------
 543// updateMotionsByType()
 544//-----------------------------------------------------------------------------
 545void LLMotionController::updateMotionsByType(LLMotion::LLMotionBlendType anim_type)
 546{
 547	BOOL update_result = TRUE;
 548	U8 last_joint_signature[LL_CHARACTER_MAX_JOINTS];
 549
 550	memset(&last_joint_signature, 0, sizeof(U8) * LL_CHARACTER_MAX_JOINTS);
 551
 552	// iterate through active motions in chronological order
 553	for (motion_list_t::iterator iter = mActiveMotions.begin();
 554		 iter != mActiveMotions.end(); )
 555	{
 556		motion_list_t::iterator curiter = iter++;
 557		LLMotion* motionp = *curiter;
 558		if (motionp->getBlendType() != anim_type)
 559		{
 560			continue;
 561		}
 562
 563		BOOL update_motion = FALSE;
 564
 565		if (motionp->getPose()->getWeight() < 1.f)
 566		{
 567			update_motion = TRUE;
 568		}
 569		else
 570		{
 571			// NUM_JOINT_SIGNATURE_STRIDES should be multiple of 4
 572			for (S32 i = 0; i < NUM_JOINT_SIGNATURE_STRIDES; i++)
 573			{
 574		 		U32 *current_signature = (U32*)&(mJointSignature[0][i * 4]);
 575				U32 test_signature = *(U32*)&(motionp->mJointSignature[0][i * 4]);
 576				
 577				if ((*current_signature | test_signature) > (*current_signature))
 578				{
 579					*current_signature |= test_signature;
 580					update_motion = TRUE;
 581				}
 582
 583				*((U32*)&last_joint_signature[i * 4]) = *(U32*)&(mJointSignature[1][i * 4]);
 584				current_signature = (U32*)&(mJointSignature[1][i * 4]);
 585				test_signature = *(U32*)&(motionp->mJointSignature[1][i * 4]);
 586
 587				if ((*current_signature | test_signature) > (*current_signature))
 588				{
 589					*current_signature |= test_signature;
 590					update_motion = TRUE;
 591				}
 592			}
 593		}
 594
 595		if (!update_motion)
 596		{
 597			updateIdleMotion(motionp);
 598			continue;
 599		}
 600
 601		LLPose *posep = motionp->getPose();
 602
 603		// only filter by LOD after running every animation at least once (to prime the avatar state)
 604		if (mHasRunOnce && motionp->getMinPixelArea() > mCharacter->getPixelArea())
 605		{
 606			motionp->fadeOut();
 607
 608			//should we notify the simulator that this motion should be stopped (check even if skipped by LOD logic)
 609			if (mAnimTime > motionp->mSendStopTimestamp)
 610			{
 611				// notify character of timed stop event on first iteration past sendstoptimestamp
 612				// this will only be called when an animation stops itself (runs out of time)
 613				if (mLastTime <= motionp->mSendStopTimestamp)
 614				{
 615					mCharacter->requestStopMotion( motionp );
 616					stopMotionInstance(motionp, FALSE);
 617				}
 618			}
 619
 620			if (motionp->getFadeWeight() < 0.01f)
 621			{
 622				if (motionp->isStopped() && mAnimTime > motionp->getStopTime() + motionp->getEaseOutDuration())
 623				{
 624					posep->setWeight(0.f);
 625					deactivateMotionInstance(motionp);
 626				}
 627				continue;
 628			}
 629		}
 630		else
 631		{
 632			motionp->fadeIn();
 633		}
 634
 635		//**********************
 636		// MOTION INACTIVE
 637		//**********************
 638		if (motionp->isStopped() && mAnimTime > motionp->getStopTime() + motionp->getEaseOutDuration())
 639		{
 640			// this motion has gone on too long, deactivate it
 641			// did we have a chance to stop it?
 642			if (mLastTime <= motionp->getStopTime())
 643			{
 644				// if not, let's stop it this time through and deactivate it the next
 645
 646				posep->setWeight(motionp->getFadeWeight());
 647				motionp->onUpdate(motionp->getStopTime() - motionp->mActivationTimestamp, last_joint_signature);
 648			}
 649			else
 650			{
 651				posep->setWeight(0.f);
 652				deactivateMotionInstance(motionp);
 653				continue;
 654			}
 655		}
 656
 657		//**********************
 658		// MOTION EASE OUT
 659		//**********************
 660		else if (motionp->isStopped() && mAnimTime > motionp->getStopTime())
 661		{
 662			// is this the first iteration in the ease out phase?
 663			if (mLastTime <= motionp->getStopTime())
 664			{
 665				// store residual weight for this motion
 666				motionp->mResidualWeight = motionp->getPose()->getWeight();
 667			}
 668
 669			if (motionp->getEaseOutDuration() == 0.f)
 670			{
 671				posep->setWeight(0.f);
 672			}
 673			else
 674			{
 675				posep->setWeight(motionp->getFadeWeight() * motionp->mResidualWeight * cubic_step(1.f - ((mAnimTime - motionp->getStopTime()) / motionp->getEaseOutDuration())));
 676			}
 677
 678			// perform motion update
 679			update_result = motionp->onUpdate(mAnimTime - motionp->mActivationTimestamp, last_joint_signature);
 680		}
 681
 682		//**********************
 683		// MOTION ACTIVE
 684		//**********************
 685		else if (mAnimTime > motionp->mActivationTimestamp + motionp->getEaseInDuration())
 686		{
 687			posep->setWeight(motionp->getFadeWeight());
 688
 689			//should we notify the simulator that this motion should be stopped?
 690			if (mAnimTime > motionp->mSendStopTimestamp)
 691			{
 692				// notify character of timed stop event on first iteration past sendstoptimestamp
 693				// this will only be called when an animation stops itself (runs out of time)
 694				if (mLastTime <= motionp->mSendStopTimestamp)
 695				{
 696					mCharacter->requestStopMotion( motionp );
 697					stopMotionInstance(motionp, FALSE);
 698				}
 699			}
 700
 701			// perform motion update
 702			update_result = motionp->onUpdate(mAnimTime - motionp->mActivationTimestamp, last_joint_signature);
 703		}
 704
 705		//**********************
 706		// MOTION EASE IN
 707		//**********************
 708		else if (mAnimTime >= motionp->mActivationTimestamp)
 709		{
 710			if (mLastTime < motionp->mActivationTimestamp)
 711			{
 712				motionp->mResidualWeight = motionp->getPose()->getWeight();
 713			}
 714			if (motionp->getEaseInDuration() == 0.f)
 715			{
 716				posep->setWeight(motionp->getFadeWeight());
 717			}
 718			else
 719			{
 720				// perform motion update
 721				posep->setWeight(motionp->getFadeWeight() * motionp->mResidualWeight + (1.f - motionp->mResidualWeight) * cubic_step((mAnimTime - motionp->mActivationTimestamp) / motionp->getEaseInDuration()));
 722			}
 723			// perform motion update
 724			update_result = motionp->onUpdate(mAnimTime - motionp->mActivationTimestamp, last_joint_signature);
 725		}
 726		else
 727		{
 728			posep->setWeight(0.f);
 729			update_result = motionp->onUpdate(0.f, last_joint_signature);
 730		}
 731		
 732		// allow motions to deactivate themselves 
 733		if (!update_result)
 734		{
 735			if (!motionp->isStopped() || motionp->getStopTime() > mAnimTime)
 736			{
 737				// animation has stopped itself due to internal logic
 738				// propagate this to the network
 739				// as not all viewers are guaranteed to have access to the same logic
 740				mCharacter->requestStopMotion( motionp );
 741				stopMotionInstance(motionp, FALSE);
 742			}
 743
 744		}
 745
 746		// even if onupdate returns FALSE, add this motion in to the blend one last time
 747		mPoseBlender.addMotion(motionp);
 748	}
 749}
 750
 751//-----------------------------------------------------------------------------
 752// updateLoadingMotions()
 753//-----------------------------------------------------------------------------
 754void LLMotionController::updateLoadingMotions()
 755{
 756	// query pending motions for completion
 757	for (motion_set_t::iterator iter = mLoadingMotions.begin();
 758		 iter != mLoadingMotions.end(); )
 759	{
 760		motion_set_t::iterator curiter = iter++;
 761		LLMotion* motionp = *curiter;
 762		if( !motionp)
 763		{
 764			continue; // maybe shouldn't happen but i've seen it -MG
 765		}
 766		LLMotion::LLMotionInitStatus status = motionp->onInitialize(mCharacter);
 767		if (status == LLMotion::STATUS_SUCCESS)
 768		{
 769			mLoadingMotions.erase(curiter);
 770			// add motion to our loaded motion list
 771			mLoadedMotions.insert(motionp);
 772			// this motion should be playing
 773			if (!motionp->isStopped())
 774			{
 775				activateMotionInstance(motionp, mAnimTime);
 776			}
 777		}
 778		else if (status == LLMotion::STATUS_FAILURE)
 779		{
 780			llinfos << "Motion " << motionp->getID() << " init failed." << llendl;
 781			sRegistry.markBad(motionp->getID());
 782			mLoadingMotions.erase(curiter);
 783			motion_set_t::iterator found_it = mDeprecatedMotions.find(motionp);
 784			if (found_it != mDeprecatedMotions.end())
 785			{
 786				mDeprecatedMotions.erase(found_it);
 787			}
 788			mAllMotions.erase(motionp->getID());
 789			delete motionp;
 790		}
 791	}
 792}
 793
 794//-----------------------------------------------------------------------------
 795// call updateMotion() or updateMotionsMinimal() every frame
 796//-----------------------------------------------------------------------------
 797
 798//-----------------------------------------------------------------------------
 799// updateMotion()
 800//-----------------------------------------------------------------------------
 801void LLMotionController::updateMotions(bool force_update)
 802{
 803	BOOL use_quantum = (mTimeStep != 0.f);
 804
 805	// Always update mPrevTimerElapsed
 806	F32 cur_time = mTimer.getElapsedTimeF32();
 807	F32 delta_time = cur_time - mPrevTimerElapsed;
 808	mPrevTimerElapsed = cur_time;
 809	mLastTime = mAnimTime;
 810
 811	// Always cap the number of loaded motions
 812	purgeExcessMotions();
 813	
 814	// Update timing info for this time step.
 815	if (!mPaused)
 816	{
 817		F32 update_time = mAnimTime + delta_time * mTimeFactor;
 818		if (use_quantum)
 819		{
 820			F32 time_interval = fmodf(update_time, mTimeStep);
 821
 822			// always animate *ahead* of actual time
 823			S32 quantum_count = llmax(0, llfloor((update_time - time_interval) / mTimeStep)) + 1;
 824			if (quantum_count == mTimeStepCount)
 825			{
 826				// we're still in same time quantum as before, so just interpolate and exit
 827				if (!mPaused)
 828				{
 829					F32 interp = time_interval / mTimeStep;
 830					mPoseBlender.interpolate(interp - mLastInterp);
 831					mLastInterp = interp;
 832				}
 833
 834				updateLoadingMotions();
 835				return;
 836			}
 837			
 838			// is calculating a new keyframe pose, make sure the last one gets applied
 839			mPoseBlender.interpolate(1.f);
 840			clearBlenders();
 841
 842			mTimeStepCount = quantum_count;
 843			mAnimTime = (F32)quantum_count * mTimeStep;
 844			mLastInterp = 0.f;
 845		}
 846		else
 847		{
 848			mAnimTime = update_time;
 849		}
 850	}
 851
 852	updateLoadingMotions();
 853
 854	resetJointSignatures();
 855
 856	if (mPaused && !force_update)
 857	{
 858		updateIdleActiveMotions();
 859	}
 860	else
 861	{
 862		// update additive motions
 863		updateAdditiveMotions();
 864		resetJointSignatures();
 865
 866		// update all regular motions
 867		updateRegularMotions();
 868
 869		if (use_quantum)
 870		{
 871			mPoseBlender.blendAndCache(TRUE);
 872		}
 873		else
 874		{
 875			mPoseBlender.blendAndApply();
 876		}
 877	}
 878
 879	mHasRunOnce = TRUE;
 880//	llinfos << "Motion controller time " << motionTimer.getElapsedTimeF32() << llendl;
 881}
 882
 883//-----------------------------------------------------------------------------
 884// updateMotionsMinimal()
 885// minimal update (e.g. while hidden)
 886//-----------------------------------------------------------------------------
 887void LLMotionController::updateMotionsMinimal()
 888{
 889	// Always update mPrevTimerElapsed
 890	mPrevTimerElapsed = mTimer.getElapsedTimeF32();
 891
 892	purgeExcessMotions();
 893	updateLoadingMotions();
 894	resetJointSignatures();
 895
 896	deactivateStoppedMotions();
 897
 898	mHasRunOnce = TRUE;
 899}
 900
 901//-----------------------------------------------------------------------------
 902// activateMotionInstance()
 903//-----------------------------------------------------------------------------
 904BOOL LLMotionController::activateMotionInstance(LLMotion *motion, F32 time)
 905{
 906	// It's not clear why the getWeight() line seems to be crashing this, but
 907	// hopefully this fixes it.
 908	if (motion == NULL || motion->getPose() == NULL)
 909	{
 910		return FALSE;	
 911	}
 912
 913	if (mLoadingMotions.find(motion) != mLoadingMotions.end())
 914	{
 915		// we want to start this motion, but we can't yet, so flag it as started
 916		motion->setStopped(FALSE);
 917		// report pending animations as activated
 918		return TRUE;
 919	}
 920
 921	motion->mResidualWeight = motion->getPose()->getWeight();
 922
 923	// set stop time based on given duration and ease out time
 924	if (motion->getDuration() != 0.f && !motion->getLoop())
 925	{
 926		F32 ease_out_time;
 927		F32 motion_duration;
 928
 929		// should we stop at the end of motion duration, or a bit earlier 
 930		// to allow it to ease out while moving?
 931		ease_out_time = motion->getEaseOutDuration();
 932
 933		// is the clock running when the motion is easing in?
 934		// if not (POSTURE_EASE) then we need to wait that much longer before triggering the stop
 935		motion_duration = llmax(motion->getDuration() - ease_out_time, 0.f);
 936		motion->mSendStopTimestamp = time + motion_duration;
 937	} 
 938	else
 939	{
 940		motion->mSendStopTimestamp = F32_MAX;
 941	}
 942	
 943	if (motion->isActive())
 944	{
 945		mActiveMotions.remove(motion);
 946	}
 947	mActiveMotions.push_front(motion);
 948
 949	motion->activate(time);
 950	motion->onUpdate(0.f, mJointSignature[1]);
 951
 952	if (mAnimTime >= motion->mSendStopTimestamp)
 953	{
 954		motion->setStopTime(motion->mSendStopTimestamp);
 955		if (motion->mResidualWeight == 0.0f)
 956		{
 957			// bit of a hack; if newly activating a motion while easing out, weight should = 1
 958			motion->mResidualWeight = 1.f;
 959		}
 960	}
 961	
 962	return TRUE;
 963}
 964
 965//-----------------------------------------------------------------------------
 966// deactivateMotionInstance()
 967//-----------------------------------------------------------------------------
 968BOOL LLMotionController::deactivateMotionInstance(LLMotion *motion)
 969{
 970	motion->deactivate();
 971
 972	motion_set_t::iterator found_it = mDeprecatedMotions.find(motion);
 973	if (found_it != mDeprecatedMotions.end())
 974	{
 975		// deprecated motions need to be completely excised
 976		removeMotionInstance(motion);	
 977		mDeprecatedMotions.erase(found_it);
 978	}
 979	else
 980	{
 981		// for motions that we are keeping, simply remove from active queue
 982		mActiveMotions.remove(motion);
 983	}
 984
 985	return TRUE;
 986}
 987
 988void LLMotionController::deprecateMotionInstance(LLMotion* motion)
 989{
 990	mDeprecatedMotions.insert(motion);
 991
 992	//fade out deprecated motion
 993	stopMotionInstance(motion, FALSE);
 994	//no longer canonical
 995	mAllMotions.erase(motion->getID());
 996}
 997
 998//-----------------------------------------------------------------------------
 999// isMotionActive()
1000//-----------------------------------------------------------------------------
1001bool LLMotionController::isMotionActive(LLMotion *motion)
1002{
1003	return (motion && motion->isActive());
1004}
1005
1006//-----------------------------------------------------------------------------
1007// isMotionLoading()
1008//-----------------------------------------------------------------------------
1009bool LLMotionController::isMotionLoading(LLMotion* motion)
1010{
1011	return (mLoadingMotions.find(motion) != mLoadingMotions.end());
1012}
1013
1014
1015//-----------------------------------------------------------------------------
1016// findMotion()
1017//-----------------------------------------------------------------------------
1018LLMotion* LLMotionController::findMotion(const LLUUID& id) const
1019{
1020	motion_map_t::const_iterator iter = mAllMotions.find(id);
1021	if(iter == mAllMotions.end())
1022	{
1023		return NULL;
1024	}
1025	else
1026	{
1027		return iter->second;
1028	}
1029}
1030
1031//-----------------------------------------------------------------------------
1032// dumpMotions()
1033//-----------------------------------------------------------------------------
1034void LLMotionController::dumpMotions()
1035{
1036	llinfos << "=====================================" << llendl;
1037	for (motion_map_t::iterator iter = mAllMotions.begin();
1038		 iter != mAllMotions.end(); iter++)
1039	{
1040		LLUUID id = iter->first;
1041		std::string state_string;
1042		LLMotion *motion = iter->second;
1043		if (mLoadingMotions.find(motion) != mLoadingMotions.end())
1044			state_string += std::string("l");
1045		if (mLoadedMotions.find(motion) != mLoadedMotions.end())
1046			state_string += std::string("L");
1047		if (std::find(mActiveMotions.begin(), mActiveMotions.end(), motion)!=mActiveMotions.end())
1048			state_string += std::string("A");
1049		if (mDeprecatedMotions.find(motion) != mDeprecatedMotions.end())
1050			state_string += std::string("D");
1051		llinfos << gAnimLibrary.animationName(id) << " " << state_string << llendl;
1052		
1053	}
1054}
1055
1056//-----------------------------------------------------------------------------
1057// deactivateAllMotions()
1058//-----------------------------------------------------------------------------
1059void LLMotionController::deactivateAllMotions()
1060{
1061	for (motion_map_t::iterator iter = mAllMotions.begin();
1062		 iter != mAllMotions.end(); iter++)
1063	{
1064		LLMotion* motionp = iter->second;
1065		deactivateMotionInstance(motionp);
1066	}
1067}
1068
1069
1070//-----------------------------------------------------------------------------
1071// flushAllMotions()
1072//-----------------------------------------------------------------------------
1073void LLMotionController::flushAllMotions()
1074{
1075	std::vector<std::pair<LLUUID,F32> > active_motions;
1076	active_motions.reserve(mActiveMotions.size());
1077	for (motion_list_t::iterator iter = mActiveMotions.begin();
1078		 iter != mActiveMotions.end(); )
1079	{
1080		motion_list_t::iterator curiter = iter++;
1081		LLMotion* motionp = *curiter;
1082		F32 dtime = mAnimTime - motionp->mActivationTimestamp;
1083		active_motions.push_back(std::make_pair(motionp->getID(),dtime));
1084		motionp->deactivate(); // don't call deactivateMotionInstance() because we are going to reactivate it
1085	}
1086 	mActiveMotions.clear();
1087	
1088	// delete all motion instances
1089	deleteAllMotions();
1090
1091	// kill current hand pose that was previously called out by
1092	// keyframe motion
1093	mCharacter->removeAnimationData("Hand Pose");
1094
1095	// restart motions
1096	for (std::vector<std::pair<LLUUID,F32> >::iterator iter = active_motions.begin();
1097		 iter != active_motions.end(); ++iter)
1098	{
1099		startMotion(iter->first, iter->second);
1100	}
1101}
1102
1103//-----------------------------------------------------------------------------
1104// pause()
1105//-----------------------------------------------------------------------------
1106void LLMotionController::pauseAllMotions()
1107{
1108	if (!mPaused)
1109	{
1110		//llinfos << "Pausing animations..." << llendl;
1111		mPaused = TRUE;
1112	}
1113	
1114}
1115
1116//-----------------------------------------------------------------------------
1117// unpause()
1118//-----------------------------------------------------------------------------
1119void LLMotionController::unpauseAllMotions()
1120{
1121	if (mPaused)
1122	{
1123		//llinfos << "Unpausing animations..." << llendl;
1124		mPaused = FALSE;
1125	}
1126}
1127// End