Skip to content

Commit

Permalink
clear_area fix / move_mapdata util
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed May 22, 2021
1 parent aa15f0a commit 665107a
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 106 deletions.
12 changes: 11 additions & 1 deletion common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@ function jumpdrive.clear_area(pos1, pos2)


for z=pos1.z, pos2.z do
for y=pos2.y, pos2.y do
for y=pos1.y, pos2.y do
for x=pos1.x, pos2.x do

local source_index = source_area:index(x, y, z)
source_data[source_index] = c_air
end
end
end

manip:set_data(source_data)
manip:write_to_map()

-- remove metadata
local target_meta_pos_list = minetest.find_nodes_with_meta(pos1, pos2)
for _,target_pos in pairs(target_meta_pos_list) do
local target_meta = minetest.get_meta(target_pos)
target_meta:from_table(nil)
end
end

function jumpdrive.sanitize_coord(coord)
Expand Down
6 changes: 6 additions & 0 deletions compat/teleporttube.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,9 @@ jumpdrive.teleporttube_compat_commit = function()

pipeworks.tptube.save_tube_db()
end


-- load tp tube on start, prevents crashes if the db isn't loaded yet
if pipeworks.tptube and pipeworks.tptube.get_db then
pipeworks.tptube.get_db()
end
1 change: 1 addition & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dofile(MP.."/is_area_protected.lua")

-- move logic
dofile(MP.."/move/move_objects.lua")
dofile(MP.."/move/move_mapdata.lua")
dofile(MP.."/move/move_metadata.lua")
dofile(MP.."/move/move_nodetimers.lua")
dofile(MP.."/move/move_players.lua")
Expand Down
107 changes: 2 additions & 105 deletions move/move.lua
Original file line number Diff line number Diff line change
@@ -1,33 +1,7 @@

local c_air = minetest.get_content_id("air")


-- map of replaced content id's on jump
-- TODO: expose as api function
-- <id> = <id>
local mapped_content_ids = {}

if minetest.get_modpath("vacuum") then
-- don't jump vacuum
mapped_content_ids[minetest.get_content_id("vacuum:vacuum")] = c_air
end

-- map of "on_movenode" aware node id's
-- content_id = nodedef
local movenode_aware_nodeids = {}

-- collect movenode aware node id's
minetest.register_on_mods_loaded(function()
local count = 0
for nodename, nodedef in pairs(minetest.registered_nodes) do
if type(nodedef.on_movenode) == "function" then
count = count + 1
local id = minetest.get_content_id(nodename)
movenode_aware_nodeids[id] = nodedef
end
end
minetest.log("action", "[jumpdrive] collected " .. count .. " 'on_movenode' aware nodes")
end)

-- moves the source to the target area
-- no protection- or overlap checking is done here
Expand All @@ -49,91 +23,14 @@ function jumpdrive.move(source_pos1, source_pos2, target_pos1, target_pos2)

local t0 = minetest.get_us_time()


-- load areas (just a precaution)
if minetest.load_area then
minetest.load_area(source_pos1, source_pos2)
minetest.load_area(target_pos1, target_pos2)
end

-- read source
local manip = minetest.get_voxel_manip()
local e1, e2 = manip:read_from_map(source_pos1, source_pos2)
local source_area = VoxelArea:new({MinEdge=e1, MaxEdge=e2})
local source_data = manip:get_data()
local source_param1 = manip:get_light_data()
local source_param2 = manip:get_param2_data()

minetest.log("action", "[jumpdrive] read source-data")

-- write target
manip = minetest.get_voxel_manip()
e1, e2 = manip:read_from_map(target_pos1, target_pos2)
local target_area = VoxelArea:new({MinEdge=e1, MaxEdge=e2})
local target_data = manip:get_data()
local target_param1 = manip:get_light_data()
local target_param2 = manip:get_param2_data()

-- list of { from_pos, to_pos, }
local movenode_list = {}

minetest.log("action", "[jumpdrive] read target-data");

for z=source_pos1.z, source_pos2.z do
for y=source_pos1.y, source_pos2.y do
for x=source_pos1.x, source_pos2.x do

local from_pos = { x=x, y=y, z=z }
local to_pos = vector.add(from_pos, delta_vector)

local source_index = source_area:indexp(from_pos)
local target_index = target_area:indexp(to_pos)

-- copy block id
local id = source_data[source_index]

if mapped_content_ids[id] then
-- replace original content id
id = mapped_content_ids[id]
end

target_data[target_index] = id

if movenode_aware_nodeids[id] then

-- check if we are on an edge
local edge = { x=0, y=0, z=0 }

-- negative edge
if source_pos1.x == x then edge.x = -1 end
if source_pos1.y == y then edge.y = -1 end
if source_pos1.z == z then edge.z = -1 end
-- positive edge
if source_pos2.z == x then edge.x = 1 end
if source_pos2.y == y then edge.y = 1 end
if source_pos2.z == z then edge.z = 1 end

