2024-01-29 10:44:14 -08:00
|
|
|
/* Copyright (C) 2024 Wildfire Games.
|
2023-12-02 16:30:12 -08:00
|
|
|
* This file is part of 0 A.D.
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
2009-04-18 10:00:33 -07:00
|
|
|
* 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.
|
|
|
|
|
*
|
2023-12-02 16:30:12 -08:00
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
2009-04-18 10:00:33 -07:00
|
|
|
* 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
|
2023-12-02 16:30:12 -08:00
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
2009-04-18 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2009-04-11 10:00:39 -07:00
|
|
|
#include "precompiled.h"
|
2022-10-12 11:16:27 -07:00
|
|
|
#include "FSM.h"
|
2009-04-11 10:00:39 -07:00
|
|
|
|
|
|
|
|
|
2023-12-20 12:44:06 -08:00
|
|
|
CFsmEvent::CFsmEvent(unsigned int type, void* pParam)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2023-01-04 09:10:05 -08:00
|
|
|
m_Type = type;
|
2023-12-20 12:44:06 -08:00
|
|
|
m_Param = pParam;
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
|
|
|
|
|
2023-01-04 09:10:05 -08:00
|
|
|
CFsmEvent::~CFsmEvent()
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2023-01-05 04:09:02 -08:00
|
|
|
m_Param = nullptr;
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
|
|
|
|
|
2023-01-04 09:10:05 -08:00
|
|
|
CFsm::CFsm()
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2023-01-04 09:10:05 -08:00
|
|
|
m_Done = false;
|
2011-10-27 07:10:55 -07:00
|
|
|
m_FirstState = FSM_INVALID_STATE;
|
2023-01-04 09:10:05 -08:00
|
|
|
m_CurrState = FSM_INVALID_STATE;
|
|
|
|
|
m_NextState = FSM_INVALID_STATE;
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
|
|
|
|
|
2023-01-04 09:10:05 -08:00
|
|
|
CFsm::~CFsm()
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
|
|
|
|
Shutdown();
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 09:10:05 -08:00
|
|
|
void CFsm::Setup()
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
|
|
|
|
// Does nothing by default
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 09:10:05 -08:00
|
|
|
void CFsm::Shutdown()
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
|
|
|
|
m_States.clear();
|
|
|
|
|
m_Transitions.clear();
|
|
|
|
|
|
2023-01-04 09:10:05 -08:00
|
|
|
m_Done = false;
|
2011-10-27 07:10:55 -07:00
|
|
|
m_FirstState = FSM_INVALID_STATE;
|
2023-01-04 09:10:05 -08:00
|
|
|
m_CurrState = FSM_INVALID_STATE;
|
|
|
|
|
m_NextState = FSM_INVALID_STATE;
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
|
|
|
|
|
2023-01-04 09:10:05 -08:00
|
|
|
void CFsm::AddState(unsigned int state)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2023-01-04 09:10:05 -08:00
|
|
|
m_States.insert(state);
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
|
|
|
|
|
2024-01-29 10:44:14 -08:00
|
|
|
void CFsm::AddTransition(unsigned int state, unsigned int eventType, unsigned int nextState,
|
2023-07-25 00:50:33 -07:00
|
|
|
Action* pAction /* = nullptr */, void* pContext /* = nullptr*/)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
|
|
|
|
// Make sure we store the current state
|
2023-01-04 09:10:05 -08:00
|
|
|
AddState(state);
|
2009-04-11 10:00:39 -07:00
|
|
|
|
|
|
|
|
// Make sure we store the next state
|
2023-01-04 09:10:05 -08:00
|
|
|
AddState(nextState);
|
2009-04-11 10:00:39 -07:00
|
|
|
|
2024-01-29 10:44:14 -08:00
|
|
|
m_Transitions.insert({TransitionKey{state, eventType}, Transition{{pAction, pContext}, nextState}});
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
|
|
|
|
|
2023-01-04 09:10:05 -08:00
|
|
|
void CFsm::SetFirstState(unsigned int firstState)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
|
|
|
|
m_FirstState = firstState;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 09:10:05 -08:00
|
|
|
void CFsm::SetCurrState(unsigned int state)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
|
|
|
|
m_CurrState = state;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 09:10:05 -08:00
|
|
|
bool CFsm::IsFirstTime() const
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2023-01-04 09:10:05 -08:00
|
|
|
return (m_CurrState == FSM_INVALID_STATE);
|
2009-04-11 10:00:39 -07:00
|
|
|
}
|
|
|
|
|
|
2023-01-04 09:10:05 -08:00
|
|
|
bool CFsm::Update(unsigned int eventType, void* pEventParam)
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2023-01-04 09:10:05 -08:00
|
|
|
if (IsFirstTime())
|
2009-04-11 10:00:39 -07:00
|
|
|
m_CurrState = m_FirstState;
|
|
|
|
|
|
2024-01-29 10:44:14 -08:00
|
|
|
if (!IsValidState(m_CurrState))
|
|
|
|
|
return false;
|
|
|
|
|
|
2009-04-11 10:00:39 -07:00
|
|
|
// Lookup transition
|
2024-01-29 10:44:14 -08:00
|
|
|
auto transitionIterator = m_Transitions.find({m_CurrState, eventType});
|
|
|
|
|
if (transitionIterator == m_Transitions.end())
|
2014-04-26 11:34:34 -07:00
|
|
|
return false;
|
2009-04-11 10:00:39 -07:00
|
|
|
|
2023-12-20 12:44:06 -08:00
|
|
|
CFsmEvent event{eventType, pEventParam};
|
2009-04-11 10:00:39 -07:00
|
|
|
|
2011-10-27 07:10:55 -07:00
|
|
|
// Save the default state transition (actions might call SetNextState
|
|
|
|
|
// to override this)
|
2024-01-29 10:44:14 -08:00
|
|
|
SetNextState(transitionIterator->second.nextState);
|
2011-10-27 07:10:55 -07:00
|
|
|
|
2024-01-29 10:44:14 -08:00
|
|
|
if (!transitionIterator->second.action(event))
|
2014-04-26 11:34:34 -07:00
|
|
|
return false;
|
2009-04-11 10:00:39 -07:00
|
|
|
|
2023-01-04 09:10:05 -08:00
|
|
|
SetCurrState(GetNextState());
|
2011-10-27 07:10:55 -07:00
|
|
|
|
|
|
|
|
// Reset the next state since it's no longer valid
|
2023-01-04 09:10:05 -08:00
|
|
|
SetNextState(FSM_INVALID_STATE);
|
2015-12-21 05:58:32 -08:00
|
|
|
|
2009-04-11 10:00:39 -07:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 09:10:05 -08:00
|
|
|
bool CFsm::IsDone() const
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
|
|
|
|
// By default the internal flag m_Done is tested
|
|
|
|
|
return m_Done;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-04 09:10:05 -08:00
|
|
|
bool CFsm::IsValidState(unsigned int state) const
|
2009-04-11 10:00:39 -07:00
|
|
|
{
|
2023-01-04 09:10:05 -08:00
|
|
|
StateSet::const_iterator it = m_States.find(state);
|
|
|
|
|
if (it == m_States.end())
|
|
|
|
|
return false;
|
2009-04-11 10:00:39 -07:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|