Code refactoring: module table instead of local vars
parent
be83903813
commit
ac7ac2b7f9
88
init.lua
88
init.lua
|
@ -24,24 +24,37 @@ USA
|
|||
|
||||
]]
|
||||
|
||||
-- Configuration variables
|
||||
local tick_time = 0.5
|
||||
local thirst_per_second = 1.0 / 20.0
|
||||
local stand_still_for_drink = 1.0
|
||||
local stand_still_for_afk = 120.0 -- 2 Minutes
|
||||
-- the main module variable
|
||||
thirsty = {
|
||||
|
||||
local drink_from_node = {
|
||||
-- value: thirst regen per second
|
||||
['default:water_source'] = 0.5,
|
||||
['default:water_flowing'] = 0.5,
|
||||
-- Configuration variables
|
||||
tick_time = 0.5,
|
||||
thirst_per_second = 1.0 / 20.0,
|
||||
stand_still_for_drink = 1.0,
|
||||
stand_still_for_afk = 120.0, -- 2 Minutes
|
||||
|
||||
drink_from_node = {
|
||||
-- value: thirst regen per second
|
||||
['default:water_source'] = 0.5,
|
||||
['default:water_flowing'] = 0.5,
|
||||
},
|
||||
|
||||
-- the players' values
|
||||
players = {
|
||||
--[[
|
||||
name = {
|
||||
thirst = 20,
|
||||
last_pos = '-10:3',
|
||||
time_in_pos = 0.0,
|
||||
}
|
||||
--]]
|
||||
},
|
||||
|
||||
-- general settings
|
||||
time_next_tick = 0.0,
|
||||
}
|
||||
|
||||
local time_next_tick = tick_time
|
||||
|
||||
local thirst_level = {}
|
||||
|
||||
-- to detect "standing still" for drinking, and also AFK detection
|
||||
local player_info = {}
|
||||
thirsty.time_next_tick = thirsty.tick_time
|
||||
|
||||
hb.register_hudbar('thirst', 0xffffff, "Thirst", {
|
||||
bar = 'thirsty_hudbars_bar.png',
|
||||
|
@ -51,10 +64,9 @@ hb.register_hudbar('thirst', 0xffffff, "Thirst", {
|
|||
minetest.register_on_joinplayer(function(player)
|
||||
hb.init_hudbar(player, 'thirst', 20, 20, false)
|
||||
local name = player:get_player_name()
|
||||
thirst_level[name] = 20
|
||||
local pos = player:getpos()
|
||||
|
||||
player_info[name] = {
|
||||
thirsty.players[name] = {
|
||||
thirst = 20,
|
||||
last_pos = math.floor(pos.x) .. ':' .. math.floor(pos.z),
|
||||
time_in_pos = 0.0,
|
||||
}
|
||||
|
@ -62,46 +74,44 @@ end)
|
|||
|
||||
minetest.register_globalstep(function(dtime)
|
||||
-- get thirsty
|
||||
time_next_tick = time_next_tick - dtime
|
||||
while time_next_tick < 0.0 do
|
||||
thirsty.time_next_tick = thirsty.time_next_tick - dtime
|
||||
while thirsty.time_next_tick < 0.0 do
|
||||
-- time for thirst
|
||||
time_next_tick = time_next_tick + tick_time
|
||||
thirsty.time_next_tick = thirsty.time_next_tick + thirsty.tick_time
|
||||
for _,player in ipairs(minetest.get_connected_players()) do
|
||||
local name = player:get_player_name()
|
||||
local pos = player:getpos()
|
||||
local p_info = player_info[name]
|
||||
local pl = thirsty.players[name]
|
||||
|
||||
-- how long have we been standing "here"?
|
||||
-- (the node coordinates in X and Z should be enough)
|
||||
local pos_hash = math.floor(pos.x) .. ':' .. math.floor(pos.z)
|
||||
local last_pos = p_info.last_pos
|
||||
if last_pos == pos_hash then
|
||||
p_info.time_in_pos = p_info.time_in_pos + tick_time
|
||||
if pl.last_pos == pos_hash then
|
||||
pl.time_in_pos = pl.time_in_pos + thirsty.tick_time
|
||||
else
|
||||
-- you moved!
|
||||
p_info.last_pos = pos_hash
|
||||
p_info.time_in_pos = 0.0
|
||||
pl.last_pos = pos_hash
|
||||
pl.time_in_pos = 0.0
|
||||
end
|
||||
|
||||
pos.y = pos.y + 0.1
|
||||
local node = minetest.get_node(pos)
|
||||
local drink_per_second = drink_from_node[node.name]
|
||||
if drink_per_second ~= nil and drink_per_second > 0 and p_info.time_in_pos > stand_still_for_drink then
|
||||
thirst_level[name] = thirst_level[name] + drink_per_second * tick_time
|
||||
--print("Raising thirst by "..(drink_per_second*tick_time).." to "..thirst_level[name])
|
||||
local drink_per_second = thirsty.drink_from_node[node.name]
|
||||
if drink_per_second ~= nil and drink_per_second > 0 and pl.time_in_pos > thirsty.stand_still_for_drink then
|
||||
pl.thirst = pl.thirst + drink_per_second * thirsty.tick_time
|
||||
-- Drinking from the ground won't give you more than max
|
||||
if thirst_level[name] > 20 then thirst_level[name] = 20 end
|
||||
if pl.thirst > 20 then pl.thirst = 20 end
|
||||
--print("Raising thirst by "..(drink_per_second*thirsty.tick_time).." to "..pl.thirst)
|
||||
else
|
||||
if p_info.time_in_pos < stand_still_for_afk then
|
||||
-- if afk, skip just about everything
|
||||
|
||||
thirst_level[name] = thirst_level[name] - thirst_per_second * tick_time
|
||||
--print("Lowering thirst by "..(thirst_per_second*tick_time).." to "..thirst_level[name])
|
||||
if thirst_level[name] < 0 then thirst_level[name] = 0 end
|
||||
if pl.time_in_pos < thirsty.stand_still_for_afk then
|
||||
-- only get thirsty if not AFK
|
||||
pl.thirst = pl.thirst - thirsty.thirst_per_second * thirsty.tick_time
|
||||
if pl.thirst< 0 then pl.thirst = 0 end
|
||||
--print("Lowering thirst by "..(thirsty.thirst_per_second*thirsty.tick_time).." to "..pl.thirst)
|
||||
end
|
||||
end
|
||||
-- should we only update the hud on an actual change?
|
||||
hb.change_hudbar(player, 'thirst', math.ceil(thirst_level[name]), 20)
|
||||
hb.change_hudbar(player, 'thirst', math.ceil(pl.thirst), 20)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
|
Loading…
Reference in New Issue