2015-06-13 17:28:25 -05:00
|
|
|
local energy_file = minetest.get_worldpath().."/energy"
|
2014-05-10 09:35:17 -05:00
|
|
|
|
2015-06-13 17:28:25 -05:00
|
|
|
player_energy = default.deserialize_from_file(energy_file)
|
2014-05-10 09:35:17 -05:00
|
|
|
player_lastpos = {}
|
|
|
|
player_sleephuds = {}
|
|
|
|
|
2015-06-13 17:28:25 -05:00
|
|
|
function energy.update_energy(p,name)
|
|
|
|
-- loop through all online players and check their movement and update their energy
|
2014-05-10 09:35:17 -05:00
|
|
|
local pos = p:getpos()
|
2014-05-25 23:48:57 -05:00
|
|
|
if player_lastpos[name] ~= nil and skills.player_levels[name] ~= nil then
|
2015-06-13 17:28:25 -05:00
|
|
|
if player_energy[name] ~= nil then
|
2014-05-16 17:11:37 -05:00
|
|
|
if minetest.check_player_privs(name, {immortal=true}) then
|
2015-06-13 17:28:25 -05:00
|
|
|
player_energy[name] = 20
|
2014-05-16 17:11:37 -05:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2014-05-10 09:35:17 -05:00
|
|
|
local anim = default.player_get_animation(p)
|
2014-05-26 00:49:03 -05:00
|
|
|
local adj = 0.2 + ( 0.2 * ( skills.player_levels[name].level / 5 ) )
|
2014-05-10 09:35:17 -05:00
|
|
|
if anim.animation == "lay" then
|
|
|
|
adj = adj + 0.75
|
|
|
|
if math.random(0,4) == 1 then
|
|
|
|
minetest.sound_play("default_snore",{object=p})
|
|
|
|
end
|
|
|
|
p:set_hp(p:get_hp()+2)
|
|
|
|
end
|
|
|
|
if anim.animation == "sit" then
|
|
|
|
adj = adj + 0.5
|
|
|
|
p:set_hp(p:get_hp()+1)
|
|
|
|
end
|
|
|
|
|
2015-06-13 17:28:25 -05:00
|
|
|
-- adjust their energy
|
2014-05-10 09:35:17 -05:00
|
|
|
local vdiff = pos.y - player_lastpos[name].y
|
|
|
|
if vdiff > 0 then
|
2015-06-26 17:12:10 -05:00
|
|
|
adj = adj - ( vdiff * 0.15 )
|
2014-05-10 09:35:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
local hdiff = math.sqrt(math.pow(pos.x-player_lastpos[name].x, 2) + math.pow(pos.z-player_lastpos[name].z, 2))
|
2015-06-26 17:12:10 -05:00
|
|
|
adj = adj - ( hdiff * 0.05 )
|
2014-05-10 09:35:17 -05:00
|
|
|
|
2015-06-13 17:28:25 -05:00
|
|
|
player_energy[name] = player_energy[name] + adj
|
|
|
|
if player_energy[name] < 0 then
|
|
|
|
player_energy[name] = 0
|
2014-05-10 09:35:17 -05:00
|
|
|
p:set_hp(p:get_hp()-1)
|
|
|
|
end
|
|
|
|
|
2015-06-13 17:28:25 -05:00
|
|
|
if player_energy[name] >= 20 then
|
|
|
|
player_energy[name] = 20
|
2014-05-12 23:39:15 -05:00
|
|
|
if anim.animation == "lay" then
|
|
|
|
-- wake them up
|
|
|
|
default.player_set_animation(p, "stand")
|
|
|
|
p:set_eye_offset({x=0,y=0,z=0},{x=0,y=0,z=0})
|
|
|
|
if player_sleephuds[name] ~= nil then
|
|
|
|
p:hud_remove(player_sleephuds[name])
|
|
|
|
player_sleephuds[name] = nil
|
|
|
|
end
|
|
|
|
minetest.chat_send_player(name,"You feel fully energized!")
|
2014-05-25 23:48:57 -05:00
|
|
|
physics.unfreeze_player(name)
|
2014-05-12 23:39:15 -05:00
|
|
|
end
|
2014-05-10 09:35:17 -05:00
|
|
|
end
|
2015-06-13 17:28:25 -05:00
|
|
|
if player_energy[name] < 3 then
|
2014-05-10 09:35:17 -05:00
|
|
|
affects.affectPlayer(name,"tired")
|
|
|
|
end
|
|
|
|
else
|
2015-06-13 17:28:25 -05:00
|
|
|
player_energy[name] = 20
|
2014-05-10 09:35:17 -05:00
|
|
|
end
|
|
|
|
end
|
2015-06-13 17:28:25 -05:00
|
|
|
player_lastpos[name] = pos
|
|
|
|
hud.change_item(p,"energy",{number = player_energy[name]})
|
2014-05-10 09:35:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
local affect_tired = {
|
|
|
|
affectid = "tired",
|
|
|
|
name = "Exhaustion",
|
|
|
|
stages = {
|
|
|
|
{
|
|
|
|
time = 360,
|
|
|
|
physics = { speed = -0.6 },
|
|
|
|
custom = { chance=100, func = function(name, player, affectid)
|
|
|
|
minetest.chat_send_player(name,"You are exhuasted")
|
|
|
|
end,runonce=true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
onremove = function(name, player, affectid)
|
|
|
|
physics.adjust_physics(player,{speed=0.6})
|
|
|
|
minetest.chat_send_player(name,"You don't feel as tired anymore",false)
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
|
|
|
|
affects.registerAffect(affect_tired)
|
|
|
|
|
|
|
|
minetest.register_chatcommand("sit",{
|
|
|
|
func = function( name, param )
|
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
default.player_set_animation(player, "sit")
|
|
|
|
player:set_eye_offset({x=0,y=-5,z=0},{x=0,y=0,z=0})
|
|
|
|
if player_sleephuds[name] ~= nil then
|
|
|
|
player:hud_remove(player_sleephuds[name])
|
|
|
|
player_sleephuds[name] = nil
|
|
|
|
end
|
2014-05-17 22:09:05 -05:00
|
|
|
physics.freeze_player(name)
|
2014-05-10 09:35:17 -05:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_chatcommand("sleep",{
|
|
|
|
func = function( name, param )
|
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
default.player_set_animation(player, "lay")
|
|
|
|
player:set_eye_offset({x=0,y=-10,z=0},{x=0,y=0,z=0})
|
|
|
|
player_sleephuds[name] = player:hud_add({
|
|
|
|
hud_elem_type = "image",
|
2015-06-13 17:28:25 -05:00
|
|
|
text = "energy_blackout.png",
|
2014-05-10 09:35:17 -05:00
|
|
|
position = {x=1,y=1},
|
|
|
|
name="sleep",
|
|
|
|
scale = {x=-100, y=-100},
|
|
|
|
alignment = {x=-1,y=-1},
|
|
|
|
offset = {x=0,y=0},
|
|
|
|
})
|
2014-05-17 22:09:05 -05:00
|
|
|
physics.freeze_player(name)
|
2014-05-10 09:35:17 -05:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
minetest.register_chatcommand("stand",{
|
|
|
|
func = function( name, param )
|
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
default.player_set_animation(player, "stand")
|
|
|
|
player:set_eye_offset({x=0,y=0,z=0},{x=0,y=0,z=0})
|
|
|
|
if player_sleephuds[name] ~= nil then
|
|
|
|
player:hud_remove(player_sleephuds[name])
|
|
|
|
player_sleephuds[name] = nil
|
|
|
|
end
|
2014-05-17 22:09:05 -05:00
|
|
|
physics.unfreeze_player(name)
|
2014-05-10 09:35:17 -05:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2015-06-26 17:12:10 -05:00
|
|
|
function energy.respawnplayer(player)
|
2014-05-10 09:35:17 -05:00
|
|
|
local name = player:get_player_name()
|
2015-06-13 17:28:25 -05:00
|
|
|
player_energy[name] = 20
|
2014-05-10 09:35:17 -05:00
|
|
|
player_lastpos[name] = player:getpos()
|
2015-06-26 17:12:10 -05:00
|
|
|
affects.removeAffect(name,"tired")
|
|
|
|
end
|
2014-05-10 09:35:17 -05:00
|
|
|
|
2014-05-11 20:49:35 -05:00
|
|
|
minetest.register_on_joinplayer(function (player)
|
|
|
|
local name = player:get_player_name()
|
2015-06-13 17:28:25 -05:00
|
|
|
if player_energy[name] == nil then
|
|
|
|
player_energy[name] = 20
|
2014-05-11 20:49:35 -05:00
|
|
|
player_lastpos[name] = player:getpos()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2014-05-10 09:35:17 -05:00
|
|
|
minetest.register_on_shutdown(function()
|
2015-06-13 17:28:25 -05:00
|
|
|
default.serialize_to_file(energy_file,player_energy)
|
|
|
|
end)
|
|
|
|
|
|
|
|
local energy_timer = 0
|
|
|
|
local energy_tick = 5
|
2015-06-17 09:40:37 -05:00
|
|
|
|
|
|
|
function energy_globalstep(dtime)
|
2015-06-13 17:28:25 -05:00
|
|
|
energy_timer = energy_timer + dtime
|
|
|
|
if energy_timer >= energy_tick then
|
|
|
|
for _,player in ipairs(minetest.get_connected_players()) do
|
|
|
|
energy.update_energy(player,player:get_player_name())
|
|
|
|
end
|
|
|
|
energy_timer = 0
|
|
|
|
end
|
2015-06-17 09:40:37 -05:00
|
|
|
end
|