Abuse inv for hunger, no heal while drowning

no file flood anymore 8)
This commit is contained in:
BlockMen 2014-04-14 06:02:42 +02:00
parent d47b065d68
commit 9fb42bd469
2 changed files with 65 additions and 52 deletions

View File

@ -1,23 +1,12 @@
-- Keep these for backwards compatibility
function hud.save_hunger(player) function hud.save_hunger(player)
local file = io.open(minetest.get_worldpath().."/hud_"..player:get_player_name().."_hunger", "w+") hud.set_hunger(player)
if file then
file:write(hud.hunger[player:get_player_name()])
file:close()
end end
end
function hud.load_hunger(player) function hud.load_hunger(player)
local file = io.open(minetest.get_worldpath().."/hud_"..player:get_player_name().."_hunger", "r") hud.get_hunger(player)
if file then
hud.hunger[player:get_player_name()] = file:read("*all")
file:close()
return hud.hunger[player:get_player_name()]
else
return
end
end end
-- Poison player
local function poisenp(tick, time, time_left, player) local function poisenp(tick, time, time_left, player)
time_left = time_left + tick time_left = time_left + tick
if time_left < time then if time_left < time then
@ -31,12 +20,13 @@ end
function hud.item_eat(hunger_change, replace_with_item, poisen) function hud.item_eat(hunger_change, replace_with_item, poisen)
return function(itemstack, user, pointed_thing) return function(itemstack, user, pointed_thing)
if itemstack:take_item() ~= nil then if itemstack:take_item() ~= nil and user ~= nil then
local h = tonumber(hud.hunger[user:get_player_name()]) local name = user:get_player_name()
local h = tonumber(hud.hunger[name])
h=h+hunger_change h=h+hunger_change
if h>30 then h=30 end if h>30 then h=30 end
hud.hunger[user:get_player_name()]=h hud.hunger[name]=h
hud.save_hunger(user) hud.set_hunger(user)
itemstack:add_item(replace_with_item) -- note: replace_with_item is optional itemstack:add_item(replace_with_item) -- note: replace_with_item is optional
--sound:eat --sound:eat
if poisen then if poisen then

View File

