From 4378750498fdaa4f862296d3a00f32138f27de27 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Fri, 30 Apr 2021 19:33:27 +0200 Subject: [PATCH] Use minetest.get_objects_in_area when possible --- worldedit/manipulations.lua | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/worldedit/manipulations.lua b/worldedit/manipulations.lua index 3bad0dd..54dc888 100644 --- a/worldedit/manipulations.lua +++ b/worldedit/manipulations.lua @@ -640,10 +640,34 @@ function worldedit.clear_objects(pos1, pos2) worldedit.keep_loaded(pos1, pos2) + local function should_delete(obj) + -- Avoid players and WorldEdit entities + if obj:is_player() then + return false + end + local entity = obj:get_luaentity() + return not entity or not entity.name:find("^worldedit:") + end + -- Offset positions to include full nodes (positions are in the center of nodes) local pos1x, pos1y, pos1z = pos1.x - 0.5, pos1.y - 0.5, pos1.z - 0.5 local pos2x, pos2y, pos2z = pos2.x + 0.5, pos2.y + 0.5, pos2.z + 0.5 + local count = 0 + if minetest.get_objects_in_area then + local objects = minetest.get_objects_in_area({x=pos1x, y=pos1y, z=pos1z}, + {x=pos2x, y=pos2y, z=pos2z}) + + for _, obj in pairs(objects) do + if should_delete(obj) then + obj:remove() + count = count + 1 + end + end + return count + end + + -- Fallback implementation via get_objects_inside_radius -- Center of region local center = { x = pos1x + ((pos2x - pos1x) / 2), @@ -655,12 +679,8 @@ function worldedit.clear_objects(pos1, pos2) (center.x - pos1x) ^ 2 + (center.y - pos1y) ^ 2 + (center.z - pos1z) ^ 2) - local count = 0 for _, obj in pairs(minetest.get_objects_inside_radius(center, radius)) do - local entity = obj:get_luaentity() - -- Avoid players and WorldEdit entities - if not obj:is_player() and (not entity or - not entity.name:find("^worldedit:")) then + if should_delete(obj) then local pos = obj:get_pos() if pos.x >= pos1x and pos.x <= pos2x and pos.y >= pos1y and pos.y <= pos2y and -- 2.44.0