From 8894a5e79df5962bc9b60a7fbe1909645aa5a51d Mon Sep 17 00:00:00 2001 From: plague006 Date: Fri, 1 Feb 2019 20:09:12 -0500 Subject: [PATCH 1/4] Refactor day_night --- config.lua | 17 +++++++ control.lua | 3 ++ map_gen/shared/day_night.lua | 94 ++++++++++++++++++++++++++---------- 3 files changed, 89 insertions(+), 25 deletions(-) diff --git a/config.lua b/config.lua index 94d680b8..eb915d46 100644 --- a/config.lua +++ b/config.lua @@ -267,6 +267,23 @@ global.config = { -- adds a useless button with the biter percentage evolution_progress = { enabled = true + }, + -- sets the day/night cycle or a fixed light level. use_day_night_cycle and use_fixed_brightness are mutually exclusive + day_night ={ + -- enables/disables the module + enabled = false, + -- for info on day/night cycles see https://github.com/Refactorio/RedMew/wiki/Day-Night-cycle + use_day_night_cycle = false, + day_night_cycle = { + ['ticks_per_day'] = 25000, + ['dusk'] = 0.25, + ['evening'] = 0.45, + ['morning'] = 0.55, + ['dawn'] = 0.75 + }, + -- brightness is a number between 0.15 and 1 + use_fixed_brightness = false, + fixed_brightness = 0.5 } } diff --git a/control.lua b/control.lua index 3d23351b..00bcb209 100644 --- a/control.lua +++ b/control.lua @@ -74,6 +74,9 @@ end if config.camera.enabled then require 'features.gui.camera' end +if config.day_night.enabled then + require 'map_gen.shared.day_night' +end -- GUIs -- The order determines the order they appear from left to right. diff --git a/map_gen/shared/day_night.lua b/map_gen/shared/day_night.lua index 1a1d19d3..f9f75106 100644 --- a/map_gen/shared/day_night.lua +++ b/map_gen/shared/day_night.lua @@ -1,17 +1,29 @@ -- For more info on the day/night cycle and examples of cycles see: https://github.com/Refactorio/RedMew/wiki/Day-Night-cycle -local Public = {} -local Debug = require 'utils.debug' +-- Dependencies +local Event = require 'utils.event' local math = require 'utils.math' +local RS = require 'map_gen.shared.redmew_surface' +-- Localized global table +local config = global.config.day_night + +-- Localized functions +local round = math.round +local format = string.format + +-- Constants local day_night_cycle_keys = { 'ticks_per_day', 'dusk', 'evening', 'morning', - 'dawn', + 'dawn' } ---- Checks that a table has a valid day night cycle. +-- Local vars +local Public = {} + +--- Checks that a table is a valid day night cycle. local function check_cycle_validity(day_night_cycle) for _, required_key in pairs(day_night_cycle_keys) do if not day_night_cycle[required_key] then @@ -19,27 +31,46 @@ local function check_cycle_validity(day_night_cycle) end end - if (day_night_cycle['dusk'] > day_night_cycle['evening']) or - (day_night_cycle['evening'] > day_night_cycle['morning']) or - (day_night_cycle['morning'] > day_night_cycle['dawn']) then + if (day_night_cycle['dusk'] > day_night_cycle['evening']) or (day_night_cycle['evening'] > day_night_cycle['morning']) or (day_night_cycle['morning'] > day_night_cycle['dawn']) then return false else return true end end +--- On init, check the config settings +local function init() + if config.use_day_night_cycle and config.use_fixed_brightness then + error('Cannot use both a day/night cycle and a fixed brightness') + return + elseif config.use_day_night_cycle then + Public.set_cycle(config.day_night_cycle, RS.get_surface()) + elseif config.use_fixed_brightness then + Public.set_fixed_brightness(config.fixed_brightness, RS.get_surface()) + end +end + +Event.on_init(init) + +-- Public functions + --- Sets the day/night cycle according to the table it is given. --- @param day_night_cycle table containing keys: ticks_per_day, dusk, evening, morning, dawn --- @param surface the LuaSurface to set the day/night cycle of --- @returns boolean true if set properly +-- Can only be used during or after init. +-- @param day_night_cycle containing specific, required keys: ticks_per_day, dusk, evening, morning, dawn +-- @param surface to set the day/night cycle of +-- @returns true if set properly, nil if not -- @see Venus::world_settings -Public.set_cycle = function(day_night_cycle, surface) +function Public.set_cycle(day_night_cycle, surface) if not check_cycle_validity(day_night_cycle) then - error('Provided day/night cycle is invalid') + log('Provided day/night cycle is invalid') return end - if not surface.valid then - error('Provided surface is invalid') + if not surface or not surface.valid then + log('Provided surface is invalid') + return + end + if not Public.unfreeze_daytime then + log('Time is frozen') return end @@ -55,19 +86,20 @@ Public.set_cycle = function(day_night_cycle, surface) end --- Sets the brightness to a fixed level --- @param daylight number between 0.15 and 1 representing the percentage of daylight (0.15 brightness is the darkest available) --- @param surface the LuaSurface to set the day/night cycle of --- @return boolean true if time is set properly -Public.set_fixed_brightness = function(daylight, surface) - if not surface.valid then - error('Provided surface is invalid') +-- Can only be used during or after init. +-- @param daylight between 0.15 and 1 representing the percentage of daylight (0.15 brightness is the darkest available) +-- @param surface to set the day/night cycle of +-- @return true if time is set properly, nil if not +function Public.set_fixed_brightness(daylight, surface) + if not surface or not surface.valid then + log('Provided surface is invalid') return end if daylight < 0.15 then - error('Daylight set too low. 0.15 is the darkest available.') + log('Daylight set too low. 0.15 is the darkest available.') return elseif daylight > 1 then - error('Daylight set too high. 1.00 is the lightest available.') + log('Daylight set too high. 1.00 is the lightest available.') return end @@ -80,12 +112,24 @@ Public.set_fixed_brightness = function(daylight, surface) -- Freeze the day/night cycle surface.freeze_daytime = true - Debug.print('breakpoint/surface_daytime: ' .. breakpoint .. '/' .. surface.daytime) - Debug.print('brightness/surface_brightness: ' .. math.round(daylight, 2) .. '/' .. math.round((1 - surface.darkness), 2)) + Debug.print(format('breakpoint/surface_daytime: %s/%s', breakpoint, surface.daytime)) + Debug.print(format('brightness/surface_brightness: %s/%s', round(daylight, 2), round((1 - surface.darkness), 2))) - if math.round(daylight, 2) == math.round((1 - surface.darkness), 2) then + if round(daylight, 2) == round((1 - surface.darkness), 2) then return true end end +--- Unfreezes daytime (usually frozen by set_fixed_brightness) +-- @param surface to unfreeze the day/night cycle of +-- @return true if daytime unfrozen, nil if not +function Public.unfreeze_daytime(surface) + if not surface or not surface.valid then + log('Provided surface is invalid') + return + end + surface.freeze_daytime = false + return true +end + return Public From 4234d657b04dc628e520a36e959de1f8229f85cd Mon Sep 17 00:00:00 2001 From: plague006 Date: Fri, 1 Feb 2019 20:33:59 -0500 Subject: [PATCH 2/4] Add day/night cycle presets --- resources/day_night_cycles.lua | 50 ++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 resources/day_night_cycles.lua diff --git a/resources/day_night_cycles.lua b/resources/day_night_cycles.lua new file mode 100644 index 00000000..fb49de20 --- /dev/null +++ b/resources/day_night_cycles.lua @@ -0,0 +1,50 @@ +return { + -- ~7 minute cycle, 3m28s of full light, 1m23s light to dark, 42s full dark, 1m23s dark to light + vanilla = { + ['ticks_per_day'] = 25000, + ['dusk'] = 0.25, + ['evening'] = 0.45, + ['morning'] = 0.55, + ['dawn'] = 0.75 + }, + -- 10 minute cycle, 4m of full light, 4m light to dark, 6s full dark, 2m dark to light + bright = { + ['ticks_per_day'] = 36000, + ['dusk'] = 0.2, + ['evening'] = 0.59, + ['morning'] = 0.6, + ['dawn'] = 0.8 + }, + -- ~14 minute cycle, 6m56s of full light, 2m46s light to dark, 1m24s full dark, 2m46s dark to light + double_length = { + ['ticks_per_day'] = 50000, + ['dusk'] = 0.25, + ['evening'] = 0.45, + ['morning'] = 0.55, + ['dawn'] = 0.75 + }, + -- 10 minute cycle, 6s of full light, 2m light to dark, 4m full dark, 4m dark to light + gloomy = { + ['ticks_per_day'] = 36000, + ['dusk'] = 0, + ['evening'] = 0.2, + ['morning'] = 0.6, + ['dawn'] = 0.99 + }, + -- ~3.5 minute cycle, 1m44s of full light, 42s light to dark, 21s full dark, 42s dark to light + half_length = { + ['ticks_per_day'] = 12500, + ['dusk'] = 0.25, + ['evening'] = 0.45, + ['morning'] = 0.55, + ['dawn'] = 0.75 + }, + -- 20 minute cycle, 9m of full light, 1m light to dark, 9m full dark, 1m dark to light + long_days_long_nights_fast_transitions = { + ['ticks_per_day'] = 72000, + ['dusk'] = 0.225, + ['evening'] = 0.275, + ['morning'] = 0.725, + ['dawn'] = 0.775 + } +} From 0a04f1d6289ab36013f44ad0bde6a553472ee935 Mon Sep 17 00:00:00 2001 From: plague006 Date: Fri, 1 Feb 2019 20:38:35 -0500 Subject: [PATCH 3/4] Logs to errors --- map_gen/shared/day_night.lua | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/map_gen/shared/day_night.lua b/map_gen/shared/day_night.lua index f9f75106..ef596f37 100644 --- a/map_gen/shared/day_night.lua +++ b/map_gen/shared/day_night.lua @@ -62,15 +62,15 @@ Event.on_init(init) -- @see Venus::world_settings function Public.set_cycle(day_night_cycle, surface) if not check_cycle_validity(day_night_cycle) then - log('Provided day/night cycle is invalid') + error('Provided day/night cycle is invalid') return end if not surface or not surface.valid then - log('Provided surface is invalid') + error('Provided surface is invalid') return end if not Public.unfreeze_daytime then - log('Time is frozen') + error('Time is stuck in a frozen state') return end @@ -92,14 +92,14 @@ end -- @return true if time is set properly, nil if not function Public.set_fixed_brightness(daylight, surface) if not surface or not surface.valid then - log('Provided surface is invalid') + error('Provided surface is invalid') return end if daylight < 0.15 then - log('Daylight set too low. 0.15 is the darkest available.') + error('Daylight set too low. 0.15 is the darkest available.') return elseif daylight > 1 then - log('Daylight set too high. 1.00 is the lightest available.') + error('Daylight set too high. 1.00 is the lightest available.') return end From 5ad8833bb63ac0356aaa9c02aff3d74853866d08 Mon Sep 17 00:00:00 2001 From: plague006 Date: Sun, 3 Feb 2019 10:31:20 -0500 Subject: [PATCH 4/4] Clean up keys --- config.lua | 10 +++--- resources/day_night_cycles.lua | 60 +++++++++++++++++----------------- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/config.lua b/config.lua index eb915d46..7ea41ab4 100644 --- a/config.lua +++ b/config.lua @@ -275,11 +275,11 @@ global.config = { -- for info on day/night cycles see https://github.com/Refactorio/RedMew/wiki/Day-Night-cycle use_day_night_cycle = false, day_night_cycle = { - ['ticks_per_day'] = 25000, - ['dusk'] = 0.25, - ['evening'] = 0.45, - ['morning'] = 0.55, - ['dawn'] = 0.75 + ticks_per_day = 25000, + dusk = 0.25, + evening = 0.45, + morning = 0.55, + dawn = 0.75 }, -- brightness is a number between 0.15 and 1 use_fixed_brightness = false, diff --git a/resources/day_night_cycles.lua b/resources/day_night_cycles.lua index fb49de20..479ef3c0 100644 --- a/resources/day_night_cycles.lua +++ b/resources/day_night_cycles.lua @@ -1,50 +1,50 @@ return { -- ~7 minute cycle, 3m28s of full light, 1m23s light to dark, 42s full dark, 1m23s dark to light vanilla = { - ['ticks_per_day'] = 25000, - ['dusk'] = 0.25, - ['evening'] = 0.45, - ['morning'] = 0.55, - ['dawn'] = 0.75 + ticks_per_day = 25000, + dusk = 0.25, + evening = 0.45, + morning = 0.55, + dawn = 0.75 }, -- 10 minute cycle, 4m of full light, 4m light to dark, 6s full dark, 2m dark to light bright = { - ['ticks_per_day'] = 36000, - ['dusk'] = 0.2, - ['evening'] = 0.59, - ['morning'] = 0.6, - ['dawn'] = 0.8 + ticks_per_day = 36000, + dusk = 0.2, + evening = 0.59, + morning = 0.6, + dawn = 0.8 }, -- ~14 minute cycle, 6m56s of full light, 2m46s light to dark, 1m24s full dark, 2m46s dark to light double_length = { - ['ticks_per_day'] = 50000, - ['dusk'] = 0.25, - ['evening'] = 0.45, - ['morning'] = 0.55, - ['dawn'] = 0.75 + ticks_per_day = 50000, + dusk = 0.25, + evening = 0.45, + morning = 0.55, + dawn = 0.75 }, -- 10 minute cycle, 6s of full light, 2m light to dark, 4m full dark, 4m dark to light gloomy = { - ['ticks_per_day'] = 36000, - ['dusk'] = 0, - ['evening'] = 0.2, - ['morning'] = 0.6, - ['dawn'] = 0.99 + ticks_per_day = 36000, + dusk = 0, + evening = 0.2, + morning = 0.6, + dawn = 0.99 }, -- ~3.5 minute cycle, 1m44s of full light, 42s light to dark, 21s full dark, 42s dark to light half_length = { - ['ticks_per_day'] = 12500, - ['dusk'] = 0.25, - ['evening'] = 0.45, - ['morning'] = 0.55, - ['dawn'] = 0.75 + ticks_per_day = 12500, + dusk = 0.25, + evening = 0.45, + morning = 0.55, + dawn = 0.75 }, -- 20 minute cycle, 9m of full light, 1m light to dark, 9m full dark, 1m dark to light long_days_long_nights_fast_transitions = { - ['ticks_per_day'] = 72000, - ['dusk'] = 0.225, - ['evening'] = 0.275, - ['morning'] = 0.725, - ['dawn'] = 0.775 + ticks_per_day = 72000, + dusk = 0.225, + evening = 0.275, + morning = 0.725, + dawn = 0.775 } }