use crate::{
rlbot_generated::rlbot::flat,
state_convert::{Point3Into, Vector3Into},
};
#[derive(Clone, Default)]
pub struct Vector3Partial {
pub x: Option<f32>,
pub y: Option<f32>,
pub z: Option<f32>,
non_exhaustive: (),
}
impl Vector3Partial {
pub fn new() -> Self {
Self::default()
}
pub fn x(mut self, x: f32) -> Self {
self.x = Some(x);
self
}
pub fn y(mut self, y: f32) -> Self {
self.y = Some(y);
self
}
pub fn z(mut self, z: f32) -> Self {
self.z = Some(z);
self
}
fn serialize<'a>(
&self,
builder: &mut flatbuffers::FlatBufferBuilder<'a>,
) -> flatbuffers::WIPOffset<flat::Vector3Partial<'a>> {
let x = self.x.map(flat::Float::new);
let y = self.y.map(flat::Float::new);
let z = self.z.map(flat::Float::new);
let args = flat::Vector3PartialArgs {
x: x.as_ref(),
y: y.as_ref(),
z: z.as_ref(),
};
flat::Vector3Partial::create(builder, &args)
}
}
#[derive(Clone, Default)]
pub struct RotatorPartial {
pub pitch: Option<f32>,
pub yaw: Option<f32>,
pub roll: Option<f32>,
non_exhaustive: (),
}
impl RotatorPartial {
pub fn new() -> Self {
Self::default()
}
pub fn pitch(mut self, pitch: f32) -> Self {
self.pitch = Some(pitch);
self
}
pub fn yaw(mut self, yaw: f32) -> Self {
self.yaw = Some(yaw);
self
}
pub fn roll(mut self, roll: f32) -> Self {
self.roll = Some(roll);
self
}
fn serialize<'a>(
&self,
builder: &mut flatbuffers::FlatBufferBuilder<'a>,
) -> flatbuffers::WIPOffset<flat::RotatorPartial<'a>> {
let pitch = self.pitch.map(flat::Float::new);
let yaw = self.yaw.map(flat::Float::new);
let roll = self.roll.map(flat::Float::new);
let args = flat::RotatorPartialArgs {
pitch: pitch.as_ref(),
yaw: yaw.as_ref(),
roll: roll.as_ref(),
};
flat::RotatorPartial::create(builder, &args)
}
}
#[derive(Clone, Default)]
pub struct DesiredPhysics {
pub location: Option<Vector3Partial>,
pub rotation: Option<RotatorPartial>,
pub velocity: Option<Vector3Partial>,
pub angular_velocity: Option<Vector3Partial>,
non_exhaustive: (),
}
impl DesiredPhysics {
pub fn new() -> Self {
Self::default()
}
pub fn location(mut self, location: impl Point3Into<Vector3Partial>) -> Self {
self.location = Some(location.into());
self
}
pub fn rotation(mut self, rotation: impl Into<RotatorPartial>) -> Self {
self.rotation = Some(rotation.into());
self
}
pub fn velocity(mut self, velocity: impl Vector3Into<Vector3Partial>) -> Self {
self.velocity = Some(velocity.into());
self
}
pub fn angular_velocity(mut self, angular_velocity: impl Vector3Into<Vector3Partial>) -> Self {
self.angular_velocity = Some(angular_velocity.into());
self
}
fn serialize<'a>(
&self,
builder: &mut flatbuffers::FlatBufferBuilder<'a>,
) -> flatbuffers::WIPOffset<flat::DesiredPhysics<'a>> {
let args = flat::DesiredPhysicsArgs {
location: self.location.as_ref().map(|x| x.serialize(builder)),
rotation: self.rotation.as_ref().map(|x| x.serialize(builder)),
velocity: self.velocity.as_ref().map(|x| x.serialize(builder)),
angularVelocity: self.angular_velocity.as_ref().map(|x| x.serialize(builder)),
};
flat::DesiredPhysics::create(builder, &args)
}
}
#[derive(Clone, Default)]
pub struct DesiredBallState {
pub physics: Option<DesiredPhysics>,
non_exhaustive: (),
}
impl DesiredBallState {
pub fn new() -> Self {
Self::default()
}
pub fn physics(mut self, physics: DesiredPhysics) -> Self {
self.physics = Some(physics);
self
}
fn serialize<'a>(
&self,
builder: &mut flatbuffers::FlatBufferBuilder<'a>,
) -> flatbuffers::WIPOffset<flat::DesiredBallState<'a>> {
let args = flat::DesiredBallStateArgs {
physics: self.physics.as_ref().map(|x| x.serialize(builder)),
};
flat::DesiredBallState::create(builder, &args)
}
}
#[derive(Clone, Default)]
pub struct DesiredCarState {
pub physics: Option<DesiredPhysics>,
pub boost_amount: Option<f32>,
pub jumped: Option<bool>,
pub double_jumped: Option<bool>,
non_exhaustive: (),
}
impl DesiredCarState {
pub fn new() -> Self {
Self::default()
}
pub fn physics(mut self, physics: DesiredPhysics) -> Self {
self.physics = Some(physics);
self
}
pub fn boost_amount(mut self, boost_amount: f32) -> Self {
self.boost_amount = Some(boost_amount);
self
}
pub fn jumped(mut self, jumped: bool) -> Self {
self.jumped = Some(jumped);
self
}
pub fn double_jumped(mut self, double_jumped: bool) -> Self {
self.double_jumped = Some(double_jumped);
self
}
fn serialize<'a>(
&self,
builder: &mut flatbuffers::FlatBufferBuilder<'a>,
) -> flatbuffers::WIPOffset<flat::DesiredCarState<'a>> {
let boost_amount = self.boost_amount.map(flat::Float::new);
let jumped = self.jumped.map(flat::Bool::new);
let double_jumped = self.double_jumped.map(flat::Bool::new);
let args = flat::DesiredCarStateArgs {
physics: self.physics.as_ref().map(|x| x.serialize(builder)),
boostAmount: boost_amount.as_ref(),
jumped: jumped.as_ref(),
doubleJumped: double_jumped.as_ref(),
};
flat::DesiredCarState::create(builder, &args)
}
}
#[derive(Clone, Default)]
pub struct DesiredBoostState {
pub respawn_time: Option<f32>,
non_exhaustive: (),
}
impl DesiredBoostState {
pub fn new() -> Self {
Self::default()
}
pub fn respawn_time(mut self, respawn_time: f32) -> Self {
self.respawn_time = Some(respawn_time);
self
}
fn serialize<'a>(
&self,
builder: &mut flatbuffers::FlatBufferBuilder<'a>,
) -> flatbuffers::WIPOffset<flat::DesiredBoostState<'a>> {
let respawn_time = self.respawn_time.map(flat::Float::new);
let args = flat::DesiredBoostStateArgs {
respawnTime: respawn_time.as_ref(),
};
flat::DesiredBoostState::create(builder, &args)
}
}
#[derive(Clone, Default)]
pub struct DesiredGameInfoState {
pub world_gravity_z: Option<f32>,
pub game_speed: Option<f32>,
non_exhaustive: (),
}
impl DesiredGameInfoState {
pub fn new() -> Self {
Self::default()
}
pub fn world_gravity_z(mut self, world_gravity_z: f32) -> Self {
self.world_gravity_z = Some(world_gravity_z);
self
}
pub fn game_speed(mut self, game_speed: f32) -> Self {
self.game_speed = Some(game_speed);
self
}
fn serialize<'a>(
&self,
builder: &mut flatbuffers::FlatBufferBuilder<'a>,
) -> flatbuffers::WIPOffset<flat::DesiredGameInfoState<'a>> {
let world_gravity_z = self.world_gravity_z.map(flat::Float::new);
let game_speed = self.game_speed.map(flat::Float::new);
let args = flat::DesiredGameInfoStateArgs {
worldGravityZ: world_gravity_z.as_ref(),
gameSpeed: game_speed.as_ref(),
};
flat::DesiredGameInfoState::create(builder, &args)
}
}
#[derive(Clone, Default)]
pub struct DesiredGameState {
pub ball_state: Option<DesiredBallState>,
pub car_states: Vec<Option<DesiredCarState>>,
pub boost_states: Vec<Option<DesiredBoostState>>,
pub game_info_state: Option<DesiredGameInfoState>,
non_exhaustive: (),
}
impl DesiredGameState {
pub fn new() -> Self {
Self::default()
}
pub fn ball_state(mut self, ball_state: DesiredBallState) -> Self {
self.ball_state = Some(ball_state);
self
}
pub fn car_state(mut self, index: usize, car_state: DesiredCarState) -> Self {
if self.car_states.len() <= index {
self.car_states.resize(index + 1, Default::default());
}
self.car_states[index] = Some(car_state);
self
}
pub fn boost_state(mut self, index: usize, boost_state: DesiredBoostState) -> Self {
if self.boost_states.len() <= index {
self.boost_states.resize(index + 1, Default::default());
}
self.boost_states[index] = Some(boost_state);
self
}
pub fn game_info_state(mut self, game_info_state: DesiredGameInfoState) -> Self {
self.game_info_state = Some(game_info_state);
self
}
pub(crate) fn serialize<'a>(&self) -> flatbuffers::FlatBufferBuilder<'a> {
let mut builder = flatbuffers::FlatBufferBuilder::new_with_capacity(1024);
let car_states = self
.car_states
.iter()
.map(|cs| {
cs.as_ref()
.unwrap_or(&DesiredCarState::new())
.serialize(&mut builder)
})
.collect::<Vec<_>>();
let boost_states = self
.boost_states
.iter()
.map(|s| {
s.as_ref()
.unwrap_or(&DesiredBoostState::new())
.serialize(&mut builder)
})
.collect::<Vec<_>>();
let args = flat::DesiredGameStateArgs {
ballState: self.ball_state.as_ref().map(|x| x.serialize(&mut builder)),
carStates: Some(builder.create_vector(&car_states)),
boostStates: Some(builder.create_vector(&boost_states)),
gameInfoState: self
.game_info_state
.as_ref()
.map(|x| x.serialize(&mut builder)),
};
let root = flat::DesiredGameState::create(&mut builder, &args);
builder.finish(root, None);
builder
}
}
#[cfg(feature = "nalgebra")]
#[cfg(test)]
mod tests {
use crate::state;
use na::{Point3, Vector3};
#[test]
fn test_nalgebra_arguments() {
let _ = state::DesiredPhysics::new()
.location(Point3::origin())
.velocity(Vector3::zeros())
.angular_velocity(Vector3::zeros());
}
}