Skip to content

Commit

Permalink
rewrite bed compat code (#81)
Browse files Browse the repository at this point in the history
* rewrite bed compat code

* round positions

* remove unused argument

* add messages to asserts

* cleanup whitespace, comments and MT -> core

---------

Co-authored-by: BuckarooBanzay <[email protected]>
Co-authored-by: Luke aka SwissalpS <[email protected]>
Co-authored-by: Luke aka SwissalpS <[email protected]>
  • Loading branch information
4 people authored Dec 31, 2024
1 parent 9b36a7a commit 1b5dac3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 63 deletions.
1 change: 1 addition & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ read_globals = {
"VoxelManip",

-- Minetest
"core",
"minetest",
"vector", "ItemStack",
"dump", "VoxelArea",
Expand Down
90 changes: 45 additions & 45 deletions compat/beds.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
local bed_bottoms = {"beds:bed_bottom", "beds:fancy_bed_bottom"}
local bed_bottoms = { "beds:bed_bottom", "beds:fancy_bed_bottom" }

-- Sanity checks
assert(beds, "global `beds` not found")
assert(beds.spawn, "field `spawn` doesn't exist in `beds`")
assert(beds.save_spawns, "field `save_spawns` doesn't exist in `beds`")

-- 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)
local dir = core.facedir_to_dir(facedir)
local bed_middle = {
x = bed_pos.x + dir.x / 2,
y = bed_pos.y,
Expand All @@ -11,54 +16,49 @@ 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
local bed_from_positions = {}

-- Look for beds in target area
local beds_list = minetest.find_nodes_in_area(target_pos1, target_pos2, bed_bottoms)
for _, nodename in ipairs(bed_bottoms) do
-- Override bed definitions
core.override_item(nodename, {
on_movenode = function(from_pos)
-- Collect bed positions while jumping
table.insert(bed_from_positions, from_pos)
end
})
end

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)
-- 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

-- 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
end
-- Go over all collected bed positions
for _, bed_pos in ipairs(bed_from_positions) do
local facedir = core.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)
local sleep_pos_floor = vector.floor(sleep_pos)

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)
for player_name, player_pos in pairs(beds.spawn) do
if vector.equals(sleep_pos_floor, vector.floor(player_pos)) then
-- Player sleeps here, move position
beds.spawn[player_name] = new_sleep_pos
core.log("action",
"[jumpdrive] Updated bed spawn for player " .. player_name)

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
-- 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 @@ -52,8 +52,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 @@ -75,13 +78,3 @@ end
jumpdrive.commit_node_compat = function()
-- Nothing to do here
end


jumpdrive.target_region_compat = function(_, _, 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 @@ -52,13 +52,6 @@ function jumpdrive.move(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

0 comments on commit 1b5dac3

Please sign in to comment.