// StarKingdoms.IO, a browser game about drifting through space // Copyright (C) 2023 ghostly_zsh, TerraMaster85, core // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Affero General Public License for more details. // // You should have received a copy of the GNU Affero General Public License // along with this program. If not, see . #![allow(clippy::type_complexity)] // bevy :( #![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}; use bevy::{ app::{PluginGroupBuilder, ScheduleRunnerPlugin}, ecs::event::ManualEventReader, prelude::*, time::TimePlugin, }; use bevy_rapier2d::prelude::*; use component::*; use hmac::{Hmac, Mac}; use jwt::VerifyWithKey; use packet::*; 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}; use std::sync::OnceLock; use std::time::Duration; pub mod component; mod config; pub mod macros; pub mod mathutil; pub mod packet; pub mod part; pub mod ws; struct StkPluginGroup; pub static CLIENT_SCALE: f32 = 50.0; pub static PART_HALF_SIZE: f32 = 25.0; static _SERVER_CONFIG: OnceLock = OnceLock::new(); #[inline] pub fn server_config() -> StkConfig { _SERVER_CONFIG.get().unwrap().clone() } static _PARTS_CONFIG: OnceLock = OnceLock::new(); #[inline] pub fn parts_config() -> PartsConfig { _PARTS_CONFIG.get().unwrap().clone() } static _PLANETS_CONFIG: OnceLock = OnceLock::new(); #[inline] pub fn planets_config() -> PlanetsConfig { _PLANETS_CONFIG.get().unwrap().clone() } #[cfg(debug_assertions)] impl PluginGroup for StkPluginGroup { fn build(self) -> PluginGroupBuilder { PluginGroupBuilder::start::() .add(TaskPoolPlugin::default()) .add(TypeRegistrationPlugin) .add(FrameCountPlugin) .add(TimePlugin) .add(ScheduleRunnerPlugin::run_loop(Duration::from_millis( server_config().server.tick_time_ms, ))) .add(bevy::log::LogPlugin { level: bevy::log::Level::DEBUG, filter: "wgpu=error,bevy_render=info,bevy_ecs=trace".to_string(), update_subscriber: None, }) } } #[cfg(not(debug_assertions))] impl PluginGroup for StkPluginGroup { fn build(self) -> PluginGroupBuilder { PluginGroupBuilder::start::() .add(TaskPoolPlugin::default()) .add(TypeRegistrationPlugin) .add(FrameCountPlugin) .add(TimePlugin) .add(ScheduleRunnerPlugin::run_loop(Duration::from_millis(1))) } } #[derive(Serialize, Deserialize, Debug, Clone)] pub struct UserToken { pub id: i64, pub username: String, pub permission_level: i32, pub expires: std::time::SystemTime, } fn main() { // read the server main config let server_config = fs::read_to_string("/etc/starkingdoms/config.toml").unwrap(); let parts_config = fs::read_to_string("/etc/starkingdoms/parts.toml").unwrap(); let planets_config = fs::read_to_string("/etc/starkingdoms/planets.toml").unwrap(); 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(); _PARTS_CONFIG.set(parts_config.clone()).unwrap(); let planets_config: PlanetsConfig = toml::from_str(&planets_config).unwrap(); _PLANETS_CONFIG.set(planets_config.clone()).unwrap(); App::new() .insert_resource(AppKeys { app_key: server_config.security.app_key.as_bytes().to_vec(), }) .insert_resource(StkTungsteniteServerConfig { addr: server_config.server.bind.ip, port: server_config.server.bind.port, }) .insert_resource(server_config.clone()) .insert_resource(parts_config) .insert_resource(planets_config) .add_plugins(StkPluginGroup) .insert_resource(RapierConfiguration { gravity: Vect { x: 0.0, y: 0.0 }, ..Default::default() }) .init_resource::() .add_plugins(RapierPhysicsPlugin::::pixels_per_meter( server_config.world.pixels_per_meter, )) .add_plugins(StkTungsteniteServerPlugin) .add_systems(Startup, setup_integration_parameters) .add_systems(Startup, spawn_planets) .add_systems(FixedUpdate, 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(), ) .add_systems(FixedUpdate, save_eligibility) .add_systems(FixedUpdate, convert_modules) .insert_resource(Time::::from_seconds( server_config.server.world_fixed_timestep, )) .run(); info!("Goodbye!"); } fn setup_integration_parameters(mut context: ResMut, server_config: Res) { context.integration_parameters = server_config.physics.parameters; match server_config.physics.solver { PhysicsSolver::SmallstepPGS => { context .integration_parameters .switch_to_small_steps_pgs_solver(); } PhysicsSolver::OldPGS => { context .integration_parameters .switch_to_standard_pgs_solver(); } } } 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