mirror of https://github.com/OpenMW/openmw.git
Merge branch 'lua_pause' into 'master'
A bit more lua bindings for game time See merge request OpenMW/openmw!1455psi29a-master-patch-54550
commit
0826de7edf
@ -0,0 +1,5 @@
|
||||
Package openmw_aux.calendar
|
||||
===========================
|
||||
|
||||
.. raw:: html
|
||||
:file: generated_html/openmw_aux_calendar.html
|
@ -0,0 +1,5 @@
|
||||
Package openmw_aux.time
|
||||
=======================
|
||||
|
||||
.. raw:: html
|
||||
:file: generated_html/openmw_aux_time.html
|
@ -0,0 +1,42 @@
|
||||
-- source: https://en.uesp.net/wiki/Lore:Calendar
|
||||
|
||||
return {
|
||||
month1 = "Morning Star",
|
||||
month2 = "Sun's Dawn",
|
||||
month3 = "First Seed",
|
||||
month4 = "Rain's Hand",
|
||||
month5 = "Second Seed",
|
||||
month6 = "Midyear",
|
||||
month7 = "Sun's Height",
|
||||
month8 = "Last Seed",
|
||||
month9 = "Hearthfire",
|
||||
month10 = "Frostfall",
|
||||
month11 = "Sun's Dusk",
|
||||
month12 = "Evening Star",
|
||||
|
||||
-- The variant of month names in the context "day X of month Y".
|
||||
-- In English it is the same, but some languages require a different form.
|
||||
monthInGenitive1 = "Morning Star",
|
||||
monthInGenitive2 = "Sun's Dawn",
|
||||
monthInGenitive3 = "First Seed",
|
||||
monthInGenitive4 = "Rain's Hand",
|
||||
monthInGenitive5 = "Second Seed",
|
||||
monthInGenitive6 = "Midyear",
|
||||
monthInGenitive7 = "Sun's Height",
|
||||
monthInGenitive8 = "Last Seed",
|
||||
monthInGenitive9 = "Hearthfire",
|
||||
monthInGenitive10 = "Frostfall",
|
||||
monthInGenitive11 = "Sun's Dusk",
|
||||
monthInGenitive12 = "Evening Star",
|
||||
|
||||
dateFormat = "day %{day} of %{monthInGenitive} %{year}",
|
||||
|
||||
weekday1 = "Sundas",
|
||||
weekday2 = "Morndas",
|
||||
weekday3 = "Tirdas",
|
||||
weekday4 = "Middas",
|
||||
weekday5 = "Turdas",
|
||||
weekday6 = "Fredas",
|
||||
weekday7 = "Loredas",
|
||||
}
|
||||
|
@ -0,0 +1,159 @@
|
||||
---
|
||||
-- `openmw_aux.calendar` defines utility functions for formatting game time.
|
||||
-- Implementation can be found in `resources/vfs/openmw_aux/calendar.lua`.
|
||||
-- @module calendar
|
||||
-- @usage local calendar = require('openmw_aux.calendar')
|
||||
|
||||
local core = require('openmw.core')
|
||||
local time = require('openmw_aux.time')
|
||||
local i18n = core.i18n('Calendar')
|
||||
|
||||
local monthsDuration = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
|
||||
local daysInWeek = 7
|
||||
local daysInYear = 0
|
||||
for _, d in ipairs(monthsDuration) do daysInYear = daysInYear + d end
|
||||
|
||||
local startingYear = 427
|
||||
local startingYearDay = 227
|
||||
local startingWeekDay = 1
|
||||
|
||||
local function gameTime(t)
|
||||
if not t then
|
||||
return core.getGameTime()
|
||||
else
|
||||
local days = (t.year or 0) * daysInYear + (t.day or 0)
|
||||
for i = 1, (t.month or 1)-1 do
|
||||
days = days + monthsDuration[i]
|
||||
end
|
||||
return days * time.day + (t.hour or 0) * time.hour +
|
||||
(t.min or 0) * time.minute + (t.sec or 0) * time.second
|
||||
end
|
||||
end
|
||||
|
||||
local function defaultDateFormat(t)
|
||||
return i18n('dateFormat', {
|
||||
day = t.day,
|
||||
month = i18n('month' .. t.month),
|
||||
monthInGenitive = i18n('monthInGenitive' .. t.month),
|
||||
year = t.year,
|
||||
})
|
||||
end
|
||||
|
||||
local function formatGameTime(formatStr, timestamp)
|
||||
timestamp = timestamp or core.getGameTime()
|
||||
|
||||
local t = {}
|
||||
local day = math.floor(timestamp / time.day)
|
||||
t.year = math.floor(day / daysInYear) + startingYear
|
||||
t.yday = (day + startingYearDay - 1) % daysInYear + 1
|
||||
t.wday = (day + startingWeekDay - 1) % daysInWeek + 1
|
||||
timestamp = timestamp % time.day
|
||||
t.hour = math.floor(timestamp / time.hour)
|
||||
timestamp = timestamp % time.hour
|
||||
t.min = math.floor(timestamp / time.minute)
|
||||
t.sec = math.floor(timestamp) % time.minute
|
||||
|
||||
t.day = t.yday
|
||||
t.month = 1
|
||||
while t.day > monthsDuration[t.month] do
|
||||
t.day = t.day - monthsDuration[t.month]
|
||||
t.month = t.month + 1
|
||||
end
|
||||
|
||||
if formatStr == '*t' then return t end
|
||||
|
||||
local replFn = function(tag)
|
||||
if tag == '%a' or tag == '%A' then return i18n('weekday' .. t.wday) end
|
||||
if tag == '%b' or tag == '%B' then return i18n('monthInGenitive' .. t.month) end
|
||||
if tag == '%c' then
|
||||
return string.format('%02d:%02d %s', t.hour, t.min, defaultDateFormat(t))
|
||||
end
|
||||
if tag == '%d' then return string.format('%02d', t.day) end
|
||||
if tag == '%e' then return string.format('%2d', t.day) end
|
||||
if tag == '%H' then return string.format('%02d', t.hour) end
|
||||
if tag == '%I' then return string.format('%02d', (t.hour - 1) % 12 + 1) end
|
||||
if tag == '%M' then return string.format('%02d', t.min) end
|
||||
if tag == '%m' then return string.format('%02d', t.month) end
|
||||
if tag == '%p' then
|
||||
if t.hour > 0 and t.hour <= 12 then
|
||||
return 'am'
|
||||
else
|
||||
return 'pm'
|
||||
end
|
||||
end
|
||||
if tag == '%S' then return string.format('%02d', t.sec) end
|
||||
if tag == '%w' then return t.wday - 1 end
|
||||
if tag == '%x' then return defaultDateFormat(t) end
|
||||
if tag == '%X' then return string.format('%02d:%02d', t.hour, t.min) end
|
||||
if tag == '%Y' then return t.year end
|
||||
if tag == '%y' then return string.format('%02d', t.year % 100) end
|
||||
if tag == '%%' then return '%' end
|
||||
error('Unknown tag "'..tag..'"')
|
||||
end
|
||||
|
||||
res, _ = string.gsub(formatStr or '%c', '%%.', replFn)
|
||||
return res
|
||||
end
|
||||
|
||||
return {
|
||||
--- An equivalent of `os.time` for game time.
|
||||
-- See [https://www.lua.org/pil/22.1.html](https://www.lua.org/pil/22.1.html)
|
||||
-- @function [parent=#calendar] gameTime
|
||||
-- @param #table table a table which describes a date (optional).
|
||||
-- @return #number a timestamp.
|
||||
gameTime = gameTime,
|
||||
|
||||
--- An equivalent of `os.date` for game time.
|
||||
-- See [https://www.lua.org/pil/22.1.html](https://www.lua.org/pil/22.1.html).
|
||||
-- It is a slow function. Please try not to use it in every frame.
|
||||
-- @function [parent=#calendar] formatGameTime
|
||||
-- @param #string format format of date (optional)
|
||||
-- @param #number time time to format (default value is current time)
|
||||
-- @return #string a formatted string representation of `time`.
|
||||
formatGameTime = formatGameTime,
|
||||
|
||||
--- The number of months in a year
|
||||
-- @field [parent=#calendar] #number monthCount
|
||||
monthCount = #monthsDuration,
|
||||
|
||||
--- The number of days in a year
|
||||
-- @field [parent=#calendar] #number daysInYear
|
||||
daysInYear = daysInYear,
|
||||
|
||||
--- The number of days in a week
|
||||
-- @field [parent=#calendar] #number daysInWeek
|
||||
daysInWeek = daysInWeek,
|
||||
|
||||
--- The number of days in a month
|
||||
-- @function [parent=#calendar] daysInMonth
|
||||
-- @param monthIndex
|
||||
-- @return #number
|
||||
daysInMonth = function(m)
|
||||
return monthsDuration[(m-1) % #monthsDuration + 1]
|
||||
end,
|
||||
|
||||
--- The name of a month
|
||||
-- @function [parent=#calendar] monthName
|
||||
-- @param monthIndex
|
||||
-- @return #string
|
||||
monthName = function(m)
|
||||
return i18n('month' .. ((m-1) % #monthsDuration + 1))
|
||||
end,
|
||||
|
||||
--- The name of a month in genitive (for English is the same as `monthName`, but in some languages the form can differ).
|
||||
-- @function [parent=#calendar] monthNameInGenitive
|
||||
-- @param monthIndex
|
||||
-- @return #string
|
||||
monthNameInGenitive = function(m)
|
||||
return i18n('monthInGenitive' .. ((m-1) % #monthsDuration + 1))
|
||||
end,
|
||||
|
||||
--- The name of a weekday
|
||||
-- @function [parent=#calendar] weekdayName
|
||||
-- @param dayIndex
|
||||
-- @return #string
|
||||
weekdayName = function(d)
|
||||
return i18n('weekday' .. ((d-1) % daysInWeek + 1))
|
||||
end,
|
||||
}
|
||||
|
@ -0,0 +1,104 @@
|
||||
---
|
||||
-- `openmw_aux.time` defines utility functions for timers.
|
||||
-- Implementation can be found in `resources/vfs/openmw_aux/time.lua`.
|
||||
-- @module time
|
||||
-- @usage local time = require('openmw_aux.time')
|
||||
|
||||
local time = {
|
||||
second = 1,
|
||||
minute = 60,
|
||||
hour = 3600,
|
||||
day = 3600 * 24,
|
||||
GameTime = 'GameTime',
|
||||
SimulationTime = 'SimulationTime',
|
||||
}
|
||||
|
||||
---
|
||||
-- Alias of async:registerTimerCallback ; register a function as a timer callback.
|
||||
-- @function [parent=#time] registerTimerCallback
|
||||
-- @param #string name
|
||||
-- @param #function func
|
||||
-- @return openmw.async#TimerCallback
|
||||
function time.registerTimerCallback(name, fn)
|
||||
local async = require('openmw.async')
|
||||
return async:registerTimerCallback(name, fn)
|
||||
end
|
||||
|
||||
---
|
||||
-- Alias of async:newSimulationTimer ; call callback(arg) in `delay` game seconds.
|
||||
-- Callback must be registered in advance.
|
||||
-- @function [parent=#time] newGameTimer
|
||||
-- @param #number delay
|
||||
-- @param openmw.async#TimerCallback callback A callback returned by `registerTimerCallback`
|
||||
-- @param arg An argument for `callback`; can be `nil`.
|
||||
function time.newGameTimer(delay, callback, callbackArg)
|
||||
local async = require('openmw.async')
|
||||
return async:newGameTimer(delay, callback, callbackArg)
|
||||
end
|
||||
|
||||
---
|
||||
-- Alias of async:newSimulationTimer ; call callback(arg) in `delay` simulation seconds.
|
||||
-- Callback must be registered in advance.
|
||||
-- @function [parent=#time] newSimulationTimer
|
||||
-- @param #number delay
|
||||
-- @param openmw.async#TimerCallback callback A callback returned by `registerTimerCallback`
|
||||
-- @param arg An argument for `callback`; can be `nil`.
|
||||
function time.newSimulationTimer(delay, callback, callbackArg)
|
||||
local async = require('openmw.async')
|
||||
return async:newSimulationTimer(delay, callback, callbackArg)
|
||||
end
|
||||
|
||||
---
|
||||
-- Run given function repeatedly.
|
||||
-- Note that loading a save stops the evaluation. If it should work always, call it during initialization of the script (i.e. not in a handler)
|
||||
-- @function [parent=#time] runRepeatedly
|
||||
-- @param #function fn the function that should be called
|
||||
-- @param #number period interval
|
||||
-- @param #table options additional options `initialDelay` and `type`.
|
||||
-- `initialDelay` - delay before the first call. If missed then the delay is a random number in range [0, N]. Randomization is used for performance reasons -- to prevent all scripts from doing time consuming operations at the same time.
|
||||
-- `type` - either `time.SimulationTime` (by default, timer uses simulation time) or `time.GameTime` (timer uses game time).
|
||||
-- @return #function a function without arguments that can be used to stop the periodical evaluation.
|
||||
-- @usage
|
||||
-- local stopFn = time.runRepeatedly(function() print('Test') end,
|
||||
-- 5 * time.second) -- print 'Test' every 5 seconds
|
||||
-- stopFn() -- stop printing 'Test'
|
||||
-- time.runRepeatedly( -- print 'Test' every 5 minutes with initial 30 second delay
|
||||
-- function() print('Test2') end, 5 * time.minute,
|
||||
-- { initialDelay = 30 * time.second })
|
||||
-- @usage
|
||||
-- local timeBeforeMidnight = time.day - time.gameTime() % time.day
|
||||
-- time.runRepeatedly(doSomething, time.day, {
|
||||
-- initialDelay = timeBeforeMidnight,
|
||||
-- type = time.GameTime,
|
||||
-- }) -- call `doSomething` at the end of every game day.
|
||||
function time.runRepeatedly(fn, period, options)
|
||||
if period <= 0 then
|
||||
error('Period must be positive. If you want it to be as small '..
|
||||
'as possible, use the engine handler `onUpdate` instead', 2)
|
||||
end
|
||||
local async = require('openmw.async')
|
||||
local core = require('openmw.core')
|
||||
local initialDelay = (options and options.initialDelay) or math.random() * period
|
||||
local getTimeFn, newTimerFn
|
||||
if (options and options.type) == time.GameTime then
|
||||
getTimeFn = core.getGameTime
|
||||
newTimerFn = async.newUnsavableGameTimer
|
||||
else
|
||||
getTimeFn = core.getSimulationTime
|
||||
newTimerFn = async.newUnsavableSimulationTimer
|
||||
end
|
||||
local baseTime = getTimeFn() + initialDelay
|
||||
local breakFlag = false
|
||||
local wrappedFn
|
||||
wrappedFn = function()
|
||||
if breakFlag then return end
|
||||
fn()
|
||||
local nextDelay = 1.5 * period - math.fmod(getTimeFn() - baseTime + period / 2, period)
|
||||
newTimerFn(async, nextDelay, wrappedFn)
|
||||
end
|
||||
newTimerFn(async, initialDelay, wrappedFn)
|
||||
return function() breakFlag = true end
|
||||
end
|
||||
|
||||
return time
|
||||
|
@ -0,0 +1,64 @@
|
||||
-------------------------------------------------------------------------------
|
||||
-- Operating System Facilities.
|
||||
-- This library is implemented through table os.
|
||||
-- @module os
|
||||
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Returns a string or a table containing date and time, formatted according
|
||||
-- to the given string `format`.
|
||||
--
|
||||
-- If the `time` argument is present, this is the time to be formatted
|
||||
-- (see the `os.time` function for a description of this value). Otherwise,
|
||||
-- `date` formats the current time.
|
||||
--
|
||||
-- If `format` starts with '`!`', then the date is formatted in Coordinated
|
||||
-- Universal Time. After this optional character, if `format` is the string
|
||||
-- "`*t`", then `date` returns a table with the following fields:
|
||||
--
|
||||
-- * `year` (four digits)
|
||||
-- * `month` (1--12)
|
||||
-- * `day` (1--31)
|
||||
-- * `hour` (0--23)
|
||||
-- * `min` (0--59)
|
||||
-- * `sec` (0--61)
|
||||
-- * `wday` (weekday, Sunday is 1)
|
||||
-- * `yday` (day of the year)
|
||||
-- * `isdst` (daylight saving flag, a boolean).
|
||||
--
|
||||
-- If `format` is not "`*t`", then `date` returns the date as a string,
|
||||
-- formatted according to the same rules as the C function `strftime`.
|
||||
-- When called without arguments, `date` returns a reasonable date and time
|
||||
-- representation that depends on the host system and on the current locale
|
||||
-- (that is, `os.date()` is equivalent to `os.date("%c")`).
|
||||
-- @function [parent=#os] date
|
||||
-- @param #string format format of date. (optional)
|
||||
-- @param #number time time to format. (default value is current time)
|
||||
-- @return #string a formatted string representation of `time`.
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Returns the number of seconds from time `t1` to time `t2`. In POSIX,
|
||||
-- Windows, and some other systems, this value is exactly `t2`*-*`t1`.
|
||||
-- @function [parent=#os] difftime
|
||||
-- @param #number t2
|
||||
-- @param #number t1
|
||||
-- @return #number the number of seconds from time `t1` to time `t2`.
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Returns the current time when called without arguments, or a time
|
||||
-- representing the date and time specified by the given table. This table
|
||||
-- must have fields `year`, `month`, and `day`, and may have fields `hour`,
|
||||
-- `min`, `sec`, and `isdst` (for a description of these fields, see the
|
||||
-- `os.date` function).
|
||||
--
|
||||
-- The returned value is a number, whose meaning depends on your system. In
|
||||
-- POSIX, Windows, and some other systems, this number counts the number
|
||||
-- of seconds since some given start time (the "epoch"). In other systems,
|
||||
-- the meaning is not specified, and the number returned by `time` can be
|
||||
-- used only as an argument to `date` and `difftime`.
|
||||
-- @function [parent=#os] time
|
||||
-- @param #table table a table which describes a date.
|
||||
-- @return #number a number meaning a date.
|
||||
|
||||
return nil
|
||||
|
Loading…
Reference in New Issue