Add time_reg set

- Allow forceful update in time_reg.loop
 - Add time_reg set <day|night> [0.0 .. 100.0]
 - Fix day skipping
This commit is contained in:
LeMagnesium 2016-02-16 18:38:48 +01:00
parent c017cbc433
commit 4f71b46e24
2 changed files with 148 additions and 117 deletions

BIN
.init.lua.swp Normal file

Binary file not shown.

View File

@ -7,7 +7,7 @@
-- Namespace first, with basic informations
time_reg = {}
time_reg.version = "00.01.00"
time_reg.version = "00.01.04"
time_reg.author = "Mg/LeMagnesium"
-- Definitions
@ -49,7 +49,7 @@ core.chatcommands["time"].func = function(...)
local res, msg = old_settime_func(...)
if res and time_reg.status == 2 then
time_reg.do_calculation()
time_reg.loop(0, false)
time_reg.loop(false, true)
minetest.log("action", "[TimeRegulation] Settime override : updating regulation")
end
return res, msg
@ -60,7 +60,7 @@ core.chatcommands["set"].func = function(...)
local res, msg = old_set_func(...)
if res and time_reg.status ~= 0 then
time_reg.update_constants()
time_reg.loop(false)
time_reg.loop(false, true)
minetest.log("action", "[TimeRegulation] Set override : updating constants and regulation")
end
return res, msg
@ -112,7 +112,7 @@ function time_reg.set_status(x, title)
end
-- And the loop
function time_reg.loop(loop)
function time_reg.loop(loop, forceupdate)
-- Determine TOD and current moment
local tod = minetest.get_timeofday() * 24000
@ -127,13 +127,13 @@ function time_reg.loop(loop)
end
-- Update if threshold reached
if moment ~= time_reg.moment then
if moment ~= time_reg.moment or forceupdate then
-- We've reached a treshold
time_reg.moment = moment
if moment == "day" then
if time_reg.ratio.day == 0 then
minetest.set_timeofday(time_reg.threshold.night / 2400)
minetest.set_timeofday(time_reg.threshold.night / 24000)
minetest.log("action", "[TimeRegulation] Entering day period : period skipped")
else
minetest.setting_set("time_speed", time_reg.day_time_speed)
@ -173,7 +173,8 @@ minetest.register_chatcommand("time_reg", {
return true, "see /time_reg help"
elseif param == "help" then
return true, "Supported commands: start, stop"
return true, "Supported commands: start, stop, set"
elseif param == "stop" then
local res = time_reg.stop_loop()
if res then
@ -182,6 +183,7 @@ minetest.register_chatcommand("time_reg", {
else
return false, "Loop couldn't be stopped, it isn't running"
end
elseif param == "start" then
local res = time_reg.start_loop()
if res then
@ -191,6 +193,35 @@ minetest.register_chatcommand("time_reg", {
else
return false, "Loop couldn't be started, it already is"
end
elseif param:split(" ")[1] == "set" then
local params = param:split(" ")
if #params < 3 then
return false, "Not enough parameters. You need to enter 'set', a moment of the day ('night' or 'day') and a percentage (0 to 100)"
elseif #params > 3 then
return false, "You entered too many parameters"
end
local moment, perc = params[2], tonumber(params[3])
if not perc or perc < 0 or perc > 100 then
return false, "Invalid percentage : " .. params[3]
end
if moment == "day" then
time_reg.ratio.day = perc
time_reg.ratio.night = 100 - perc
elseif moment == "night" then
time_reg.ratio.night = perc
time_reg.ratio.day = 100 - perc
else
return false, "Invalid moment of the day : " .. moment .. ". Use either 'day' or 'night'"
end
time_reg.update_constants()
time_reg.loop(false, true)
return true, "Operation succeeded.\nRatio: " .. time_reg.ratio.day .. "% day and " .. time_reg.ratio.night .. "% night"
else
return false, "Unknown subcommand: " .. param
end