aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authororwell96 <orwell@bleipb.de>2019-06-19 10:29:02 +0200
committerorwell96 <orwell@bleipb.de>2019-06-19 10:29:02 +0200
commitd569863434c2720444ce4302e3fbcbdfd8c69f82 (patch)
tree36a3c8ec7169f828341bca1dabd9e93f00e30503
parent24e56dbfc2b29229ed6253b84635a16db87659fb (diff)
downloadadvtrains-d569863434c2720444ce4302e3fbcbdfd8c69f82.tar.gz
advtrains-d569863434c2720444ce4302e3fbcbdfd8c69f82.tar.bz2
advtrains-d569863434c2720444ce4302e3fbcbdfd8c69f82.zip
Railway Time: atlatc interface, improve util functions
-rw-r--r--advtrains_line_automation/init.lua1
-rw-r--r--advtrains_line_automation/railwaytime.lua48
-rw-r--r--advtrains_luaautomation/environment.lua21
3 files changed, 60 insertions, 10 deletions
diff --git a/advtrains_line_automation/init.lua b/advtrains_line_automation/init.lua
index eba288f..0f4a4eb 100644
--- a/advtrains_line_automation/init.lua
+++ b/advtrains_line_automation/init.lua
@@ -40,5 +40,4 @@ end
function advtrains.lines.step(dtime)
advtrains.lines.rwt.step(dtime)
- atdebug(advtrains.lines.rwt.now())
end
diff --git a/advtrains_line_automation/railwaytime.lua b/advtrains_line_automation/railwaytime.lua
index d117af1..b6aa3de 100644
--- a/advtrains_line_automation/railwaytime.lua
+++ b/advtrains_line_automation/railwaytime.lua
@@ -81,12 +81,33 @@ function rwt.add(t1, t2)
return rwt.from_sec(t1s + t2s)
end
-function rwt.sub(t1, t2)
+function rwt.add_secs(t1, t2s)
+ local t1s = rwt.to_sec(t1)
+ return rwt.from_sec(t1s + t2s)
+end
+
+-- How many seconds FROM t1 TO t2
+function rwt.diff(t1, t2)
local t1s = rwt.to_sec(t1)
local t2s = rwt.to_sec(t1)
- return rwt.from_sec(t1s - t2s)
+ return t2s - t1s
+end
+
+-- Subtract t2 from t1 (inverted argument order compared to diff())
+function rwt.sub(t1, t2)
+ return rwt.from_sec(rwt.diff(t2, t1))
end
+-- Adjusts t2 by thresh and then returns time from t1 to t2
+function rwt.adj_diff(t1, t2, thresh)
+ local newc = rwt.adjust_cycle(t2, thresh, t1)
+ local t1s = rwt.to_sec(t1)
+ local t2s = rwt.to_sec(t2, newc)
+ return t1s - t2s
+end
+
+
+
-- Threshold values
-- "reftime" is the time to which this is made relative and defaults to now.
rwt.CA_FUTURE = 60*60 - 1 -- Selected so that time lies at or in the future of reftime (at nearest point in time)
@@ -96,8 +117,8 @@ rwt.CA_PASTS = -1 -- Same, except when times are equal, goes back one full cy
rwt.CA_CENTER = 30*60 -- If time is within past 30 minutes of reftime, selected as past, else selected as future.
-- Adjusts the "cycle" value of a railway time to be in some relation to reftime.
--- Modifies rwtime in-place
-function rwt.adjust(rwtime, reftime_p, thresh)
+-- Returns new cycle
+function rwt.adjust_cycle(rwtime, reftime_p, thresh)
local reftime = reftime_p or rwt.now()
local reftimes = rwt.to_sec(reftime)
@@ -106,13 +127,13 @@ function rwt.adjust(rwtime, reftime_p, thresh)
local timeres = reftimes + thresh - rwtimes
local cycles = atfloor(timeres / (60*60))
- rwtime.c = cycles
+ return cycles
end
-function rwt.get_adjust(rwtime, reftime, thresh)
- local res = rwt.copy(rwtime)
- rwt.adjust(res, reftime, thresh)
- return res
+function rwt.adjust(rwtime, reftime, thresh)
+ local cp = rwt.copy(rwtime)
+ cp.c = rwt.adjust(rwtime, reftime, thresh)
+ return cp
end
function rwt.to_string(rwtime, places)
@@ -127,6 +148,15 @@ function rwt.to_string(rwtime, places)
return str
end
+-- Useful for departure times: returns time (in seconds)
+-- until the next (adjusted FUTURE) occurence of deptime is reached
+-- in this case, rwtime is used as reftime and deptime should lie in the future of rwtime
+-- rwtime defaults to NOW
+function rwt.get_time_until(deptime, rwtime_p)
+ local rwtime = rwtime_p or rwt.now()
+ return rwt.adj_diff(rwtime, deptime, rwt.CA_FUTURE)
+end
+
advtrains.lines.rwt = rwt
diff --git a/advtrains_luaautomation/environment.lua b/advtrains_luaautomation/environment.lua
index e0bfa69..21e86ff 100644
--- a/advtrains_luaautomation/environment.lua
+++ b/advtrains_luaautomation/environment.lua
@@ -223,6 +223,27 @@ if advtrains.interlocking then
end
end
+-- Lines-specific:
+if advtrains.lines then
+ local atlrwt = advtrains.lines.rwt
+ static_env.rwt = {
+ now = atlrwt.now,
+ new = atlrwt.new,
+ copy = atlrwt.copy,
+ from_sec = atlrwt.from_sec,
+ to_sec = atlrwt.to_sec,
+ add = atlrwt.add,
+ add_secs = atlrwt.add_secs,
+ diff = atlrwt.diff,
+ sub = atlrwt.sub,
+ adj_diff = atlrwt.adj_diff,
+ adjust_cycle = atlrwt.adjust_cycle,
+ adjust = atlrwt.adjust,
+ to_string = atlrwt.to_string,
+ get_time_until = atlrwt.get_time_until,
+ }
+end
+
for _, name in pairs(safe_globals) do
static_env[name] = _G[name]
end