Fix hunger crash (2)
[minetest_hbhunger/mttechnic_game.git] / init.lua
blob8b7eac061965f0bc58f6955eb5e69c99e475b823
1 if minetest.setting_getbool("enable_damage") then
3 hbhunger = {}
5 -- HUD statbar values
6 hbhunger.hunger = {}
7 hbhunger.hunger_out = {}
9 -- HUD item ids
10 local hunger_hud = {}
12 HUNGER_HUD_TICK = 0.1
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")
26 if set then
27 dofile(minetest.get_modpath("hbhunger").."/hbhunger.conf")
28 set:close()
29 end
31 local function custom_hud(player)
32 hb.init_hudbar(player, "satiation", hbhunger.get_hunger(player))
33 end
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()
43 --hunger
44 local h_out = tonumber(hbhunger.hunger_out[name])
45 local h = tonumber(hbhunger.hunger[name])
46 if h_out ~= h then
47 hbhunger.hunger_out[name] = h
48 hb.change_hudbar(player, "satiation", h)
49 end
50 end
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()
56 if hgp == 0 then
57 hgp = 21
58 inv:set_stack("hunger", 1, ItemStack({name=":", count=hgp}))
59 else
60 hgp = hgp
61 end
62 return hgp-1
63 end
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}))
75 return true
76 end
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
85 custom_hud(player)
86 hbhunger.set_hunger(player)
87 end)
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
95 end)
97 local main_timer = 0
98 local timer = 0
99 local timer2 = 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()
111 if timer > 4 then
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
114 player:set_hp(hp+1)
115 -- or damage player by 1 hp if satiation is < 2 (of 30)
116 elseif h <= 1 then
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
122 if h > 0 then
123 h = h-1
124 hbhunger.hunger[name] = h
125 hbhunger.set_hunger(player)
129 -- update all hud elements
130 update_hud(player)
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
141 end)