From 7f4f8c606d27b902717be50643e7602c6e8efc90 Mon Sep 17 00:00:00 2001 From: orwell96 Date: Wed, 6 Nov 2019 21:22:33 +0100 Subject: lines: Add rwt adapt mode to adapt to real time --- advtrains_line_automation/railwaytime.lua | 46 ++++++++++++++++++++++++++++-- advtrains_line_automation/settingtypes.txt | 6 ++++ 2 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 advtrains_line_automation/settingtypes.txt diff --git a/advtrains_line_automation/railwaytime.lua b/advtrains_line_automation/railwaytime.lua index 0e222d2..bd16285 100644 --- a/advtrains_line_automation/railwaytime.lua +++ b/advtrains_line_automation/railwaytime.lua @@ -13,6 +13,9 @@ -- 43;3 22;0 2;30 0;10 ;10 -- Those places are then filled with zeroes. Indeed, ";" would be valid for 00;00 . +-- There is an "adapt mode", which was proposed by gpcf, and results in RWT automatically adapting itself to real-world time. +-- It works by shifting the minute/second after the realtime minute/second, adjusting the cycle value as needed. + -- Using negative times is discouraged. If you need a negative time, you may insert a minus (-) ONLY in the "c" place --[[ @@ -37,11 +40,37 @@ local rwt = {} --Time Stamp (Seconds since start of world) local e_time = 0 --- Current rw time, cached and updated each step -local crwtime +local setting_rwt_real = minetest.settings:get("advtrains_lines_rwt_realtime") +if setting_rwt_real=="" then + setting_rwt_real = "independent" +end + +local e_last_epoch -- last real-time timestamp + +-- Advance RWT to match minute/second to the current real-world time +-- only accounts for the minute/second part, leaves hour/cycle untouched +local function adapt_real_time() + local datetab = os.date("*t") + local real_sectotal = 60*datetab.min + datetab.sec + + local rwttab = rwt.now() + local rwt_sectotal = 60*rwttab.m + rwttab.s + + --calculate the difference and take it %3600 (seconds/hour) to always move forward + local secsfwd = (real_sectotal - rwt_sectotal) % 3600 + + atlog("[lines][rwt] Skipping",secsfwd,"seconds forward to sync rwt (",rwt.to_string(rwttab),") to real time (",os.date("%H:%M:%S"),")") + + e_time = e_time + secsfwd +end function rwt.set_time(t) e_time = t or 0 + if setting_rwt_real == "adapt_real" then + adapt_real_time() + end + atlog("[lines][rwt] Initialized railway time: ",rwt.to_string(e_time)) + e_last_epoch = os.time() end function rwt.get_time() @@ -49,7 +78,18 @@ function rwt.get_time() end function rwt.step(dt) - e_time = e_time + dt + if setting_rwt_real=="independent" then + -- Regular stepping with dtime + e_time = e_time + dt + else + -- advance with real-world time + local diff = os.time() - e_last_epoch + e_last_epoch = os.time() + + if diff>0 then + e_time = e_time + diff + end + end end function rwt.now() diff --git a/advtrains_line_automation/settingtypes.txt b/advtrains_line_automation/settingtypes.txt new file mode 100644 index 0000000..ea3f66e --- /dev/null +++ b/advtrains_line_automation/settingtypes.txt @@ -0,0 +1,6 @@ +# This controls how the Railway Time relates to real-world time: +# *independent - RWT counts independent of real time synchronized to ingame steps. Recommended for singleplayer and ad-hoc servers. +# *follow_real - RWT is independent of real time, but counts up in real-world time speed +# *adapt_real - RWT adapts its minute and second to real-world time. When the server is stopped and restarted, this results in a "time jump". +# Time will jump for 1 cycle (59;59) at maximum. +advtrains_lines_rwt_realtime (RWT real time adaption) enum independent independent,follow_real,adapt_real \ No newline at end of file -- cgit v1.2.3