Use player attribute storage for stats.
This commit is contained in:
parent
bb25e23917
commit
4efa14179d
227
init.lua
227
init.lua
@ -23,53 +23,49 @@ STAMINA_STARVE_LVL = 3 -- level of staturation that causes starving
|
||||
|
||||
STAMINA_VISUAL_MAX = 20 -- hud bar extends only to 20
|
||||
|
||||
SPRINT_SPEED = 0.8 -- how much faster player can run if satiated
|
||||
SPRINT_JUMP = 0.1 -- how much higher player can jump if satiated
|
||||
SPRINT_DRAIN = 0.35 -- how fast to drain satation while sprinting (0-1)
|
||||
SPRINT_SPEED = 0.8 -- how much faster player can run if satiated
|
||||
SPRINT_JUMP = 0.1 -- how much higher player can jump if satiated
|
||||
SPRINT_DRAIN = 0.35 -- how fast to drain satation while sprinting (0-1)
|
||||
|
||||
local function stamina_read(player)
|
||||
local inv = player:get_inventory()
|
||||
if not inv then
|
||||
local function get_int_attribute(player, key)
|
||||
local level = player:get_attribute(key)
|
||||
if level then
|
||||
return tonumber( level )
|
||||
else
|
||||
return nil
|
||||
end
|
||||
|
||||
-- itemstack storage is offest by 1 to make the math work
|
||||
local v = inv:get_stack("stamina", 1):get_count()
|
||||
if v == 0 then
|
||||
v = 21
|
||||
inv:set_stack("stamina", 1, ItemStack({name = ":", count = v}))
|
||||
end
|
||||
|
||||
return v - 1
|
||||
end
|
||||
|
||||
local function stamina_save(player)
|
||||
local inv = player:get_inventory()
|
||||
if not inv then
|
||||
return nil
|
||||
end
|
||||
local name = player:get_player_name()
|
||||
local level = stamina_players[name].level
|
||||
|
||||
level = math.max(level, 0)
|
||||
|
||||
inv:set_stack("stamina", 1, ItemStack({name = ":", count = level + 1}))
|
||||
return true
|
||||
local function stamina_get_level(player)
|
||||
return get_int_attribute(player, "stamina:level")
|
||||
end
|
||||
|
||||
local function stamina_update(player, level)
|
||||
local name = player:get_player_name()
|
||||
if not name then
|
||||
return false
|
||||
end
|
||||
local old = stamina_players[name].level
|
||||
if level == old then
|
||||
local function stamina_update_level(player, level)
|
||||
local old = stamina_get_level(player)
|
||||
|
||||
if level == old then -- To suppress HUD update
|
||||
return
|
||||
end
|
||||
stamina_players[name].level = level
|
||||
|
||||
player:hud_change(stamina_players[name].hud_id, "number", math.min(STAMINA_VISUAL_MAX, level))
|
||||
stamina_save(player)
|
||||
player:set_attribute("stamina:level", level)
|
||||
|
||||
player:hud_change(player:get_attribute("stamina:hud_id"), "number", math.min(STAMINA_VISUAL_MAX, level))
|
||||
end
|
||||
|
||||
local function stamina_is_poisoned(player)
|
||||
return player:get_attribute("stamina:poisoned") == "yes"
|
||||
end
|
||||
|
||||
local function stamina_set_poisoned(player, poisoned)
|
||||
if poisoned then
|
||||
player:set_attribute("stamina:poisoned", "yes")
|
||||
else
|
||||
player:set_attribute("stamina:poisoned", "no")
|
||||
end
|
||||
end
|
||||
|
||||
local function stamina_get_exhaustion(player)
|
||||
return get_int_attribute(player, "stamina:exhaustion")
|
||||
end
|
||||
|
||||
-- global function for mods to amend stamina level
|
||||
@ -78,10 +74,10 @@ stamina.change = function(player, change)
|
||||
if not name or not change or change == 0 then
|
||||
return false
|
||||
end
|
||||
local level = stamina_players[name].level + change
|
||||
local level = stamina_get_level(player) + change
|
||||
if level < 0 then level = 0 end
|
||||
if level > STAMINA_VISUAL_MAX then level = STAMINA_VISUAL_MAX end
|
||||
stamina_update(player, level)
|
||||
stamina_update_level(player, level)
|
||||
return true
|
||||
end
|
||||
|
||||
@ -95,27 +91,19 @@ local function exhaust_player(player, v)
|
||||
return
|
||||
end
|
||||
|
||||
local s = stamina_players[name]
|
||||
if not s then
|
||||
return
|
||||
end
|
||||
local exhaustion = stamina_get_exhaustion( player ) or 0
|
||||
|
||||
local e = s.exhaust
|
||||
if not e then
|
||||
s.exhaust = 0
|
||||
end
|
||||
exhaustion = exhaustion + v
|
||||
|
||||
e = e + v
|
||||
|
||||
if e > STAMINA_EXHAUST_LVL then
|
||||
e = 0
|
||||
local h = tonumber(stamina_players[name].level)
|
||||
if exhaustion > STAMINA_EXHAUST_LVL then
|
||||
exhaustion = 0
|
||||
local h = stamina_get_level(player)
|
||||
if h > 0 then
|
||||
stamina_update(player, h - 1)
|
||||
stamina_update_level(player, h - 1)
|
||||
end
|
||||
end
|
||||
|
||||
s.exhaust = e
|
||||
player:set_attribute("stamina:exhaustion", exhaustion)
|
||||
end
|
||||
|
||||
-- Sprint settings and function
|
||||
@ -124,45 +112,37 @@ local enable_sprint_particles = minetest.setting_getbool("sprint_particles") ~=
|
||||
local armor_mod = minetest.get_modpath("3d_armor")
|
||||
|
||||
function set_sprinting(name, sprinting)
|
||||
local player = minetest.get_player_by_name(name)
|
||||
local def = {}
|
||||
if armor_mod and armor and armor.def then
|
||||
def = armor.def[name] -- get player physics from armor
|
||||
end
|
||||
|
||||
if stamina_players[name] then
|
||||
def.speed = def.speed or 1
|
||||
def.jump = def.jump or 1
|
||||
def.gravity = def.gravity or 1
|
||||
|
||||
local player = minetest.get_player_by_name(name)
|
||||
local def = {}
|
||||
if armor_mod and armor and armor.def then
|
||||
def = armor.def[name] -- get player physics from armor
|
||||
end
|
||||
if sprinting == true then
|
||||
|
||||
def.speed = def.speed or 1
|
||||
def.jump = def.jump or 1
|
||||
def.gravity = def.gravity or 1
|
||||
|
||||
if sprinting == true then
|
||||
|
||||
player:set_physics_override({
|
||||
speed = def.speed + SPRINT_SPEED,
|
||||
jump = def.jump + SPRINT_JUMP,
|
||||
gravity = def.gravity
|
||||
})
|
||||
player:set_physics_override({
|
||||
speed = def.speed + SPRINT_SPEED,
|
||||
jump = def.jump + SPRINT_JUMP,
|
||||
gravity = def.gravity
|
||||
})
|
||||
|
||||
--print ("Speed:", def.speed + SPRINT_SPEED, "Jump:", def.jump + SPRINT_JUMP, "Gravity:", def.gravity)
|
||||
|
||||
elseif sprinting == false then
|
||||
elseif sprinting == false then
|
||||
|
||||
player:set_physics_override({
|
||||
speed = def.speed,
|
||||
jump = def.jump,
|
||||
gravity = def.gravity
|
||||
})
|
||||
player:set_physics_override({
|
||||
speed = def.speed,
|
||||
jump = def.jump,
|
||||
gravity = def.gravity
|
||||
})
|
||||
|
||||
--print ("Speed:", def.speed, "Jump:", def.jump, "Gravity:", def.gravity)
|
||||
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
return false
|
||||
end
|
||||
|
||||
-- Time based stamina functions
|
||||
@ -193,7 +173,7 @@ local function stamina_globaltimer(dtime)
|
||||
-- check if player can sprint (stamina must be over 6 points)
|
||||
if controls.aux1 and controls.up
|
||||
and not minetest.check_player_privs(player, {fast = true})
|
||||
and stamina_players[name].level > 6 then
|
||||
and stamina_get_level(player) > 6 then
|
||||
|
||||
set_sprinting(name, true)
|
||||
|
||||
@ -228,8 +208,8 @@ local function stamina_globaltimer(dtime)
|
||||
end
|
||||
|
||||
-- Lower the player's stamina when sprinting
|
||||
local level = tonumber(stamina_players[name].level)
|
||||
stamina_update(player, level - (SPRINT_DRAIN * STAMINA_MOVE_TICK))
|
||||
local level = stamina_get_level(player)
|
||||
stamina_update_level(player, level - (SPRINT_DRAIN * STAMINA_MOVE_TICK))
|
||||
else
|
||||
set_sprinting(name, false)
|
||||
end
|
||||
@ -243,13 +223,9 @@ local function stamina_globaltimer(dtime)
|
||||
-- lower saturation by 1 point after STAMINA_TICK second(s)
|
||||
if stamina_timer > STAMINA_TICK then
|
||||
for _,player in ipairs(minetest.get_connected_players()) do
|
||||
local name = player:get_player_name()
|
||||
local tab = stamina_players[name]
|
||||
if tab then
|
||||
local h = tab.level
|
||||
if h > STAMINA_TICK_MIN then
|
||||
stamina_update(player, h - 1)
|
||||
end
|
||||
local h = stamina_get_level(player)
|
||||
if h > STAMINA_TICK_MIN then
|
||||
stamina_update_level(player, h - 1)
|
||||
end
|
||||
end
|
||||
stamina_timer = 0
|
||||
@ -259,24 +235,21 @@ local function stamina_globaltimer(dtime)
|
||||
if health_timer > STAMINA_HEALTH_TICK then
|
||||
for _,player in ipairs(minetest.get_connected_players()) do
|
||||
local name = player:get_player_name()
|
||||
local tab = stamina_players[name]
|
||||
if tab then
|
||||
local air = player:get_breath() or 0
|
||||
local hp = player:get_hp()
|
||||
local air = player:get_breath() or 0
|
||||
local hp = player:get_hp()
|
||||
|
||||
-- don't heal if drowning or dead
|
||||
-- TODO: don't heal if poisoned?
|
||||
local h = tonumber(tab.level)
|
||||
if h >= STAMINA_HEAL_LVL and h >= hp and hp > 0 and air > 0
|
||||
and tab.poison == false then
|
||||
player:set_hp(hp + STAMINA_HEAL)
|
||||
stamina_update(player, h - 1)
|
||||
end
|
||||
-- don't heal if drowning or dead
|
||||
-- TODO: don't heal if poisoned?
|
||||
local h = stamina_get_level(player)
|
||||
if h >= STAMINA_HEAL_LVL and h >= hp and hp > 0 and air > 0
|
||||
and not stamina_is_poisoned(player) then
|
||||
player:set_hp(hp + STAMINA_HEAL)
|
||||
stamina_update_level(player, h - 1)
|
||||
end
|
||||
|
||||
-- or damage player by 1 hp if saturation is < 2 (of 30)
|
||||
if tonumber(tab.level) < STAMINA_STARVE_LVL then
|
||||
player:set_hp(hp - STAMINA_STARVE)
|
||||
end
|
||||
-- or damage player by 1 hp if saturation is < 2 (of 30)
|
||||
if stamina_get_level(player) < STAMINA_STARVE_LVL then
|
||||
player:set_hp(hp - STAMINA_STARVE)
|
||||
end
|
||||
end
|
||||
|
||||
@ -285,13 +258,12 @@ local function stamina_globaltimer(dtime)
|
||||
end
|
||||
|
||||
local function poison_player(ticks, time, elapsed, user)
|
||||
local name = user:get_player_name()
|
||||
if elapsed <= ticks then
|
||||
minetest.after(time, poison_player, ticks, time, elapsed + 1, user)
|
||||
stamina_players[name].poison = true
|
||||
stamina_set_poisoned(user,true)
|
||||
else
|
||||
user:hud_change(stamina_players[name].hud_id, "text", "stamina_hud_fg.png")
|
||||
stamina_players[name].poison = false
|
||||
user:hud_change(user:get_attribute("stamina:hud_id"), "text", "stamina_hud_fg.png")
|
||||
stamina_set_poisoned(user,false)
|
||||
end
|
||||
local hp = user:get_hp() -1 or 0
|
||||
if hp > 0 then
|
||||
@ -322,26 +294,21 @@ function stamina.eat(hp_change, replace_with_item, itemstack, user, pointed_thin
|
||||
return itemstack
|
||||
end
|
||||
|
||||
local name = user:get_player_name()
|
||||
if not stamina_players[name] then
|
||||
return itemstack
|
||||
end
|
||||
|
||||
local level = tonumber(stamina_players[name].level or 0)
|
||||
local level = stamina_get_level(user) or 0
|
||||
if level >= STAMINA_VISUAL_MAX then
|
||||
return itemstack
|
||||
end
|
||||
|
||||
if hp_change > 0 then
|
||||
level = level + hp_change
|
||||
stamina_update(user, level)
|
||||
stamina_update_level(user, level)
|
||||
else
|
||||
-- assume hp_change < 0.
|
||||
user:hud_change(stamina_players[name].hud_id, "text", "stamina_hud_poison.png")
|
||||
user:hud_change(user:get_attribute("stamina:hud_id"), "text", "stamina_hud_poison.png")
|
||||
poison_player(2.0, -hp_change, 0, user)
|
||||
end
|
||||
|
||||
minetest.sound_play("stamina_eat", {to_player = name, gain = 0.7})
|
||||
minetest.sound_play("stamina_eat", {to_player = user:get_player_name(), gain = 0.7})
|
||||
|
||||
|
||||
itemstack:take_item()
|
||||
@ -367,15 +334,13 @@ end
|
||||
-- stamina is disabled if damage is disabled
|
||||
if minetest.setting_getbool("enable_damage") and minetest.is_yes(minetest.setting_get("enable_stamina") or "1") then
|
||||
minetest.register_on_joinplayer(function(player)
|
||||
local inv = player:get_inventory()
|
||||
inv:set_size("stamina", 1)
|
||||
|
||||
local name = player:get_player_name()
|
||||
stamina_players[name] = {}
|
||||
stamina_players[name].level = stamina_read(player)
|
||||
stamina_players[name].exhaust = 0
|
||||
stamina_players[name].poison = false
|
||||
local level = math.min(stamina_players[name].level, STAMINA_VISUAL_MAX)
|
||||
local level = STAMINA_VISUAL_MAX -- TODO
|
||||
if stamina_get_level(player) then
|
||||
level = math.min(stamina_get_level(player), STAMINA_VISUAL_MAX)
|
||||
else
|
||||
player:set_attribute("stamina:level", level)
|
||||
end
|
||||
local id = player:hud_add({
|
||||
name = "stamina",
|
||||
hud_elem_type = "statbar",
|
||||
@ -387,7 +352,7 @@ if minetest.setting_getbool("enable_damage") and minetest.is_yes(minetest.settin
|
||||
offset = {x = -266, y = -110},
|
||||
max = 0,
|
||||
})
|
||||
stamina_players[name].hud_id = id
|
||||
player:set_attribute("stamina:hud_id", id)
|
||||
end)
|
||||
|
||||
minetest.register_globalstep(stamina_globaltimer)
|
||||
@ -406,6 +371,6 @@ if minetest.setting_getbool("enable_damage") and minetest.is_yes(minetest.settin
|
||||
end)
|
||||
|
||||
minetest.register_on_respawnplayer(function(player)
|
||||
stamina_update(player, STAMINA_VISUAL_MAX)
|
||||
stamina_update_level(player, STAMINA_VISUAL_MAX)
|
||||
end)
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user