1 /* Copyright (C) 2015 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
27 #define FSM_INVALID_STATE ( unsigned int )( ~0 )
33 typedef bool ( *CONDITION
) ( void* pContext
);
34 typedef bool ( *ACTION
) ( void* pContext
, const CFsmEvent
* pEvent
);
43 typedef std::set
< unsigned int > StateSet
;
44 typedef std::map
< unsigned int, CFsmEvent
* > EventMap
;
45 typedef std::vector
< CFsmTransition
* > TransitionList
;
46 typedef std::vector
< CallbackFunction
> CallbackList
;
49 * Represents a signal in the state machine that a change has occurred.
50 * The CFsmEvent objects are under the control of CFsm so
51 * they are created and deleted via CFsm.
55 NONCOPYABLE(CFsmEvent
);
58 CFsmEvent( unsigned int type
);
61 unsigned int GetType ( void ) const { return m_Type
; }
62 void* GetParamRef ( void ) { return m_Param
; }
63 void SetParamRef ( void* pParam
);
66 unsigned int m_Type
; // Event type
67 void* m_Param
; // Event paramater
72 * An association of event, condition, action and next state.
76 NONCOPYABLE(CFsmTransition
);
79 CFsmTransition( unsigned int state
);
80 ~CFsmTransition( void );
85 void RegisterCondition (
88 void SetEvent ( CFsmEvent
* pEvent
);
89 CFsmEvent
* GetEvent ( void ) const { return m_Event
; }
90 void SetNextState ( unsigned int nextState
);
91 unsigned int GetNextState ( void ) const { return m_NextState
; }
92 unsigned int GetCurrState ( void ) const { return m_CurrState
; }
93 const CallbackList
& GetActions ( void ) const { return m_Actions
; }
94 const CallbackList
& GetConditions ( void ) const { return m_Conditions
; }
95 bool ApplyConditions ( void ) const;
96 bool RunActions ( void ) const;
99 unsigned int m_CurrState
; // Current state
100 unsigned int m_NextState
; // Next state
101 CFsmEvent
* m_Event
; // Transition event
102 CallbackList m_Actions
; // List of actions for transition
103 CallbackList m_Conditions
; // List of conditions for transition
107 * Manages states, events, actions and transitions
108 * between states. It provides an interface for advertising
109 * events and track the current state. The implementation is
110 * a Mealy state machine, so the system respond to events
111 * and execute some action.
113 * A Mealy state machine has behaviour associated with state
114 * transitions; Mealy machines are event driven where an
115 * event triggers a state transition
123 virtual ~CFsm( void );
126 * Constructs the state machine. This method must be overriden so that
127 * connections are constructed for the particular state machine implemented
129 virtual void Setup( void );
132 * Clear event, action and condition lists and reset state machine
134 void Shutdown( void );
136 void AddState ( unsigned int state
);
137 CFsmEvent
* AddEvent ( unsigned int eventType
);
138 CFsmTransition
* AddTransition (
140 unsigned int eventType
,
141 unsigned int nextState
);
142 CFsmTransition
* AddTransition (
144 unsigned int eventType
,
145 unsigned int nextState
,
148 CFsmTransition
* GetTransition (
150 unsigned int eventType
) const;
151 CFsmTransition
* GetEventTransition ( unsigned int eventType
) const;
152 void SetFirstState ( unsigned int firstState
);
153 void SetCurrState ( unsigned int state
);
154 unsigned int GetCurrState ( void ) const { return m_CurrState
; }
155 void SetNextState ( unsigned int nextState
) { m_NextState
= nextState
; }
156 unsigned int GetNextState ( void ) const { return m_NextState
; }
157 const StateSet
& GetStates ( void ) const { return m_States
; }
158 const EventMap
& GetEvents ( void ) const { return m_Events
; }
159 const TransitionList
& GetTransitions ( void ) const { return m_Transitions
; }
160 bool Update ( unsigned int eventType
, void* pEventData
);
161 bool IsValidState ( unsigned int state
) const;
162 bool IsValidEvent ( unsigned int eventType
) const;
163 virtual bool IsDone ( void ) const;
166 bool IsFirstTime ( void ) const;
168 bool m_Done
; // FSM work is done
169 unsigned int m_FirstState
; // Initial state
170 unsigned int m_CurrState
; // Current state
171 unsigned int m_NextState
; // Next state
172 StateSet m_States
; // List of states
173 EventMap m_Events
; // List of events
174 TransitionList m_Transitions
; // State transitions