2015-04-07 13:01:43 -07:00
|
|
|
--[[
|
|
|
|
|
|
|
|
Thirsty mod [thirsty]
|
|
|
|
==========================
|
|
|
|
|
|
|
|
A mod that adds a "thirst" mechanic, similar to hunger.
|
|
|
|
|
|
|
|
Copyright (C) 2015 Ben Deutsch <ben@bendeutsch.de>
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2.1 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
|
|
|
|
USA
|
|
|
|
|
|
|
|
]]
|
|
|
|
|
2015-04-11 14:06:31 -07:00
|
|
|
-- the main module variable
|
|
|
|
thirsty = {
|
|
|
|
|
|
|
|
-- 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,
|
|
|
|
}
|
|
|
|
--]]
|
|
|
|
},
|
|
|
|
|
2015-04-12 12:37:29 -07:00
|
|
|
stash_filename = 'thirsty.mt',
|
|
|
|
|
2015-04-11 14:06:31 -07:00
|
|
|
-- general settings
|
|
|
|
time_next_tick = 0.0,
|
2015-04-07 13:01:43 -07:00
|
|
|
}
|
|
|
|
|
2015-04-11 14:06:31 -07:00
|
|
|
thirsty.time_next_tick = thirsty.tick_time
|
2015-04-11 13:21:09 -07:00
|
|
|
|
2015-04-07 13:01:43 -07:00
|
|
|
hb.register_hudbar('thirst', 0xffffff, "Thirst", {
|
|
|
|
bar = 'thirsty_hudbars_bar.png',
|
|
|
|
icon = 'thirsty_cup_100_16.png'
|
|
|
|
}, 20, 20, false)
|
|
|
|
|
|
|
|
minetest.register_on_joinplayer(function(player)
|
2015-04-11 13:21:09 -07:00
|
|
|
local name = player:get_player_name()
|
2015-04-12 12:37:29 -07:00
|
|
|
-- default entry for new players
|
|
|
|
if not thirsty.players[name] then
|
|
|
|
local pos = player:getpos()
|
|
|
|
thirsty.players[name] = {
|
|
|
|
thirst = 20,
|
|
|
|
last_pos = math.floor(pos.x) .. ':' .. math.floor(pos.z),
|
|
|
|
time_in_pos = 0.0,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
hb.init_hudbar(player, 'thirst', thirsty.players[name].thirst, 20, false)
|
2015-04-07 13:01:43 -07:00
|
|
|
end)
|
|
|
|
|
|
|
|
minetest.register_globalstep(function(dtime)
|
|
|
|
-- get thirsty
|
2015-04-11 14:06:31 -07:00
|
|
|
thirsty.time_next_tick = thirsty.time_next_tick - dtime
|
|
|
|
while thirsty.time_next_tick < 0.0 do
|
2015-04-07 13:01:43 -07:00
|
|
|
-- time for thirst
|
2015-04-11 14:06:31 -07:00
|
|
|
thirsty.time_next_tick = thirsty.time_next_tick + thirsty.tick_time
|
2015-04-07 13:01:43 -07:00
|
|
|
for _,player in ipairs(minetest.get_connected_players()) do
|
|
|
|
local name = player:get_player_name()
|
|
|
|
local pos = player:getpos()
|
2015-04-11 14:06:31 -07:00
|
|
|
local pl = thirsty.players[name]
|
2015-04-11 13:21:09 -07:00
|
|
|
|
|
|
|
-- 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)
|
2015-04-11 14:06:31 -07:00
|
|
|
if pl.last_pos == pos_hash then
|
|
|
|
pl.time_in_pos = pl.time_in_pos + thirsty.tick_time
|
2015-04-11 13:21:09 -07:00
|
|
|
else
|
|
|
|
-- you moved!
|
2015-04-11 14:06:31 -07:00
|
|
|
pl.last_pos = pos_hash
|
|
|
|
pl.time_in_pos = 0.0
|
2015-04-11 13:21:09 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
pos.y = pos.y + 0.1
|
2015-04-07 13:01:43 -07:00
|
|
|
local node = minetest.get_node(pos)
|
2015-04-11 14:06:31 -07:00
|
|
|
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
|
2015-04-07 13:01:43 -07:00
|
|
|
-- Drinking from the ground won't give you more than max
|
2015-04-11 14:06:31 -07:00
|
|
|
if pl.thirst > 20 then pl.thirst = 20 end
|
|
|
|
--print("Raising thirst by "..(drink_per_second*thirsty.tick_time).." to "..pl.thirst)
|
2015-04-07 13:01:43 -07:00
|
|
|
else
|
2015-04-11 14:06:31 -07:00
|
|
|
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)
|
2015-04-11 13:21:09 -07:00
|
|
|
end
|
2015-04-07 13:01:43 -07:00
|
|
|
end
|
2015-04-11 13:21:09 -07:00
|
|
|
-- should we only update the hud on an actual change?
|
2015-04-11 14:06:31 -07:00
|
|
|
hb.change_hudbar(player, 'thirst', math.ceil(pl.thirst), 20)
|
2015-04-07 13:01:43 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
2015-04-12 12:37:29 -07:00
|
|
|
|
|
|
|
--[[
|
|
|
|
|
|
|
|
Stash: persist the thirst values in a file in the world directory.
|
|
|
|
|
|
|
|
If this is missing or corrupted, then no worries: nobody's thirsty ;-)
|
|
|
|
|
|
|
|
--]]
|
|
|
|
|
|
|
|
function thirsty.read_stash()
|
|
|
|
local filename = minetest.get_worldpath() .. "/" .. thirsty.stash_filename
|
|
|
|
local file, err = io.open(filename, "r")
|
|
|
|
if not file then
|
|
|
|
-- no problem, it's just not there
|
|
|
|
-- TODO: or parse err?
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local str = file:read()
|
|
|
|
local contents = minetest.deserialize(str)
|
|
|
|
if contents then
|
2015-04-12 13:11:55 -07:00
|
|
|
if contents.players then
|
|
|
|
thirsty.players = contents.players
|
|
|
|
end
|
2015-04-12 12:37:29 -07:00
|
|
|
end
|
|
|
|
file:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
function thirsty.write_stash()
|
|
|
|
local filename = minetest.get_worldpath() .. "/" .. thirsty.stash_filename
|
|
|
|
local file, err = io.open(filename, "w")
|
|
|
|
if not file then
|
|
|
|
minetest.log("error", "Thirsty: could not write " .. thirsty.stash_filename .. ": " ..err)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local str = minetest.serialize({
|
|
|
|
-- TODO: just the 'thirst' values
|
|
|
|
players = thirsty.players
|
|
|
|
})
|
|
|
|
file:write(str)
|
|
|
|
file:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- read on startup
|
|
|
|
thirsty.read_stash()
|
|
|
|
-- write on shutdown
|
|
|
|
minetest.register_on_shutdown(thirsty.write_stash)
|