Hunger: Add internal saturation and exhaustion

This commit is contained in:
Wuzzy 2017-05-20 16:20:03 +02:00
parent 10446881b4
commit bed9ccaa65
2 changed files with 58 additions and 59 deletions

View file

@ -168,34 +168,15 @@ function mcl_hunger.item_eat(hunger_change, replace_with_item, poisen, heal, sou
end end
-- player-action based hunger changes -- 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 -- 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 if not player or not player:is_player() or player.is_fake_player == true then
return return
end end
local name = player:get_player_name() local name = player:get_player_name()
local exhaus = mcl_hunger.exhaustion[name] -- dig event
if exhaus == nil then return end mcl_hunger.exhaust(name, mcl_hunger.EXHAUST_DIG)
local new = mcl_hunger.EXHAUST_PLACE end)
-- 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
-- Apply simple poison effect as long there are no real status effect -- Apply simple poison effect as long there are no real status effect
-- TODO: Remove this when status effects are in place -- TODO: Remove this when status effects are in place
@ -209,6 +190,3 @@ end
if minetest.get_modpath("mcl_fishing") then if minetest.get_modpath("mcl_fishing") then
mcl_hunger.register_food("mcl_fishing:pufferfish_raw", 0, "", 60) mcl_hunger.register_food("mcl_fishing:pufferfish_raw", 0, "", 60)
end end
minetest.register_on_placenode(mcl_hunger.handle_node_actions)
minetest.register_on_dignode(mcl_hunger.handle_node_actions)

View file

@ -23,13 +23,19 @@ local hunger_hud = {}
mcl_hunger.HUD_TICK = 0.1 mcl_hunger.HUD_TICK = 0.1
--Some hunger settings --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 -- Exhaustion increase
mcl_hunger.EXHAUST_DIG = 3 -- exhaustion increased this value after digged node mcl_hunger.EXHAUST_DIG = 0.005 -- exhaustion increased this value after digged node
mcl_hunger.EXHAUST_PLACE = 1 -- exhaustion increased this value after placed mcl_hunger.EXHAUST_JUMP = 0.05 -- jump
mcl_hunger.EXHAUST_MOVE = 0.3 -- exhaustion increased this value if player movement detected mcl_hunger.EXHAUST_SPRINT_JUMP = 0.2 -- jump while sprinting
mcl_hunger.EXHAUST_LVL = 160 -- at what exhaustion player satiation gets lowerd 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 --load custom settings
@ -40,13 +46,17 @@ if set then
end end
local function custom_hud(player) 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 end
dofile(minetest.get_modpath("mcl_hunger").."/hunger.lua") dofile(minetest.get_modpath("mcl_hunger").."/hunger.lua")
-- register satiation hudbar -- register saturation hudbar
hb.register_hudbar("satiation", 0xFFFFFF, S("Satiation"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 20, 20, false) 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 -- update hud elemtens if value has changed
local function update_hud(player) local function update_hud(player)
@ -56,7 +66,7 @@ local function update_hud(player)
local h = tonumber(mcl_hunger.hunger[name]) local h = tonumber(mcl_hunger.hunger[name])
if h_out ~= h then if h_out ~= h then
mcl_hunger.hunger_out[name] = h mcl_hunger.hunger_out[name] = h
hb.change_hudbar(player, "satiation", h) hb.change_hudbar(player, "food", h)
end end
end end
@ -109,7 +119,8 @@ minetest.register_on_joinplayer(function(player)
inv:set_size("hunger",1) inv:set_size("hunger",1)
mcl_hunger.hunger[name] = mcl_hunger.get_hunger_raw(player) mcl_hunger.hunger[name] = mcl_hunger.get_hunger_raw(player)
mcl_hunger.hunger_out[name] = mcl_hunger.hunger[name] 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 mcl_hunger.poisonings[name] = 0
custom_hud(player) custom_hud(player)
mcl_hunger.set_hunger_raw(player) mcl_hunger.set_hunger_raw(player)
@ -120,18 +131,25 @@ minetest.register_on_respawnplayer(function(player)
local name = player:get_player_name() local name = player:get_player_name()
mcl_hunger.hunger[name] = 20 mcl_hunger.hunger[name] = 20
mcl_hunger.set_hunger_raw(player) mcl_hunger.set_hunger_raw(player)
mcl_hunger.exhaustion[name] = 0 mcl_hunger.exhaustion[name] = 0.0
mcl_hunger.saturation[name] = 5.0
end) 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 main_timer = 0
local timer = 0 -- Half second timer 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 local timerMult = 1 -- Cycles from 0 to 7, each time when timer hits half a second
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
main_timer = main_timer + dtime main_timer = main_timer + dtime
timer = timer + dtime timer = timer + dtime
timer2 = timer2 + dtime if main_timer > mcl_hunger.HUD_TICK or timer > 0.5 then
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 then main_timer = 0 end if main_timer > mcl_hunger.HUD_TICK then main_timer = 0 end
for _,player in ipairs(minetest.get_connected_players()) do for _,player in ipairs(minetest.get_connected_players()) do
local name = player:get_player_name() local name = player:get_player_name()
@ -141,19 +159,16 @@ minetest.register_globalstep(function(dtime)
if timer > 0.5 then if timer > 0.5 then
-- Quick heal (every 0.5s) -- Quick heal (every 0.5s)
if h >= 20 and hp > 0 and hp < 20 then if h >= 20 and hp > 0 and hp < 20 then
-- +1 HP, -3 food points -- +1 HP, +6 exhaustion
player:set_hp(hp+1) player:set_hp(hp+1)
h = h-3 mcl_hunger.exhaust(name, mcl_hunger.EXHAUST_REGEN)
mcl_hunger.hunger[name] = h
mcl_hunger.set_hunger_raw(player)
-- Slow heal, and hunger damage (every 4s) -- Slow heal, and hunger damage (every 4s)
elseif timerMult == 0 then elseif timerMult == 0 then
if h >= 18 and hp > 0 and hp < 20 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) player:set_hp(hp+1)
h = h-3 mcl_hunger.exhaust(name, mcl_hunger.EXHAUST_REGEN)
mcl_hunger.hunger[name] = h hb.change_hudbar(player, "exhaustion", mcl_hunger.exhaustion[name])
mcl_hunger.set_hunger_raw(player)
elseif h == 0 then elseif h == 0 then
-- Damage hungry player down to 1 HP -- Damage hungry player down to 1 HP
if hp-1 >= 0 then if hp-1 >= 0 then
@ -161,12 +176,19 @@ minetest.register_globalstep(function(dtime)
end end
end end
end end
-- lower satiation by 1 point after xx seconds
if timer2 > mcl_hunger.HUNGER_TICK then if h > 0 then
if h > 0 then if mcl_hunger.exhaustion[name] >= 4.0 then
h = h-1 if mcl_hunger.saturation[name] > 0.0 then
mcl_hunger.hunger[name] = h mcl_hunger.saturation[name] = mcl_hunger.saturation[name] - 1.0
mcl_hunger.set_hunger_raw(player) 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
end end
@ -174,9 +196,9 @@ minetest.register_globalstep(function(dtime)
update_hud(player) update_hud(player)
local controls = player:get_player_control() 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 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 end
end end
@ -188,7 +210,6 @@ minetest.register_globalstep(function(dtime)
timerMult = 0 timerMult = 0
end end
end end
if timer2 > mcl_hunger.HUNGER_TICK then timer2 = 0 end
end) end)
end end