2 if (minetest
.get_modpath("intllib")) then
5 S
= function ( s
) return s
end
8 if minetest
.settings
:get_bool("enable_damage") then
15 hbhunger
.hunger_out
= {}
17 -- Count number of poisonings a player has at once
18 hbhunger
.poisonings
= {}
23 hbhunger
.HUD_TICK
= 0.1
25 --Some hunger settings
26 hbhunger
.exhaustion
= {} -- Exhaustion is experimental!
28 hbhunger
.HUNGER_TICK
= 800 -- time in seconds after that 1 hunger point is taken
29 hbhunger
.EXHAUST_DIG
= 3 -- exhaustion increased this value after digged node
30 hbhunger
.EXHAUST_PLACE
= 1 -- exhaustion increased this value after placed
31 hbhunger
.EXHAUST_MOVE
= 0.3 -- exhaustion increased this value if player movement detected
32 hbhunger
.EXHAUST_LVL
= 160 -- at what exhaustion player satiation gets lowerd
35 --load custom settings
36 local set
= io
.open(minetest
.get_modpath("hbhunger").."/hbhunger.conf", "r")
38 dofile(minetest
.get_modpath("hbhunger").."/hbhunger.conf")
42 local function custom_hud(player
)
43 hb
.init_hudbar(player
, "satiation", hbhunger
.get_hunger_raw(player
))
46 dofile(minetest
.get_modpath("hbhunger").."/hunger.lua")
48 -- register satiation hudbar
49 hb
.register_hudbar("satiation", 0xFFFFFF, S("Satiation"), { icon
= "hbhunger_icon.png", bgicon
= "hbhunger_bgicon.png", bar
= "hbhunger_bar.png" }, 20, 30, false)
51 -- update hud elemtens if value has changed
52 local function update_hud(player
)
53 local name
= player
:get_player_name()
55 local h_out
= tonumber(hbhunger
.hunger_out
[name
])
56 local h
= tonumber(hbhunger
.hunger
[name
])
58 hbhunger
.hunger_out
[name
] = h
59 hb
.change_hudbar(player
, "satiation", h
)
63 hbhunger
.get_hunger_raw
= function(player
)
64 local inv
= player
:get_inventory()
65 if not inv
then return nil end
66 local hgp
= inv
:get_stack("hunger", 1):get_count()
69 inv
:set_stack("hunger", 1, ItemStack({name
=":", count
=hgp
}))
76 hbhunger
.set_hunger_raw
= function(player
)
77 local inv
= player
:get_inventory()
78 local name
= player
:get_player_name()
79 local value
= hbhunger
.hunger
[name
]
80 if not inv
or not value
then return nil end
81 if value
> 30 then value
= 30 end
82 if value
< 0 then value
= 0 end
84 inv
:set_stack("hunger", 1, ItemStack({name
=":", count
=value
+1}))
89 minetest
.register_on_joinplayer(function(player
)
90 local name
= player
:get_player_name()
91 local inv
= player
:get_inventory()
92 inv
:set_size("hunger",1)
93 hbhunger
.hunger
[name
] = hbhunger
.get_hunger_raw(player
)
94 hbhunger
.hunger_out
[name
] = hbhunger
.hunger
[name
]
95 hbhunger
.exhaustion
[name
] = 0
96 hbhunger
.poisonings
[name
] = 0
98 hbhunger
.set_hunger_raw(player
)
101 minetest
.register_on_respawnplayer(function(player
)
102 -- reset hunger (and save)
103 local name
= player
:get_player_name()
104 hbhunger
.hunger
[name
] = 20
105 hbhunger
.set_hunger_raw(player
)
106 hbhunger
.exhaustion
[name
] = 0
112 minetest
.register_globalstep(function(dtime
)
113 main_timer
= main_timer
+ dtime
114 timer
= timer
+ dtime
115 timer2
= timer2
+ dtime
116 if main_timer
> hbhunger
.HUD_TICK
or timer
> 4 or timer2
> hbhunger
.HUNGER_TICK
then
117 if main_timer
> hbhunger
.HUD_TICK
then main_timer
= 0 end
118 for _
,player
in ipairs(minetest
.get_connected_players()) do
119 local name
= player
:get_player_name()
121 local h
= tonumber(hbhunger
.hunger
[name
])
122 local hp
= player
:get_hp()
124 -- heal player by 1 hp if not dead and satiation is > 15 (of 30)
125 if h
> 15 and hp
> 0 and player
:get_breath() > 0 then
127 -- or damage player by 1 hp if satiation is < 2 (of 30)
129 if hp
-1 >= 0 then player
:set_hp(hp
-1) end
132 -- lower satiation by 1 point after xx seconds
133 if timer2
> hbhunger
.HUNGER_TICK
then
136 hbhunger
.hunger
[name
] = h
137 hbhunger
.set_hunger_raw(player
)
141 -- update all hud elements
144 local controls
= player
:get_player_control()
145 -- Determine if the player is walking
146 if controls
.up
or controls
.down
or controls
.left
or controls
.right
then
147 hbhunger
.handle_node_actions(nil, nil, player
)
151 if timer
> 4 then timer
= 0 end
152 if timer2
> hbhunger
.HUNGER_TICK
then timer2
= 0 end