Skip to content

Various QoL things #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
[workspace]
members = ["crates/rlbot", "crates/rlbot_flat"]
resolver = "2"
resolver = "3"
package.license-file = "LICENSE"
package.version = "0.1.0"

[profile.release]
lto = "fat"
opt-level = 3
codegen-units = 1
panic = "abort"

[profile.dev]
opt-level = 3
codegen-units = 1
panic = "abort"
18 changes: 5 additions & 13 deletions crates/rlbot/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
[package]
name = "rlbot"
version.workspace = true
edition = "2021"
edition = "2024"
license-file.workspace = true

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[profile.release]
lto = "fat"
opt-level = 3
codegen-units = 1
panic = "abort"

[profile.dev]
opt-level = 3
codegen-units = 1
panic = "abort"

[dependencies]
kanal = { version = "0.1.0-pre8", default-features = false }
thiserror = "1.0.50"
thiserror = "2.0.12"
rlbot_flat = { path = "../rlbot_flat" }

[features]
default = ["glam"]
glam = ["rlbot_flat/glam"]

[lints.clippy]
all = "warn"
2 changes: 1 addition & 1 deletion crates/rlbot/examples/atba_agent/bot.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[settings]
name = "Rust Example Bot (atba_agent)"
agent_id = "rlbot/rust-example/atba_agent"
# looks_config = "looks.toml"
# loadout_file = "loadout.toml"
run_command = "..\\..\\..\\..\\target\\release\\examples\\atba_agent.exe"
run_command_linux = "../../../../target/release/examples/atba_agent"
# rust_interface::agents::run_agents supports "non-hivemind" multithreading using one connection
Expand Down
81 changes: 52 additions & 29 deletions crates/rlbot/examples/atba_agent/main.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,67 @@
use std::f32::consts::PI;
use std::{f32::consts::PI, sync::Arc};

use rlbot::{
agents::{run_agents, Agent, PacketQueue},
flat::{ConnectionSettings, ControllableInfo, ControllerState, PlayerInput},
util::RLBotEnvironment,
RLBotConnection,
agents::{Agent, run_agents},
flat::{
ControllableInfo, ControllerState, FieldInfo, GamePacket, MatchConfiguration, PlayerInput,
},
util::{PacketQueue, RLBotEnvironment},
};

#[allow(dead_code)]
struct AtbaAgent {
controllable_info: ControllableInfo,
index: u32,
spawn_id: i32,
team: u32,
name: String,
match_config: Arc<MatchConfiguration>,
field_info: Arc<FieldInfo>,
}

