From 4daa8be06437c36f0da4a00a9fc88839fe639b12 Mon Sep 17 00:00:00 2001 From: ghostlyzsh Date: Wed, 24 Jul 2024 23:19:12 -0500 Subject: [PATCH] basic organizational fixes --- server/src/component.rs | 87 +-- server/src/config.rs | 36 +- server/src/main.rs | 1238 +------------------------------- server/src/module/component.rs | 78 ++ server/src/module/mod.rs | 666 +++++++++++++++++ server/src/module/save.rs | 343 +++++++++ server/src/module/thruster.rs | 168 +++++ server/src/packet.rs | 3 +- server/src/part.rs | 32 - server/src/planet.rs | 76 ++ server/src/player/mod.rs | 0 11 files changed, 1390 insertions(+), 1337 deletions(-) create mode 100644 server/src/module/component.rs create mode 100644 server/src/module/mod.rs create mode 100644 server/src/module/save.rs create mode 100644 server/src/module/thruster.rs delete mode 100644 server/src/part.rs create mode 100644 server/src/planet.rs create mode 100644 server/src/player/mod.rs diff --git a/server/src/component.rs b/server/src/component.rs index 3983e9ec4f817720c947d0c75aa60b3cce9df910..b39b82716a70e7dd89a272acfb2e5651655302aa 100644 --- a/server/src/component.rs +++ b/server/src/component.rs @@ -17,63 +17,8 @@ use std::net::SocketAddr; use bevy::prelude::*; use serde::{Deserialize, Serialize}; -use starkingdoms_common::PartType as c_PartType; -#[derive(Component, Clone, Copy, Serialize, Deserialize, Debug, PartialEq, Eq, Hash)] -pub enum PlanetType { - Earth, - Moon, - Mars, -} - -#[derive(Component, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Debug)] -pub enum PartType { - Placeholder, - Hearty, - Cargo, - Hub, - LandingThruster, - LandingThrusterSuspension, -} -impl From for c_PartType { - fn from(value: PartType) -> Self { - match value { - PartType::Placeholder => c_PartType::Placeholder, - PartType::Hearty => c_PartType::Hearty, - PartType::Cargo => c_PartType::Cargo, - PartType::Hub => c_PartType::Hub, - PartType::LandingThruster => c_PartType::LandingThruster, - PartType::LandingThrusterSuspension => c_PartType::LandingThrusterSuspension, - } - } -} -impl From for PartType { - fn from(value: c_PartType) -> Self { - match value { - c_PartType::Placeholder => PartType::Placeholder, - c_PartType::Hearty => PartType::Hearty, - c_PartType::Cargo => PartType::Cargo, - c_PartType::Hub => PartType::Hub, - c_PartType::LandingThruster => PartType::LandingThruster, - c_PartType::LandingThrusterSuspension => PartType::LandingThrusterSuspension, - } - } -} - -#[derive(Component, Clone, Debug)] -pub struct Attach { - pub associated_player: Option, - pub parent: Option, - pub children: [Option; 4], -} - -#[derive(Component, Clone, Debug)] -pub struct LooseAttach { - pub children: [Option; 4], -} - -#[derive(Component, Clone, Copy, PartialEq, Debug)] -pub struct CanAttach(pub u8); // each bit means a slot able to attach to +use crate::module::component::{Attach, PartBundle}; #[derive(Component, Clone, Copy, Serialize, Deserialize, Debug, Default)] pub struct Input { @@ -94,23 +39,6 @@ pub struct Player { pub energy: u32, } -#[derive(Bundle)] -pub struct PlanetBundle { - pub planet_type: PlanetType, - pub transform: TransformBundle, -} - -#[derive(Component, Copy, Clone)] -pub struct PartFlags { - pub attached: bool, -} - -#[derive(Bundle)] -pub struct PartBundle { - pub transform: TransformBundle, - pub part_type: PartType, - pub flags: PartFlags, -} #[derive(Bundle)] pub struct PlayerBundle { @@ -119,19 +47,6 @@ pub struct PlayerBundle { pub attach: Attach, } -#[derive(Resource)] -pub struct ModuleTimer(pub Timer); -impl ModuleTimer { - pub fn new() -> Self { - Self(Timer::from_seconds(0.3, TimerMode::Repeating)) - } -} -impl Default for ModuleTimer { - fn default() -> Self { - Self::new() - } -} - #[derive(Resource)] pub struct AppKeys { pub app_key: Vec, diff --git a/server/src/config.rs b/server/src/config.rs index 6d3a62655335f152c1d691f993efebe43a87c1c7..4c50a5c593fa36c0556cfd5d0179a790be3d4f5f 100644 --- a/server/src/config.rs +++ b/server/src/config.rs @@ -14,7 +14,8 @@ // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . -use crate::component::{PartType, PlanetType}; +use crate::module::component::PartType; +use crate::planet::PlanetType; use bevy::ecs::system::Resource; use bevy_rapier2d::rapier::dynamics::IntegrationParameters; use serde::{Deserialize, Serialize}; @@ -88,3 +89,36 @@ pub struct PartConfig { pub thruster_force: f32, pub thruster_energy: u32, } + +#[macro_export] +macro_rules! mass { + ($p:expr) => { + match $crate::parts_config().parts.get(&$p) { + Some(v) => v.mass, + None => 1.0, + } + }; +} + +#[macro_export] +macro_rules! capacity { + ($p:expr) => { + match $crate::parts_config().parts.get(&$p) { + Some(v) => v.energy_capacity, + None => 0, + } + }; +} + +#[macro_export] +macro_rules! planet { + ($t:expr) => { + $crate::planets_config().planets.get(&$t).unwrap() + }; +} +#[macro_export] +macro_rules! part { + ($t:expr) => { + $crate::parts_config().parts.get(&$t).unwrap() + }; +} diff --git a/server/src/main.rs b/server/src/main.rs index 92933f40a40882175f350945933cdb8a73a829fe..495a6f7e8b08f5f9d5ca11f17a61277340b60c37 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -17,8 +17,6 @@ #![allow(clippy::too_many_arguments)] // bevy :( #![allow(clippy::only_used_in_recursion)] // todo: remove this -use std::collections::HashMap; - use crate::mathutil::rot2d; use crate::ws::{StkTungsteniteServerConfig, StkTungsteniteServerPlugin, WsEvent}; use bevy::math::{vec2, vec3}; @@ -32,13 +30,16 @@ use bevy_rapier2d::prelude::*; use component::*; use hmac::{Hmac, Mac}; use jwt::VerifyWithKey; +use module::component::{Attach, CanAttach, LooseAttach, ModuleTimer, PartBundle, PartFlags, PartType}; +use module::save::{construct_save_data, load_savefile}; +use module::thruster::search_thrusters; +use module::{attach_on_module_tree, despawn_module_tree, detach_recursive}; use packet::*; +use planet::PlanetType; use rand::Rng; use serde::{Deserialize, Serialize}; use sha2::Sha256; -use starkingdoms_common::SaveModule; use starkingdoms_common::{pack_savefile, unpack_savefile, SaveData}; -use std::f32::consts::PI; use std::fs; use crate::config::{PartsConfig, PhysicsSolver, PlanetsConfig, StkConfig}; @@ -50,14 +51,19 @@ mod config; pub mod macros; pub mod mathutil; pub mod packet; -pub mod part; pub mod ws; +pub mod planet; +pub mod module; +pub mod player; struct StkPluginGroup; +// factor to multiply positions by to send to the client pub static CLIENT_SCALE: f32 = 50.0; +// half size of hearty pub static PART_HALF_SIZE: f32 = 25.0; +// good ol' classic almost useless but still necessary code static _SERVER_CONFIG: OnceLock = OnceLock::new(); #[inline] pub fn server_config() -> StkConfig { @@ -74,6 +80,7 @@ pub fn planets_config() -> PlanetsConfig { _PLANETS_CONFIG.get().unwrap().clone() } +// group main stk plugins together #[cfg(debug_assertions)] impl PluginGroup for StkPluginGroup { fn build(self) -> PluginGroupBuilder { @@ -105,6 +112,7 @@ impl PluginGroup for StkPluginGroup { } } +// auth token #[derive(Serialize, Deserialize, Debug, Clone)] pub struct UserToken { pub id: i64, @@ -119,6 +127,7 @@ fn main() { let parts_config = fs::read_to_string("/etc/starkingdoms/parts.toml").unwrap(); let planets_config = fs::read_to_string("/etc/starkingdoms/planets.toml").unwrap(); + // put config in variables let server_config: StkConfig = toml::from_str(&server_config).unwrap(); _SERVER_CONFIG.set(server_config.clone()).unwrap(); let parts_config: PartsConfig = toml::from_str(&parts_config).unwrap(); @@ -126,6 +135,7 @@ fn main() { let planets_config: PlanetsConfig = toml::from_str(&planets_config).unwrap(); _PLANETS_CONFIG.set(planets_config.clone()).unwrap(); + // make the game, start it in .run() App::new() .insert_resource(AppKeys { app_key: server_config.security.app_key.as_bytes().to_vec(), @@ -148,23 +158,24 @@ fn main() { )) .add_plugins(StkTungsteniteServerPlugin) .add_systems(Startup, setup_integration_parameters) - .add_systems(Startup, spawn_planets) - .add_systems(FixedUpdate, module_spawn) + .add_systems(Startup, planet::spawn_planets) + .add_systems(FixedUpdate, module::module_spawn) .add_systems(Update, on_message) .add_systems(Update, on_close) .add_systems(FixedUpdate, send_player_energy) .add_systems(FixedUpdate, on_position_change) .add_systems( FixedUpdate, - (break_modules, gravity_update, player_input_update).chain(), + (module::break_modules, gravity_update, player_input_update).chain(), ) - .add_systems(FixedUpdate, save_eligibility) - .add_systems(FixedUpdate, convert_modules) + .add_systems(FixedUpdate, module::save::save_eligibility) + .add_systems(FixedUpdate, module::convert_modules) .insert_resource(Time::::from_seconds( server_config.server.world_fixed_timestep, )) .run(); + // game is done running info!("Goodbye!"); } @@ -184,117 +195,6 @@ fn setup_integration_parameters(mut context: ResMut, server_confi } } } -fn spawn_planets(mut commands: Commands) { - info!("Spawning planets"); - let earth_pos = Transform::from_xyz(0.0, 0.0, 0.0); - commands - .spawn(PlanetBundle { - planet_type: PlanetType::Earth, - transform: TransformBundle::from(earth_pos), - }) - .insert(Collider::ball(planet!(PlanetType::Earth).size)) - .insert(AdditionalMassProperties::Mass( - planet!(PlanetType::Earth).mass, - )) - .insert(ReadMassProperties::default()) - .with_children(|children| { - children - .spawn(Collider::ball(planet!(PlanetType::Earth).size + 0.3)) - .insert(ActiveEvents::COLLISION_EVENTS) - .insert(Sensor); - }) - .insert(RigidBody::Fixed); - let moon_pos = Transform::from_xyz(50.0, 20.0, 0.0); - commands - .spawn(PlanetBundle { - planet_type: PlanetType::Moon, - transform: TransformBundle::from(moon_pos), - }) - .insert(Collider::ball(planet!(PlanetType::Moon).size)) - .insert(AdditionalMassProperties::Mass( - planet!(PlanetType::Moon).mass, - )) - .insert(ReadMassProperties::default()) - .with_children(|children| { - children - .spawn(Collider::ball(planet!(PlanetType::Moon).size + 0.1)) - .insert(ActiveEvents::COLLISION_EVENTS) - .insert(Sensor); - }) - .insert(RigidBody::Fixed); - let mars_pos = Transform::from_xyz(-50.0, 300.0, 0.0); - commands - .spawn(PlanetBundle { - planet_type: PlanetType::Mars, - transform: TransformBundle::from(mars_pos), - }) - .insert(Collider::ball(planet!(PlanetType::Mars).size)) - .insert(AdditionalMassProperties::Mass( - planet!(PlanetType::Mars).mass, - )) - .insert(ReadMassProperties::default()) - .with_children(|children| { - children - .spawn(Collider::ball(planet!(PlanetType::Mars).size + 0.1)) - .insert(ActiveEvents::COLLISION_EVENTS) - .insert(Sensor); - }) - .insert(RigidBody::Fixed); -} -fn module_spawn( - mut commands: Commands, - time: Res