From 3a53876ac02a5f633b86bfc645bb8d390846e3a2 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Tue, 27 Oct 2020 08:34:34 +0100 Subject: [PATCH] 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)