minetest-mm/mods/stamina/init.lua

735 lines
18 KiB
Lua
Raw Normal View History

2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
-- global
stamina = {
players = {}, mod = "redo",
-- time in seconds after that 1 stamina point is taken
TICK = tonumber(minetest.settings:get("stamina_tick")) or 800,
-- stamina ticks won't reduce stamina below this level
TICK_MIN = 4,
-- time in seconds after player gets healed/damaged
HEALTH_TICK = 4,
-- time in seconds after the movement is checked
MOVE_TICK = 0.5,
-- time in seconds after player is poisoned
POISON_TICK = 1.25,
-- exhaustion increased this value after digged node
EXHAUST_DIG = 2,
-- .. after digging node
EXHAUST_PLACE = 1,
-- .. if player movement detected
EXHAUST_MOVE = 1.5,
-- .. if jumping
EXHAUST_JUMP = 5,
-- .. if player crafts
EXHAUST_CRAFT = 2,
-- .. if player punches another player
EXHAUST_PUNCH = 40,
-- at what exhaustion player saturation gets lowered
EXHAUST_LVL = 160,
-- number of HP player gets healed after stamina.HEALTH_TICK
HEAL = 1,
-- lower level of saturation needed to get healed
HEAL_LVL = 5,
-- number of HP player gets damaged by stamina after stamina.HEALTH_TICK
STARVE = 1,
-- level of staturation that causes starving
STARVE_LVL = 3,
-- hud bar extends only to 20
VISUAL_MAX = 20
}
-- Translation support & localize math functions
local S = minetest.get_translator("stamina")
local math_max, math_min = math.max, math.min
local math_floor, math_random = math.floor, math.random
-- clamp values helper
local function clamp(val, minval, maxval)
return math_max(math_min(val, maxval), minval)
end
-- how much faster players can run if satiated.
local SPRINT_SPEED = clamp(tonumber(
minetest.settings:get("stamina_sprint_speed")) or 0.5, 0.0, 1.0)
-- how much higher player can jump if satiated
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
local SPRINT_JUMP = clamp(tonumber(
minetest.settings:get("stamina_sprint_jump")) or 0.1, 0.0, 1.0)
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
-- how fast to drain satation while sprinting (0-1)
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
local SPRINT_DRAIN = clamp(tonumber(
minetest.settings:get("stamina_sprint_drain")) or 0.35, 0.0, 1.0)
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
-- are we a real player ?
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
local function is_player(player)
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
if player and type(player) == "userdata" and minetest.is_player(player) then
return true
end
end
-- grab stamina level
2020-10-26 17:38:53 +01:00
local function get_int_attribute(player)
-- pipeworks fake player check
2024-12-19 12:55:40 +01:00
if not is_player(player) then
2020-10-26 17:38:53 +01:00
return nil
end
local meta = player:get_meta()
local level = meta and meta:get_string("stamina:level")
if level then
return tonumber(level)
end
return nil
end
2024-12-19 12:55:40 +01:00
-- is player stamina & damage enabled
2020-10-26 17:38:53 +01:00
2021-01-02 22:12:17 +01:00
local stamina_enabled = minetest.settings:get_bool("enable_stamina") ~= false
2024-12-19 12:55:40 +01:00
local damage_enabled = minetest.settings:get_bool("enable_damage")
-- update stamina level
2021-01-02 22:12:17 +01:00
2020-10-26 17:38:53 +01:00
local function stamina_update_level(player, level)
-- pipeworks fake player check
2024-12-19 12:55:40 +01:00
if not is_player(player) then return nil end
2020-10-26 17:38:53 +01:00
local old = get_int_attribute(player)
2024-12-19 12:55:40 +01:00
if level == old then return end -- To suppress HUD update
2020-10-26 17:38:53 +01:00
-- players without interact priv cannot eat
2024-12-19 12:55:40 +01:00
if not minetest.check_player_privs(player, {interact = true}) then return end
2020-10-26 17:38:53 +01:00
2020-11-11 19:52:54 +01:00
local meta = player and player:get_meta() ; if not meta then return end
2020-10-26 17:38:53 +01:00
meta:set_string("stamina:level", level)
2020-11-11 19:52:54 +01:00
player:hud_change(
stamina.players[player:get_player_name()].hud_id,
"number",
2024-12-19 12:55:40 +01:00
math_min(stamina.VISUAL_MAX, level)
2020-11-11 19:52:54 +01:00
)
2020-10-26 17:38:53 +01:00
end
-- global function for mods to amend stamina level
2024-12-19 12:55:40 +01:00
function stamina.change(player, change)
2020-10-26 17:38:53 +01:00
local name = player:get_player_name()
2024-12-19 12:55:40 +01:00
if not damage_enabled or not name or not change or change == 0 then
2020-10-26 17:38:53 +01:00
return false
end
local level = get_int_attribute(player) + change
2024-12-19 12:55:40 +01:00
level = clamp(level, 0, stamina.VISUAL_MAX)
2020-10-26 17:38:53 +01:00
stamina_update_level(player, level)
return true
end
2024-12-19 12:55:40 +01:00
-- reduce stamina level
2020-10-26 17:38:53 +01:00
local function exhaust_player(player, v)
2024-12-19 12:55:40 +01:00
if not is_player(player) or not player.set_attribute then return end
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
local name = player:get_player_name() ; if not name then return end
2020-10-26 17:38:53 +01:00
local exhaustion = stamina.players[name].exhaustion + v
2024-12-19 12:55:40 +01:00
if exhaustion > stamina.EXHAUST_LVL then
2020-10-26 17:38:53 +01:00
exhaustion = 0
local h = get_int_attribute(player)
2024-12-19 12:55:40 +01:00
if h > 0 then stamina_update_level(player, h - 1) end
2020-10-26 17:38:53 +01:00
end
stamina.players[name].exhaustion = exhaustion
end
2024-12-19 12:55:40 +01:00
-- Sprint settings
2020-10-26 17:38:53 +01:00
2021-01-02 22:12:17 +01:00
local enable_sprint = minetest.settings:get_bool("sprint") ~= false
local enable_sprint_particles = minetest.settings:get_bool("sprint_particles") ~= false
2020-10-26 17:38:53 +01:00
local monoids = minetest.get_modpath("player_monoids")
local pova_mod = minetest.get_modpath("pova")
2024-12-19 12:55:40 +01:00
-- turn sprint on/off
2020-10-26 17:38:53 +01:00
local function set_sprinting(name, sprinting)
2024-12-19 12:55:40 +01:00
local player = minetest.get_player_by_name(name) ; if not player then return end
2020-10-26 17:38:53 +01:00
-- get player physics
local def = player:get_physics_override()
--print ("---", def.speed, def.jump)
if sprinting == true and not stamina.players[name].sprint then
2024-12-19 12:55:40 +01:00
if pova_mod then
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
pova.add_override(name, "sprint", {speed = SPRINT_SPEED, jump = SPRINT_JUMP})
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
pova.do_override(player)
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
stamina.players[name].sprint = true
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
elseif monoids then
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
stamina.players[name].sprint = player_monoids.speed:add_change(
player, def.speed + SPRINT_SPEED)
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
stamina.players[name].jump = player_monoids.jump:add_change(
player, def.jump + SPRINT_JUMP)
2020-10-26 17:38:53 +01:00
else
player:set_physics_override({
speed = def.speed + SPRINT_SPEED,
jump = def.jump + SPRINT_JUMP,
})
stamina.players[name].sprint = true
end
elseif sprinting == false
2024-12-19 12:55:40 +01:00
and stamina.players[name] and stamina.players[name].sprint then
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
if pova_mod then
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
pova.del_override(name, "sprint")
pova.do_override(player)
2020-10-26 17:38:53 +01:00
stamina.players[name].sprint = nil
2024-12-19 12:55:40 +01:00
elseif monoids then
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
player_monoids.speed:del_change(player, stamina.players[name].sprint)
player_monoids.jump:del_change(player, stamina.players[name].jump)
2020-10-26 17:38:53 +01:00
stamina.players[name].sprint = nil
2024-12-19 12:55:40 +01:00
stamina.players[name].jump = nil
2020-10-26 17:38:53 +01:00
else
player:set_physics_override({
speed = def.speed - SPRINT_SPEED,
jump = def.jump - SPRINT_JUMP,
})
stamina.players[name].sprint = nil
end
end
end
2024-12-19 12:55:40 +01:00
-- particle effect when eating
2020-10-26 17:38:53 +01:00
2021-06-27 17:39:50 +02:00
local function head_particle(player, texture)
2024-12-19 12:55:40 +01:00
local prop = player and player:get_properties() ; if not prop then return end
local pos = player:get_pos() ; pos.y = pos.y + (prop.eye_height or 1.23) -- mouth level
2021-06-27 17:39:50 +02:00
local dir = player:get_look_dir()
minetest.add_particlespawner({
amount = 5,
time = 0.1,
minpos = pos,
maxpos = pos,
minvel = {x = dir.x - 1, y = dir.y, z = dir.z - 1},
maxvel = {x = dir.x + 1, y = dir.y, z = dir.z + 1},
minacc = {x = 0, y = -5, z = 0},
maxacc = {x = 0, y = -9, z = 0},
minexptime = 1,
maxexptime = 1,
minsize = 1,
maxsize = 2,
texture = texture
})
end
2024-12-19 12:55:40 +01:00
-- drunk check
2021-02-25 20:49:03 +01:00
local function drunk_tick()
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
for _,player in pairs(minetest.get_connected_players()) do
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
local name = player and player:get_player_name()
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
if name and stamina.players[name] and stamina.players[name].drunk then
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
-- play burp sound every 20 seconds when drunk
local num = stamina.players[name].drunk
2024-12-19 12:55:40 +01:00
if num and num > 0 and math_floor(num / 20) == num / 20 then
2021-06-27 17:39:50 +02:00
head_particle(player, "bubble.png")
2021-02-25 20:49:03 +01:00
minetest.sound_play("stamina_burp",
{to_player = name, gain = 0.7}, true)
end
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
stamina.players[name].drunk = stamina.players[name].drunk - 1
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
if stamina.players[name].drunk < 1 then
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
stamina.players[name].drunk = nil
stamina.players[name].units = 0
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
if not stamina.players[name].poisoned then
2021-01-07 21:52:00 +01:00
2021-02-25 20:49:03 +01:00
player:hud_change(stamina.players[name].hud_id,
"text", "stamina_hud_fg.png")
2021-01-07 21:52:00 +01:00
end
2021-02-25 20:49:03 +01:00
end
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
-- effect only works when not riding boat/cart/horse etc.
if not player:get_attach() then
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
local yaw = player:get_look_horizontal() + math_random(-0.5, 0.5)
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
player:set_look_horizontal(yaw)
end
end
end
end
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
-- health check
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
local function health_tick()
2024-12-19 12:55:40 +01:00
for _,player in pairs(minetest.get_connected_players()) do
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
local name = player and player:get_player_name()
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
if name then
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
local air = player:get_breath() or 0
local hp = player:get_hp()
local h = get_int_attribute(player)
2021-02-25 20:49:03 +01:00
-- damage player by 1 hp if saturation is < 2
2024-12-19 12:55:40 +01:00
if h and h < stamina.STARVE_LVL and hp > 0 then
player:set_hp(hp - stamina.STARVE, {hunger = true})
2020-10-26 17:38:53 +01:00
end
2021-02-25 20:49:03 +01:00
-- don't heal if drowning or dead or poisoned
2024-12-19 12:55:40 +01:00
if h and h >= stamina.HEAL_LVL and h >= hp and hp > 0 and air > 0
2021-02-25 20:49:03 +01:00
and not stamina.players[name].poisoned then
2024-12-19 12:55:40 +01:00
player:set_hp(hp + stamina.HEAL)
2021-02-25 20:49:03 +01:00
stamina_update_level(player, h - 1)
end
2020-10-26 17:38:53 +01:00
end
end
2021-02-25 20:49:03 +01:00
end
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
-- movement check
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
local function action_tick()
2024-12-19 12:55:40 +01:00
for _,player in pairs(minetest.get_connected_players()) do
2021-02-25 20:49:03 +01:00
local controls = player and player:get_player_control()
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
-- Determine if the player is walking or jumping
if controls then
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
if controls.jump then
2024-12-19 12:55:40 +01:00
exhaust_player(player, stamina.EXHAUST_JUMP)
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
elseif controls.up or controls.down or controls.left or controls.right then
exhaust_player(player, stamina.EXHAUST_MOVE)
2021-02-25 20:49:03 +01:00
end
end
--- START sprint
if enable_sprint then
2024-12-19 12:55:40 +01:00
local name = is_player(player) and player:get_player_name()
2021-02-25 20:49:03 +01:00
-- check if player can sprint (stamina must be over 6 points)
if name
and stamina.players[name]
and not stamina.players[name].poisoned
and not stamina.players[name].drunk
and controls and controls.aux1 and controls.up
and not minetest.check_player_privs(player, {fast = true})
and get_int_attribute(player) > 6 then
set_sprinting(name, true)
-- create particles behind player when sprinting
if enable_sprint_particles then
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
local pos = player:get_pos()
local node = minetest.get_node({
x = pos.x,
y = pos.y - 1,
z = pos.z
})
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
if node.name ~= "air" then
minetest.add_particlespawner({
amount = 5,
2024-12-19 12:55:40 +01:00
time = 0.5,
minpos = {x = -0.5, y = 0.1, z = -0.5},
maxpos = {x = 0.5, y = 0.1, z = 0.5},
minvel = {x = 0, y = 5, z = 0},
maxvel = {x = 0, y = 5, z = 0},
minacc = {x = 0, y = -13, z = 0},
maxacc = {x = 0, y = -13, z = 0},
minexptime = 0.1,
maxexptime = 1,
2021-02-25 20:49:03 +01:00
minsize = 0.5,
2024-12-19 12:55:40 +01:00
maxsize = 1.5,
2021-02-25 20:49:03 +01:00
vertical = false,
collisiondetection = false,
2024-12-19 12:55:40 +01:00
attached = player,
2021-02-25 20:49:03 +01:00
texture = "default_dirt.png"
})
end
2020-10-26 17:38:53 +01:00
end
2021-02-25 20:49:03 +01:00
-- Lower the player's stamina when sprinting
local level = get_int_attribute(player)
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
stamina_update_level(player,
2024-12-19 12:55:40 +01:00
level - (SPRINT_DRAIN * stamina.MOVE_TICK))
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
elseif name then
set_sprinting(name, false)
2020-10-26 17:38:53 +01:00
end
end
2021-02-25 20:49:03 +01:00
-- END sprint
2020-10-26 17:38:53 +01:00
end
2021-02-25 20:49:03 +01:00
end
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
-- poisoned check
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
local function poison_tick()
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
for _,player in pairs(minetest.get_connected_players()) do
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
local name = player and player:get_player_name()
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
if name
and stamina.players[name]
and stamina.players[name].poisoned
and stamina.players[name].poisoned > 0 then
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
stamina.players[name].poisoned = stamina.players[name].poisoned - 1
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
local hp = player:get_hp() - 1
2020-10-26 17:38:53 +01:00
2021-06-27 17:39:50 +02:00
head_particle(player, "stamina_poison_particle.png")
2021-02-25 20:49:03 +01:00
if hp > 0 then
player:set_hp(hp, {poison = true})
end
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
elseif name and stamina.players[name] and stamina.players[name].poisoned then
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
if not stamina.players[name].drunk then
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
player:hud_change(stamina.players[name].hud_id,
"text", "stamina_hud_fg.png")
2020-10-26 17:38:53 +01:00
end
2021-02-25 20:49:03 +01:00
stamina.players[name].poisoned = nil
2020-10-26 17:38:53 +01:00
end
end
2021-02-25 20:49:03 +01:00
end
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
-- stamina check
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
local function stamina_tick()
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
for _,player in pairs(minetest.get_connected_players()) do
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
local h = player and get_int_attribute(player)
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
if h and h > stamina.TICK_MIN then
2021-02-25 20:49:03 +01:00
stamina_update_level(player, h - 1)
2020-10-26 17:38:53 +01:00
end
end
2021-02-25 20:49:03 +01:00
end
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
-- Time based stamina functions
2024-12-19 12:55:40 +01:00
2021-02-25 20:49:03 +01:00
local stamina_timer, health_timer, action_timer, poison_timer, drunk_timer = 0,0,0,0,0
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
local function stamina_globaltimer(dtime)
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
stamina_timer = stamina_timer + dtime
health_timer = health_timer + dtime
action_timer = action_timer + dtime
poison_timer = poison_timer + dtime
drunk_timer = drunk_timer + dtime
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
-- simulate drunk walking (thanks LumberJ)
2024-12-19 12:55:40 +01:00
if drunk_timer > 1.0 then drunk_tick() ; drunk_timer = 0 end
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
-- hurt player when poisoned
2024-12-19 12:55:40 +01:00
if poison_timer > stamina.POISON_TICK then poison_tick() ; poison_timer = 0 end
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
-- sprint control and particle animation
2024-12-19 12:55:40 +01:00
if action_timer > stamina.MOVE_TICK then action_tick() ; action_timer = 0 end
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
-- lower saturation by 1 point after stamina.TICK
if stamina_timer > stamina.TICK then stamina_tick() ; stamina_timer = 0 end
2020-10-26 17:38:53 +01:00
2021-02-25 20:49:03 +01:00
-- heal or damage player, depending on saturation
2024-12-19 12:55:40 +01:00
if health_timer > stamina.HEALTH_TICK then health_tick() ; health_timer = 0 end
2020-10-26 17:38:53 +01:00
end
2024-12-19 12:55:40 +01:00
-- stamina and eating functions disabled if damage is disabled
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
if damage_enabled and minetest.settings:get_bool("enable_stamina") ~= false then
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
-- override core.do_item_eat() so we can redirect hp_change to stamina
core.do_item_eat = function(hp_change, replace_with_item, itemstack, user, pointed_thing)
if not is_player(user) then return end -- abort if called by fake player
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
local old_itemstack = itemstack
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
itemstack = stamina.eat(
hp_change, replace_with_item, itemstack, user, pointed_thing)
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
for _, callback in pairs(core.registered_on_item_eats) do
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
local result = callback(hp_change, replace_with_item, itemstack, user,
pointed_thing, old_itemstack)
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
if result then return result end
2020-10-26 17:38:53 +01:00
end
2024-12-19 12:55:40 +01:00
return itemstack
2020-10-26 17:38:53 +01:00
end
2024-12-19 12:55:40 +01:00
-- not local since it's called from within core context
function stamina.eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
if not itemstack or not user then return itemstack end
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
local level = get_int_attribute(user) or 0
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
if level >= stamina.VISUAL_MAX then return itemstack end
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
local name = user:get_player_name()
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
if hp_change > 0 then
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
stamina_update_level(user, level + hp_change)
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
elseif hp_change < 0 then
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
-- assume hp_change < 0
user:hud_change(stamina.players[name].hud_id, "text", "stamina_hud_poison.png")
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
stamina.players[name].poisoned = -hp_change
end
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
-- if {drink=1} group set then use sip sound instead of default eat
local snd, gain = "stamina_eat", 0.7
local itemname = itemstack:get_name()
local def = minetest.registered_items[itemname]
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
if def and def.groups and def.groups.drink then
snd = "stamina_sip" ; gain = 1.0
end
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
minetest.sound_play(snd, {to_player = name, gain = gain}, true)
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
-- particle effect when eating
local texture = minetest.registered_items[itemname].inventory_image
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
texture = texture or minetest.registered_items[itemname].wield_image
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
head_particle(user, texture)
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
-- if player drinks milk then stop poison and being drunk
local item_name = itemstack:get_name() or ""
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
if item_name == "mobs:bucket_milk"
or item_name == "mobs:glass_milk"
or item_name == "farming:soy_milk" then
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
stamina.players[name].poisoned = 0
stamina.players[name].drunk = 0
end
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
itemstack:take_item()
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
if replace_with_item then
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
if itemstack:is_empty() then
itemstack:add_item(replace_with_item)
2020-10-26 17:38:53 +01:00
else
2024-12-19 12:55:40 +01:00
local inv = user:get_inventory()
if inv:room_for_item("main", {name = replace_with_item}) then
inv:add_item("main", replace_with_item)
else
local pos = user:get_pos()
2021-07-24 12:22:10 +02:00
2024-12-19 12:55:40 +01:00
if pos then core.add_item(pos, replace_with_item) end
end
2020-10-26 17:38:53 +01:00
end
end
2024-12-19 12:55:40 +01:00
-- check for alcohol
local units = minetest.registered_items[itemname].groups
and minetest.registered_items[itemname].groups.alcohol or 0
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
if units > 0 then
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
stamina.players[name].units = (stamina.players[name].units or 0) + 1
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
if stamina.players[name].units > 3 then
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
stamina.players[name].drunk = 60
stamina.players[name].units = 0
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
user:hud_change(stamina.players[name].hud_id, "text",
"stamina_hud_poison.png")
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
minetest.chat_send_player(name,
minetest.get_color_escape_sequence("#1eff00")
.. S("You suddenly feel tipsy!"))
end
2020-10-26 17:38:53 +01:00
end
2024-12-19 12:55:40 +01:00
return itemstack
end
2020-10-26 17:38:53 +01:00
minetest.register_on_joinplayer(function(player)
2024-12-19 12:55:40 +01:00
if not player then return end
local level = stamina.VISUAL_MAX -- TODO
2020-10-26 17:38:53 +01:00
if get_int_attribute(player) then
2024-12-19 12:55:40 +01:00
level = math_min(get_int_attribute(player), stamina.VISUAL_MAX)
end
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
local meta = player:get_meta()
2020-10-26 17:38:53 +01:00
2024-12-19 12:55:40 +01:00
if meta then meta:set_string("stamina:level", level) end
2020-10-26 17:38:53 +01:00
local name = player:get_player_name()
2024-12-19 12:55:40 +01:00
local hud_style = minetest.has_feature("hud_def_type_field")
local hud_tab = {
2020-10-26 17:38:53 +01:00
name = "stamina",
position = {x = 0.5, y = 1},
size = {x = 24, y = 24},
text = "stamina_hud_fg.png",
number = level,
alignment = {x = -1, y = -1},
offset = {x = -266, y = -110},
2021-04-05 14:46:21 +02:00
max = 0
2024-12-19 12:55:40 +01:00
}
if hud_style then
hud_tab["type"] = "statbar"
else
hud_tab["hud_elem_type"] = "statbar"
end
local id = player:hud_add(hud_tab)
2020-10-26 17:38:53 +01:00
stamina.players[name] = {
hud_id = id,
exhaustion = 0,
poisoned = nil,
drunk = nil,
2021-04-05 14:46:21 +02:00
sprint = nil
2020-10-26 17:38:53 +01:00
}
end)
minetest.register_on_respawnplayer(function(player)
2024-12-19 12:55:40 +01:00
local name = player and player:get_player_name() ; if not name then return end
2020-10-26 17:38:53 +01:00
if stamina.players[name].poisoned
or stamina.players[name].drunk then
2021-01-07 21:52:00 +01:00
player:hud_change(stamina.players[name].hud_id, "text", "stamina_hud_fg.png")
2020-10-26 17:38:53 +01:00
end
stamina.players[name].exhaustion = 0
stamina.players[name].poisoned = nil
stamina.players[name].drunk = nil
stamina.players[name].sprint = nil
2024-12-19 12:55:40 +01:00
stamina_update_level(player, stamina.VISUAL_MAX)
2020-10-26 17:38:53 +01:00
end)
minetest.register_globalstep(stamina_globaltimer)
minetest.register_on_placenode(function(pos, oldnode, player, ext)
2024-12-19 12:55:40 +01:00
exhaust_player(player, stamina.EXHAUST_PLACE)
2020-10-26 17:38:53 +01:00
end)
minetest.register_on_dignode(function(pos, oldnode, player, ext)
2024-12-19 12:55:40 +01:00
exhaust_player(player, stamina.EXHAUST_DIG)
2020-10-26 17:38:53 +01:00
end)
minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
2024-12-19 12:55:40 +01:00
exhaust_player(player, stamina.EXHAUST_CRAFT)
2020-10-26 17:38:53 +01:00
end)
minetest.register_on_punchplayer(function(player, hitter, time_from_last_punch,
tool_capabilities, dir, damage)
2024-12-19 12:55:40 +01:00
exhaust_player(hitter, stamina.EXHAUST_PUNCH)
2020-10-26 17:38:53 +01:00
end)
2024-12-19 12:55:40 +01:00
else -- create player table on join
2020-10-26 17:38:53 +01:00
minetest.register_on_joinplayer(function(player)
2024-12-19 12:55:40 +01:00
if player then
stamina.players[player:get_player_name()] = {
poisoned = nil, sprint = nil, drunk = nil, exhaustion = 0}
end
2020-10-26 17:38:53 +01:00
end)
end
-- clear when player leaves
2024-12-19 12:55:40 +01:00
2020-10-26 17:38:53 +01:00
minetest.register_on_leaveplayer(function(player)
2024-12-19 12:55:40 +01:00
if player then
stamina.players[player:get_player_name()] = nil
end
2020-10-26 17:38:53 +01:00
end)
2021-06-12 20:20:04 +02:00
2024-12-19 12:55:40 +01:00
-- add lucky blocks (if damage and stamina active)
2021-06-12 20:20:04 +02:00
if minetest.get_modpath("lucky_block")
and minetest.settings:get_bool("enable_damage")
and minetest.settings:get_bool("enable_stamina") ~= false then
2024-12-19 12:55:40 +01:00
local MP = minetest.get_modpath(minetest.get_current_modname())
2021-06-12 20:20:04 +02:00
2024-12-19 12:55:40 +01:00
dofile(MP .. "/lucky_block.lua")
2021-06-12 20:20:04 +02:00
end
2024-12-19 12:55:40 +01:00
print("[MOD] Stamina loaded")