2017-05-15 19:44:27 +02:00
|
|
|
|
2015-09-01 17:15:24 +02:00
|
|
|
--
|
|
|
|
-- Bed mod
|
|
|
|
-- By PilzAdam, thefamilygrog66
|
|
|
|
-- Tweaked by Kaadmy, for Pixture
|
|
|
|
--
|
|
|
|
|
2017-05-15 19:44:27 +02:00
|
|
|
bed = {}
|
|
|
|
|
|
|
|
-- Per-user data table
|
|
|
|
|
|
|
|
bed.userdata = {}
|
|
|
|
|
2017-05-16 20:31:46 +02:00
|
|
|
-- Savefile
|
|
|
|
|
2017-05-15 19:44:27 +02:00
|
|
|
local bed_file = minetest.get_worldpath() .. "/bed.dat"
|
|
|
|
local saving = false
|
|
|
|
|
2017-05-16 20:31:46 +02:00
|
|
|
-- Timer
|
|
|
|
|
|
|
|
local timer_interval = 1
|
2017-05-15 19:44:27 +02:00
|
|
|
local timer = 0
|
|
|
|
|
|
|
|
local delay_daytime = false
|
|
|
|
|
|
|
|
local function is_bed_node(pos)
|
|
|
|
if pos == nil then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
|
|
|
|
if node.name == "bed:bed_foot" then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
local function put_player_in_bed(player)
|
|
|
|
if player == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
|
|
|
if bed.userdata[name].slept
|
|
|
|
and not is_bed_node(bed.userdata[name].node_pos) then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
player:set_look_horizontal(bed.userdata[name].spawn_yaw)
|
2019-03-20 15:04:20 +01:00
|
|
|
player:set_pos(bed.userdata[name].spawn_pos)
|
2017-05-15 19:44:27 +02:00
|
|
|
|
|
|
|
player_effects.apply_effect(player, "inbed")
|
|
|
|
|
|
|
|
player:set_eye_offset(vector.new(0, -13, 0), vector.new(0, -13, 0))
|
|
|
|
player:set_local_animation(
|
|
|
|
{x=162, y=166},
|
|
|
|
{x=162, y=166},
|
|
|
|
{x=162, y=166},
|
|
|
|
{x=162, y=168},
|
2017-05-15 23:41:46 +02:00
|
|
|
default.player_animation_speed)
|
2017-05-15 19:44:27 +02:00
|
|
|
|
2017-05-16 20:31:46 +02:00
|
|
|
default.player_set_animation(player, "lay", default.player_animation_speed)
|
2017-05-15 19:44:27 +02:00
|
|
|
|
|
|
|
default.player_attached[name] = true
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
local function take_player_from_bed(player)
|
|
|
|
if player == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
2019-03-20 15:04:20 +01:00
|
|
|
player:set_pos(bed.userdata[name].spawn_pos)
|
2017-05-15 19:44:27 +02:00
|
|
|
|
|
|
|
player_effects.remove_effect(player, "inbed")
|
|
|
|
|
|
|
|
player:set_eye_offset(vector.new(0, 0, 0), vector.new(0, 0, 0))
|
|
|
|
player:set_local_animation(
|
|
|
|
{x=0, y=79},
|
|
|
|
{x=168, y=187},
|
|
|
|
{x=189, y=198},
|
|
|
|
{x=200, y=219},
|
2017-05-15 23:41:46 +02:00
|
|
|
default.player_animation_speed)
|
2017-05-15 19:44:27 +02:00
|
|
|
|
2017-05-15 23:41:46 +02:00
|
|
|
default.player_set_animation(player, "stand", default.player_animation_speed)
|
2017-05-15 19:44:27 +02:00
|
|
|
|
|
|
|
default.player_attached[name] = false
|
|
|
|
end
|
|
|
|
|
|
|
|
local function save_bed()
|
|
|
|
local f = io.open(bed_file, "w")
|
|
|
|
|
|
|
|
f:write(minetest.serialize(bed.userdata))
|
|
|
|
|
|
|
|
io.close(f)
|
|
|
|
|
|
|
|
saving = false
|
|
|
|
end
|
|
|
|
|
|
|
|
local function delayed_save()
|
|
|
|
if not saving then
|
|
|
|
saving = true
|
|
|
|
|
|
|
|
minetest.after(40, save_bed)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function load_bed()
|
|
|
|
local f = io.open(bed_file, "r")
|
|
|
|
|
|
|
|
if f then
|
|
|
|
bed.userdata = minetest.deserialize(f:read("*all"))
|
|
|
|
|
|
|
|
io.close(f)
|
|
|
|
else
|
|
|
|
save_bed()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-16 20:31:46 +02:00
|
|
|
-- Server start
|
|
|
|
|
2017-05-15 19:44:27 +02:00
|
|
|
local function on_load()
|
|
|
|
load_bed()
|
|
|
|
end
|
|
|
|
|
2017-05-16 20:31:46 +02:00
|
|
|
-- Server shutdown
|
|
|
|
|
2017-05-15 19:44:27 +02:00
|
|
|
local function on_shutdown()
|
|
|
|
save_bed()
|
|
|
|
end
|
|
|
|
|
2017-05-16 20:31:46 +02:00
|
|
|
-- Joining player
|
|
|
|
|
2017-05-15 19:44:27 +02:00
|
|
|
local function on_joinplayer(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
|
|
|
if not bed.userdata[name] then
|
|
|
|
bed.userdata[name] = {
|
|
|
|
in_bed = false,
|
|
|
|
|
|
|
|
spawn_yaw = 0,
|
|
|
|
spawn_pos = nil,
|
|
|
|
|
|
|
|
slept = false,
|
|
|
|
|
|
|
|
node_pos = nil,
|
|
|
|
}
|
|
|
|
|
|
|
|
delayed_save()
|
|
|
|
end
|
|
|
|
|
|
|
|
if bed.userdata[name].in_bed then
|
|
|
|
minetest.after(
|
|
|
|
0.1,
|
|
|
|
function()
|
|
|
|
put_player_in_bed(player)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-16 20:31:46 +02:00
|
|
|
-- Respawning player
|
|
|
|
|
2017-05-15 19:44:27 +02:00
|
|
|
local function on_respawnplayer(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
|
|
|
|
if bed.userdata[name] then
|
|
|
|
if not bed.userdata[name].slept then
|
|
|
|
if not is_bed_node(bed.userdata[name].node_pos) then
|
|
|
|
minetest.chat_send_player(
|
|
|
|
name,
|
|
|
|
minetest.colorize(
|
|
|
|
"#f00", "You don't have a bed"))
|
|
|
|
else
|
|
|
|
minetest.chat_send_player(
|
|
|
|
name,
|
|
|
|
minetest.colorize(
|
|
|
|
"#f00", "You haven't slept in a bed yet"))
|
|
|
|
end
|
|
|
|
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
bed.userdata[name].in_bed = false
|
|
|
|
|
|
|
|
take_player_from_bed(player)
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-16 20:31:46 +02:00
|
|
|
-- Update function
|
|
|
|
|
2017-05-15 19:44:27 +02:00
|
|
|
local function on_globalstep(dtime)
|
|
|
|
timer = timer + dtime
|
|
|
|
|
2017-05-16 20:31:46 +02:00
|
|
|
if timer < timer_interval then
|
2017-05-15 19:44:27 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
timer = 0
|
|
|
|
|
|
|
|
local sleeping_players = 0
|
|
|
|
|
2017-05-16 20:31:46 +02:00
|
|
|
for name, data in pairs(bed.userdata) do
|
2017-05-15 19:44:27 +02:00
|
|
|
if data.in_bed then
|
2017-05-16 20:31:46 +02:00
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
|
2017-05-15 19:44:27 +02:00
|
|
|
sleeping_players = sleeping_players + 1
|
2017-05-16 20:31:46 +02:00
|
|
|
|
2019-03-20 15:04:20 +01:00
|
|
|
if vector.distance(player:get_pos(), data.spawn_pos) > 2 then
|
|
|
|
player:move_to(data.spawn_pos)
|
2017-05-16 20:31:46 +02:00
|
|
|
end
|
2017-05-15 19:44:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-16 20:31:46 +02:00
|
|
|
local players = minetest.get_connected_players()
|
|
|
|
local player_count = #players
|
2017-05-15 19:44:27 +02:00
|
|
|
|
2017-05-16 20:31:46 +02:00
|
|
|
if player_count > 0 and (player_count / 2.0) < sleeping_players then
|
2017-05-15 19:44:27 +02:00
|
|
|
if minetest.get_timeofday() < 0.2 or minetest.get_timeofday() > 0.8 then
|
|
|
|
if not delay_daytime then
|
|
|
|
delay_daytime = true
|
|
|
|
|
|
|
|
minetest.after(
|
|
|
|
2,
|
|
|
|
function()
|
|
|
|
minetest.chat_send_all(
|
|
|
|
minetest.colorize(
|
|
|
|
"#0ff",
|
2017-05-16 20:31:46 +02:00
|
|
|
"*** " .. sleeping_players .. " of " .. player_count
|
2017-05-15 19:44:27 +02:00
|
|
|
.. " players slept, rise and shine!"))
|
|
|
|
|
|
|
|
minetest.set_timeofday(0.23)
|
|
|
|
delay_daytime = false
|
|
|
|
|
2017-05-16 20:31:46 +02:00
|
|
|
for _, player in ipairs(players) do
|
2017-05-15 19:44:27 +02:00
|
|
|
if bed.userdata[player:get_player_name()].in_bed then
|
|
|
|
bed.userdata[player:get_player_name()].slept = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
delayed_save()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
minetest.after(0, on_load)
|
|
|
|
|
|
|
|
minetest.register_on_shutdown(on_shutdown)
|
|
|
|
|
|
|
|
minetest.register_on_joinplayer(on_joinplayer)
|
|
|
|
|
|
|
|
minetest.register_on_respawnplayer(on_respawnplayer)
|
|
|
|
|
|
|
|
minetest.register_globalstep(on_globalstep)
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-05-16 20:31:46 +02:00
|
|
|
-- Nodes
|
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_node(
|
2015-09-01 17:15:24 +02:00
|
|
|
"bed:bed_foot",
|
|
|
|
{
|
|
|
|
description = "Bed",
|
|
|
|
drawtype = "nodebox",
|
|
|
|
paramtype = "light",
|
|
|
|
paramtype2 = "facedir",
|
2015-10-10 08:23:27 +02:00
|
|
|
sunlight_propagates = true,
|
|
|
|
wield_image = "bed_bed_inventory.png",
|
|
|
|
inventory_image = "bed_bed_inventory.png",
|
|
|
|
tiles = {"bed_foot.png", "default_wood.png", "bed_side.png"},
|
2015-09-01 17:15:24 +02:00
|
|
|
groups = {snappy = 1, choppy = 2, oddly_breakable_by_hand = 2, flammable = 3},
|
|
|
|
sounds = default.node_sound_wood_defaults(),
|
|
|
|
node_box = {
|
|
|
|
type = "fixed",
|
2015-10-10 08:23:27 +02:00
|
|
|
fixed = {-0.5, -0.5, -0.5, 0.5, 2/16, 0.5}
|
2015-09-01 17:15:24 +02:00
|
|
|
},
|
|
|
|
selection_box = {
|
|
|
|
type = "fixed",
|
2015-10-10 08:23:27 +02:00
|
|
|
fixed = {-0.5, -0.5, -0.5, 0.5, 2/16, 1.5}
|
2015-09-01 17:15:24 +02:00
|
|
|
},
|
2015-10-10 08:23:27 +02:00
|
|
|
|
|
|
|
after_place_node = function(pos)
|
2017-05-15 19:44:27 +02:00
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
local dir = minetest.facedir_to_dir(node.param2)
|
|
|
|
local head_pos = vector.add(pos, dir)
|
|
|
|
node.name = "bed:bed_head"
|
|
|
|
if minetest.registered_nodes[minetest.get_node(head_pos).name].buildable_to then
|
|
|
|
minetest.set_node(head_pos, node)
|
|
|
|
else
|
|
|
|
minetest.remove_node(pos)
|
|
|
|
end
|
|
|
|
end,
|
2015-10-10 08:23:27 +02:00
|
|
|
|
2015-09-01 17:15:24 +02:00
|
|
|
on_destruct = function(pos)
|
2017-05-15 19:44:27 +02:00
|
|
|
local node = minetest.get_node(pos)
|
|
|
|
local dir = minetest.facedir_to_dir(node.param2)
|
|
|
|
local head_pos = vector.add(pos, dir)
|
|
|
|
if minetest.get_node(head_pos).name == "bed:bed_head" then
|
|
|
|
minetest.remove_node(head_pos)
|
|
|
|
end
|
|
|
|
end,
|
2015-10-10 08:23:27 +02:00
|
|
|
|
|
|
|
on_rightclick = function(pos, node, clicker)
|
2017-05-15 19:44:27 +02:00
|
|
|
if not clicker:is_player() then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local name = clicker:get_player_name()
|
|
|
|
local meta = minetest.get_meta(pos)
|
|
|
|
local put_pos = vector.add(pos, vector.divide(
|
|
|
|
minetest.facedir_to_dir(node.param2), 2))
|
|
|
|
|
|
|
|
if clicker:get_player_name() == meta:get_string("player") then
|
|
|
|
put_pos.y = put_pos.y - 0.5
|
2015-10-11 18:13:20 +02:00
|
|
|
|
2017-05-15 19:44:27 +02:00
|
|
|
bed.userdata[name].in_bed = false
|
2015-10-11 18:13:20 +02:00
|
|
|
|
2017-05-15 19:44:27 +02:00
|
|
|
take_player_from_bed(clicker)
|
2015-10-11 18:13:20 +02:00
|
|
|
|
2017-05-15 19:44:27 +02:00
|
|
|
meta:set_string("player", "")
|
|
|
|
elseif meta:get_string("player") == "" and not default.player_attached[name]
|
|
|
|
and bed.userdata[name].in_bed == false then
|
2017-06-27 21:13:30 +02:00
|
|
|
if not minetest.settings:get_bool("bed_enabled") then
|
2017-05-15 19:44:27 +02:00
|
|
|
return
|
|
|
|
end
|
2015-10-11 18:13:20 +02:00
|
|
|
|
2017-05-15 19:44:27 +02:00
|
|
|
put_pos.y = put_pos.y + 0.6
|
2015-10-11 18:13:20 +02:00
|
|
|
|
2017-05-15 19:44:27 +02:00
|
|
|
local yaw = 0
|
2015-10-11 18:13:20 +02:00
|
|
|
|
2017-05-15 19:44:27 +02:00
|
|
|
if node.param2 ~= 2 then
|
|
|
|
yaw = (node.param2 / 2.0) * math.pi
|
|
|
|
end
|
2015-10-11 18:13:20 +02:00
|
|
|
|
2017-05-15 19:44:27 +02:00
|
|
|
bed.userdata[name].in_bed = true
|
2015-10-10 08:23:27 +02:00
|
|
|
|
2017-05-15 19:44:27 +02:00
|
|
|
bed.userdata[name].spawn_yaw = yaw
|
|
|
|
bed.userdata[name].spawn_pos = put_pos
|
2015-10-10 08:23:27 +02:00
|
|
|
|
2017-05-15 19:44:27 +02:00
|
|
|
bed.userdata[name].node_pos = pos
|
|
|
|
|
|
|
|
put_player_in_bed(clicker)
|
|
|
|
|
|
|
|
meta:set_string("player", name)
|
|
|
|
end
|
|
|
|
end,
|
2015-09-30 19:09:59 +02:00
|
|
|
|
2015-10-10 08:23:27 +02:00
|
|
|
can_dig = function(pos)
|
2017-05-15 19:44:27 +02:00
|
|
|
return minetest.get_meta(pos):get_string("player") == ""
|
|
|
|
end
|
|
|
|
})
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_node(
|
2015-09-01 17:15:24 +02:00
|
|
|
"bed:bed_head",
|
|
|
|
{
|
|
|
|
drawtype = "nodebox",
|
|
|
|
paramtype = "light",
|
|
|
|
paramtype2 = "facedir",
|
2015-10-10 08:23:27 +02:00
|
|
|
pointable = false,
|
|
|
|
tiles = {"bed_head.png", "default_wood.png", "bed_side.png"},
|
2015-09-01 17:15:24 +02:00
|
|
|
groups = {snappy = 1, choppy = 2, oddly_breakable_by_hand = 2, flammable = 3},
|
|
|
|
sounds = default.node_sound_wood_defaults(),
|
|
|
|
node_box = {
|
|
|
|
type = "fixed",
|
2015-10-10 08:23:27 +02:00
|
|
|
fixed = {-0.5, -0.5, -0.5, 0.5, 2/16, 0.5}
|
|
|
|
}
|
2017-05-15 19:44:27 +02:00
|
|
|
})
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-05-12 04:29:55 +02:00
|
|
|
minetest.register_alias("bed:bed", "bed:bed_foot")
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-05-16 20:31:46 +02:00
|
|
|
-- Crafting
|
|
|
|
|
2017-05-15 19:44:27 +02:00
|
|
|
crafting.register_craft(
|
2015-09-01 17:15:24 +02:00
|
|
|
{
|
|
|
|
output = "bed:bed",
|
2017-05-15 19:44:27 +02:00
|
|
|
items = {
|
|
|
|
"group:fuzzy 3",
|
|
|
|
"group:planks 3",
|
2015-09-01 17:15:24 +02:00
|
|
|
}
|
2017-05-15 19:44:27 +02:00
|
|
|
})
|
2015-09-01 17:15:24 +02:00
|
|
|
|
2017-05-16 20:31:46 +02:00
|
|
|
-- Player effects
|
|
|
|
|
2015-10-24 20:36:31 +02:00
|
|
|
player_effects.register_effect(
|
|
|
|
"inbed",
|
|
|
|
{
|
|
|
|
title = "In bed",
|
|
|
|
description = "If you're in a bed",
|
|
|
|
duration = -1,
|
|
|
|
physics = {
|
|
|
|
speed = 0,
|
|
|
|
jump = 0,
|
|
|
|
gravity = 0,
|
|
|
|
}
|
2017-05-15 19:44:27 +02:00
|
|
|
})
|
2015-09-30 19:09:59 +02:00
|
|
|
|
2015-10-25 22:18:54 +01:00
|
|
|
-- Achievements
|
|
|
|
|
|
|
|
achievements.register_achievement(
|
|
|
|
"bedtime",
|
|
|
|
{
|
|
|
|
title = "Bed Time",
|
|
|
|
description = "Craft a bed",
|
|
|
|
times = 1,
|
2015-11-09 20:16:25 +01:00
|
|
|
craftitem = "bed:bed_foot",
|
2017-05-15 19:44:27 +02:00
|
|
|
})
|
2015-10-25 22:18:54 +01:00
|
|
|
|
2015-10-04 06:25:26 +02:00
|
|
|
default.log("mod:bed", "loaded")
|