From a2bba82b9d5e4e9175b1e794a266e963d72e22bf Mon Sep 17 00:00:00 2001 From: Ykkrosh Date: Thu, 27 Oct 2011 14:10:55 +0000 Subject: [PATCH] Let CFsm actions override the default state transition This was SVN commit r10432. --- source/network/fsm.cpp | 21 +++++++++++++++------ source/network/fsm.h | 5 ++++- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/source/network/fsm.cpp b/source/network/fsm.cpp index a245ac14b5..2075c73682 100644 --- a/source/network/fsm.cpp +++ b/source/network/fsm.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2010 Wildfire Games. +/* Copyright (C) 2011 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -169,8 +169,9 @@ bool CFsmTransition::RunActions( void ) const CFsm::CFsm( void ) { m_Done = false; - m_FirstState = ( unsigned int )FSM_INVALID_STATE; - m_CurrState = ( unsigned int )FSM_INVALID_STATE; + m_FirstState = FSM_INVALID_STATE; + m_CurrState = FSM_INVALID_STATE; + m_NextState = FSM_INVALID_STATE; } //----------------------------------------------------------------------------- @@ -222,8 +223,9 @@ void CFsm::Shutdown( void ) m_Transitions.clear(); m_Done = false; - m_FirstState = ( unsigned int )FSM_INVALID_STATE; - m_CurrState = ( unsigned int )FSM_INVALID_STATE; + m_FirstState = FSM_INVALID_STATE; + m_CurrState = FSM_INVALID_STATE; + m_NextState = FSM_INVALID_STATE; } //----------------------------------------------------------------------------- @@ -409,11 +411,18 @@ bool CFsm::Update( unsigned int eventType, void* pEventParam ) // Valid transition? if ( !pTransition->ApplyConditions() ) return false; + // Save the default state transition (actions might call SetNextState + // to override this) + SetNextState( pTransition->GetNextState() ); + // Run transition actions if ( !pTransition->RunActions() ) return false; // Switch state - SetCurrState( pTransition->GetNextState() ); + SetCurrState( GetNextState() ); + + // Reset the next state since it's no longer valid + SetNextState( FSM_INVALID_STATE ); return true; } diff --git a/source/network/fsm.h b/source/network/fsm.h index a656ee7e68..3077b5d6ff 100644 --- a/source/network/fsm.h +++ b/source/network/fsm.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2010 Wildfire Games. +/* Copyright (C) 2011 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify @@ -152,6 +152,8 @@ public: void SetFirstState ( unsigned int firstState ); void SetCurrState ( unsigned int state ); unsigned int GetCurrState ( void ) const { return m_CurrState; } + void SetNextState ( unsigned int nextState ) { m_NextState = nextState; } + unsigned int GetNextState ( void ) const { return m_NextState; } const StateSet& GetStates ( void ) const { return m_States; } const EventMap& GetEvents ( void ) const { return m_Events; } const TransitionList& GetTransitions ( void ) const { return m_Transitions; } @@ -166,6 +168,7 @@ private: bool m_Done; // FSM work is done unsigned int m_FirstState; // Initial state unsigned int m_CurrState; // Current state + unsigned int m_NextState; // Next state StateSet m_States; // List of states EventMap m_Events; // List of events TransitionList m_Transitions; // State transitions