-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathplay_selection_fsm.h
125 lines (106 loc) · 4.3 KB
/
play_selection_fsm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#pragma once
#include "proto/parameters.pb.h"
#include "shared/constants.h"
#include "software/ai/hl/stp/play/play.h"
struct PlaySelectionFSM
{
class Halt;
class Playing;
class Stop;
class SetPlay;
class OverridePlay;
struct Update
{
Update(const std::function<void(std::unique_ptr<Play>)>& set_current_play,
const GameState& game_state, const TbotsProto::AiConfig& ai_config)
: set_current_play(set_current_play),
game_state(game_state),
ai_config(ai_config)
{
}
std::function<void(std::unique_ptr<Play>)> set_current_play;
GameState game_state;
TbotsProto::AiConfig ai_config;
};
/**
* Creates a play selection FSM
*
* @param ai_config the default play config for this play fsm
*/
explicit PlaySelectionFSM(TbotsProto::AiConfig ai_config);
/**
* Guards for whether the game state is stopped, halted, playing, or in set up
*
* @param event The PlaySelection::Update event
*
* @return whether the gamestate is stopped, halted, playing, or in set up
*/
bool gameStateStopped(const Update& event);
bool gameStateHalted(const Update& event);
bool gameStatePlaying(const Update& event);
bool gameStateSetupRestart(const Update& event);
/**
* Action to set up the OverridePlay, SetPlay, StopPlay, HaltPlay, or OffensePlay
*
* @param event The PlaySelection::Update event
*
*/
void setupSetPlay(const Update& event);
void setupStopPlay(const Update& event);
void setupHaltPlay(const Update& event);
void setupOffensePlay(const Update& event);
/**
* Action to reset the current SetPlay to none
*
* @param event The PlaySelection::Update event
*/
void resetSetPlay(const Update& event);
auto operator()()
{
using namespace boost::sml;
DEFINE_SML_STATE(SetPlay)
DEFINE_SML_STATE(Halt)
DEFINE_SML_STATE(Playing)
DEFINE_SML_STATE(Stop)
DEFINE_SML_GUARD(gameStateStopped)
DEFINE_SML_GUARD(gameStateHalted)
DEFINE_SML_GUARD(gameStatePlaying)
DEFINE_SML_GUARD(gameStateSetupRestart)
DEFINE_SML_EVENT(Update)
DEFINE_SML_ACTION(setupSetPlay)
DEFINE_SML_ACTION(setupStopPlay)
DEFINE_SML_ACTION(setupHaltPlay)
DEFINE_SML_ACTION(setupOffensePlay)
DEFINE_SML_ACTION(resetSetPlay)
return make_transition_table(
// src_state + event [guard] / action = dest_state
// Check for transitions to other states, if not then default to running the
// current play
*Halt_S + Update_E[gameStateStopped_G] / setupStopPlay_A = Stop_S,
Halt_S + Update_E[gameStatePlaying_G] / setupOffensePlay_A = Playing_S,
Halt_S + Update_E[gameStateSetupRestart_G] / setupSetPlay_A = SetPlay_S,
// Check for transitions to other states, if not then default to running the
// current play
Stop_S + Update_E[gameStateHalted_G] / setupHaltPlay_A = Halt_S,
Stop_S + Update_E[gameStatePlaying_G] / setupOffensePlay_A = Playing_S,
Stop_S + Update_E[gameStateSetupRestart_G] / setupSetPlay_A = SetPlay_S,
// Check for transitions to other states, if not then default to running the
// current play
Playing_S + Update_E[gameStateHalted_G] / setupHaltPlay_A = Halt_S,
Playing_S + Update_E[gameStateStopped_G] / setupStopPlay_A = Stop_S,
Playing_S + Update_E[gameStateSetupRestart_G] / setupSetPlay_A = SetPlay_S,
// Check for transitions to other states, if not then default to running the
// current play
SetPlay_S + Update_E[gameStateHalted_G] / (resetSetPlay_A, setupHaltPlay_A) =
Halt_S,
SetPlay_S + Update_E[gameStateStopped_G] / (resetSetPlay_A, setupStopPlay_A) =
Stop_S,
SetPlay_S + Update_E[gameStatePlaying_G] /
(resetSetPlay_A, setupOffensePlay_A) = Playing_S,
SetPlay_S + Update_E[gameStateSetupRestart_G] / setupSetPlay_A,
X + Update_E = X);
}
private:
TbotsProto::AiConfig ai_config;
std::optional<TbotsProto::PlayName> current_set_play;
};