diff options
author | orwell96 <orwell@bleipb.de> | 2019-06-19 10:29:02 +0200 |
---|---|---|
committer | orwell96 <orwell@bleipb.de> | 2019-06-19 10:29:02 +0200 |
commit | d569863434c2720444ce4302e3fbcbdfd8c69f82 (patch) | |
tree | 36a3c8ec7169f828341bca1dabd9e93f00e30503 /advtrains_line_automation | |
parent | 24e56dbfc2b29229ed6253b84635a16db87659fb (diff) | |
download | advtrains-d569863434c2720444ce4302e3fbcbdfd8c69f82.tar.gz advtrains-d569863434c2720444ce4302e3fbcbdfd8c69f82.tar.bz2 advtrains-d569863434c2720444ce4302e3fbcbdfd8c69f82.zip |
Railway Time: atlatc interface, improve util functions
Diffstat (limited to 'advtrains_line_automation')
-rw-r--r-- | advtrains_line_automation/init.lua | 1 | ||||
-rw-r--r-- | advtrains_line_automation/railwaytime.lua | 48 |
2 files changed, 39 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 |