This commit is contained in:
Eason
2024-06-12 16:09:12 +08:00
commit d8b781011b
156 changed files with 26489 additions and 0 deletions

View 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"),
}
}
}

View 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,
}

View 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),
}
}
}

View 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,
}

View 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,
}

View 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
View File

@ -0,0 +1,7 @@
mod action;
mod info;
pub mod prelude {
pub use super::action::*;
pub use super::info::*;
}