Skip to content

Commit

Permalink
harmonize time usage and definition
Browse files Browse the repository at this point in the history
  • Loading branch information
Uriopass committed Jan 17, 2024
1 parent 50cbed0 commit d630d83
Show file tree
Hide file tree
Showing 41 changed files with 625 additions and 412 deletions.
2 changes: 1 addition & 1 deletion base_mod/companies.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ data:extend {
recipe = {
consumption = {{"flour", 1}},
production = {{"bread", 1}},
complexity = 100,
complexity = "100s",
storage_multiplier = 5,
},
n_workers = 3,
Expand Down
6 changes: 5 additions & 1 deletion common/src/timestep.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::time::{Duration, Instant};

pub const UP_DT: Duration = Duration::from_millis(20);
const UP_DT: Duration = Duration::from_millis(20);

pub fn debug_up_dt() -> Duration {
UP_DT
}

/// A timestep that can be used to update the game state.
/// It will try to keep a constant update rate.
Expand Down
2 changes: 1 addition & 1 deletion native_app/src/game_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::rendering::immediate::{ImmediateDraw, ImmediateSound};
use common::history::History;
use engine::{Context, FrameContext, Tesselator};
use geom::{vec2, vec3, Camera, LinearColor};
use simulation::utils::time::GameTime;
use simulation::Simulation;

use crate::audio::GameAudio;
Expand All @@ -18,6 +17,7 @@ use crate::inputmap::{Bindings, InputAction, InputMap};
use crate::rendering::{InstancedRender, MapRenderOptions, MapRenderer, OrbitCamera};
use crate::uiworld::{SaveLoadState, UiWorld};
use common::saveload::Encoder;
use prototypes::GameTime;
use simulation::utils::scheduler::SeqSchedule;

pub const VERSION: &str = include_str!("../../VERSION");
Expand Down
9 changes: 3 additions & 6 deletions native_app/src/gui/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ use egui::panel::TopBottomSide;
use egui::{Align2, Color32, Frame, RichText, ScrollArea, TextBuffer, TopBottomPanel};

use geom::Color;
use prototypes::{GameDuration, GameTime};
use simulation::multiplayer::chat::{Message, MessageKind};
use simulation::multiplayer::MultiplayerState;
use simulation::utils::time::{GameInstant, GameTime, SECONDS_PER_REALTIME_SECOND};
use simulation::world_command::WorldCommand;
use simulation::Simulation;

Expand All @@ -20,10 +20,7 @@ pub struct GUIChatState {
pub fn chat(ui: &egui::Context, uiw: &mut UiWorld, sim: &Simulation) {
const MAX_MESSAGES: usize = 30;
let mut state = uiw.write::<GUIChatState>();
let one_minute_ago = GameInstant {
timestamp: sim.read::<GameTime>().instant().timestamp
- 120.0 * SECONDS_PER_REALTIME_SECOND as f64,
};
let five_minute_ago = sim.read::<GameTime>().instant() - GameDuration::from_minutes(5);

let mstate = sim.read::<MultiplayerState>();

Expand All @@ -38,7 +35,7 @@ pub fn chat(ui: &egui::Context, uiw: &mut UiWorld, sim: &Simulation) {

let msgs: Vec<_> = mstate
.chat
.messages_since(one_minute_ago)
.messages_since(five_minute_ago)
.take(MAX_MESSAGES)
.collect();

Expand Down
12 changes: 4 additions & 8 deletions native_app/src/gui/topgui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ use egui::{
use egui_inspect::{Inspect, InspectArgs};
use geom::{Polygon, Vec2};
use prototypes::{
prototypes_iter, BuildingGen, FreightStationPrototype, GoodsCompanyPrototype, ItemID, Money,
Power,
prototypes_iter, BuildingGen, FreightStationPrototype, GameTime, GoodsCompanyPrototype, ItemID,
Money, Power,
};
use serde::{Deserialize, Serialize};
use simulation::economy::Government;
use simulation::map::{
BuildingKind, LanePatternBuilder, LightPolicy, MapProject, TerraformKind, TurnPolicy, Zone,
};
use simulation::utils::time::{GameTime, SECONDS_PER_HOUR};
use simulation::world_command::WorldCommand;
use simulation::Simulation;
use std::sync::atomic::Ordering;
Expand Down Expand Up @@ -638,7 +637,7 @@ impl Gui {
}
ui.add_space(10.0);
}
ui.label(format!("time: {}s", recipe.complexity));
ui.label(format!("time: {}", recipe.duration));
ui.label(format!(
"storage multiplier: {}",
recipe.storage_multiplier
Expand Down Expand Up @@ -708,12 +707,9 @@ impl Gui {
ui.horizontal(|ui| {
ui.label(format!(" Day {}", time.day));
ui.add_space(40.0);
const OFF: i32 = SECONDS_PER_HOUR / 60;
ui.label(format!(
"{:02}:{:02}:{:02}",
time.hour,
time.second / OFF,
time.second % OFF * 60 / OFF
time.hour, time.minute, time.second
));
});

Expand Down
35 changes: 19 additions & 16 deletions native_app/src/gui/windows/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use crate::gui::InspectedEntity;
use crate::uiworld::UiWorld;
use simulation::map_dynamic::ParkingManagement;
use simulation::transportation::TransportGrid;
use simulation::utils::time::{GameTime, Tick, SECONDS_PER_DAY};
use simulation::{Simulation, TrainID};

use crate::inputmap::InputMap;
use egui::Widget;
use engine::{PerfCountersStatic, Tesselator};
use geom::{Camera, Color, LinearColor, Spline3, Vec2};
use prototypes::{GameDuration, GameTime, SECONDS_PER_DAY};
use simulation::map::{
IntersectionID, Map, MapSubscriber, NetworkObjectID, RoadSegmentKind, TraverseKind, UpdateType,
};
Expand Down Expand Up @@ -82,42 +82,45 @@ pub fn debug(
);
drop(objs);

let time = sim.read::<GameTime>().timestamp;
let time = *sim.read::<GameTime>();
let daysecleft = SECONDS_PER_DAY - sim.read::<GameTime>().daytime.daysec();

if ui.small_button("set night").clicked() {
uiworld
.commands()
.set_game_time(GameTime::new(0.1, time + daysecleft as f64));
.set_game_time(time + GameDuration::from_secs(daysecleft as u64));
}

if ui.small_button("set morning").clicked() {
uiworld.commands().set_game_time(GameTime::new(
0.1,
time + daysecleft as f64 + 5.5 * GameTime::HOUR as f64,
));
uiworld.commands().set_game_time(
time + GameDuration::from_secs(
(daysecleft as f64 + 6.0 * GameTime::HOUR as f64) as u64,
),
);
}

if ui.small_button("set day").clicked() {
uiworld.commands().set_game_time(GameTime::new(
0.1,
time + daysecleft as f64 + 12.0 * GameTime::HOUR as f64,
));
uiworld.commands().set_game_time(
time + GameDuration::from_secs(
(daysecleft as f64 + 12.0 * GameTime::HOUR as f64) as u64,
),
);
}

if ui.small_button("set dawn").clicked() {
uiworld.commands().set_game_time(GameTime::new(
0.1,
time + daysecleft as f64 + 21.7 * GameTime::HOUR as f64,
));
uiworld.commands().set_game_time(
time + GameDuration::from_secs(
(daysecleft as f64 + 21.7 * GameTime::HOUR as f64) as u64,
),
);
}

ui.label(format!(
"World timestamp: {:.1}",
sim.read::<GameTime>().timestamp
));

ui.label(format!("Tick: {}", sim.read::<Tick>().0));
ui.label(format!("Tick: {}", time.tick));

let timings = uiworld.read::<Timings>();
let mouse = uiworld.read::<InputMap>().unprojected;
Expand Down
4 changes: 2 additions & 2 deletions native_app/src/gui/windows/economy.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::uiworld::UiWorld;
use common::timestep::UP_DT;
use egui::{Align2, Color32, Ui};
use egui_plot::{Line, PlotPoints};
use geom::Color;
use prototypes::DELTA_F64;
use simulation::economy::{
EcoStats, ItemHistories, Market, HISTORY_SIZE, LEVEL_FREQS, LEVEL_NAMES,
};
Expand Down Expand Up @@ -97,7 +97,7 @@ pub fn economy(window: egui::Window<'_>, ui: &egui::Context, uiw: &mut UiWorld,
}
});

let seconds_per_step = LEVEL_FREQS[state.curlevel] as f64 * UP_DT.as_secs_f64();
let seconds_per_step = LEVEL_FREQS[state.curlevel] as f64 * DELTA_F64;
let xs: Vec<f64> = (0..HISTORY_SIZE)
.map(|i| i as f64 * seconds_per_step)
.collect();
Expand Down
10 changes: 0 additions & 10 deletions prototypes/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
#[macro_export]
macro_rules! export_mods {
{$(mod $v: ident;)+} => {
$(
mod $v;
pub use $v::*;
)+
};
}

#[macro_export]
macro_rules! gen_prototypes {
($($name:ident : $id:ident = $t:ty $(=> $parent_id:ident)?,)+) => {
Expand Down
15 changes: 9 additions & 6 deletions prototypes/src/prototypes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
crate::export_mods! {
mod company;
mod item;
mod solar;
mod freightstation;
}
mod company;
mod freightstation;
mod item;
mod solar;

pub use company::*;
pub use freightstation::*;
pub use item::*;
pub use solar::*;

crate::gen_prototypes!(
companies: GoodsCompanyID = GoodsCompanyPrototype,
Expand Down
20 changes: 13 additions & 7 deletions prototypes/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
crate::export_mods! {
mod power;
mod recipe;
mod zone;
mod money;
mod size;
}
mod money;
mod power;
mod recipe;
mod size;
mod time;
mod zone;

pub use money::*;
pub use power::*;
pub use recipe::*;
pub use size::*;
pub use time::*;
pub use zone::*;
16 changes: 16 additions & 0 deletions prototypes/src/types/money.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,19 @@ impl Div<i64> for Money {
Money(self.0 / rhs)
}
}

impl Mul<f64> for Money {
type Output = Money;

fn mul(self, rhs: f64) -> Self::Output {
Money((self.0 as f64 * rhs) as i64)
}
}

impl Mul<Money> for f64 {
type Output = Money;

fn mul(self, rhs: Money) -> Self::Output {
rhs * self
}
}
8 changes: 4 additions & 4 deletions prototypes/src/types/recipe.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{get_with_err, ItemID};
use crate::{get_with_err, GameDuration, ItemID};
use egui_inspect::Inspect;
use mlua::{FromLua, Lua, Table, Value};

Expand Down Expand Up @@ -37,8 +37,8 @@ pub struct Recipe {
pub consumption: Vec<RecipeItem>,
pub production: Vec<RecipeItem>,

/// Time to execute the recipe when the facility is at full capacity, in seconds
pub complexity: i32,
/// Time to execute the recipe when the facility is at full capacity
pub duration: GameDuration,

/// Quantity to store per production in terms of quantity produced. So if it takes 1ton of flour to make
/// 1 ton of bread. A storage multiplier of 3 means 3 tons of bread will be stored before stopping to
Expand All @@ -52,7 +52,7 @@ impl<'lua> FromLua<'lua> for Recipe {
Ok(Self {
consumption: get_with_err(&table, "consumption")?,
production: get_with_err(&table, "production")?,
complexity: get_with_err(&table, "complexity")?,
duration: get_with_err(&table, "duration")?,
storage_multiplier: get_with_err(&table, "storage_multiplier")?,
})
}
Expand Down
Loading

0 comments on commit d630d83

Please sign in to comment.