API, Saving players stamina, Fix hud bugs

master
aa6 2016-04-30 23:44:16 +03:00
parent 6756a6beb1
commit 5de00a8a55
7 changed files with 234 additions and 21 deletions

View File

@ -1 +1 @@
0.2.10
0.3.1

View File

@ -1,5 +1,7 @@
-- Common config values
minetest_wadsprint.STAMINA_MAX_VALUE = 100
minetest_wadsprint.SAVE_PLAYERS_STATS_TO_FILE = true
minetest_wadsprint.PLAYERS_STATS_FILE_LIMIT_RECORDS = 100
minetest_wadsprint.PLAYER_STATS_UPDATE_PERIOD_SECONDS = 1
minetest_wadsprint.PLAYER_CONTROLS_CHECK_PERIOD_SECONDS = 0.2
minetest_wadsprint.SPRINT_SPEED_MODIFIER_COEFFICIENT = 3.8

View File

@ -2,11 +2,38 @@
-- @link https://github.com/aa6/minetest_wadsprint
minetest_wadsprint =
{
api = {},
players = {},
version = io.open(minetest.get_modpath(minetest.get_current_modname()).."/VERSION","r"):read("*all"),
savedstats = { index = {} },
savetablepath = minetest.get_modpath(minetest.get_current_modname()).."/saved_players_stats.dat",
}
dofile(minetest.get_modpath(minetest.get_current_modname()).."/config.lua")
dofile(minetest.get_modpath(minetest.get_current_modname()).."/init_hudbars.lua")
dofile(minetest.get_modpath(minetest.get_current_modname()).."/lib_savetable.lua")
dofile(minetest.get_modpath(minetest.get_current_modname()).."/lib_file_exists.lua")
-- API function to safely get player's stamina value.
function minetest_wadsprint.api.get_stamina_rate(player_name)
if minetest_wadsprint.players[player_name] ~= nil then
return minetest_wadsprint.players[player_name].stamina / minetest_wadsprint.STAMINA_MAX_VALUE
end
end
-- API function to safely change player's stamina.
function minetest_wadsprint.api.change_stamina_by_coefficient(player_name,stamina_change_coefficient)
if minetest_wadsprint.players[player_name] ~= nil then
local player = minetest_wadsprint.players[player_name]
player.stamina = player.stamina + (stamina_change_coefficient * minetest_wadsprint.STAMINA_MAX_VALUE)
if player.stamina < 0 then
player.stamina = 0
elseif player.stamina > minetest_wadsprint.STAMINA_MAX_VALUE then
player.stamina = minetest_wadsprint.STAMINA_MAX_VALUE
end
minetest_wadsprint.hudbar_update_stamina(player)
end
return minetest_wadsprint.api
end
function minetest_wadsprint.stamina_update_cycle(player)
if player.is_sprinting then
@ -70,21 +97,55 @@ function minetest_wadsprint.scan_player_controls(player)
end
end
function minetest_wadsprint.reset_stamina(player)
player.stamina = minetest_wadsprint.STAMINA_MAX_VALUE
function minetest_wadsprint.reset_stamina(player,stamina_value)
if stamina_value == nil then stamina_value = minetest_wadsprint.STAMINA_MAX_VALUE end
player.stamina = stamina_value
minetest_wadsprint.set_sprinting(player,false)
minetest_wadsprint.set_ready_to_sprint(player,false)
return player
end
function minetest_wadsprint.save_players_stats()
local stats = {}
local counter = 1
for key,val in pairs(minetest_wadsprint.players) do
stats[counter] = { name = key, stamina = val.stamina }
counter = counter + 1
end
for key,val in ipairs(minetest_wadsprint.savedstats) do
if minetest_wadsprint.players[val.name] == nil then
stats[counter] = { name = val.name, stamina = val.stamina }
counter = counter + 1
end
if counter == minetest_wadsprint.PLAYERS_STATS_FILE_LIMIT_RECORDS + 1 then
break
end
end
table.save(stats,minetest_wadsprint.savetablepath)
end
function minetest_wadsprint.load_players_stats()
if file_exists(minetest_wadsprint.savetablepath) then
minetest_wadsprint.savedstats = table.load(minetest_wadsprint.savetablepath)
minetest_wadsprint.savedstats.index = {}
for key,val in ipairs(minetest_wadsprint.savedstats) do
minetest_wadsprint.savedstats.index[val.name] = { stamina = val.stamina }
end
end
end
minetest.register_on_joinplayer(function(player_obj)
local player =
{
obj = player_obj,
}
minetest_wadsprint.players[player_obj:get_player_name()] = player
local player = {}
local playername = player_obj:get_player_name()
if minetest_wadsprint.savedstats.index[playername] ~= nil then
player = minetest_wadsprint.savedstats.index[playername]
else
player = { stamina = minetest_wadsprint.STAMINA_MAX_VALUE }
end
player.obj = player_obj
minetest_wadsprint.players[playername] = player
minetest_wadsprint.initialize_hudbar(player)
minetest_wadsprint.reset_stamina(player)
minetest_wadsprint.reset_stamina(player,player.stamina)
end)
minetest.register_on_respawnplayer(function(player_obj)
@ -92,13 +153,24 @@ minetest.register_on_respawnplayer(function(player_obj)
end)
minetest.register_on_leaveplayer(function(player_obj)
minetest_wadsprint.players[player_obj:get_player_name()] = nil
local playername = player_obj:get_player_name()
local player = minetest_wadsprint.players[playername]
table.insert(minetest_wadsprint.savedstats, 1, { name = playername, stamina = player.stamina})
minetest_wadsprint.savedstats.index[playername] = { stamina = player.stamina }
minetest_wadsprint.players[playername] = nil
end)
-- Register hudbar call for compatibility with some hudbar mods.
if minetest_wadsprint.register_hudbar ~= nil then
minetest_wadsprint.register_hudbar()
end
-- Save player stats to file on server shutdown.
if minetest_wadsprint.SAVE_PLAYERS_STATS_TO_FILE then
minetest_wadsprint.load_players_stats()
minetest.register_on_shutdown(minetest_wadsprint.save_players_stats)
end
-- Main cycle.
local timer_stats_update = 0
local timer_controls_check = 0

View File

@ -24,7 +24,7 @@ if minetest.get_modpath("hudbars") ~= nil then
hb.init_hudbar(
player.obj, -- `player`: `ObjectRef` of the player to which the new HUD bar should be displayed to.
minetest_wadsprint.HUDBARS_IDENTIFIER, -- `identifier`: The identifier of the HUD bar type, as specified in `hb.register_hudbar`.
nil, -- `start_value`: The initial current value of the HUD bar. This is optional, `default_start_value` of the registration function will be used, if this is `nil`.
math.ceil(player.stamina), -- `start_value`: The initial current value of the HUD bar. This is optional, `default_start_value` of the registration function will be used, if this is `nil`.
nil, -- `start_max`: The initial maximum value of the HUD bar. This is optional, `default_start_max` of the registration function will be used, if this is `nil`
nil -- `start_hidden`: Whether the HUD bar is initially hidden. This is optional, `default_start_hidden` of the registration function will be used as default.
)
@ -62,13 +62,7 @@ elseif minetest.get_modpath("hud") ~= nil then
)
end
function minetest_wadsprint.initialize_hudbar(player)
hud.change_item(
player.obj,
minetest_wadsprint.HUDHUNGER_BAR_NAME,
{
number = math.ceil(minetest_wadsprint.HUDHUNGER_HALF_ICONS_NUMBER),
}
)
minetest_wadsprint.hudbar_update_stamina(player)
end
function minetest_wadsprint.hudbar_update_ready_to_sprint(player)
if player.is_sprinting or player.is_ready_to_sprint then
@ -94,7 +88,14 @@ elseif minetest.get_modpath("hud") ~= nil then
player.obj,
minetest_wadsprint.HUDHUNGER_BAR_NAME,
{
number = math.ceil((player.stamina/minetest_wadsprint.STAMINA_MAX_VALUE)*minetest_wadsprint.HUDHUNGER_HALF_ICONS_NUMBER),
number = 0, -- Workaround for some obscure bug.
}
)
hud.change_item(
player.obj,
minetest_wadsprint.HUDHUNGER_BAR_NAME,
{
number = math.ceil((player.stamina / minetest_wadsprint.STAMINA_MAX_VALUE) * minetest_wadsprint.HUDHUNGER_HALF_ICONS_NUMBER),
}
)
end
@ -106,7 +107,7 @@ else
hud_elem_type = "statbar", -- HUD type. Statbar displays a horizontal bar made up of half-images.
size = minetest_wadsprint.MINETESTHUD_ICON_SIZE, -- `size`: If used will force full-image size to this value (override texture pack image size).
text = minetest_wadsprint.MINETESTHUD_IS_NOT_SPRINTING_ICON, -- `text`: The name of the texture that is used.
number = minetest_wadsprint.MINETESTHUD_HALF_ICONS_NUMBER, -- `number`: The number of half-textures that are displayed. If odd, will end with a vertically center-split texture.
number = math.ceil((player.stamina / minetest_wadsprint.STAMINA_MAX_VALUE) * minetest_wadsprint.MINETESTHUD_HALF_ICONS_NUMBER), -- `number`: The number of half-textures that are displayed. If odd, will end with a vertically center-split texture.
offset = minetest_wadsprint.MINETESTHUD_OFFSET, -- `offset`: Specifies a pixel offset from the position. Not scaled to the screen size. Note: offset WILL adapt to screen DPI as well as the user defined scaling factor!
position = minetest_wadsprint.MINETESTHUD_POSITION, -- `position`: Used for all element types. To account for differing resolutions, the position coordinates are the percentage of the screen, ranging in value from 0 to 1. 0 means left/top, 1 means right/bottom.
alignment = minetest_wadsprint.MINETESTHUD_ALIGNMENT, -- `alignment`: Specifies how the item will be aligned. It ranges from -1 to 1, with 0 being the center, -1 is moved to the left/up, and 1 is to the right/down. Fractional values can be used.
@ -120,6 +121,6 @@ else
end
end
function minetest_wadsprint.hudbar_update_stamina(player)
player.obj:hud_change(player.hud, "number", math.ceil((player.stamina/minetest_wadsprint.STAMINA_MAX_VALUE)*minetest_wadsprint.MINETESTHUD_HALF_ICONS_NUMBER))
player.obj:hud_change(player.hud, "number", math.ceil((player.stamina / minetest_wadsprint.STAMINA_MAX_VALUE) * minetest_wadsprint.MINETESTHUD_HALF_ICONS_NUMBER))
end
end

