From a39c32d1d00b41d120745708535ab0e23e00ae48 Mon Sep 17 00:00:00 2001 From: mckaygerhard Date: Sun, 23 Jul 2023 21:01:21 -0400 Subject: [PATCH] sync code with MR and comment on performance issues * do not check fake players in callbacks due performance impack but made proper player checks on each loop cos each loop retrieve players at live so a suddent disconected player may happened or craker player with invalid object that property check at `ck_player(obj)` * added comments about this, this commit tries to sync the MR at https://notabug.org/TenPlus1/stamina/pulls/16 until (as always) tenplus1 commit their own solution --- init.lua | 27 ++++++++++++--------------- settingtypes.txt | 4 ++-- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/init.lua b/init.lua index b70c898..8ccd5fc 100644 --- a/init.lua +++ b/init.lua @@ -67,7 +67,7 @@ local damage_enabled = minetest.settings:get_bool("enable_damage") local function stamina_update_level(player, level) - if not ck_player(player) then return end + if not ck_player(player) then return nil end local old = get_int_attribute(player) @@ -156,7 +156,7 @@ local pova_mod = minetest.get_modpath("pova") local function set_sprinting(name, sprinting) - local player = minetest.get_player_by_name(name) ; if not ck_player(player) then return end + local player = minetest.get_player_by_name(name) ; if not player then return end -- get player physics local def = player:get_physics_override() @@ -225,8 +225,7 @@ local function head_particle(player, texture) if not ck_player(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 + local pos = player:get_pos() ; pos.y = pos.y + (prop.eye_height or 1.23) -- mouth level local dir = player:get_look_dir() @@ -251,7 +250,7 @@ local function drunk_tick() for _,player in ipairs(minetest.get_connected_players()) do - if not ck_player(player) then return end + if not ck_player(player) then return end -- need cos some rare cases during game running object is not player local name = player:get_player_name() @@ -300,7 +299,7 @@ local function health_tick() for _,player in ipairs(minetest.get_connected_players()) do - if not ck_player(player) then return end + if not ck_player(player) then return end -- need cos some rare cases during game running object is not player local name = player:get_player_name() @@ -312,7 +311,7 @@ local function health_tick() -- damage player by 1 hp if saturation is < 2 if h and h < STAMINA_STARVE_LVL - and hp > 0 then + and hp > 1 then player:set_hp(hp - STAMINA_STARVE, {hunger = true}) end @@ -336,7 +335,8 @@ local function action_tick() for _,player in ipairs(minetest.get_connected_players()) do - if not ck_player(player) then return end + if not ck_player(player) then return end -- need cos some rare cases during game running object is not player + local controls = player:get_player_control() -- Determine if the player is walking or jumping @@ -356,8 +356,7 @@ local function action_tick() --- START sprint if enable_sprint then - if not ck_player(player) then return end - local name = player:get_player_name() + local name = player and player:get_player_name() -- check if player can sprint (stamina must be over 6 points) if name @@ -422,7 +421,7 @@ local function poison_tick() for _,player in ipairs(minetest.get_connected_players()) do - if not ck_player(player) then return end + if not ck_player(player) then return end -- need cos some rare cases during game running object is not player local name = player:get_player_name() @@ -462,7 +461,7 @@ local function stamina_tick() for _,player in ipairs(minetest.get_connected_players()) do - if not ck_player(player) then return end + if not ck_player(player) then return end -- need cos some rare cases during game running object is not player local h = get_int_attribute(player) @@ -644,6 +643,7 @@ if damage_enabled and minetest.settings:get_bool("enable_stamina") ~= false then -- the code here was redundant cos get_int_attribute already call the level -- but with backguard compat, tenplus1 just inherint the code but with removed backguard compatibility + -- also here we do not check if name its valid, object its already valid if get_int_attribute(player) then level = math.min(get_int_attribute(player), STAMINA_VISUAL_MAX) end @@ -656,8 +656,6 @@ if damage_enabled and minetest.settings:get_bool("enable_stamina") ~= false then local name = player:get_player_name() - if not name then return end - local id = player:hud_add({ name = "stamina", hud_elem_type = "statbar", @@ -700,7 +698,6 @@ if damage_enabled and minetest.settings:get_bool("enable_stamina") ~= false then minetest.register_globalstep(stamina_globaltimer) minetest.register_on_placenode(function(pos, oldnode, player, ext) - if not player then return end exhaust_player(player, STAMINA_EXHAUST_PLACE) end) diff --git a/settingtypes.txt b/settingtypes.txt index 467b8a1..3e61d97 100644 --- a/settingtypes.txt +++ b/settingtypes.txt @@ -4,10 +4,10 @@ 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_tick (Time in seconds after which 1 saturation point is taken) float 1600 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 +stamina_sprint_drain (Sprint stamina drain - 0.0 to 1.0) float 0.2