Translation into portuguese
[minetest_hbhunger/brunomine.git] / init.lua
blobc9e7063831c80eb2673223256b98dc9d73063a35
1 local S
2 if (minetest.get_modpath("intllib")) then
3 dofile(minetest.get_modpath("intllib").."/intllib.lua")
4 S = intllib.Getter(minetest.get_current_modname())
5 else
6 S = function ( s ) return s end
7 end
9 if minetest.setting_getbool("enable_damage") then
11 hbhunger = {}
12 hbhunger.food = {}
14 -- HUD statbar values
15 hbhunger.hunger = {}
16 hbhunger.hunger_out = {}
18 -- Count number of poisonings a player has at once
19 hbhunger.poisonings = {}
21 -- HUD item ids
22 local hunger_hud = {}
24 hbhunger.HUD_TICK = 0.1
26 --Some hunger settings
27 hbhunger.exhaustion = {} -- Exhaustion is experimental!
29 hbhunger.HUNGER_TICK = 800 -- time in seconds after that 1 hunger point is taken
30 hbhunger.EXHAUST_DIG = 3 -- exhaustion increased this value after digged node
31 hbhunger.EXHAUST_PLACE = 1 -- exhaustion increased this value after placed
32 hbhunger.EXHAUST_MOVE = 0.3 -- exhaustion increased this value if player movement detected
33 hbhunger.EXHAUST_LVL = 160 -- at what exhaustion player satiation gets lowerd
36 --load custom settings
37 local set = io.open(minetest.get_modpath("hbhunger").."/hbhunger.conf", "r")
38 if set then
39 dofile(minetest.get_modpath("hbhunger").."/hbhunger.conf")
40 set:close()
41 end
43 local function custom_hud(player)
44 hb.init_hudbar(player, "satiation", hbhunger.get_hunger_raw(player))
45 end
47 dofile(minetest.get_modpath("hbhunger").."/hunger.lua")
49 -- register satiation hudbar
50 hb.register_hudbar("satiation", 0xFFFFFF, S("Satiation"), { icon = "hbhunger_icon.png", bgicon = "hbhunger_bgicon.png", bar = "hbhunger_bar.png" }, 20, 30, false)
52 -- update hud elemtens if value has changed
53 local function update_hud(player)
54 local name = player:get_player_name()
55 --hunger
56 local h_out = tonumber(hbhunger.hunger_out[name])
57 local h = tonumber(hbhunger.hunger[name])
58 if h_out ~= h then
59 hbhunger.hunger_out[name] = h
60 hb.change_hudbar(player, "satiation", h)
61 end
62 end
64 hbhunger.get_hunger_raw = function(player)
65 local inv = player:get_inventory()
66 if not inv then return nil end
67 local hgp = inv:get_stack("hunger", 1):get_count()
68 if hgp == 0 then
69 hgp = 21
70 inv:set_stack("hunger", 1, ItemStack({name=":", count=hgp}))
71 else
72 hgp = hgp
73 end
74 return hgp-1
75 end
77 hbhunger.set_hunger_raw = function(player)
78 local inv = player:get_inventory()
79 local name = player:get_player_name()
80 local value = hbhunger.hunger[name]
81 if not inv or not value then return nil end
82 if value > 30 then value = 30 end
83 if value < 0 then value = 0 end
85 inv:set_stack("hunger", 1, ItemStack({name=":", count=value+1}))
87 return true
88 end
90 minetest.register_on_joinplayer(function(player)
91 local name = player:get_player_name()
92 local inv = player:get_inventory()
93 inv:set_size("hunger",1)
94 hbhunger.hunger[name] = hbhunger.get_hunger_raw(player)
95 hbhunger.hunger_out[name] = hbhunger.hunger[name]
96 hbhunger.exhaustion[name] = 0
97 hbhunger.poisonings[name] = 0
98 custom_hud(player)
99 hbhunger.set_hunger_raw(player)
100 end)
102 minetest.register_on_respawnplayer(function(player)
103 -- reset hunger (and save)
104 local name = player:get_player_name()
105 hbhunger.hunger[name] = 20
106 hbhunger.set_hunger_raw(player)
107 hbhunger.exhaustion[name] = 0
108 end)
110 local main_timer = 0
111 local timer = 0
112 local timer2 = 0
113 minetest.register_globalstep(function(dtime)
114 main_timer = main_timer + dtime
115 timer = timer + dtime
116 timer2 = timer2 + dtime
117 if main_timer > hbhunger.HUD_TICK or timer > 4 or timer2 > hbhunger.HUNGER_TICK then
118 if main_timer > hbhunger.HUD_TICK then main_timer = 0 end
119 for _,player in ipairs(minetest.get_connected_players()) do
120 local name = player:get_player_name()
122 local h = tonumber(hbhunger.hunger[name])
123 local hp = player:get_hp()
124 if timer > 4 then
125 -- heal player by 1 hp if not dead and satiation is > 15 (of 30)
126 if h > 15 and hp > 0 and player:get_breath() > 0 then
127 player:set_hp(hp+1)
128 -- or damage player by 1 hp if satiation is < 2 (of 30)
129 elseif h <= 1 then
130 if hp-1 >= 0 then player:set_hp(hp-1) end
133 -- lower satiation by 1 point after xx seconds
134 if timer2 > hbhunger.HUNGER_TICK then
135 if h > 0 then
136 h = h-1
137 hbhunger.hunger[name] = h
138 hbhunger.set_hunger_raw(player)
142 -- update all hud elements
143 update_hud(player)
145 local controls = player:get_player_control()
146 -- Determine if the player is walking
147 if controls.up or controls.down or controls.left or controls.right then
148 hbhunger.handle_node_actions(nil, nil, player)
152 if timer > 4 then timer = 0 end
153 if timer2 > hbhunger.HUNGER_TICK then timer2 = 0 end
154 end)