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
Add hunger #3388
Changes from all commits
5b9d65d
a13ecef
5cc80c4
50b8de0
e6d6564
12b6d70
f364e72
3606b08
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
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() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,22 @@ | ||
-- Minetest: builtin/item.lua | ||
--------------------------------------------------------------------------------- | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
-- 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 { | ||
|
@@ -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() | ||
|
@@ -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 | ||
|
||
|
There was a problem hiding this comment.
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?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep :)