aboutsummaryrefslogtreecommitdiff
path: root/src/script/cpp_api/s_base.cpp
Commit message (Expand)AuthorAge
* HPChange Reason: Fix push after free, and type being overwritten (#8359)rubenwardy2019-03-12
* Move client-specific files to 'src/client' (#7902)Quentin Bazin2018-11-28
* Add a MSVC / Windows compatible snprintf function (#7353)nOOb31672018-07-22
* Modernize lua read (part 2 & 3): C++ templating assurance (#7410)Loïc Blot2018-06-30
* Add online content repositoryrubenwardy2018-04-19
* Add reasons to on_dieplayer and on_hpchangeAndrew Ward2018-03-28
* Move `setlocale` from Lua to C++.red-0012018-02-08
* [CSM] Don't Load the package library (#6944)red-0012018-01-22
* [CSM] Don't load the IO library. (#6087)red-0012018-01-04
* ServerEnv: Clean up object lifecycle handling (#6414)sfan52017-09-15
* Modernize source code: last part (#6285)Loïc Blot2017-08-20
* Create a filesystem abstraction layer for CSM and only allow accessing files ...red-0012017-06-30
* C++11 cleanup on constructors (#6000)Vincent Glize2017-06-19
* Fix a warning pointed by GCC 7.1Loic Blot2017-05-18
* [CSM] Add event on_place_node API lua (#5548)Vincent Glize2017-04-29
* [CSM] Client side moddingLoic Blot2017-03-13
* Make minetest abort on lua panicRogier2016-12-24
* Fix C++11 compilabilityest312016-01-23
* Fix Lua scripting synchronizationkwolekr2015-11-01
* Fix server crashing on Lua errorsShadowNinja2015-10-31
* Push error handler afresh each time lua_pcall is usedKahrl2015-08-27
* Use numeric indices and raw table access with LUA_REGISTRYINDEXKahrl2015-08-27
* SAPI: Track last executed mod and include in error messageskwolekr2015-08-12
* Improve Script CPP API diagnosticskwolekr2015-08-05
* Fix code style from recent commits and add misc. optimizationskwolekr2015-07-02
* Add Lua errors to error dialogrubenwardy2015-06-29
* Fix uninitialized variable errorest312015-06-02
* Add mod securityShadowNinja2015-05-16
* For usages of assert() that are meant to persist in Release builds (when NDEB...Craig Robbins2015-03-07
* Simplify loading of Android version of menuShadowNinja2014-11-20
* Fix object reference pushing functions when called from coroutinesShadowNinja2014-10-07
* Use "core" namespace internallyShadowNinja2014-05-08
* Organize builtin into subdirectoriesShadowNinja2014-05-07
* Only push the Lua error handler onceShadowNinja2014-04-27
* Remove lua_State parameter from LuaError::LuaErrorShadowNinja2014-03-15
* Pass arguments by referenceSelat2014-03-12
* Handle LuaErrors in Lua -> C++ calls on LuaJITShadowNinja2013-12-18
* Cleanup jthread and fix win32 buildsapier2013-12-01
* Pass a errfunc to lua_pcall to get a tracebackShadowNinja2013-11-15
* Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenuKahrl2013-08-14
* Move scriptapi to separate folder (by sapier)sapier2013-05-25
e(~hour), not displayed most time } Railway times can exist in 3 forms: - as table (see above) - as string (like "12;34") - as number (of seconds) Forms are automagically converted as needed by the converter functions to_* To be sure a rwt is in the required form, explicitly use a converter. ]] local rwt = {} --Time Stamp (Seconds since start of world) local e_time = 0 local e_has_loaded = false 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() e_has_loaded = true end function rwt.get_time() return e_time end function rwt.step(dt) if not e_has_loaded then rwt.set_time(0) end 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() return rwt.to_table(e_time) end function rwt.new(c, m, s) return { c = c or 0, m = m or 0, s = s or 0 } end function rwt.copy(rwtime) local rwtimet = rwt.to_table(rwtime) return { c = rwtimet.c or 0, m = rwtimet.m or 0, s = rwtimet.s or 0 } end function rwt.to_table(rwtime) if type(rwtime) == "table" then return rwtime elseif type(rwtime) == "string" then return rwt.parse(rwtime) elseif type(rwtime) == "number" then local res = {} local seconds = atfloor(rwtime) res.s = seconds % 60 local minutes = atfloor(seconds/60) res.m = minutes % 60 res.c = atfloor(minutes/60) return res end end function rwt.to_secs(rwtime, c_over) local res = rwtime if type(rwtime) == "string" then res = rwt.parse(rwtime) elseif type(rwtime) == "number" then return rwtime end if type(res)=="table" then return (c_over or res.c)*60*60 + res.m*60 + res.s end end function rwt.to_string(rwtime_p, no_cycle) local rwtime = rwt.to_table(rwtime_p) if rwtime.c~=0 and not no_cycle then return string.format("%d;%02d;%02d", rwtime.c, rwtime.m, rwtime.s) else return string.format("%02d;%02d", rwtime.m, rwtime.s) end end --- local function v_n(str, cpl) if not str then return nil end if str == "" then return 0 end local n = tonumber(str) if not cpl and (n<0 or n>59) then return nil end return n end function rwt.parse(str) --atdebug("parse",str) --3-value form local str_c, str_m, str_s = string.match(str, "^(%-?%d?%d?);(%d%d);(%d?%d?)$") if str_c and str_m and str_s then --atdebug("3v",str_c, str_m, str_s) local c, m, s = v_n(str_c, true), v_n(str_m), v_n(str_s) if c and m and s then return rwt.new(c,m,s) end end --2-value form local str_m, str_s = string.match(str, "^(%d?%d?);(%d?%d?)$") if str_m and str_s then --atdebug("2v",str_m, str_s) local m, s = v_n(str_m), v_n(str_s) if m and s then return rwt.new(0,m,s) end end end --- function rwt.add(t1, t2) local t1s = rwt.to_secs(t1) local t2s = rwt.to_secs(t2) return rwt.to_table(t1s + t2s) end -- How many seconds FROM t1 TO t2 function rwt.diff(t1, t2) local t1s = rwt.to_secs(t1) local t2s = rwt.to_secs(t2) return t2s - t1s end -- Subtract t2 from t1 (inverted argument order compared to diff()) function rwt.sub(t1, t2) return rwt.to_table(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_secs(t1) local t2s = rwt.to_secs(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) rwt.CA_FUTURES = 60*60 -- Same, except when times are equal, advances one full cycle rwt.CA_PAST = 0 -- Selected so that time lies at or in the past of reftime rwt.CA_PASTS = -1 -- Same, except when times are equal, goes back one full cycle 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. -- Returns new cycle function rwt.adjust_cycle(rwtime, reftime_p, thresh) local reftime = reftime_p or rwt.now() local reftimes = rwt.to_secs(reftime) local rwtimes = rwt.to_secs(rwtime, 0) local timeres = reftimes + thresh - rwtimes local cycles = atfloor(timeres / (60*60)) return cycles end function rwt.adjust(rwtime, reftime, thresh) local cp = rwt.copy(rwtime) cp.c = rwt.adjust_cycle(rwtime, reftime, thresh) return cp 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 -- Helper functions for handling "repeating times" (rpt) -- Those are generic declarations for time intervals like "every 5 minutes", with an optional offset -- ( /02;00-00;45 in timetable syntax -- Get the time (in seconds) until the next time this rpt occurs function rwt.time_to_next_rpt(rwtime, rpt_interval, rpt_offset) local rpti_s = rwt.to_secs(rpt_interval) return (rpti_s - rwt.time_from_last_rpt(rwtime, rpti_s, rpt_offset)) % rpti_s -- Modulo is just there to clip a false value of rpti_s to 0 end -- Get the time (in seconds) since the last time this rpt occured function rwt.time_from_last_rpt(rwtime, rpt_interval, rpt_offset) local rwtime_s = rwt.to_secs(rwtime) local rpti_s = rwt.to_secs(rpt_interval) local rpto_s = rwt.to_secs(rpt_offset) return ((rwtime_s - rpto_s) % rpti_s) end -- From rwtime, get the next time that is divisible by rpt_interval offset by rpt_offset function rwt.next_rpt(rwtime, rpt_interval, rpt_offset) local rwtime_s = rwt.to_secs(rwtime) local rpti_s = rwt.to_secs(rpt_interval) local time_from_last = rwt.time_from_last_rpt(rwtime_s, rpti_s, rpt_offset) local res_s = rwtime_s - time_from_last + rpti_s return rwt.to_table(res_s) end -- from rwtime, get the last time that this rpt matched (which is actually just next_rpt - rpt_offset function rwt.last_rpt(rwtime, rpt_interval, rpt_offset) local rwtime_s = rwt.to_sec(rwtime) local rpti_s = rwt.to_sec(rpt_interval) local time_from_last = rwt.time_from_last_rpt(rwtime, rpt_interval, rpt_offset) local res_s = rwtime_s - time_from_last return rwt.to_table(res_s) end advtrains.lines.rwt = rwt