Skip to content
Snippets Groups Projects
Commit e1ca8ff5 authored by Rye Mutt's avatar Rye Mutt :bread:
Browse files

Remove dead code

parent d524b38a
No related branches found
No related tags found
No related merge requests found
......@@ -33,14 +33,12 @@ set(llcharacter_SOURCE_FILES
lljointsolverrp3.cpp
llkeyframefallmotion.cpp
llkeyframemotion.cpp
llkeyframemotionparam.cpp
llkeyframestandmotion.cpp
llkeyframewalkmotion.cpp
llmotioncontroller.cpp
llmotion.cpp
llmultigesture.cpp
llpose.cpp
llstatemachine.cpp
lltargetingmotion.cpp
llvisualparam.cpp
)
......@@ -61,14 +59,12 @@ set(llcharacter_HEADER_FILES
lljointstate.h
llkeyframefallmotion.h
llkeyframemotion.h
llkeyframemotionparam.h
llkeyframestandmotion.h
llkeyframewalkmotion.h
llmotion.h
llmotioncontroller.h
llmultigesture.h
llpose.h
llstatemachine.h
lltargetingmotion.h
llvisualparam.h
)
......
/**
* @file llkeyframemotionparam.cpp
* @brief Implementation of LLKeyframeMotion class.
*
* $LicenseInfo:firstyear=2001&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2010, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
//-----------------------------------------------------------------------------
// Header Files
//-----------------------------------------------------------------------------
#include "linden_common.h"
#include "llkeyframemotionparam.h"
#include "llcharacter.h"
#include "llmath.h"
#include "m3math.h"
#include "lldir.h"
#include "llanimationstates.h"
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// LLKeyframeMotionParam class
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// LLKeyframeMotionParam()
// Class Constructor
//-----------------------------------------------------------------------------
LLKeyframeMotionParam::LLKeyframeMotionParam( const LLUUID &id) : LLMotion(id)
{
mDefaultKeyframeMotion = NULL;
mCharacter = NULL;
mEaseInDuration = 0.f;
mEaseOutDuration = 0.f;
mDuration = 0.f;
mPriority = LLJoint::LOW_PRIORITY;
}
//-----------------------------------------------------------------------------
// ~LLKeyframeMotionParam()
// Class Destructor
//-----------------------------------------------------------------------------
LLKeyframeMotionParam::~LLKeyframeMotionParam()
{
for (motion_map_t::iterator iter = mParameterizedMotions.begin();
iter != mParameterizedMotions.end(); ++iter)
{
motion_list_t& motionList = iter->second;
for (motion_list_t::iterator iter2 = motionList.begin(); iter2 != motionList.end(); ++iter2)
{
const ParameterizedMotion& paramMotion = *iter2;
delete paramMotion.mMotion;
}
motionList.clear();
}
mParameterizedMotions.clear();
}
//-----------------------------------------------------------------------------
// LLKeyframeMotionParam::onInitialize(LLCharacter *character)
//-----------------------------------------------------------------------------
LLMotion::LLMotionInitStatus LLKeyframeMotionParam::onInitialize(LLCharacter *character)
{
mCharacter = character;
if (!loadMotions())
{
return STATUS_FAILURE;
}
for (motion_map_t::iterator iter = mParameterizedMotions.begin();
iter != mParameterizedMotions.end(); ++iter)
{
motion_list_t& motionList = iter->second;
for (motion_list_t::iterator iter2 = motionList.begin(); iter2 != motionList.end(); ++iter2)
{
const ParameterizedMotion& paramMotion = *iter2;
LLMotion* motion = paramMotion.mMotion;
motion->onInitialize(character);
if (motion->getDuration() > mEaseInDuration)
{
mEaseInDuration = motion->getEaseInDuration();
}
if (motion->getEaseOutDuration() > mEaseOutDuration)
{
mEaseOutDuration = motion->getEaseOutDuration();
}
if (motion->getDuration() > mDuration)
{
mDuration = motion->getDuration();
}
if (motion->getPriority() > mPriority)
{
mPriority = motion->getPriority();
}
LLPose *pose = motion->getPose();
mPoseBlender.addMotion(motion);
for (LLJointState *jsp = pose->getFirstJointState(); jsp; jsp = pose->getNextJointState())
{
LLPose *blendedPose = mPoseBlender.getBlendedPose();
blendedPose->addJointState(jsp);
}
}
}
return STATUS_SUCCESS;
}
//-----------------------------------------------------------------------------
// LLKeyframeMotionParam::onActivate()
//-----------------------------------------------------------------------------
BOOL LLKeyframeMotionParam::onActivate()
{
for (motion_map_t::iterator iter = mParameterizedMotions.begin();
iter != mParameterizedMotions.end(); ++iter)
{
motion_list_t& motionList = iter->second;
for (motion_list_t::iterator iter2 = motionList.begin(); iter2 != motionList.end(); ++iter2)
{
const ParameterizedMotion& paramMotion = *iter2;
paramMotion.mMotion->activate(mActivationTimestamp);
}
}
return TRUE;
}
//-----------------------------------------------------------------------------
// LLKeyframeMotionParam::onUpdate()
//-----------------------------------------------------------------------------
BOOL LLKeyframeMotionParam::onUpdate(F32 time, U8* joint_mask)
{
F32 weightFactor = 1.f / (F32)mParameterizedMotions.size();
// zero out all pose weights
for (motion_map_t::iterator iter = mParameterizedMotions.begin();
iter != mParameterizedMotions.end(); ++iter)
{
motion_list_t& motionList = iter->second;
for (motion_list_t::iterator iter2 = motionList.begin(); iter2 != motionList.end(); ++iter2)
{
const ParameterizedMotion& paramMotion = *iter2;
// LL_INFOS() << "Weight for pose " << paramMotion.mMotion->getName() << " is " << paramMotion.mMotion->getPose()->getWeight() << LL_ENDL;
paramMotion.mMotion->getPose()->setWeight(0.f);
}
}
for (motion_map_t::iterator iter = mParameterizedMotions.begin();
iter != mParameterizedMotions.end(); ++iter)
{
const std::string& paramName = iter->first;
F32* paramValue = (F32 *)mCharacter->getAnimationData(paramName);
if (NULL == paramValue) // unexpected, but...
{
LL_WARNS() << "paramValue == NULL" << LL_ENDL;
continue;
}
// DANGER! Do not modify mParameterizedMotions while using these pointers!
const ParameterizedMotion* firstMotion = NULL;
const ParameterizedMotion* secondMotion = NULL;
motion_list_t& motionList = iter->second;
for (motion_list_t::iterator iter2 = motionList.begin(); iter2 != motionList.end(); ++iter2)
{
const ParameterizedMotion& paramMotion = *iter2;
paramMotion.mMotion->onUpdate(time, joint_mask);
F32 distToParam = paramMotion.mParam - *paramValue;
if ( distToParam <= 0.f)
{
// keep track of the motion closest to the parameter value
firstMotion = &paramMotion;
}
else
{
// we've passed the parameter value
// so store the first motion we find as the second one we want to blend...
if (firstMotion && !secondMotion )
{
secondMotion = &paramMotion;
}
//...or, if we've seen no other motion so far, make sure we blend to this only
else if (!firstMotion)
{
firstMotion = &paramMotion;
secondMotion = &paramMotion;
}
}
}
LLPose *firstPose;
LLPose *secondPose;
if (firstMotion)
firstPose = firstMotion->mMotion->getPose();
else
firstPose = NULL;
if (secondMotion)
secondPose = secondMotion->mMotion->getPose();
else
secondPose = NULL;
// now modify weight of the subanim (only if we are blending between two motions)
if (firstMotion && secondMotion)
{
if (firstMotion == secondMotion)
{
firstPose->setWeight(weightFactor);
}
else if (firstMotion->mParam == secondMotion->mParam)
{
firstPose->setWeight(0.5f * weightFactor);
secondPose->setWeight(0.5f * weightFactor);
}
else
{
F32 first_weight = 1.f -
((llclamp(*paramValue - firstMotion->mParam, 0.f, (secondMotion->mParam - firstMotion->mParam))) /
(secondMotion->mParam - firstMotion->mParam));
first_weight = llclamp(first_weight, 0.f, 1.f);
F32 second_weight = 1.f - first_weight;
firstPose->setWeight(first_weight * weightFactor);
secondPose->setWeight(second_weight * weightFactor);
// LL_INFOS() << "Parameter " << *paramName << ": " << *paramValue << LL_ENDL;
// LL_INFOS() << "Weights " << firstPose->getWeight() << " " << secondPose->getWeight() << LL_ENDL;
}
}
else if (firstMotion && !secondMotion)
{
firstPose->setWeight(weightFactor);
}
}
// blend poses
mPoseBlender.blendAndApply();
LL_INFOS() << "Param Motion weight " << mPoseBlender.getBlendedPose()->getWeight() << LL_ENDL;
return TRUE;
}
//-----------------------------------------------------------------------------
// LLKeyframeMotionParam::onDeactivate()
//-----------------------------------------------------------------------------
void LLKeyframeMotionParam::onDeactivate()
{
for (motion_map_t::iterator iter = mParameterizedMotions.begin();
iter != mParameterizedMotions.end(); ++iter)
{
motion_list_t& motionList = iter->second;
for (motion_list_t::iterator iter2 = motionList.begin(); iter2 != motionList.end(); ++iter2)
{
const ParameterizedMotion& paramMotion = *iter2;
paramMotion.mMotion->onDeactivate();
}
}
}
//-----------------------------------------------------------------------------
// LLKeyframeMotionParam::addKeyframeMotion()
//-----------------------------------------------------------------------------
BOOL LLKeyframeMotionParam::addKeyframeMotion(char *name, const LLUUID &id, char *param, F32 value)
{
LLMotion *newMotion = mCharacter->createMotion( id );
if (!newMotion)
{
return FALSE;
}
newMotion->setName(name);
// now add motion to this list
mParameterizedMotions[param].insert(ParameterizedMotion(newMotion, value));
return TRUE;
}
//-----------------------------------------------------------------------------
// LLKeyframeMotionParam::setDefaultKeyframeMotion()
//-----------------------------------------------------------------------------
void LLKeyframeMotionParam::setDefaultKeyframeMotion(char *name)
{
for (motion_map_t::iterator iter = mParameterizedMotions.begin();
iter != mParameterizedMotions.end(); ++iter)
{
motion_list_t& motionList = iter->second;
for (motion_list_t::iterator iter2 = motionList.begin(); iter2 != motionList.end(); ++iter2)
{
const ParameterizedMotion& paramMotion = *iter2;
if (paramMotion.mMotion->getName() == name)
{
mDefaultKeyframeMotion = paramMotion.mMotion;
}
}
}
}
//-----------------------------------------------------------------------------
// loadMotions()
//-----------------------------------------------------------------------------
BOOL LLKeyframeMotionParam::loadMotions()
{
//-------------------------------------------------------------------------
// Load named file by concatenating the character prefix with the motion name.
// Load data into a buffer to be parsed.
//-------------------------------------------------------------------------
//std::string path = gDirUtilp->getExpandedFilename(LL_PATH_MOTIONS,mCharacter->getAnimationPrefix())
// + "_" + getName() + ".llp";
//RN: deprecated unused reference to "motion" directory
std::string path;
//-------------------------------------------------------------------------
// open the file
//-------------------------------------------------------------------------
S32 fileSize = 0;
LLAPRFile infile ;
infile.open(path, LL_APR_R, NULL, &fileSize);
apr_file_t* fp = infile.getFileHandle() ;
if (!fp || fileSize == 0)
{
LL_INFOS() << "ERROR: can't open: " << path << LL_ENDL;
return FALSE;
}
// allocate a text buffer
std::vector<char> text(fileSize+1);
//-------------------------------------------------------------------------
// load data from file into buffer
//-------------------------------------------------------------------------
bool error = false;
char *p = &text[0];
while ( 1 )
{
if (apr_file_eof(fp) == APR_EOF)
{
break;
}
if (apr_file_gets(p, 1024, fp) != APR_SUCCESS)
{
error = true;
break;
}
while ( *(++p) )
;
}
//-------------------------------------------------------------------------
// close the file
//-------------------------------------------------------------------------
infile.close();
//-------------------------------------------------------------------------
// check for error
//-------------------------------------------------------------------------
llassert( p <= (&text[0] + fileSize) );
if ( error )
{
LL_INFOS() << "ERROR: error while reading from " << path << LL_ENDL;
return FALSE;
}
LL_INFOS() << "Loading parametric keyframe data for: " << getName() << LL_ENDL;
//-------------------------------------------------------------------------
// parse the text and build keyframe data structures
//-------------------------------------------------------------------------
p = &text[0];
S32 num;
char strA[80]; /* Flawfinder: ignore */
char strB[80]; /* Flawfinder: ignore */
F32 floatA = 0.0f;
//-------------------------------------------------------------------------
// get priority
//-------------------------------------------------------------------------
BOOL isFirstMotion = TRUE;
num = sscanf(p, "%79s %79s %f", strA, strB, &floatA); /* Flawfinder: ignore */
while(1)
{
if (num == 0 || num == EOF) break;
if ((num != 3))
{
LL_INFOS() << "WARNING: can't read parametric motion" << LL_ENDL;
return FALSE;
}
addKeyframeMotion(strA, gAnimLibrary.stringToAnimState(std::string(strA)), strB, floatA);
if (isFirstMotion)
{
isFirstMotion = FALSE;
setDefaultKeyframeMotion(strA);
}
p = strstr(p, "\n");
if (!p)
{
break;
}
p++;
num = sscanf(p, "%79s %79s %f", strA, strB, &floatA); /* Flawfinder: ignore */
}
return TRUE;
}
// End
/**
* @file llkeyframemotionparam.h
* @brief Implementation of LLKeframeMotionParam class.
*
* $LicenseInfo:firstyear=2002&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2010, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#ifndef LL_LLKEYFRAMEMOTIONPARAM_H
#define LL_LLKEYFRAMEMOTIONPARAM_H
//-----------------------------------------------------------------------------
// Header files
//-----------------------------------------------------------------------------
#include <string>
#include "llmotion.h"
#include "lljointstate.h"
#include "v3math.h"
#include "llquaternion.h"
#include "llkeyframemotion.h"
//-----------------------------------------------------------------------------
// class LLKeyframeMotionParam
//-----------------------------------------------------------------------------
class LLKeyframeMotionParam final :
public LLMotion
{
public:
// Constructor
LLKeyframeMotionParam(const LLUUID &id);
// Destructor
virtual ~LLKeyframeMotionParam();
public:
//-------------------------------------------------------------------------
// functions to support MotionController and MotionRegistry
//-------------------------------------------------------------------------
// static constructor
// all subclasses must implement such a function and register it
static LLMotion *create(const LLUUID &id) { return new LLKeyframeMotionParam(id); }
public:
//-------------------------------------------------------------------------
// animation callbacks to be implemented by subclasses
//-------------------------------------------------------------------------
// motions must specify whether or not they loop
virtual BOOL getLoop() {
return TRUE;
}
// motions must report their total duration
virtual F32 getDuration() {
return mDuration;
}
// motions must report their "ease in" duration
virtual F32 getEaseInDuration() {
return mEaseInDuration;
}
// motions must report their "ease out" duration.
virtual F32 getEaseOutDuration() {
return mEaseOutDuration;
}
// motions must report their priority
virtual LLJoint::JointPriority getPriority() {
return mPriority;
}
virtual LLMotionBlendType getBlendType() { return NORMAL_BLEND; }
// called to determine when a motion should be activated/deactivated based on avatar pixel coverage
virtual F32 getMinPixelArea() { return MIN_REQUIRED_PIXEL_AREA_KEYFRAME; }
// run-time (post constructor) initialization,
// called after parameters have been set
// must return true to indicate success and be available for activation
virtual LLMotionInitStatus onInitialize(LLCharacter *character);
// called when a motion is activated
// must return TRUE to indicate success, or else
// it will be deactivated
virtual BOOL onActivate();
// called per time step
// must return TRUE while it is active, and
// must return FALSE when the motion is completed.
virtual BOOL onUpdate(F32 time, U8* joint_mask);
// called when a motion is deactivated
virtual void onDeactivate();
virtual LLPose* getPose() { return mPoseBlender.getBlendedPose();}
protected:
//-------------------------------------------------------------------------
// new functions defined by this subclass
//-------------------------------------------------------------------------
struct ParameterizedMotion
{
ParameterizedMotion(LLMotion* motion, F32 param) : mMotion(motion), mParam(param) {}
LLMotion* mMotion;
F32 mParam;
};
// add a motion and associated parameter triplet
BOOL addKeyframeMotion(char *name, const LLUUID &id, char *param, F32 value);
// set default motion for LOD and retrieving blend constants
void setDefaultKeyframeMotion(char *);
BOOL loadMotions();
protected:
//-------------------------------------------------------------------------
// Member Data
//-------------------------------------------------------------------------
struct compare_motions
{
bool operator() (const ParameterizedMotion& a, const ParameterizedMotion& b) const
{
if (a.mParam != b.mParam)
return (a.mParam < b.mParam);
else
return a.mMotion < b.mMotion;
}
};
typedef std::set < ParameterizedMotion, compare_motions > motion_list_t;
typedef std::map <std::string, motion_list_t > motion_map_t;
motion_map_t mParameterizedMotions;
LLMotion* mDefaultKeyframeMotion;
LLCharacter* mCharacter;
LLPoseBlender mPoseBlender;
F32 mEaseInDuration;
F32 mEaseOutDuration;
F32 mDuration;
LLJoint::JointPriority mPriority;
LLUUID mTransactionID;
};
#endif // LL_LLKEYFRAMEMOTIONPARAM_H
......@@ -37,7 +37,6 @@
#include "llmotion.h"
#include "llpose.h"
#include "llframetimer.h"
#include "llstatemachine.h"
#include "llstring.h"
#include "absl/container/node_hash_map.h"
......
/**
* @file llstatemachine.cpp
* @brief LLStateMachine implementation file.
*
* $LicenseInfo:firstyear=2001&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2010, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#include "linden_common.h"
#include "llstatemachine.h"
#include "llapr.h"
#define FSM_PRINT_STATE_TRANSITIONS (0)
U32 LLUniqueID::sNextID = 0;
bool operator==(const LLUniqueID &a, const LLUniqueID &b)
{
return (a.mId == b.mId);
}
bool operator!=(const LLUniqueID &a, const LLUniqueID &b)
{
return (a.mId != b.mId);
}
//-----------------------------------------------------------------------------
// LLStateDiagram
//-----------------------------------------------------------------------------
LLStateDiagram::LLStateDiagram()
{
mDefaultState = NULL;
mUseDefaultState = FALSE;
}
// add a state to the state graph
BOOL LLStateDiagram::addState(LLFSMState *state)
{
mStates[state] = Transitions();
return TRUE;
}
// add a directed transition between 2 states
BOOL LLStateDiagram::addTransition(LLFSMState& start_state, LLFSMState& end_state, LLFSMTransition& transition)
{
StateMap::iterator state_it;
state_it = mStates.find(&start_state);
Transitions* state_transitions = NULL;
if (state_it == mStates.end() )
{
addState(&start_state);
state_transitions = &mStates[&start_state];
}
else
{
state_transitions = &state_it->second;
}
state_it = mStates.find(&end_state);
if (state_it == mStates.end() )
{
addState(&end_state);
}
Transitions::iterator transition_it = state_transitions->find(&transition);
if (transition_it != state_transitions->end())
{
LL_ERRS() << "LLStateTable::addDirectedTransition() : transition already exists" << LL_ENDL;
return FALSE; // transition already exists
}
(*state_transitions)[&transition] = &end_state;
return TRUE;
}
// add an undirected transition between 2 states
BOOL LLStateDiagram::addUndirectedTransition(LLFSMState& start_state, LLFSMState& end_state, LLFSMTransition& transition)
{
BOOL result;
result = addTransition(start_state, end_state, transition);
if (result)
{
result = addTransition(end_state, start_state, transition);
}
return result;
}
// add a transition that exists for every state
void LLStateDiagram::addDefaultTransition(LLFSMState& end_state, LLFSMTransition& transition)
{
mDefaultTransitions[&transition] = &end_state;
}
// process a possible transition, and get the resulting state
LLFSMState* LLStateDiagram::processTransition(LLFSMState& start_state, LLFSMTransition& transition)
{
// look up transition
//LLFSMState** dest_state = (mStates.getValue(&start_state))->getValue(&transition);
LLFSMState* dest_state = NULL;
StateMap::iterator state_it = mStates.find(&start_state);
if (state_it == mStates.end())
{
return NULL;
}
Transitions::iterator transition_it = state_it->second.find(&transition);
// try default transitions if state-specific transition not found
if (transition_it == state_it->second.end())
{
dest_state = mDefaultTransitions[&transition];
}
else
{
dest_state = transition_it->second;
}
// if we have a destination state...
if (NULL != dest_state)
{
// ...return it...
return dest_state;
}
// ... otherwise ...
else
{
// ...look for default state...
if (mUseDefaultState)
{
// ...return it if we have it...
return mDefaultState;
}
else
{
// ...or else we're still in the same state.
return &start_state;
}
}
}
void LLStateDiagram::setDefaultState(LLFSMState& default_state)
{
mUseDefaultState = TRUE;
mDefaultState = &default_state;
}
S32 LLStateDiagram::numDeadendStates()
{
S32 numDeadends = 0;
StateMap::iterator state_it;
for(state_it = mStates.begin(); state_it != mStates.end(); ++state_it)
{
if (state_it->second.size() == 0)
{
numDeadends++;
}
}
return numDeadends;
}
BOOL LLStateDiagram::stateIsValid(LLFSMState& state)
{
if (mStates.find(&state) != mStates.end())
{
return TRUE;
}
return FALSE;
}
LLFSMState* LLStateDiagram::getState(U32 state_id)
{
StateMap::iterator state_it;
for(state_it = mStates.begin(); state_it != mStates.end(); ++state_it)
{
if (state_it->first->getID() == state_id)
{
return state_it->first;
}
}
return NULL;
}
BOOL LLStateDiagram::saveDotFile(const std::string& filename)
{
LLAPRFile outfile ;
outfile.open(filename, LL_APR_W);
apr_file_t* dot_file = outfile.getFileHandle() ;
if (!dot_file)
{
LL_WARNS() << "LLStateDiagram::saveDotFile() : Couldn't open " << filename << " to save state diagram." << LL_ENDL;
return FALSE;
}
apr_file_printf(dot_file, "digraph StateMachine {\n\tsize=\"100,100\";\n\tfontsize=40;\n\tlabel=\"Finite State Machine\";\n\torientation=landscape\n\tratio=.77\n");
StateMap::iterator state_it;
for(state_it = mStates.begin(); state_it != mStates.end(); ++state_it)
{
apr_file_printf(dot_file, "\t\"%s\" [fontsize=28,shape=box]\n", state_it->first->getName().c_str());
}
apr_file_printf(dot_file, "\t\"All States\" [fontsize=30,style=bold,shape=box]\n");
Transitions::iterator transitions_it;
for(transitions_it = mDefaultTransitions.begin(); transitions_it != mDefaultTransitions.end(); ++transitions_it)
{
apr_file_printf(dot_file, "\t\"All States\" -> \"%s\" [label = \"%s\",fontsize=24];\n", transitions_it->second->getName().c_str(),
transitions_it->second->getName().c_str());
}
if (mDefaultState)
{
apr_file_printf(dot_file, "\t\"All States\" -> \"%s\";\n", mDefaultState->getName().c_str());
}
for(state_it = mStates.begin(); state_it != mStates.end(); ++state_it)
{
LLFSMState *state = state_it->first;
Transitions::iterator transitions_it;
for(transitions_it = state_it->second.begin();
transitions_it != state_it->second.end();
++transitions_it)
{
std::string state_name = state->getName();
std::string target_name = transitions_it->second->getName();
std::string transition_name = transitions_it->first->getName();
apr_file_printf(dot_file, "\t\"%s\" -> \"%s\" [label = \"%s\",fontsize=24];\n", state->getName().c_str(),
target_name.c_str(),
transition_name.c_str());
}
}
apr_file_printf(dot_file, "}\n");
return TRUE;
}
std::ostream& operator<<(std::ostream &s, LLStateDiagram &FSM)
{
if (FSM.mDefaultState)
{
s << "Default State: " << FSM.mDefaultState->getName() << "\n";
}
LLStateDiagram::Transitions::iterator transitions_it;
for(transitions_it = FSM.mDefaultTransitions.begin();
transitions_it != FSM.mDefaultTransitions.end();
++transitions_it)
{
s << "Any State -- " << transitions_it->first->getName()
<< " --> " << transitions_it->second->getName() << "\n";
}
LLStateDiagram::StateMap::iterator state_it;
for(state_it = FSM.mStates.begin(); state_it != FSM.mStates.end(); ++state_it)
{
LLStateDiagram::Transitions::iterator transitions_it;
for(transitions_it = state_it->second.begin();
transitions_it != state_it->second.end();
++transitions_it)
{
s << state_it->first->getName() << " -- " << transitions_it->first->getName()
<< " --> " << transitions_it->second->getName() << "\n";
}
s << "\n";
}
return s;
}
//-----------------------------------------------------------------------------
// LLStateMachine
//-----------------------------------------------------------------------------
LLStateMachine::LLStateMachine()
{
// we haven't received a starting state yet
mCurrentState = NULL;
mLastState = NULL;
mLastTransition = NULL;
mStateDiagram = NULL;
}
// returns current state
LLFSMState* LLStateMachine::getCurrentState() const
{
return mCurrentState;
}
// executes current state
void LLStateMachine::runCurrentState(void *data)
{
mCurrentState->execute(data);
}
// set current state
BOOL LLStateMachine::setCurrentState(LLFSMState *initial_state, void* user_data, BOOL skip_entry)
{
llassert(mStateDiagram);
if (mStateDiagram->stateIsValid(*initial_state))
{
mLastState = mCurrentState = initial_state;
if (!skip_entry)
{
initial_state->onEntry(user_data);
}
return TRUE;
}
return FALSE;
}
BOOL LLStateMachine::setCurrentState(U32 state_id, void* user_data, BOOL skip_entry)
{
llassert(mStateDiagram);
LLFSMState* state = mStateDiagram->getState(state_id);
if (state)
{
mLastState = mCurrentState = state;
if (!skip_entry)
{
state->onEntry(user_data);
}
return TRUE;
}
return FALSE;
}
void LLStateMachine::processTransition(LLFSMTransition& transition, void* user_data)
{
llassert(mStateDiagram);
if (NULL == mCurrentState)
{
LL_WARNS() << "mCurrentState == NULL; aborting processTransition()" << LL_ENDL;
return;
}
LLFSMState* new_state = mStateDiagram->processTransition(*mCurrentState, transition);
if (NULL == new_state)
{
LL_WARNS() << "new_state == NULL; aborting processTransition()" << LL_ENDL;
return;
}
mLastTransition = &transition;
mLastState = mCurrentState;
if (*mCurrentState != *new_state)
{
mCurrentState->onExit(user_data);
mCurrentState = new_state;
mCurrentState->onEntry(user_data);
#if FSM_PRINT_STATE_TRANSITIONS
LL_INFOS() << "Entering state " << mCurrentState->getName() <<
" on transition " << transition.getName() << " from state " <<
mLastState->getName() << LL_ENDL;
#endif
}
}
void LLStateMachine::setStateDiagram(LLStateDiagram* diagram)
{
mStateDiagram = diagram;
}
/**
* @file llstatemachine.h
* @brief LLStateMachine class header file.
*
* $LicenseInfo:firstyear=2001&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2010, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#ifndef LL_LLSTATEMACHINE_H
#define LL_LLSTATEMACHINE_H
#include <string>
#include "llerror.h"
#include <map>
class LLUniqueID
{
friend bool operator==(const LLUniqueID &a, const LLUniqueID &b);
friend bool operator!=(const LLUniqueID &a, const LLUniqueID &b);
protected:
static U32 sNextID;
U32 mId;
public:
LLUniqueID(){mId = sNextID++;}
virtual ~LLUniqueID() = default;
U32 getID() {return mId;}
};
class LLFSMTransition final : public LLUniqueID
{
public:
LLFSMTransition() : LLUniqueID(){};
virtual std::string getName()const { return "unnamed"; }
};
class LLFSMState final : public LLUniqueID
{
public:
LLFSMState() : LLUniqueID(){};
virtual void onEntry(void *){};
virtual void onExit(void *){};
virtual void execute(void *){};
virtual std::string getName() const { return "unnamed"; }
};
class LLStateDiagram
{
typedef std::map<LLFSMTransition*, LLFSMState*> Transitions;
friend std::ostream& operator<<(std::ostream &s, LLStateDiagram &FSM);
friend class LLStateMachine;
protected:
typedef std::map<LLFSMState*, Transitions> StateMap;
StateMap mStates;
Transitions mDefaultTransitions;
LLFSMState* mDefaultState;
BOOL mUseDefaultState;
public:
LLStateDiagram();
virtual ~LLStateDiagram() = default;
protected:
// add a state to the state graph, executed implicitly when adding transitions
BOOL addState(LLFSMState *state);
// add a directed transition between 2 states
BOOL addTransition(LLFSMState& start_state, LLFSMState& end_state, LLFSMTransition& transition);
// add an undirected transition between 2 states
BOOL addUndirectedTransition(LLFSMState& start_state, LLFSMState& end_state, LLFSMTransition& transition);
// add a transition that is taken if none other exist
void addDefaultTransition(LLFSMState& end_state, LLFSMTransition& transition);
// process a possible transition, and get the resulting state
LLFSMState* processTransition(LLFSMState& start_state, LLFSMTransition& transition);
// add a transition that exists for every state
void setDefaultState(LLFSMState& default_state);
// return total number of states with no outgoing transitions
S32 numDeadendStates();
// does this state exist in the state diagram?
BOOL stateIsValid(LLFSMState& state);
// get a state pointer by ID
LLFSMState* getState(U32 state_id);
public:
// save the graph in a DOT file for rendering and visualization
BOOL saveDotFile(const std::string& filename);
};
class LLStateMachine
{
protected:
LLFSMState* mCurrentState;
LLFSMState* mLastState;
LLFSMTransition* mLastTransition;
LLStateDiagram* mStateDiagram;
public:
LLStateMachine();
virtual ~LLStateMachine() = default;
// set state diagram
void setStateDiagram(LLStateDiagram* diagram);
// process this transition
void processTransition(LLFSMTransition &transition, void* user_data);
// returns current state
LLFSMState* getCurrentState() const;
// execute current state
void runCurrentState(void *data);
// set state by state pointer
BOOL setCurrentState(LLFSMState *initial_state, void* user_data, BOOL skip_entry = TRUE);
// set state by unique ID
BOOL setCurrentState(U32 state_id, void* user_data, BOOL skip_entry = TRUE);
};
#endif //_LL_LLSTATEMACHINE_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment