1
- use std:: f32:: consts:: PI ;
1
+ use std:: { f32:: consts:: PI , sync :: Arc } ;
2
2
3
3
use rlbot:: {
4
- agents:: { run_agents, Agent , PacketQueue } ,
5
- flat:: { ConnectionSettings , ControllableInfo , ControllerState , PlayerInput } ,
6
- util:: RLBotEnvironment ,
7
4
RLBotConnection ,
5
+ agents:: { Agent , run_agents} ,
6
+ flat:: {
7
+ ControllableInfo , ControllerState , FieldInfo , GamePacket , MatchConfiguration , PlayerInput ,
8
+ } ,
9
+ util:: { PacketQueue , RLBotEnvironment } ,
8
10
} ;
9
11
12
+ #[ allow( dead_code) ]
10
13
struct AtbaAgent {
11
- controllable_info : ControllableInfo ,
14
+ index : u32 ,
15
+ spawn_id : i32 ,
16
+ team : u32 ,
17
+ name : String ,
18
+ match_config : Arc < MatchConfiguration > ,
19
+ field_info : Arc < FieldInfo > ,
12
20
}
13
21
14
22
impl Agent for AtbaAgent {
15
- fn new ( controllable_info : ControllableInfo ) -> Self {
16
- Self { controllable_info }
23
+ fn new (
24
+ team : u32 ,
25
+ controllable_info : ControllableInfo ,
26
+ match_config : Arc < MatchConfiguration > ,
27
+ field_info : Arc < FieldInfo > ,
28
+ _packet_queue : & mut PacketQueue ,
29
+ ) -> Self {
30
+ let name = match_config
31
+ . player_configurations
32
+ . iter ( )
33
+ . find_map ( |player| {
34
+ if player. spawn_id == controllable_info. spawn_id {
35
+ Some ( player. name . clone ( ) )
36
+ } else {
37
+ None
38
+ }
39
+ } )
40
+ . unwrap ( ) ;
41
+
42
+ Self {
43
+ index : controllable_info. index ,
44
+ spawn_id : controllable_info. spawn_id ,
45
+ team,
46
+ name,
47
+ match_config,
48
+ field_info,
49
+ }
17
50
}
18
- fn tick ( & mut self , game_packet : & rlbot:: flat:: GamePacket , packet_queue : & mut PacketQueue ) {
51
+
52
+ fn tick ( & mut self , game_packet : & GamePacket , packet_queue : & mut PacketQueue ) {
19
53
let Some ( ball) = game_packet. balls . first ( ) else {
20
54
// If theres no ball, theres nothing to chase, don't do anything
21
55
return ;
22
56
} ;
23
57
24
58
// We're not in the gtp, skip this tick
25
- if game_packet. players . len ( ) <= self . controllable_info . index as usize {
59
+ if game_packet. players . len ( ) <= self . index as usize {
26
60
return ;
27
61
}
28
62
29
63
let target = & ball. physics ;
30
- let car = game_packet
31
- . players
32
- . get ( self . controllable_info . index as usize )
33
- . unwrap ( )
34
- . physics ;
64
+ let car = game_packet. players [ self . index as usize ] . physics ;
35
65
36
66
let bot_to_target_angle = f32:: atan2 (
37
67
target. location . y - car. location . y ,
@@ -53,17 +83,18 @@ impl Agent for AtbaAgent {
53
83
controller. throttle = 1. ;
54
84
55
85
packet_queue. push ( PlayerInput {
56
- player_index : self . controllable_info . index ,
86
+ player_index : self . index ,
57
87
controller_state : controller,
58
88
} ) ;
59
89
}
60
90
}
91
+
61
92
fn main ( ) {
62
93
let RLBotEnvironment {
63
94
server_addr,
64
95
agent_id,
65
96
} = RLBotEnvironment :: from_env ( ) ;
66
- let agent_id = agent_id. unwrap_or ( "rlbot/rust-example/atba_agent" . into ( ) ) ;
97
+ let agent_id = agent_id. unwrap_or_else ( || "rlbot/rust-example/atba_agent" . into ( ) ) ;
67
98
68
99
println ! ( "Connecting" ) ;
69
100
@@ -76,17 +107,9 @@ fn main() {
76
107
// If the hivemind field is set to true, one instance of your bot will handle
77
108
// all of the bots in a team.
78
109
79
- // Blocking
80
- run_agents :: < AtbaAgent > (
81
- ConnectionSettings {
82
- agent_id : agent_id. clone ( ) ,
83
- wants_ball_predictions : true ,
84
- wants_comms : true ,
85
- close_between_matches : true ,
86
- } ,
87
- rlbot_connection,
88
- )
89
- . expect ( "run_agents crashed" ) ;
90
-
91
- println ! ( "Agent(s) with agent_id `{agent_id}` exited nicely" )
110
+ // Blocking.
111
+ run_agents :: < AtbaAgent > ( agent_id. clone ( ) , true , true , rlbot_connection)
112
+ . expect ( "run_agents crashed" ) ;
113
+
114
+ println ! ( "Agent(s) with agent_id `{agent_id}` exited nicely" ) ;
92
115
}
0 commit comments