Skip to content

Commit

Permalink
Fix clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
VirxEC committed May 21, 2024
1 parent 95d25da commit 1246782
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 72 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "rocketsim_rs"
description = "Rust bindings for the RocketSim project"
version = "0.29.1"
version = "0.29.2"
edition = "2021"
license = "MIT"
repository = "https://github.com/VirxEC/rocketsim-rs"
Expand Down
112 changes: 55 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,85 +13,83 @@ use rocketsim_rs::{
};
use std::time::Instant;

fn main() {
// Load in the Rocket League assets from the collision_meshes folder in the current directory
rocketsim_rs::init(None);
// Load in the Rocket League assets from the collision_meshes folder in the current directory
rocketsim_rs::init(None);

// Create a new arena with gamemode soccar and a tick rate of 120
let mut arena = Arena::default_standard();
println!("Arena tick rate: {}", arena.get_tick_rate());
// Create a new arena with gamemode soccar and a tick rate of 120
let mut arena = Arena::default_standard();
println!("Arena tick rate: {}", arena.get_tick_rate());

let car_id = arena.pin_mut().add_car(Team::Blue, CarConfig::octane());
let car_id = arena.pin_mut().add_car(Team::Blue, CarConfig::octane());

println!("Car id: {car_id}");
println!("Car id: {car_id}");

{
// custom initial car state
let mut car_state = arena.pin_mut().get_car(car_id);
{
// custom initial car state
let mut car_state = arena.pin_mut().get_car(car_id);

car_state.pos = Vec3::new(5., 0., 50.);
car_state.vel = Vec3::new(500., 800., 0.);
car_state.boost = 100.;
car_state.pos = Vec3::new(5., 0., 50.);
car_state.vel = Vec3::new(500., 800., 0.);
car_state.boost = 100.;

println!("Created custom car state");
println!("Created custom car state");

// Make the car boost
arena
.pin_mut()
.set_car_controls(
car_id,
CarControls {
boost: true,
..Default::default()
},
)
.unwrap();
// Make the car boost
arena
.pin_mut()
.set_car_controls(
car_id,
CarControls {
boost: true,
..Default::default()
},
)
.unwrap();

// If car_id can't be found in arena than this will return Err
arena.pin_mut().set_car(car_id, car_state).unwrap();
// If car_id can't be found in arena than this will return Err
arena.pin_mut().set_car(car_id, car_state).unwrap();

println!("Set car ({car_id}) state");
}
println!("Set car ({car_id}) state");
}

{
let mut ball_state = arena.pin_mut().get_ball();
{
let mut ball_state = arena.pin_mut().get_ball();

ball_state.pos.z = 1050.;
ball_state.vel = Vec3::new(0., 0., 250.);
ball_state.pos.z = 1050.;
ball_state.vel = Vec3::new(0., 0., 250.);

arena.pin_mut().set_ball(ball_state);
arena.pin_mut().set_ball(ball_state);

println!("Set ball state");
}
println!("Set ball state");
}

let ticks = 1800;
let curr_time = Instant::now();
let ticks = 1800;
let curr_time = Instant::now();

arena.pin_mut().step(ticks);
arena.pin_mut().step(ticks);

println!("Simulated {}s in {}ms", ticks as f32 / 120., curr_time.elapsed().as_millis());
println!("Simulated {}s in {}ms", ticks as f32 / 120., curr_time.elapsed().as_millis());

{
// get the car state again
let car_state = arena.pin_mut().get_car(car_id);
{
// get the car state again
let car_state = arena.pin_mut().get_car(car_id);

println!("Got new car state");
println!("Got new car state");

// You can debug the whole of the state
// but it takes up a lot of space in stdout
// dbg!(&car_state);
// You can debug the whole of the state
// but it takes up a lot of space in stdout
// dbg!(&car_state);

println!("New car location: {}", car_state.pos);
println!("New car boost: {}", car_state.boost);
}
println!("New car location: {}", car_state.pos);
println!("New car boost: {}", car_state.boost);
}

// Cast the ball state position to a glam Vec3A
#[cfg(feature = "glam")]
println!("New ball location: {}", arena.pin_mut().get_ball().pos.to_glam());
// Cast the ball state position to a glam Vec3A
#[cfg(feature = "glam")]
println!("New ball location: {}", arena.pin_mut().get_ball().pos.to_glam());

#[cfg(not(feature = "glam"))]
println!("New ball location: {}", arena.pin_mut().get_ball().pos);
}
#[cfg(not(feature = "glam"))]
println!("New ball location: {}", arena.pin_mut().get_ball().pos);
```

## Benchmarks
Expand Down
15 changes: 12 additions & 3 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,19 +462,19 @@ impl ToBytes for Render {
let mut bytes = Vec::with_capacity(num_bytes);

match self {
Render::Line2D { start, end, color } => {
Self::Line2D { start, end, color } => {
bytes.push(0);
bytes.extend_from_slice(&start.to_bytes());
bytes.extend_from_slice(&end.to_bytes());
bytes.extend_from_slice(&color.to_bytes());
}
Render::Line { start, end, color } => {
Self::Line { start, end, color } => {
bytes.push(1);
bytes.extend_from_slice(&start.to_bytes());
bytes.extend_from_slice(&end.to_bytes());
bytes.extend_from_slice(&color.to_bytes());
}
Render::LineStrip { positions, color } => {
Self::LineStrip { positions, color } => {
bytes.push(2);
bytes.extend_from_slice(&(positions.len() as u16).to_bytes());

Expand Down Expand Up @@ -524,6 +524,8 @@ impl RenderMessage {
}
}

#[inline]
#[must_use]
pub fn get_num_bytes(bytes: &[u8]) -> usize {
u32::from_bytes(&bytes[..u32::NUM_BYTES]) as usize
}
Expand Down Expand Up @@ -578,6 +580,7 @@ impl FromBytes for GameState {
impl GameState {
pub const MIN_NUM_BYTES: usize = u64::NUM_BYTES + f32::NUM_BYTES + 1 + u32::NUM_BYTES * 2;

#[inline]
fn count_bytes(&self) -> usize {
Self::MIN_NUM_BYTES
+ BallState::NUM_BYTES
Expand All @@ -586,6 +589,7 @@ impl GameState {
}

#[inline]
#[must_use]
pub fn get_num_bytes(bytes: &[u8]) -> usize {
Self::MIN_NUM_BYTES
+ BallState::NUM_BYTES
Expand All @@ -594,27 +598,32 @@ impl GameState {
}

#[inline]
#[must_use]
pub fn read_tick_count(bytes: &[u8]) -> u64 {
u64::from_bytes(&bytes[..u64::NUM_BYTES])
}

#[inline]
#[must_use]
pub fn read_tick_rate(bytes: &[u8]) -> f32 {
f32::from_bytes(&bytes[u64::NUM_BYTES..u64::NUM_BYTES + f32::NUM_BYTES])
}

#[inline]
#[must_use]
pub fn read_game_mode(bytes: &[u8]) -> GameMode {
GameMode::from_bytes(&bytes[(u64::NUM_BYTES + f32::NUM_BYTES)..=(u64::NUM_BYTES + f32::NUM_BYTES)])
}

#[inline]
#[must_use]
pub fn read_num_pads(bytes: &[u8]) -> usize {
u32::from_bytes(&bytes[u64::NUM_BYTES + f32::NUM_BYTES + 1..u64::NUM_BYTES + f32::NUM_BYTES + 1 + u32::NUM_BYTES])
as usize
}

#[inline]
#[must_use]
pub fn read_num_cars(bytes: &[u8]) -> usize {
u32::from_bytes(&bytes[u64::NUM_BYTES + f32::NUM_BYTES + 1 + u32::NUM_BYTES..Self::MIN_NUM_BYTES]) as usize
}
Expand Down
11 changes: 6 additions & 5 deletions src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ impl Default for ArenaConfig {
}

impl MutatorConfig {
#[must_use]
pub fn default(game_mode: GameMode) -> Self {
Self {
gravity: Vec3::new(0., 0., consts::GRAVITY_Z),
Expand Down Expand Up @@ -499,7 +500,7 @@ impl fmt::Display for RotMat {
}

impl RotMat {
pub const IDENTITY: RotMat = RotMat {
pub const IDENTITY: Self = Self {
forward: Vec3::X,
right: Vec3::Y,
up: Vec3::Z,
Expand Down Expand Up @@ -535,10 +536,10 @@ impl fmt::Display for Vec3 {
}

impl Vec3 {
pub const ZERO: Vec3 = Vec3::new(0., 0., 0.);
pub const X: Vec3 = Vec3::new(1., 0., 0.);
pub const Y: Vec3 = Vec3::new(0., 1., 0.);
pub const Z: Vec3 = Vec3::new(0., 0., 1.);
pub const ZERO: Self = Self::new(0., 0., 0.);
pub const X: Self = Self::new(1., 0., 0.);
pub const Y: Self = Self::new(0., 1., 0.);
pub const Z: Self = Self::new(0., 0., 1.);

#[inline]
#[must_use]
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![warn(clippy::all)]
#![doc = include_str!("../README.md")]
#![cfg_attr(
all(not(any(target_arch = "x86", target_arch = "x86_64")), feature = "glam"),
Expand Down Expand Up @@ -59,14 +60,15 @@ mod base {
#[rust_name = "Arena"]
type Arenar = crate::sim::Arena;

#[must_use]
#[namespace = "RocketSim"]
#[cxx_name = "GetStage"]
pub fn get_stage() -> RocketSimStage;

fn Init(folder: &str);

#[cxx_name = "InitFromMem"]
/// Initializes the collision mesh system for `RocketSim` from memory
#[cxx_name = "InitFromMem"]
fn init_from_mem(soccar: &[&[u8]], hoops: &[&[u8]]);

#[must_use]
Expand Down
4 changes: 4 additions & 0 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ impl Vec2 {
pub const ZERO: Self = Self::new(0., 0.);

#[inline]
#[must_use]
pub const fn new(x: f32, y: f32) -> Self {
Self { x, y }
}
Expand All @@ -24,6 +25,7 @@ pub struct Color {
}

impl Default for Color {
#[inline]
fn default() -> Self {
Self::BLACK
}
Expand All @@ -37,11 +39,13 @@ impl Color {
pub const BLUE: Self = Self::rgb(0., 0., 1.);

#[inline]
#[must_use]
pub const fn rgb(r: f32, g: f32, b: f32) -> Self {
Self { r, g, b, a: 1. }
}

#[inline]
#[must_use]
pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
Self { r, g, b, a }
}
Expand Down
Loading

0 comments on commit 1246782

Please sign in to comment.