Do not read time_speed's value when it has already been changed

- For #4
This commit is contained in:
LeMagnesium 2016-05-14 20:41:49 +02:00
parent da8bbfee9a
commit e9787b55aa
1 changed files with 71 additions and 45 deletions

116
init.lua
View File

@ -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
@ -58,22 +58,22 @@ end
-- Crappy overrides -- Crappy overrides
local old_settime_func = core.chatcommands["time"].func 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
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()
local day_htime, night_htime = time_reg.duration * (time_reg.ratio.day/100), time_reg.duration * (time_reg.ratio.night/100) time_reg.log("Calculation using time_speed = " .. time_reg.time_speed)
time_reg.day_time_speed = 1440 / (day_htime) local day_htime, night_htime = time_reg.duration * (time_reg.ratio.day/100), time_reg.duration * (time_reg.ratio.night/100)
time_reg.night_time_speed = 1440 / (night_htime) time_reg.day_time_speed = 1440 / (day_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,29 +109,47 @@ 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
if time_reg.real_life_seasons then -- - date : true to update the current date from either the game or real life
time_reg.day_of_year = tonumber(os.date("%j")) function time_reg.update_constants(tab)
else if tab.time_speed then
time_reg.day_of_year = minetest.get_day_count() -- Updating time_speed should only be done when booting, or after an update of time_speed's value in MT's configuration
end 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 if time_reg.status == time_reg.STATUS_IDLE and time_reg.time_speed > 0 then
time_reg.set_status(time_reg.STATUS_ACTIVE, "ACTIVE") time_reg.start_loop()
end 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 time_reg.status == time_reg.STATUS_ACTIVE then if tab.date then
if time_reg.seasons_mode then if time_reg.real_life_seasons then
time_reg.seasonal_calculation() -- Calculate season-dependant ratio time_reg.day_of_year = tonumber(os.date("%j"))
end else
time_reg.duration = 1440 / time_reg.time_speed -- Absolute Human Speed time_reg.day_of_year = minetest.get_day_count()
time_reg.std_calculation() -- Use ratio and time_speed to calculate time end
time_reg.loop_interval = math.min(1440 / time_reg.night_time_speed, 1440 / time_reg.night_time_speed) * 60 -- Since we (hypothetically) changed the current day we compute again our seasonal rations
end if time_reg.seasons_mode then
time_reg.seasonal_calculation() -- Calculate season-dependant ratio
end
end
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.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
-- Start the Loop -- Start the Loop
-- Launch the Loop with the order to repeat itself indefinitely -- Launch the Loop with the order to repeat itself indefinitely
function time_reg.start_loop() function time_reg.start_loop()
@ -138,9 +158,10 @@ function time_reg.start_loop()
return false return false
end end
time_reg.loop_active = true time_reg.loop_active = true
time_reg.set_status(time_reg.STATUS_ACTIVE, "ACTIVE")
time_reg.loop(true)
time_reg.log("Loop started") time_reg.log("Loop started")
time_reg.loop(true) return true
return true
end end
-- Stop the Loop -- Stop the Loop
@ -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"