impl Agent for AtbaAgent {
fn new(controllable_info: ControllableInfo) -> Self {
Self { controllable_info }
fn new(
team: u32,
controllable_info: ControllableInfo,
match_config: Arc<MatchConfiguration>,
field_info: Arc<FieldInfo>,
_packet_queue: &mut PacketQueue,
) -> Self {
let name = match_config
.player_configurations
.iter()
.find_map(|player| {
if player.spawn_id == controllable_info.spawn_id {
Some(player.name.clone())
} else {
None
}
})
.unwrap();

Self {
index: controllable_info.index,
spawn_id: controllable_info.spawn_id,
team,
name,
match_config,
field_info,
}
}
fn tick(&mut self, game_packet: &rlbot::flat::GamePacket, packet_queue: &mut PacketQueue) {

fn tick(&mut self, game_packet: &GamePacket, packet_queue: &mut PacketQueue) {
let Some(ball) = game_packet.balls.first() else {
// If theres no ball, theres nothing to chase, don't do anything
return;
};

// We're not in the gtp, skip this tick
if game_packet.players.len() <= self.controllable_info.index as usize {
if game_packet.players.len() <= self.index as usize {
return;
}

let target = &ball.physics;
let car = game_packet
.players
.get(self.controllable_info.index as usize)
.unwrap()
.physics;
let car = game_packet.players[self.index as usize].physics;

let bot_to_target_angle = f32::atan2(
target.location.y - car.location.y,
Expand All @@ -53,17 +83,18 @@ impl Agent for AtbaAgent {
controller.throttle = 1.;

packet_queue.push(PlayerInput {
player_index: self.controllable_info.index,
player_index: self.index,
controller_state: controller,
});
}
}

fn main() {
let RLBotEnvironment {
server_addr,
agent_id,
} = RLBotEnvironment::from_env();
let agent_id = agent_id.unwrap_or("rlbot/rust-example/atba_agent".into());
let agent_id = agent_id.unwrap_or_else(|| "rlbot/rust-example/atba_agent".into());

println!("Connecting");

Expand All @@ -76,17 +107,9 @@ fn main() {
// If the hivemind field is set to true, one instance of your bot will handle
// all of the bots in a team.

// Blocking
run_agents::<AtbaAgent>(
ConnectionSettings {
agent_id: agent_id.clone(),
wants_ball_predictions: true,
wants_comms: true,
close_between_matches: true,
},
rlbot_connection,
)
.expect("run_agents crashed");

println!("Agent(s) with agent_id `{agent_id}` exited nicely")
// Blocking.
run_agents::<AtbaAgent>(agent_id.clone(), true, true, rlbot_connection)
.expect("run_agents crashed");

println!("Agent(s) with agent_id `{agent_id}` exited nicely");
}
19 changes: 19 additions & 0 deletions crates/rlbot/examples/atba_hivemind/bot.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[settings]
name = "Rust Example Bot (atba_hivemind)"
agent_id = "rlbot/rust-example/atba_hivemind"
# loadout_file = "loadout.toml"
run_command = "..\\..\\..\\..\\target\\release\\examples\\atba_hivemind.exe"
run_command_linux = "../../../../target/release/examples/atba_hivemind"
hivemind = true

[details]
description = "Rust always-towards-ball-agent."
# fun_fact = ""
# source_link = "https://github.com/swz-git/rust-interface"
developer = ""
language = "rust"
# logo_file = "logo.png"
# ALL POSSIBLE TAGS: 1v1, teamplay, goalie, hoops, dropshot, snow-day, spike-rush, heatseaker, memebot
# NOTE: Only add the goalie tag if your bot only plays as a goalie; this directly contrasts with the teamplay tag!
# NOTE: Only add a tag for a special game mode if you bot properly supports it
tags = []
128 changes: 128 additions & 0 deletions crates/rlbot/examples/atba_hivemind/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
use std::f32::consts::PI;

use rlbot::{
RLBotConnection,
flat::{
ControllableTeamInfo, ControllerState, FieldInfo, GamePacket, MatchConfiguration,
PlayerInput,
},
hivemind::{Hivemind, run_hivemind},
util::{PacketQueue, RLBotEnvironment},
};

#[allow(dead_code)]
struct AtbaHivemind {
indices: Vec<u32>,
spawn_ids: Vec<i32>,
team: u32,
names: Vec<String>,
match_config: MatchConfiguration,
field_info: FieldInfo,
}

impl Hivemind for AtbaHivemind {
fn new(
controllable_team_info: ControllableTeamInfo,
match_config: MatchConfiguration,
field_info: FieldInfo,
_packet_queue: &mut PacketQueue,
) -> Self {
let names = match_config
.player_configurations
.iter()
.filter_map(|player| {
controllable_team_info
.controllables
.iter()
.find_map(|controllable| {
if controllable.spawn_id == player.spawn_id {
Some(player.name.clone())
} else {
None
}
})
})
.collect();

let (indices, spawn_ids) = controllable_team_info
.controllables
.iter()
.map(|controllable| (controllable.index, controllable.spawn_id))
.unzip();

Self {
indices,
spawn_ids,
team: controllable_team_info.team,
names,
match_config,
field_info,
}
}

fn tick(&mut self, game_packet: GamePacket, packet_queue: &mut PacketQueue) {
let Some(ball) = game_packet.balls.first() else {
// If theres no ball, theres nothing to chase, don't do anything
return;
};

// We're not in the gtp, skip this tick
if game_packet.players.len() <= self.indices[self.indices.len() - 1] as usize {
return;
}

for &index in &self.indices {
let target = &ball.physics;
let car = game_packet.players[index as usize].physics;

let bot_to_target_angle = f32::atan2(
target.location.y - car.location.y,
target.location.x - car.location.x,
);

let mut bot_front_to_target_angle = bot_to_target_angle - car.rotation.yaw;

bot_front_to_target_angle = (bot_front_to_target_angle + PI).rem_euclid(2. * PI) - PI;

let mut controller = ControllerState::default();

if bot_front_to_target_angle > 0. {
controller.steer = 1.;
} else {
controller.steer = -1.;
}

controller.throttle = 1.;

packet_queue.push(PlayerInput {
player_index: index,
controller_state: controller,
});
}
}
}

fn main() {
let RLBotEnvironment {
server_addr,
agent_id,
} = RLBotEnvironment::from_env();
let agent_id = agent_id.unwrap_or_else(|| "rlbot/rust-example/atba_hivemind".into());

println!("Connecting");

let rlbot_connection = RLBotConnection::new(&server_addr).expect("connection");

println!("Running!");

// The hivemind field in your bot.toml file decides if rlbot core is going to
// start your bot as one or multiple instances of your binary/exe.
// If the hivemind field is set to true, one instance of your bot will handle
// all of the bots in a team.

// Blocking.
run_hivemind::<AtbaHivemind>(agent_id.clone(), true, true, rlbot_connection)
.expect("run_hivemind crashed");

println!("Hivemind with agent_id `{agent_id}` exited nicely");
}
2 changes: 1 addition & 1 deletion crates/rlbot/examples/atba_raw/bot.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[settings]
name = "Rust Example Bot (atba_raw)"
agent_id = "rlbot/rust-example/atba_raw"
# looks_config = "looks.toml"
# loadout_file = "loadout.toml"
run_command = "..\\..\\..\\..\\target\\release\\examples\\atba_raw.exe"
run_command_linux = "../../../../target/release/examples/atba_raw"
# the "raw" atba example doesn't support hivemind mode
Expand Down
18 changes: 9 additions & 9 deletions crates/rlbot/examples/atba_raw/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use std::f32::consts::PI;

use rlbot::{
Packet, RLBotConnection,
flat::{ConnectionSettings, ControllerState, PlayerInput},
util::RLBotEnvironment,
Packet, RLBotConnection,
};

fn main() {
let RLBotEnvironment {
server_addr,
agent_id,
} = RLBotEnvironment::from_env();
let agent_id = agent_id.unwrap_or("rlbot/rust-example/atba_raw".into());
let agent_id = agent_id.unwrap_or_else(|| "rlbot/rust-example/atba_raw".into());

let mut rlbot_connection = RLBotConnection::new(&server_addr).expect("connection");

Expand All @@ -33,15 +33,15 @@ fn main() {
let packet = rlbot_connection.recv_packet().unwrap();
if let Packet::ControllableTeamInfo(x) = packet {
break x;
} else {
packets_to_process.push(packet);
continue;
}

packets_to_process.push(packet);
};

if controllable_team_info.controllables.len() != 1 {
panic!("The raw atba example code does not support hiveminds, please disable the hivemind field in bot.toml")
}
assert!(
controllable_team_info.controllables.len() == 1,
"The raw atba example code does not support hiveminds, please disable the hivemind field in bot.toml"
);

let controllable_info = controllable_team_info
.controllables
Expand All @@ -53,7 +53,7 @@ fn main() {
loop {
let Packet::GamePacket(game_packet) = packets_to_process
.pop()
.unwrap_or(rlbot_connection.recv_packet().unwrap())
.unwrap_or_else(|| rlbot_connection.recv_packet().unwrap())
else {
continue;
};
Expand Down
Loading