Skip to content

Commit

Permalink
Update RocketSim
Browse files Browse the repository at this point in the history
Change to_glam for Vec3 to output a Vec3A instead of a Vec4
Add to_glam fn for more things
  • Loading branch information
VirxEC committed May 5, 2024
1 parent deba219 commit d9cd4f2
Show file tree
Hide file tree
Showing 9 changed files with 244 additions and 53 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.26.2"
version = "0.27.0"
edition = "2021"
license = "MIT"
repository = "https://github.com/VirxEC/rocketsim-rs"
Expand Down
2 changes: 1 addition & 1 deletion RocketSim
1 change: 1 addition & 0 deletions examples/rlviser_socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ fn setup_arena(arena_type: GameMode) -> UniquePtr<Arena> {
(
i,
CarControls {
steer: 0.2,
throttle: 1.,
pitch: -0.1,
boost: true,
Expand Down
47 changes: 38 additions & 9 deletions src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ use crate::{
math::{RotMat, Vec3},
render::{Color, Render, RenderMessage, Vec2},
sim::{
BallHitInfo, BallState, BoostPadState, CarConfig, CarControls, CarState, GameMode, HeatseekerInfo, Team,
WheelPairConfig,
BallHitInfo, BallState, BoostPadState, CarConfig, CarContact, CarControls, CarState, GameMode, HeatseekerInfo, Team,
WheelPairConfig, WorldContact,
},
BoostPad, CarInfo, GameState,
};
use core::fmt;

pub trait FromBytes {
fn from_bytes(bytes: &[u8]) -> Self;
Expand Down Expand Up @@ -34,6 +35,7 @@ impl<'a> ByteReader<'a> {
}

#[inline]
#[track_caller]
pub fn debug_assert_num_bytes(&self, num_bytes: usize) {
debug_assert_eq!(self.idx, num_bytes, "ByteReader::debug_assert_num_bytes() failed");
}
Expand Down Expand Up @@ -116,6 +118,20 @@ impl FromBytes for i32 {
}
}

impl<T: FromBytesExact + fmt::Debug, const N: usize> FromBytesExact for [T; N] {
const NUM_BYTES: usize = T::NUM_BYTES * N;
}

impl<T: FromBytesExact + fmt::Debug, const N: usize> FromBytes for [T; N] {
fn from_bytes(bytes: &[u8]) -> Self {
let mut reader = ByteReader::new(bytes);

let items = (0..N).map(|_| reader.read()).collect::<Vec<T>>();
reader.debug_assert_num_bytes(Self::NUM_BYTES);
items.try_into().unwrap()
}
}

impl FromBytesExact for Team {
const NUM_BYTES: usize = 1;
}
Expand Down Expand Up @@ -228,6 +244,16 @@ impl<const N: usize> ByteWriter<N> {
}
}

impl ToBytesExact<{ bool::NUM_BYTES * 4 }> for [bool; 4] {
fn to_bytes(&self) -> [u8; bool::NUM_BYTES * 4] {
let mut writer = ByteWriter::<{ bool::NUM_BYTES * 4 }>::new();
for item in self {
writer.write(item);
}
writer.inner()
}
}

macro_rules! impl_to_bytes_exact_via_std {
($($t:ty),+) => {
$(impl ToBytesExact<{ Self::NUM_BYTES }> for $t {
Expand Down Expand Up @@ -330,14 +356,17 @@ impl_bytes_exact!(
jump,
handbrake
);
impl_bytes_exact!(WorldContact, 1 + Vec3::NUM_BYTES, has_contact, contact_normal);
impl_bytes_exact!(CarContact, u32::NUM_BYTES + f32::NUM_BYTES, other_car_id, cooldown_timer);
impl_bytes_exact!(
CarState,
u64::NUM_BYTES
+ Vec3::NUM_BYTES * 5
+ Vec3::NUM_BYTES * 4
+ RotMat::NUM_BYTES
+ 10
+ 13
+ f32::NUM_BYTES * 11
+ u32::NUM_BYTES
+ WorldContact::NUM_BYTES
+ CarContact::NUM_BYTES
+ BallHitInfo::NUM_BYTES
+ CarControls::NUM_BYTES,
update_counter,
Expand All @@ -346,6 +375,7 @@ impl_bytes_exact!(
vel,
ang_vel,
is_on_ground,
wheels_with_contact,
has_jumped,
has_double_jumped,
has_flipped,
Expand All @@ -354,6 +384,7 @@ impl_bytes_exact!(
flip_time,
is_flipping,
is_jumping,
air_time,
air_time_since_jump,
boost,
time_spent_boosting,
Expand All @@ -363,10 +394,8 @@ impl_bytes_exact!(
is_auto_flipping,
auto_flip_timer,
auto_flip_torque_scale,
has_contact,
contact_normal,
other_car_id,
cooldown_timer,
world_contact,
car_contact,
is_demoed,
demo_respawn_timer,
ball_hit_info,
Expand Down
22 changes: 14 additions & 8 deletions src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::{
consts,
math::{Angle, RotMat, Vec3},
sim::{
Arena, ArenaMemWeightMode, BallHitInfo, BallState, BoostPadState, CarConfig, CarControls, CarState, DemoMode,
GameMode, HeatseekerInfo, MutatorConfig, Team,
Arena, ArenaMemWeightMode, BallHitInfo, BallState, BoostPadState, CarConfig, CarContact, CarControls, CarState,
DemoMode, GameMode, HeatseekerInfo, MutatorConfig, Team, WorldContact,
},
Init::AngleFromRotMat,
};
Expand Down Expand Up @@ -442,6 +442,7 @@ impl Default for CarState {
vel: Vec3::ZERO,
ang_vel: Vec3::ZERO,
is_on_ground: true,
wheels_with_contact: [true; 4],
has_jumped: false,
has_double_jumped: false,
has_flipped: false,
Expand All @@ -450,6 +451,7 @@ impl Default for CarState {
flip_time: 0.,
is_flipping: false,
is_jumping: false,
air_time: 0.,
air_time_since_jump: 0.,
boost: 100. / 3.,
time_spent_boosting: 0.,
Expand All @@ -459,10 +461,14 @@ impl Default for CarState {
is_auto_flipping: false,
auto_flip_timer: 0.,
auto_flip_torque_scale: 0.,
has_contact: false,
contact_normal: Vec3::ZERO,
other_car_id: 0,
cooldown_timer: 0.,
world_contact: WorldContact {
has_contact: false,
contact_normal: Vec3::ZERO,
},
car_contact: CarContact {
other_car_id: 0,
cooldown_timer: 0.,
},
is_demoed: false,
demo_respawn_timer: 0.,
ball_hit_info: BallHitInfo::default(),
Expand All @@ -476,10 +482,10 @@ impl CarState {
#[must_use]
/// Returns the other Car that this Car is currently contacting, if any
pub fn get_contacting_car(&self, arena: Pin<&mut Arena>) -> Option<Self> {
if self.other_car_id == 0 {
if self.car_contact.other_car_id == 0 {
None
} else {
Some(arena.get_car(self.other_car_id))
Some(arena.get_car(self.car_contact.other_car_id))
}
}
}
Expand Down
Loading

0 comments on commit d9cd4f2

Please sign in to comment.