Index: ps/trunk/source/network/FSM.cpp =================================================================== --- ps/trunk/source/network/FSM.cpp (revision 27986) +++ ps/trunk/source/network/FSM.cpp (revision 27987) @@ -1,253 +1,190 @@ /* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #include "precompiled.h" #include "FSM.h" -CFsmEvent::CFsmEvent(unsigned int type) +CFsmEvent::CFsmEvent(unsigned int type, void* pParam) { m_Type = type; - m_Param = nullptr; + m_Param = pParam; } CFsmEvent::~CFsmEvent() { m_Param = nullptr; } -void CFsmEvent::SetParamRef(void* pParam) -{ - m_Param = pParam; -} - CFsmTransition::CFsmTransition(const unsigned int state, const CallbackFunction action) : m_CurrState{state}, m_Action{action} {} -void CFsmTransition::SetEvent(CFsmEvent* pEvent) +void CFsmTransition::SetEventType(const unsigned int eventType) { - m_Event = pEvent; + m_EventType = eventType; } void CFsmTransition::SetNextState(unsigned int nextState) { m_NextState = nextState; } -bool CFsmTransition::RunAction() const +bool CFsmTransition::RunAction(CFsmEvent& event) const { - return !m_Action.pFunction || m_Action.pFunction(m_Action.pContext, m_Event); + return !m_Action.pFunction || m_Action.pFunction(m_Action.pContext, &event); } CFsm::CFsm() { m_Done = false; m_FirstState = FSM_INVALID_STATE; m_CurrState = FSM_INVALID_STATE; m_NextState = FSM_INVALID_STATE; } CFsm::~CFsm() { Shutdown(); } void CFsm::Setup() { // Does nothing by default } void CFsm::Shutdown() { // Release transitions TransitionList::iterator itTransition = m_Transitions.begin(); for (; itTransition < m_Transitions.end(); ++itTransition) delete *itTransition; - // Release events - EventMap::iterator itEvent = m_Events.begin(); - for (; itEvent != m_Events.end(); ++itEvent) - delete itEvent->second; - m_States.clear(); - m_Events.clear(); m_Transitions.clear(); m_Done = false; m_FirstState = FSM_INVALID_STATE; m_CurrState = FSM_INVALID_STATE; m_NextState = FSM_INVALID_STATE; } void CFsm::AddState(unsigned int state) { m_States.insert(state); } -CFsmEvent* CFsm::AddEvent(unsigned int eventType) -{ - CFsmEvent* pEvent = nullptr; - - // Lookup event by type - EventMap::iterator it = m_Events.find(eventType); - if (it != m_Events.end()) - { - pEvent = it->second; - } - else - { - pEvent = new CFsmEvent(eventType); - - // Store new event into internal map - m_Events[eventType] = pEvent; - } - - return pEvent; -} - CFsmTransition* CFsm::AddTransition(unsigned int state, unsigned int eventType, unsigned int nextState, Action* pAction /* = nullptr */, void* pContext /* = nullptr*/) { // Make sure we store the current state AddState(state); // Make sure we store the next state AddState(nextState); - // Make sure we store the event - CFsmEvent* pEvent = AddEvent(eventType); - if (!pEvent) - return nullptr; - // Create new transition CFsmTransition* pNewTransition = new CFsmTransition(state, {pAction, pContext}); // Setup new transition - pNewTransition->SetEvent(pEvent); + pNewTransition->SetEventType(eventType); pNewTransition->SetNextState(nextState); // Store new transition m_Transitions.push_back(pNewTransition); return pNewTransition; } CFsmTransition* CFsm::GetTransition(unsigned int state, unsigned int eventType) const { if (!IsValidState(state)) return nullptr; - if (!IsValidEvent(eventType)) - return nullptr; - TransitionList::const_iterator it = m_Transitions.begin(); for (; it != m_Transitions.end(); ++it) { CFsmTransition* pCurrTransition = *it; if (!pCurrTransition) continue; - CFsmEvent* pCurrEvent = pCurrTransition->GetEvent(); - if (!pCurrEvent) - continue; - // Is it our transition? - if (pCurrTransition->GetCurrState() == state && pCurrEvent->GetType() == eventType) + if (pCurrTransition->GetCurrState() == state && pCurrTransition->GetEventType() == eventType) return pCurrTransition; } // No transition found return nullptr; } void CFsm::SetFirstState(unsigned int firstState) { m_FirstState = firstState; } void CFsm::SetCurrState(unsigned int state) { m_CurrState = state; } bool CFsm::IsFirstTime() const { return (m_CurrState == FSM_INVALID_STATE); } bool CFsm::Update(unsigned int eventType, void* pEventParam) { - if (!IsValidEvent(eventType)) - return false; - if (IsFirstTime()) m_CurrState = m_FirstState; // Lookup transition CFsmTransition* pTransition = GetTransition(m_CurrState, eventType); if (!pTransition) return false; - // Setup event parameter - EventMap::iterator it = m_Events.find(eventType); - if (it != m_Events.end()) - { - CFsmEvent* pEvent = it->second; - if (pEvent) - pEvent->SetParamRef(pEventParam); - } + CFsmEvent event{eventType, pEventParam}; // Save the default state transition (actions might call SetNextState // to override this) SetNextState(pTransition->GetNextState()); - if (!pTransition->RunAction()) + if (!pTransition->RunAction(event)) return false; SetCurrState(GetNextState()); // Reset the next state since it's no longer valid SetNextState(FSM_INVALID_STATE); return true; } bool CFsm::IsDone() const { // By default the internal flag m_Done is tested return m_Done; } bool CFsm::IsValidState(unsigned int state) const { StateSet::const_iterator it = m_States.find(state); if (it == m_States.end()) return false; return true; } - -bool CFsm::IsValidEvent(unsigned int eventType) const -{ - EventMap::const_iterator it = m_Events.find(eventType); - if (it == m_Events.end()) - return false; - - return true; -} Index: ps/trunk/source/network/FSM.h =================================================================== --- ps/trunk/source/network/FSM.h (revision 27986) +++ ps/trunk/source/network/FSM.h (revision 27987) @@ -1,262 +1,240 @@ /* Copyright (C) 2023 Wildfire Games. * This file is part of 0 A.D. * * 0 A.D. is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * 0 A.D. 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with 0 A.D. If not, see . */ #ifndef FSM_H #define FSM_H #include -#include #include #include constexpr unsigned int FSM_INVALID_STATE{std::numeric_limits::max()}; class CFsmEvent; class CFsmTransition; class CFsm; using Action = bool(void* pContext, CFsmEvent* pEvent); struct CallbackFunction { Action* pFunction{nullptr}; void* pContext{nullptr}; }; using StateSet = std::set; -using EventMap = std::map; using TransitionList = std::vector; /** * Represents a signal in the state machine that a change has occurred. * The CFsmEvent objects are under the control of CFsm so * they are created and deleted via CFsm. */ class CFsmEvent { NONCOPYABLE(CFsmEvent); public: - CFsmEvent(unsigned int type); + CFsmEvent(unsigned int type, void* pParam); ~CFsmEvent(); unsigned int GetType() const { return m_Type; } void* GetParamRef() { return m_Param; } - void SetParamRef(void* pParam); - private: unsigned int m_Type; // Event type void* m_Param; // Event paramater }; /** * An association of event, action and next state. */ class CFsmTransition { NONCOPYABLE(CFsmTransition); public: /** * @param action Object executed upon transition. */ CFsmTransition(const unsigned int state, const CallbackFunction action); /** * Set event for which transition will occur. */ - void SetEvent(CFsmEvent* pEvent); - CFsmEvent* GetEvent() const + void SetEventType(const unsigned int eventType); + unsigned int GetEventType() const { - return m_Event; + return m_EventType; } /** * Set next state the transition will switch the system to. */ void SetNextState(unsigned int nextState); unsigned int GetNextState() const { return m_NextState; } unsigned int GetCurrState() const { return m_CurrState; } CallbackFunction GetAction() const { return m_Action; } /** * Executes action for the transition. * @note If there are no action, assume true. * @return whether the action returned true. */ - bool RunAction() const; + bool RunAction(CFsmEvent& event) const; private: unsigned int m_CurrState; unsigned int m_NextState; - CFsmEvent* m_Event; + unsigned int m_EventType; CallbackFunction m_Action; }; /** * Manages states, events, actions and transitions * between states. It provides an interface for advertising * events and track the current state. The implementation is * a Mealy state machine, so the system respond to events * and execute some action. * * A Mealy state machine has behaviour associated with state * transitions; Mealy machines are event driven where an * event triggers a state transition. */ class CFsm { NONCOPYABLE(CFsm); public: CFsm(); virtual ~CFsm(); /** * Constructs the state machine. This method must be overriden so that * connections are constructed for the particular state machine implemented. */ virtual void Setup(); /** * Clear event, transition lists and reset state machine. */ void Shutdown(); /** * Adds the specified state to the internal list of states. * @note If a state with the specified ID exists, the state is not added. */ void AddState(unsigned int state); /** - * Adds the specified event to the internal list of events. - * @note If an eveny with the specified ID exists, the event is not added. - * @return a pointer to the new event. - */ - CFsmEvent* AddEvent(unsigned int eventType); - - /** * Adds a new transistion to the state machine. * @return a pointer to the new transition. */ CFsmTransition* AddTransition(unsigned int state, unsigned int eventType, unsigned int nextState, Action* pAction = nullptr, void* pContext = nullptr); /** * Looks up the transition given the state, event and next state to transition to. */ CFsmTransition* GetTransition(unsigned int state, unsigned int eventType) const; CFsmTransition* GetEventTransition (unsigned int eventType) const; /** * Sets the initial state for FSM. */ void SetFirstState(unsigned int firstState); /** * Sets the current state and update the last state to the current state. */ void SetCurrState(unsigned int state); unsigned int GetCurrState() const { return m_CurrState; } void SetNextState(unsigned int nextState) { m_NextState = nextState; } unsigned int GetNextState() const { return m_NextState; } const StateSet& GetStates() const { return m_States; } - const EventMap& GetEvents() const - { - return m_Events; - } - const TransitionList& GetTransitions() const { return m_Transitions; } /** * Updates the FSM and retrieves next state. * @return whether the state was changed. */ bool Update(unsigned int eventType, void* pEventData); /** * Verifies whether the specified state is managed by the FSM. */ bool IsValidState(unsigned int state) const; /** - * Verifies whether the specified event is managed by the FSM. - */ - bool IsValidEvent(unsigned int eventType) const; - - /** * Tests whether the state machine has finished its work. * @note This is state machine specific. */ virtual bool IsDone() const; private: /** * Verifies whether state machine has already been updated. */ bool IsFirstTime() const; bool m_Done; unsigned int m_FirstState; unsigned int m_CurrState; unsigned int m_NextState; StateSet m_States; - EventMap m_Events; TransitionList m_Transitions; }; #endif // FSM_H