Merge branch 'zesterer/relative-time-cmd' into 'master'

Made /time relative to the current day for day-relative times

See merge request veloren/veloren!1879
This commit is contained in:
Joshua Barretto 2021-03-11 19:25:37 +00:00
commit 155bb7a146
2 changed files with 39 additions and 11 deletions

View File

@ -46,6 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Giant tree sites - Giant tree sites
- Reset button for graphics settings - Reset button for graphics settings
- Gave weapons critical strike {chance, multiplier} stats - Gave weapons critical strike {chance, multiplier} stats
- A system to add glow and reflection effects to figures (i.e: characters, armour, weapons, etc.)
### Changed ### Changed
@ -72,6 +73,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Improved the tree spawning model to allow for overlapping forests - Improved the tree spawning model to allow for overlapping forests
- Changed sunlight (and, in general, static light) propagation through blocks to allow for more material properties - Changed sunlight (and, in general, static light) propagation through blocks to allow for more material properties
- Overhauled the sceptre - Overhauled the sceptre
- Make the /time command relative to the current day
### Removed ### Removed

View File

@ -513,25 +513,54 @@ fn handle_time(
args: String, args: String,
action: &ChatCommand, action: &ChatCommand,
) { ) {
const DAY: u64 = 86400;
let time_in_seconds = server.state.ecs_mut().read_resource::<TimeOfDay>().0;
let current_day = time_in_seconds as u64 / DAY;
let day_start = (current_day * DAY) as f64;
// Find the next occurence of the given time in the day/night cycle
let next_cycle = |time| {
let new_time = day_start + time;
new_time
+ if new_time < time_in_seconds {
DAY as f64
} else {
0.0
}
};
let time = scan_fmt_some!(&args, &action.arg_fmt(), String); let time = scan_fmt_some!(&args, &action.arg_fmt(), String);
let new_time = match time.as_deref() { let new_time = match time.as_deref() {
Some("midnight") => NaiveTime::from_hms(0, 0, 0).num_seconds_from_midnight() as f64, Some("midnight") => {
Some("night") => NaiveTime::from_hms(20, 0, 0).num_seconds_from_midnight() as f64, next_cycle(NaiveTime::from_hms(0, 0, 0).num_seconds_from_midnight() as f64)
Some("dawn") => NaiveTime::from_hms(5, 0, 0).num_seconds_from_midnight() as f64, },
Some("morning") => NaiveTime::from_hms(8, 0, 0).num_seconds_from_midnight() as f64, Some("night") => {
Some("day") => NaiveTime::from_hms(10, 0, 0).num_seconds_from_midnight() as f64, next_cycle(NaiveTime::from_hms(20, 0, 0).num_seconds_from_midnight() as f64)
Some("noon") => NaiveTime::from_hms(12, 0, 0).num_seconds_from_midnight() as f64, },
Some("dusk") => NaiveTime::from_hms(17, 0, 0).num_seconds_from_midnight() as f64, Some("dawn") => next_cycle(NaiveTime::from_hms(5, 0, 0).num_seconds_from_midnight() as f64),
Some("morning") => {
next_cycle(NaiveTime::from_hms(8, 0, 0).num_seconds_from_midnight() as f64)
},
Some("day") => next_cycle(NaiveTime::from_hms(10, 0, 0).num_seconds_from_midnight() as f64),
Some("noon") => {
next_cycle(NaiveTime::from_hms(12, 0, 0).num_seconds_from_midnight() as f64)
},
Some("dusk") => {
next_cycle(NaiveTime::from_hms(17, 0, 0).num_seconds_from_midnight() as f64)
},
Some(n) => match n.parse() { Some(n) => match n.parse() {
Ok(n) => n, Ok(n) => n,
Err(_) => match NaiveTime::parse_from_str(n, "%H:%M") { Err(_) => match NaiveTime::parse_from_str(n, "%H:%M") {
Ok(time) => time.num_seconds_from_midnight() as f64, // Relative to current day
Ok(time) => next_cycle(time.num_seconds_from_midnight() as f64),
// Accept `u12345`, seconds since midnight day 0 // Accept `u12345`, seconds since midnight day 0
Err(_) => match n Err(_) => match n
.get(1..) .get(1..)
.filter(|_| n.starts_with('u')) .filter(|_| n.starts_with('u'))
.and_then(|n| n.trim_start_matches('u').parse::<u64>().ok()) .and_then(|n| n.trim_start_matches('u').parse::<u64>().ok())
{ {
// Absolute time (i.e: since in-game epoch)
Some(n) => n as f64, Some(n) => n as f64,
None => { None => {
server.notify_client( server.notify_client(
@ -547,8 +576,6 @@ fn handle_time(
}, },
}, },
None => { None => {
let time_in_seconds = server.state.ecs_mut().read_resource::<TimeOfDay>().0;
// Would this ever change? Perhaps in a few hundred thousand years some // Would this ever change? Perhaps in a few hundred thousand years some
// game archeologists of the future will resurrect the best game of all // game archeologists of the future will resurrect the best game of all
// time which, obviously, would be Veloren. By that time, the inescapable // time which, obviously, would be Veloren. By that time, the inescapable
@ -588,7 +615,6 @@ fn handle_time(
// it! Everybody was henceforth happy until the end of time. // it! Everybody was henceforth happy until the end of time.
// //
// This one's for you, xMac ;) // This one's for you, xMac ;)
const DAY: u64 = 86400;
let current_time = NaiveTime::from_num_seconds_from_midnight_opt( let current_time = NaiveTime::from_num_seconds_from_midnight_opt(
// Wraps around back to 0s if it exceeds 24 hours (24 hours = 86400s) // Wraps around back to 0s if it exceeds 24 hours (24 hours = 86400s)
(time_in_seconds as u64 % DAY) as u32, (time_in_seconds as u64 % DAY) as u32,