upstream merge sync: fix redundant checks for players, backguard compatibility
* sync commit "added 0.4.x support and fake player check function"
This commit is contained in:
parent
cd4833fae7
commit
3ae96044a6
51
init.lua
51
init.lua
@ -24,7 +24,6 @@ STAMINA_STARVE_LVL = 3 -- level of staturation that causes starving
|
||||
|
||||
STAMINA_VISUAL_MAX = 20 -- hud bar extends only to 20
|
||||
|
||||
local is_50 = minetest.has_feature("object_use_texture_alpha")
|
||||
|
||||
local function clamp(val, minval, maxval)
|
||||
return math.max(math.min(val, maxval), minval)
|
||||
@ -37,37 +36,50 @@ SPRINT_JUMP = clamp(tonumber(minetest.settings:get("stamina_sprint_jump")) or 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)
|
||||
|
||||
-- minetest 5.x check
|
||||
local is_50 = minetest.has_feature("object_use_texture_alpha")
|
||||
|
||||
-- are we a real player ?
|
||||
local function is_player(player)
|
||||
|
||||
local function ck_player(player)
|
||||
-- pipeworks fake player check its not necesary with real player check:
|
||||
if player then if minetest.is_player(player) then return true end end
|
||||
return false
|
||||
|
||||
end
|
||||
|
||||
local function get_int_attribute(player)
|
||||
|
||||
if not ck_player(player) then return nil end
|
||||
if not is_player(player) then
|
||||
return nil
|
||||
end
|
||||
|
||||
local level
|
||||
|
||||
if is_50 then
|
||||
|
||||
local meta = player:get_meta()
|
||||
level = meta and meta:get_string("stamina:level")
|
||||
else
|
||||
level = player and player:get_attribute("stamina:level")
|
||||
end
|
||||
|
||||
if level then return tonumber(level) end -- true its converted to number, even false
|
||||
if level then
|
||||
return tonumber(level)
|
||||
end
|
||||
|
||||
return nil
|
||||
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)
|
||||
|
||||
if not ck_player(player) then return nil end
|
||||
if not is_player(player) then
|
||||
return nil
|
||||
end
|
||||
|
||||
local old = get_int_attribute(player)
|
||||
|
||||
@ -81,7 +93,9 @@ local function stamina_update_level(player, level)
|
||||
end
|
||||
|
||||
if is_50 then
|
||||
|
||||
local meta = player and player:get_meta() ; if not meta then return end
|
||||
|
||||
meta:set_string("stamina:level", level)
|
||||
else
|
||||
player:set_attribute("stamina:level", level)
|
||||
@ -118,9 +132,7 @@ end
|
||||
|
||||
local function exhaust_player(player, v)
|
||||
|
||||
if not player
|
||||
or not ck_player(player) -- player.is_player can return nill in rare cases, and real check of player here
|
||||
or not player.set_attribute then
|
||||
if not is_player(player) then
|
||||
return
|
||||
end
|
||||
|
||||
@ -222,7 +234,7 @@ end
|
||||
|
||||
local function head_particle(player, texture)
|
||||
|
||||
if not ck_player(player) then return end
|
||||
if not player then return end
|
||||
|
||||
local prop = player:get_properties()
|
||||
local pos = player:get_pos() ; pos.y = pos.y + (prop.eye_height or 1.23) -- mouth level
|
||||
@ -250,7 +262,7 @@ local function drunk_tick()
|
||||
|
||||
for _,player in ipairs(minetest.get_connected_players()) do
|
||||
|
||||
if not ck_player(player) then return end -- need cos some rare cases during game running object is not player
|
||||
if not is_player(player) then return end -- need cos some rare cases during game running object is not player
|
||||
|
||||
local name = player:get_player_name()
|
||||
|
||||
@ -299,7 +311,7 @@ local function health_tick()
|
||||
|
||||
for _,player in ipairs(minetest.get_connected_players()) do
|
||||
|
||||
if not ck_player(player) then return end -- need cos some rare cases during game running object is not player
|
||||
if not is_player(player) then return end -- need cos some rare cases during game running object is not player
|
||||
|
||||
local name = player:get_player_name()
|
||||
|
||||
@ -335,7 +347,7 @@ local function action_tick()
|
||||
|
||||
for _,player in ipairs(minetest.get_connected_players()) do
|
||||
|
||||
if not ck_player(player) then return end -- need cos some rare cases during game running object is not player
|
||||
if not is_player(player) then return end -- need cos some rare cases during game running object is not player
|
||||
|
||||
local controls = player:get_player_control()
|
||||
|
||||
@ -356,7 +368,7 @@ local function action_tick()
|
||||
--- START sprint
|
||||
if enable_sprint then
|
||||
|
||||
local name = player and player:get_player_name()
|
||||
local name = player:get_player_name()
|
||||
|
||||
-- check if player can sprint (stamina must be over 6 points)
|
||||
if name
|
||||
@ -421,7 +433,7 @@ local function poison_tick()
|
||||
|
||||
for _,player in ipairs(minetest.get_connected_players()) do
|
||||
|
||||
if not ck_player(player) then return end -- need cos some rare cases during game running object is not player
|
||||
if not is_player(player) then return end -- need cos some rare cases during game running object is not player
|
||||
|
||||
local name = player:get_player_name()
|
||||
|
||||
@ -461,7 +473,7 @@ local function stamina_tick()
|
||||
|
||||
for _,player in ipairs(minetest.get_connected_players()) do
|
||||
|
||||
if not ck_player(player) then return end -- need cos some rare cases during game running object is not player
|
||||
if not is_player(player) then return end -- need cos some rare cases during game running object is not player
|
||||
|
||||
local h = get_int_attribute(player)
|
||||
|
||||
@ -516,7 +528,7 @@ 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 not ck_player(user) then return end -- abort if called by fake player (eg. pipeworks-wielder)
|
||||
if not is_player(user) then return end -- abort if called by fake player (eg. pipeworks-wielder)
|
||||
|
||||
local old_itemstack = itemstack
|
||||
|
||||
@ -641,13 +653,12 @@ if damage_enabled and minetest.settings:get_bool("enable_stamina") ~= false then
|
||||
|
||||
local level = STAMINA_VISUAL_MAX -- TODO
|
||||
|
||||
-- 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
|
||||
|
||||
if is_50 then
|
||||
|
||||
local meta = player:get_meta()
|
||||
if meta then meta:set_string("stamina:level", level) end
|
||||
else
|
||||
|
Loading…
x
Reference in New Issue
Block a user