Tweaked and tidied code, added error checking
parent
89fba7a826
commit
a5ae31fe3c
113
hbarmor/init.lua
113
hbarmor/init.lua
|
@ -1,19 +1,11 @@
|
|||
|
||||
hbarmor = {}
|
||||
hbarmor.armor = {} -- HUD statbar values
|
||||
hbarmor.player_active = {} -- stores if player hug has been init. so far
|
||||
hbarmor.tick = 1 -- was 0.1
|
||||
hbarmor.autohide = true -- hide when player not wearing armor
|
||||
|
||||
-- HUD statbar values
|
||||
hbarmor.armor = {}
|
||||
|
||||
-- Stores if player's HUD bar has been initialized so far.
|
||||
hbarmor.player_active = {}
|
||||
|
||||
-- HUD item ids
|
||||
local armor_hud = {}
|
||||
|
||||
hbarmor.tick = 1 -- 0.1
|
||||
|
||||
-- If true, the armor bar is hidden when the player does not wear any armor
|
||||
hbarmor.autohide = true
|
||||
|
||||
local armor_hud = {} -- HUD item id's
|
||||
local enable_damage = minetest.setting_getbool("enable_damage")
|
||||
|
||||
--[[load custom settings
|
||||
|
@ -32,62 +24,78 @@ local arm_printable = function(arm)
|
|||
end
|
||||
|
||||
local function custom_hud(player)
|
||||
|
||||
local name = player:get_player_name()
|
||||
|
||||
if enable_damage then
|
||||
local ret = hbarmor.get_armor(player)
|
||||
if ret == false then
|
||||
minetest.log("error", "[hbarmor] Call to hbarmor.get_armor in custom_hud returned with false!")
|
||||
end
|
||||
local arm = tonumber(hbarmor.armor[name])
|
||||
if not arm then arm = 0 end
|
||||
local hide
|
||||
if hbarmor.autohide then
|
||||
hide = must_hide(name, arm)
|
||||
else
|
||||
hide = false
|
||||
end
|
||||
hb.init_hudbar(player, "armor", arm_printable(arm), nil, hide)
|
||||
if not enable_damage then
|
||||
return
|
||||
end
|
||||
|
||||
if hbarmor.get_armor(player) == false then
|
||||
|
||||
minetest.log("error",
|
||||
"[hbarmor] Call to hbarmor.get_armor in custom_hud returned with false!")
|
||||
end
|
||||
|
||||
local arm = tonumber(hbarmor.armor[name]) or 0
|
||||
|
||||
local hide = false
|
||||
|
||||
if hbarmor.autohide then
|
||||
hide = must_hide(name, arm)
|
||||
end
|
||||
|
||||
hb.init_hudbar(player, "armor", arm_printable(arm), nil, hide)
|
||||
end
|
||||
|
||||
--register and define armor HUD bar
|
||||
hb.register_hudbar("armor", 0xFFFFFF, "Armor",
|
||||
{ icon = "hbarmor_icon.png", bar = "hbarmor_bar.png" }, 0, 100, hbarmor.autohide, "%s: %d%%")
|
||||
|
||||
--dofile(minetest.get_modpath("hbarmor").."/armor.lua")
|
||||
-- START armor.lua file
|
||||
|
||||
minetest.after(0, function()
|
||||
|
||||
if not armor.def then
|
||||
|
||||
minetest.after(2,minetest.chat_send_all,
|
||||
"#Better HUD: Please update your version of 3darmor")
|
||||
|
||||
HUD_SHOW_ARMOR = false
|
||||
end
|
||||
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
|
||||
return false
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
function hbarmor.set_armor(player_name, ges_state, items)
|
||||
|
||||
local max_items = 4
|
||||
if items == 5 then
|
||||
|
||||
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
|
||||
|
@ -96,20 +104,27 @@ function hbarmor.set_armor(player_name, ges_state, items)
|
|||
|
||||
end
|
||||
|
||||
-- END armor.lua
|
||||
|
||||
-- update hud elemtens if value has changed
|
||||
local function update_hud(player)
|
||||
|
||||
if not player then
|
||||
return
|
||||
end
|
||||
|
||||
local name = player:get_player_name()
|
||||
--armor
|
||||
local arm = tonumber(hbarmor.armor[name])
|
||||
|
||||
if not arm then
|
||||
arm = 0
|
||||
hbarmor.armor[name] = 0
|
||||
end
|
||||
|
||||
if hbarmor.autohide then
|
||||
|
||||
-- hide armor bar completely when there is none
|
||||
if must_hide(name, arm) then
|
||||
|
||||
hb.hide_hudbar(player, "armor")
|
||||
else
|
||||
hb.change_hudbar(player, "armor", arm_printable(arm))
|
||||
|
@ -121,36 +136,50 @@ local function update_hud(player)
|
|||
end
|
||||
|
||||
minetest.register_on_joinplayer(function(player)
|
||||
|
||||
local name = player:get_player_name()
|
||||
|
||||
custom_hud(player)
|
||||
|
||||
hbarmor.player_active[name] = true
|
||||
end)
|
||||
|
||||
minetest.register_on_leaveplayer(function(player)
|
||||
|
||||
local name = player:get_player_name()
|
||||
|
||||
hbarmor.player_active[name] = false
|
||||
end)
|
||||
|
||||
local main_timer = 0
|
||||
--local timer = 0
|
||||
|
||||
minetest.register_globalstep(function(dtime)
|
||||
|
||||
main_timer = main_timer + dtime
|
||||
-- timer = timer + dtime
|
||||
if main_timer > hbarmor.tick then -- or timer > 4 then
|
||||
|
||||
if main_timer > hbarmor.tick then
|
||||
|
||||
if enable_damage then
|
||||
if main_timer > hbarmor.tick then main_timer = 0 end
|
||||
|
||||
if main_timer > hbarmor.tick then
|
||||
main_timer = 0
|
||||
end
|
||||
|
||||
for _,player in pairs(minetest.get_connected_players()) do
|
||||
|
||||
local name = player:get_player_name()
|
||||
|
||||
if hbarmor.player_active[name] == true then
|
||||
local ret = hbarmor.get_armor(player)
|
||||
if ret == false then
|
||||
minetest.log("error", "[hbarmor] Call to hbarmor.get_armor in globalstep returned with false!")
|
||||
|
||||
if hbarmor.get_armor(player) == false then
|
||||
|
||||
minetest.log("error",
|
||||
"[hbarmor] Call to hbarmor.get_armor in globalstep returned with false!")
|
||||
end
|
||||
-- update all hud elements
|
||||
|
||||
update_hud(player)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
-- if timer > 4 then timer = 0 end
|
||||
end)
|
||||
|
|
|
@ -1,31 +1,41 @@
|
|||
|
||||
-- Keep these for backwards compatibility
|
||||
function hbhunger.save_hunger(player)
|
||||
hbhunger.set_hunger(player)
|
||||
end
|
||||
|
||||
function hbhunger.load_hunger(player)
|
||||
hbhunger.get_hunger(player)
|
||||
end
|
||||
|
||||
-- Poison player
|
||||
local function poisenp(tick, time, time_left, player)
|
||||
|
||||
time_left = time_left + tick
|
||||
|
||||
if time_left < time then
|
||||
minetest.after(tick, poisenp, tick, time, time_left, player)
|
||||
else
|
||||
--reset hud image
|
||||
end
|
||||
if player:get_hp()-1 > 0 then
|
||||
player:set_hp(player:get_hp()-1)
|
||||
|
||||
if player:get_hp() - 1 > 0 then
|
||||
|
||||
player:set_hp(player:get_hp() - 1)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function hbhunger.item_eat(hunger_change, replace_with_item, poisen, heal)
|
||||
|
||||
return function(itemstack, user, pointed_thing)
|
||||
|
||||
if itemstack:take_item() ~= nil and user ~= nil then
|
||||
|
||||
local name = user:get_player_name()
|
||||
local h = tonumber(hbhunger.hunger[name])
|
||||
local hp = user:get_hp()
|
||||
|
||||
minetest.sound_play("hbhunger_eat_generic", {
|
||||
object = user,
|
||||
max_hear_distance = 10,
|
||||
|
@ -34,63 +44,90 @@ function hbhunger.item_eat(hunger_change, replace_with_item, poisen, heal)
|
|||
|
||||
-- Saturation
|
||||
if h < 30 and hunger_change then
|
||||
|
||||
h = h + hunger_change
|
||||
|
||||
if h > 30 then h = 30 end
|
||||
|
||||
hbhunger.hunger[name] = h
|
||||
hbhunger.set_hunger(user)
|
||||
end
|
||||
|
||||
-- Healing
|
||||
if hp < 20 and heal then
|
||||
|
||||
hp = hp + heal
|
||||
|
||||
if hp > 20 then hp = 20 end
|
||||
|
||||
user:set_hp(hp)
|
||||
end
|
||||
|
||||
-- Poison
|
||||
if poisen then
|
||||
|
||||
--set hud-img
|
||||
poisenp(1.0, poisen, 0, user)
|
||||
end
|
||||
|
||||
if replace_with_item then
|
||||
|
||||
if itemstack:is_empty() then
|
||||
|
||||
itemstack:add_item(replace_with_item)
|
||||
else
|
||||
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:getpos()
|
||||
|
||||
pos.y = math.floor(pos.y + 0.5)
|
||||
|
||||
core.add_item(pos, replace_with_item)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
return itemstack
|
||||
end
|
||||
end
|
||||
|
||||
local function overwrite(name, hunger_change, replace_with_item, poisen, heal)
|
||||
|
||||
local tab = minetest.registered_items[name]
|
||||
|
||||
if tab == nil then return end
|
||||
|
||||
tab.on_use = hbhunger.item_eat(hunger_change, replace_with_item, poisen, heal)
|
||||
|
||||
minetest.registered_items[name] = tab
|
||||
end
|
||||
|
||||
if minetest.get_modpath("default") ~= nil then
|
||||
if minetest.get_modpath("default") then
|
||||
|
||||
overwrite("default:apple", 2)
|
||||
end
|
||||
if minetest.get_modpath("farming") ~= nil then
|
||||
|
||||
if minetest.get_modpath("farming") then
|
||||
|
||||
overwrite("farming:bread", 4)
|
||||
end
|
||||
if minetest.get_modpath("flowers") ~= nil then
|
||||
|
||||
if minetest.get_modpath("flowers") then
|
||||
|
||||
overwrite("flowers:mushroom_brown", 2)
|
||||
overwrite("flowers:mushroom_red", 0, "", 6)
|
||||
end
|
||||
|
||||
if minetest.get_modpath("mobs") ~= nil then
|
||||
if mobs.mod ~= nil and mobs.mod == "redo" then
|
||||
if minetest.get_modpath("mobs") then
|
||||
|
||||
if mobs and mobs.mod and mobs.mod == "redo" then
|
||||
|
||||
overwrite("mobs:cheese", 4)
|
||||
overwrite("mobs:meat", 8)
|
||||
overwrite("mobs:meat_raw", 4)
|
||||
|
@ -101,7 +138,9 @@ if minetest.get_modpath("mobs") ~= nil then
|
|||
overwrite("mobs:chicken_cooked", 6)
|
||||
overwrite("mobs:chicken_raw", 2, "", 3)
|
||||
overwrite("mobs:chicken_egg_fried", 2)
|
||||
|
||||
if minetest.get_modpath("bucket") then
|
||||
|
||||
overwrite("mobs:bucket_milk", 3, "bucket:bucket_empty")
|
||||
end
|
||||
else
|
||||
|
@ -111,7 +150,8 @@ if minetest.get_modpath("mobs") ~= nil then
|
|||
end
|
||||
end
|
||||
|
||||
if minetest.get_modpath("moretrees") ~= nil then
|
||||
if minetest.get_modpath("moretrees") then
|
||||
|
||||
overwrite("moretrees:coconut_milk", 1)
|
||||
overwrite("moretrees:raw_coconut", 2)
|
||||
overwrite("moretrees:acorn_muffin", 3)
|
||||
|
@ -120,7 +160,8 @@ if minetest.get_modpath("moretrees") ~= nil then
|
|||
overwrite("moretrees:fir_nuts", 1)
|
||||
end
|
||||
|
||||
if minetest.get_modpath("dwarves") ~= nil then
|
||||
if minetest.get_modpath("dwarves") then
|
||||
|
||||
overwrite("dwarves:beer", 2)
|
||||
overwrite("dwarves:apple_cider", 1)
|
||||
overwrite("dwarves:midus", 2)
|
||||
|
@ -129,7 +170,8 @@ if minetest.get_modpath("dwarves") ~= nil then
|
|||
overwrite("dwarves:sake", 2)
|
||||
end
|
||||
|
||||
if minetest.get_modpath("animalmaterials") ~= nil then
|
||||
if minetest.get_modpath("animalmaterials") then
|
||||
|
||||
overwrite("animalmaterials:milk", 2)
|
||||
overwrite("animalmaterials:meat_raw", 3)
|
||||
overwrite("animalmaterials:meat_pork", 3)
|
||||
|
@ -144,7 +186,8 @@ if minetest.get_modpath("animalmaterials") ~= nil then
|
|||
overwrite("animalmaterials:fish_clownfish", 2)
|
||||
end
|
||||
|
||||
if minetest.get_modpath("fishing") ~= nil then
|
||||
if minetest.get_modpath("fishing") then
|
||||
|
||||
overwrite("fishing:fish_raw", 2)
|
||||
overwrite("fishing:fish_cooked", 5)
|
||||
overwrite("fishing:sushi", 6)
|
||||
|
@ -154,11 +197,13 @@ if minetest.get_modpath("fishing") ~= nil then
|
|||
overwrite("fishing:pike_cooked", 8)
|
||||
end
|
||||
|
||||
if minetest.get_modpath("glooptest") ~= nil then
|
||||
if minetest.get_modpath("glooptest") then
|
||||
|
||||
overwrite("glooptest:kalite_lump", 1)
|
||||
end
|
||||
|
||||
if minetest.get_modpath("bushes") ~= nil then
|
||||
if minetest.get_modpath("bushes") then
|
||||
|
||||
overwrite("bushes:sugar", 1)
|
||||
overwrite("bushes:strawberry", 2)
|
||||
overwrite("bushes:berry_pie_raw", 3)
|
||||
|
@ -166,36 +211,45 @@ if minetest.get_modpath("bushes") ~= nil then
|
|||
overwrite("bushes:basket_pies", 15)
|
||||
end
|
||||
|
||||
-- bushes_classic mod, as found in the plantlife modpack
|
||||
if minetest.get_modpath("bushes_classic") then
|
||||
-- bushes_classic mod, as found in the plantlife modpack
|
||||
|
||||
local berries = {
|
||||
"strawberry",
|
||||
"blackberry",
|
||||
"blueberry",
|
||||
"raspberry",
|
||||
"gooseberry",
|
||||
"mixed_berry"}
|
||||
"mixed_berry"
|
||||
}
|
||||
|
||||
for _, berry in pairs(berries) do
|
||||
|
||||
if berry ~= "mixed_berry" then
|
||||
overwrite("bushes:"..berry, 1)
|
||||
overwrite("bushes:" .. berry, 1)
|
||||
end
|
||||
overwrite("bushes:"..berry.."_pie_raw", 2)
|
||||
overwrite("bushes:"..berry.."_pie_cooked", 5)
|
||||
overwrite("bushes:basket_"..berry, 15)
|
||||
|
||||
overwrite("bushes:" .. berry .. "_pie_raw", 2)
|
||||
overwrite("bushes:" .. berry .. "_pie_cooked", 5)
|
||||
overwrite("bushes:basket_" .. berry, 15)
|
||||
end
|
||||
end
|
||||
|
||||
if minetest.get_modpath("mushroom") ~= nil then
|
||||
if minetest.get_modpath("mushroom") then
|
||||
|
||||
overwrite("mushroom:brown", 1)
|
||||
overwrite("mushroom:red", 1, "", 3)
|
||||
|
||||
-- mushroom potions: red = strong poison, brown = light restorative
|
||||
if minetest.get_modpath("vessels") then
|
||||
|
||||
overwrite("mushroom:brown_essence", 1, "vessels:glass_bottle", nil, 4)
|
||||
overwrite("mushroom:poison", 1, "vessels:glass_bottle", 10)
|
||||
end
|
||||
end
|
||||
|
||||
if minetest.get_modpath("docfarming") ~= nil then
|
||||
if minetest.get_modpath("docfarming") then
|
||||
|
||||
overwrite("docfarming:carrot", 3)
|
||||
overwrite("docfarming:cucumber", 2)
|
||||
overwrite("docfarming:corn", 3)
|
||||
|
@ -204,7 +258,8 @@ if minetest.get_modpath("docfarming") ~= nil then
|
|||
overwrite("docfarming:raspberry", 3)
|
||||
end
|
||||
|
||||
if minetest.get_modpath("farming_plus") ~= nil then
|
||||
if minetest.get_modpath("farming_plus") then
|
||||
|
||||
overwrite("farming_plus:carrot_item", 3)
|
||||
overwrite("farming_plus:banana", 2)
|
||||
overwrite("farming_plus:orange_item", 2)
|
||||
|
@ -215,7 +270,8 @@ if minetest.get_modpath("farming_plus") ~= nil then
|
|||
overwrite("farming_plus:rhubarb_item", 2)
|
||||
end
|
||||
|
||||
if minetest.get_modpath("mtfoods") ~= nil then
|
||||
if minetest.get_modpath("mtfoods") then
|
||||
|
||||
overwrite("mtfoods:dandelion_milk", 1)
|
||||
overwrite("mtfoods:sugar", 1)
|
||||
overwrite("mtfoods:short_bread", 4)
|
||||
|
@ -235,7 +291,6 @@ if minetest.get_modpath("mtfoods") ~= nil then
|
|||
overwrite("mtfoods:mlt_burger", 5)
|
||||
overwrite("mtfoods:potato_slices", 2)
|
||||
overwrite("mtfoods:potato_chips", 3)
|
||||
--mtfoods:medicine
|
||||
overwrite("mtfoods:casserole", 3)
|
||||
overwrite("mtfoods:glass_flute", 2)
|
||||
overwrite("mtfoods:orange_juice", 2)
|
||||
|
@ -244,18 +299,21 @@ if minetest.get_modpath("mtfoods") ~= nil then
|
|||
overwrite("mtfoods:cider_rack", 2)
|
||||
end
|
||||
|
||||
if minetest.get_modpath("fruit") ~= nil then
|
||||
if minetest.get_modpath("fruit") then
|
||||
|
||||
overwrite("fruit:apple", 2)
|
||||
overwrite("fruit:pear", 2)
|
||||
overwrite("fruit:bananna", 3)
|
||||
overwrite("fruit:orange", 2)
|
||||
end
|
||||
|
||||
if minetest.get_modpath("mush45") ~= nil then
|
||||
if minetest.get_modpath("mush45") then
|
||||
|
||||
overwrite("mush45:meal", 4)
|
||||
end
|
||||
|
||||
if minetest.get_modpath("seaplants") ~= nil then
|
||||
if minetest.get_modpath("seaplants") then
|
||||
|
||||
overwrite("seaplants:kelpgreen", 1)
|
||||
overwrite("seaplants:kelpbrown", 1)
|
||||
overwrite("seaplants:seagrassgreen", 1)
|
||||
|
@ -267,7 +325,8 @@ if minetest.get_modpath("seaplants") ~= nil then
|
|||
overwrite("seaplants:seagrassgreensalad", 1)
|
||||
end
|
||||
|
||||
if minetest.get_modpath("mobfcooking") ~= nil then
|
||||
if minetest.get_modpath("mobfcooking") then
|
||||
|
||||
overwrite("mobfcooking:cooked_pork", 6)
|
||||
overwrite("mobfcooking:cooked_ostrich", 6)
|
||||
overwrite("mobfcooking:cooked_beef", 6)
|
||||
|
@ -277,13 +336,15 @@ if minetest.get_modpath("mobfcooking") ~= nil then
|
|||
overwrite("mobfcooking:cooked_fish", 6)
|
||||
end
|
||||
|
||||
if minetest.get_modpath("creatures") ~= nil then
|
||||
if minetest.get_modpath("creatures") then
|
||||
|
||||
overwrite("creatures:meat", 6)
|
||||
overwrite("creatures:flesh", 3)
|
||||
overwrite("creatures:rotten_flesh", 3, "", 3)
|
||||
end
|
||||
|
||||
if minetest.get_modpath("ethereal") then
|
||||
|
||||
overwrite("ethereal:strawberry", 1)
|
||||
overwrite("ethereal:banana", 4)
|
||||
overwrite("ethereal:pine_nuts", 1)
|
||||
|
@ -295,12 +356,13 @@ if minetest.get_modpath("ethereal") then
|
|||
overwrite("ethereal:golden_apple", 4, "", nil, 10)
|
||||
overwrite("ethereal:wild_onion_plant", 2)
|
||||
overwrite("ethereal:mushroom_soup", 5, "ethereal:bowl")
|
||||
-- overwrite("ethereal:mushroom_soup_cooked", 6, "ethereal:bowl")
|
||||
overwrite("ethereal:hearty_stew", 10, "ethereal:bowl")
|
||||
-- overwrite("ethereal:hearty_stew_cooked", 10, "ethereal:bowl")
|
||||
|
||||
if minetest.get_modpath("bucket") then
|
||||
|
||||
overwrite("ethereal:bucket_cactus", 2, "bucket:bucket_empty")
|
||||
end
|
||||
|
||||
overwrite("ethereal:fish_raw", 2)
|
||||
overwrite("ethereal:fish_cooked", 5)
|
||||
overwrite("ethereal:seaweed", 1)
|
||||
|
@ -310,6 +372,7 @@ if minetest.get_modpath("ethereal") then
|
|||
end
|
||||
|
||||
if minetest.get_modpath("farming") and farming.mod == "redo" then
|
||||
|
||||
overwrite("farming:bread", 6)
|
||||
overwrite("farming:potato", 1)
|
||||
overwrite("farming:baked_potato", 6)
|
||||
|
@ -332,42 +395,52 @@ if minetest.get_modpath("farming") and farming.mod == "redo" then
|
|||
overwrite("farming:raspberries", 1)
|
||||
overwrite("farming:blueberries", 1)
|
||||
overwrite("farming:muffin_blueberry", 4)
|
||||
|
||||
if minetest.get_modpath("vessels") then
|
||||
|
||||
overwrite("farming:smoothie_raspberry", 2, "vessels:drinking_glass")
|
||||
end
|
||||
|
||||
overwrite("farming:rhubarb", 1)
|
||||
overwrite("farming:rhubarb_pie", 6)
|
||||
overwrite("farming:beans", 1)
|
||||
overwrite("farming:grapes", 2)
|
||||
end
|
||||
|
||||
if minetest.get_modpath("kpgmobs") ~= nil then
|
||||
if minetest.get_modpath("kpgmobs") then
|
||||
|
||||
overwrite("kpgmobs:uley", 3)
|
||||
overwrite("kpgmobs:meat", 6)
|
||||
overwrite("kpgmobs:rat_cooked", 5)
|
||||
overwrite("kpgmobs:med_cooked", 4)
|
||||
|
||||
if minetest.get_modpath("bucket") then
|
||||
|
||||
overwrite("kpgmobs:bucket_milk", 4, "bucket:bucket_empty")
|
||||
end
|
||||
end
|
||||
|
||||
if minetest.get_modpath("jkfarming") ~= nil then
|
||||
if minetest.get_modpath("jkfarming") then
|
||||
|
||||
overwrite("jkfarming:carrot", 3)
|
||||
overwrite("jkfarming:corn", 3)
|
||||
overwrite("jkfarming:melon_part", 2)
|
||||
overwrite("jkfarming:cake", 3)
|
||||
end
|
||||
|
||||
if minetest.get_modpath("jkanimals") ~= nil then
|
||||
if minetest.get_modpath("jkanimals") then
|
||||
|
||||
overwrite("jkanimals:meat", 6)
|
||||
end
|
||||
|
||||
if minetest.get_modpath("jkwine") ~= nil then
|
||||
if minetest.get_modpath("jkwine") then
|
||||
|
||||
overwrite("jkwine:grapes", 2)
|
||||
overwrite("jkwine:winebottle", 1)
|
||||
end
|
||||
|
||||
if minetest.get_modpath("cooking") ~= nil then
|
||||
if minetest.get_modpath("cooking") then
|
||||
|
||||
overwrite("cooking:meat_beef_cooked", 4)
|
||||
overwrite("cooking:fish_bluewhite_cooked", 3)
|
||||
overwrite("cooking:fish_clownfish_cooked", 1)
|
||||
|
@ -380,7 +453,8 @@ if minetest.get_modpath("cooking") ~= nil then
|
|||
end
|
||||
|
||||
-- ferns mod of plantlife_modpack
|
||||
if minetest.get_modpath("ferns") ~= nil then
|
||||
if minetest.get_modpath("ferns") then
|
||||
|
||||
overwrite("ferns:fiddlehead", 1, "", 1)
|
||||
overwrite("ferns:fiddlehead_roasted", 3)
|
||||
overwrite("ferns:ferntuber_roasted", 3)
|
||||
|
@ -389,6 +463,7 @@ end
|
|||
|
||||
-- Xanadu server only
|
||||
if minetest.get_modpath("xanadu") then
|
||||
|
||||
overwrite("xanadu:cinnamon_roll", 4)
|
||||
overwrite("xanadu:pumpkin_pie", 10)
|
||||
overwrite("xanadu:french_toast", 2)
|
||||
|
@ -423,30 +498,44 @@ end
|
|||
|
||||
-- player-action based hunger changes
|
||||
function hbhunger.handle_node_actions(pos, oldnode, player, ext)
|
||||
|
||||
if not player or not player:is_player() then
|
||||
return
|
||||
end
|
||||
|
||||
local name = player:get_player_name()
|
||||
local exhaus = hbhunger.exhaustion[name]
|
||||
|
||||
if exhaus == nil then return end
|
||||
|
||||
local new = HUNGER_EXHAUST_PLACE
|
||||
|
||||
-- placenode event
|
||||
if not ext then
|
||||
new = HUNGER_EXHAUST_DIG
|
||||
end
|
||||
|
||||
-- assume its send by main timer when movement detected
|
||||
if not pos and not oldnode then
|
||||
new = HUNGER_EXHAUST_MOVE
|
||||
end
|
||||
|
||||
exhaus = exhaus + new
|
||||
|
||||
if exhaus > HUNGER_EXHAUST_LVL then
|
||||
|
||||
exhaus = 0
|
||||
|
||||
local h = tonumber(hbhunger.hunger[name])
|
||||
|
||||
h = h - 1
|
||||
|
||||
if h < 0 then h = 0 end
|
||||
|
||||
hbhunger.hunger[name] = h
|
||||
hbhunger.set_hunger(player)
|
||||
end
|
||||
|
||||
hbhunger.exhaustion[name] = exhaus
|
||||
end
|
||||
|
||||
|
|
|
@ -1,21 +1,18 @@
|
|||
-- if damage enabled
|
||||
if minetest.setting_getbool("enable_damage") then
|
||||
|
||||
if not minetest.setting_getbool("enable_damage") then
|
||||
return
|
||||
end
|
||||
|
||||
hbhunger = {}
|
||||
|
||||
-- HUD statbar values
|
||||
hbhunger.hunger = {}
|
||||
hbhunger.hunger = {} -- HUD statbar values
|
||||
hbhunger.hunger_out = {}
|
||||
hbhunger.exhaustion = {} -- Exhaustion is experimental!
|
||||
|
||||
-- HUD item ids
|
||||
local hunger_hud = {}
|
||||
|
||||
HUNGER_HUD_TICK = 0.5 -- 0.1
|
||||
|
||||
--Some hunger settings
|
||||
hbhunger.exhaustion = {} -- Exhaustion is experimental!
|
||||
|
||||
HUNGER_HUNGER_TICK = 600 -- time in seconds after that 1 hunger point is taken (600)
|
||||
HUNGER_HUD_TICK = 0.5 -- was 0.1
|
||||
HUNGER_HUNGER_TICK = 500 -- time in seconds after that 1 hunger point is taken (600)
|
||||
HUNGER_EXHAUST_DIG = 3 -- exhaustion increased this value after digged node
|
||||
HUNGER_EXHAUST_PLACE = 1 -- exhaustion increased this value after placed
|
||||
HUNGER_EXHAUST_MOVE = 0.3 -- exhaustion increased this value if player movement detected
|
||||
|
@ -30,10 +27,11 @@ if set then
|
|||
end--]]
|
||||
|
||||
local function custom_hud(player)
|
||||
|
||||
hb.init_hudbar(player, "satiation", hbhunger.get_hunger(player))
|
||||
end
|
||||
|
||||
dofile(minetest.get_modpath("hbhunger").."/hunger.lua")
|
||||
dofile(minetest.get_modpath("hbhunger") .. "/hunger.lua")
|
||||
|
||||
-- register satiation hudbar
|
||||
hb.register_hudbar(
|
||||
|
@ -48,25 +46,37 @@ hb.register_hudbar(
|
|||
|
||||
-- update hud elemtents if value has changed
|
||||
local function update_hud(player)
|
||||
|
||||
if not player then return end
|
||||
|
||||
local name = player:get_player_name()
|
||||
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
|
||||
|
||||
hbhunger.get_hunger = 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
|
||||
|
||||
|
@ -82,19 +92,26 @@ hbhunger.set_hunger = function(player)
|
|||
end
|
||||
|
||||
minetest.register_on_joinplayer(function(player)
|
||||
|
||||
local name = player:get_player_name()
|
||||
local inv = player:get_inventory()
|
||||
|
||||
inv:set_size("hunger", 1)
|
||||
|
||||
hbhunger.hunger[name] = hbhunger.get_hunger(player)
|
||||
hbhunger.hunger_out[name] = hbhunger.hunger[name]
|
||||
hbhunger.exhaustion[name] = 0
|
||||
|
||||
custom_hud(player)
|
||||
|
||||
hbhunger.set_hunger(player)
|
||||
end)
|
||||
|
||||
minetest.register_on_respawnplayer(function(player)
|
||||
|
||||
-- reset hunger (and save)
|
||||
local name = player:get_player_name()
|
||||
|
||||
hbhunger.hunger[name] = 20
|
||||
hbhunger.set_hunger(player)
|
||||
hbhunger.exhaustion[name] = 0
|
||||
|
@ -130,9 +147,12 @@ minetest.register_globalstep(function(dtime)
|
|||
if h > 15
|
||||
and hp > 0
|
||||
and player:get_breath() > 0 then
|
||||
|
||||
player:set_hp(hp + 1)
|
||||
|
||||
-- or damage player by 1 hp if satiation is < 2
|
||||
elseif h <= 1 then
|
||||
|
||||
if hp - 1 >= 0 then
|
||||
player:set_hp(hp - 1)
|
||||
end
|
||||
|
@ -141,8 +161,11 @@ minetest.register_globalstep(function(dtime)
|
|||
|
||||
-- lower satiation by 1 point after xx seconds
|
||||
if timer2 > HUNGER_HUNGER_TICK then
|
||||
|
||||
if h > 0 then
|
||||
|
||||
h = h - 1
|
||||
|
||||
hbhunger.hunger[name] = h
|
||||
hbhunger.set_hunger(player)
|
||||
end
|
||||
|
@ -157,6 +180,7 @@ minetest.register_globalstep(function(dtime)
|
|||
or controls.down
|
||||
or controls.left
|
||||
or controls.right then
|
||||
|
||||
hbhunger.handle_node_actions(nil, nil, player)
|
||||
end
|
||||
|
||||
|
@ -173,5 +197,3 @@ minetest.register_globalstep(function(dtime)
|
|||
end
|
||||
|
||||
end)
|
||||
|
||||
end -- end if damage enabled
|
||||
|
|
400
hudbars/init.lua
400
hudbars/init.lua
|
@ -1,17 +1,17 @@
|
|||
|
||||
hb = {}
|
||||
hb.hudtables = {}
|
||||
-- number of registered HUD bars
|
||||
hb.hudbars_count = 0
|
||||
-- table that records which HUD bar slots have been “registered” so far
|
||||
-- used for automatic positioning
|
||||
hb.registered_slots = {}
|
||||
hb.hudbars_count = 0 -- number of registered HUD bars
|
||||
hb.registered_slots = {} -- table of HUD bars for automatic positioning
|
||||
hb.settings = {}
|
||||
|
||||
local enable_damage = minetest.setting_getbool("enable_damage")
|
||||
local gui_scale = tonumber(minetest.setting_get("gui_scaling")) or 1 -- use this to scale hud
|
||||
local gui_scale = tonumber(minetest.setting_get("gui_scaling")) or 1 -- scales hud
|
||||
|
||||
function hb.load_setting(sname, stype, defaultval, valid_values)
|
||||
|
||||
local sval
|
||||
|
||||
if stype == "string" then
|
||||
sval = minetest.setting_get(sname)
|
||||
elseif stype == "bool" then
|
||||
|
@ -19,28 +19,33 @@ function hb.load_setting(sname, stype, defaultval, valid_values)
|
|||
elseif stype == "number" then
|
||||
sval = tonumber(minetest.setting_get(sname))
|
||||
end
|
||||
if sval ~= nil then
|
||||
if valid_values ~= nil then
|
||||
local valid = false
|
||||
for i=1,#valid_values do
|
||||
if sval == valid_values[i] then
|
||||
valid = true
|
||||
end
|
||||
end
|
||||
if not valid then
|
||||
-- minetest.log("error", "[hudbars] Invalid value for "
|
||||
-- ..sname.."! Using default value ("
|
||||
-- ..tostring(defaultval)..").")
|
||||
return defaultval
|
||||
else
|
||||
return sval
|
||||
end
|
||||
else
|
||||
return sval
|
||||
end
|
||||
else
|
||||
|
||||
if not sval then
|
||||
return defaultval
|
||||
end
|
||||
|
||||
if not valid_values then
|
||||
return sval
|
||||
end
|
||||
|
||||
local valid = false
|
||||
|
||||
for i = 1, #valid_values do
|
||||
|
||||
if sval == valid_values[i] then
|
||||
valid = true
|
||||
end
|
||||
end
|
||||
|
||||
if not valid then
|
||||
|
||||
-- minetest.log("error", "[hudbars] Invalid value for "
|
||||
-- ..sname.."! Using default value ("
|
||||
-- ..tostring(defaultval)..").")
|
||||
return defaultval
|
||||
end
|
||||
|
||||
return sval
|
||||
end
|
||||
|
||||
-- (hardcoded) default settings
|
||||
|
@ -52,16 +57,6 @@ hb.settings.pos_left = {}
|
|||
hb.settings.pos_right = {}
|
||||
hb.settings.start_offset_left = {}
|
||||
hb.settings.start_offset_right= {}
|
||||
--[[
|
||||
hb.settings.pos_left.x = hb.load_setting("hudbars_pos_left_x", "number", 0) -- 0.5
|
||||
hb.settings.pos_left.y = hb.load_setting("hudbars_pos_left_y", "number", 0.7) -- 1
|
||||
hb.settings.pos_right.x = hb.load_setting("hudbars_pos_right_x", "number", 1) -- 0.5
|
||||
hb.settings.pos_right.y = hb.load_setting("hudbars_pos_right_y", "number", 1) -- 1
|
||||
hb.settings.start_offset_left.x = hb.load_setting("hudbars_start_offset_left_x", "number", 50) -- -175
|
||||
hb.settings.start_offset_left.y = hb.load_setting("hudbars_start_offset_left_y", "number", 0) -- -86
|
||||
hb.settings.start_offset_right.x = hb.load_setting("hudbars_start_offset_right_x", "number", -50) -- 15
|
||||
hb.settings.start_offset_right.y = hb.load_setting("hudbars_start_offset_right_y", "number", -100) -- -86
|
||||
--]]
|
||||
|
||||
hb.settings.pos_left.x = hb.load_setting("hudbars_pos_left_x", "number", 0.5)
|
||||
hb.settings.pos_left.y = hb.load_setting("hudbars_pos_left_y", "number", 1)
|
||||
|
@ -72,7 +67,6 @@ hb.settings.start_offset_left.y = hb.load_setting("hudbars_start_offset_left_y",
|
|||
hb.settings.start_offset_right.x = hb.load_setting("hudbars_start_offset_right_x", "number", (15 * gui_scale))
|
||||
hb.settings.start_offset_right.y = hb.load_setting("hudbars_start_offset_right_y", "number", -86)
|
||||
|
||||
|
||||
hb.settings.vmargin = hb.load_setting("hudbars_tick", "number", 24 * gui_scale)
|
||||
hb.settings.tick = hb.load_setting("hudbars_tick", "number", 1) -- was 0.1
|
||||
|
||||
|
@ -98,6 +92,7 @@ hb.settings.bar_type = hb.load_setting(
|
|||
{"progress_bar", "statbar_classic", "statbar_modern"})
|
||||
hb.settings.autohide_breath = hb.load_setting("hudbars_autohide_breath", "bool", true)
|
||||
hb.direction = 0 -- 0: left-right, 1: right-left, 2: top-bottom, 3: bottom-top -TENPLUS1
|
||||
|
||||
if hb.settings.bar_type == "progress_bar" then
|
||||
hb.size = {x = 3 * gui_scale, y = 17 * gui_scale}
|
||||
else
|
||||
|
@ -107,14 +102,17 @@ end
|
|||
local sorting = minetest.setting_get("hudbars_sorting")
|
||||
|
||||
if sorting ~= nil then
|
||||
|
||||
hb.settings.sorting = {}
|
||||
hb.settings.sorting_reverse = {}
|
||||
|
||||
for k,v in string.gmatch(sorting, "(%w+)=(%w+)") do
|
||||
|
||||
hb.settings.sorting[k] = tonumber(v)
|
||||
hb.settings.sorting_reverse[tonumber(v)] = k
|
||||
end
|
||||
else -- swapped health & breath around and added hunger & armor
|
||||
--hb.settings.sorting = { ["breath"] = 0, ["health"] = 1, ["hunger"] = 2, ["armor"] = 3 }
|
||||
else
|
||||
-- swapped health & breath around and added hunger & armor
|
||||
hb.settings.sorting = { ["health"] = 0, ["hunger"] = 1, ["armor"] = 2, ["breath"] = 3}
|
||||
hb.settings.sorting_reverse = {}-- [0] = "breath", [1] = "health"}
|
||||
end
|
||||
|
@ -123,21 +121,25 @@ end
|
|||
hb.players = {}
|
||||
|
||||
function hb.value_to_barlength(value, max)
|
||||
|
||||
if max == 0 then
|
||||
return 0
|
||||
else
|
||||
if hb.settings.bar_type == "progress_bar" then
|
||||
local x
|
||||
if value < 0 then x=-0.5 else x = 0.5 end
|
||||
local ret = math.modf((value/max) * hb.settings.max_bar_length + x)
|
||||
return ret
|
||||
else
|
||||
local x
|
||||
if value < 0 then x=-0.5 else x = 0.5 end
|
||||
local ret = math.modf((value/max) * hb.settings.statbar_length + x)
|
||||
return ret
|
||||
end
|
||||
end
|
||||
|
||||
local x
|
||||
|
||||
if value < 0 then
|
||||
x = -0.5
|
||||
else
|
||||
x = 0.5
|
||||
end
|
||||
|
||||
if hb.settings.bar_type == "progress_bar" then
|
||||
|
||||
return math.modf( (value / max) * hb.settings.max_bar_length + x)
|
||||
end
|
||||
|
||||
return math.modf( (value / max) * hb.settings.statbar_length + x)
|
||||
end
|
||||
|
||||
function hb.get_hudtable(identifier)
|
||||
|
@ -145,71 +147,96 @@ function hb.get_hudtable(identifier)
|
|||
end
|
||||
|
||||
function hb.get_hudbar_position_index(identifier)
|
||||
|
||||
if hb.settings.sorting[identifier] ~= nil then
|
||||
return hb.settings.sorting[identifier]
|
||||
else
|
||||
local i = 0
|
||||
while true do
|
||||
if hb.registered_slots[i] ~= true
|
||||
and hb.settings.sorting_reverse[i] == nil then
|
||||
return i
|
||||
end
|
||||
i = i + 1
|
||||
end
|
||||
|
||||
local i = 0
|
||||
|
||||
while true do
|
||||
|
||||
if hb.registered_slots[i] ~= true
|
||||
and hb.settings.sorting_reverse[i] == nil then
|
||||
return i
|
||||
end
|
||||
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
|
||||
function hb.register_hudbar(identifier, text_color, label, textures, default_start_value, default_start_max, default_start_hidden, format_string)
|
||||
function hb.register_hudbar(identifier, text_color, label, textures,
|
||||
default_start_value, default_start_max, default_start_hidden, format_string)
|
||||
|
||||
-- minetest.log("action", "hb.register_hudbar: "..tostring(identifier))
|
||||
|
||||
local hudtable = {}
|
||||
local pos, offset
|
||||
local index = math.floor(hb.get_hudbar_position_index(identifier))
|
||||
|
||||
hb.registered_slots[index] = true
|
||||
|
||||
if hb.settings.alignment_pattern == "stack_up" then
|
||||
|
||||
pos = hb.settings.pos_left
|
||||
|
||||
offset = {
|
||||
x = hb.settings.start_offset_left.x,
|
||||
y = hb.settings.start_offset_left.y - hb.settings.vmargin * index
|
||||
}
|
||||
|
||||
elseif hb.settings.alignment_pattern == "stack_down" then
|
||||
|
||||
pos = hb.settings.pos_left
|
||||
|
||||
offset = {
|
||||
x = hb.settings.start_offset_left.x,
|
||||
y = hb.settings.start_offset_left.y + hb.settings.vmargin * index
|
||||
}
|
||||
else
|
||||
if index % 2 == 0 then
|
||||
|
||||
pos = hb.settings.pos_left
|
||||
|
||||
offset = {
|
||||
x = hb.settings.start_offset_left.x,
|
||||
y = hb.settings.start_offset_left.y - hb.settings.vmargin * (index/2)
|
||||
y = hb.settings.start_offset_left.y - hb.settings.vmargin * (index / 2)
|
||||
}
|
||||
else
|
||||
pos = hb.settings.pos_right
|
||||
|
||||
offset = {
|
||||
x = hb.settings.start_offset_right.x,
|
||||
y = hb.settings.start_offset_right.y - hb.settings.vmargin * ((index-1)/2)
|
||||
y = hb.settings.start_offset_right.y - hb.settings.vmargin * ((index - 1) / 2)
|
||||
}
|
||||
end
|
||||
end
|
||||
if format_string == nil then
|
||||
format_string = "%s: %d/%d"
|
||||
end
|
||||
|
||||
format_string = format_string or "%s: %d/%d"
|
||||
|
||||
hudtable.add_all = function(player, hudtable, start_value, start_max, start_hidden)
|
||||
if start_value == nil then start_value = hudtable.default_start_value end
|
||||
if start_max == nil then start_max = hudtable.default_start_max end
|
||||
if start_hidden == nil then start_hidden = hudtable.default_start_hidden end
|
||||
|
||||
start_value = start_value or hudtable.default_start_value
|
||||
|
||||
start_max = start_max or hudtable.default_start_max
|
||||
|
||||
start_hidden = start_hidden or hudtable.default_start_hidden
|
||||
|
||||
local ids = {}
|
||||
local state = {}
|
||||
local name = player:get_player_name()
|
||||
local bgscale, iconscale, text, barnumber
|
||||
if start_max == 0 or start_hidden then
|
||||
|
||||
if start_max == 0
|
||||
or start_hidden then
|
||||
|
||||
bgscale = {x = 0, y = 0}
|
||||
else
|
||||
bgscale = {x = 1 * gui_scale, y = 1 * gui_scale}
|
||||
end
|
||||
|
||||
if start_hidden then
|
||||
|
||||
iconscale = {x = 0, y = 0}
|
||||
barnumber = 0
|
||||
text = ""
|
||||
|
@ -218,7 +245,9 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
|
|||
barnumber = hb.value_to_barlength(start_value, start_max)
|
||||
text = string.format(format_string, label, start_value, start_max)
|
||||
end
|
||||
|
||||
if hb.settings.bar_type == "progress_bar" then
|
||||
|
||||
ids.bg = player:hud_add({
|
||||
hud_elem_type = "image",
|
||||
position = pos,
|
||||
|
@ -229,7 +258,9 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
|
|||
direction = hb.direction, -- ADDED
|
||||
size = hb.size,
|
||||
})
|
||||
|
||||
if textures.icon ~= nil then
|
||||
|
||||
ids.icon = player:hud_add({
|
||||
hud_elem_type = "image",
|
||||
position = pos,
|
||||
|
@ -241,8 +272,11 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
|
|||
size = hb.size,
|
||||
})
|
||||
end
|
||||
|
||||
elseif hb.settings.bar_type == "statbar_modern" then
|
||||
|
||||
if textures.bgicon ~= nil then
|
||||
|
||||
ids.bg = player:hud_add({
|
||||
hud_elem_type = "statbar",
|
||||
position = pos,
|
||||
|
@ -256,12 +290,19 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
|
|||
})
|
||||
end
|
||||
end
|
||||
|
||||
local bar_image
|
||||
|
||||
if hb.settings.bar_type == "progress_bar" then
|
||||
|
||||
bar_image = textures.bar
|
||||
elseif hb.settings.bar_type == "statbar_classic" or hb.settings.bar_type == "statbar_modern" then
|
||||
|
||||
elseif hb.settings.bar_type == "statbar_classic"
|
||||
or hb.settings.bar_type == "statbar_modern" then
|
||||
|
||||
bar_image = textures.icon
|
||||
end
|
||||
|
||||
ids.bar = player:hud_add({
|
||||
hud_elem_type = "statbar",
|
||||
position = pos,
|
||||
|
@ -272,7 +313,9 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
|
|||
direction = hb.direction, -- ADDED
|
||||
size = hb.size,
|
||||
})
|
||||
|
||||
if hb.settings.bar_type == "progress_bar" then
|
||||
|
||||
ids.text = player:hud_add({
|
||||
hud_elem_type = "text",
|
||||
position = pos,
|
||||
|
@ -282,8 +325,9 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
|
|||
offset = { x = offset.x + 2, y = offset.y },
|
||||
direction = hb.direction, -- ADDED
|
||||
size = hb.size,
|
||||
})
|
||||
})
|
||||
end
|
||||
|
||||
-- Do not forget to update hb.get_hudbar_state if you add new fields to the state table
|
||||
state.hidden = start_hidden
|
||||
state.value = start_value
|
||||
|
@ -327,41 +371,46 @@ function hb.register_hudbar(identifier, text_color, label, textures, default_sta
|
|||
end
|
||||
|
||||
function hb.init_hudbar(player, identifier, start_value, start_max, start_hidden)
|
||||
|
||||
local hudtable = hb.get_hudtable(identifier)
|
||||
|
||||
hb.hudtables[identifier].add_all(player, hudtable, start_value, start_max, start_hidden)
|
||||
end
|
||||
|
||||
function hb.change_hudbar(player, identifier, new_value, new_max_value)
|
||||
if new_value == nil and new_max_value == nil then
|
||||
|
||||
if new_value == nil
|
||||
and new_max_value == nil then
|
||||
return
|
||||
end
|
||||
|
||||
--- ADDED THIS BIT
|
||||
if not player then
|
||||
print ("line 340 player nil")
|
||||
return
|
||||
end
|
||||
if not player then -- TENPLUS1
|
||||
return
|
||||
end
|
||||
|
||||
local name = player:get_player_name()
|
||||
local hudtable = hb.get_hudtable(identifier)
|
||||
local value_changed, max_changed = false, false
|
||||
|
||||
-- ADDED THIS TOO
|
||||
if not hudtable.hudstate[name] then
|
||||
print ("Warning: [hudbars init.lua] line 350 hudtable.hudstate[" .. name .. "] is nil")
|
||||
return
|
||||
end
|
||||
if not hudtable.hudstate[name] then -- TENPLUS1
|
||||
return
|
||||
end
|
||||
|
||||
if new_value ~= nil then
|
||||
|
||||
if new_value ~= hudtable.hudstate[name].value then
|
||||
|
||||
hudtable.hudstate[name].value = new_value
|
||||
value_changed = true
|
||||
end
|
||||
else
|
||||
new_value = hudtable.hudstate[name].value
|
||||
end
|
||||
|
||||
if new_max_value ~= nil then
|
||||
|
||||
if new_max_value ~= hudtable.hudstate[name].max then
|
||||
|
||||
hudtable.hudstate[name].max = new_max_value
|
||||
max_changed = true
|
||||
end
|
||||
|
@ -387,37 +436,48 @@ end
|
|||
end
|
||||
]]
|
||||
if hudtable.hudstate[name].hidden == false then
|
||||
|
||||
if max_changed and hb.settings.bar_type == "progress_bar" then
|
||||
|
||||
if hudtable.hudstate[name].max == 0 then
|
||||
player:hud_change(hudtable.hudids[name].bg, "scale", {x=0,y=0})
|
||||
|
||||
player:hud_change(hudtable.hudids[name].bg, "scale", {x = 0, y = 0})
|
||||
else
|
||||
player:hud_change(hudtable.hudids[name].bg, "scale", {x=1,y=1})
|
||||
player:hud_change(hudtable.hudids[name].bg, "scale", {x = 1, y = 1})
|
||||
end
|
||||
end
|
||||
|
||||
if value_changed or max_changed then
|
||||
|
||||
local new_barlength = hb.value_to_barlength(new_value, new_max_value)
|
||||
|
||||
if new_barlength ~= hudtable.hudstate[name].barlength then
|
||||
|
||||
player:hud_change(
|
||||
hudtable.hudids[name].bar,
|
||||
"number",
|
||||
hb.value_to_barlength(new_value, new_max_value)
|
||||
)
|
||||
|
||||
hudtable.hudstate[name].barlength = new_barlength
|
||||
end
|
||||
|
||||
if hb.settings.bar_type == "progress_bar" then
|
||||
|
||||
local new_text = string.format(
|
||||
hudtable.format_string,
|
||||
hudtable.label,
|
||||
new_value, new_max_value
|
||||
)
|
||||
|
||||
if new_text ~= hudtable.hudstate[name].text then
|
||||
|
||||
player:hud_change(
|
||||
hudtable.hudids[name].text,
|
||||
"text",
|
||||
new_text
|
||||
)
|
||||
|
||||
hudtable.hudstate[name].text = new_text
|
||||
end
|
||||
end
|
||||
|
@ -426,85 +486,117 @@ end
|
|||
end
|
||||
|
||||
function hb.hide_hudbar(player, identifier)
|
||||
|
||||
if not player then -- TENPLUS1
|
||||
return
|
||||
end
|
||||
|
||||
local name = player:get_player_name()
|
||||
local hudtable = hb.get_hudtable(identifier)
|
||||
|
||||
if hudtable.hudstate[name]
|
||||
and hudtable.hudstate[name].hidden == false then
|
||||
if hb.settings.bar_type == "progress_bar" then
|
||||
if hudtable.hudids[name].icon ~= nil then
|
||||
player:hud_change(
|
||||
hudtable.hudids[name].icon,
|
||||
"scale",
|
||||
{x = 0, y = 0}
|
||||
)
|
||||
end
|
||||
and hudtable.hudstate[name].hidden ~= false then
|
||||
return
|
||||
end
|
||||
|
||||
if hb.settings.bar_type == "progress_bar" then
|
||||
|
||||
if hudtable.hudids[name].icon ~= nil then
|
||||
|
||||
player:hud_change(
|
||||
hudtable.hudids[name].bg,
|
||||
hudtable.hudids[name].icon,
|
||||
"scale",
|
||||
{x = 0, y = 0}
|
||||
)
|
||||
player:hud_change(
|
||||
hudtable.hudids[name].text,
|
||||
"text",
|
||||
""
|
||||
)
|
||||
end
|
||||
|
||||
player:hud_change(
|
||||
hudtable.hudids[name].bar,
|
||||
"number",
|
||||
0
|
||||
hudtable.hudids[name].bg,
|
||||
"scale",
|
||||
{x = 0, y = 0}
|
||||
)
|
||||
|
||||
player:hud_change(
|
||||
hudtable.hudids[name].text,
|
||||
"text",
|
||||
""
|
||||
)
|
||||
hudtable.hudstate[name].hidden = true
|
||||
end
|
||||
|
||||
player:hud_change(
|
||||
hudtable.hudids[name].bar,
|
||||
"number",
|
||||
0
|
||||
)
|
||||
|
||||
hudtable.hudstate[name].hidden = true
|
||||
end
|
||||
|
||||
function hb.unhide_hudbar(player, identifier)
|
||||
|
||||
if not player then -- TenPlus1
|
||||
return
|
||||
end
|
||||
|
||||
local name = player:get_player_name()
|
||||
local hudtable = hb.get_hudtable(identifier)
|
||||
|
||||
if hudtable.hudstate[name] -- ADDED
|
||||
and hudtable.hudstate[name].hidden then
|
||||
local name = player:get_player_name()
|
||||
local value = hudtable.hudstate[name].value
|
||||
local max = hudtable.hudstate[name].max
|
||||
if hb.settings.bar_type == "progress_bar" then
|
||||
if hudtable.hudids[name].icon ~= nil then
|
||||
player:hud_change(
|
||||
hudtable.hudids[name].icon,
|
||||
"scale",
|
||||
{x = 1, y = 1}
|
||||
)
|
||||
end
|
||||
if hudtable.hudstate[name].max ~= 0 then
|
||||
player:hud_change(
|
||||
hudtable.hudids[name].bg,
|
||||
"scale",
|
||||
{x = 1, y = 1}
|
||||
)
|
||||
end
|
||||
and hudtable.hudstate[name].hidden == false then
|
||||
return
|
||||
end
|
||||
|
||||
local name = player:get_player_name()
|
||||
local value = hudtable.hudstate[name].value
|
||||
local max = hudtable.hudstate[name].max
|
||||
|
||||
if hb.settings.bar_type == "progress_bar" then
|
||||
|
||||
if hudtable.hudids[name].icon ~= nil then
|
||||
|
||||
player:hud_change(
|
||||
hudtable.hudids[name].text,
|
||||
"text",
|
||||
tostring(
|
||||
string.format(
|
||||
hudtable.format_string,
|
||||
hudtable.label,
|
||||
value,
|
||||
max
|
||||
)
|
||||
)
|
||||
hudtable.hudids[name].icon,
|
||||
"scale",
|
||||
{x = 1, y = 1}
|
||||
)
|
||||
end
|
||||
|
||||
if hudtable.hudstate[name].max ~= 0 then
|
||||
|
||||
player:hud_change(
|
||||
hudtable.hudids[name].bg,
|
||||
"scale",
|
||||
{x = 1, y = 1}
|
||||
)
|
||||
end
|
||||
|
||||
player:hud_change(
|
||||
hudtable.hudids[name].bar,
|
||||
"number",
|
||||
hb.value_to_barlength(value, max)
|
||||
hudtable.hudids[name].text,
|
||||
"text",
|
||||
tostring(
|
||||
string.format(
|
||||
hudtable.format_string,
|
||||
hudtable.label,
|
||||
value,
|
||||
max
|
||||
)
|
||||
)
|
||||
)
|
||||
hudtable.hudstate[name].hidden = false
|
||||
end
|
||||
|
||||
player:hud_change(
|
||||
hudtable.hudids[name].bar,
|
||||
"number",
|
||||
hb.value_to_barlength(value, max)
|
||||
)
|
||||
|
||||
hudtable.hudstate[name].hidden = false
|
||||
end
|
||||
|
||||
function hb.get_hudbar_state(player, identifier)
|
||||
|
||||
local ref = hb.get_hudtable(identifier).hudstate[player:get_player_name()]
|
||||
|
||||
-- Do not forget to update this chunk of code in case the state changes
|
||||
local copy = {
|
||||
hidden = ref.hidden,
|
||||
|
@ -513,12 +605,14 @@ function hb.get_hudbar_state(player, identifier)
|
|||
text = ref.text,
|
||||
barlength = ref.barlength,
|
||||
}
|
||||
|
||||
return copy
|
||||
end
|
||||
|
||||
--register built-in HUD bars
|
||||
if enable_damage
|
||||
or hb.settings.forceload_default_hudbars then
|
||||
|
||||
hb.register_hudbar("health", 0xFFFFFF, "Health",
|
||||
{
|
||||
bar = "hudbars_bar_health.png",
|
||||
|
@ -536,30 +630,36 @@ or hb.settings.forceload_default_hudbars then
|
|||
end
|
||||
|
||||
local function hide_builtin(player)
|
||||
|
||||
local flags = player:hud_get_flags()
|
||||
|
||||
flags.healthbar = false
|
||||
flags.breathbar = false
|
||||
|
||||
player:hud_set_flags(flags)
|
||||
end
|
||||
|
||||
|
||||
local function custom_hud(player)
|
||||
|
||||
if enable_damage or hb.settings.forceload_default_hudbars then
|
||||
local hide
|
||||
|
||||
local hide = true
|
||||
|
||||
if enable_damage then
|
||||
hide = false
|
||||
else
|
||||
hide = true
|
||||
end
|
||||
|
||||
hb.init_hudbar(player, "health", player:get_hp(), nil, hide)
|
||||
|
||||
local breath = player:get_breath()
|
||||
local hide_breath
|
||||
local hide_breath = false
|
||||
|
||||
if breath == 11
|
||||
and hb.settings.autohide_breath == true then
|
||||
hide_breath = true
|
||||
else
|
||||
hide_breath = false
|
||||
end
|
||||
|
||||
hb.init_hudbar(player, "breath", math.min(breath, 10), nil, hide_breath or hide)
|
||||
end
|
||||
end
|
||||
|
@ -567,14 +667,18 @@ end
|
|||
|
||||
-- update built-in HUD bars
|
||||
local function update_hud(player)
|
||||
|
||||
if enable_damage then
|
||||
|
||||
if hb.settings.forceload_default_hudbars then
|
||||
hb.unhide_hudbar(player, "health")
|
||||
end
|
||||
|
||||
--air
|
||||
local breath = player:get_breath() or 11 -- ADDED or 11
|
||||
local breath = player:get_breath() or 11 -- TenPlus1 (or 11)
|
||||
|
||||
if breath == 11 and hb.settings.autohide_breath == true then
|
||||
|
||||
hb.hide_hudbar(player, "breath")
|
||||
else
|
||||
hb.unhide_hudbar(player, "breath")
|
||||
|
@ -583,19 +687,25 @@ local function update_hud(player)
|
|||
|
||||
--health
|
||||
hb.change_hudbar(player, "health", player:get_hp())
|
||||
|
||||
elseif hb.settings.forceload_default_hudbars then
|
||||
|
||||
hb.hide_hudbar(player, "health")
|
||||
hb.hide_hudbar(player, "breath")
|
||||
end
|
||||
end
|
||||
|
||||
minetest.register_on_joinplayer(function(player)
|
||||
|
||||
hide_builtin(player)
|
||||
|
||||
custom_hud(player)
|
||||
|
||||
hb.players[player:get_player_name()] = player
|
||||
end)
|
||||
|
||||
minetest.register_on_leaveplayer(function(player)
|
||||
|
||||
hb.players[player:get_player_name()] = nil
|
||||
end)
|
||||
|
||||
|
@ -613,11 +723,11 @@ minetest.register_globalstep(function(dtime)
|
|||
-- only proceed if damage is enabled
|
||||
if enable_damage
|
||||
or hb.settings.forceload_default_hudbars then
|
||||
|
||||
for playername, player in pairs(hb.players) do
|
||||
-- update all hud elements
|
||||
|
||||
update_hud(player)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end)
|
||||
|
|
Loading…
Reference in New Issue