pub const PROTOCOL_VERSION: u32 = 3;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct NetworkId(pub u64);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LocalId(pub u64);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct ClientId(pub u64);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct ComponentKind(pub u16);
pub const INPUT_COMMAND_KIND: ComponentKind = ComponentKind(128);
pub const MINING_BEAM_KIND: ComponentKind = ComponentKind(1024);
pub const CARGO_HOLD_KIND: ComponentKind = ComponentKind(1025);
pub const ASTEROID_KIND: ComponentKind = ComponentKind(1026);
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[repr(C)]
pub struct Transform {
pub x: f32,
pub y: f32,
pub z: f32,
pub rotation: f32,
pub entity_type: u16,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[repr(u8)]
pub enum ShipClass {
Interceptor = 0,
Dreadnought = 1,
Hauler = 2,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct WeaponId(pub u8);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct SectorId(pub u64);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[repr(u8)]
pub enum OreType {
RawOre = 0,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[repr(u8)]
pub enum ProjectileType {
PulseLaser = 0,
SeekerMissile = 1,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[repr(u8)]
pub enum AIState {
Patrol = 0,
Aggro = 1,
Combat = 2,
Return = 3,
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum RespawnLocation {
NearestSafeZone,
Station(u64),
Coordinate(f32, f32),
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub enum PlayerInputKind {
Move { x: f32, y: f32 },
ToggleMining { target: NetworkId },
FirePrimary,
}
pub const MAX_ACTIONS: usize = 128;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InputCommand {
pub tick: u64,
pub actions: Vec<PlayerInputKind>,
}
impl InputCommand {
#[must_use]
pub fn clamped(mut self) -> Self {
for action in &mut self.actions {
if let PlayerInputKind::Move { x, y } = action {
*x = x.clamp(-1.0, 1.0);
*y = y.clamp(-1.0, 1.0);
}
}
self
}
pub fn validate(&self) -> Result<(), &'static str> {
if self.actions.len() > MAX_ACTIONS {
return Err("Too many actions in InputCommand");
}
Ok(())
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub struct MiningBeam {
pub active: bool,
pub target: Option<NetworkId>,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub struct CargoHold {
pub ore_count: u16,
pub capacity: u16,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Default)]
pub struct Asteroid {
pub ore_remaining: u16,
pub total_capacity: u16,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub struct ShipStats {
pub hp: u16,
pub max_hp: u16,
pub shield: u16,
pub max_shield: u16,
pub energy: u16,
pub max_energy: u16,
pub shield_regen_per_s: u16,
pub energy_regen_per_s: u16,
}
impl Default for ShipStats {
fn default() -> Self {
Self {
hp: 100,
max_hp: 100,
shield: 100,
max_shield: 100,
energy: 100,
max_energy: 100,
shield_regen_per_s: 0,
energy_regen_per_s: 0,
}
}
}
use std::sync::atomic::{AtomicU64, Ordering};
use thiserror::Error;
#[derive(Debug, Error, PartialEq, Eq)]
pub enum AllocatorError {
#[error("NetworkId overflow (reached u64::MAX)")]
Overflow,
#[error("NetworkId allocator exhausted (reached limit)")]
Exhausted,
}
#[derive(Debug)]
pub struct NetworkIdAllocator {
start_id: u64,
next: AtomicU64,
}
impl Default for NetworkIdAllocator {
fn default() -> Self {
Self::new(1)
}
}
impl NetworkIdAllocator {
#[must_use]
pub fn new(start_id: u64) -> Self {
Self {
start_id,
next: AtomicU64::new(start_id),
}
}
pub fn allocate(&self) -> Result<NetworkId, AllocatorError> {
let val = self
.next
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |curr| {
if curr == u64::MAX {
None
} else {
Some(curr + 1)
}
})
.map_err(|_| AllocatorError::Overflow)?;
if val == 0 {
return Err(AllocatorError::Exhausted);
}
Ok(NetworkId(val))
}
pub fn reset(&self) {
self.next.store(self.start_id, Ordering::Relaxed);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_primitive_derives() {
let nid1 = NetworkId(42);
let nid2 = nid1;
assert_eq!(nid1, nid2);
let lid1 = LocalId(42);
let lid2 = LocalId(42);
assert_eq!(lid1, lid2);
let cid = ClientId(99);
assert_eq!(format!("{cid:?}"), "ClientId(99)");
let kind = ComponentKind(1);
assert_eq!(kind.0, 1);
}
#[test]
fn test_input_command_clamping() {
let cmd = InputCommand {
tick: 1,
actions: vec![PlayerInputKind::Move { x: 2.0, y: -5.0 }],
};
let clamped = cmd.clamped();
if let PlayerInputKind::Move { x, y } = clamped.actions[0] {
assert!((x - 1.0).abs() < f32::EPSILON);
assert!((y - -1.0).abs() < f32::EPSILON);
} else {
panic!("Expected Move action");
}
let valid = InputCommand {
tick: 1,
actions: vec![PlayerInputKind::Move { x: 0.5, y: -0.2 }],
};
let clamped = valid.clamped();
if let PlayerInputKind::Move { x, y } = clamped.actions[0] {
assert!((x - 0.5).abs() < f32::EPSILON);
assert!((y - -0.2).abs() < f32::EPSILON);
} else {
panic!("Expected Move action");
}
}
#[test]
fn test_ship_stats_non_zero_default() {
let stats = ShipStats::default();
assert!(stats.max_hp > 0);
assert!(stats.max_shield > 0);
assert!(stats.max_energy > 0);
assert_eq!(stats.hp, stats.max_hp);
}
}