2015-10-05 00:17:30 +02:00
|
|
|
local pi = math.pi
|
2015-02-24 11:59:15 +01:00
|
|
|
local is_sp = minetest.is_singleplayer()
|
2017-05-22 15:41:17 +02:00
|
|
|
local enable_respawn = minetest.settings:get_bool("enable_bed_respawn")
|
2015-03-06 19:29:16 +01:00
|
|
|
if enable_respawn == nil then
|
|
|
|
enable_respawn = true
|
|
|
|
end
|
2015-02-24 11:59:15 +01:00
|
|
|
|
2019-09-18 20:38:27 +02:00
|
|
|
-- support for MT game translation.
|
|
|
|
local S = beds.get_translator
|
|
|
|
|
2016-03-08 04:14:29 +01:00
|
|
|
-- Helper functions
|
2015-02-24 11:59:15 +01:00
|
|
|
|
|
|
|
local function get_look_yaw(pos)
|
2017-09-28 19:23:08 +02:00
|
|
|
local rotation = minetest.get_node(pos).param2
|
|
|
|
if rotation > 3 then
|
|
|
|
rotation = rotation % 4 -- Mask colorfacedir values
|
|
|
|
end
|
|
|
|
if rotation == 1 then
|
|
|
|
return pi / 2, rotation
|
|
|
|
elseif rotation == 3 then
|
|
|
|
return -pi / 2, rotation
|
|
|
|
elseif rotation == 0 then
|
|
|
|
return pi, rotation
|
2015-02-24 11:59:15 +01:00
|
|
|
else
|
2017-09-28 19:23:08 +02:00
|
|
|
return 0, rotation
|
2015-02-24 11:59:15 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-14 18:57:41 +02:00
|
|
|
local function is_night_skip_enabled()
|
2017-05-22 15:41:17 +02:00
|
|
|
local enable_night_skip = minetest.settings:get_bool("enable_bed_night_skip")
|
2015-05-14 18:57:41 +02:00
|
|
|
if enable_night_skip == nil then
|
|
|
|
enable_night_skip = true
|
|
|
|
end
|
|
|
|
return enable_night_skip
|
|
|
|
end
|
|
|
|
|
2015-02-24 11:59:15 +01:00
|
|
|
local function check_in_beds(players)
|
|
|
|
local in_bed = beds.player
|
|
|
|
if not players then
|
|
|
|
players = minetest.get_connected_players()
|
|
|
|
end
|
|
|
|
|
|
|
|
for n, player in ipairs(players) do
|
|
|
|
local name = player:get_player_name()
|
|
|
|
if not in_bed[name] then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-14 14:41:26 +02:00
|
|
|
return #players > 0
|
2015-02-24 11:59:15 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local function lay_down(player, pos, bed_pos, state, skip)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
local hud_flags = player:hud_get_flags()
|
|
|
|
|
|
|
|
if not player or not name then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- stand up
|
|
|
|
if state ~= nil and not state then
|
2021-02-11 12:30:37 +01:00
|
|
|
if not beds.player[name] then
|
|
|
|
-- player not in bed, do nothing
|
|
|
|
return false
|
|
|
|
end
|
2019-03-24 00:12:44 +01:00
|
|
|
beds.bed_position[name] = nil
|
2015-02-24 11:59:15 +01:00
|
|
|
-- skip here to prevent sending player specific changes (used for leaving players)
|
|
|
|
if skip then
|
|
|
|
return
|
|
|
|
end
|
2021-02-11 12:30:37 +01:00
|
|
|
player:set_pos(beds.pos[name])
|
2015-02-24 11:59:15 +01:00
|
|
|
|
|
|
|
-- physics, eye_offset, etc
|
2021-02-11 12:30:37 +01:00
|
|
|
local physics_override = beds.player[name].physics_override
|
2021-02-01 23:00:05 +01:00
|
|
|
beds.player[name] = nil
|
|
|
|
player:set_physics_override({
|
|
|
|
speed = physics_override.speed,
|
|
|
|
jump = physics_override.jump,
|
|
|
|
gravity = physics_override.gravity
|
|
|
|
})
|
2016-03-08 04:14:29 +01:00
|
|
|
player:set_eye_offset({x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
|
2016-07-25 17:42:42 +02:00
|
|
|
player:set_look_horizontal(math.random(1, 180) / 100)
|
2020-04-23 22:19:58 +02:00
|
|
|
player_api.player_attached[name] = false
|
2015-02-24 11:59:15 +01:00
|
|
|
hud_flags.wielditem = true
|
2020-04-23 22:19:58 +02:00
|
|
|
player_api.set_animation(player, "stand" , 30)
|
2015-02-24 11:59:15 +01:00
|
|
|
|
|
|
|
-- lay down
|
|
|
|
else
|
2020-09-02 18:32:06 +02:00
|
|
|
|
|
|
|
-- Check if bed is occupied
|
|
|
|
for _, other_pos in pairs(beds.bed_position) do
|
|
|
|
if vector.distance(bed_pos, other_pos) < 0.1 then
|
|
|
|
minetest.chat_send_player(name, S("This bed is already occupied!"))
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Check if player is moving
|
2020-11-04 21:42:35 +01:00
|
|
|
if vector.length(player:get_velocity()) > 0.001 then
|
2020-09-02 18:32:06 +02:00
|
|
|
minetest.chat_send_player(name, S("You have to stop moving before going to bed!"))
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2021-05-08 11:30:57 +02:00
|
|
|
-- Check if player is attached to an object
|
|
|
|
if player:get_attach() then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2021-02-11 12:30:37 +01:00
|
|
|
if beds.player[name] then
|
|
|
|
-- player already in bed, do nothing
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2015-02-24 11:59:15 +01:00
|
|
|
beds.pos[name] = pos
|
2018-06-19 23:07:01 +02:00
|
|
|
beds.bed_position[name] = bed_pos
|
2021-02-01 23:00:05 +01:00
|
|
|
beds.player[name] = {physics_override = player:get_physics_override()}
|
2015-02-24 11:59:15 +01:00
|
|
|
|
|
|
|
-- physics, eye_offset, etc
|
2016-03-08 04:14:29 +01:00
|
|
|
player:set_eye_offset({x = 0, y = -13, z = 0}, {x = 0, y = 0, z = 0})
|
2015-02-24 11:59:15 +01:00
|
|
|
local yaw, param2 = get_look_yaw(bed_pos)
|
2016-07-25 17:42:42 +02:00
|
|
|
player:set_look_horizontal(yaw)
|
2015-02-24 11:59:15 +01:00
|
|
|
local dir = minetest.facedir_to_dir(param2)
|
2020-02-08 04:11:44 +01:00
|
|
|
-- p.y is just above the nodebox height of the 'Simple Bed' (the highest bed),
|
|
|
|
-- to avoid sinking down through the bed.
|
|
|
|
local p = {
|
|
|
|
x = bed_pos.x + dir.x / 2,
|
|
|
|
y = bed_pos.y + 0.07,
|
|
|
|
z = bed_pos.z + dir.z / 2
|
|
|
|
}
|
2020-11-04 21:42:35 +01:00
|
|
|
player:set_physics_override({speed = 0, jump = 0, gravity = 0})
|
2018-07-01 21:44:03 +02:00
|
|
|
player:set_pos(p)
|
2020-04-23 22:19:58 +02:00
|
|
|
player_api.player_attached[name] = true
|
2015-02-24 11:59:15 +01:00
|
|
|
hud_flags.wielditem = false
|
2020-04-23 22:19:58 +02:00
|
|
|
player_api.set_animation(player, "lay" , 0)
|
2015-02-24 11:59:15 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
player:hud_set_flags(hud_flags)
|
|
|
|
end
|
|
|
|
|
2019-03-10 02:43:49 +01:00
|
|
|
local function get_player_in_bed_count()
|
|
|
|
local c = 0
|
|
|
|
for _, _ in pairs(beds.player) do
|
|
|
|
c = c + 1
|
|
|
|
end
|
|
|
|
return c
|
|
|
|
end
|
|
|
|
|
2015-02-24 11:59:15 +01:00
|
|
|
local function update_formspecs(finished)
|
|
|
|
local ges = #minetest.get_connected_players()
|
2019-03-10 02:43:49 +01:00
|
|
|
local player_in_bed = get_player_in_bed_count()
|
2016-03-08 04:14:29 +01:00
|
|
|
local is_majority = (ges / 2) < player_in_bed
|
2015-02-24 11:59:15 +01:00
|
|
|
|
2019-09-18 20:38:27 +02:00
|
|
|
local form_n
|
|
|
|
local esc = minetest.formspec_escape
|
2015-02-24 11:59:15 +01:00
|
|
|
if finished then
|
2019-09-18 20:38:27 +02:00
|
|
|
form_n = beds.formspec .. "label[2.7,9;" .. esc(S("Good morning.")) .. "]"
|
2015-02-24 11:59:15 +01:00
|
|
|
else
|
2019-09-18 20:38:27 +02:00
|
|
|
form_n = beds.formspec .. "label[2.2,9;" ..
|
|
|
|
esc(S("@1 of @2 players are in bed", player_in_bed, ges)) .. "]"
|
2015-05-14 18:57:41 +02:00
|
|
|
if is_majority and is_night_skip_enabled() then
|
2019-09-18 20:38:27 +02:00
|
|
|
form_n = form_n .. "button_exit[2,6;4,0.75;force;" ..
|
|
|
|
esc(S("Force night skip")) .. "]"
|
2015-02-24 11:59:15 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for name,_ in pairs(beds.player) do
|
|
|
|
minetest.show_formspec(name, "beds_form", form_n)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2016-03-08 04:14:29 +01:00
|
|
|
-- Public functions
|
2015-02-24 11:59:15 +01:00
|
|
|
|
|
|
|
function beds.kick_players()
|
2016-03-08 04:14:29 +01:00
|
|
|
for name, _ in pairs(beds.player) do
|
2015-02-24 11:59:15 +01:00
|
|
|
local player = minetest.get_player_by_name(name)
|
|
|
|
lay_down(player, nil, nil, false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function beds.skip_night()
|
|
|
|
minetest.set_timeofday(0.23)
|
|
|
|
end
|
|
|
|
|
|
|
|
function beds.on_rightclick(pos, player)
|
|
|
|
local name = player:get_player_name()
|
2018-07-01 21:44:03 +02:00
|
|
|
local ppos = player:get_pos()
|
2015-02-24 11:59:15 +01:00
|
|
|
local tod = minetest.get_timeofday()
|
|
|
|
|
|
|
|
if tod > 0.2 and tod < 0.805 then
|
|
|
|
if beds.player[name] then
|
|
|
|
lay_down(player, nil, nil, false)
|
|
|
|
end
|
2019-09-18 20:38:27 +02:00
|
|
|
minetest.chat_send_player(name, S("You can only sleep at night."))
|
2015-02-24 11:59:15 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- move to bed
|
|
|
|
if not beds.player[name] then
|
|
|
|
lay_down(player, ppos, pos)
|
2016-05-20 12:40:15 +02:00
|
|
|
beds.set_spawns() -- save respawn positions when entering bed
|
2015-02-24 11:59:15 +01:00
|
|
|
else
|
|
|
|
lay_down(player, nil, nil, false)
|
|
|
|
end
|
|
|
|
|
|
|
|
if not is_sp then
|
|
|
|
update_formspecs(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- skip the night and let all players stand up
|
|
|
|
if check_in_beds() then
|
|
|
|
minetest.after(2, function()
|
|
|
|
if not is_sp then
|
2015-05-14 18:57:41 +02:00
|
|
|
update_formspecs(is_night_skip_enabled())
|
|
|
|
end
|
|
|
|
if is_night_skip_enabled() then
|
|
|
|
beds.skip_night()
|
|
|
|
beds.kick_players()
|
2015-02-24 11:59:15 +01:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-19 23:07:01 +02:00
|
|
|
function beds.can_dig(bed_pos)
|
|
|
|
-- Check all players in bed which one is at the expected position
|
|
|
|
for _, player_bed_pos in pairs(beds.bed_position) do
|
|
|
|
if vector.equals(bed_pos, player_bed_pos) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2015-02-24 11:59:15 +01:00
|
|
|
|
2016-03-08 04:14:29 +01:00
|
|
|
-- Callbacks
|
2016-07-12 11:02:01 +02:00
|
|
|
-- Only register respawn callback if respawn enabled
|
2016-11-20 04:24:37 +01:00
|
|
|
if enable_respawn then
|
2016-07-12 11:02:01 +02:00
|
|
|
-- respawn player at bed if enabled and valid position is found
|
|
|
|
minetest.register_on_respawnplayer(function(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
local pos = beds.spawn[name]
|
|
|
|
if pos then
|
2018-07-01 21:44:03 +02:00
|
|
|
player:set_pos(pos)
|
2016-07-12 11:02:01 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2015-02-24 11:59:15 +01:00
|
|
|
|
|
|
|
minetest.register_on_leaveplayer(function(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
lay_down(player, nil, nil, false, true)
|
|
|
|
beds.player[name] = nil
|
|
|
|
if check_in_beds() then
|
|
|
|
minetest.after(2, function()
|
2015-05-14 18:57:41 +02:00
|
|
|
update_formspecs(is_night_skip_enabled())
|
|
|
|
if is_night_skip_enabled() then
|
|
|
|
beds.skip_night()
|
|
|
|
beds.kick_players()
|
|
|
|
end
|
2015-02-24 11:59:15 +01:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2020-09-02 18:32:06 +02:00
|
|
|
minetest.register_on_dieplayer(function(player)
|
|
|
|
local name = player:get_player_name()
|
|
|
|
local in_bed = beds.player
|
|
|
|
local pos = player:get_pos()
|
|
|
|
local yaw = get_look_yaw(pos)
|
|
|
|
|
|
|
|
if in_bed[name] then
|
|
|
|
lay_down(player, nil, pos, false)
|
|
|
|
player:set_look_horizontal(yaw)
|
|
|
|
player:set_pos(pos)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2015-02-24 11:59:15 +01:00
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|
|
|
if formname ~= "beds_form" then
|
|
|
|
return
|
|
|
|
end
|
2018-07-03 20:50:16 +02:00
|
|
|
|
|
|
|
-- Because "Force night skip" button is a button_exit, it will set fields.quit
|
|
|
|
-- and lay_down call will change value of player_in_bed, so it must be taken
|
|
|
|
-- earlier.
|
2019-03-10 02:43:49 +01:00
|
|
|
local last_player_in_bed = get_player_in_bed_count()
|
2018-07-03 20:50:16 +02:00
|
|
|
|
2015-02-24 11:59:15 +01:00
|
|
|
if fields.quit or fields.leave then
|
|
|
|
lay_down(player, nil, nil, false)
|
|
|
|
update_formspecs(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
if fields.force then
|
2018-07-03 20:50:16 +02:00
|
|
|
local is_majority = (#minetest.get_connected_players() / 2) < last_player_in_bed
|
|
|
|
if is_majority and is_night_skip_enabled() then
|
|
|
|
update_formspecs(true)
|
2015-05-14 18:57:41 +02:00
|
|
|
beds.skip_night()
|
|
|
|
beds.kick_players()
|
2018-07-03 20:50:16 +02:00
|
|
|
else
|
|
|
|
update_formspecs(false)
|
2015-05-14 18:57:41 +02:00
|
|
|
end
|
2015-02-24 11:59:15 +01:00
|
|
|
end
|
|
|
|
end)
|