Added support for vitamin mod
[minetest_hbhunger/mttechnic_game.git] / init.lua
blobcadaa897dbe7c63fb6a4d9cc0a0de7d2d80bc473
1 local S
2 if (minetest.get_modpath("intllib")) then
3 S = intllib.Getter()
4 else
5 S = function ( s ) return s end
6 end
8 if minetest.settings:get_bool("enable_damage") then
10 hbhunger = {}
11 hbhunger.food = {}
13 -- HUD statbar values
14 hbhunger.hunger = {}
15 hbhunger.hunger_out = {}
17 -- Count number of poisonings a player has at once
18 hbhunger.poisonings = {}
20 -- HUD item ids
21 local hunger_hud = {}
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")
37 if set then
38 dofile(minetest.get_modpath("hbhunger").."/hbhunger.conf")
39 set:close()
40 end
42 local function custom_hud(player)
43 hb.init_hudbar(player, "satiation", hbhunger.get_hunger_raw(player))
44 end
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()
54 --hunger
55 local h_out = tonumber(hbhunger.hunger_out[name])
56 local h = tonumber(hbhunger.hunger[name])
57 if h_out ~= h then
58 hbhunger.hunger_out[name] = h
59 hb.change_hudbar(player, "satiation", h)
60 end
61 end
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()
67 if hgp == 0 then
68 hgp = 21
69 inv:set_stack("hunger", 1, ItemStack({name=":", count=hgp}))
70 else
71 hgp = hgp
72 end
73 return hgp-1
74 end
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}))
86 return true
87 end
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
97 custom_hud(player)
98 hbhunger.set_hunger_raw(player)
99 end)
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
107 end)
109 local main_timer = 0
110 local timer = 0
111 local timer2 = 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()
123 if timer > 4 then
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
126 player:set_hp(hp+1)
127 -- or damage player by 1 hp if satiation is < 2 (of 30)
128 elseif h <= 1 then
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
134 if h > 0 then
135 h = h-1
136 hbhunger.hunger[name] = h
137 hbhunger.set_hunger_raw(player)
141 -- update all hud elements
142 update_hud(player)
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
153 end)