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