sync player nil checks; add settings for stamina speed, jump and drain

* add settings for stamina speed, jump and drain
* Merge branch 'master' of https://notabug.org/TenPlus1/stamina into main-sync
* Fix https://notabug.org/TenPlus1/stamina/issues/14 extra end at line 16
* property dont update hud/hunger if damage not enabled
* added more nil player checks due constant loop codes inside functions
* sync license and replace sip sound with licensed one
This commit is contained in:
mckaygerhard 2023-07-23 18:37:50 -04:00
commit 41bd87405d
7 changed files with 447 additions and 398 deletions

View File

@ -55,6 +55,7 @@ TenPlus1 Additions:
- Added Pipeworks checks for fake players
- Added 60 second drunk effect when foods have {alcohol=1} group (eat 4 or more)
- Moved exhaustion and hud_id to player table instead of player attributes
- Added 4 lucky block effects
License:

View File

@ -2,3 +2,4 @@ default
3d_armor?
player_monoids?
pova?
lucky_block?

132
init.lua
View File

@ -24,9 +24,17 @@ STAMINA_STARVE_LVL = 3 -- level of staturation that causes starving
STAMINA_VISUAL_MAX = 20 -- hud bar extends only to 20
SPRINT_SPEED = 0.3 -- 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 clamp(val, minval, maxval)
return math.max(math.min(val, maxval), minval)
end
-- how much faster players can run if satiated.
SPRINT_SPEED = clamp(tonumber(minetest.settings:get("stamina_sprint_speed")) or 0.3, 0.0, 1.0)
-- how much higher player can jump if satiated
SPRINT_JUMP = clamp(tonumber(minetest.settings:get("stamina_sprint_jump")) or 0.1, 0.0, 1.0)
-- how fast to drain satation while sprinting (0-1)
SPRINT_DRAIN = clamp(tonumber(minetest.settings:get("stamina_sprint_drain")) or 0.35, 0.0, 1.0)
local function get_int_attribute(player)
@ -48,6 +56,8 @@ end
local stamina_enabled = minetest.settings:get_bool("enable_stamina") ~= false
local damage_enabled = minetest.settings:get_bool("enable_damage")
local function stamina_update_level(player, level)
@ -84,7 +94,7 @@ stamina.change = function(player, change)
local name = player:get_player_name()
if not name or not change or change == 0 then
if not damage_enabled or not name or not change or change == 0 then
return false
end
@ -141,13 +151,12 @@ local pova_mod = minetest.get_modpath("pova")
local function set_sprinting(name, sprinting)
local player = minetest.get_player_by_name(name)
local player = minetest.get_player_by_name(name) ; if not player then return end
if player then
-- get player physics
local def = player:get_physics_override()
--print ("---", def.speed, def.jump)
--print ("---", def.speed, def.jump)
if sprinting == true and not stamina.players[name].sprint then
@ -203,13 +212,13 @@ local function set_sprinting(name, sprinting)
stamina.players[name].sprint = nil
end
end
end
end
local function head_particle(player, texture)
if player then
if not player then return end
local prop = player:get_properties()
local eye_height = prop.eye_height or 1.23
local pos = player:get_pos() ; pos.y = pos.y + eye_height -- mouth level
@ -231,14 +240,13 @@ local function head_particle(player, texture)
maxsize = 2,
texture = texture
})
end
end
local function drunk_tick()
for _,player in ipairs(minetest.get_connected_players()) do
if player then
if not player then return end
local name = player:get_player_name()
@ -279,9 +287,6 @@ local function drunk_tick()
player:set_look_horizontal(yaw)
end
end
end
end
end
@ -290,14 +295,15 @@ local function health_tick()
for _,player in ipairs(minetest.get_connected_players()) do
if player then
if not player then return end
local name = player:get_player_name()
if name then
local air = player:get_breath() or 0
local hp = player:get_hp()
local h = get_int_attribute(player)
local name = player:get_player_name()
if name then
-- damage player by 1 hp if saturation is < 2
if h and h < STAMINA_STARVE_LVL
@ -318,7 +324,6 @@ local function health_tick()
end
end
end
end
end
@ -326,9 +331,8 @@ local function action_tick()
for _,player in ipairs(minetest.get_connected_players()) do
if player then
local controls = player and player:get_player_control()
if not player then return end
local controls = player:get_player_control()
-- Determine if the player is walking or jumping
if controls then
@ -347,7 +351,8 @@ local function action_tick()
--- START sprint
if enable_sprint then
local name = player and player:get_player_name()
if not player then return end
local name = player:get_player_name()
-- check if player can sprint (stamina must be over 6 points)
if name
@ -405,7 +410,6 @@ local function action_tick()
end
-- END sprint
end
end
end
@ -413,7 +417,7 @@ local function poison_tick()
for _,player in ipairs(minetest.get_connected_players()) do
if player then
if not player then return end
local name = player:get_player_name()
@ -446,7 +450,6 @@ local function poison_tick()
stamina.players[name].poisoned = nil
end
end
end
end
@ -454,16 +457,13 @@ local function stamina_tick()
for _,player in ipairs(minetest.get_connected_players()) do
if player then
if not player then return end
local h = get_int_attribute(player)
if h and h > STAMINA_TICK_MIN then
stamina_update_level(player, h - 1)
end
end
end
end
@ -506,8 +506,11 @@ local function stamina_globaltimer(dtime)
end
-- 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)
-- stamina and eating functions disabled if damage is disabled
if damage_enabled and minetest.settings:get_bool("enable_stamina") ~= false then
-- 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 user.is_fake_player then
return -- abort if called by fake player (eg. pipeworks-wielder)
@ -529,11 +532,10 @@ core.do_item_eat = function(hp_change, replace_with_item, itemstack, user, point
end
return itemstack
end
end
-- not local since it's called from within core context
function stamina.eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
-- not local since it's called from within core context
function stamina.eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
if not itemstack or not user then
return itemstack
@ -573,6 +575,8 @@ function stamina.eat(hp_change, replace_with_item, itemstack, user, pointed_thin
-- particle effect when eating
local texture = minetest.registered_items[itemname].inventory_image
texture = texture or minetest.registered_items[itemname].wield_image
head_particle(user, texture)
-- if player drinks milk then stop poison and being drunk
@ -627,18 +631,14 @@ function stamina.eat(hp_change, replace_with_item, itemstack, user, pointed_thin
end
return itemstack
end
-- stamina is disabled if damage is disabled
if minetest.settings:get_bool("enable_damage")
and minetest.settings:get_bool("enable_stamina") ~= false then
end
minetest.register_on_joinplayer(function(player)
if not player then return end
local level = STAMINA_VISUAL_MAX -- TODO
if player then
if get_int_attribute(player) then
level = math.min(get_int_attribute(player), STAMINA_VISUAL_MAX)
@ -669,13 +669,11 @@ and minetest.settings:get_bool("enable_stamina") ~= false then
drunk = nil,
sprint = nil
}
end
end)
minetest.register_on_respawnplayer(function(player)
if player then
local name = player:get_player_name()
local name = player:get_player_name() ; if not name then return end
if stamina.players[name].poisoned
or stamina.players[name].drunk then
@ -688,7 +686,6 @@ and minetest.settings:get_bool("enable_stamina") ~= false then
stamina.players[name].sprint = nil
stamina_update_level(player, STAMINA_VISUAL_MAX)
end
end)
minetest.register_globalstep(stamina_globaltimer)
@ -729,44 +726,15 @@ minetest.register_on_leaveplayer(function(player)
end)
--lucky blocks (if damage and stamina active)
-- add lucky blocks (if damage and stamina active)
if minetest.get_modpath("lucky_block")
and minetest.settings:get_bool("enable_damage")
and minetest.settings:get_bool("enable_stamina") ~= false then
local effect_me = function(pos, player, def)
local MP = minetest.get_modpath(minetest.get_current_modname())
local green = minetest.get_color_escape_sequence("#bada55")
if player then
local name = player:get_player_name()
if def.poison or def.drunk then
player:hud_change(stamina.players[name].hud_id,
"text", "stamina_hud_poison.png")
end
if def.poison and def.poison > 0 then
stamina.players[name].poisoned = def.poison
minetest.chat_send_player(name,
green .. "Seems you have been poisoned!")
elseif def.drunk and def.drunk > 0 then
stamina.players[name].drunk = def.drunk
minetest.chat_send_player(name,
green .. "You seem a little tipsy!")
end
end
end
lucky_block:add_blocks({
{"cus", effect_me, {poison = 5} },
{"cus", effect_me, {poison = 10} },
{"cus", effect_me, {drunk = 30} },
})
dofile(MP .. "/lucky_block.lua")
end
print("[MOD] Stamina loaded")

28
license.txt Normal file
View File

@ -0,0 +1,28 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 2.1, February 1999
Copyright (C) 1991, 1999 Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
[This is the first released version of the Lesser GPL. It also counts
as the successor of the GNU Library Public License, version 2, hence
the version number 2.1.]
Code:
- all code LGPL-2.1+
Textures:
- stamina_hud_poison.png - BlockMen (CC-BY 3.0)
- stamina_hud_fg.png - PilzAdam (WTFPL), modified by BlockMen
- stamina_hud_bg.png - PilzAdam (WTFPL), modified by BlockMen
Sounds (CC-BY-3.0):
- stamina_eat.*.ogg - http://www.freesound.org/people/sonictechtonic/sounds/242215/
- stamina_burp.ogg - https://www.freesfx.co.uk/sfx/burp (small burp)
Sounds (Attribution 3.0):
- stamina_sip.ogg - https://freesound.org/people/Stevious42/sounds/259640/

45
lucky_block.lua Normal file
View File

@ -0,0 +1,45 @@
local green = minetest.get_color_escape_sequence("#bada55")
local green2 = minetest.get_color_escape_sequence("#33ff55")
-- poison and drunk effects function
local effect_me = function(pos, player, def)
local name = player:get_player_name() ; if not name then return end
if def.poison or def.drunk then
player:hud_change(stamina.players[name].hud_id, "text", "stamina_hud_poison.png")
end
if def.poison and def.poison > 0 then
stamina.players[name].poisoned = def.poison
minetest.chat_send_player(name, green .. "Seems you have been poisoned!")
elseif def.drunk and def.drunk > 0 then
stamina.players[name].drunk = def.drunk
minetest.chat_send_player(name, green .. "You seem a little tipsy!")
end
-- restore stamina function
local full_stamina = function(pos, player, def)
local name = player:get_player_name() ; if not name then return end
stamina.change(player, 100) -- set to 100 incase of default stamina increase
minetest.chat_send_player(name, green2 .. "You suddenly feel full!")
end
-- add lucky blocks
lucky_block:add_blocks({
{"cus", effect_me, {poison = 5}},
{"cus", effect_me, {poison = 10}},
{"cus", effect_me, {drunk = 30}},
{"cus", full_stamina}
})

View File

@ -5,3 +5,9 @@ sprint_particles (Enable sprint particles) bool true
enable_stamina (Enable stamina/hunger) bool true
stamina_tick (Time in seconds after which 1 saturation point is taken) float 800
stamina_sprint_speed (Extra sprint speed - 0.0 to 1.0) float 0.3
stamina_sprint_jump (Extra sprint jump height - 0.0 to 1.0) float 0.1
stamina_sprint_drain (Sprint stamina drain - 0.0 to 1.0) float 0.35

Binary file not shown.