mirror of
https://github.com/minetest-mods/time_regulation.git
synced 2025-01-25 01:00:27 +01:00
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:
parent
c017cbc433
commit
4f71b46e24
BIN
.init.lua.swp
Normal file
BIN
.init.lua.swp
Normal file
Binary file not shown.
265
init.lua
265
init.lua
@ -7,7 +7,7 @@
|
|||||||
|
|
||||||
-- Namespace first, with basic informations
|
-- Namespace first, with basic informations
|
||||||
time_reg = {}
|
time_reg = {}
|
||||||
time_reg.version = "00.01.00"
|
time_reg.version = "00.01.04"
|
||||||
time_reg.author = "Mg/LeMagnesium"
|
time_reg.author = "Mg/LeMagnesium"
|
||||||
|
|
||||||
-- Definitions
|
-- Definitions
|
||||||
@ -35,166 +35,197 @@ time_reg.night_time_speed = 0
|
|||||||
time_reg.status = 2
|
time_reg.status = 2
|
||||||
|
|
||||||
time_reg.ratio = { -- Expressed in percent
|
time_reg.ratio = { -- Expressed in percent
|
||||||
day = tonumber(minetest.setting_get("day_time_ratio")) or 50,
|
day = tonumber(minetest.setting_get("day_time_ratio")) or 50,
|
||||||
night = tonumber(minetest.setting_get("night_time_ratio")) or 50,
|
night = tonumber(minetest.setting_get("night_time_ratio")) or 50,
|
||||||
}
|
}
|
||||||
if (time_reg.ratio.day + time_reg.ratio.night ~= 100) or time_reg.ratio.day < 0 or time_reg.ratio.night < 0 then
|
if (time_reg.ratio.day + time_reg.ratio.night ~= 100) or time_reg.ratio.day < 0 or time_reg.ratio.night < 0 then
|
||||||
minetest.log("error", ("[TimeRegulation] Invalid ratio : %d/100 day & %d/100 night. Setting to 50/50"):format(time_reg.ratio.day, time_reg.ratio.night))
|
minetest.log("error", ("[TimeRegulation] Invalid ratio : %d/100 day & %d/100 night. Setting to 50/50"):format(time_reg.ratio.day, time_reg.ratio.night))
|
||||||
time_reg.ratio.day, time_reg.ratio.night = 50, 50
|
time_reg.ratio.day, time_reg.ratio.night = 50, 50
|
||||||
end
|
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 == 2 then
|
if res and time_reg.status == 2 then
|
||||||
time_reg.do_calculation()
|
time_reg.do_calculation()
|
||||||
time_reg.loop(0, false)
|
time_reg.loop(false, true)
|
||||||
minetest.log("action", "[TimeRegulation] Settime override : updating regulation")
|
minetest.log("action", "[TimeRegulation] Settime override : updating regulation")
|
||||||
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 ~= 0 then
|
if res and time_reg.status ~= 0 then
|
||||||
time_reg.update_constants()
|
time_reg.update_constants()
|
||||||
time_reg.loop(false)
|
time_reg.loop(false, true)
|
||||||
minetest.log("action", "[TimeRegulation] Set override : updating constants and regulation")
|
minetest.log("action", "[TimeRegulation] Set override : updating constants and regulation")
|
||||||
end
|
end
|
||||||
return res, msg
|
return res, msg
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- Then methods
|
-- Then methods
|
||||||
function time_reg.do_calculation()
|
function time_reg.do_calculation()
|
||||||
time_reg.time_speed = tonumber(minetest.setting_get("time_speed")) -- Absolute Time Speed
|
time_reg.time_speed = tonumber(minetest.setting_get("time_speed")) -- Absolute Time Speed
|
||||||
time_reg.duration = 1440 / time_reg.time_speed -- Absolute Human Speed
|
time_reg.duration = 1440 / time_reg.time_speed -- Absolute Human 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.loop_interval = (math.min(night_htime, day_htime) / 12) * 60
|
time_reg.loop_interval = (math.min(night_htime, day_htime) / 12) * 60
|
||||||
time_reg.day_time_speed = 1440 / (day_htime * 2)
|
time_reg.day_time_speed = 1440 / (day_htime * 2)
|
||||||
time_reg.night_time_speed = 1440 / (night_htime * 2)
|
time_reg.night_time_speed = 1440 / (night_htime * 2)
|
||||||
end
|
end
|
||||||
|
|
||||||
function time_reg.update_constants()
|
function time_reg.update_constants()
|
||||||
time_reg.time_speed = minetest.setting_get("time_speed")
|
time_reg.time_speed = minetest.setting_get("time_speed")
|
||||||
time_reg.do_calculation()
|
time_reg.do_calculation()
|
||||||
if time_reg.status == 1 and time_reg.time_speed > 0 then
|
if time_reg.status == 1 and time_reg.time_speed > 0 then
|
||||||
time_reg.set_status(2, "ACTIVE")
|
time_reg.set_status(2, "ACTIVE")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function time_reg.start_loop()
|
function time_reg.start_loop()
|
||||||
if time_reg.loop_active then
|
if time_reg.loop_active then
|
||||||
minetest.log("action", "[TimeRegulation] Will not start the loop : one is already running")
|
minetest.log("action", "[TimeRegulation] Will not start the loop : one is already running")
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
time_reg.loop_active = true
|
time_reg.loop_active = true
|
||||||
minetest.log("action", "[TimeRegulation] Loop started")
|
minetest.log("action", "[TimeRegulation] Loop started")
|
||||||
minetest.after(0, time_reg.loop, true)
|
minetest.after(0, time_reg.loop, true)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
function time_reg.stop_loop()
|
function time_reg.stop_loop()
|
||||||
if not time_reg.loop_active then
|
if not time_reg.loop_active then
|
||||||
minetest.log("action", "[TimeRegulation] Will not break the loop : no loop running")
|
minetest.log("action", "[TimeRegulation] Will not break the loop : no loop running")
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
time_reg.loop_active = false
|
time_reg.loop_active = false
|
||||||
minetest.log("action", "[TimeRegulation] Loop asked to stop")
|
minetest.log("action", "[TimeRegulation] Loop asked to stop")
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
function time_reg.set_status(x, title)
|
function time_reg.set_status(x, title)
|
||||||
minetest.log("action", "[TimeRegulation] Entered status " .. x .. " (" .. title .. ")")
|
minetest.log("action", "[TimeRegulation] Entered status " .. x .. " (" .. title .. ")")
|
||||||
time_reg.status = x
|
time_reg.status = x
|
||||||
end
|
end
|
||||||
|
|
||||||
-- And the loop
|
-- And the loop
|
||||||
function time_reg.loop(loop)
|
function time_reg.loop(loop, forceupdate)
|
||||||
-- Determine TOD and current moment
|
-- Determine TOD and current moment
|
||||||
local tod = minetest.get_timeofday() * 24000
|
local tod = minetest.get_timeofday() * 24000
|
||||||
|
|
||||||
local moment = "day"
|
local moment = "day"
|
||||||
if tod < time_reg.threshold.day or tod > time_reg.threshold.night then
|
if tod < time_reg.threshold.day or tod > time_reg.threshold.night then
|
||||||
moment = "night"
|
moment = "night"
|
||||||
end
|
|
||||||
|
|
||||||
if time_reg.time_speed == 0 then
|
|
||||||
time_reg.set_status(1, "IDLE")
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
-- Update if threshold reached
|
|
||||||
if moment ~= time_reg.moment 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.log("action", "[TimeRegulation] Entering day period : period skipped")
|
|
||||||
else
|
|
||||||
minetest.setting_set("time_speed", time_reg.day_time_speed)
|
|
||||||
minetest.log("action", "[TimeRegulation] Entering day period : time_speed " .. time_reg.day_time_speed)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
if time_reg.ratio.night == 0 then
|
|
||||||
minetest.set_timeofday(time_reg.threshold.day / 24000)
|
|
||||||
minetest.log("action", "[TimeRegulation] Entering night period : period skipped")
|
|
||||||
else
|
|
||||||
minetest.setting_set("tims_speed", time_reg.night_time_speed)
|
|
||||||
minetest.log("action", "[TimeRegulation] Entering night period : time_speed " .. time_reg.night_time_speed)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
-- Loop if we weren't broken
|
if time_reg.time_speed == 0 then
|
||||||
if loop then
|
time_reg.set_status(1, "IDLE")
|
||||||
minetest.after(time_reg.loop_interval, time_reg.loop, time_reg.loop_active)
|
return
|
||||||
else
|
end
|
||||||
minetest.log("action", "[TimeRegulation] Loop stopped")
|
|
||||||
end
|
-- Update if threshold reached
|
||||||
|
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 / 24000)
|
||||||
|
minetest.log("action", "[TimeRegulation] Entering day period : period skipped")
|
||||||
|
else
|
||||||
|
minetest.setting_set("time_speed", time_reg.day_time_speed)
|
||||||
|
minetest.log("action", "[TimeRegulation] Entering day period : time_speed " .. time_reg.day_time_speed)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
if time_reg.ratio.night == 0 then
|
||||||
|
minetest.set_timeofday(time_reg.threshold.day / 24000)
|
||||||
|
minetest.log("action", "[TimeRegulation] Entering night period : period skipped")
|
||||||
|
else
|
||||||
|
minetest.setting_set("tims_speed", time_reg.night_time_speed)
|
||||||
|
minetest.log("action", "[TimeRegulation] Entering night period : time_speed " .. time_reg.night_time_speed)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Loop if we weren't broken
|
||||||
|
if loop then
|
||||||
|
minetest.after(time_reg.loop_interval, time_reg.loop, time_reg.loop_active)
|
||||||
|
else
|
||||||
|
minetest.log("action", "[TimeRegulation] Loop stopped")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
time_reg.update_constants()
|
time_reg.update_constants()
|
||||||
|
|
||||||
if time_reg.enabled then
|
if time_reg.enabled then
|
||||||
time_reg.start_loop()
|
time_reg.start_loop()
|
||||||
end
|
end
|
||||||
|
|
||||||
-- chatcommand
|
-- chatcommand
|
||||||
minetest.register_chatcommand("time_reg", {
|
minetest.register_chatcommand("time_reg", {
|
||||||
description = "Control time_regulation",
|
description = "Control time_regulation",
|
||||||
privs = {server = true},
|
privs = {server = true},
|
||||||
func = function(name, param)
|
func = function(name, param)
|
||||||
if param == "" then
|
if param == "" then
|
||||||
return true, "see /time_reg help"
|
return true, "see /time_reg help"
|
||||||
|
|
||||||
elseif param == "help" then
|
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()
|
elseif param == "stop" then
|
||||||
if res then
|
local res = time_reg.stop_loop()
|
||||||
time_reg.set_status(0, "DEAD")
|
if res then
|
||||||
return true, "Loop was told to stop\nTime regulation disabled"
|
time_reg.set_status(0, "DEAD")
|
||||||
else
|
return true, "Loop was told to stop\nTime regulation disabled"
|
||||||
return false, "Loop couldn't be stopped, it isn't running"
|
else
|
||||||
end
|
return false, "Loop couldn't be stopped, it isn't running"
|
||||||
elseif param == "start" then
|
end
|
||||||
local res = time_reg.start_loop()
|
|
||||||
if res then
|
elseif param == "start" then
|
||||||
time_reg.set_status(2, "ACTIVE")
|
local res = time_reg.start_loop()
|
||||||
time_reg.update_constants()
|
if res then
|
||||||
return true, "Loop started. Time regulation enabled"
|
time_reg.set_status(2, "ACTIVE")
|
||||||
else
|
time_reg.update_constants()
|
||||||
return false, "Loop couldn't be started, it already is"
|
return true, "Loop started. Time regulation enabled"
|
||||||
end
|
else
|
||||||
else
|
return false, "Loop couldn't be started, it already is"
|
||||||
return false, "Unknown subcommand: " .. param
|
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
|
||||||
end
|
end
|
||||||
end
|
|
||||||
})
|
})
|
||||||
|
|
||||||
-- Startup informations
|
-- Startup informations
|
||||||
@ -212,5 +243,5 @@ log("Applied time speeds:")
|
|||||||
log("\tDay: " .. time_reg.day_time_speed)
|
log("\tDay: " .. time_reg.day_time_speed)
|
||||||
log("\tNight: " .. time_reg.night_time_speed)
|
log("\tNight: " .. time_reg.night_time_speed)
|
||||||
if not time_reg.enabled then
|
if not time_reg.enabled then
|
||||||
log("Time Regulation is disabled by default. Use /time_reg start to start it")
|
log("Time Regulation is disabled by default. Use /time_reg start to start it")
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user