table.insert(movenode_list, {
from_pos = from_pos,
to_pos = to_pos,
edge = edge,
nodedef = movenode_aware_nodeids[id]
})
end

-- copy params
target_param1[target_index] = source_param1[source_index]
target_param2[target_index] = source_param2[source_index]
end
end
end


manip:set_data(target_data)
manip:set_light_data(target_param1)
manip:set_param2_data(target_param2)
manip:write_to_map()
manip:update_map()
-- move mapdata (nodeids, param1, param2)
local movenode_list = jumpdrive.move_mapdata(source_pos1, source_pos2, target_pos1, target_pos2)

local t1 = minetest.get_us_time()
minetest.log("action", "[jumpdrive] step I took " .. (t1 - t0) .. " us")
Expand Down
115 changes: 115 additions & 0 deletions move/move_mapdata.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
local c_air = minetest.get_content_id("air")

-- map of replaced content id's on jump
-- TODO: expose as api function
-- <id> = <id>
local mapped_content_ids = {}

if minetest.get_modpath("vacuum") then
-- don't jump vacuum
mapped_content_ids[minetest.get_content_id("vacuum:vacuum")] = c_air
end

-- map of "on_movenode" aware node id's
-- content_id = nodedef
local movenode_aware_nodeids = {}

-- collect movenode aware node id's
minetest.register_on_mods_loaded(function()
local count = 0
for nodename, nodedef in pairs(minetest.registered_nodes) do
if type(nodedef.on_movenode) == "function" then
count = count + 1
local id = minetest.get_content_id(nodename)
movenode_aware_nodeids[id] = nodedef
end
end
minetest.log("action", "[jumpdrive] collected " .. count .. " 'on_movenode' aware nodes")
end)

function jumpdrive.move_mapdata(source_pos1, source_pos2, target_pos1, target_pos2)

-- delta between source and target
local delta_vector = vector.subtract(target_pos1, source_pos1)

-- read source
local manip = minetest.get_voxel_manip()
local e1, e2 = manip:read_from_map(source_pos1, source_pos2)
local source_area = VoxelArea:new({MinEdge=e1, MaxEdge=e2})
local source_data = manip:get_data()
local source_param1 = manip:get_light_data()
local source_param2 = manip:get_param2_data()

minetest.log("action", "[jumpdrive] read source-data")

-- write target
manip = minetest.get_voxel_manip()
e1, e2 = manip:read_from_map(target_pos1, target_pos2)
local target_area = VoxelArea:new({MinEdge=e1, MaxEdge=e2})
local target_data = manip:get_data()
local target_param1 = manip:get_light_data()
local target_param2 = manip:get_param2_data()

-- list of { from_pos, to_pos, }
local movenode_list = {}

minetest.log("action", "[jumpdrive] read target-data");

for z=source_pos1.z, source_pos2.z do
for y=source_pos1.y, source_pos2.y do
for x=source_pos1.x, source_pos2.x do

local from_pos = { x=x, y=y, z=z }
local to_pos = vector.add(from_pos, delta_vector)

local source_index = source_area:indexp(from_pos)
local target_index = target_area:indexp(to_pos)

-- copy block id
local id = source_data[source_index]

if mapped_content_ids[id] then
-- replace original content id
id = mapped_content_ids[id]
end

target_data[target_index] = id

if movenode_aware_nodeids[id] then

-- check if we are on an edge
local edge = { x=0, y=0, z=0 }

-- negative edge
if source_pos1.x == x then edge.x = -1 end
if source_pos1.y == y then edge.y = -1 end
if source_pos1.z == z then edge.z = -1 end
-- positive edge
if source_pos2.z == x then edge.x = 1 end
if source_pos2.y == y then edge.y = 1 end
if source_pos2.z == z then edge.z = 1 end

table.insert(movenode_list, {
from_pos = from_pos,
to_pos = to_pos,
edge = edge,
nodedef = movenode_aware_nodeids[id]
})
end

-- copy params
target_param1[target_index] = source_param1[source_index]
target_param2[target_index] = source_param2[source_index]
end
end
end


manip:set_data(target_data)
manip:set_light_data(target_param1)
manip:set_param2_data(target_param2)
manip:write_to_map()
manip:update_map()

return movenode_list
end
1 change: 1 addition & 0 deletions move/move_metadata.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ jumpdrive.move_metadata = function(source_pos1, source_pos2, delta_vector)
local target_pos1 = vector.add(source_pos1, delta_vector)
local target_pos2 = vector.add(source_pos2, delta_vector)

-- check if there is some "stale" metadata in the target area
local target_meta_pos_list = minetest.find_nodes_with_meta(target_pos1, target_pos2)
for _,target_pos in pairs(target_meta_pos_list) do
minetest.log("warning", "[jumpdrive] clearing spurious meta in " .. minetest.pos_to_string(target_pos))
Expand Down

0 comments on commit 665107a

Please sign in to comment.