Repository navigation
Expand file tree
/
Copy pathgamestate.cpp
More file actions
159 lines (118 loc) · 5.04 KB
/
Copy pathgamestate.cpp
File metadata and controls
159 lines (118 loc) · 5.04 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "gamestate.h"
#include "QDebug"
//setup constants
const int GameState::GAME_ON = (1 << 0);
const int GameState::GAME_OFF = (1 << 1);
const int GameState::HALTED = (1 << 2);
const int GameState::KICKOFF = (1 << 3);
const int GameState::PENALTY = (1 << 4);
const int GameState::DIRECT = (1 << 5);
const int GameState::INDIRECT = (1 << 6);
//added
const int GameState::BALLPLACEMENT = (1 << 12);
const int GameState::HALF_TIME=(1 << 13);
const int GameState::PENALTY_SHOOTOUT=(1 << 14);
const int GameState::RESTART = (KICKOFF | PENALTY | DIRECT | INDIRECT | BALLPLACEMENT);
const int GameState::BLUE = (1 << 8);
const int GameState::YELLOW = (1 << 9);
const int GameState::READY = (1 << 10);
const int GameState::NOTREADY = (1 << 11);
GameState::GameState()
{
color = BLUE; state = GAME_OFF;
gametimes = GAME_ON;
yellowscore=bluescore=0;
}
int GameState::ourScore()
{
if (color == BLUE) return bluescore;
return yellowscore;
}
int GameState::oppScore()
{
if (color == YELLOW) return bluescore;
return yellowscore;
}
void GameState::init(int _color) { color = (_color == TEAM_BLUE) ? BLUE : YELLOW; }
int GameState::get() { return state; }
void GameState::set(int _state) { state = _state; }
// This is the state machine transition function. It takes the last
// ref_command as input
void GameState::transition(char ref_command) {
if (ref_command == COMM_GOAL_BLUE) bluescore++;
if (ref_command == COMM_SUBGOAL_BLUE) bluescore--;
if (ref_command == COMM_GOAL_YELLOW) yellowscore++;
if (ref_command == COMM_SUBGOAL_YELLOW) yellowscore--;
if (ref_command == COMM_HALT) {
state = HALTED; return; }
if (ref_command == COMM_STOP) {
state = GAME_OFF; return; }
if (ref_command == COMM_START) {
state = GAME_ON; return; }
if (ref_command == COMM_READY && state & NOTREADY) {
state &= ~NOTREADY; state |= READY; return; }
if (state & READY) {
state = GAME_ON; return; }
if (ref_command == COMM_PENALTY_SHOOTOUT){
gametimes = PENALTY_SHOOTOUT;
}
if (state == GAME_OFF) {
switch (ref_command) {
case COMM_KICKOFF_BLUE:
state = KICKOFF | BLUE | NOTREADY; return;
case COMM_KICKOFF_YELLOW:
state = KICKOFF | YELLOW | NOTREADY; return;
case COMM_PENALTY_BLUE:
state = PENALTY | BLUE | NOTREADY; return;
case COMM_PENALTY_YELLOW:
state = PENALTY | YELLOW | NOTREADY; return;
case COMM_DIRECT_BLUE:
state = DIRECT | BLUE | READY; return;
case COMM_DIRECT_YELLOW:
state = DIRECT | YELLOW | READY; return;
case COMM_INDIRECT_BLUE:
state = INDIRECT | BLUE | READY; return;
case COMM_INDIRECT_YELLOW:
state = INDIRECT | YELLOW | READY; return;
//added
case COMM_BALLPLACEMENT_BLUE:
state = BALLPLACEMENT | BLUE | READY; return;
case COMM_BALLPLACEMENT_YELLOW:
state = BALLPLACEMENT | YELLOW | READY; return;
case COMM_HALF_TIME:
state=HALF_TIME;return;
default: break;
}
}
}
bool GameState::gameOn() { return (state == GAME_ON); }
bool GameState::restart() { return (state & RESTART); }
bool GameState::ourRestart() { return restart() && (state & color); }
bool GameState::theirRestart() { return restart() && ! (state & color); }
bool GameState::kickoff() { return (state & KICKOFF); }
bool GameState::ourKickoff() { return kickoff() && (state & color); }
bool GameState::theirKickoff() { return kickoff() && ! (state & color); }
bool GameState::penaltyKick() { return (state & PENALTY); }
bool GameState::ourPenaltyKick() { return penaltyKick() && (state & color); }
bool GameState::theirPenaltyKick() { return penaltyKick() && ! (state & color); }
bool GameState::directKick() { return (state & DIRECT); }
bool GameState::ourDirectKick() { return directKick() && (state & color); }
bool GameState::theirDirectKick() { return directKick() && ! (state & color); }
bool GameState::indirectKick() { return (state & INDIRECT); }
bool GameState::ourIndirectKick() { return indirectKick() && (state & color) ;}
bool GameState::theirIndirectKick() { return indirectKick() && ! (state & color); }
bool GameState::freeKick() { return directKick() || indirectKick(); }
bool GameState::ourFreeKick() { return ourDirectKick() || ourIndirectKick(); }
bool GameState::theirFreeKick() { return theirDirectKick() || theirIndirectKick(); }
//added
bool GameState::ballPlacement() { return (state & BALLPLACEMENT); }
bool GameState::ourBallPlacement() { return ballPlacement() && (state & color); }
bool GameState::theirBallPlacement() { return ballPlacement() && ! (state & color); }
bool GameState::halfTimeLineUp(){return state & HALF_TIME;}
bool GameState::penalty_shootout() {
return gametimes & PENALTY_SHOOTOUT;}
bool GameState::canMove() { return (state != HALTED); }
bool GameState::allowedNearBall() {
return gameOn() || (state & color); }
bool GameState::canKickBall() {
return gameOn() || (ourRestart() && (state & READY)); }