1 if minetest
.setting_getbool("enable_damage") then
7 hbhunger
.hunger_out
= {}
14 --Some hunger settings
15 hbhunger
.exhaustion
= {} -- Exhaustion is experimental!
17 HUNGER_HUNGER_TICK
= 800 -- time in seconds after that 1 hunger point is taken
18 HUNGER_EXHAUST_DIG
= 3 -- exhaustion increased this value after digged node
19 HUNGER_EXHAUST_PLACE
= 1 -- exhaustion increased this value after placed
20 HUNGER_EXHAUST_MOVE
= 0.3 -- exhaustion increased this value if player movement detected
21 HUNGER_EXHAUST_LVL
= 160 -- at what exhaustion player satiation gets lowerd
24 --load custom settings
25 local set
= io
.open(minetest
.get_modpath("hbhunger").."/hbhunger.conf", "r")
27 dofile(minetest
.get_modpath("hbhunger").."/hbhunger.conf")
31 local function custom_hud(player
)
32 hb
.init_hudbar(player
, "satiation", hbhunger
.get_hunger(player
))
35 dofile(minetest
.get_modpath("hbhunger").."/hunger.lua")
37 -- register satiation hudbar
38 hb
.register_hudbar("satiation", 0xFFFFFF, "Satiation", { icon
= "hbhunger_icon.png", bgicon
= "hbhunger_bgicon.png", bar
= "hbhunger_bar.png" }, 20, 30, false)
40 -- update hud elemtens if value has changed
41 local function update_hud(player
)
42 local name
= player
:get_player_name()
44 local h_out
= tonumber(hbhunger
.hunger_out
[name
])
45 local h
= tonumber(hbhunger
.hunger
[name
])
47 hbhunger
.hunger_out
[name
] = h
48 hb
.change_hudbar(player
, "satiation", h
)
52 hbhunger
.get_hunger
= function(player
)
53 local inv
= player
:get_inventory()
54 if not inv
then return nil end
55 local hgp
= inv
:get_stack("hunger", 1):get_count()
58 inv
:set_stack("hunger", 1, ItemStack({name
=":", count
=hgp
}))
65 hbhunger
.set_hunger
= function(player
)
66 local inv
= player
:get_inventory()
67 local name
= player
:get_player_name()
68 local value
= hbhunger
.hunger
[name
]
69 if not inv
or not value
then return nil end
70 if value
> 30 then value
= 30 end
71 if value
< 0 then value
= 0 end
73 inv
:set_stack("hunger", 1, ItemStack({name
=":", count
=value
+1}))
78 minetest
.register_on_joinplayer(function(player
)
79 local name
= player
:get_player_name()
80 local inv
= player
:get_inventory()
81 inv
:set_size("hunger",1)
82 hbhunger
.hunger
[name
] = hbhunger
.get_hunger(player
)
83 hbhunger
.hunger_out
[name
] = hbhunger
.hunger
[name
]
84 hbhunger
.exhaustion
[name
] = 0
86 hbhunger
.set_hunger(player
)
89 minetest
.register_on_respawnplayer(function(player
)
90 -- reset hunger (and save)
91 local name
= player
:get_player_name()
92 hbhunger
.hunger
[name
] = 20
93 hbhunger
.set_hunger(player
)
94 hbhunger
.exhaustion
[name
] = 0
100 minetest
.register_globalstep(function(dtime
)
101 main_timer
= main_timer
+ dtime
102 timer
= timer
+ dtime
103 timer2
= timer2
+ dtime
104 if main_timer
> HUNGER_HUD_TICK
or timer
> 4 or timer2
> HUNGER_HUNGER_TICK
then
105 if main_timer
> HUNGER_HUD_TICK
then main_timer
= 0 end
106 for _
,player
in ipairs(minetest
.get_connected_players()) do
107 local name
= player
:get_player_name()
109 local h
= tonumber(hbhunger
.hunger
[name
])
110 local hp
= player
:get_hp()
112 -- heal player by 1 hp if not dead and satiation is > 15 (of 30)
113 if h
> 15 and hp
> 0 and player
:get_breath() > 0 then
115 -- or damage player by 1 hp if satiation is < 2 (of 30)
117 if hp
-1 >= 0 then player
:set_hp(hp
-1) end
120 -- lower satiation by 1 point after xx seconds
121 if timer2
> HUNGER_HUNGER_TICK
then
124 hbhunger
.hunger
[name
] = h
125 hbhunger
.set_hunger(player
)
129 -- update all hud elements
132 local controls
= player
:get_player_control()
133 -- Determine if the player is walking
134 if controls
.up
or controls
.down
or controls
.left
or controls
.right
then
135 hbhunger
.handle_node_actions(nil, nil, player
)
139 if timer
> 4 then timer
= 0 end
140 if timer2
> HUNGER_HUNGER_TICK
then timer2
= 0 end