1
0
mirror of https://github.com/Refactorio/RedMew.git synced 2024-12-12 10:04:40 +02:00

Merge pull request #725 from plague006/day_night

Refactor day_night
This commit is contained in:
Matthew 2019-02-03 14:15:50 -05:00 committed by GitHub
commit 47551adb85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 134 additions and 20 deletions

View File

@ -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
}
}

View File

@ -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.

View File

@ -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,29 +31,48 @@ 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 <table> containing specific, required keys: ticks_per_day, dusk, evening, morning, dawn
-- @param surface <LuaSurface> to set the day/night cycle of
-- @returns <boolean> 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')
return
end
if not surface.valid then
if not surface or not surface.valid then
error('Provided surface is invalid')
return
end
if not Public.unfreeze_daytime then
error('Time is stuck in a frozen state')
return
end
surface.ticks_per_day = day_night_cycle.ticks_per_day
surface.dusk = 0
@ -55,11 +86,12 @@ 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
-- Can only be used during or after init.
-- @param daylight <number> between 0.15 and 1 representing the percentage of daylight (0.15 brightness is the darkest available)
-- @param surface <LuaSurface> to set the day/night cycle of
-- @return <boolean> true if time is set properly, nil if not
function Public.set_fixed_brightness(daylight, surface)
if not surface or not surface.valid then
error('Provided surface is invalid')
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 <LuaSurface> to unfreeze the day/night cycle of
-- @return <boolean> 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

View File

@ -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
}
}