mods - hudbars+hbarmor+hunger - detection of original mods

* detection of original mods and bypass build-in own features
* optimize loading of hubar for armor, bad initialization
* real check player becouse of wrong implementation at engine
master
mckaygerhard 2023-07-24 14:27:52 -04:00
parent 5e27b5b423
commit 5812e5473c
1 changed files with 178 additions and 138 deletions

View File

@ -65,6 +65,9 @@ hbarmor.autohide = true
hb.settings = {}
hb.hba = minetest.get_modpath("hbarmor")
hb.hbh = minetest.get_modpath("hbhunger")
function hb.load_setting(sname, stype, defaultval, valid_values)
local sval
if stype == "string" then
@ -99,39 +102,43 @@ end
-- Load default settings
dofile(minetest.get_modpath("hudbars").."/default_settings.lua")
-- due lackof global hbhunger these need to be first
hbhunger.get_hunger_raw = function(player)
local inv = player:get_inventory()
if not inv then return nil end
local hgp = inv:get_stack("hunger", 1):get_count()
if hgp == 0 then
hgp = 21
inv:set_stack("hunger", 1, ItemStack({name=":", count=hgp}))
else
hgp = hgp
if not hb.hbh then
-- due lackof global hbhunger these need to be first
hbhunger.get_hunger_raw = function(player)
local inv = player:get_inventory()
if not inv then return nil end
local hgp = inv:get_stack("hunger", 1):get_count()
if hgp == 0 then
hgp = 21
inv:set_stack("hunger", 1, ItemStack({name=":", count=hgp}))
else
hgp = hgp
end
return hgp-1
end
return hgp-1
hbhunger.set_hunger_raw = function(player)
local inv = player:get_inventory()
local name = player:get_player_name()
local value = hbhunger.hunger[name]
if not inv or not value then return nil end
if value > hbhunger.SAT_MAX then value = hbhunger.SAT_MAX end
if value < 0 then value = 0 end
inv:set_stack("hunger", 1, ItemStack({name=":", count=value+1}))
return true
end
-- Load hunger management function for food and mod handling
dofile(minetest.get_modpath("hudbars").."/hunger.lua")
dofile(minetest.get_modpath("hudbars").."/register_foods.lua")
end
hbhunger.set_hunger_raw = function(player)
local inv = player:get_inventory()
local name = player:get_player_name()
local value = hbhunger.hunger[name]
if not inv or not value then return nil end
if value > hbhunger.SAT_MAX then value = hbhunger.SAT_MAX end
if value < 0 then value = 0 end
inv:set_stack("hunger", 1, ItemStack({name=":", count=value+1}))
return true
end
-- Load hunger management function for food and mod handling
dofile(minetest.get_modpath("hudbars").."/hunger.lua")
dofile(minetest.get_modpath("hudbars").."/register_foods.lua")
local function player_exists(player)
return player ~= nil and minetest.is_player(player)
return player ~= nil and (type(player) == "userdata")
end
local must_hide = function(playername, arm)
@ -600,8 +607,12 @@ if minetest.settings:get_bool("enable_damage") or hb.settings.forceload_default_
end
hb.register_hudbar("health", 0xFFFFFF, S("Health"), { bar = "hudbars_bar_health.png", icon = hicon, bgicon = nil }, hb.settings.hp_player_maximun, hb.settings.hp_player_maximun, false)
hb.register_hudbar("breath", 0xFFFFFF, S("Breath"), { bar = "hudbars_bar_breath.png", icon = bicon, bgicon = nil }, hb.settings.br_player_maximun, hb.settings.br_player_maximun, false)
hb.register_hudbar("armor", 0xFFFFFF, S("Armor"), { bar = "hbarmor_bar.png", icon = aicon, bgicon = nil }, 0, 100, hbarmor.autohide, N("@1: @2%"), { order = { "label", "value" } } )
hb.register_hudbar("satiation", 0xFFFFFF, S("Satiation"), { bar = "hbhunger_bar.png", icon = sicon, bgicon = nil }, hbhunger.SAT_INIT, hbhunger.SAT_MAX, false, nil, { format_value = "%02d", format_max_value = "%02d" })
if not hb.hba then
hb.register_hudbar("armor", 0xFFFFFF, S("Armor"), { bar = "hbarmor_bar.png", icon = aicon, bgicon = nil }, 0, 100, hbarmor.autohide, N("@1: @2%"), { order = { "label", "value" } } )
end
if not hb.hbh then
hb.register_hudbar("satiation", 0xFFFFFF, S("Satiation"), { bar = "hbhunger_bar.png", icon = sicon, bgicon = nil }, hbhunger.SAT_INIT, hbhunger.SAT_MAX, false, nil, { format_value = "%02d", format_max_value = "%02d" })
end
end
local function hide_builtin(player)
@ -611,71 +622,66 @@ local function hide_builtin(player)
player:hud_set_flags(flags)
end
function hbarmor.get_armor(player)
if not player or not armor.def then
return false
end
local name = player:get_player_name()
local def = armor.def[name] or nil
if def and def.state and def.count then
hbarmor.set_armor(name, def.state, def.count)
else
minetest.log("error", "[hudbars] Call to hbarmor.get_armor returned with false!")
return false
end
return true
end
if not hb.hba then
function hbarmor.set_armor(player_name, ges_state, items)
local max_items = 4
if items == 5 then max_items = items end
local max = max_items * 65535
local lvl = max - ges_state
lvl = lvl/max
if ges_state == 0 and items == 0 then lvl = 0 end
function hbarmor.get_armor(player)
if not player or not armor.def then
return false
end
local name = player:get_player_name()
local def = armor.def[name] or nil
if def and def.state and def.count then
hbarmor.set_armor(name, def.state, def.count)
else
minetest.log("error", "[hudbars] Call to hbarmor.get_armor returned with false!")
return false
end
return true
end
function hbarmor.set_armor(player_name, ges_state, items)
local max_items = 4
if items == 5 then max_items = items end
local max = max_items * 65535
local lvl = max - ges_state
lvl = lvl/max
if ges_state == 0 and items == 0 then lvl = 0 end
hbarmor.armor[player_name] = math.max(0, math.min(lvl* (items * (100 / max_items)), 100))
end
hbarmor.armor[player_name] = math.max(0, math.min(lvl* (items * (100 / max_items)), 100))
end
local function custom_hud(player)
local name = player:get_player_name()
if minetest.settings:get_bool("enable_damage") or hb.settings.forceload_default_hudbars then
local arm = tonumber(hbarmor.armor[name])
if not arm then arm = 0 end
local hbhide
if minetest.settings:get_bool("enable_damage") then
hbhide = false
else
hbhide = true
end
local hahide
if hbarmor.autohide then
hahide = must_hide(name, arm)
else
hahide = false
end
local sahide = true
local hp = player:get_hp()
local hp_max = hb.settings.hp_player_maximun
local hide_hp = not minetest.settings:get_bool("enable_damage") or true
if player:get_properties().hp_max then player:set_properties({hp_max = hb.settings.hp_player_maximun}) end
hb.init_hudbar(player, "health", math.min(hp, hp_max), hp_max, hbhide)
hb.init_hudbar(player, "health", math.min(hp, hp_max), hp_max, hide_hp)
local breath = player:get_breath()
local breath_max = hb.settings.br_player_maximun
local hide_breath
if player:get_properties().breath_max then player:set_properties({breath_max = hb.settings.br_player_maximun}) end
if breath >= breath_max and hb.settings.autohide_breath == true then hide_breath = true else hide_breath = false end
hb.init_hudbar(player, "breath", math.min(breath, breath_max), breath_max, hide_breath or hbhide)
hb.init_hudbar(player, "breath", math.min(breath, breath_max), breath_max, hide_breath)
hbarmor.get_armor(player)
arm = tonumber(hbarmor.armor[name])
hb.init_hudbar(player, "armor", arm_printable(arm), 100, hahide)
if hb.settings.forceload_default_hudbars then sahide = false end
hb.init_hudbar(player, "satiation", hbhunger.get_hunger_raw(player), nil, sahide)
if not hb.hba then
local arm = tonumber(hbarmor.armor[name])
if not arm then arm = 0 end
local hide_ar = hbarmor.autohide and must_hide(name, arm)
hbarmor.get_armor(player)
arm = tonumber(hbarmor.armor[name])
hb.init_hudbar(player, "armor", arm_printable(arm), 100, hide_ar)
end
if not hb.hbh then
hb.init_hudbar(player, "satiation", hbhunger.get_hunger_raw(player), nil, (not hb.settings.forceload_default_hudbars) )
end
end
end
@ -688,15 +694,9 @@ end
-- update built-in HUD bars
local function update_hud(player)
local lplayer = player_exists(player)
if not lplayer then return end
local name = player:get_player_name()
local name = player:get_player_name() -- player checks are already made before call this function
if not name then return end
local larmor = hbarmor.armor[name]
if not larmor then return end
local arm = tonumber(larmor)
if not arm then arm = 0; hbarmor.armor[name] = 0 end
-- loading only if need or force loading
if minetest.settings:get_bool("enable_damage") then
if hb.settings.forceload_default_hudbars then
@ -714,23 +714,30 @@ local function update_hud(player)
--health
update_health(player)
-- armor
if hbarmor.autohide then
if must_hide(name, arm) then
hb.hide_hudbar(player, "armor")
if not hb.hba then
local larmor = hbarmor.armor[name]
local arm = tonumber(larmor)
if not arm then arm = 0; hbarmor.armor[name] = 0 end
if hbarmor.autohide then
if must_hide(name, arm) then
hb.hide_hudbar(player, "armor")
else
hb.change_hudbar(player, "armor", arm_printable(arm))
hb.unhide_hudbar(player, "armor")
end
else
hb.change_hudbar(player, "armor", arm_printable(arm))
hb.unhide_hudbar(player, "armor")
end
else
hb.change_hudbar(player, "armor", arm_printable(arm))
hb.unhide_hudbar(player, "armor")
end
-- hunger
local h_out = tonumber(hbhunger.hunger_out[name])
local h = tonumber(hbhunger.hunger[name])
if h_out ~= h then
hbhunger.hunger_out[name] = h
hb.change_hudbar(player, "satiation", h)
if not hb.hbh then
local h_out = tonumber(hbhunger.hunger_out[name])
local h = tonumber(hbhunger.hunger[name])
if h_out ~= h then
hbhunger.hunger_out[name] = h
hb.change_hudbar(player, "satiation", h)
end
end
elseif hb.settings.forceload_default_hudbars then
update_health(player)
@ -748,35 +755,59 @@ minetest.register_on_player_hpchange(function(player)
end)
minetest.register_on_respawnplayer(function(player)
update_health(player)
if not player_exists(player) then return end
local name = player:get_player_name()
hbhunger.hunger[name] = hbhunger.SAT_INIT
hbhunger.set_hunger_raw(player)
hbhunger.exhaustion[name] = 0
update_health(player)
if not hb.hbh then
hbhunger.hunger[name] = hbhunger.SAT_INIT
hbhunger.set_hunger_raw(player)
hbhunger.exhaustion[name] = 0
end
hb.hide_hudbar(player, "breath")
end)
minetest.register_on_joinplayer(function(player)
if not player_exists(player) then return end
local name = player:get_player_name()
local inv = player:get_inventory()
inv:set_size("hunger",1)
hbhunger.hunger[name] = hbhunger.get_hunger_raw(player)
hbhunger.hunger_out[name] = hbhunger.hunger[name]
hbhunger.exhaustion[name] = 0
hbhunger.poisonings[name] = 0
if not hb.hbh then
inv:set_size("hunger",1)
hbhunger.hunger[name] = hbhunger.get_hunger_raw(player)
hbhunger.hunger_out[name] = hbhunger.hunger[name]
hbhunger.exhaustion[name] = 0
hbhunger.poisonings[name] = 0
end
hide_builtin(player)
custom_hud(player)
hb.players[name] = player
hbarmor.player_active[name] = true
hbhunger.set_hunger_raw(player)
if not hb.hba then
hbarmor.player_active[name] = true
end
if not hb.hbh then
hbhunger.set_hunger_raw(player)
end
end)
minetest.register_on_leaveplayer(function(player)
if not player_exists(player) then return end
local name = player:get_player_name()
hb.players[name] = nil
hbarmor.player_active[name] = false
if not hb.hba then
hbarmor.player_active[name] = false
end
end)
local modresult = ""
if hb.hba then modresult = modresult .. " without build-in hbarmor" end
if hb.hbh then modresult = modresult .. " without build-in hbhunger" end
minetest.log("[MOD] hudbars"..modresult.." loaded" )
local main_timer = 0
local timer = 0
local timer2 = 0
@ -793,44 +824,53 @@ minetest.register_globalstep(function(dtime)
for _, player in pairs(hb.players) do
update_hud(player)
end
-- update all hud elements
for _,player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
if hbarmor.player_active[name] == true then
hbarmor.get_armor(player)
update_hud(player)
if not hb.hba then
-- update all hud elements
for _,player in ipairs(minetest.get_connected_players()) do
if player_exists(player) then
local name = player:get_player_name()
if hbarmor.player_active[name] == true then
hbarmor.get_armor(player)
update_hud(player)
end
end
end
end
end
end
-- satiaton are internal properties, so update live (not hb) player properties
if timer > 4 or timer2 > hbhunger.HUNGER_TICK then
for _,player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name()
local h = tonumber(hbhunger.hunger[name])
local hp = player:get_hp()
if timer > 4 then
if h > hbhunger.SAT_HEAL and hp > 0 and player:get_breath() > 0 then
player:set_hp(hp+1) -- heal player by 1 hp if not dead and satiation is > hbhunger.SAT_HEAL
elseif h <= 1 then
if hp-1 >= 0 then player:set_hp(hp-1) end -- or damage player by 1 hp if satiation is < 2
if not hb.hbh then
-- satiaton are internal properties, so update live (not hb) player properties
if timer > 4 or timer2 > hbhunger.HUNGER_TICK then
for _,player in ipairs(minetest.get_connected_players()) do
if player_exists(player) then
local name = player:get_player_name()
local h = tonumber(hbhunger.hunger[name])
local hp = player:get_hp()
if timer > 4 then
if h > hbhunger.SAT_HEAL and hp > 0 and player:get_breath() > 0 then
player:set_hp(hp+1) -- heal player by 1 hp if not dead and satiation is > hbhunger.SAT_HEAL
elseif h <= 1 then
if hp-1 >= 0 then player:set_hp(hp-1) end -- or damage player by 1 hp if satiation is < 2
end
end
if timer2 > hbhunger.HUNGER_TICK then
if h > 0 then
h = h-1 -- lower satiation by 1 point after xx seconds
hbhunger.hunger[name] = h
hbhunger.set_hunger_raw(player)
end
end
-- still do not update all hud elements cos we have 2 loops
local controls = player:get_player_control()
if controls.up or controls.down or controls.left or controls.right then
hbhunger.handle_node_actions(nil, nil, player) -- Determine if the player is walking
end
end
end
if timer2 > hbhunger.HUNGER_TICK then
if h > 0 then
h = h-1 -- lower satiation by 1 point after xx seconds
hbhunger.hunger[name] = h
hbhunger.set_hunger_raw(player)
end
end
-- still do not update all hud elements cos we have 2 loops
local controls = player:get_player_control()
if controls.up or controls.down or controls.left or controls.right then
hbhunger.handle_node_actions(nil, nil, player) -- Determine if the player is walking
end
end
end
if timer > 4 then timer = 0 end
if timer2 > hbhunger.HUNGER_TICK then timer2 = 0 end
end)