veloren_common/
resources.rs

1use crate::{comp::Pos, shared_server_config::ServerConstants, time::DayPeriod};
2use serde::{Deserialize, Serialize};
3use specs::Entity;
4use std::ops::{Mul, MulAssign};
5use vek::Vec3;
6
7/// A resource that stores the time of day.
8#[derive(Copy, Clone, Debug, Serialize, Deserialize, Default)]
9pub struct TimeOfDay(pub f64);
10impl TimeOfDay {
11    pub fn new(t: f64) -> Self { TimeOfDay(t) }
12
13    fn get_angle_rad(self) -> f32 {
14        const TIME_FACTOR: f64 = (std::f64::consts::PI * 2.0) / (3600.0 * 24.0);
15        ((self.0 * TIME_FACTOR) % (std::f64::consts::PI * 2.0)) as f32
16    }
17
18    /// Computes the direction of light from the sun based on the time of day.
19    pub fn get_sun_dir(self) -> Vec3<f32> {
20        let angle_rad = self.get_angle_rad();
21        Vec3::new(-angle_rad.sin(), 0.0, angle_rad.cos())
22    }
23
24    /// Computes the direction of light from the moon based on the time of day.
25    pub fn get_moon_dir(self) -> Vec3<f32> {
26        let angle_rad = self.get_angle_rad();
27        -Vec3::new(-angle_rad.sin(), 0.0, angle_rad.cos() - 0.5).normalized()
28    }
29
30    /// Get the current [`DayPeriod`].
31    pub fn day_period(&self) -> DayPeriod { DayPeriod::from(self.0) }
32}
33
34impl TimeOfDay {
35    pub fn day(&self) -> f64 { self.0.rem_euclid(24.0 * 3600.0) }
36}
37
38/// A resource that stores the tick (i.e: physics) time.
39#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq, PartialOrd)]
40pub struct Time(pub f64);
41
42impl Time {
43    pub fn add_seconds(self, seconds: f64) -> Self { Self(self.0 + seconds) }
44
45    pub fn add_minutes(self, minutes: f64) -> Self { Self(self.0 + minutes * 60.0) }
46
47    // Note that this applies in 'game time' and does not respect either real time
48    // or in-game time of day.
49    pub fn add_days(self, days: f64, server_constants: &ServerConstants) -> Self {
50        self.add_seconds(days * 3600.0 * 24.0 / server_constants.day_cycle_coefficient)
51    }
52}
53
54/// A resource that stores the real tick, local to the server/client.
55#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
56pub struct ProgramTime(pub f64);
57
58#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
59pub struct TimeScale(pub f64);
60
61impl Default for TimeScale {
62    fn default() -> Self { Self(1.0) }
63}
64
65/// A resource that stores the time since the previous tick.
66#[derive(Default)]
67pub struct DeltaTime(pub f32);
68
69/// A resource used to indicate a duration of time, in seconds
70#[derive(Copy, Clone, Debug, Default, Serialize, Deserialize, PartialEq)]
71#[serde(transparent)]
72pub struct Secs(pub f64);
73
74impl Mul<f64> for Secs {
75    type Output = Self;
76
77    fn mul(self, mult: f64) -> Self { Self(self.0 * mult) }
78}
79impl MulAssign<f64> for Secs {
80    fn mul_assign(&mut self, mult: f64) { *self = *self * mult; }
81}
82
83#[derive(Default)]
84pub struct EntitiesDiedLastTick(pub Vec<(Entity, Pos)>);
85
86/// A resource that indicates what mode the local game is being played in.
87#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
88pub enum GameMode {
89    /// The game is being played in server mode (i.e: the code is running
90    /// server-side)
91    Server,
92    /// The game is being played in client mode (i.e: the code is running
93    /// client-side)
94    Client,
95    /// The game is being played in singleplayer mode (i.e: both client and
96    /// server at once)
97    // To be used later when we no longer start up an entirely new server for singleplayer
98    Singleplayer,
99}
100
101/// A resource that stores the player's entity (on the client), and None on the
102/// server
103#[derive(Copy, Clone, Default, Debug)]
104pub struct PlayerEntity(pub Option<Entity>);
105
106#[derive(Copy, Clone, Debug, Default, Eq, PartialEq)]
107pub struct PlayerPhysicsSetting {
108    /// true if the client wants server-authoratative physics (e.g. to use
109    /// airships properly)
110    pub client_optin: bool,
111}
112
113impl PlayerPhysicsSetting {
114    /// Indicates that the client wants to use server authoritative physics
115    ///
116    /// NOTE: This is not the only source used to determine whether a client
117    /// should use server authoritative physics, make sure to also check
118    /// `ServerPhysicsForceList` on the server.
119    pub fn server_authoritative_physics_optin(&self) -> bool { self.client_optin }
120}
121
122/// Describe how the map should be generated.
123#[derive(Copy, Clone, Debug, Deserialize, Serialize, PartialEq, Eq, enum_map::Enum)]
124pub enum MapKind {
125    /// The normal square map, with oceans beyond the map edge
126    Square,
127    /// A more circular map, might have more islands
128    Circle,
129}
130
131impl std::fmt::Display for MapKind {
132    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
133        match self {
134            MapKind::Square => f.write_str("Square"),
135            MapKind::Circle => f.write_str("Circle"),
136        }
137    }
138}
139
140/// List of which players are using client-authoratative vs server-authoratative
141/// physics, as a stop-gap until we can use server-authoratative physics for
142/// everyone
143#[derive(Clone, Default, Debug)]
144pub struct PlayerPhysicsSettings {
145    pub settings: hashbrown::HashMap<uuid::Uuid, PlayerPhysicsSetting>,
146}
147
148/// Describe how players interact with other players.
149///
150/// May be removed when we will discover better way
151/// to handle duels and murders
152#[derive(PartialEq, Eq, Copy, Clone, Debug, Deserialize, Serialize)]
153pub enum BattleMode {
154    PvP,
155    PvE,
156}