diff --git a/README.md b/README.md index 7e5de8f..739f6fd 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -Hudbars mod for Minetest +Hudbars, hbhunger and hbarmor mods for Minetest -This mod adds hud bars to the player's screen for health, hunger and drowning. +These mod adds hud bars to the player's screen for health, hunger, drowning and armor levels. -Mod created by Wuzzy and edited by TenPlus1 for Xanadu server \ No newline at end of file +Mods created by Wuzzy and edited by TenPlus1 for Xanadu server \ No newline at end of file diff --git a/hbarmor/depends.txt b/hbarmor/depends.txt new file mode 100644 index 0000000..579f7fb --- /dev/null +++ b/hbarmor/depends.txt @@ -0,0 +1,2 @@ +hudbars +3d_armor diff --git a/hbarmor/init.lua b/hbarmor/init.lua new file mode 100644 index 0000000..7a3cd74 --- /dev/null +++ b/hbarmor/init.lua @@ -0,0 +1,156 @@ +hbarmor = {} + +-- 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 enable_damage = minetest.setting_getbool("enable_damage") + +--[[load custom settings +local set = io.open(minetest.get_modpath("hbarmor").."/hbarmor.conf", "r") +if set then + dofile(minetest.get_modpath("hbarmor").."/hbarmor.conf") + set:close() +end--]] + +local must_hide = function(playername, arm) + return ((not armor.def[playername].count or armor.def[playername].count == 0) and arm == 0) +end + +local arm_printable = function(arm) + return math.ceil(math.floor(arm + 0.5)) +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) + end +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 + 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] = lvl * (items * (100 / max_items)) + +end + +-- END armor.lua + +-- update hud elemtens if value has changed +local function update_hud(player) + 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)) + hb.unhide_hudbar(player, "armor") + end + else + hb.change_hudbar(player, "armor", arm_printable(arm)) + end +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 enable_damage then + if main_timer > hbarmor.tick then main_timer = 0 end + for _,player in ipairs(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!") + end + -- update all hud elements + update_hud(player) + end + end + end + end +-- if timer > 4 then timer = 0 end +end) diff --git a/hbarmor/textures/hbarmor_bar.png b/hbarmor/textures/hbarmor_bar.png new file mode 100644 index 0000000..5c3654c Binary files /dev/null and b/hbarmor/textures/hbarmor_bar.png differ diff --git a/hbarmor/textures/hbarmor_icon.png b/hbarmor/textures/hbarmor_icon.png new file mode 100644 index 0000000..164ab10 Binary files /dev/null and b/hbarmor/textures/hbarmor_icon.png differ diff --git a/hbhunger/depends.txt b/hbhunger/depends.txt new file mode 100644 index 0000000..11cedaf --- /dev/null +++ b/hbhunger/depends.txt @@ -0,0 +1,29 @@ +hudbars +default? +animalmaterials? +bucket? +bushes? +bushes_classic? +cooking? +creatures? +docfarming? +dwarves? +ethereal? +farming? +farming_plus? +ferns? +fishing? +fruit? +glooptest? +jkanimals? +jkfarming? +jkwine? +kpgmobs? +mobfcooking? +mobs? +moretrees? +mtfoods? +mush45? +mushroom? +seaplants? +bakedclay? \ No newline at end of file diff --git a/hbhunger/hunger.lua b/hbhunger/hunger.lua new file mode 100644 index 0000000..be955a4 --- /dev/null +++ b/hbhunger/hunger.lua @@ -0,0 +1,446 @@ +-- 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) + 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({ + name = "hbhunger_eat_generic", + gain = 1 + }, { + pos = user:getpos(), + max_hear_distance = 16 + }) + + -- 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 + overwrite("default:apple", 2) +end +if minetest.get_modpath("farming") ~= nil then + overwrite("farming:bread", 4) +end +if minetest.get_modpath("flowers") ~= nil 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 + overwrite("mobs:cheese", 4) + overwrite("mobs:meat", 8) + overwrite("mobs:meat_raw", 4) + overwrite("mobs:rat_cooked", 4) + overwrite("mobs:honey", 2) + overwrite("mobs:pork_raw", 3, "", 3) + overwrite("mobs:pork_cooked", 8) + 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 + overwrite("mobs:meat", 6) + overwrite("mobs:meat_raw", 3) + overwrite("mobs:rat_cooked", 5) + end +end + +if minetest.get_modpath("moretrees") ~= nil then + overwrite("moretrees:coconut_milk", 1) + overwrite("moretrees:raw_coconut", 2) + overwrite("moretrees:acorn_muffin", 3) + overwrite("moretrees:spruce_nuts", 1) + overwrite("moretrees:pine_nuts", 1) + overwrite("moretrees:fir_nuts", 1) +end + +if minetest.get_modpath("dwarves") ~= nil then + overwrite("dwarves:beer", 2) + overwrite("dwarves:apple_cider", 1) + overwrite("dwarves:midus", 2) + overwrite("dwarves:tequila", 2) + overwrite("dwarves:tequila_with_lime", 2) + overwrite("dwarves:sake", 2) +end + +if minetest.get_modpath("animalmaterials") ~= nil then + overwrite("animalmaterials:milk", 2) + overwrite("animalmaterials:meat_raw", 3) + overwrite("animalmaterials:meat_pork", 3) + overwrite("animalmaterials:meat_beef", 3) + overwrite("animalmaterials:meat_chicken", 3) + overwrite("animalmaterials:meat_lamb", 3) + overwrite("animalmaterials:meat_venison", 3) + overwrite("animalmaterials:meat_undead", 3, "", 3) + overwrite("animalmaterials:meat_toxic", 3, "", 5) + overwrite("animalmaterials:meat_ostrich", 3) + overwrite("animalmaterials:fish_bluewhite", 2) + overwrite("animalmaterials:fish_clownfish", 2) +end + +if minetest.get_modpath("fishing") ~= nil then + overwrite("fishing:fish_raw", 2) + overwrite("fishing:fish_cooked", 5) + overwrite("fishing:sushi", 6) + overwrite("fishing:shark", 4) + overwrite("fishing:shark_cooked", 8) + overwrite("fishing:pike", 4) + overwrite("fishing:pike_cooked", 8) +end + +if minetest.get_modpath("glooptest") ~= nil then + overwrite("glooptest:kalite_lump", 1) +end + +if minetest.get_modpath("bushes") ~= nil then + overwrite("bushes:sugar", 1) + overwrite("bushes:strawberry", 2) + overwrite("bushes:berry_pie_raw", 3) + overwrite("bushes:berry_pie_cooked", 4) + overwrite("bushes:basket_pies", 15) +end + +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"} + for _, berry in ipairs(berries) do + if berry ~= "mixed_berry" then + overwrite("bushes:"..berry, 1) + end + 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 + 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 + overwrite("docfarming:carrot", 3) + overwrite("docfarming:cucumber", 2) + overwrite("docfarming:corn", 3) + overwrite("docfarming:potato", 4) + overwrite("docfarming:bakedpotato", 5) + overwrite("docfarming:raspberry", 3) +end + +if minetest.get_modpath("farming_plus") ~= nil then + overwrite("farming_plus:carrot_item", 3) + overwrite("farming_plus:banana", 2) + overwrite("farming_plus:orange_item", 2) + overwrite("farming:pumpkin_bread", 4) + overwrite("farming_plus:strawberry_item", 2) + overwrite("farming_plus:tomato_item", 2) + overwrite("farming_plus:potato_item", 4) + overwrite("farming_plus:rhubarb_item", 2) +end + +if minetest.get_modpath("mtfoods") ~= nil then + overwrite("mtfoods:dandelion_milk", 1) + overwrite("mtfoods:sugar", 1) + overwrite("mtfoods:short_bread", 4) + overwrite("mtfoods:cream", 1) + overwrite("mtfoods:chocolate", 2) + overwrite("mtfoods:cupcake", 2) + overwrite("mtfoods:strawberry_shortcake", 2) + overwrite("mtfoods:cake", 3) + overwrite("mtfoods:chocolate_cake", 3) + overwrite("mtfoods:carrot_cake", 3) + overwrite("mtfoods:pie_crust", 3) + overwrite("mtfoods:apple_pie", 3) + overwrite("mtfoods:rhubarb_pie", 2) + overwrite("mtfoods:banana_pie", 3) + overwrite("mtfoods:pumpkin_pie", 3) + overwrite("mtfoods:cookies", 2) + 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) + overwrite("mtfoods:apple_juice", 2) + overwrite("mtfoods:apple_cider", 2) + overwrite("mtfoods:cider_rack", 2) +end + +if minetest.get_modpath("fruit") ~= nil 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 + overwrite("mush45:meal", 4) +end + +if minetest.get_modpath("seaplants") ~= nil then + overwrite("seaplants:kelpgreen", 1) + overwrite("seaplants:kelpbrown", 1) + overwrite("seaplants:seagrassgreen", 1) + overwrite("seaplants:seagrassred", 1) + overwrite("seaplants:seasaladmix", 6) + overwrite("seaplants:kelpgreensalad", 1) + overwrite("seaplants:kelpbrownsalad", 1) + overwrite("seaplants:seagrassgreensalad", 1) + overwrite("seaplants:seagrassgreensalad", 1) +end + +if minetest.get_modpath("mobfcooking") ~= nil then + overwrite("mobfcooking:cooked_pork", 6) + overwrite("mobfcooking:cooked_ostrich", 6) + overwrite("mobfcooking:cooked_beef", 6) + overwrite("mobfcooking:cooked_chicken", 6) + overwrite("mobfcooking:cooked_lamb", 6) + overwrite("mobfcooking:cooked_venison", 6) + overwrite("mobfcooking:cooked_fish", 6) +end + +if minetest.get_modpath("creatures") ~= nil 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) + overwrite("ethereal:bamboo_sprout", 0, "", 3) + overwrite("ethereal:fern_tubers", 1) + overwrite("ethereal:banana_bread", 7) + overwrite("ethereal:mushroom_plant", 2) + overwrite("ethereal:coconut_slice", 2) + 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) + overwrite("ethereal:yellowleaves", 1, "", nil, 1) + overwrite("ethereal:sashimi", 4) + overwrite("ethereal:orange", 2) +end + +if minetest.get_modpath("farming") and farming.mod == "redo" then + overwrite("farming:bread", 6) + overwrite("farming:potato", 1) + overwrite("farming:baked_potato", 6) + overwrite("farming:cucumber", 4) + overwrite("farming:tomato", 4) + overwrite("farming:carrot", 3) + overwrite("farming:carrot_gold", 6, "", nil, 8) + overwrite("farming:corn", 3) + overwrite("farming:corn_cob", 5) + overwrite("farming:melon_slice", 2) + overwrite("farming:pumpkin_slice", 1) + overwrite("farming:pumpkin_bread", 9) + overwrite("farming:coffee_cup", 2, "farming:drinking_cup") + overwrite("farming:coffee_cup_hot", 3, "farming:drinking_cup", nil, 2) + overwrite("farming:cookie", 2) + overwrite("farming:chocolate_dark", 3) + overwrite("farming:donut", 4) + overwrite("farming:donut_chocolate", 6) + overwrite("farming:donut_apple", 6) + 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) +end + +if minetest.get_modpath("kpgmobs") ~= nil 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 + 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 + overwrite("jkanimals:meat", 6) +end + +if minetest.get_modpath("jkwine") ~= nil then + overwrite("jkwine:grapes", 2) + overwrite("jkwine:winebottle", 1) +end + +if minetest.get_modpath("cooking") ~= nil then + overwrite("cooking:meat_beef_cooked", 4) + overwrite("cooking:fish_bluewhite_cooked", 3) + overwrite("cooking:fish_clownfish_cooked", 1) + overwrite("cooking:meat_chicken_cooked", 2) + overwrite("cooking:meat_cooked", 2) + overwrite("cooking:meat_pork_cooked", 3) + overwrite("cooking:meat_toxic_cooked", -3) + overwrite("cooking:meat_venison_cooked", 3) + overwrite("cooking:meat_undead_cooked", 1) +end + +-- ferns mod of plantlife_modpack +if minetest.get_modpath("ferns") ~= nil then + overwrite("ferns:fiddlehead", 1, "", 1) + overwrite("ferns:fiddlehead_roasted", 3) + overwrite("ferns:ferntuber_roasted", 3) + overwrite("ferns:horsetail_01", 1) +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) + overwrite("xanadu:icecream_strawberry", 3) + overwrite("xanadu:icecream_melon", 4) + overwrite("xanadu:milkshake_strawberry", 3, "vessels:drinking_glass") + overwrite("xanadu:milkshake_banana", 4, "vessels:drinking_glass") + overwrite("xanadu:iced_coffee", 3, "vessels:drinking_glass") + overwrite("xanadu:pizza_slice", 3) + overwrite("xanadu:cupcake", 4) + overwrite("xanadu:juice_apple", 4, "vessels:drinking_glass") + overwrite("xanadu:juice_coconut", 4, "vessels:drinking_glass") + overwrite("xanadu:juice_orange", 4, "vessels:drinking_glass") + overwrite("xanadu:juice_cactus", 2, "vessels:drinking_glass") + overwrite("xanadu:hotchocolate", 6, "bucket:bucket_empty") + overwrite("xanadu:milk_chocolate", 3) + overwrite("xanadu:chocolate_donut", 3) + overwrite("xanadu:bacon", 4) + overwrite("xanadu:burger", 7) + overwrite("xanadu:fries", 6) + overwrite("xanadu:potato_salad", 8, "ethereal:bowl", nil, 2) +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 + +minetest.register_on_placenode(hbhunger.handle_node_actions) +minetest.register_on_dignode(hbhunger.handle_node_actions) diff --git a/hbhunger/init.lua b/hbhunger/init.lua new file mode 100644 index 0000000..bcfc677 --- /dev/null +++ b/hbhunger/init.lua @@ -0,0 +1,177 @@ +-- if damage enabled +if minetest.setting_getbool("enable_damage") then + +hbhunger = {} + +-- HUD statbar values +hbhunger.hunger = {} +hbhunger.hunger_out = {} + +-- 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_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 +HUNGER_EXHAUST_LVL = 160 -- at what exhaustion player satiation gets lowerd + + +--[[load custom settings +local set = io.open(minetest.get_modpath("hbhunger").."/hbhunger.conf", "r") +if set then + dofile(minetest.get_modpath("hbhunger").."/hbhunger.conf") + set:close() +end--]] + +local function custom_hud(player) + hb.init_hudbar(player, "satiation", hbhunger.get_hunger(player)) +end + +dofile(minetest.get_modpath("hbhunger").."/hunger.lua") + +-- register satiation hudbar +hb.register_hudbar( + "satiation", 0xFFFFFF, "Satiation", + { + icon = "hbhunger_icon.png", + bgicon = "hbhunger_bgicon.png", + bar = "hbhunger_bar.png" + }, + 20, 30, false +) + +-- update hud elemtents if value has changed +local function update_hud(player) + 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 + +hbhunger.set_hunger = 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 > 30 then value = 30 end + if value < 0 then value = 0 end + inv:set_stack("hunger", 1, ItemStack({name = ":", count = value + 1})) + return true +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 +end) + +local main_timer = 0 +local timer = 0 +local timer2 = 0 + +minetest.register_globalstep(function(dtime) + + main_timer = main_timer + dtime + timer = timer + dtime + timer2 = timer2 + dtime + + if main_timer > HUNGER_HUD_TICK + or timer > 4 + or timer2 > HUNGER_HUNGER_TICK then + + if main_timer > HUNGER_HUD_TICK then + main_timer = 0 + end + + 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 + + -- heal player by 1 hp if not dead and satiation is > 15 + 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 + end + end + + -- 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 + end + + -- update hud elements + update_hud(player) + + -- Determine if player is walking + 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) + end + + end + + end + + if timer > 4 then + timer = 0 + end + + if timer2 > HUNGER_HUNGER_TICK then + timer2 = 0 + end + +end) + +end -- end if damage enabled diff --git a/hbhunger/sounds/hbhunger_eat_generic.ogg b/hbhunger/sounds/hbhunger_eat_generic.ogg new file mode 100644 index 0000000..490fa6f Binary files /dev/null and b/hbhunger/sounds/hbhunger_eat_generic.ogg differ diff --git a/hbhunger/textures/hbhunger_bar.png b/hbhunger/textures/hbhunger_bar.png new file mode 100644 index 0000000..c94bf52 Binary files /dev/null and b/hbhunger/textures/hbhunger_bar.png differ diff --git a/hbhunger/textures/hbhunger_bgicon.png b/hbhunger/textures/hbhunger_bgicon.png new file mode 100644 index 0000000..07e21e7 Binary files /dev/null and b/hbhunger/textures/hbhunger_bgicon.png differ diff --git a/hbhunger/textures/hbhunger_icon.png b/hbhunger/textures/hbhunger_icon.png new file mode 100644 index 0000000..a5cc2a1 Binary files /dev/null and b/hbhunger/textures/hbhunger_icon.png differ diff --git a/depends.txt b/hudbars/depends.txt similarity index 100% rename from depends.txt rename to hudbars/depends.txt diff --git a/init.lua b/hudbars/init.lua similarity index 100% rename from init.lua rename to hudbars/init.lua diff --git a/textures/hudbars_bar_background.png b/hudbars/textures/hudbars_bar_background.png similarity index 100% rename from textures/hudbars_bar_background.png rename to hudbars/textures/hudbars_bar_background.png diff --git a/textures/hudbars_bar_breath.png b/hudbars/textures/hudbars_bar_breath.png similarity index 100% rename from textures/hudbars_bar_breath.png rename to hudbars/textures/hudbars_bar_breath.png diff --git a/textures/hudbars_bar_health.png b/hudbars/textures/hudbars_bar_health.png similarity index 100% rename from textures/hudbars_bar_health.png rename to hudbars/textures/hudbars_bar_health.png diff --git a/textures/hudbars_bgicon_health.png b/hudbars/textures/hudbars_bgicon_health.png similarity index 100% rename from textures/hudbars_bgicon_health.png rename to hudbars/textures/hudbars_bgicon_health.png diff --git a/textures/hudbars_icon_breath.png b/hudbars/textures/hudbars_icon_breath.png similarity index 100% rename from textures/hudbars_icon_breath.png rename to hudbars/textures/hudbars_icon_breath.png diff --git a/textures/hudbars_icon_health.png b/hudbars/textures/hudbars_icon_health.png similarity index 100% rename from textures/hudbars_icon_health.png rename to hudbars/textures/hudbars_icon_health.png diff --git a/modpack.txt b/modpack.txt new file mode 100644 index 0000000..e69de29