landsandboat/scripts/utils/common.lua

65 lines
1.5 KiB
Lua
Raw Normal View History

2016-01-16 01:26:33 -05:00
-----------------------------------
-- switch
-----------------------------------
function switch(c)
local swtbl =
{
2018-05-01 21:53:02 -04:00
casevar = c,
caseof = function(self, code)
local f
if self.casevar then
f = code[self.casevar] or code.default
2018-05-01 21:53:02 -04:00
else
f = code.missing or code.default
2018-05-01 21:53:02 -04:00
end
if f then
if type(f) == 'function' then
return f(self.casevar, self)
2018-05-01 21:53:02 -04:00
else
error('case '..tostring(self.casevar)..' not a function')
2018-05-01 21:53:02 -04:00
end
end
end
}
return swtbl
end
2016-01-16 01:26:33 -05:00
-----------------------------------
-- printf
-----------------------------------
function printf(s, ...)
print(s:format(...))
end
2016-01-16 01:26:33 -05:00
2020-02-23 07:05:57 -08:00
-----------------------------------
-- set()
-- Returns a set that can be checked against
-----------------------------------
function set(list)
local set = {}
for _, item in pairs(list) do
set[item] = true
end
2020-02-23 07:05:57 -08:00
return set
end
-----------------------------------
-- getVanaMidnight(day)
-- Returns earth time value for midnight for current (or supplied day) in epoch format
-----------------------------------
function getVanaMidnight(day)
local curtime = GetSystemTime()
local secondsToMidnight = xi.vanaTime.DAY - (VanadielTime() % xi.vanaTime.DAY)
if day ~= nil then
secondsToMidnight = secondsToMidnight + (day * xi.vanaTime.DAY)
end
return curtime + secondsToMidnight
end