4
lib_file_exists.lua Normal file
View File

@ -0,0 +1,4 @@
function file_exists(name)
local fd = io.open(name,"r")
if fd ~= nil then io.close(fd) return true else return false end
end

134
lib_savetable.lua Normal file
View File

@ -0,0 +1,134 @@
--[[
Save Table to File
Load Table from File
v 1.0
Lua 5.2 compatible
Only Saves Tables, Numbers and Strings
Insides Table References are saved
Does not save Userdata, Metatables, Functions and indices of these
----------------------------------------------------
table.save( table , filename )
on failure: returns an error msg
----------------------------------------------------
table.load( filename or stringtable )
Loads a table that has been saved via the table.save function
on success: returns a previously saved table
on failure: returns as second argument an error msg
----------------------------------------------------
Licensed under the same terms as Lua itself.
]]--
do
-- declare local variables
--// exportstring( string )
--// returns a "Lua" portable version of the string
local function exportstring( s )
return string.format("%q", s)
end
--// The Save Function
function table.save( tbl,filename )
local charS,charE = " ","\n"
local file,err = io.open( filename, "wb" )
if err then return err end
-- initiate variables for save procedure
local tables,lookup = { tbl },{ [tbl] = 1 }
file:write( "return {"..charE )
for idx,t in ipairs( tables ) do
file:write( "-- Table: {"..idx.."}"..charE )
file:write( "{"..charE )
local thandled = {}
for i,v in ipairs( t ) do
thandled[i] = true
local stype = type( v )
-- only handle value
if stype == "table" then
if not lookup[v] then
table.insert( tables, v )
lookup[v] = #tables
end
file:write( charS.."{"..lookup[v].."},"..charE )
elseif stype == "string" then
file:write( charS..exportstring( v )..","..charE )
elseif stype == "number" then
file:write( charS..tostring( v )..","..charE )
end
end
for i,v in pairs( t ) do
-- escape handled values
if (not thandled[i]) then
local str = ""
local stype = type( i )
-- handle index
if stype == "table" then
if not lookup[i] then
table.insert( tables,i )
lookup[i] = #tables
end
str = charS.."[{"..lookup[i].."}]="
elseif stype == "string" then
str = charS.."["..exportstring( i ).."]="
elseif stype == "number" then
str = charS.."["..tostring( i ).."]="
end
if str ~= "" then
stype = type( v )
-- handle value
if stype == "table" then
if not lookup[v] then
table.insert( tables,v )
lookup[v] = #tables
end
file:write( str.."{"..lookup[v].."},"..charE )
elseif stype == "string" then
file:write( str..exportstring( v )..","..charE )
elseif stype == "number" then
file:write( str..tostring( v )..","..charE )
end
end
end
end
file:write( "},"..charE )
end
file:write( "}" )
file:close()
end
--// The Load Function
function table.load( sfile )
local ftables,err = loadfile( sfile )
if err then return _,err end
local tables = ftables()
for idx = 1,#tables do
local tolinki = {}
for i,v in pairs( tables[idx] ) do
if type( v ) == "table" then
tables[idx][i] = tables[v[1]]
end
if type( i ) == "table" and tables[i[1]] then
table.insert( tolinki,{ i,tables[i[1]] } )
end
end
-- link indices
for _,v in ipairs( tolinki ) do
tables[idx][v[2]],tables[idx][v[1]] = tables[idx][v[1]],nil
end
end
return tables[1]
end
-- close do
end
-- ChillCode

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.8 KiB

After

Width:  |  Height:  |  Size: 1.4 KiB