Skip to content

Commit

Permalink
Optional region brightness based on server time
Browse files Browse the repository at this point in the history
  • Loading branch information
markusmoenig committed Mar 17, 2023
1 parent edc4c12 commit e4c145a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 4 deletions.
38 changes: 38 additions & 0 deletions core_render/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,44 @@ impl GameRender<'_> {
base_light = property.to_float() as f64;
}

if let Some(property) = region.settings.get(&"lighting") {
fn get_brightness(minutes: i32) -> f32 {
let sunrise = 300; // 5:00 am
let sunset = 1200; // 8:00 pm
let transition_duration = 60; // 1 hour

let daylight_start = sunrise + transition_duration;
let daylight_end = sunset + transition_duration;

if minutes < sunrise || minutes > daylight_end {
return 0.0; // it's dark outside
}

if minutes >= sunrise && minutes <= daylight_start {
// transition from darkness to daylight
let transition_start = sunrise;
let time_since_transition_start = minutes - transition_start;
let brightness = time_since_transition_start as f32 / transition_duration as f32;
return brightness;
} else if minutes >= sunset && minutes <= daylight_end {
// transition from daylight to darkness
let transition_start = sunset;
let time_since_transition_start = minutes - transition_start;
let brightness = 1.0 - time_since_transition_start as f32 / transition_duration as f32;
return brightness;
} else {
return 1.0;
}
}

if let Some(v) = property.as_string() {
if v == "timeofday" {
let daylight = get_brightness(update.date.minutes_in_day) as f64;
base_light = base_light.max(daylight);
}
}
}

// Compute the light_map
let mut light_map : FxHashMap<(isize, isize), f64> = FxHashMap::default();
if let Some(lights) = self.lights.get(&region.id) {
Expand Down
7 changes: 6 additions & 1 deletion core_server/src/gamedata/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,12 @@ pub fn update_region_sink(sink: &mut PropertySink) {
sink.properties.insert(1,Property::new_string("movement".to_string(), "pixel".to_string()));
}

if sink.contains("lighting") == false {
sink.push(Property::new_string("lighting".to_string(), "timeofday".into()));
}

if sink.contains("base_lighting") == false {
sink.push(Property::new_float("base_lighting".to_string(), 1.0));
sink.push(Property::new_float("base_lighting".to_string(), 0.5));
}

if sink.contains("visibility") == false {
Expand All @@ -508,6 +512,7 @@ pub fn generate_region_sink_descriptions() -> FxHashMap<String, Vec<String>> {

map.insert("background".to_string(), vec!["The background color of the region".to_string()]);
map.insert("movement".to_string(), vec!["Use \"tile\" for tile based movement or \"pixel\" for sub-tile movement.".to_string()]);
map.insert("lighting".to_string(), vec!["The lighting mode. \"timeofday\" for base_lighting + auto time of day lighting or \"basic\" for only using the base lighting (dungeons etc.).".to_string()]);
map.insert("base_lighting".to_string(), vec!["The base lighting of the region. 0.0 for fully black and 1.0 for fully lit.".to_string()]);
map.insert("visibility".to_string(), vec!["Use \"full\" for unlimited visibility or \"limited\" to enable the parameters below.".to_string()]);
map.insert("visible_distance".to_string(), vec!["The visible distance in tiles. \"visibility\" has to be set to \"limited\".".to_string()]);
Expand Down
10 changes: 9 additions & 1 deletion core_shared/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub struct Date {

pub hours : i32,
pub minutes : i32,
pub minutes_in_day : i32,
}

impl Date {
Expand All @@ -17,15 +18,17 @@ impl Date {

hours : 0,
minutes : 0,
minutes_in_day : 0,
}
}

pub fn from_ticks(&mut self, ticks: usize) {
let minutes = ticks / 4;
let minutes = ticks;// / 4;
self.hours = (minutes / 60) as i32;
self.minutes = (minutes % 60) as i32;

self.total_minutes = minutes;
self.minutes_in_day = (minutes % 1440) as i32;
}

pub fn get_hours(&mut self) -> i32 {
Expand All @@ -39,6 +42,10 @@ impl Date {
pub fn time24(&mut self) -> String {
format!("{:0>2}:{:0>2}", self.hours, self.minutes)
}

pub fn time12(&mut self) -> String {
format!("{}{}", self.hours % 12, if self.hours > 12 { "pm" } else { "am" })
}
}

use std::cmp::*;
Expand All @@ -63,5 +70,6 @@ pub fn script_register_date_api(engine: &mut rhai::Engine) {
.register_get("hours", Date::get_hours)
.register_get("minutes", Date::get_minutes)

.register_fn("time12", Date::time12)
.register_fn("time24", Date::time24);
}
2 changes: 1 addition & 1 deletion game/game.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion game/regions/World/level1.json

Large diffs are not rendered by default.

0 comments on commit e4c145a

Please sign in to comment.