From 4f71b46e24fc9785dac2664f0788849a870dfd81 Mon Sep 17 00:00:00 2001 From: LeMagnesium Date: Tue, 16 Feb 2016 18:38:48 +0100 Subject: [PATCH] Add time_reg set - Allow forceful update in time_reg.loop - Add time_reg set [0.0 .. 100.0] - Fix day skipping --- .init.lua.swp | Bin 0 -> 1024 bytes init.lua | 265 ++++++++++++++++++++++++++++---------------------- 2 files changed, 148 insertions(+), 117 deletions(-) create mode 100644 .init.lua.swp diff --git a/.init.lua.swp b/.init.lua.swp new file mode 100644 index 0000000000000000000000000000000000000000..8340a4907601155097bdfbc2c4823407c3f6c868 GIT binary patch literal 1024 zcmYc?$V<%2S1{5u)iY#30%xQd7;-9ev&%D)1aUBZQ`2)&Gm}z_aLB`i^)vEwQ}rRH z>IW3%rxzvW<|dY8=I80VXO 0 then - time_reg.set_status(2, "ACTIVE") - end + time_reg.time_speed = minetest.setting_get("time_speed") + time_reg.do_calculation() + if time_reg.status == 1 and time_reg.time_speed > 0 then + time_reg.set_status(2, "ACTIVE") + end end function time_reg.start_loop() - if time_reg.loop_active then - minetest.log("action", "[TimeRegulation] Will not start the loop : one is already running") - return false - end - time_reg.loop_active = true - minetest.log("action", "[TimeRegulation] Loop started") - minetest.after(0, time_reg.loop, true) - return true + if time_reg.loop_active then + minetest.log("action", "[TimeRegulation] Will not start the loop : one is already running") + return false + end + time_reg.loop_active = true + minetest.log("action", "[TimeRegulation] Loop started") + minetest.after(0, time_reg.loop, true) + return true end function time_reg.stop_loop() - if not time_reg.loop_active then - minetest.log("action", "[TimeRegulation] Will not break the loop : no loop running") - return false - end - time_reg.loop_active = false - minetest.log("action", "[TimeRegulation] Loop asked to stop") - return true + if not time_reg.loop_active then + minetest.log("action", "[TimeRegulation] Will not break the loop : no loop running") + return false + end + time_reg.loop_active = false + minetest.log("action", "[TimeRegulation] Loop asked to stop") + return true end function time_reg.set_status(x, title) - minetest.log("action", "[TimeRegulation] Entered status " .. x .. " (" .. title .. ")") - time_reg.status = x + minetest.log("action", "[TimeRegulation] Entered status " .. x .. " (" .. title .. ")") + time_reg.status = x end -- And the loop -function time_reg.loop(loop) - -- Determine TOD and current moment - local tod = minetest.get_timeofday() * 24000 +function time_reg.loop(loop, forceupdate) + -- Determine TOD and current moment + local tod = minetest.get_timeofday() * 24000 - local moment = "day" - if tod < time_reg.threshold.day or tod > time_reg.threshold.night then - 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 + local moment = "day" + if tod < time_reg.threshold.day or tod > time_reg.threshold.night then + moment = "night" 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 + if time_reg.time_speed == 0 then + time_reg.set_status(1, "IDLE") + return + 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 time_reg.update_constants() if time_reg.enabled then - time_reg.start_loop() + time_reg.start_loop() end -- chatcommand minetest.register_chatcommand("time_reg", { - description = "Control time_regulation", - privs = {server = true}, - func = function(name, param) - if param == "" then - return true, "see /time_reg help" + description = "Control time_regulation", + privs = {server = true}, + func = function(name, param) + if param == "" then + return true, "see /time_reg help" - elseif param == "help" then - return true, "Supported commands: start, stop" - elseif param == "stop" then - local res = time_reg.stop_loop() - if res then - time_reg.set_status(0, "DEAD") - return true, "Loop was told to stop\nTime regulation disabled" - 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 - time_reg.set_status(2, "ACTIVE") - time_reg.update_constants() - return true, "Loop started. Time regulation enabled" - else - return false, "Loop couldn't be started, it already is" - end - else - return false, "Unknown subcommand: " .. param + elseif param == "help" then + return true, "Supported commands: start, stop, set" + + elseif param == "stop" then + local res = time_reg.stop_loop() + if res then + time_reg.set_status(0, "DEAD") + return true, "Loop was told to stop\nTime regulation disabled" + 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 + time_reg.set_status(2, "ACTIVE") + time_reg.update_constants() + return true, "Loop started. Time regulation enabled" + 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 end - end }) -- Startup informations @@ -212,5 +243,5 @@ log("Applied time speeds:") log("\tDay: " .. time_reg.day_time_speed) log("\tNight: " .. time_reg.night_time_speed) 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