From 89cb60e394a956d3800eb4577d747db7cc37a812 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Tue, 14 Mar 2023 14:00:32 +0100 Subject: [PATCH] add `building_def.remove_conditions` support --- conditions.lua | 9 ++++++++- readme.md | 5 +++++ remove.lua | 18 +++++++++++++++--- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/conditions.lua b/conditions.lua index 208034d..5598caf 100644 --- a/conditions.lua +++ b/conditions.lua @@ -72,13 +72,20 @@ function building_lib.check_condition_group(mapblock_pos1, mapblock_pos2, condit elseif selector == "base" then -- match only base positions it = mapblock_lib.pos_iterator(mapblock_pos1, {x=mapblock_pos2.x, y=mapblock_pos1.y, z=mapblock_pos2.z}) - elseif selector == "underground" then + elseif selector == "underground" or selector == "below" then -- match only underground positions it = mapblock_lib.pos_iterator({ x=mapblock_pos1.x, y=mapblock_pos1.y-1, z=mapblock_pos1.z },{ x=mapblock_pos2.x, y=mapblock_pos1.y-1, z=mapblock_pos2.z }) + elseif selector == "above" then + -- match only above positions + it = mapblock_lib.pos_iterator({ + x=mapblock_pos1.x, y=mapblock_pos1.y+1, z=mapblock_pos1.z + },{ + x=mapblock_pos2.x, y=mapblock_pos1.y+1, z=mapblock_pos2.z + }) else -- try to parse a manual position local rel_pos = minetest.string_to_pos(selector) diff --git a/readme.md b/readme.md index 6ec9b87..10ba9ec 100644 --- a/readme.md +++ b/readme.md @@ -20,8 +20,13 @@ local success, message = building_lib.remove(mapblock_pos) building_lib.register_building("buildings:my_building", { placement = "mapblock_lib", conditions = { + -- can only be placed if the whole area is empty {["*"] = { empty = true }} }, + remove_conditions = { + -- can only be removed if nothing is built above + {["above"] = { empty = true }} + }, -- simple catalog catalog = "my.zip", -- more catalog options diff --git a/remove.lua b/remove.lua index 649ed6e..40277bf 100644 --- a/remove.lua +++ b/remove.lua @@ -1,15 +1,27 @@ function building_lib.can_remove(mapblock_pos) - local mapblock_data = mapblock_lib.resolve_data_link(building_lib.store, mapblock_pos) - if not mapblock_data or not mapblock_data.building then + local building_info, origin = building_lib.get_placed_building_info(mapblock_pos) + if not building_info then return false, "no building found" end - local building_def = building_lib.get_building(mapblock_data.building.name) + local building_def = building_lib.get_building(building_info.name) if not building_def then return false, "unknown building" end + if building_def.remove_conditions then + -- check removal conditions + local size = building_info.size or {x=1, y=1, z=1} + local mapblock_pos2 = vector.add(origin, vector.subtract(size, 1)) + local success, message = building_lib.check_condition_groups( + origin, mapblock_pos2, building_def.remove_conditions + ) + if not success then + return false, message + end + end + return true end