From 9bc27fb3903c75749031c97979fe8c8e1b96faa0 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sat, 3 Feb 2007 06:17:02 +0000 Subject: [PATCH] Messages and commands are nearly identical: they both have a name, they can both have parameters stashed in a table, and they both play stuff out of the actor command list. Merge them; now the only difference between playing and broadcasting a command is that playing plays recursively on a tree and broadcasting sends to all subscribers. The distinction between "messages" and "commands" is cosmetic now; everything is a message. The only thing special about commands intended to be received as a broadcast is the "Message" suffix, which subscribes it. --- stepmania/src/Actor.cpp | 15 ++++++--------- stepmania/src/Actor.h | 2 +- stepmania/src/ActorFrame.cpp | 8 ++++---- stepmania/src/ActorFrame.h | 2 +- stepmania/src/MessageManager.cpp | 22 +++++++++++++++++++++- stepmania/src/MessageManager.h | 2 ++ stepmania/src/NoteDisplay.cpp | 9 +++------ stepmania/src/ScoreDisplayAliveTime.cpp | 4 ++-- stepmania/src/ScoreDisplayAliveTime.h | 2 +- stepmania/src/ScoreDisplayCalories.cpp | 6 +++--- stepmania/src/ScoreDisplayCalories.h | 2 +- stepmania/src/Transition.cpp | 6 +++--- stepmania/src/Transition.h | 2 +- 13 files changed, 49 insertions(+), 33 deletions(-) diff --git a/stepmania/src/Actor.cpp b/stepmania/src/Actor.cpp index 80049658a2..774acb6b3d 100644 --- a/stepmania/src/Actor.cpp +++ b/stepmania/src/Actor.cpp @@ -1189,11 +1189,11 @@ const apActorCommands *Actor::GetCommand( const RString &sCommandName ) const return &it->second; } -void Actor::PlayCommand( const RString &sCommandName, const LuaReference *pParamTable ) +void Actor::HandleMessage( const Message &msg ) { - const apActorCommands *pCmd = GetCommand( sCommandName ); + const apActorCommands *pCmd = GetCommand( msg.GetName() ); if( pCmd != NULL ) - RunCommands( *pCmd, pParamTable ); + RunCommands( *pCmd, &msg.GetParamTable() ); } void Actor::PushContext( lua_State *L ) @@ -1220,11 +1220,6 @@ void Actor::SetParent( Actor *pParent ) LUA->Release( L ); } -void Actor::HandleMessage( const Message &msg ) -{ - PlayCommand( msg.GetName(), &msg.GetParamTable() ); -} - Actor::TweenInfo::TweenInfo() { m_pTween = NULL; @@ -1376,7 +1371,9 @@ public: lua_pushvalue( L, 2 ); ParamTable.SetFromStack( L ); - p->PlayCommand( SArg(1), &ParamTable ); + Message msg( SArg(1), ParamTable ); + p->HandleMessage( msg ); + return 0; } static int queuecommand( T* p, lua_State *L ) { p->QueueCommand(SArg(1)); return 0; } diff --git a/stepmania/src/Actor.h b/stepmania/src/Actor.h index c3a708eb4a..1b4d75ac6a 100644 --- a/stepmania/src/Actor.h +++ b/stepmania/src/Actor.h @@ -356,7 +356,7 @@ public: void AddCommand( const RString &sCmdName, apActorCommands apac ); bool HasCommand( const RString &sCmdName ); const apActorCommands *GetCommand( const RString &sCommandName ) const; - virtual void PlayCommand( const RString &sCommandName, const LuaReference *pParamTable = NULL ); + void PlayCommand( const RString &sCommandName ) { HandleMessage( Message(sCommandName) ); } virtual void RunCommands( const LuaReference& cmds, const LuaReference *pParamTable = NULL ); void RunCommands( const apActorCommands& cmds, const LuaReference *pParamTable = NULL ) { this->RunCommands( *cmds, pParamTable ); } // convenience // If we're a leaf, then execute this command. diff --git a/stepmania/src/ActorFrame.cpp b/stepmania/src/ActorFrame.cpp index 676a75b1d6..e615452dce 100644 --- a/stepmania/src/ActorFrame.cpp +++ b/stepmania/src/ActorFrame.cpp @@ -352,19 +352,19 @@ void ActorFrame::SetPropagateCommands( bool b ) m_bPropagateCommands = b; } -void ActorFrame::PlayCommand( const RString &sCommandName, const LuaReference *pParamTable ) +void ActorFrame::HandleMessage( const Message &msg ) { - Actor::PlayCommand( sCommandName, pParamTable ); + Actor::HandleMessage( msg ); // HACK: Don't propogate Init. It gets called once for every Actor when the // Actor is loaded, and we don't want to call it again. - if( sCommandName == "Init" ) + if( msg.GetName() == "Init" ) return; for( unsigned i=0; iPlayCommand( sCommandName, pParamTable ); + pActor->HandleMessage( msg ); } } diff --git a/stepmania/src/ActorFrame.h b/stepmania/src/ActorFrame.h index b6bb09e5e9..365d786f0e 100644 --- a/stepmania/src/ActorFrame.h +++ b/stepmania/src/ActorFrame.h @@ -64,7 +64,7 @@ public: /* Amount of time until all tweens (and all children's tweens) have stopped: */ virtual float GetTweenTimeLeft() const; - virtual void PlayCommand( const RString &sCommandName, const LuaReference *pParamTable = NULL ); + virtual void HandleMessage( const Message &msg ); virtual void RunCommands( const LuaReference& cmds, const LuaReference *pParamTable = NULL ); void RunCommands( const apActorCommands& cmds, const LuaReference *pParamTable = NULL ) { this->RunCommands( *cmds, pParamTable ); } // convenience diff --git a/stepmania/src/MessageManager.cpp b/stepmania/src/MessageManager.cpp index cde920fd84..4f92b241df 100644 --- a/stepmania/src/MessageManager.cpp +++ b/stepmania/src/MessageManager.cpp @@ -158,6 +158,17 @@ Message::Message( const RString &s ) m_pParams = new LuaTable; } +Message::Message( const RString &s, const LuaReference ¶ms ) +{ + m_sName = s; + Lua *L = LUA->Get(); + m_pParams = new LuaTable; // XXX: creates an extra table + params.PushSelf( L ); + m_pParams->SetFromStack( L ); + LUA->Release( L ); +// m_pParams = new LuaTable( params ); +} + Message::~Message() { delete m_pParams; @@ -168,6 +179,14 @@ void Message::PushParamTable( lua_State *L ) m_pParams->PushSelf( L ); } +void Message::SetParamTable( const LuaReference ¶ms ) +{ + Lua *L = LUA->Get(); + params.PushSelf( L ); + m_pParams->SetFromStack( L ); + LUA->Release( L ); +} + const LuaReference &Message::GetParamTable() const { return *m_pParams; @@ -305,7 +324,8 @@ public: lua_pushvalue( L, 2 ); ParamTable.SetFromStack( L ); - p->Broadcast( SArg(1) ); + Message msg( SArg(1), ParamTable ); + p->Broadcast( msg ); return 0; } diff --git a/stepmania/src/MessageManager.h b/stepmania/src/MessageManager.h index 93cea7dacc..c4e1510103 100644 --- a/stepmania/src/MessageManager.h +++ b/stepmania/src/MessageManager.h @@ -149,11 +149,13 @@ const RString& MessageIDToString( MessageID m ); struct Message { explicit Message( const RString &s ); + Message( const RString &s, const LuaReference ¶ms ); ~Message(); RString GetName() const { return m_sName; } void PushParamTable( lua_State *L ); const LuaReference &GetParamTable() const; + void SetParamTable( const LuaReference ¶ms ); void GetParamFromStack( lua_State *L, const RString &sName ) const; void SetParamFromStack( lua_State *L, const RString &sName ); diff --git a/stepmania/src/NoteDisplay.cpp b/stepmania/src/NoteDisplay.cpp index 86efee738d..a02ac4a62f 100644 --- a/stepmania/src/NoteDisplay.cpp +++ b/stepmania/src/NoteDisplay.cpp @@ -746,12 +746,9 @@ void NoteDisplay::DrawTap( const TapNote& tn, int iCol, float fBeat, bool bOnSam if( tn.type == TapNote::attack ) { - Lua *L = LUA->Get(); - LuaTable tab; - LuaHelpers::Push( L, tn.sAttackModifiers ); - tab.Set( L, "Modifiers" ); - pActor->PlayCommand( "SetAttack", &tab ); - LUA->Release( L ); + Message msg( "SetAttack" ); + msg.SetParam( "Modifiers", tn.sAttackModifiers ); + pActor->HandleMessage( msg ); } const float fYOffset = ArrowEffects::GetYOffset( m_pPlayerState, iCol, fBeat ); diff --git a/stepmania/src/ScoreDisplayAliveTime.cpp b/stepmania/src/ScoreDisplayAliveTime.cpp index aa5613b47b..df556212ab 100644 --- a/stepmania/src/ScoreDisplayAliveTime.cpp +++ b/stepmania/src/ScoreDisplayAliveTime.cpp @@ -40,11 +40,11 @@ void ScoreDisplayAliveTime::Update( float fDelta ) BitmapText::Update( fDelta ); } -void ScoreDisplayAliveTime::PlayCommand( const RString &sCommandName, const LuaReference *pParamTable ) +void ScoreDisplayAliveTime::HandleMessage( const Message &msg ) { // TODO: Add handling of GoalComplete message - BitmapText::PlayCommand( sCommandName, pParamTable ); + BitmapText::HandleMessage( msg ); } void ScoreDisplayAliveTime::UpdateNumber() diff --git a/stepmania/src/ScoreDisplayAliveTime.h b/stepmania/src/ScoreDisplayAliveTime.h index bb09326a4e..1e3b8904a6 100644 --- a/stepmania/src/ScoreDisplayAliveTime.h +++ b/stepmania/src/ScoreDisplayAliveTime.h @@ -19,7 +19,7 @@ public: void LoadFromNode( const XNode* pNode ); virtual ScoreDisplayAliveTime *Copy() const; - void PlayCommand( const RString &sCommandName, const LuaReference *pParamTable ); + virtual void HandleMessage( const Message &msg ); void UpdateNumber(); diff --git a/stepmania/src/ScoreDisplayCalories.cpp b/stepmania/src/ScoreDisplayCalories.cpp index 61536d38df..95d0f38321 100644 --- a/stepmania/src/ScoreDisplayCalories.cpp +++ b/stepmania/src/ScoreDisplayCalories.cpp @@ -49,14 +49,14 @@ void ScoreDisplayCalories::Update( float fDelta ) RollingNumbers::Update( fDelta ); } -void ScoreDisplayCalories::PlayCommand( const RString &sCommandName, const LuaReference *pParamTable ) +void ScoreDisplayCalories::HandleMessage( const Message &msg ) { - if( sCommandName == m_sMessageOnStep ) + if( msg.GetName() == m_sMessageOnStep ) { UpdateNumber(); } - RollingNumbers::PlayCommand( sCommandName, pParamTable ); + RollingNumbers::HandleMessage( msg ); } void ScoreDisplayCalories::UpdateNumber() diff --git a/stepmania/src/ScoreDisplayCalories.h b/stepmania/src/ScoreDisplayCalories.h index c950b0b15b..5734bc5536 100644 --- a/stepmania/src/ScoreDisplayCalories.h +++ b/stepmania/src/ScoreDisplayCalories.h @@ -18,7 +18,7 @@ public: void LoadFromNode( const XNode* pNode ); virtual ScoreDisplayCalories *Copy() const; - void PlayCommand( const RString &sCommandName, const LuaReference *pParamTable = NULL ); + virtual void HandleMessage( const Message &msg ); void UpdateNumber(); diff --git a/stepmania/src/Transition.cpp b/stepmania/src/Transition.cpp index ba215ee5b7..be6eeb55f1 100644 --- a/stepmania/src/Transition.cpp +++ b/stepmania/src/Transition.cpp @@ -66,12 +66,12 @@ bool Transition::EarlyAbortDraw() const /* Our parent might send us OnCommand. We do that ourself, because * we sometimes want to know GetLengthSeconds before StartTransitioning. * Make sure we don't process OnCommand twice. */ -void Transition::PlayCommand( const RString &sCommandName, const LuaReference *pParamTable ) +void Transition::HandleMessage( const Message &msg ) { - if( sCommandName == "On" ) + if( msg.GetName() == "On" ) return; - ActorFrame::PlayCommand( sCommandName, pParamTable ); + ActorFrame::HandleMessage( msg ); } void Transition::StartTransitioning( ScreenMessage send_when_done ) diff --git a/stepmania/src/Transition.h b/stepmania/src/Transition.h index d580e037e1..5130e95ec4 100644 --- a/stepmania/src/Transition.h +++ b/stepmania/src/Transition.h @@ -16,7 +16,7 @@ public: void Load( RString sBGAniDir ); virtual void UpdateInternal( float fDeltaTime ); - virtual void PlayCommand( const RString &sCommandName, const LuaReference *pParamTable = NULL ); + virtual void HandleMessage( const Message &msg ); virtual void StartTransitioning( ScreenMessage send_when_done = SM_None ); virtual bool EarlyAbortDraw() const;