1 -- Keep these for backwards compatibility
2 function hbhunger
.save_hunger(player
)
3 hbhunger
.set_hunger_raw(player
)
5 function hbhunger
.load_hunger(player
)
6 hbhunger
.get_hunger_raw(player
)
9 -- wrapper for minetest.item_eat (this way we make sure other mods can't break this one)
10 local org_eat
= minetest
.do_item_eat
11 minetest
.do_item_eat
= function(hp_change
, replace_with_item
, itemstack
, user
, pointed_thing
)
12 local old_itemstack
= itemstack
13 itemstack
= hbhunger
.eat(hp_change
, replace_with_item
, itemstack
, user
, pointed_thing
)
14 for _
, callback
in pairs(minetest
.registered_on_item_eats
) do
15 local result
= callback(hp_change
, replace_with_item
, itemstack
, user
, pointed_thing
, old_itemstack
)
24 local food
= hbhunger
.food
26 function hbhunger
.register_food(name
, hunger_change
, replace_with_item
, poisen
, heal
, sound
)
28 food
[name
].saturation
= hunger_change
-- hunger points added
29 food
[name
].replace
= replace_with_item
-- what item is given back after eating
30 food
[name
].poisen
= poisen
-- time its poisening
31 food
[name
].healing
= heal
-- amount of HP
32 food
[name
].sound
= sound
-- special sound that is played when eating
35 function hbhunger
.eat(hp_change
, replace_with_item
, itemstack
, user
, pointed_thing
)
36 local item
= itemstack
:get_name()
37 local def
= food
[item
]
40 if type(hp_change
) ~= "number" then
42 minetest
.log("error", "Wrong on_use() definition for item '" .. item
.. "'")
44 def
.saturation
= hp_change
* 1.3
45 def
.replace
= replace_with_item
47 local func
= hbhunger
.item_eat(def
.saturation
, def
.replace
, def
.poisen
, def
.healing
, def
.sound
)
48 return func(itemstack
, user
, pointed_thing
)
52 local function poisenp(tick
, time
, time_left
, player
)
53 -- First check if player is still there
54 if not player
:is_player() then
57 time_left
= time_left
+ tick
58 if time_left
< time
then
59 minetest
.after(tick
, poisenp
, tick
, time
, time_left
, player
)
61 hbhunger
.poisonings
[player
:get_player_name()] = hbhunger
.poisonings
[player
:get_player_name()] - 1
62 if hbhunger
.poisonings
[player
:get_player_name()] <= 0 then
63 -- Reset HUD bar color
64 hb
.change_hudbar(player
, "health", nil, nil, "hudbars_icon_health.png", nil, "hudbars_bar_health.png")
67 if player
:get_hp()-1 > 0 then
68 player
:set_hp(player
:get_hp()-1)
73 function hbhunger
.item_eat(hunger_change
, replace_with_item
, poisen
, heal
, sound
)
74 return function(itemstack
, user
, pointed_thing
)
75 if itemstack
:take_item() ~= nil and user
~= nil then
76 local name
= user
:get_player_name()
77 local h
= tonumber(hbhunger
.hunger
[name
])
78 local hp
= user
:get_hp()
79 if h
== nil or hp
== nil then
82 if user
:is_player() then
83 local object
, object_pos
84 -- Check if user is a "fake player" (unofficial imitation of a the player data structure)
85 if type(user
) == "userdata" then
88 object_pos
= user
:get_pos()
91 {name
= sound
or "hbhunger_eat_generic",
95 max_hear_distance
= 16,
96 pitch
= 1 + math
.random(-10, 10)*0.005,},
102 if h
< hbhunger
.SAT_MAX
and hunger_change
then
103 h
= h
+ hunger_change
104 if h
> hbhunger
.SAT_MAX
then h
= hbhunger
.SAT_MAX
end
105 hbhunger
.hunger
[name
] = h
106 hbhunger
.set_hunger_raw(user
)
109 if hp
< 20 and heal
then
111 if hp
> 20 then hp
= 20 end
117 hb
.change_hudbar(user
, "health", nil, nil, "hbhunger_icon_health_poison.png", nil, "hbhunger_bar_health_poison.png")
118 hbhunger
.poisonings
[name
] = hbhunger
.poisonings
[name
] + 1
119 poisenp(1, poisen
, 0, user
)
122 if itemstack
:get_count() == 0 then
123 itemstack
:add_item(replace_with_item
)
125 local inv
= user
:get_inventory()
126 if inv
:room_for_item("main", replace_with_item
) then
127 inv
:add_item("main", replace_with_item
)
129 minetest
.add_item(user
:get_pos(), replace_with_item
)
137 -- player-action based hunger changes
138 function hbhunger
.handle_node_actions(pos
, oldnode
, player
, ext
)
139 -- is_fake_player comes from the pipeworks, we are not interested in those
140 if not player
or not player
:is_player() or player
.is_fake_player
== true then
143 local name
= player
:get_player_name()
144 local exhaus
= hbhunger
.exhaustion
[name
]
145 if exhaus
== nil then return end
146 local new
= hbhunger
.EXHAUST_PLACE
149 new
= hbhunger
.EXHAUST_DIG
151 -- assume its send by main timer when movement detected
152 if not pos
and not oldnode
then
153 new
= hbhunger
.EXHAUST_MOVE
155 exhaus
= exhaus
+ new
156 if exhaus
> hbhunger
.EXHAUST_LVL
then
158 local h
= tonumber(hbhunger
.hunger
[name
])
160 if h
< 0 then h
= 0 end
161 hbhunger
.hunger
[name
] = h
162 hbhunger
.set_hunger_raw(player
)
164 hbhunger
.exhaustion
[name
] = exhaus
167 minetest
.register_on_placenode(hbhunger
.handle_node_actions
)
168 minetest
.register_on_dignode(hbhunger
.handle_node_actions
)