-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathgame_controller_state_message.rs
More file actions
477 lines (444 loc) · 16.8 KB
/
Copy pathgame_controller_state_message.rs
File metadata and controls
477 lines (444 loc) · 16.8 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
use std::{
convert::{TryFrom, TryInto},
ffi::c_char,
mem::size_of,
ptr::read,
time::Duration,
};
use color_eyre::{Report, Result, eyre::bail};
use ros_z::Message;
use serde::{Deserialize, Serialize};
use crate::{
HULKS_TEAM_NUMBER, NONE_TEAM_NUMBER, PlayerNumber,
bindings::{
COMPETITION_TYPE_LARGE, COMPETITION_TYPE_MIDDLE, COMPETITION_TYPE_SMALL,
GAME_PHASE_EXTRA_TIME, GAME_PHASE_NORMAL, GAME_PHASE_PENALTY_SHOOT_OUT, GAME_PHASE_TIMEOUT,
GAMECONTROLLER_STRUCT_HEADER, GAMECONTROLLER_STRUCT_VERSION, MAX_NUM_PLAYERS,
PENALTY_BALL_HOLDING, PENALTY_CAUTIONED, PENALTY_ILLEGAL_POSITIONING,
PENALTY_INCAPABLE_ROBOT, PENALTY_LEAVING_THE_FIELD, PENALTY_LOCAL_GAME_STUCK,
PENALTY_MOTION_IN_SET, PENALTY_MOTION_IN_STOP, PENALTY_NONE, PENALTY_PICK_UP,
PENALTY_PLAYING_WITH_ARMS_HANDS, PENALTY_PUSHING, PENALTY_SENT_OFF, PENALTY_SUBSTITUTE,
RoboCupGameControlData, RobotInfo, SET_PLAY_CORNER_KICK, SET_PLAY_DIRECT_FREE_KICK,
SET_PLAY_GOAL_KICK, SET_PLAY_INDIRECT_FREE_KICK, SET_PLAY_NONE, SET_PLAY_PENALTY_KICK,
SET_PLAY_THROW_IN, STATE_FINISHED, STATE_INITIAL, STATE_PLAYING, STATE_READY, STATE_SET,
TEAM_BLACK, TEAM_BLUE, TEAM_BROWN, TEAM_GRAY, TEAM_GREEN, TEAM_ORANGE, TEAM_PURPLE,
TEAM_RED, TEAM_WHITE, TEAM_YELLOW,
},
};
#[derive(Clone, Debug, Deserialize, Serialize, Message)]
pub struct GameControllerStateMessage {
pub competition_type: CompetitionType,
pub stopped: bool,
pub game_phase: GamePhase,
pub game_state: GameState,
pub sub_state: Option<SubState>,
pub half: Half,
pub remaining_time_in_half: Duration,
pub secondary_time: Duration,
pub hulks_team: TeamState,
pub opponent_team: TeamState,
pub kicking_team: Option<Team>,
pub hulks_team_is_home_after_coin_toss: bool,
}
impl TryFrom<&[u8]> for GameControllerStateMessage {
type Error = Report;
fn try_from(buffer: &[u8]) -> Result<Self> {
if buffer.len() < size_of::<RoboCupGameControlData>() {
bail!("buffer too small");
}
let message = unsafe { read(buffer.as_ptr() as *const RoboCupGameControlData) };
message.try_into()
}
}
impl TryFrom<RoboCupGameControlData> for GameControllerStateMessage {
type Error = Report;
fn try_from(message: RoboCupGameControlData) -> Result<Self> {
if message.header[0] != GAMECONTROLLER_STRUCT_HEADER[0] as c_char
&& message.header[1] != GAMECONTROLLER_STRUCT_HEADER[1] as c_char
&& message.header[2] != GAMECONTROLLER_STRUCT_HEADER[2] as c_char
&& message.header[3] != GAMECONTROLLER_STRUCT_HEADER[3] as c_char
{
bail!("unexpected header: {:?}", message.header);
}
if message.version != GAMECONTROLLER_STRUCT_VERSION {
bail!("unexpected version: {}", message.version);
}
let (hulks_team_index, opponent_team_index) =
match (message.teams[0].teamNumber, message.teams[1].teamNumber) {
(HULKS_TEAM_NUMBER, _) => (0, 1),
(_, HULKS_TEAM_NUMBER) => (1, 0),
_ => {
bail!(
"failed to find HULKs team, teams were {:?} and {:?}",
message.teams[0],
message.teams[1]
);
}
};
const MAXIMUM_NUMBER_OF_PENALTY_SHOOTS: u8 = 16;
if message.teams[hulks_team_index].penaltyShot >= MAXIMUM_NUMBER_OF_PENALTY_SHOOTS {
bail!(
"unexpected penalty shoot index for team HULKs: {:?}",
message.teams[hulks_team_index].penaltyShot
);
}
if message.teams[opponent_team_index].penaltyShot >= MAXIMUM_NUMBER_OF_PENALTY_SHOOTS {
bail!(
"unexpected penalty shoot index for opponent team: {:?}",
message.teams[opponent_team_index].penaltyShot
);
}
let hulks_penalty_shoots = (0..message.teams[hulks_team_index].penaltyShot)
.map(|shoot_index| {
if message.teams[hulks_team_index].singleShots & (1 << shoot_index) != 0 {
PenaltyShoot::Successful
} else {
PenaltyShoot::Unsuccessful
}
})
.collect();
let opponent_penalty_shoots = (0..message.teams[opponent_team_index].penaltyShot)
.map(|shoot_index| {
if message.teams[opponent_team_index].singleShots & (1 << shoot_index) != 0 {
PenaltyShoot::Successful
} else {
PenaltyShoot::Unsuccessful
}
})
.collect();
if message.playersPerTeam > MAX_NUM_PLAYERS {
bail!(
"unexpected number of players per team. Expected: {MAX_NUM_PLAYERS}. Got: {}",
message.playersPerTeam
);
}
let hulks_players = (0..MAX_NUM_PLAYERS)
.map(|player_index| {
message.teams[hulks_team_index].players[player_index as usize].try_into()
})
.collect::<Result<Vec<_>>>()?;
let opponent_players = (0..MAX_NUM_PLAYERS)
.map(|player_index| {
message.teams[opponent_team_index].players[player_index as usize].try_into()
})
.collect::<Result<Vec<_>>>()?;
Ok(GameControllerStateMessage {
competition_type: CompetitionType::try_from(message.competitionType)?,
stopped: match message.stopped {
0 => false,
1 => true,
_ => {
bail!("unexpected stopped value: {}", message.stopped);
}
},
game_phase: GamePhase::try_from(message.gamePhase, message.kickingTeam)?,
game_state: GameState::try_from(message.state)?,
sub_state: SubState::try_from(message.setPlay)?,
half: message.firstHalf.try_into()?,
remaining_time_in_half: Duration::from_secs(message.secsRemaining.max(0).try_into()?),
secondary_time: Duration::from_secs(message.secondaryTime.max(0).try_into()?),
hulks_team: TeamState {
team_number: message.teams[hulks_team_index].teamNumber,
field_player_color: message.teams[hulks_team_index]
.fieldPlayerColour
.try_into()?,
goal_keeper_color: message.teams[hulks_team_index]
.goalkeeperColour
.try_into()?,
goal_keeper_player_number: match message.teams[hulks_team_index].goalkeeper {
0 => None,
1 => Some(PlayerNumber::One),
2 => Some(PlayerNumber::Two),
3 => Some(PlayerNumber::Three),
4 => Some(PlayerNumber::Four),
5 => Some(PlayerNumber::Five),
_ => {
eprintln!(
"unexpected hulks goal keeper player number {}, defaulting to None",
message.teams[hulks_team_index].goalkeeper
);
None
}
},
score: message.teams[hulks_team_index].score,
penalty_shoot_index: message.teams[hulks_team_index].penaltyShot,
penalty_shoots: hulks_penalty_shoots,
remaining_amount_of_messages: message.teams[hulks_team_index].messageBudget,
players: hulks_players,
},
opponent_team: TeamState {
team_number: message.teams[opponent_team_index].teamNumber,
field_player_color: message.teams[opponent_team_index]
.fieldPlayerColour
.try_into()?,
goal_keeper_color: message.teams[opponent_team_index]
.goalkeeperColour
.try_into()?,
goal_keeper_player_number: match message.teams[opponent_team_index].goalkeeper {
0 => None,
1 => Some(PlayerNumber::One),
2 => Some(PlayerNumber::Two),
3 => Some(PlayerNumber::Three),
4 => Some(PlayerNumber::Four),
5 => Some(PlayerNumber::Five),
_ => {
eprintln!(
"unexpected opponent goal keeper player number {}, defaulting to None",
message.teams[opponent_team_index].goalkeeper
);
None
}
},
score: message.teams[opponent_team_index].score,
penalty_shoot_index: message.teams[opponent_team_index].penaltyShot,
penalty_shoots: opponent_penalty_shoots,
remaining_amount_of_messages: message.teams[opponent_team_index].messageBudget,
players: opponent_players,
},
kicking_team: Team::try_from(message.kickingTeam).ok(),
hulks_team_is_home_after_coin_toss: hulks_team_index == 0,
})
}
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize, Message)]
pub enum CompetitionType {
Small,
Middle,
Large,
}
impl CompetitionType {
fn try_from(competition_type: u8) -> Result<Self> {
match competition_type {
COMPETITION_TYPE_SMALL => Ok(CompetitionType::Small),
COMPETITION_TYPE_MIDDLE => Ok(CompetitionType::Middle),
COMPETITION_TYPE_LARGE => Ok(CompetitionType::Large),
_ => {
bail!("unexpected competition type");
}
}
}
}
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq, Message)]
pub enum GamePhase {
#[default]
Normal,
PenaltyShootout {
kicking_team: Team,
},
Extratime,
Timeout,
}
impl GamePhase {
fn try_from(game_phase: u8, kicking_team: u8) -> Result<Self> {
let team = if kicking_team == HULKS_TEAM_NUMBER {
Team::Hulks
} else {
Team::Opponent
};
match game_phase {
GAME_PHASE_NORMAL => Ok(GamePhase::Normal),
GAME_PHASE_PENALTY_SHOOT_OUT => Ok(GamePhase::PenaltyShootout { kicking_team: team }),
GAME_PHASE_EXTRA_TIME => Ok(GamePhase::Extratime),
GAME_PHASE_TIMEOUT => Ok(GamePhase::Timeout),
_ => {
bail!("unexpected game phase");
}
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize, Message)]
pub enum GameState {
Initial,
Ready,
Set,
Playing,
Finished,
}
impl GameState {
fn try_from(game_state: u8) -> Result<Self> {
match game_state {
STATE_INITIAL => Ok(GameState::Initial),
STATE_READY => Ok(GameState::Ready),
STATE_SET => Ok(GameState::Set),
STATE_PLAYING => Ok(GameState::Playing),
STATE_FINISHED => Ok(GameState::Finished),
_ => {
bail!("unexpected game state");
}
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize, Message)]
pub enum Team {
Hulks,
Opponent,
}
impl TryFrom<u8> for Team {
type Error = Report;
fn try_from(team_number: u8) -> Result<Self> {
let team = if team_number == NONE_TEAM_NUMBER {
return Err(Report::msg("kicking team is none"));
} else if team_number == HULKS_TEAM_NUMBER {
Team::Hulks
} else {
Team::Opponent
};
Ok(team)
}
}
#[derive(Default, Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Message)]
pub enum SubState {
#[default]
DirectFreeKick,
IndirectFreeKick,
PenaltyKick,
ThrowIn,
GoalKick,
CornerKick,
}
impl SubState {
fn try_from(sub_state: u8) -> Result<Option<Self>> {
match sub_state {
SET_PLAY_NONE => Ok(None),
SET_PLAY_DIRECT_FREE_KICK => Ok(Some(SubState::DirectFreeKick)),
SET_PLAY_INDIRECT_FREE_KICK => Ok(Some(SubState::IndirectFreeKick)),
SET_PLAY_PENALTY_KICK => Ok(Some(SubState::PenaltyKick)),
SET_PLAY_THROW_IN => Ok(Some(SubState::ThrowIn)),
SET_PLAY_GOAL_KICK => Ok(Some(SubState::GoalKick)),
SET_PLAY_CORNER_KICK => Ok(Some(SubState::CornerKick)),
_ => {
bail!("unexpected sub state");
}
}
}
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize, Message)]
pub enum Half {
First,
Second,
}
impl TryFrom<u8> for Half {
type Error = Report;
fn try_from(half: u8) -> Result<Self> {
match half {
1 => Ok(Half::First),
0 => Ok(Half::Second),
_ => {
bail!("unexpected half");
}
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize, Message)]
pub struct TeamState {
pub team_number: u8,
pub field_player_color: TeamColor,
pub goal_keeper_color: TeamColor,
pub goal_keeper_player_number: Option<PlayerNumber>,
pub score: u8,
pub penalty_shoot_index: u8,
pub penalty_shoots: Vec<PenaltyShoot>,
pub remaining_amount_of_messages: u16,
pub players: Vec<Player>,
}
#[derive(Clone, Debug, Deserialize, Serialize, Message)]
pub enum TeamColor {
Blue,
Red,
Yellow,
Black,
White,
Green,
Orange,
Purple,
Brown,
Gray,
}
impl TryFrom<u8> for TeamColor {
type Error = Report;
fn try_from(team_color: u8) -> Result<Self> {
match team_color {
TEAM_BLUE => Ok(TeamColor::Blue),
TEAM_RED => Ok(TeamColor::Red),
TEAM_YELLOW => Ok(TeamColor::Yellow),
TEAM_BLACK => Ok(TeamColor::Black),
TEAM_WHITE => Ok(TeamColor::White),
TEAM_GREEN => Ok(TeamColor::Green),
TEAM_ORANGE => Ok(TeamColor::Orange),
TEAM_PURPLE => Ok(TeamColor::Purple),
TEAM_BROWN => Ok(TeamColor::Brown),
TEAM_GRAY => Ok(TeamColor::Gray),
_ => {
bail!("unexpected team color");
}
}
}
}
#[derive(Clone, Debug, Deserialize, Serialize, Message)]
pub enum PenaltyShoot {
Successful,
Unsuccessful,
}
#[derive(Clone, Debug, Deserialize, Serialize, Message)]
pub struct Player {
pub penalty: Option<Penalty>,
pub caution: u8,
}
impl TryFrom<RobotInfo> for Player {
type Error = Report;
fn try_from(player: RobotInfo) -> Result<Self> {
let remaining = Duration::from_secs(player.secsTillUnpenalised.into());
Ok(Self {
penalty: Penalty::try_from(remaining, player.penalty)?,
caution: player.cautions,
})
}
}
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Message)]
pub enum Penalty {
IllegalPosition { remaining: Duration },
MotionInSet { remaining: Duration },
MotionInStop { remaining: Duration },
LocalGameStuck { remaining: Duration },
IncapableRobot { remaining: Duration },
PickUp { remaining: Duration },
BallHolding { remaining: Duration },
LeavingTheField { remaining: Duration },
PlayingWithArmsHands { remaining: Duration },
Pushing { remaining: Duration },
Cautioned { remaining: Duration },
SentOff { remaining: Duration },
Substitute { remaining: Duration },
}
impl Penalty {
fn try_from(remaining: Duration, penalty: u8) -> Result<Option<Self>> {
match penalty {
PENALTY_NONE => Ok(None),
PENALTY_ILLEGAL_POSITIONING => Ok(Some(Penalty::IllegalPosition { remaining })),
PENALTY_MOTION_IN_SET => Ok(Some(Penalty::MotionInSet { remaining })),
PENALTY_MOTION_IN_STOP => Ok(Some(Penalty::MotionInStop { remaining })),
PENALTY_LOCAL_GAME_STUCK => Ok(Some(Penalty::LocalGameStuck { remaining })),
PENALTY_INCAPABLE_ROBOT => Ok(Some(Penalty::IncapableRobot { remaining })),
PENALTY_PICK_UP => Ok(Some(Penalty::PickUp { remaining })),
PENALTY_BALL_HOLDING => Ok(Some(Penalty::BallHolding { remaining })),
PENALTY_LEAVING_THE_FIELD => Ok(Some(Penalty::LeavingTheField { remaining })),
PENALTY_PLAYING_WITH_ARMS_HANDS => {
Ok(Some(Penalty::PlayingWithArmsHands { remaining }))
}
PENALTY_PUSHING => Ok(Some(Penalty::Pushing { remaining })),
PENALTY_CAUTIONED => Ok(Some(Penalty::Cautioned { remaining })),
PENALTY_SENT_OFF => Ok(Some(Penalty::SentOff { remaining })),
PENALTY_SUBSTITUTE => Ok(Some(Penalty::Substitute { remaining })),
_ => {
bail!("unexpected penalty type");
}
}
}
}
impl Default for Penalty {
fn default() -> Self {
Self::PickUp {
remaining: Duration::ZERO,
}
}
}