From bed9ccaa65fcd68e55ff6e80b5b11610125d09b3 Mon Sep 17 00:00:00 2001 From: Wuzzy Date: Sat, 20 May 2017 16:20:03 +0200 Subject: [PATCH] Hunger: Add internal saturation and exhaustion --- mods/PLAYER/mcl_hunger/hunger.lua | 30 ++--------- mods/PLAYER/mcl_hunger/init.lua | 87 +++++++++++++++++++------------ 2 files changed, 58 insertions(+), 59 deletions(-) diff --git a/mods/PLAYER/mcl_hunger/hunger.lua b/mods/PLAYER/mcl_hunger/hunger.lua index 576298417..1e843a2e6 100644 --- a/mods/PLAYER/mcl_hunger/hunger.lua +++ b/mods/PLAYER/mcl_hunger/hunger.lua @@ -168,34 +168,15 @@ function mcl_hunger.item_eat(hunger_change, replace_with_item, poisen, heal, sou end -- player-action based hunger changes -function mcl_hunger.handle_node_actions(pos, oldnode, player, ext) +minetest.register_on_dignode(function(pos, oldnode, player) -- is_fake_player comes from the pipeworks, we are not interested in those if not player or not player:is_player() or player.is_fake_player == true then return end local name = player:get_player_name() - local exhaus = mcl_hunger.exhaustion[name] - if exhaus == nil then return end - local new = mcl_hunger.EXHAUST_PLACE - -- placenode event - if not ext then - new = mcl_hunger.EXHAUST_DIG - end - -- assume its send by main timer when movement detected - if not pos and not oldnode then - new = mcl_hunger.EXHAUST_MOVE - end - exhaus = exhaus + new - if exhaus > mcl_hunger.EXHAUST_LVL then - exhaus = 0 - local h = tonumber(mcl_hunger.hunger[name]) - h = h - 1 - if h < 0 then h = 0 end - mcl_hunger.hunger[name] = h - mcl_hunger.set_hunger_raw(player) - end - mcl_hunger.exhaustion[name] = exhaus -end + -- dig event + mcl_hunger.exhaust(name, mcl_hunger.EXHAUST_DIG) +end) -- Apply simple poison effect as long there are no real status effect -- TODO: Remove this when status effects are in place @@ -209,6 +190,3 @@ end if minetest.get_modpath("mcl_fishing") then mcl_hunger.register_food("mcl_fishing:pufferfish_raw", 0, "", 60) end - -minetest.register_on_placenode(mcl_hunger.handle_node_actions) -minetest.register_on_dignode(mcl_hunger.handle_node_actions) diff --git a/mods/PLAYER/mcl_hunger/init.lua b/mods/PLAYER/mcl_hunger/init.lua index ae90298f2..4813c7d31 100644 --- a/mods/PLAYER/mcl_hunger/init.lua +++ b/mods/PLAYER/mcl_hunger/init.lua @@ -23,30 +23,40 @@ local hunger_hud = {} mcl_hunger.HUD_TICK = 0.1 --Some hunger settings -mcl_hunger.exhaustion = {} -- Exhaustion is experimental! +mcl_hunger.exhaustion = {} +mcl_hunger.saturation = {} -mcl_hunger.HUNGER_TICK = 800 -- time in seconds after that 1 hunger point is taken -mcl_hunger.EXHAUST_DIG = 3 -- exhaustion increased this value after digged node -mcl_hunger.EXHAUST_PLACE = 1 -- exhaustion increased this value after placed -mcl_hunger.EXHAUST_MOVE = 0.3 -- exhaustion increased this value if player movement detected -mcl_hunger.EXHAUST_LVL = 160 -- at what exhaustion player satiation gets lowerd +-- Exhaustion increase +mcl_hunger.EXHAUST_DIG = 0.005 -- exhaustion increased this value after digged node +mcl_hunger.EXHAUST_JUMP = 0.05 -- jump +mcl_hunger.EXHAUST_SPRINT_JUMP = 0.2 -- jump while sprinting +mcl_hunger.EXHAUST_ATTACK = 0.1 -- attack +mcl_hunger.EXHAUST_SWIM = 0.01 -- player movement in water +mcl_hunger.EXHAUST_SPRINT = 0.1 -- sprint (per node) +mcl_hunger.EXHAUST_DAMAGE = 0.1 -- taking damage (protected by armor) +mcl_hunger.EXHAUST_REGEN = 6.0 -- Regenerate 1 HP +mcl_hunger.EXHAUST_LVL = 4.0 -- at what exhaustion player saturation gets lowerd --load custom settings local set = io.open(minetest.get_modpath("mcl_hunger").."/mcl_hunger.conf", "r") -if set then +if set then dofile(minetest.get_modpath("mcl_hunger").."/mcl_hunger.conf") set:close() end local function custom_hud(player) - hb.init_hudbar(player, "satiation", mcl_hunger.get_hunger_raw(player)) + hb.init_hudbar(player, "food", mcl_hunger.get_hunger_raw(player)) + hb.init_hudbar(player, "saturation", mcl_hunger.saturation[player:get_player_name()]) + hb.init_hudbar(player, "exhaustion", mcl_hunger.exhaustion[player:get_player_name()]) end dofile(minetest.get_modpath("mcl_hunger").."/hunger.lua") --- register satiation hudbar -hb.register_hudbar("satiation", 0xFFFFFF, S("Satiation"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 20, 20, false) +-- register saturation hudbar +hb.register_hudbar("food", 0xFFFFFF, S("Food"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 20, 20, false) +hb.register_hudbar("saturation", 0xFFFFFF, S("Saturation"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 5, 20, false, S("%s: %.1f/%d")) +hb.register_hudbar("exhaustion", 0xFFFFFF, S("Exhaustion"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 0, 4, false, S("%s: %.3f/%d")) -- update hud elemtens if value has changed local function update_hud(player) @@ -56,7 +66,7 @@ local function update_hud(player) local h = tonumber(mcl_hunger.hunger[name]) if h_out ~= h then mcl_hunger.hunger_out[name] = h - hb.change_hudbar(player, "satiation", h) + hb.change_hudbar(player, "food", h) end end @@ -109,7 +119,8 @@ minetest.register_on_joinplayer(function(player) inv:set_size("hunger",1) mcl_hunger.hunger[name] = mcl_hunger.get_hunger_raw(player) mcl_hunger.hunger_out[name] = mcl_hunger.hunger[name] - mcl_hunger.exhaustion[name] = 0 + mcl_hunger.exhaustion[name] = 0.0 + mcl_hunger.saturation[name] = 5.0 mcl_hunger.poisonings[name] = 0 custom_hud(player) mcl_hunger.set_hunger_raw(player) @@ -120,18 +131,25 @@ minetest.register_on_respawnplayer(function(player) local name = player:get_player_name() mcl_hunger.hunger[name] = 20 mcl_hunger.set_hunger_raw(player) - mcl_hunger.exhaustion[name] = 0 + mcl_hunger.exhaustion[name] = 0.0 + mcl_hunger.saturation[name] = 5.0 end) +function mcl_hunger.exhaust(playername, increase) + mcl_hunger.exhaustion[playername] = mcl_hunger.exhaustion[playername] + increase + if mcl_hunger.exhaustion[playername] > 4 then + mcl_hunger.exhaustion[playername] = 4 + end + hb.change_hudbar(minetest.get_player_by_name(playername), "exhaustion", mcl_hunger.exhaustion[playername]) +end + local main_timer = 0 local timer = 0 -- Half second timer -local timer2 = 0 local timerMult = 1 -- Cycles from 0 to 7, each time when timer hits half a second minetest.register_globalstep(function(dtime) main_timer = main_timer + dtime timer = timer + dtime - timer2 = timer2 + dtime - if main_timer > mcl_hunger.HUD_TICK or timer > 0.5 or timer2 > mcl_hunger.HUNGER_TICK then + if main_timer > mcl_hunger.HUD_TICK or timer > 0.5 then if main_timer > mcl_hunger.HUD_TICK then main_timer = 0 end for _,player in ipairs(minetest.get_connected_players()) do local name = player:get_player_name() @@ -141,19 +159,16 @@ minetest.register_globalstep(function(dtime) if timer > 0.5 then -- Quick heal (every 0.5s) if h >= 20 and hp > 0 and hp < 20 then - -- +1 HP, -3 food points + -- +1 HP, +6 exhaustion player:set_hp(hp+1) - h = h-3 - mcl_hunger.hunger[name] = h - mcl_hunger.set_hunger_raw(player) + mcl_hunger.exhaust(name, mcl_hunger.EXHAUST_REGEN) -- Slow heal, and hunger damage (every 4s) elseif timerMult == 0 then if h >= 18 and hp > 0 and hp < 20 then - -- +1 HP, -3 food points + -- +1 HP, +6 exhaustion player:set_hp(hp+1) - h = h-3 - mcl_hunger.hunger[name] = h - mcl_hunger.set_hunger_raw(player) + mcl_hunger.exhaust(name, mcl_hunger.EXHAUST_REGEN) + hb.change_hudbar(player, "exhaustion", mcl_hunger.exhaustion[name]) elseif h == 0 then -- Damage hungry player down to 1 HP if hp-1 >= 0 then @@ -161,12 +176,19 @@ minetest.register_globalstep(function(dtime) end end end - -- lower satiation by 1 point after xx seconds - if timer2 > mcl_hunger.HUNGER_TICK then - if h > 0 then - h = h-1 - mcl_hunger.hunger[name] = h - mcl_hunger.set_hunger_raw(player) + + if h > 0 then + if mcl_hunger.exhaustion[name] >= 4.0 then + if mcl_hunger.saturation[name] > 0.0 then + mcl_hunger.saturation[name] = mcl_hunger.saturation[name] - 1.0 + mcl_hunger.exhaustion[name] = 0.0 + hb.change_hudbar(player, "exhaustion", mcl_hunger.exhaustion[name]) + hb.change_hudbar(player, "saturation", mcl_hunger.saturation[name]) + else + h = h-1 + mcl_hunger.hunger[name] = h + mcl_hunger.set_hunger_raw(player) + end end end @@ -174,9 +196,9 @@ minetest.register_globalstep(function(dtime) update_hud(player) local controls = player:get_player_control() - -- Determine if the player is walking + -- Determine if the player is moving if controls.up or controls.down or controls.left or controls.right then - mcl_hunger.handle_node_actions(nil, nil, player) + -- TODO: Add exhaustion for moving in water end end end @@ -188,7 +210,6 @@ minetest.register_globalstep(function(dtime) timerMult = 0 end end - if timer2 > mcl_hunger.HUNGER_TICK then timer2 = 0 end end) end