Skip to main content

high_jump_script/
main.rs

1use rlbot::{
2    RLBotConnection,
3    agents::{ScriptAgent, run_script_agent},
4    flat::{
5        DesiredCarState, DesiredGameState, DesiredPhysics, FieldInfo, GamePacket,
6        MatchConfiguration, MatchPhase, Vector3Partial,
7    },
8    util::{AgentEnvironment, PacketQueue},
9};
10
11#[allow(dead_code)]
12struct MyScript {
13    agent_id: String,
14    name: String,
15    prev_jumps: Vec<bool>,
16}
17
18impl ScriptAgent for MyScript {
19    fn new(
20        agent_id: String,
21        match_config: MatchConfiguration,
22        _field_info: FieldInfo,
23        _packet_queue: &mut PacketQueue,
24    ) -> Self {
25        let name = match_config
26            .script_configurations
27            .iter()
28            .find_map(|script| {
29                if script.agent_id == agent_id {
30                    Some(script.name.clone())
31                } else {
32                    None
33                }
34            })
35            .unwrap();
36
37        Self {
38            agent_id,
39            name,
40            prev_jumps: vec![false; match_config.player_configurations.len()],
41        }
42    }
43
44    fn tick(&mut self, game_packet: GamePacket, packet_queue: &mut PacketQueue) {
45        if game_packet.match_info.match_phase != MatchPhase::Active {
46            return;
47        }
48
49        if self.prev_jumps.len() != game_packet.players.len() {
50            self.prev_jumps.resize(game_packet.players.len(), false);
51        }
52
53        let mut car_states = Vec::with_capacity(game_packet.players.len());
54
55        for (player, prev_jump) in game_packet.players.iter().zip(self.prev_jumps.iter_mut()) {
56            if player.last_input.jump && !*prev_jump {
57                let mut physics: Box<DesiredPhysics> = Box::default();
58                let mut velocity: Box<Vector3Partial> = Box::default();
59                velocity.z = Some(rlbot::flat::Float {
60                    // make cars jump super high!
61                    val: player.physics.velocity.z + 1000.0,
62                });
63                physics.velocity = Some(velocity);
64
65                car_states.push(DesiredCarState {
66                    physics: Some(physics),
67                    ..Default::default()
68                });
69            } else {
70                car_states.push(DesiredCarState::default());
71            }
72
73            *prev_jump = player.last_input.jump;
74        }
75
76        packet_queue.push(DesiredGameState {
77            car_states,
78            ..Default::default()
79        });
80    }
81}
82
83fn main() {
84    let AgentEnvironment {
85        server_addr,
86        agent_id,
87    } = AgentEnvironment::from_env();
88    let agent_id = agent_id.unwrap_or_else(|| "rlbot/rust-example/high_jump_script".into());
89    let rlbot_connection = RLBotConnection::new(&server_addr).expect("connection");
90
91    // Blocking.
92    run_script_agent::<MyScript>(agent_id.clone(), true, true, rlbot_connection)
93        .expect("run_script_agent crashed");
94
95    println!("Script with agent_id `{agent_id}` exited nicely");
96}