reinit
This commit is contained in:
30
tank-rust/src/ffi/action.rs
Normal file
30
tank-rust/src/ffi/action.rs
Normal file
@ -0,0 +1,30 @@
|
||||
#[repr(i32)]
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
|
||||
pub enum Action {
|
||||
Forward = 0,
|
||||
Backward = 1,
|
||||
TurnRight = 2,
|
||||
TurnLeft = 3,
|
||||
AimRight = 4,
|
||||
AimLeft = 5,
|
||||
Shoot = 6,
|
||||
None = 7,
|
||||
}
|
||||
|
||||
impl TryFrom<i32> for Action {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(value: i32) -> Result<Self, Self::Error> {
|
||||
match value {
|
||||
0 => Ok(Action::Forward),
|
||||
1 => Ok(Action::Backward),
|
||||
2 => Ok(Action::TurnRight),
|
||||
3 => Ok(Action::TurnLeft),
|
||||
4 => Ok(Action::AimRight),
|
||||
5 => Ok(Action::AimLeft),
|
||||
6 => Ok(Action::Shoot),
|
||||
7 => Ok(Action::None),
|
||||
_ => Err("Invalid action"),
|
||||
}
|
||||
}
|
||||
}
|
6
tank-rust/src/ffi/info/bullet.rs
Normal file
6
tank-rust/src/ffi/info/bullet.rs
Normal file
@ -0,0 +1,6 @@
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
|
||||
pub struct Bullet {
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
}
|
58
tank-rust/src/ffi/info/mod.rs
Normal file
58
tank-rust/src/ffi/info/mod.rs
Normal file
@ -0,0 +1,58 @@
|
||||
mod bullet;
|
||||
mod player;
|
||||
mod station;
|
||||
mod wall;
|
||||
|
||||
pub use bullet::*;
|
||||
pub use player::*;
|
||||
pub use station::*;
|
||||
pub use wall::*;
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub struct Info<'a> {
|
||||
pub player: Player,
|
||||
pub teammates: &'a [Player],
|
||||
pub enemies: &'a [Player],
|
||||
pub bullets: &'a [Bullet],
|
||||
pub bullet_stations: &'a [Station],
|
||||
pub oil_stations: &'a [Station],
|
||||
pub walls: &'a [Wall],
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
pub struct RawInfo {
|
||||
player: *const Player,
|
||||
teammates: *const Player,
|
||||
teammates_len: u32,
|
||||
enemies: *const Player,
|
||||
enemies_len: u32,
|
||||
bullets: *const Bullet,
|
||||
bullet_len: u32,
|
||||
bullet_stations: *const Station,
|
||||
bullet_stations_len: u32,
|
||||
oil_stations: *const Station,
|
||||
oil_stations_len: u32,
|
||||
walls: *const Wall,
|
||||
walls_len: u32,
|
||||
}
|
||||
|
||||
impl<'a> Info<'a> {
|
||||
pub unsafe fn from_raw(self_: *const RawInfo) -> Self {
|
||||
let raw = &*self_;
|
||||
Info {
|
||||
player: (&*raw.player).clone(),
|
||||
teammates: std::slice::from_raw_parts(raw.teammates, raw.teammates_len as usize),
|
||||
enemies: std::slice::from_raw_parts(raw.enemies, raw.enemies_len as usize),
|
||||
bullets: std::slice::from_raw_parts(raw.bullets, raw.bullet_len as usize),
|
||||
bullet_stations: std::slice::from_raw_parts(
|
||||
raw.bullet_stations,
|
||||
raw.bullet_stations_len as usize,
|
||||
),
|
||||
oil_stations: std::slice::from_raw_parts(
|
||||
raw.oil_stations,
|
||||
raw.oil_stations_len as usize,
|
||||
),
|
||||
walls: std::slice::from_raw_parts(raw.walls, raw.walls_len as usize),
|
||||
}
|
||||
}
|
||||
}
|
14
tank-rust/src/ffi/info/player.rs
Normal file
14
tank-rust/src/ffi/info/player.rs
Normal file
@ -0,0 +1,14 @@
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize, Default)]
|
||||
pub struct Player {
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
pub speed: i32,
|
||||
pub score: i32,
|
||||
pub power: i32,
|
||||
pub oil: f32,
|
||||
pub lives: i32,
|
||||
pub angle: i32,
|
||||
pub gun_angle: i32,
|
||||
pub cooldown: i32,
|
||||
}
|
7
tank-rust/src/ffi/info/station.rs
Normal file
7
tank-rust/src/ffi/info/station.rs
Normal file
@ -0,0 +1,7 @@
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize, Default)]
|
||||
pub struct Station {
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
pub power: i32,
|
||||
}
|
7
tank-rust/src/ffi/info/wall.rs
Normal file
7
tank-rust/src/ffi/info/wall.rs
Normal file
@ -0,0 +1,7 @@
|
||||
#[repr(C)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, serde::Deserialize, serde::Serialize)]
|
||||
pub struct Wall {
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
pub lives: i32,
|
||||
}
|
7
tank-rust/src/ffi/mod.rs
Normal file
7
tank-rust/src/ffi/mod.rs
Normal file
@ -0,0 +1,7 @@
|
||||
mod action;
|
||||
mod info;
|
||||
|
||||
pub mod prelude {
|
||||
pub use super::action::*;
|
||||
pub use super::info::*;
|
||||
}
|
Reference in New Issue
Block a user