aboutsummaryrefslogtreecommitdiff
path: root/advtrains_line_automation/railwaytime.lua
diff options
context:
space:
mode:
authororwell96 <orwell@bleipb.de>2019-11-06 21:22:33 +0100
committerorwell96 <orwell@bleipb.de>2019-11-06 21:22:33 +0100
commit7f4f8c606d27b902717be50643e7602c6e8efc90 (patch)
tree0e8882a44e50dba612cc775fcb4b75b57e425174 /advtrains_line_automation/railwaytime.lua
parenta3c468058a259f619b7257b84324ca5c47780885 (diff)
downloadadvtrains-7f4f8c606d27b902717be50643e7602c6e8efc90.tar.gz
advtrains-7f4f8c606d27b902717be50643e7602c6e8efc90.tar.bz2
advtrains-7f4f8c606d27b902717be50643e7602c6e8efc90.zip
lines: Add rwt adapt mode to adapt to real time
Diffstat (limited to 'advtrains_line_automation/railwaytime.lua')
-rw-r--r--advtrains_line_automation/railwaytime.lua46
1 files changed, 43 insertions, 3 deletions
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()