@ -15,14 +15,8 @@ local air_hud = {}
local armor_hud = {} local armor_hud = {}
local armor_hud_bg = {} local armor_hud_bg = {}
local SAVE_INTERVAL = 0.5*60--currently useless
-- default settings -- default settings
HUD_ENABLE_HUNGER = minetest.setting_getbool("hud_hunger_enable") -- statbar positions
HUD_SHOW_ARMOR = false
if minetest.get_modpath("3d_armor") ~= nil then HUD_SHOW_ARMOR = true end
if HUD_ENABLE_HUNGER == nil then HUD_ENABLE_HUNGER = minetest.setting_getbool("enable_damage") end
HUD_HUNGER_TICK = 300
HUD_HEALTH_POS = {x=0.5,y=0.9} HUD_HEALTH_POS = {x=0.5,y=0.9}
HUD_HEALTH_OFFSET = {x=-175, y=2} HUD_HEALTH_OFFSET = {x=-175, y=2}
HUD_HUNGER_POS = {x=0.5,y=0.9} HUD_HUNGER_POS = {x=0.5,y=0.9}
@ -32,7 +26,19 @@ HUD_AIR_OFFSET = {x=15,y=-15}
HUD_ARMOR_POS = {x=0.5,y=0.9} HUD_ARMOR_POS = {x=0.5,y=0.9}
HUD_ARMOR_OFFSET = {x=-175, y=-15} HUD_ARMOR_OFFSET = {x=-175, y=-15}
--load costum settings HUD_HUNGER_TICK = 300
HUD_ENABLE_HUNGER = minetest.setting_getbool("hud_hunger_enable")
if HUD_ENABLE_HUNGER == nil then
HUD_ENABLE_HUNGER = minetest.setting_getbool("enable_damage")
end
HUD_SHOW_ARMOR = false
if minetest.get_modpath("3d_armor") ~= nil then
HUD_SHOW_ARMOR = true
end
--load custom settings
local set = io.open(minetest.get_modpath("hud").."/hud.conf", "r") local set = io.open(minetest.get_modpath("hud").."/hud.conf", "r")
if set then if set then
dofile(minetest.get_modpath("hud").."/hud.conf") dofile(minetest.get_modpath("hud").."/hud.conf")
@ -43,14 +49,12 @@ else
end end
end end
--minetest.after(SAVE_INTERVAL, timer, SAVE_INTERVAL)
local function hide_builtin(player) local function hide_builtin(player)
player:hud_set_flags({crosshair = true, hotbar = true, healthbar = false, wielditem = true, breathbar = false}) player:hud_set_flags({crosshair = true, hotbar = true, healthbar = false, wielditem = true, breathbar = false})
end end
local function costum_hud(player) local function custom_hud(player)
local name = player:get_player_name() local name = player:get_player_name()
--fancy hotbar --fancy hotbar
@ -71,13 +75,14 @@ local function costum_hud(player)
alignment = {x=-1,y=-1}, alignment = {x=-1,y=-1},
offset = HUD_HUNGER_OFFSET, offset = HUD_HUNGER_OFFSET,
}) })
local h = hud.hunger[name]
if h == nil or h > 20 then h = 20 end
hunger_hud[name] = player:hud_add({ hunger_hud[name] = player:hud_add({
hud_elem_type = "statbar", hud_elem_type = "statbar",
position = HUD_HUNGER_POS, position = HUD_HUNGER_POS,
scale = {x=1, y=1}, scale = {x=1, y=1},
text = "hud_hunger_fg.png", text = "hud_hunger_fg.png",
number = 20, number = h,
alignment = {x=-1,y=-1}, alignment = {x=-1,y=-1},
offset = HUD_HUNGER_OFFSET, offset = HUD_HUNGER_OFFSET,
}) })
@ -92,7 +97,6 @@ local function costum_hud(player)
alignment = {x=-1,y=-1}, alignment = {x=-1,y=-1},
offset = HUD_HEALTH_OFFSET, offset = HUD_HEALTH_OFFSET,
}) })
health_hud[name] = player:hud_add({ health_hud[name] = player:hud_add({
hud_elem_type = "statbar", hud_elem_type = "statbar",
position = HUD_HEALTH_POS, position = HUD_HEALTH_POS,
@ -125,7 +129,6 @@ local function costum_hud(player)
alignment = {x=-1,y=-1}, alignment = {x=-1,y=-1},
offset = HUD_ARMOR_OFFSET, offset = HUD_ARMOR_OFFSET,
}) })
armor_hud[name] = player:hud_add({ armor_hud[name] = player:hud_add({
hud_elem_type = "statbar", hud_elem_type = "statbar",
position = HUD_ARMOR_POS, position = HUD_ARMOR_POS,
@ -137,7 +140,6 @@ local function costum_hud(player)
}) })
end end
end end
end end
--needs to be defined for older version of 3darmor --needs to be defined for older version of 3darmor
@ -174,6 +176,7 @@ local function update_hud(player)
if arm_out ~= arm then if arm_out ~= arm then
hud.armor_out[name] = arm hud.armor_out[name] = arm
player:hud_change(armor_hud[name], "number", arm) player:hud_change(armor_hud[name], "number", arm)
-- hide armor bar completely when there is none
if (not armor.def[name].count or armor.def[name].count == 0) and arm == 0 then if (not armor.def[name].count or armor.def[name].count == 0) and arm == 0 then
player:hud_change(armor_hud_bg[name], "number", 0) player:hud_change(armor_hud_bg[name], "number", 0)
else else
@ -191,36 +194,56 @@ local function update_hud(player)
end end
end end
local function timer(interval, player) hud.get_hunger = function(player)
if interval > 0 then local inv = player:get_inventory()
hud.save_hunger(player) if not inv then return nil end
minetest.after(interval, timer, interval, player) 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 end
return hgp-1
end
hud.set_hunger = function(player)
local inv = player:get_inventory()
local name = player:get_player_name()
local value = hud.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 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()
inv:set_size("hunger",1)
hud.health[name] = player:get_hp() hud.health[name] = player:get_hp()
local air = player:get_breath() if HUD_ENABLE_HUNGER then
hud.air[name] = air hud.hunger[name] = hud.get_hunger(player)
if HUD_ENABLE_HUNGER then hud.hunger[name] = hud.load_hunger(player) end
if not hud.hunger[name] then
hud.hunger[name] = 20
end
hud.hunger_out[name] = hud.hunger[name] hud.hunger_out[name] = hud.hunger[name]
end
hud.armor[name] = 0 hud.armor[name] = 0
hud.armor_out[name] = 0 hud.armor_out[name] = 0
local air = player:get_breath()
hud.air[name] = air
minetest.after(0.5, function() minetest.after(0.5, function()
hide_builtin(player) hide_builtin(player)
costum_hud(player) custom_hud(player)
if HUD_ENABLE_HUNGER then hud.save_hunger(player) end if HUD_ENABLE_HUNGER then hud.set_hunger(player) end
end) end)
end) end)
minetest.register_on_respawnplayer(function(player) minetest.register_on_respawnplayer(function(player)
hud.hunger[player:get_player_name()] = 20 hud.hunger[player:get_player_name()] = 20
minetest.after(0.5, function() minetest.after(0.5, function()
if HUD_ENABLE_HUNGER then hud.save_hunger(player) end if HUD_ENABLE_HUNGER then hud.set_hunger(player) end
end) end)
end) end)
@ -239,7 +262,7 @@ minetest.after(2.5, function()
local hp = player:get_hp() local hp = player:get_hp()
if HUD_ENABLE_HUNGER and timer > 4 then if HUD_ENABLE_HUNGER and timer > 4 then
-- heal player by 1 hp if not dead and saturation is > 15 (of 30) -- heal player by 1 hp if not dead and saturation is > 15 (of 30)
if h > 15 and hp > 0 then if h > 15 and hp > 0 and hud.air[name] > 0 then
player:set_hp(hp+1) player:set_hp(hp+1)
-- or damage player by 1 hp if saturation is < 2 (of 30) and player would not die -- or damage player by 1 hp if saturation is < 2 (of 30) and player would not die
elseif h <= 1 and minetest.setting_getbool("enable_damage") then elseif h <= 1 and minetest.setting_getbool("enable_damage") then
@ -248,10 +271,10 @@ minetest.after(2.5, function()
end end
-- lower saturation by 1 point after xx seconds -- lower saturation by 1 point after xx seconds
if HUD_ENABLE_HUNGER and timer2 > HUD_HUNGER_TICK then if HUD_ENABLE_HUNGER and timer2 > HUD_HUNGER_TICK then
if h > 1 then if h > 0 then
h = h-1 h = h-1
hud.hunger[name] = h hud.hunger[name] = h
hud.save_hunger(player) hud.set_hunger(player)
end end
end end
-- update current armor level -- update current armor level