use less globals

According to the Lua documentation using globals is slower than using
locals. Before this commit xTend mods used a lot of stuff from the
global _xtend table. A lot of those uses was not necessary.
This commit is contained in:
Dirk Sohler 2017-05-25 12:15:46 +02:00
parent 80b6cb68a3
commit 11468b08dc

@ -10,39 +10,40 @@ local _xtimer_location_value = function (o)
end
_xtend.v.xtimer = {
playerData = {},
interval = _xtend.g('xtimer_update_interval'),
placeholder = _xtend.g('xtimer_placeholder'),
font_color = _xtend.g('xtimer_font_color'),
format = {
locale = _xtend.g('xtimer_locale'),
start = _xtend.g('xtimer_session_start'),
runtime = _xtend.g('xtimer_session_runtime'),
current = _xtend.g('xtimer_current_time'),
ingame = _xtend.g('xtimer_ingame_time'),
output = _xtend.g('xtimer_output_format')
},
location = {
local interval = _xtend.g('xtimer_update_interval')
local placeholder = _xtend.g('xtimer_placeholder')
local font_color = _xtend.g('xtimer_font_color')
local format = {
locale = _xtend.g('xtimer_locale'),
start = _xtend.g('xtimer_session_start'),
runtime = _xtend.g('xtimer_session_runtime'),
current = _xtend.g('xtimer_current_time'),
ingame = _xtend.g('xtimer_ingame_time'),
output = _xtend.g('xtimer_output_format')
}
local location = {
position = _xtimer_location_value(_xtend.g('xtimer_position')),
alignment = _xtimer_location_value(_xtend.g('xtimer_alignment')),
offset = _xtimer_location_value(_xtend.g('xtimer_offset'))
}
}
local playerData = {}
minetest.register_on_joinplayer(function(player)
minetest.after(1, function(player)
local playerName = player:get_player_name()
local _hudID = player:hud_add({
hud_elem_type = 'text',
position = _xtend.v.xtimer.location.position,
alignment = _xtend.v.xtimer.location.alignment,
offset = _xtend.v.xtimer.location.offset,
text = _xtend.v.xtimer.placeholder,
number = '0x'.._xtend.v.xtimer.font_color
position = location.position,
alignment = location.alignment,
offset = location.offset,
text = placeholder,
number = '0x'..font_color
})
_xtend.v.xtimer.playerData[playerName] = {
playerData[playerName] = {
startTime = os.time(),
hudID = _hudID
}
@ -53,7 +54,7 @@ end)
minetest.register_on_leaveplayer(function(player)
local playerName = player:get_player_name()
_xtend.v.xtimer.playerData[playerName] = nil
playerData[playerName] = nil
end)
@ -64,24 +65,24 @@ function _xtimer_changeText(player)
-- Save the game-detected locale. Fun fact: changing the locale on runtime
-- from within a mod causes the UI to glitch out completely.
local currentLocale = os.setlocale(nil)
os.setlocale(_xtend.v.xtimer.format.locale)
os.setlocale(format.locale)
local startTime = _xtend.v.xtimer.playerData[playerName].startTime
local hudID = _xtend.v.xtimer.playerData[playerName].hudID
local startTime = playerData[playerName].startTime
local hudID = playerData[playerName].hudID
local time = 24*60*minetest.get_timeofday()
local h = tostring((math.floor(time/60) % 60))
local m = tostring((math.floor(time) % 60))
local res = _xtend.v.xtimer.format.output:gsub('(+.)', {
['+s'] = os.date(_xtend.v.xtimer.format.start, startTime),
['+c'] = os.date(_xtend.v.xtimer.format.current),
['+r'] = os.date('!'.._xtend.v.xtimer.format.runtime:gsub('(+.)', {
local res = format.output:gsub('(+.)', {
['+s'] = os.date(format.start, startTime),
['+c'] = os.date(format.current),
['+r'] = os.date('!'..format.runtime:gsub('(+.)', {
['+h'] = '%H',
['+m'] = '%M',
['+s'] = '%S'
}), os.time() - startTime),
['+i'] = _xtend.v.xtimer.format.ingame:gsub('(+.)', {
['+i'] = format.ingame:gsub('(+.)', {
['+h'] = string.rep('0', 2-#h)..h,
['+m'] = string.rep('0', 2-#m)..m
}),
@ -91,5 +92,5 @@ function _xtimer_changeText(player)
os.setlocale(currentLocale) -- Restore game-detected locale
player:hud_change(hudID, 'text', res)
minetest.after(_xtend.v.xtimer.interval, _xtimer_changeText, player)
minetest.after(interval, _xtimer_changeText, player)
end