mirror of
https://github.com/minetest-mods/time_regulation.git
synced 2025-01-09 09:30:24 +01:00
Do not read time_speed's value when it has already been changed
- For #4
This commit is contained in:
parent
da8bbfee9a
commit
e9787b55aa
74
init.lua
74
init.lua
@ -8,7 +8,7 @@
|
|||||||
|
|
||||||
-- Namespace first, with basic informations
|
-- Namespace first, with basic informations
|
||||||
time_reg = {}
|
time_reg = {}
|
||||||
time_reg.version = "00.01.23"
|
time_reg.version = "00.01.24"
|
||||||
time_reg.authors = {"Mg/LeMagnesium"}
|
time_reg.authors = {"Mg/LeMagnesium"}
|
||||||
|
|
||||||
-- Definitions
|
-- Definitions
|
||||||
@ -60,8 +60,8 @@ local old_settime_func = core.chatcommands["time"].func
|
|||||||
core.chatcommands["time"].func = function(...)
|
core.chatcommands["time"].func = function(...)
|
||||||
local res, msg = old_settime_func(...)
|
local res, msg = old_settime_func(...)
|
||||||
if res and time_reg.status == time_reg.STATUS_ACTIVE then
|
if res and time_reg.status == time_reg.STATUS_ACTIVE then
|
||||||
time_reg.loop(false, true)
|
|
||||||
time_reg.log("Settime override : updating regulation")
|
time_reg.log("Settime override : updating regulation")
|
||||||
|
time_reg.loop(false, true)
|
||||||
end
|
end
|
||||||
return res, msg
|
return res, msg
|
||||||
end
|
end
|
||||||
@ -69,9 +69,9 @@ end
|
|||||||
local old_set_func = core.chatcommands["set"].func
|
local old_set_func = core.chatcommands["set"].func
|
||||||
core.chatcommands["set"].func = function(...)
|
core.chatcommands["set"].func = function(...)
|
||||||
local res, msg = old_set_func(...)
|
local res, msg = old_set_func(...)
|
||||||
if res and time_reg.status ~= time_reg.STATUS_DEAD then
|
if res and time_reg.status ~= time_reg.STATUS_DEAD and tonumber(minetest.setting_get("time_speed")) ~= time_reg.time_speed then
|
||||||
time_reg.loop(false, true)
|
|
||||||
time_reg.log("Set override : updating constants and regulation")
|
time_reg.log("Set override : updating constants and regulation")
|
||||||
|
time_reg.update_constants({time_speed = true})
|
||||||
end
|
end
|
||||||
return res, msg
|
return res, msg
|
||||||
end
|
end
|
||||||
@ -86,9 +86,11 @@ function time_reg.log(x) minetest.log("action", "[TimeRegulation] " .. (x or "")
|
|||||||
-- Standard calculation function
|
-- Standard calculation function
|
||||||
-- Function used when performing calculation of standard method (meaning that we already have the ratio)
|
-- Function used when performing calculation of standard method (meaning that we already have the ratio)
|
||||||
function time_reg.std_calculation()
|
function time_reg.std_calculation()
|
||||||
|
time_reg.log("Calculation using time_speed = " .. time_reg.time_speed)
|
||||||
local day_htime, night_htime = time_reg.duration * (time_reg.ratio.day/100), time_reg.duration * (time_reg.ratio.night/100)
|
local day_htime, night_htime = time_reg.duration * (time_reg.ratio.day/100), time_reg.duration * (time_reg.ratio.night/100)
|
||||||
time_reg.day_time_speed = 1440 / (day_htime)
|
time_reg.day_time_speed = 1440 / (day_htime)
|
||||||
time_reg.night_time_speed = 1440 / (night_htime)
|
time_reg.night_time_speed = 1440 / (night_htime)
|
||||||
|
time_reg.log("Output is : " .. time_reg.day_time_speed .. " (day); " .. time_reg.night_time_speed .. " (night)")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Seasonal calculation function
|
-- Seasonal calculation function
|
||||||
@ -107,26 +109,44 @@ function time_reg.seasonal_calculation()
|
|||||||
end
|
end
|
||||||
|
|
||||||
-- Constants update function
|
-- Constants update function
|
||||||
-- Global constant update function which determines what calculation method to use
|
-- Global constant update used to update calculation values
|
||||||
function time_reg.update_constants()
|
-- It needs a parameter, a table with key/value elements. The following keys are available :
|
||||||
time_reg.time_speed = minetest.setting_get("time_speed") or time_reg.time_speed -- Absolute Time Speed
|
-- - time_speed : true to update time_speed
|
||||||
|
-- - date : true to update the current date from either the game or real life
|
||||||
|
function time_reg.update_constants(tab)
|
||||||
|
if tab.time_speed then
|
||||||
|
-- Updating time_speed should only be done when booting, or after an update of time_speed's value in MT's configuration
|
||||||
|
time_reg.time_speed = tonumber(minetest.setting_get("time_speed")) or time_reg.time_speed -- Absolute Time Speed
|
||||||
|
time_reg.duration = 1440 / time_reg.time_speed -- Absolute Human Speed
|
||||||
|
|
||||||
|
if time_reg.status == time_reg.STATUS_IDLE and time_reg.time_speed > 0 then
|
||||||
|
time_reg.start_loop()
|
||||||
|
elseif time_reg.status == time_reg.STATUS_ACTIVE and time_reg.time_speed == 0 then
|
||||||
|
time_reg.stop_loop()
|
||||||
|
else
|
||||||
|
time_reg.loop(false)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if tab.date then
|
||||||
if time_reg.real_life_seasons then
|
if time_reg.real_life_seasons then
|
||||||
time_reg.day_of_year = tonumber(os.date("%j"))
|
time_reg.day_of_year = tonumber(os.date("%j"))
|
||||||
else
|
else
|
||||||
time_reg.day_of_year = minetest.get_day_count()
|
time_reg.day_of_year = minetest.get_day_count()
|
||||||
end
|
end
|
||||||
|
-- Since we (hypothetically) changed the current day we compute again our seasonal rations
|
||||||
if time_reg.status == time_reg.STATUS_IDLE and time_reg.time_speed > 0 then
|
|
||||||
time_reg.set_status(time_reg.STATUS_ACTIVE, "ACTIVE")
|
|
||||||
end
|
|
||||||
|
|
||||||
if time_reg.status == time_reg.STATUS_ACTIVE then
|
|
||||||
if time_reg.seasons_mode then
|
if time_reg.seasons_mode then
|
||||||
time_reg.seasonal_calculation() -- Calculate season-dependant ratio
|
time_reg.seasonal_calculation() -- Calculate season-dependant ratio
|
||||||
end
|
end
|
||||||
time_reg.duration = 1440 / time_reg.time_speed -- Absolute Human Speed
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Central computing function
|
||||||
|
-- A computing function separated from update_constants, for clarity's sake
|
||||||
|
function time_reg.compute()
|
||||||
|
if time_reg.status == time_reg.STATUS_ACTIVE then
|
||||||
time_reg.std_calculation() -- Use ratio and time_speed to calculate time
|
time_reg.std_calculation() -- Use ratio and time_speed to calculate time
|
||||||
time_reg.loop_interval = math.min(1440 / time_reg.night_time_speed, 1440 / time_reg.night_time_speed) * 60
|
time_reg.loop_interval = math.min(1440 / time_reg.night_time_speed, 1440 / time_reg.night_time_speed) * 30 -- (not 60, we only want half of it)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -138,8 +158,9 @@ function time_reg.start_loop()
|
|||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
time_reg.loop_active = true
|
time_reg.loop_active = true
|
||||||
time_reg.log("Loop started")
|
time_reg.set_status(time_reg.STATUS_ACTIVE, "ACTIVE")
|
||||||
time_reg.loop(true)
|
time_reg.loop(true)
|
||||||
|
time_reg.log("Loop started")
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -164,8 +185,19 @@ end
|
|||||||
|
|
||||||
-- And the loop
|
-- And the loop
|
||||||
function time_reg.loop(loop, forceupdate)
|
function time_reg.loop(loop, forceupdate)
|
||||||
|
if not loop then
|
||||||
|
time_reg.log("Loop running as standalone")
|
||||||
|
end
|
||||||
|
|
||||||
-- Do all calculations
|
-- Do all calculations
|
||||||
time_reg.update_constants()
|
time_reg.update_constants({date = true})
|
||||||
|
time_reg.compute()
|
||||||
|
if not time_reg.loop_active then
|
||||||
|
time_reg.set_status(time_reg.STATUS_IDLE, "IDLE")
|
||||||
|
time_reg.log("Loop broken")
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
local tod = minetest.get_timeofday() * 24000
|
local tod = minetest.get_timeofday() * 24000
|
||||||
|
|
||||||
local moment = "day"
|
local moment = "day"
|
||||||
@ -173,11 +205,6 @@ function time_reg.loop(loop, forceupdate)
|
|||||||
moment = "night"
|
moment = "night"
|
||||||
end
|
end
|
||||||
|
|
||||||
if time_reg.time_speed == 0 then
|
|
||||||
time_reg.set_status(time_reg.STATUS_IDLE, "IDLE")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Update if threshold reached
|
-- Update if threshold reached
|
||||||
if moment ~= time_reg.moment or forceupdate then
|
if moment ~= time_reg.moment or forceupdate then
|
||||||
-- We've reached a treshold
|
-- We've reached a treshold
|
||||||
@ -234,7 +261,6 @@ minetest.register_chatcommand("time_reg", {
|
|||||||
local res = time_reg.start_loop()
|
local res = time_reg.start_loop()
|
||||||
if res then
|
if res then
|
||||||
time_reg.set_status(time_reg.STATUS_ACTIVE, "ACTIVE")
|
time_reg.set_status(time_reg.STATUS_ACTIVE, "ACTIVE")
|
||||||
time_reg.update_constants()
|
|
||||||
return true, "Loop started. Time regulation enabled"
|
return true, "Loop started. Time regulation enabled"
|
||||||
else
|
else
|
||||||
return false, "Loop couldn't be started, it already is"
|
return false, "Loop couldn't be started, it already is"
|
||||||
@ -269,7 +295,7 @@ minetest.register_chatcommand("time_reg", {
|
|||||||
return false, "Invalid moment of the day : " .. moment .. ". Use either 'day' or 'night'"
|
return false, "Invalid moment of the day : " .. moment .. ". Use either 'day' or 'night'"
|
||||||
end
|
end
|
||||||
|
|
||||||
time_reg.update_constants()
|
time_reg.compute()
|
||||||
time_reg.loop(false, true)
|
time_reg.loop(false, true)
|
||||||
return true, "Operation succeeded.\nRatio: " .. time_reg.ratio.day .. "% day and " .. time_reg.ratio.night .. "% night"
|
return true, "Operation succeeded.\nRatio: " .. time_reg.ratio.day .. "% day and " .. time_reg.ratio.night .. "% night"
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user