From fe1edc5dd58440fbad56a91c6fb57265e242d3ad Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Tue, 8 Nov 2022 14:18:15 +0100 Subject: [PATCH] `build_over` support --- build.lua | 45 +++++++++++++++++++++++++++++++++++++++++++++ readme.md | 9 ++++++++- 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/build.lua b/build.lua index 1fc8ac8..c1d8d57 100644 --- a/build.lua +++ b/build.lua @@ -97,6 +97,51 @@ function building_lib.can_build(mapblock_pos, _, building_name, rotation) return false, message or "size check '" .. building_def.placement .. "' failed" end + -- check if we can build over other buildings + if building_def.build_over then + local other_building_info, origin = building_lib.get_placed_building_info(mapblock_pos) + if other_building_info then + -- other building exists, check if it matches + if not vector.equals(other_building_info.size or {x=1,y=1,z=1}, size) then + return false, "Existing building has different size" + end + + if not vector.equals(origin, mapblock_pos) then + return false, "Placement-origin mismatch" + end + + local other_building_def = building_lib.get_building(other_building_info.name) + if not other_building_def then + return false, "Unknown building" + end + + local matches = false + if building_def.build_over.groups then + for _, group in ipairs(building_def.build_over.groups) do + if other_building_def[group] then + matches = true + break + end + end + end + + if not matches and building_def.build_over.names then + for _, name in ipairs(building_def.build_over.names) do + if name == other_building_def.name then + matches = true + break + end + end + end + + if not matches then + -- can't build over pointed building + -- TODO: proper pointed position + return false, "Existing building can't be built over" + end + end + end + local it = mapblock_lib.pos_iterator(mapblock_pos, vector.add(mapblock_pos, vector.subtract(size, 1))) while true do local offset_mapblock_pos = it() diff --git a/readme.md b/readme.md index 8ad984a..61fb54d 100644 --- a/readme.md +++ b/readme.md @@ -52,7 +52,14 @@ building_lib.register_building("buildings:my_building", { return { ["old_mod:node"] = "new_mod:node" } - end + end, + -- build-over config + build_over = { + -- by group + groups = {"my_group"}, + -- by name + names = {"other_building:name"} + } }) -- registers a placement type (connected, simple, etc)