Change health bar color+icon on poisoning
[minetest_hbhunger.git] / init.lua
blob46158ac98fa592df5a3759e251c8b0cf855a5166
1 if minetest.setting_getbool("enable_damage") then
3 hbhunger = {}
4 hbhunger.food = {}
6 -- HUD statbar values
7 hbhunger.hunger = {}
8 hbhunger.hunger_out = {}
10 -- Count number of poisonings a player has at once
11 hbhunger.poisonings = {}
13 -- HUD item ids
14 local hunger_hud = {}
16 HUNGER_HUD_TICK = 0.1
18 --Some hunger settings
19 hbhunger.exhaustion = {} -- Exhaustion is experimental!
21 HUNGER_HUNGER_TICK = 800 -- time in seconds after that 1 hunger point is taken
22 HUNGER_EXHAUST_DIG = 3 -- exhaustion increased this value after digged node
23 HUNGER_EXHAUST_PLACE = 1 -- exhaustion increased this value after placed
24 HUNGER_EXHAUST_MOVE = 0.3 -- exhaustion increased this value if player movement detected
25 HUNGER_EXHAUST_LVL = 160 -- at what exhaustion player satiation gets lowerd
28 --load custom settings
29 local set = io.open(minetest.get_modpath("hbhunger").."/hbhunger.conf", "r")
30 if set then
31 dofile(minetest.get_modpath("hbhunger").."/hbhunger.conf")
32 set:close()
33 end
35 local function custom_hud(player)
36 hb.init_hudbar(player, "satiation", hbhunger.get_hunger(player))
37 end
39 dofile(minetest.get_modpath("hbhunger").."/hunger.lua")
41 -- register satiation hudbar
42 hb.register_hudbar("satiation", 0xFFFFFF, "Satiation", { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 20, 30, false)
44 -- update hud elemtens if value has changed
45 local function update_hud(player)
46 local name = player:get_player_name()
47 --hunger
48 local h_out = tonumber(hbhunger.hunger_out[name])
49 local h = tonumber(hbhunger.hunger[name])
50 if h_out ~= h then
51 hbhunger.hunger_out[name] = h
52 hb.change_hudbar(player, "satiation", h)
53 end
54 end
56 hbhunger.get_hunger = function(player)
57 local inv = player:get_inventory()
58 if not inv then return nil end
59 local hgp = inv:get_stack("hunger", 1):get_count()
60 if hgp == 0 then
61 hgp = 21
62 inv:set_stack("hunger", 1, ItemStack({name=":", count=hgp}))
63 else
64 hgp = hgp
65 end
66 return hgp-1
67 end
69 hbhunger.set_hunger = function(player)
70 local inv = player:get_inventory()
71 local name = player:get_player_name()
72 local value = hbhunger.hunger[name]
73 if not inv or not value then return nil end
74 if value > 30 then value = 30 end
75 if value < 0 then value = 0 end
77 inv:set_stack("hunger", 1, ItemStack({name=":", count=value+1}))
79 return true
80 end
82 minetest.register_on_joinplayer(function(player)
83 local name = player:get_player_name()
84 local inv = player:get_inventory()
85 inv:set_size("hunger",1)
86 hbhunger.hunger[name] = hbhunger.get_hunger(player)
87 hbhunger.hunger_out[name] = hbhunger.hunger[name]
88 hbhunger.exhaustion[name] = 0
89 hbhunger.poisonings[name] = 0
90 custom_hud(player)
91 hbhunger.set_hunger(player)
92 end)
94 minetest.register_on_respawnplayer(function(player)
95 -- reset hunger (and save)
96 local name = player:get_player_name()
97 hbhunger.hunger[name] = 20
98 hbhunger.set_hunger(player)
99 hbhunger.exhaustion[name] = 0
100 end)
102 local main_timer = 0
103 local timer = 0
104 local timer2 = 0
105 minetest.register_globalstep(function(dtime)
106 main_timer = main_timer + dtime
107 timer = timer + dtime
108 timer2 = timer2 + dtime
109 if main_timer > HUNGER_HUD_TICK or timer > 4 or timer2 > HUNGER_HUNGER_TICK then
110 if main_timer > HUNGER_HUD_TICK then main_timer = 0 end
111 for _,player in ipairs(minetest.get_connected_players()) do
112 local name = player:get_player_name()
114 local h = tonumber(hbhunger.hunger[name])
115 local hp = player:get_hp()
116 if timer > 4 then
117 -- heal player by 1 hp if not dead and satiation is > 15 (of 30)
118 if h > 15 and hp > 0 and player:get_breath() > 0 then
119 player:set_hp(hp+1)
120 -- or damage player by 1 hp if satiation is < 2 (of 30)
121 elseif h <= 1 then
122 if hp-1 >= 0 then player:set_hp(hp-1) end
125 -- lower satiation by 1 point after xx seconds
126 if timer2 > HUNGER_HUNGER_TICK then
127 if h > 0 then
128 h = h-1
129 hbhunger.hunger[name] = h
130 hbhunger.set_hunger(player)
134 -- update all hud elements
135 update_hud(player)
137 local controls = player:get_player_control()
138 -- Determine if the player is walking
139 if controls.up or controls.down or controls.left or controls.right then
140 hbhunger.handle_node_actions(nil, nil, player)
144 if timer > 4 then timer = 0 end
145 if timer2 > HUNGER_HUNGER_TICK then timer2 = 0 end
146 end)