~starkingdoms/starkingdoms

ref: 6b3599eccce0aeb36f457c69de06b87ee53f725e starkingdoms/crates/server/src/config.rs -rw-r--r-- 3.2 KiB
6b3599ec — core format 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// StarKingdoms.IO, a browser game about drifting through space
//     Copyright (C) 2024 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 <https://www.gnu.org/licenses/>.

use crate::module::component::PartType;
use crate::planet::PlanetType;
use bevy::ecs::system::Resource;
use bevy_rapier2d::rapier::dynamics::IntegrationParameters;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::net::IpAddr;

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

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

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ServerConfig {
    pub tick_time_ms: u64,
    pub world_fixed_timestep: f64,
    pub bind: BindConfig,
    pub max_free_parts: usize,
}

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

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

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

#[derive(Resource, Serialize, Deserialize, Debug, Clone)]
pub struct PlanetsConfig {
    pub planets: HashMap<PlanetType, PlanetConfig>,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PlanetConfig {
    pub size: f32,
    pub mass: f32,
}

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

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct PartConfig {
    pub mass: f32,
    pub energy_capacity: u32,
    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()
    };
}