StateMachine.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-09-05
  6. * Changed: 2021-04-23
  7. *
  8. * */
  9. #include <ls_std/logic/StateMachine.hpp>
  10. ls_std::StateMachine::StateMachine(std::string _name)
  11. : ls_std::Class("StateMachine"),
  12. name(std::move(_name))
  13. {}
  14. bool ls_std::StateMachine::addState(const std::shared_ptr<ls_std::State> &_state)
  15. {
  16. bool condition = !this->_hasState(_state->getId());
  17. if (condition)
  18. {
  19. this->states.insert({_state->getId(), _state});
  20. condition = this->_hasState(_state->getId());
  21. }
  22. return condition;
  23. }
  24. std::shared_ptr<ls_std::State> ls_std::StateMachine::getCurrentState()
  25. {
  26. return this->currentState;
  27. }
  28. std::vector<ls_std::StateId> ls_std::StateMachine::getMemory()
  29. {
  30. return this->memory;
  31. }
  32. std::string ls_std::StateMachine::getName()
  33. {
  34. return this->name;
  35. }
  36. std::unordered_map<ls_std::StateId, std::shared_ptr<ls_std::State>> ls_std::StateMachine::getStates()
  37. {
  38. return this->states;
  39. }
  40. bool ls_std::StateMachine::hasState(const ls_std::StateId &_id)
  41. {
  42. return this->_hasState(_id);
  43. }
  44. bool ls_std::StateMachine::proceed()
  45. {
  46. std::vector<ls_std::StateId> nextValidStates = this->_getNextValidStates();
  47. bool condition = nextValidStates.size() == 1;
  48. if (condition)
  49. {
  50. this->currentState = this->states[nextValidStates.at(0)];
  51. this->_remember(nextValidStates.at(0));
  52. }
  53. return condition;
  54. }
  55. void ls_std::StateMachine::setMemory(std::vector<ls_std::StateId> _memory)
  56. {
  57. this->memory = std::move(_memory);
  58. }
  59. void ls_std::StateMachine::setName(std::string _name)
  60. {
  61. this->name = std::move(_name);
  62. }
  63. bool ls_std::StateMachine::setStartState(const ls_std::StateId &_id)
  64. {
  65. bool exists = this->_hasState(_id);
  66. if (exists)
  67. {
  68. this->currentState = this->states[_id];
  69. this->_remember(_id);
  70. }
  71. return exists;
  72. }
  73. std::vector<ls_std::StateId> ls_std::StateMachine::_getNextValidStates()
  74. {
  75. std::vector<ls_std::StateId> validStates{};
  76. for (const auto &state : this->currentState->getConnectedStates())
  77. {
  78. if (state.second->isPassable())
  79. {
  80. validStates.push_back(state.second->getStateId());
  81. }
  82. }
  83. return validStates;
  84. }
  85. void ls_std::StateMachine::_remember(const ls_std::StateId &_id)
  86. {
  87. this->memory.push_back(_id);
  88. }
  89. bool ls_std::StateMachine::_hasState(const ls_std::StateId &_id)
  90. {
  91. return this->states.find(_id) != this->states.end();
  92. }