//move+: fix leaving-source-behind problem

This commit is contained in:
Starbeamrainbowlabs 2021-12-31 13:38:13 +00:00
parent 8170ecb1f1
commit 2d53869f11
Signed by: sbrl
GPG Key ID: 1BE5172E637709C2
1 changed files with 58 additions and 4 deletions

View File

@ -11,6 +11,10 @@ local Vector3 = wea.Vector3
-- ██ ██ ██████ ████ ███████
function worldeditadditions.move(source_pos1, source_pos2, target_pos1, target_pos2)
---
-- 0: Preamble
---
source_pos1, source_pos2 = Vector3.sort(source_pos1, source_pos2)
target_pos1, target_pos2 = Vector3.sort(target_pos1, target_pos2)
@ -27,6 +31,11 @@ function worldeditadditions.move(source_pos1, source_pos2, target_pos1, target_p
local manip_target, area_target = worldedit.manip_helpers.init(target_pos1, target_pos2)
local data_target = manip_target:get_data()
---
-- 1: Perform Copy
---
-- z y x is the preferred loop order (because CPU cache, since then we're iterating linearly through the data array backwards. This only holds true for little-endian machines however)
for z = source_pos2.z, source_pos1.z, -1 do
for y = source_pos2.y, source_pos1.y, -1 do
@ -38,7 +47,6 @@ function worldeditadditions.move(source_pos1, source_pos2, target_pos1, target_p
data_target[target_i] = data_source[source_i]
data_source[source_i] = node_id_air
end
end
@ -51,13 +59,59 @@ function worldeditadditions.move(source_pos1, source_pos2, target_pos1, target_p
-- Potential solution:
-- 1. Grab source & target
-- 2. Perform copy
-- 3. Save move
-- 2. Perform copy → target
-- 3. Save target
-- 4. Re-grab source
-- 5. Zero out source, but avoiding touching any nodes that fall within target
-- 6. Save source
---
-- 2: Save target
---
worldedit.manip_helpers.finish(manip_target, data_target)
---
-- 3: Re-grab source
---
manip_source, area_source = worldedit.manip_helpers.init(source_pos1, source_pos2)
data_source = manip_source:get_data()
---
-- 4: Zero out source, but avoiding touching any nodes that fall within target
---
for z = source_pos2.z, source_pos1.z, -1 do
for y = source_pos2.y, source_pos1.y, -1 do
for x = source_pos2.x, source_pos1.x, -1 do
local source = Vector3.new(x, y, z)
local source_i = area_source:index(x, y, z)
local target = source:subtract(offset)
local target_i = area_target:index(target.x, target.y, target.z)
if not source:is_contained(target_pos1, target_pos2) then
data_source[source_i] = node_id_air
end
end
end
end
---
-- 5: Save source
---
worldedit.manip_helpers.finish(manip_source, data_source)
worldedit.manip_helpers.finish(manip_target, data_target)
---
-- 6: Finish up and return
---
return true, worldedit.volume(target_pos1, target_pos2)
end