@@ -8,6 +8,7 @@ use ml::{
88use nalgebra:: Point2 ;
99
1010use crate :: {
11+ behavior:: roles:: LostBallSearchTimer ,
1112 core:: config:: showtime:: PlayerConfig ,
1213 motion:: walking_engine:: Gait ,
1314 nao:: { NaoManager , Priority , RobotInfo } ,
@@ -17,18 +18,20 @@ use crate::{
1718
1819use super :: {
1920 behaviors:: {
20- CatchFall , CatchFallBehaviorPlugin , ObserveBehaviorPlugin , RlStrikerSearchBehaviorPlugin ,
21- Sitting , SittingBehaviorPlugin , Stand , StandBehaviorPlugin , StandLookAt ,
22- StandLookAtBehaviorPlugin , Standup , StandupBehaviorPlugin , StartUpBehaviorPlugin ,
23- VisualReferee , VisualRefereeBehaviorPlugin , WalkBehaviorPlugin , WalkToBallBehaviorPlugin ,
24- WalkToBehaviorPlugin , WalkToSet , WalkToSetBehaviorPlugin ,
21+ CatchFall , CatchFallBehaviorPlugin , LostBallSearchBehaviorPlugin , ObserveBehaviorPlugin ,
22+ RlStrikerSearchBehaviorPlugin , Sitting , SittingBehaviorPlugin , Stand , StandBehaviorPlugin ,
23+ StandLookAt , StandLookAtBehaviorPlugin , Standup , StandupBehaviorPlugin ,
24+ StartUpBehaviorPlugin , VisualReferee , VisualRefereeBehaviorPlugin , WalkBehaviorPlugin ,
25+ WalkToBallBehaviorPlugin , WalkToBehaviorPlugin , WalkToSet , WalkToSetBehaviorPlugin ,
2526 } ,
2627 primary_state:: PrimaryState ,
2728 roles:: {
2829 Defender , DefenderRolePlugin , Goalkeeper , GoalkeeperRolePlugin , Striker , StrikerRolePlugin ,
2930 } ,
3031} ;
3132
33+ use std:: time:: Duration ;
34+
3235const FORWARD_LEANING_THRESHOLD : f32 = 0.2 ;
3336const BACKWARD_LEANING_THRESHOLD : f32 = -0.2 ;
3437
@@ -57,6 +60,7 @@ impl Plugin for BehaviorEnginePlugin {
5760 WalkToBallBehaviorPlugin ,
5861 WalkToBehaviorPlugin ,
5962 WalkToSetBehaviorPlugin ,
63+ LostBallSearchBehaviorPlugin ,
6064 ) )
6165 . add_systems ( PostUpdate , role_base) ;
6266 }
@@ -70,6 +74,20 @@ fn broken_robot_behavior_override(robot_info: Res<RobotInfo>, mut nao_manager: R
7074 }
7175}
7276
77+ #[ derive( Resource ) ]
78+ pub struct DefenderSwitchTimer {
79+ timer : Timer ,
80+ }
81+
82+ impl DefenderSwitchTimer {
83+ #[ must_use]
84+ pub fn new ( duration : Duration ) -> Self {
85+ DefenderSwitchTimer {
86+ timer : Timer :: new ( duration, TimerMode :: Once ) ,
87+ }
88+ }
89+ }
90+
7391pub trait RlBehaviorInput < T > {
7492 fn to_input ( & self ) -> T ;
7593}
@@ -111,6 +129,7 @@ pub enum BehaviorState {
111129 WalkToSet ,
112130 WalkToBall ,
113131 RlStrikerSearchBehavior ,
132+ LostBallSearch ,
114133}
115134
116135#[ must_use]
@@ -173,13 +192,38 @@ impl RoleState {
173192 commands : & mut Commands ,
174193 player_number : u8 ,
175194 possible_ball_distance : Option < f32 > ,
195+ role_state : Res < State < RoleState > > ,
196+ defender_switch_timer : Option < ResMut < DefenderSwitchTimer > > ,
197+ time : Res < Time > ,
176198 ) {
177199 if let Some ( distance) = possible_ball_distance {
178200 if distance < 3.0 {
179201 commands. set_role ( Striker ) ;
180202 return ;
181203 }
182204 }
205+
206+ // check if the current role is striker
207+ if * role_state == RoleState :: Striker && ( player_number != 4 && player_number != 5 ) {
208+ if let Some ( mut timer) = defender_switch_timer {
209+ timer. timer . tick ( time. delta ( ) ) ;
210+ if timer. timer . finished ( ) {
211+ commands. remove_resource :: < DefenderSwitchTimer > ( ) ;
212+ if player_number == 1 {
213+ commands. set_role ( Goalkeeper ) ;
214+ } else {
215+ commands. set_role ( Defender ) ;
216+ }
217+ } else {
218+ commands. set_role ( Striker ) ;
219+ }
220+ return ;
221+ }
222+ commands. insert_resource ( DefenderSwitchTimer :: new ( Duration :: from_secs ( 9 ) ) ) ;
223+ commands. set_role ( Striker ) ;
224+ return ;
225+ }
226+
183227 // TODO: Check if robots have been penalized, or which robot is closed to the ball etc.
184228 Self :: by_player_number ( commands, player_number) ;
185229 }
@@ -216,6 +260,9 @@ pub fn role_base(
216260 game_controller_message : Option < Res < GameControllerMessage > > ,
217261 imu_values : Res < IMUValues > ,
218262 ball_tracker : Res < BallTracker > ,
263+ role_state : Res < State < RoleState > > ,
264+ defender_switch_timer : Option < ResMut < DefenderSwitchTimer > > ,
265+ time : Res < Time > ,
219266) {
220267 commands. disable_role ( ) ;
221268 let behavior = behavior_state. get ( ) ;
@@ -278,6 +325,9 @@ pub fn role_base(
278325 match * primary_state {
279326 PrimaryState :: Sitting => commands. set_behavior ( Sitting ) ,
280327 PrimaryState :: Penalized => {
328+ // reset all timers
329+ commands. remove_resource :: < DefenderSwitchTimer > ( ) ;
330+ commands. remove_resource :: < LostBallSearchTimer > ( ) ;
281331 commands. set_behavior ( Stand ) ;
282332 }
283333 PrimaryState :: Standby => {
@@ -308,6 +358,9 @@ pub fn role_base(
308358 & mut commands,
309359 player_config. player_number ,
310360 possible_ball_distance,
361+ role_state,
362+ defender_switch_timer,
363+ time,
311364 ) ;
312365 }
313366 }
0 commit comments