Camas/mods/bed/init.lua

192 lines
5.4 KiB
Lua
Raw Normal View History

2015-09-01 17:15:24 +02:00
--
-- Bed mod
-- By PilzAdam, thefamilygrog66
-- Tweaked by Kaadmy, for Pixture
--
2015-09-30 19:09:59 +02:00
local players_in_bed = {}
2015-09-01 17:15:24 +02:00
minetest.register_node(
"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)
2015-10-04 06:25:26 +02:00
local node = minetest.get_node(pos)
2015-10-10 08:23:27 +02:00
local dir = minetest.facedir_to_dir(node.param2)
local head_pos = vector.add(pos, dir)
2015-09-01 17:15:24 +02:00
node.name = "bed:bed_head"
2015-10-10 08:23:27 +02:00
if minetest.registered_nodes[minetest.get_node(head_pos).name].buildable_to then
minetest.set_node(head_pos, node)
2015-09-01 17:15:24 +02:00
else
2015-10-10 08:23:27 +02:00
minetest.remove_node(pos)
2015-09-01 17:15:24 +02:00
end
end,
2015-10-10 08:23:27 +02:00
2015-09-01 17:15:24 +02:00
on_destruct = function(pos)
2015-10-04 06:25:26 +02:00
local node = minetest.get_node(pos)
2015-10-10 08:23:27 +02:00
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
2015-10-11 18:13:20 +02:00
minetest.remove_node(head_pos)
2015-09-01 17:15:24 +02:00
end
end,
2015-10-10 08:23:27 +02:00
on_rightclick = function(pos, node, clicker)
2015-10-11 18:13:20 +02:00
if not clicker:is_player() or not minetest.setting_getbool("bed_enabled") 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
clicker:setpos(put_pos)
clicker:set_physics_override(1, 1, 1)
clicker:set_eye_offset(vector.new(0, 0, 0), vector.new(0, 0, 0))
clicker:set_local_animation({x=0, y=79}, {x=168, y=187}, {x=189, y=198}, {x=200, y=219}, 30)
default.player_set_animation(clicker, "stand", 30)
players_in_bed[name] = nil
default.player_attached[name] = false
meta:set_string("player", "")
elseif meta:get_string("player") == "" then
put_pos.y = put_pos.y + 0.6
clicker:setpos(put_pos)
clicker:set_physics_override(0, 0, 0)
clicker:set_eye_offset(vector.new(0, -13, 0), vector.new(0, -13, 0))
clicker:set_local_animation({x=162, y=166}, {x=162, y=166}, {x=162, y=166}, {x=162, y=168}, 30)
default.player_set_animation(clicker, "lay", 30)
if node.param2 == 2 then
clicker:set_look_yaw(0)
else
clicker:set_look_yaw(node.param2 / 2 * math.pi)
end
2015-10-10 08:23:27 +02:00
2015-10-11 18:13:20 +02:00
players_in_bed[name] = true
default.player_attached[name] = true
2015-10-10 08:23:27 +02:00
2015-10-11 18:13:20 +02:00
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)
2015-10-11 18:13:20 +02:00
return minetest.get_meta(pos):get_string("player") == ""
end
2015-09-01 17:15:24 +02:00
})
minetest.register_node(
"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}
}
2015-09-01 17:15:24 +02:00
})
minetest.register_alias("bed:bed", "bed:bed_foot")
minetest.register_craft(
{
output = "bed:bed",
recipe = {
{"group:fuzzy", "group:fuzzy", "group:fuzzy"},
2015-10-10 08:23:27 +02:00
{"group:planks", "group:planks", "group:planks"}
2015-09-01 17:15:24 +02:00
}
})
bed_player_spawns = {}
local file = io.open(minetest.get_worldpath().."/bed.txt", "r")
if file then
bed_player_spawns = minetest.deserialize(file:read("*all"))
file:close()
end
local timer = 0
local wait = false
minetest.register_globalstep(
function(dtime)
if timer < 2 then
timer = timer + dtime
return
end
timer = 0
2015-10-10 08:23:27 +02:00
2015-09-30 19:09:59 +02:00
local sleeping_players = 0
for _, i in pairs(players_in_bed) do
2015-10-10 08:23:27 +02:00
if i then
2015-09-30 19:09:59 +02:00
sleeping_players = sleeping_players + 1
end
end
2015-09-01 17:15:24 +02:00
local players = #minetest.get_connected_players()
2015-10-10 08:23:27 +02:00
if players > 0 and players * 0.5 < sleeping_players then
2015-10-04 06:25:26 +02:00
if minetest.get_timeofday() < 0.2 or minetest.get_timeofday() > 0.8 then
2015-09-01 17:15:24 +02:00
if not wait then
2015-10-10 08:23:27 +02:00
minetest.chat_send_all("[zzz] "..sleeping_players.." of "..players.." players slept, skipping to day.")
wait = true
2015-09-01 17:15:24 +02:00
minetest.after(2, function()
2015-10-04 06:25:26 +02:00
minetest.set_timeofday(0.23)
2015-09-01 17:15:24 +02:00
wait = false
end)
2015-10-10 08:23:27 +02:00
for _, player in ipairs(minetest.get_connected_players()) do
2015-09-01 17:15:24 +02:00
bed_player_spawns[player:get_player_name()] = player:getpos()
end
2015-10-10 08:23:27 +02:00
2015-09-01 17:15:24 +02:00
local file = io.open(minetest.get_worldpath().."/bed.txt", "w")
2015-10-10 08:23:27 +02:00
file:write(minetest.serialize(bed_player_spawns))
file:close()
2015-09-01 17:15:24 +02:00
end
end
end
end)
minetest.register_on_respawnplayer(
function(player)
local name = player:get_player_name()
if bed_player_spawns[name] then
player:setpos(bed_player_spawns[name])
end
end)
2015-09-30 19:09:59 +02:00
minetest.register_on_leaveplayer(
function(player)
players_in_bed[player:get_player_name()] = nil
end)
2015-10-04 06:25:26 +02:00
default.log("mod:bed", "loaded")