mirror of
https://git.minetest.land/VoxeLibre/VoxeLibre.git
synced 2024-11-04 23:31:05 +01:00
Use new player attribute API for mcl_hunger
This commit is contained in:
parent
d21eefbb78
commit
f1b99dc117
2 changed files with 23 additions and 54 deletions
|
@ -183,7 +183,7 @@ function mcl_hunger.item_eat(hunger_change, replace_with_item, poisontime, poiso
|
||||||
if not _mcl_saturation then
|
if not _mcl_saturation then
|
||||||
saturation = 0
|
saturation = 0
|
||||||
else
|
else
|
||||||
saturation = math.floor(minetest.registered_items[itemname]._mcl_saturation * 10)
|
saturation = minetest.registered_items[itemname]._mcl_saturation
|
||||||
end
|
end
|
||||||
mcl_hunger.saturate(name, saturation, false)
|
mcl_hunger.saturate(name, saturation, false)
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,7 @@ mcl_hunger.EXHAUST_DAMAGE = 100 -- TODO (mostly done): taking damage (protected
|
||||||
mcl_hunger.EXHAUST_REGEN = 6000 -- Regenerate 1 HP
|
mcl_hunger.EXHAUST_REGEN = 6000 -- Regenerate 1 HP
|
||||||
mcl_hunger.EXHAUST_LVL = 4000 -- at what exhaustion player saturation gets lowered
|
mcl_hunger.EXHAUST_LVL = 4000 -- at what exhaustion player saturation gets lowered
|
||||||
|
|
||||||
mcl_hunger.SATURATION_INIT = 50 -- Initial saturation for new/respawning players
|
mcl_hunger.SATURATION_INIT = 5 -- Initial saturation for new/respawning players
|
||||||
|
|
||||||
mcl_hunger.active = false
|
mcl_hunger.active = false
|
||||||
|
|
||||||
|
@ -41,7 +41,7 @@ end
|
||||||
Hunger values is identical to Minecraft's and ranges from 0 to 20.
|
Hunger values is identical to Minecraft's and ranges from 0 to 20.
|
||||||
Exhaustion and saturation values are stored as integers, unlike in Minecraft.
|
Exhaustion and saturation values are stored as integers, unlike in Minecraft.
|
||||||
Exhaustion is Minecraft exhaustion times 1000 and ranges from 0 to 4000.
|
Exhaustion is Minecraft exhaustion times 1000 and ranges from 0 to 4000.
|
||||||
Saturation is Minecraft exhaustion times 10 and ranges from 0 to 200.
|
Saturation is Minecraft saturation and ranges from 0 to 20.
|
||||||
|
|
||||||
Food saturation is stored in the custom item definition field _mcl_saturation.
|
Food saturation is stored in the custom item definition field _mcl_saturation.
|
||||||
This field uses the original Minecraft value.
|
This field uses the original Minecraft value.
|
||||||
|
@ -59,7 +59,7 @@ local hunger_hud = {}
|
||||||
local function init_hud(player)
|
local function init_hud(player)
|
||||||
hb.init_hudbar(player, "hunger", mcl_hunger.get_hunger(player))
|
hb.init_hudbar(player, "hunger", mcl_hunger.get_hunger(player))
|
||||||
if mcl_hunger.debug then
|
if mcl_hunger.debug then
|
||||||
hb.init_hudbar(player, "saturation", mcl_hunger.get_saturation(player), mcl_hunger.get_hunger(player)*10)
|
hb.init_hudbar(player, "saturation", mcl_hunger.get_saturation(player), mcl_hunger.get_hunger(player))
|
||||||
hb.init_hudbar(player, "exhaustion", mcl_hunger.get_exhaustion(player))
|
hb.init_hudbar(player, "exhaustion", mcl_hunger.get_exhaustion(player))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -67,11 +67,7 @@ end
|
||||||
-- HUD updating functions for Debug Mode. No-op if not in Debug Mode
|
-- HUD updating functions for Debug Mode. No-op if not in Debug Mode
|
||||||
function mcl_hunger.update_saturation_hud(player, saturation, hunger)
|
function mcl_hunger.update_saturation_hud(player, saturation, hunger)
|
||||||
if mcl_hunger.debug then
|
if mcl_hunger.debug then
|
||||||
local satulimit
|
hb.change_hudbar(player, "saturation", saturation, hunger)
|
||||||
if hunger then
|
|
||||||
satulimit = hunger * 10
|
|
||||||
end
|
|
||||||
hb.change_hudbar(player, "saturation", saturation, satulimit)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
function mcl_hunger.update_exhaustion_hud(player, exhaustion)
|
function mcl_hunger.update_exhaustion_hud(player, exhaustion)
|
||||||
|
@ -85,51 +81,26 @@ dofile(minetest.get_modpath("mcl_hunger").."/hunger.lua")
|
||||||
-- register saturation hudbar
|
-- register saturation hudbar
|
||||||
hb.register_hudbar("hunger", 0xFFFFFF, S("Food"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 20, 20, false)
|
hb.register_hudbar("hunger", 0xFFFFFF, S("Food"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 20, 20, false)
|
||||||
if mcl_hunger.debug then
|
if mcl_hunger.debug then
|
||||||
hb.register_hudbar("saturation", 0xFFFFFF, S("Saturation"), { icon = "mcl_hunger_icon_saturation.png", bgicon = "mcl_hunger_bgicon_saturation.png", bar = "mcl_hunger_bar_saturation.png" }, mcl_hunger.SATURATION_INIT, 200, false, S("%s: %d/%d"))
|
hb.register_hudbar("saturation", 0xFFFFFF, S("Saturation"), { icon = "mcl_hunger_icon_saturation.png", bgicon = "mcl_hunger_bgicon_saturation.png", bar = "mcl_hunger_bar_saturation.png" }, mcl_hunger.SATURATION_INIT, 200, false, S("%s: %.1f/%d"))
|
||||||
hb.register_hudbar("exhaustion", 0xFFFFFF, S("Exhaust."), { icon = "mcl_hunger_icon_exhaustion.png", bgicon = "mcl_hunger_bgicon_exhaustion.png", bar = "mcl_hunger_bar_exhaustion.png" }, 0, mcl_hunger.EXHAUST_LVL, false, S("%s: %d/%d"))
|
hb.register_hudbar("exhaustion", 0xFFFFFF, S("Exhaust."), { icon = "mcl_hunger_icon_exhaustion.png", bgicon = "mcl_hunger_bgicon_exhaustion.png", bar = "mcl_hunger_bar_exhaustion.png" }, 0, mcl_hunger.EXHAUST_LVL, false, S("%s: %d/%d"))
|
||||||
end
|
end
|
||||||
|
|
||||||
local RAW_VALUE_FOOD = 1
|
|
||||||
local RAW_VALUE_SATURATION = 2
|
|
||||||
local RAW_VALUE_EXHAUSTION = 3
|
|
||||||
|
|
||||||
local get_player_value_raw = function(player, id, default)
|
|
||||||
local inv = player:get_inventory()
|
|
||||||
if not inv then return nil end
|
|
||||||
local value = inv:get_stack("hunger", id):get_count()
|
|
||||||
if value == 0 then
|
|
||||||
inv:set_stack("hunger", id, ItemStack({name=":", count=default+1}))
|
|
||||||
return default
|
|
||||||
else
|
|
||||||
return value - 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local set_player_value_raw = function(player, id, value)
|
|
||||||
local inv = player:get_inventory()
|
|
||||||
inv:set_stack("hunger", id, ItemStack({name=":", count=value+1}))
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
|
|
||||||
-- API START --
|
-- API START --
|
||||||
mcl_hunger.get_hunger = function(player)
|
mcl_hunger.get_hunger = function(player)
|
||||||
return get_player_value_raw(player, RAW_VALUE_FOOD, 20)
|
return tonumber(player:get_attribute("mcl_hunger:hunger")) or 20
|
||||||
end
|
end
|
||||||
|
|
||||||
mcl_hunger.get_saturation = function(player)
|
mcl_hunger.get_saturation = function(player)
|
||||||
return get_player_value_raw(player, RAW_VALUE_SATURATION, 50)
|
return tonumber(player:get_attribute("mcl_hunger:saturation")) or mcl_hunger.SATURATION_INIT
|
||||||
end
|
end
|
||||||
|
|
||||||
mcl_hunger.get_exhaustion = function(player)
|
mcl_hunger.get_exhaustion = function(player)
|
||||||
return get_player_value_raw(player, RAW_VALUE_EXHAUSTION, 0)
|
return tonumber(player:get_attribute("mcl_hunger:exhaustion")) or 0
|
||||||
end
|
end
|
||||||
|
|
||||||
mcl_hunger.set_hunger = function(player, hunger, update_hudbars)
|
mcl_hunger.set_hunger = function(player, hunger, update_hudbars)
|
||||||
hunger = math.min(20, math.max(0, hunger))
|
hunger = math.min(20, math.max(0, hunger))
|
||||||
|
player:set_attribute("mcl_hunger:hunger", tostring(hunger))
|
||||||
local ok = set_player_value_raw(player, RAW_VALUE_FOOD, hunger)
|
|
||||||
if not ok then return false end
|
|
||||||
|
|
||||||
if update_hudbars ~= false then
|
if update_hudbars ~= false then
|
||||||
hb.change_hudbar(player, "hunger", hunger)
|
hb.change_hudbar(player, "hunger", hunger)
|
||||||
mcl_hunger.update_saturation_hud(player, nil, hunger)
|
mcl_hunger.update_saturation_hud(player, nil, hunger)
|
||||||
|
@ -138,11 +109,8 @@ mcl_hunger.set_hunger = function(player, hunger, update_hudbars)
|
||||||
end
|
end
|
||||||
|
|
||||||
mcl_hunger.set_saturation = function(player, saturation, update_hudbar)
|
mcl_hunger.set_saturation = function(player, saturation, update_hudbar)
|
||||||
saturation = math.min(mcl_hunger.get_hunger(player)*10, math.max(0, saturation))
|
saturation = math.min(mcl_hunger.get_hunger(player), math.max(0, saturation))
|
||||||
|
player:set_attribute("mcl_hunger:saturation", tostring(saturation))
|
||||||
local ok = set_player_value_raw(player, RAW_VALUE_SATURATION, saturation)
|
|
||||||
if not ok then return false end
|
|
||||||
|
|
||||||
if update_hudbar ~= false then
|
if update_hudbar ~= false then
|
||||||
mcl_hunger.update_saturation_hud(player, saturation)
|
mcl_hunger.update_saturation_hud(player, saturation)
|
||||||
end
|
end
|
||||||
|
@ -151,10 +119,7 @@ end
|
||||||
|
|
||||||
mcl_hunger.set_exhaustion = function(player, exhaustion, update_hudbar)
|
mcl_hunger.set_exhaustion = function(player, exhaustion, update_hudbar)
|
||||||
exhaustion = math.min(mcl_hunger.EXHAUST_LVL, math.max(0.0, exhaustion))
|
exhaustion = math.min(mcl_hunger.EXHAUST_LVL, math.max(0.0, exhaustion))
|
||||||
|
player:set_attribute("mcl_hunger:exhaustion", tostring(exhaustion))
|
||||||
local ok = set_player_value_raw(player, RAW_VALUE_EXHAUSTION, exhaustion)
|
|
||||||
if not ok then return false end
|
|
||||||
|
|
||||||
if update_hudbar ~= false then
|
if update_hudbar ~= false then
|
||||||
mcl_hunger.update_exhaustion_hud(player, exhaustion)
|
mcl_hunger.update_exhaustion_hud(player, exhaustion)
|
||||||
end
|
end
|
||||||
|
@ -164,14 +129,18 @@ end
|
||||||
|
|
||||||
|
|
||||||
-- END OF API --
|
-- END OF API --
|
||||||
|
minetest.register_on_newplayer(function(player)
|
||||||
|
local name = player:get_player_name()
|
||||||
|
mcl_hunger.set_hunger(player, 20, false)
|
||||||
|
mcl_hunger.set_saturation(player, mcl_hunger.SATURATION_INIT, false)
|
||||||
|
mcl_hunger.set_exhaustion(player, 0, false)
|
||||||
|
end)
|
||||||
|
|
||||||
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()
|
init_hud(player)
|
||||||
inv:set_size("hunger", 3)
|
|
||||||
mcl_hunger.poisonings[name] = 0
|
mcl_hunger.poisonings[name] = 0
|
||||||
mcl_hunger.last_eat[name] = -1
|
mcl_hunger.last_eat[name] = -1
|
||||||
init_hud(player)
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
minetest.register_on_respawnplayer(function(player)
|
minetest.register_on_respawnplayer(function(player)
|
||||||
|
@ -208,7 +177,7 @@ function mcl_hunger.exhaust(playername, increase)
|
||||||
local satuchanged = false
|
local satuchanged = false
|
||||||
local s = mcl_hunger.get_saturation(player)
|
local s = mcl_hunger.get_saturation(player)
|
||||||
if s > 0 then
|
if s > 0 then
|
||||||
mcl_hunger.set_saturation(player, math.max(s - 10, 0))
|
mcl_hunger.set_saturation(player, math.max(s - 1.0, 0))
|
||||||
satuchanged = true
|
satuchanged = true
|
||||||
elseif s <= 0.0001 then
|
elseif s <= 0.0001 then
|
||||||
h = mcl_hunger.get_hunger(player)
|
h = mcl_hunger.get_hunger(player)
|
||||||
|
@ -217,7 +186,7 @@ function mcl_hunger.exhaust(playername, increase)
|
||||||
satuchanged = true
|
satuchanged = true
|
||||||
end
|
end
|
||||||
if satuchanged then
|
if satuchanged then
|
||||||
if h ~= nil then h = h*10 end
|
if h ~= nil then h = h end
|
||||||
mcl_hunger.update_saturation_hud(player, mcl_hunger.get_saturation(player), h)
|
mcl_hunger.update_saturation_hud(player, mcl_hunger.get_saturation(player), h)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -227,7 +196,7 @@ end
|
||||||
|
|
||||||
function mcl_hunger.saturate(playername, increase, update_hudbar)
|
function mcl_hunger.saturate(playername, increase, update_hudbar)
|
||||||
local player = minetest.get_player_by_name(playername)
|
local player = minetest.get_player_by_name(playername)
|
||||||
mcl_hunger.set_saturation(player, math.min(mcl_hunger.get_saturation(player) + increase, mcl_hunger.get_hunger(player)*10))
|
mcl_hunger.set_saturation(player, math.min(mcl_hunger.get_saturation(player) + increase, mcl_hunger.get_hunger(player)))
|
||||||
if update_hudbar ~= false then
|
if update_hudbar ~= false then
|
||||||
mcl_hunger.update_saturation_hud(player, mcl_hunger.get_saturation(player), mcl_hunger.get_hunger(player))
|
mcl_hunger.update_saturation_hud(player, mcl_hunger.get_saturation(player), mcl_hunger.get_hunger(player))
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue