1
0
Fork 0

Get rid of those weird hunger_raw functions

This commit is contained in:
Wuzzy 2017-05-20 20:59:30 +02:00
parent 690e81c0fe
commit 26e13119cb
2 changed files with 21 additions and 69 deletions

View File

@ -1,11 +1,3 @@
-- Keep these for backwards compatibility
function mcl_hunger.save_hunger(player)
mcl_hunger.set_hunger_raw(player)
end
function mcl_hunger.load_hunger(player)
mcl_hunger.get_hunger_raw(player)
end
-- wrapper for minetest.item_eat (this way we make sure other mods can't break this one) -- wrapper for minetest.item_eat (this way we make sure other mods can't break this one)
local org_eat = core.do_item_eat local org_eat = core.do_item_eat
core.do_item_eat = function(hp_change, replace_with_item, itemstack, user, pointed_thing) core.do_item_eat = function(hp_change, replace_with_item, itemstack, user, pointed_thing)
@ -85,7 +77,7 @@ function mcl_hunger.item_eat(hunger_change, replace_with_item, poisen, heal, sou
local itemname = itemstack:get_name() local itemname = itemstack:get_name()
if itemstack:take_item() ~= nil and user ~= nil then if itemstack:take_item() ~= nil and user ~= nil then
local name = user:get_player_name() local name = user:get_player_name()
local h = tonumber(mcl_hunger.hunger[name]) local h = tonumber(mcl_hunger.get_hunger(user))
local hp = user:get_hp() local hp = user:get_hp()
local pos = user:getpos() local pos = user:getpos()
@ -152,8 +144,7 @@ function mcl_hunger.item_eat(hunger_change, replace_with_item, poisen, heal, sou
if h < 20 and hunger_change then if h < 20 and hunger_change then
h = h + hunger_change h = h + hunger_change
if h > 20 then h = 20 end if h > 20 then h = 20 end
mcl_hunger.hunger[name] = h mcl_hunger.set_hunger(user, h)
mcl_hunger.set_hunger_raw(user)
end end
hb.change_hudbar(user, "saturation", mcl_hunger.saturation[name], mcl_hunger.get_hunger(user)) hb.change_hudbar(user, "saturation", mcl_hunger.saturation[name], mcl_hunger.get_hunger(user))

View File

@ -10,10 +10,6 @@ if minetest.setting_getbool("enable_damage") then
mcl_hunger = {} mcl_hunger = {}
mcl_hunger.food = {} mcl_hunger.food = {}
-- HUD statbar values
mcl_hunger.hunger = {}
mcl_hunger.hunger_out = {}
-- Count number of poisonings a player has at once -- Count number of poisonings a player has at once
mcl_hunger.poisonings = {} mcl_hunger.poisonings = {}
@ -46,8 +42,8 @@ if set then
end end
local function custom_hud(player) local function custom_hud(player)
hb.init_hudbar(player, "food", mcl_hunger.get_hunger_raw(player)) hb.init_hudbar(player, "food", mcl_hunger.get_hunger(player))
hb.init_hudbar(player, "saturation", mcl_hunger.saturation[player:get_player_name()], mcl_hunger.get_hunger_raw(player)) hb.init_hudbar(player, "saturation", mcl_hunger.saturation[player:get_player_name()], mcl_hunger.get_hunger(player))
hb.init_hudbar(player, "exhaustion", mcl_hunger.exhaustion[player:get_player_name()]) hb.init_hudbar(player, "exhaustion", mcl_hunger.exhaustion[player:get_player_name()])
end end
@ -58,37 +54,8 @@ hb.register_hudbar("food", 0xFFFFFF, S("Food"), { icon = "hbhunger_icon.png", bg
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("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")) 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)
local name = player:get_player_name()
--hunger
local h_out = tonumber(mcl_hunger.hunger_out[name])
local h = tonumber(mcl_hunger.hunger[name])
if h_out ~= h then
mcl_hunger.hunger_out[name] = h
hb.change_hudbar(player, "food", h)
hb.change_hudbar(player, "saturation", nil, h)
end
end
-- API START -- -- API START --
mcl_hunger.get_hunger = function(player) mcl_hunger.get_hunger = function(player)
local name = player:get_player_name()
return mcl_hunger.hunger[name]
end
mcl_hunger.set_hunger = function(player, hunger)
local name = player:get_player_name()
mcl_hunger.hunger[name] = hunger
mcl_hunger.set_hunger_raw(player)
update_hud(player)
end
-- END OF API --
-- For internal use only. Don't use the “raw” functions outside of mcl_hunger!
mcl_hunger.get_hunger_raw = function(player)
local inv = player:get_inventory() local inv = player:get_inventory()
if not inv then return nil end if not inv then return nil end
local hgp = inv:get_stack("hunger", 1):get_count() local hgp = inv:get_stack("hunger", 1):get_count()
@ -101,38 +68,40 @@ mcl_hunger.get_hunger_raw = function(player)
return hgp-1 return hgp-1
end end
mcl_hunger.set_hunger_raw = function(player) mcl_hunger.set_hunger = function(player, hunger, update_hudbars)
local name = player:get_player_name()
local inv = player:get_inventory() local inv = player:get_inventory()
local name = player:get_player_name() local name = player:get_player_name()
local value = mcl_hunger.hunger[name] if not inv or not hunger then return nil end
if not inv or not value then return nil end if hunger > 20 then hunger = 20 end
if value > 20 then value = 20 end if hunger < 0 then hunger = 0 end
if value < 0 then value = 0 end
inv:set_stack("hunger", 1, ItemStack({name=":", count=value+1})) inv:set_stack("hunger", 1, ItemStack({name=":", count=hunger+1}))
return true
if update_hudbars ~= false then
hb.change_hudbar(player, "food", hunger)
hb.change_hudbar(player, "saturation", nil, hunger)
end end
end
-- END OF API --
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
local name = player:get_player_name() local name = player:get_player_name()
local inv = player:get_inventory() local inv = player:get_inventory()
inv:set_size("hunger",1) 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.0 mcl_hunger.exhaustion[name] = 0.0
mcl_hunger.saturation[name] = 5.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)
end) end)
minetest.register_on_respawnplayer(function(player) minetest.register_on_respawnplayer(function(player)
-- reset hunger (and save) -- reset hunger (and save)
local name = player:get_player_name() local name = player:get_player_name()
local h = 20 local h = 20
mcl_hunger.hunger[name] = h mcl_hunger.set_hunger(player, h, false)
mcl_hunger.set_hunger_raw(player)
mcl_hunger.exhaustion[name] = 0.0 mcl_hunger.exhaustion[name] = 0.0
mcl_hunger.saturation[name] = 5.0 mcl_hunger.saturation[name] = 5.0
hb.change_hudbar(player, "exhaustion", mcl_hunger.exhaustion[name]) hb.change_hudbar(player, "exhaustion", mcl_hunger.exhaustion[name])
@ -151,10 +120,9 @@ function mcl_hunger.exhaust(playername, increase)
mcl_hunger.saturation[playername] = math.max(mcl_hunger.saturation[playername] - 1.0, 0.0) mcl_hunger.saturation[playername] = math.max(mcl_hunger.saturation[playername] - 1.0, 0.0)
satuchanged = true satuchanged = true
elseif mcl_hunger.saturation[playername] < 0.0001 then elseif mcl_hunger.saturation[playername] < 0.0001 then
h = mcl_hunger.get_hunger_raw(player) h = mcl_hunger.get_hunger(player)
h = math.max(h-1, 0) h = math.max(h-1, 0)
mcl_hunger.hunger[playername] = h mcl_hunger.set_hunger(player, h)
mcl_hunger.set_hunger_raw(player)
satuchanged = true satuchanged = true
end end
if satuchanged then if satuchanged then
@ -183,7 +151,7 @@ minetest.register_globalstep(function(dtime)
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()
local h = tonumber(mcl_hunger.hunger[name]) local h = tonumber(mcl_hunger.get_hunger(player))
local hp = player:get_hp() local hp = player:get_hp()
if timer > 0.5 then if timer > 0.5 then
-- Quick heal (every 0.5s) -- Quick heal (every 0.5s)
@ -217,13 +185,6 @@ minetest.register_globalstep(function(dtime)
mcl_hunger.exhaust(name, mcl_hunger.EXHAUST_JUMP) mcl_hunger.exhaust(name, mcl_hunger.EXHAUST_JUMP)
end end
-- Reduce hunter if 0 saturation
if timerMult == 0 and h > 0 and mcl_hunger.saturation[name] < 0.0001 then
end
-- update all hud elements
update_hud(player)
end end
end end
end end