From 3a53876ac02a5f633b86bfc645bb8d390846e3a2 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Tue, 27 Oct 2020 08:34:34 +0100 Subject: [PATCH 1/5] rewrite bed compat code --- compat/beds.lua | 85 +++++++++++++++++++++++------------------------ compat/compat.lua | 15 +++------ move/move.lua | 7 ---- 3 files changed, 46 insertions(+), 61 deletions(-) diff --git a/compat/beds.lua b/compat/beds.lua index 3488851..cf8eb6b 100644 --- a/compat/beds.lua +++ b/compat/beds.lua @@ -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) + -- 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) @@ -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) diff --git a/compat/compat.lua b/compat/compat.lua index 2d8bc77..edf4f28 100644 --- a/compat/compat.lua +++ b/compat/compat.lua @@ -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") @@ -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 diff --git a/move/move.lua b/move/move.lua index 29b1062..99779c2 100644 --- a/move/move.lua +++ b/move/move.lua @@ -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) From 9ba1e1f82a4f7f278ee0e6362b7a2e3f96d20a6b Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Sun, 22 Dec 2024 19:04:53 +0100 Subject: [PATCH 2/5] round positions --- compat/beds.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compat/beds.lua b/compat/beds.lua index cf8eb6b..be03231 100644 --- a/compat/beds.lua +++ b/compat/beds.lua @@ -40,9 +40,10 @@ jumpdrive.register_after_jump(function(from_area, to_area) 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) for player_name, player_pos in pairs(beds.spawn) do - if vector.equals(sleep_pos, player_pos) then + if vector.equals(sleep_pos_floor, vector.floor(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) From 8c0b7cb51eb2505ab1f62f58f97799e7860a365d Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Sun, 22 Dec 2024 19:11:03 +0100 Subject: [PATCH 3/5] remove unused argument --- compat/beds.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compat/beds.lua b/compat/beds.lua index be03231..71976f0 100644 --- a/compat/beds.lua +++ b/compat/beds.lua @@ -22,7 +22,7 @@ local bed_from_positions = {} for _, nodename in ipairs(bed_bottoms) do -- override bed definitions minetest.override_item(nodename, { - on_movenode = function(from_pos, to_pos) + on_movenode = function(from_pos) -- collect bed positions while jumping table.insert(bed_from_positions, from_pos) end From 41e9c6d818ce443a5d59ea98387f58020e47843d Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Sun, 22 Dec 2024 19:18:50 +0100 Subject: [PATCH 4/5] add messages to asserts --- compat/beds.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compat/beds.lua b/compat/beds.lua index 71976f0..ec09691 100644 --- a/compat/beds.lua +++ b/compat/beds.lua @@ -1,9 +1,9 @@ local bed_bottoms = {"beds:bed_bottom", "beds:fancy_bed_bottom"} -- sanity checks -assert(beds) -assert(beds.spawn) -assert(beds.save_spawns) +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) From 1b82cc1f9d59ab86ec85c480a0e120df60610036 Mon Sep 17 00:00:00 2001 From: Luke aka SwissalpS Date: Sun, 22 Dec 2024 19:24:20 +0100 Subject: [PATCH 5/5] cleanup whitespace, comments and MT -> core --- .luacheckrc | 1 + compat/beds.lua | 30 +++++++++++++++--------------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index 353e7cc..7c427e2 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -15,6 +15,7 @@ read_globals = { "VoxelManip", -- Minetest + "core", "minetest", "vector", "ItemStack", "dump", "VoxelArea", diff --git a/compat/beds.lua b/compat/beds.lua index ec09691..73caf32 100644 --- a/compat/beds.lua +++ b/compat/beds.lua @@ -1,13 +1,13 @@ -local bed_bottoms = {"beds:bed_bottom", "beds:fancy_bed_bottom"} +local bed_bottoms = { "beds:bed_bottom", "beds:fancy_bed_bottom" } --- sanity checks +-- 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, @@ -18,37 +18,37 @@ end local bed_from_positions = {} - for _, nodename in ipairs(bed_bottoms) do - -- override bed definitions - minetest.override_item(nodename, { + -- Override bed definitions + core.override_item(nodename, { on_movenode = function(from_pos) - -- collect bed positions while jumping + -- Collect bed positions while jumping table.insert(bed_from_positions, from_pos) end }) end --- executed after jump +-- 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 - -- go over all collected bed positions + -- Go over all collected bed positions for _, bed_pos in ipairs(bed_from_positions) do - local facedir = minetest.get_node(bed_pos).param2 + local facedir = core.get_node(bed_pos).param2 local sleep_pos = calc_bed_middle(bed_pos, facedir) - -- sleep position in target area + -- Sleep position in target area local new_sleep_pos = vector.add(sleep_pos, delta_vector) local sleep_pos_floor = vector.floor(sleep_pos) 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 + -- Player sleeps here, move position beds.spawn[player_name] = new_sleep_pos - minetest.log("action", "[jumpdrive] Updated bed spawn for player " .. player_name) + core.log("action", + "[jumpdrive] Updated bed spawn for player " .. player_name) - -- set modified flag to save afterwards + -- Set modified flag to save afterwards modified = true end end @@ -59,6 +59,6 @@ jumpdrive.register_after_jump(function(from_area, to_area) beds.save_spawns() end - -- clear collected bed positions + -- Clear collected bed positions bed_from_positions = {} end)