~starkingdoms/starkingdoms

032f64b33964659ed64df0d63759fd2a70c5e284 — core 1 year, 8 months ago e21a77a
fmt
3 files changed, 66 insertions(+), 40 deletions(-)

M server/src/config.rs
M server/src/main.rs
M server/src/part.rs
M server/src/config.rs => server/src/config.rs +14 -15
@@ 14,24 14,24 @@
//     You should have received a copy of the GNU Affero General Public License
//     along with this program.  If not, see <https://www.gnu.org/licenses/>.

use std::collections::HashMap;
use std::net::IpAddr;
use crate::component::{PartType, PlanetType};
use bevy_rapier2d::rapier::dynamics::IntegrationParameters;
use serde::{Deserialize, Serialize};
use crate::component::{PartType, PlanetType};
use std::collections::HashMap;
use std::net::IpAddr;

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct StkConfig {
    pub security: SecurityConfig,
    pub server: ServerConfig,
    pub world: WorldConfig,
    pub physics: PhysicsConfig
    pub physics: PhysicsConfig,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct SecurityConfig {
    pub app_key: String,
    pub required_permission_level: i32
    pub required_permission_level: i32,
}

#[derive(Serialize, Deserialize, Debug, Clone)]


@@ 39,32 39,31 @@ pub struct ServerConfig {
    pub tick_time_ms: u64,
    pub world_fixed_timestep: f64,
    pub bind: BindConfig,
    pub max_free_parts: usize
    pub max_free_parts: usize,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BindConfig {
    pub ip: IpAddr,
    pub port: u16
    pub port: u16,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct WorldConfig {
    pub gravity: f32,
    pub part_half_size: f32,
    pub pixels_per_meter: f32
    pub pixels_per_meter: f32,
}


#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PhysicsConfig {
    pub parameters: IntegrationParameters,
    pub solver: PhysicsSolver
    pub solver: PhysicsSolver,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum PhysicsSolver {
    SmallstepPGS,
    OldPGS
    OldPGS,
}

#[derive(Serialize, Deserialize, Debug, Clone)]


@@ 74,12 73,12 @@ pub struct PlanetsConfig {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PlanetConfig {
    pub size: f32,
    pub mass: f32
    pub mass: f32,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PartsConfig {
    pub parts: HashMap<PartType, PartConfig>
    pub parts: HashMap<PartType, PartConfig>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]


@@ 87,5 86,5 @@ pub struct PartConfig {
    pub mass: f32,
    pub energy_capacity: u32,
    pub thruster_force: f32,
    pub thruster_energy: u32
}
\ No newline at end of file
    pub thruster_energy: u32,
}

M server/src/main.rs => server/src/main.rs +49 -22
@@ 17,10 17,8 @@
#![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};


@@ 43,17 41,17 @@ use starkingdoms_common::{pack_savefile, unpack_savefile, SaveData};
use std::f32::consts::PI;
use std::fs;

use std::sync::{OnceLock};
use std::time::Duration;
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;
mod config;

struct StkPluginGroup;



@@ 68,7 66,9 @@ impl PluginGroup for StkPluginGroup {
            .add(TypeRegistrationPlugin)
            .add(FrameCountPlugin)
            .add(TimePlugin)
            .add(ScheduleRunnerPlugin::run_loop(Duration::from_millis(server_config().server.tick_time_ms)))
            .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(),


@@ 127,7 127,7 @@ fn main() {
        let planets_config: PlanetsConfig = toml::from_str(&planets_config).unwrap();
        _PLANETS_CONFIG.set(planets_config).unwrap();
    }
    

    let cfg = server_config();

    App::new()


@@ 144,7 144,9 @@ fn main() {
            ..Default::default()
        })
        .init_resource::<ModuleTimer>()
        .add_plugins(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(cfg.world.pixels_per_meter))
        .add_plugins(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(
            cfg.world.pixels_per_meter,
        ))
        .add_plugins(StkTungsteniteServerPlugin)
        .add_systems(Startup, setup_integration_parameters)
        .add_systems(Startup, spawn_planets)


@@ 167,13 169,17 @@ fn main() {

fn setup_integration_parameters(mut context: ResMut<RapierContext>) {
    context.integration_parameters = server_config().physics.parameters;
    

    match server_config().physics.solver {
        PhysicsSolver::SmallstepPGS => {
            context.integration_parameters.switch_to_small_steps_pgs_solver();
        },
            context
                .integration_parameters
                .switch_to_small_steps_pgs_solver();
        }
        PhysicsSolver::OldPGS => {
            context.integration_parameters.switch_to_standard_pgs_solver();
            context
                .integration_parameters
                .switch_to_standard_pgs_solver();
        }
    }
}


@@ 186,7 192,9 @@ fn spawn_planets(mut commands: Commands) {
            transform: TransformBundle::from(earth_pos),
        })
        .insert(Collider::ball(planet!(PlanetType::Earth).size))
        .insert(AdditionalMassProperties::Mass(planet!(PlanetType::Earth).mass))
        .insert(AdditionalMassProperties::Mass(
            planet!(PlanetType::Earth).mass,
        ))
        .insert(ReadMassProperties::default())
        .with_children(|children| {
            children


@@ 202,7 210,9 @@ fn spawn_planets(mut commands: Commands) {
            transform: TransformBundle::from(moon_pos),
        })
        .insert(Collider::ball(planet!(PlanetType::Moon).size))
        .insert(AdditionalMassProperties::Mass(planet!(PlanetType::Moon).mass))
        .insert(AdditionalMassProperties::Mass(
            planet!(PlanetType::Moon).mass,
        ))
        .insert(ReadMassProperties::default())
        .with_children(|children| {
            children


@@ 218,7 228,9 @@ fn spawn_planets(mut commands: Commands) {
            transform: TransformBundle::from(mars_pos),
        })
        .insert(Collider::ball(planet!(PlanetType::Mars).size))
        .insert(AdditionalMassProperties::Mass(planet!(PlanetType::Mars).mass))
        .insert(AdditionalMassProperties::Mass(
            planet!(PlanetType::Mars).mass,
        ))
        .insert(ReadMassProperties::default())
        .with_children(|children| {
            children


@@ 351,7 363,9 @@ fn on_message(
                            }
                        };

                        if claims.permission_level < server_config().security.required_permission_level {
                        if claims.permission_level
                            < server_config().security.required_permission_level
                        {
                            event_queue.push(WsEvent::Send {
                                to: *from,
                                message: Packet::Message { message_type: MessageType::Error, actor: "SERVER".to_string(), content: format!("Permission level {} is too low, {} is required. If your permissions were just changed, you need to log out and log back in for the change to take effect. If you believe this is a mistake, contact StarKingdoms staff.", claims.permission_level, server_config().security.required_permission_level) }.into(),


@@ 465,9 479,15 @@ fn on_message(
                                    translation * CLIENT_SCALE
                                )),
                                radius: match *planet_type {
                                    PlanetType::Earth => planet!(PlanetType::Earth).size * CLIENT_SCALE,
                                    PlanetType::Moon => planet!(PlanetType::Moon).size * CLIENT_SCALE,
                                    PlanetType::Mars => planet!(PlanetType::Mars).size * CLIENT_SCALE,
                                    PlanetType::Earth => {
                                        planet!(PlanetType::Earth).size * CLIENT_SCALE
                                    }
                                    PlanetType::Moon => {
                                        planet!(PlanetType::Moon).size * CLIENT_SCALE
                                    }
                                    PlanetType::Mars => {
                                        planet!(PlanetType::Mars).size * CLIENT_SCALE
                                    }
                                },
                            },
                        ));


@@ 2032,7 2052,10 @@ fn search_thrusters(
                force.torque += thruster_force.torque;
                *energy -= energy_lose_by;
            }
            if 5. * PI / 4. < relative_angle && relative_angle < 7. * PI / 4. && relative_pos.y > 0.48 {
            if 5. * PI / 4. < relative_angle
                && relative_angle < 7. * PI / 4.
                && relative_pos.y > 0.48
            {
                let thruster_force = ExternalForce::at_point(
                    Vec2::new(-force_mult * angle.sin(), force_mult * angle.cos()),
                    transform.translation.xy(),


@@ 2080,7 2103,10 @@ fn search_thrusters(
                force.torque += thruster_force.torque;
                *energy -= energy_lose_by;
            }
            if 5. * PI / 4. < relative_angle && relative_angle < 7. * PI / 4. && relative_pos.y < -0.48 {
            if 5. * PI / 4. < relative_angle
                && relative_angle < 7. * PI / 4.
                && relative_pos.y < -0.48
            {
                let thruster_force = ExternalForce::at_point(
                    Vec2::new(-force_mult * angle.sin(), force_mult * angle.cos()),
                    transform.translation.xy(),


@@ 2122,7 2148,8 @@ fn gravity_update(
            let planet_mass = planet_mp.mass;
            let planet_translate = planet_transform.translation;
            let distance = planet_translate.distance(part_translate);
            let force = server_config().world.gravity * ((part_mass * planet_mass) / (distance * distance));
            let force =
                server_config().world.gravity * ((part_mass * planet_mass) / (distance * distance));
            let direction = (planet_translate - part_translate).normalize() * force;
            /*let gravity_force = ExternalForce::at_point(
                direction.xy(),

M server/src/part.rs => server/src/part.rs +3 -3
@@ 3,7 3,7 @@ macro_rules! mass {
    ($p:expr) => {
        match $crate::parts_config().parts.get(&$p) {
            Some(v) => v.mass,
            None => 1.0
            None => 1.0,
        }
    };
}


@@ 13,7 13,7 @@ macro_rules! capacity {
    ($p:expr) => {
        match $crate::parts_config().parts.get(&$p) {
            Some(v) => v.energy_capacity,
            None => 0
            None => 0,
        }
    };
}


@@ 29,4 29,4 @@ macro_rules! part {
    ($t:expr) => {
        $crate::parts_config().parts.get(&$t).unwrap()
    };
}
\ No newline at end of file
}