Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite bed compat code #81

Merged
merged 7 commits into from
Dec 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 42 additions & 43 deletions compat/beds.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
local bed_bottoms = {"beds:bed_bottom", "beds:fancy_bed_bottom"}

-- sanity checks
assert(beds)
assert(beds.spawn)
assert(beds.save_spawns)
SwissalpS marked this conversation as resolved.
Show resolved Hide resolved

-- Calculate a bed's middle position (where players would spawn)
local function calc_bed_middle(bed_pos, facedir)
local dir = minetest.facedir_to_dir(facedir)
Expand All @@ -11,54 +16,48 @@ local function calc_bed_middle(bed_pos, facedir)
return bed_middle
end

jumpdrive.beds_compat = function(target_pos1, target_pos2, delta_vector)
if beds == nil or
beds.spawn == nil or
beds.save_spawns == nil then
-- Something is wrong. Don't do anything
return
end

-- Look for beds in target area
local beds_list = minetest.find_nodes_in_area(target_pos1, target_pos2, bed_bottoms)
local bed_from_positions = {}

if next(beds_list) ~= nil then
-- We found some beds!
local source_pos1 = vector.subtract(target_pos1, delta_vector)
local source_pos2 = vector.subtract(target_pos2, delta_vector)

-- Look for players with spawn in source area
local affected_players = {}
for name, pos in pairs(beds.spawn) do
-- pos1 and pos2 must already be sorted
if pos.x >= source_pos1.x and pos.x <= source_pos2.x and
pos.y >= source_pos1.y and pos.y <= source_pos2.y and
pos.z >= source_pos1.z and pos.z <= source_pos2.z then
table.insert(affected_players, name)
end
for _, nodename in ipairs(bed_bottoms) do
-- override bed definitions
minetest.override_item(nodename, {
on_movenode = function(from_pos, to_pos)
-- collect bed positions while jumping
table.insert(bed_from_positions, from_pos)
end
})
end

-- executed after jump
jumpdrive.register_after_jump(function(from_area, to_area)
local delta_vector = vector.subtract(to_area.pos1, from_area.pos1)
local modified = false

if next(affected_players) ~= nil then
-- Some players seem to be affected.
-- Iterate over all beds
for _, pos in pairs(beds_list) do
local facedir = minetest.get_node(pos).param2
local old_middle = calc_bed_middle(vector.subtract(pos, delta_vector), facedir)
-- go over all collected bed positions
for _, bed_pos in ipairs(bed_from_positions) do
local facedir = minetest.get_node(bed_pos).param2
local sleep_pos = calc_bed_middle(bed_pos, facedir)
-- sleep position in target area
local new_sleep_pos = vector.add(sleep_pos, delta_vector)

for _, name in ipairs(affected_players) do
local spawn = beds.spawn[name]
if spawn.x == old_middle.x and
spawn.y == old_middle.y and
spawn.z == old_middle.z then
---- Player spawn seems to match old bed position; update
beds.spawn[name] = calc_bed_middle(pos, facedir)
minetest.log("action",
"[jumpdrive] Updated bed spawn for player " .. name)
end
end
for player_name, player_pos in pairs(beds.spawn) do
if vector.equals(sleep_pos, player_pos) then
-- player sleeps here, move position
beds.spawn[player_name] = new_sleep_pos
minetest.log("action", "[jumpdrive] Updated bed spawn for player " .. player_name)

-- set modified flag to save afterwards
modified = true
end
-- Tell beds mod to save the new spawns.
beds.save_spawns()
end
end
end

if modified then
-- Tell beds mod to save the new spawns.
beds.save_spawns()
end

-- clear collected bed positions
bed_from_positions = {}
end)
15 changes: 4 additions & 11 deletions compat/compat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ if has_ropes_mod then
dofile(MP.."/compat/ropes.lua")
end

if has_beds_mod then
dofile(MP.."/compat/beds.lua")
end

dofile(MP.."/compat/telemosaic.lua")
dofile(MP.."/compat/beds.lua")

if has_pipeworks_mod then
dofile(MP.."/compat/teleporttube.lua")
Expand All @@ -76,13 +79,3 @@ jumpdrive.commit_node_compat = function()
jumpdrive.teleporttube_compat_commit()
end
end


jumpdrive.target_region_compat = function(source_pos1, source_pos2, target_pos1, target_pos2, delta_vector)
-- sync compat functions

if has_beds_mod then
jumpdrive.beds_compat(target_pos1, target_pos2, delta_vector)
end

end
7 changes: 0 additions & 7 deletions move/move.lua
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,6 @@ jumpdrive.move = function(source_pos1, source_pos2, target_pos1, target_pos2)
minetest.log("action", "[jumpdrive] step II took " .. (t1 - t0) .. " us")


-- step 3: execute target region compat code
t0 = minetest.get_us_time()
jumpdrive.target_region_compat(source_pos1, source_pos2, target_pos1, target_pos2, delta_vector)
t1 = minetest.get_us_time()
minetest.log("action", "[jumpdrive] step III took " .. (t1 - t0) .. " us")


-- step 4: move objects
t0 = minetest.get_us_time()
jumpdrive.move_objects(source_center, source_pos1, source_pos2, delta_vector)
Expand Down