Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hunger #3388

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
111 changes: 111 additions & 0 deletions builtin/game/hunger.lua
@@ -0,0 +1,111 @@
---------------------------------------------------------------------------------
-- Minetest
-- Copyright (C) 2015 BlockMen <blockmen2015@gmail.com>
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation; either version 2.1 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public License along
-- with this program; if not, write to the Free Software Foundation, Inc.,
-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
---------------------------------------------------------------------------------


-- Dont do anything when no damage or hunger
if core.setting_getbool("enable_damage") ~= true then
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you forget to add a check for enable_hunger or something similar here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep :)

return
end


local hunger_step = 300 -- time in seconds until player looses 1 hunger point
local healing_step = 4 -- Time in seconds after player gets 1 HP (when healing)
local healing = {}


function core.heal_timer()
for player_name, check in pairs(healing) do
if check == true then
local player = core.get_player_by_name(player_name)
if player and player:is_player() then
player:set_hp(player:get_hp() + 1)
end
end

end

core.after(healing_step, core.heal_timer)
end


function core.hunger_timer(player, hunger_change)
player:set_hunger(player:get_hunger() - hunger_change)
core.after(hunger_step, core.hunger_timer, player, hunger_change)
end


local function check_healing_state(player)
assert(player:is_player())

local name = player:get_player_name()
if not name or name == "" then
return
end

local is_healing = (healing[name] == true)

if player:get_hunger() > 3 and player:get_breath() > 0 then
if not is_healing then
healing[name] = true
end
else
healing[name] = nil
end
end


function core.hunger_event_handler(player, eventname)
assert(player:is_player())

local name = player:get_player_name()
if not name or name == "" then
return
end

if eventname == "health_changed" then
local hp = player:get_hp()
if hp > 19 then
healing[name] = nil
return
else
check_healing_state(player)
end
end

if eventname == "breath_changed" or
eventname == "hunger_changed" then
check_healing_state(player)
end
end


core.register_on_joinplayer(function(player)
core.after(hunger_step, core.hunger_timer, player, 1)
end)
core.register_on_leaveplayer(function(player)
local name = player:get_player_name()
if not name or name == "" then
return
end
healing[name] = nil
end)
core.register_playerevent(core.hunger_event_handler)

-- Init healing timer
core.heal_timer()
22 changes: 22 additions & 0 deletions builtin/game/init.lua
@@ -1,3 +1,21 @@
---------------------------------------------------------------------------------
-- Minetest
-- Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation; either version 2.1 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public License along
-- with this program; if not, write to the Free Software Foundation, Inc.,
-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
---------------------------------------------------------------------------------

local scriptpath = core.get_builtin_path()..DIR_DELIM
local commonpath = scriptpath.."common"..DIR_DELIM
Expand Down Expand Up @@ -26,3 +44,7 @@ dofile(gamepath.."features.lua")
dofile(gamepath.."voxelarea.lua")
dofile(gamepath.."forceloading.lua")
dofile(gamepath.."statbars.lua")

if core.setting_getbool("enable_hunger") then
dofile(gamepath .. "hunger.lua")
end
46 changes: 39 additions & 7 deletions builtin/game/item.lua
@@ -1,4 +1,22 @@
-- Minetest: builtin/item.lua
---------------------------------------------------------------------------------
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the files in builtin/ have a copyright header. And if you add one, can you at least watch out that the year matches the current one?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats not right. Only builtin/game/ has no headers yet, everywhere else are often some. In mainmenu 99% have.
I would have said that it is the state of 2013, but ok, i can change it to 2015. Also moving it out of this PR might be good.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BlockMen lets discuss it in #3390 . And if we change, lets change it for the whole builtin/ directory.

-- Minetest
-- Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
--
-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation; either version 2.1 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public License along
-- with this program; if not, write to the Free Software Foundation, Inc.,
-- 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
---------------------------------------------------------------------------------


local function copy_pointed_thing(pointed_thing)
return {
Expand Down Expand Up @@ -375,22 +393,31 @@ function core.item_drop(itemstack, dropper, pos)
-- environment failed
end

function core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
function core.do_item_eat(hunger_change, replace_with_item, itemstack, user, pointed_thing, hp_change, eat_sound)
for _, callback in pairs(core.registered_on_item_eats) do
local result = callback(hp_change, replace_with_item, itemstack, user, pointed_thing)
local result = callback(hunger_change, replace_with_item, itemstack, user, pointed_thing, hp_change)
if result then
return result
end
end
if itemstack:take_item() ~= nil then
user:set_hp(user:get_hp() + hp_change)
user:set_hunger(user:get_hunger() + hunger_change)
if hp_change then
user:set_hp(user:get_hp() + hp_change)
end

-- eating sound
if not eat_sound then
eat_sound = "item_eat"
end
core.sound_play(eat_sound, {to_player = user:get_player_name(), gain = 0.7})

if replace_with_item then
if itemstack:is_empty() then
itemstack:add_item(replace_with_item)
else
local inv = user:get_inventory()
if inv:room_for_item("main", {name=replace_with_item}) then
if inv:room_for_item("main", {name = replace_with_item}) then
inv:add_item("main", replace_with_item)
else
local pos = user:getpos()
Expand All @@ -403,9 +430,14 @@ function core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed
return itemstack
end

function core.item_eat(hp_change, replace_with_item)
function core.item_eat(hunger_change, replace_with_item, hp_change, eat_sound)
-- With hunger disabled food has to heal (fallback to old behavior)
if core.is_yes(core.setting_getbool("enable_hunger")) == false and not hp_change then
hp_change = hunger_change
end
return function(itemstack, user, pointed_thing) -- closure
return core.do_item_eat(hp_change, replace_with_item, itemstack, user, pointed_thing)
return core.do_item_eat(hunger_change, replace_with_item, itemstack,
user, pointed_thing, hp_change, eat_sound)
end
end

Expand Down