forked from 0ad/0ad
Remove the StateSet from FSM
The `StateSet` is only used to check if a state is valid. That's useless since it wouldn't be found in the `TransitionMap` - With the same outcome. Accepted By: @vladislavbelov Differential Revision: https://code.wildfiregames.com/D5236 This was SVN commit r28039.
This commit is contained in:
parent
04ce68f42e
commit
1eb11b39bf
@ -50,7 +50,6 @@ void CFsm::Setup()
|
|||||||
|
|
||||||
void CFsm::Shutdown()
|
void CFsm::Shutdown()
|
||||||
{
|
{
|
||||||
m_States.clear();
|
|
||||||
m_Transitions.clear();
|
m_Transitions.clear();
|
||||||
|
|
||||||
m_Done = false;
|
m_Done = false;
|
||||||
@ -59,20 +58,9 @@ void CFsm::Shutdown()
|
|||||||
m_NextState = FSM_INVALID_STATE;
|
m_NextState = FSM_INVALID_STATE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CFsm::AddState(unsigned int state)
|
|
||||||
{
|
|
||||||
m_States.insert(state);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CFsm::AddTransition(unsigned int state, unsigned int eventType, unsigned int nextState,
|
void CFsm::AddTransition(unsigned int state, unsigned int eventType, unsigned int nextState,
|
||||||
Action* pAction /* = nullptr */, void* pContext /* = nullptr*/)
|
Action* pAction /* = nullptr */, void* pContext /* = nullptr*/)
|
||||||
{
|
{
|
||||||
// Make sure we store the current state
|
|
||||||
AddState(state);
|
|
||||||
|
|
||||||
// Make sure we store the next state
|
|
||||||
AddState(nextState);
|
|
||||||
|
|
||||||
m_Transitions.insert({TransitionKey{state, eventType}, Transition{{pAction, pContext}, nextState}});
|
m_Transitions.insert({TransitionKey{state, eventType}, Transition{{pAction, pContext}, nextState}});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,9 +84,6 @@ bool CFsm::Update(unsigned int eventType, void* pEventParam)
|
|||||||
if (IsFirstTime())
|
if (IsFirstTime())
|
||||||
m_CurrState = m_FirstState;
|
m_CurrState = m_FirstState;
|
||||||
|
|
||||||
if (!IsValidState(m_CurrState))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
// Lookup transition
|
// Lookup transition
|
||||||
auto transitionIterator = m_Transitions.find({m_CurrState, eventType});
|
auto transitionIterator = m_Transitions.find({m_CurrState, eventType});
|
||||||
if (transitionIterator == m_Transitions.end())
|
if (transitionIterator == m_Transitions.end())
|
||||||
@ -126,12 +111,3 @@ bool CFsm::IsDone() const
|
|||||||
// By default the internal flag m_Done is tested
|
// By default the internal flag m_Done is tested
|
||||||
return m_Done;
|
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;
|
|
||||||
}
|
|
||||||
|
@ -19,7 +19,6 @@
|
|||||||
#define FSM_H
|
#define FSM_H
|
||||||
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <set>
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
|
|
||||||
@ -41,8 +40,6 @@ struct CallbackFunction
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
using StateSet = std::set<unsigned int>;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a signal in the state machine that a change has occurred.
|
* Represents a signal in the state machine that a change has occurred.
|
||||||
* The CFsmEvent objects are under the control of CFsm so
|
* The CFsmEvent objects are under the control of CFsm so
|
||||||
@ -101,12 +98,6 @@ public:
|
|||||||
*/
|
*/
|
||||||
void Shutdown();
|
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 a new transistion to the state machine.
|
* Adds a new transistion to the state machine.
|
||||||
*/
|
*/
|
||||||
@ -137,22 +128,12 @@ public:
|
|||||||
return m_NextState;
|
return m_NextState;
|
||||||
}
|
}
|
||||||
|
|
||||||
const StateSet& GetStates() const
|
|
||||||
{
|
|
||||||
return m_States;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Updates the FSM and retrieves next state.
|
* Updates the FSM and retrieves next state.
|
||||||
* @return whether the state was changed.
|
* @return whether the state was changed.
|
||||||
*/
|
*/
|
||||||
bool Update(unsigned int eventType, void* pEventData);
|
bool Update(unsigned int eventType, void* pEventData);
|
||||||
|
|
||||||
/**
|
|
||||||
* Verifies whether the specified state is managed by the FSM.
|
|
||||||
*/
|
|
||||||
bool IsValidState(unsigned int state) const;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests whether the state machine has finished its work.
|
* Tests whether the state machine has finished its work.
|
||||||
* @note This is state machine specific.
|
* @note This is state machine specific.
|
||||||
@ -200,7 +181,6 @@ private:
|
|||||||
unsigned int m_FirstState;
|
unsigned int m_FirstState;
|
||||||
unsigned int m_CurrState;
|
unsigned int m_CurrState;
|
||||||
unsigned int m_NextState;
|
unsigned int m_NextState;
|
||||||
StateSet m_States;
|
|
||||||
TransitionMap m_Transitions;
|
TransitionMap m_Transitions;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
/* Copyright (C) 2023 Wildfire Games.
|
/* Copyright (C) 2024 Wildfire Games.
|
||||||
* This file is part of 0 A.D.
|
* This file is part of 0 A.D.
|
||||||
*
|
*
|
||||||
* 0 A.D. is free software: you can redistribute it and/or modify
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
||||||
@ -48,6 +48,7 @@
|
|||||||
#include <miniupnpc/upnperrors.h>
|
#include <miniupnpc/upnperrors.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#include <set>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user