gusimplewhiteboard
GCGameState.hpp
Go to the documentation of this file.
1/*
2 * /GCGameState.h
3 * gusimplewhiteboard / clfsm
4 *
5 * Created by Rene Hexel on 25/03/13.
6 * Copyright (c) 2013, 2014 Rene Hexel. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer in the documentation and/or other materials
18 * provided with the distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgement:
22 *
23 * This product includes software developed by Rene Hexel.
24 *
25 * 4. Neither the name of the author nor the names of contributors
26 * may be used to endorse or promote products derived from this
27 * software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
33 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
34 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
35 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
36 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
37 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
38 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
39 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * -----------------------------------------------------------------------
42 * This program is free software; you can redistribute it and/or
43 * modify it under the above terms or under the terms of the GNU
44 * General Public License as published by the Free Software Foundation;
45 * either version 2 of the License, or (at your option) any later version.
46 *
47 * This program is distributed in the hope that it will be useful,
48 * but WITHOUT ANY WARRANTY; without even the implied warranty of
49 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50 * GNU General Public License for more details.
51 *
52 * You should have received a copy of the GNU General Public License
53 * along with this program; if not, see http://www.gnu.org/licenses/
54 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
55 * Fifth Floor, Boston, MA 02110-1301, USA.
56 *
57 */
58#ifndef GCGameState_DEFINED
59#define GCGameState_DEFINED
60
61#include <cstdlib>
62#include <stdint.h>
63#include <sys/types.h>
64#include <sstream>
65#include <gu_util.h>
66
67#define SPL_NUM_PLAYERS 6
68#define SPL_NUM_TEAMS 2
69
70#define DEFAULT_PLAYER_NUMBER 2
71
72#define MIPAL_TEAM_NO 11 // MiPal's SPL assigned Team number, persisting from 2013)
73
74#define kGSTeamHome "HomeTeam"
75#define kGSTeamAway "AwayTeam"
76
77#define kGSFirstHalf "FirstHalf"
78#define kGSSecondHalf "SecondHalf"
79
80#define kGSNormalGame "NormalGame"
81#define kGSMixedTeam "MixedTeams"
82#define kGSDropIn "Drop In"
83
84#define kGSPenaltyShots "PenaltyShots"
85#define kGSOverTime "OverTime"
86#define kGSTimeOut "TimeOut"
87
88#define kGSInitialReceived "InitialReceived"
89#define kGSReadyReceived "ReadyReceived"
90#define kGSSetReceived "SetReceived"
91#define kGSPlayingReceived "PlayingReceived"
92#define kGSFinishedReceived "FinishedReceived"
93
94#define kGSInitial "GSInitial"
95#define kGSReady "GSReady"
96#define kGSSet "GSSet"
97#define kGSPlaying "GSPlaying"
98#define kGSFinished "GSFinished"
99
100#define kGSNoSetPlay "GSNoSetPlay"
101#define kGSGoalFreeKick "GSGoalFreeKick"
102#define kGSPushingFreeKick "GSPushingFreeKick"
103#define kGSCornerKick "GSCornerKick"
104#define kGSKickIn "GSKickIn"
105
106
107#define kGSNoPenalty "NoPenalty"
108#define kGSIllegalBallContact "IllegalBallContact"
109#define kGSPlayerPushing "PlayerPushing"
110#define kGSIllegalMotionInSet "IllegalMotionInSet"
111#define kGSInactivePlayer "InactivePlayer"
112#define kGSIllegalDefender "IllegalDefender"
113#define kGSLeavingTheField "LeavingTheField"
114#define kGSKickOffGoal "KickOffGoal"
115#define kGSRequestForPickup "RequestForPickup"
116#define kGSLocalGameStuck "LocalGameStuck"
117#define kGSIllegalPositioning "IllegalPositioning"
118#define kGSSubstitutionPenalty "SubstitutionPenalty"
119#define kGSManualPenalty "ManualPenalty"
120
121
122#define kNoGSsignal "NoGSsignal"
123#define kGSOurGoalSignalPushed "GSOurGoalSignalPushed"
124#define kGSTheirGoalSignalPushed "GSTheirGoalSignalPushed"
125#define kGSBlueKickOffSignalPushed "GSBlueKickOffSignalPushed"
126#define kGSRedKickOffSignalPushed "GSRedKickOffSignalPushed"
127
128namespace guWhiteboard
129{
130 // Variables prescribed by the Game Controller
131
133 enum TeamColors : int8_t
135
137 static const char *teamColorsStr[NUM_JERSEY_COLORS] =
138 { "Blue", "Red", "Yellow", "Black", "White", "Green", "Orange", "Purple", "Brown", "Gray" };
139
141 enum CompetitionType : int8_t
142 // { NormalCompetition, MixedTeam, GeneralPenaltyKick, NUM_COMPETITION_TYPES };
144
146 enum CompetitionPhase : int8_t
148
150 enum GamePhase : int8_t
152
154 enum GameState : int8_t
156
158 enum SetPlay : int8_t
160
162 enum PenaltyTypes : int8_t
163 // OUR OLD FORMAT (CoachMotion removed)
164 // { NoPenalty = 0, BallHolding, PlayerPushing, IllegalMotionInSet, InactivePlayer, IllegalDefender, LeavingTheField,
165 // KickOffGoal, RequestForPickup, CoachMotion, PenaltySubstitute = 14, ManualButtonPenalty = 15};
175 LocalGameStuck, // New Penalty Code
176 IllegalPositioning, // New Penalty Code
178 ManualPenalty = 15
179 };
180
182 {
183 PenaltyTypes penalty; // penalty state of the player
184 uint8_t secsTillUnpenalised; // estimate of time till unpenalised
185 };
186
187
188 // Definitions used internally to make sense of the game.
189
191 enum HomeAway : int8_t
192 { Home=0, Away=1 };
193
195 enum GameHalf : int8_t
197
199 enum BallOut : int8_t
201
205
209
210
211
216
217 private:
219 RobotPenaltyState _RobotGCPenaltyState[SPL_NUM_TEAMS][SPL_NUM_PLAYERS];
220
222 int16_t _score[SPL_NUM_TEAMS];
223
225 PROPERTY(GameHalf, theGCHalf)
226
227
228 PROPERTY(CompetitionType, theGCCompetitionType)
229
230
231 PROPERTY(CompetitionPhase, theGCCompetitionPhase)
232
233
234 PROPERTY(GamePhase, theGCGamePhase)
235
236
237 PROPERTY(GameState, theGCGameState)
238
239// /// GC GameControllerCommand
240// PROPERTY(GameControllerCommand, theGCCommand)
241
242
243 PROPERTY(SetPlay, theGCSetPlayInProgress)
244
245
246 PROPERTY(HomeAway, theGCTeamWithFreeKick)
247
248
249 PROPERTY(int16_t, secondsReminingInHalf )
250
251
252 PROPERTY(HomeAway, homeaway )
253
254 public:
257 GameHalf theGCHalf = FirstHalf,
258 CompetitionType theGCCompetitionType = NormalCompetition,
259 CompetitionPhase theGCCompetitionPhase = RoundRobin,
260 GamePhase theGCGamePhase = NormalPhase,
261 GameState theGCGameState = Initial,
262 SetPlay theGCSetPlayInProgress = NoSetPlay,
263 HomeAway theGCTeamWithFreeKick = Home,
264 int16_t secondsReminingInHalf = Home,
265 HomeAway homeaway = Home
266 ):
267 _theGCHalf(theGCHalf),
268 _theGCCompetitionType(theGCCompetitionType),
269 _theGCCompetitionPhase(theGCCompetitionPhase),
270 _theGCGamePhase(theGCGamePhase),
271 _theGCGameState(theGCGameState),
272 _theGCSetPlayInProgress(theGCSetPlayInProgress),
273 _theGCTeamWithFreeKick(theGCTeamWithFreeKick),
274 _secondsReminingInHalf(secondsReminingInHalf),
275 _homeaway(homeaway)
276 {
277 for (int i=0; i< SPL_NUM_TEAMS; i++) {
278 for (int j=0; j< SPL_NUM_PLAYERS; j++) {
279 _RobotGCPenaltyState[i][j].penalty = NoPenalty;
280 _RobotGCPenaltyState[i][j].secsTillUnpenalised = 0;
281 }
282 }
283 for (int i=0; i< SPL_NUM_TEAMS; i++) _score[i] = 0;
284 }
285
287 GCGameState(const std::string &names) { from_string(names); }
288
290 GCGameState(const GCGameState &other):
291 _theGCHalf(other._theGCHalf),
292 _theGCCompetitionType(other._theGCCompetitionType),
293 _theGCCompetitionPhase(other._theGCCompetitionPhase),
294 _theGCGamePhase(other._theGCGamePhase),
295 _theGCGameState(other._theGCGameState),
296 _theGCSetPlayInProgress(other._theGCSetPlayInProgress),
297 _theGCTeamWithFreeKick(other._theGCTeamWithFreeKick),
298 _secondsReminingInHalf(other._secondsReminingInHalf),
299 _homeaway(other._homeaway)
300 {
301 for (int i=0; i< SPL_NUM_TEAMS; i++) {
302 for (int j=0; j< SPL_NUM_PLAYERS; j++) {
303 _RobotGCPenaltyState[i][j].penalty = other._RobotGCPenaltyState[i][j].penalty;
304 _RobotGCPenaltyState[i][j].secsTillUnpenalised = other._RobotGCPenaltyState[i][j].secsTillUnpenalised;
305 }
306 }
307 for (int i=0; i< SPL_NUM_TEAMS; i++) _score[i]=other._score[i];
308 }
309
311 const GCGameState &operator=(const GCGameState &other) { memcpy(this, &other, sizeof(*this)); return *this; }
312
314 void setPenaltyVectors(const RobotPenaltyState robotGCPenaltyState[SPL_NUM_TEAMS][SPL_NUM_PLAYERS]) {
315 for (int i=0; i< SPL_NUM_TEAMS; i++) {
316 for (int j=0; j< SPL_NUM_PLAYERS; j++) {
317 _RobotGCPenaltyState[i][j].penalty=robotGCPenaltyState[i][j].penalty;
318 _RobotGCPenaltyState[i][j].secsTillUnpenalised=robotGCPenaltyState[i][j].secsTillUnpenalised;
319 }
320 }
321 }
322
324 void setScore(int16_t goalsByHome, int16_t goalsByAway) {
325 _score[Home] = goalsByHome;
326 _score[Away] = goalsByAway;
327 }
328
330 int16_t getScoreHome() { return _score[Home]; }
331
333 int16_t getScoreAway() { return _score[Away]; }
334
336 int16_t getOurScore() { return _score[_homeaway]; }
337
339 int16_t getTheirScore() {
340 if (Home == _homeaway ) return getScoreAway();
341 else return getScoreHome();
342 }
343
345 RobotPenaltyState myPenaltyStateIs (int PlayerNumber)
346 {
347 // the index in the structure starts at 0 but robot numbers start with 1
348 if ((0<PlayerNumber) && (PlayerNumber<=SPL_NUM_PLAYERS ))
349 return static_cast<RobotPenaltyState>(_RobotGCPenaltyState[_homeaway][PlayerNumber-1]);
350 RobotPenaltyState noPenalty;
351 noPenalty.penalty = NoPenalty;
352 noPenalty.secsTillUnpenalised = 0;
353 return noPenalty;
354 }
355
357 PenaltyTypes myPenaltyIs (int PlayerNumber)
358 {
359 // the index in the structure starts at 0 but robot numbers start with 1
360 if ((0<PlayerNumber) && (PlayerNumber<=SPL_NUM_PLAYERS ))
361 return static_cast<PenaltyTypes>(_RobotGCPenaltyState[_homeaway][PlayerNumber-1].penalty);
362 return NoPenalty;
363 }
364
366 bool amIPenalized(int PlayerNumber)
367 {
368 if (NoPenalty==myPenaltyIs(PlayerNumber))
369 return false;
370 else return true;
371 }
372
374 void setMyPenalty(int PlayerNumber, PenaltyTypes thePenalty, uint8_t duration = 0) {
375 _RobotGCPenaltyState[_homeaway][PlayerNumber-1].penalty=thePenalty;
376 _RobotGCPenaltyState[_homeaway][PlayerNumber-1].secsTillUnpenalised = duration;
377 }
378
380 std::string description() {
381 std::ostringstream ss;
382 if ( Home == homeaway() ) ss << kGSTeamHome << ","; else ss << kGSTeamAway << ",";
383 switch(int (_theGCSetPlayInProgress)) {
384 case NoSetPlay:
385 ss << kGSNoSetPlay << ",";
386 break;
387 case GoalFreeKick:
388 ss << kGSGoalFreeKick << ",";
389 break;
390 case PushingFreeKick:
391 ss << kGSPushingFreeKick << ",";
392 break;
393 case CornerKick:
394 ss << kGSCornerKick << ",";
395 break;
396 case KickIn:
397 ss << kGSKickIn << ",";
398 break;
399 default:
400 ss << _theGCSetPlayInProgress << ",";
401 }
402
403
404 ss << "Next kick to: ";
405 if ( Home == theGCTeamWithFreeKick() ) ss << kGSTeamHome << ","; else ss << kGSTeamAway << ",";
406 if ( FirstHalf == theGCHalf() ) ss << kGSFirstHalf << ","; else ss << kGSSecondHalf << ",";
407 if ( NormalCompetition == theGCCompetitionType()) ss << kGSNormalGame << ","; else ss << kGSMixedTeam << ",";
408
409 switch(int (_theGCGameState)) {
410 case Initial:
411 ss << kGSInitial << ",";
412 break;
413 case Ready:
414 ss << kGSReady << ",";
415 break;
416 case Set:
417 ss << kGSSet << ",";
418 break;
419 case Playing:
420 ss << kGSPlaying << ",";
421 break;
422 case Finished:
423 ss << kGSFinished << ",";
424 break;
425 default:
426 ss << _theGCGameState << ",";
427 }
428 for (int i=0; i< SPL_NUM_PLAYERS; i++)
429 {
430 switch (_RobotGCPenaltyState[_homeaway][i].penalty ) {
431 case NoPenalty :
432 ss << kGSNoPenalty << ", - ," ;
433 break;
434 case IllegalBallContact :
435 ss << kGSIllegalBallContact << ",";
436 ss << " (" << _RobotGCPenaltyState[_homeaway][i].secsTillUnpenalised << ") ,";
437 break;
438 case PlayerPushing :
439 ss << kGSPlayerPushing<<",";
440 ss << " (" << _RobotGCPenaltyState[_homeaway][i].secsTillUnpenalised << ") ,";
441 break;
442 case IllegalMotionInSet :
443 ss << kGSIllegalMotionInSet<<",";
444 ss << " (" << _RobotGCPenaltyState[_homeaway][i].secsTillUnpenalised << ") ,";
445 break;
446 case InactivePlayer :
447 ss << kGSInactivePlayer<<",";
448 ss << " (" << _RobotGCPenaltyState[_homeaway][i].secsTillUnpenalised << ") ,";
449 break;
450 case IllegalDefender :
451 ss << kGSIllegalDefender<<",";
452 ss << " (" << _RobotGCPenaltyState[_homeaway][i].secsTillUnpenalised << ") ,";
453 break;
454 case LeavingTheField :
455 ss << kGSLeavingTheField<<",";
456 ss << " (" << _RobotGCPenaltyState[_homeaway][i].secsTillUnpenalised << ") ,";
457 break;
458 case KickOffGoal :
459 ss << kGSKickOffGoal<<",";
460 ss << " (" << _RobotGCPenaltyState[_homeaway][i].secsTillUnpenalised << ") ,";
461 break;
462 case RequestForPickUp :
463 ss << kGSRequestForPickup<<",";
464 ss << " (" << _RobotGCPenaltyState[_homeaway][i].secsTillUnpenalised << ") ,";
465 break;
466 case LocalGameStuck :
467 ss << kGSLocalGameStuck<<",";
468 ss << " (" << _RobotGCPenaltyState[_homeaway][i].secsTillUnpenalised << ") ,";
469 break;
470 case IllegalPositioning :
471 ss << kGSIllegalPositioning<<",";
472 ss << " (" << _RobotGCPenaltyState[_homeaway][i].secsTillUnpenalised << ") ,";
473 break;
475 ss << kGSSubstitutionPenalty<<",";
476 ss << " (" << _RobotGCPenaltyState[_homeaway][i].secsTillUnpenalised << ") ,";
477 break;
478 case ManualPenalty :
479 ss << kGSManualPenalty << ", - ," ;
480 break;
481 }
482 }
483 ss << getScoreHome() << "," << getScoreAway() << ",";
484 return ss.str();
485 }
486
488 void from_string(const std::string &str) {
489 std::istringstream iss(str);
490 std::string token;
491 getline(iss, token, ',');
492 }
493 };
494}
495#endif // GCGameState_DEFINED
#define SPL_NUM_PLAYERS
Definition: GCGameState.hpp:67
#define kGSNormalGame
Definition: GCGameState.hpp:80
#define kGSIllegalMotionInSet
#define kGSSecondHalf
Definition: GCGameState.hpp:78
#define kGSPlayerPushing
#define kGSIllegalBallContact
#define kGSTeamHome
Definition: GCGameState.hpp:74
#define kGSNoPenalty
#define kGSSubstitutionPenalty
#define kGSRequestForPickup
#define kGSInitial
Definition: GCGameState.hpp:94
#define kGSFinished
Definition: GCGameState.hpp:98
#define kGSInactivePlayer
#define kGSTeamAway
Definition: GCGameState.hpp:75
#define kGSReady
Definition: GCGameState.hpp:95
#define kGSIllegalPositioning
#define kGSKickIn
#define kGSSet
Definition: GCGameState.hpp:96
#define kGSFirstHalf
Definition: GCGameState.hpp:77
#define kGSPlaying
Definition: GCGameState.hpp:97
#define kGSCornerKick
#define kGSMixedTeam
Definition: GCGameState.hpp:81
#define kGSNoSetPlay
#define kGSGoalFreeKick
#define kGSPushingFreeKick
#define kGSKickOffGoal
#define kGSManualPenalty
#define kGSIllegalDefender
#define SPL_NUM_TEAMS
Definition: GCGameState.hpp:68
#define kGSLocalGameStuck
#define kGSLeavingTheField
WB Class to record game state received from the Game Controller.
/file APM_Interface.h
PenaltyTypes
Penalty types.
SetPlay
Set play codes.
GameContollerSignal
major game event signals (goals, kick-offs)
@ GSRedKickOffSignalPushed
@ GSBlueKickOffSignalPushed
GameState
High level Game Controller states.
TeamColors
Team Colours.
HomeAway
Used to nominate which team is home/away and to decouple game state from team specifics.
CompetitionType
Type of the competition.
CompetitionPhase
Phase of the competition.
GameHalf
Soccer games comprise of two halves.
BallOut
Who kicked the ball out.
GameControllerCommand
received command from the Game Controller
GamePhase
Phase of the game.