From 970defba65ec69dd78008d1bef4ac82f44f22bab Mon Sep 17 00:00:00 2001 From: FaceDeer Date: Sat, 10 Aug 2019 14:30:15 -0600 Subject: [PATCH] add place-schematic variant that aborts if the schematic doesn't fit --- bounding_boxes.lua | 13 +++++++++++++ place_schematic.lua | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/bounding_boxes.lua b/bounding_boxes.lua index dfb0d85..6f452e0 100644 --- a/bounding_boxes.lua +++ b/bounding_boxes.lua @@ -38,4 +38,17 @@ end mapgen_helper.intersect_exists_xz = function(minpos1, maxpos1, minpos2, maxpos2) return (minpos1.x <= maxpos2.x and maxpos1.x >= minpos2.x and minpos1.z <= maxpos2.z and maxpos1.z >= minpos2.z) +end + +-- Simply tests whether pos is within the bounding box defined by minpos and maxpos +local intersect_exists = mapgen_helper.intersect_exists +mapgen_helper.is_pos_within_box = function(pos, minpos, maxpos) + return intersect_exists(pos, pos, minpos, maxpos) +end + +-- Tests whether box 1 is entirely contained within box 2 +mapgen_helper.is_box_within_box = function(minpos1, maxpos1, minpos2, maxpos2) + return (minpos1.x >= minpos2.x and maxpos1.x <= maxpos2.x and + minpos1.z >= minpos2.z and maxpos1.z <= maxpos2.z and + minpos1.y >= minpos2.y and maxpos1.y <= maxpos2.y) end \ No newline at end of file diff --git a/place_schematic.lua b/place_schematic.lua index df94b90..e3d0106 100644 --- a/place_schematic.lua +++ b/place_schematic.lua @@ -292,6 +292,16 @@ mapgen_helper.place_schematic_on_data = function(data, data_param2, area, pos, s return contained_in_area end +-- aborts schematic placement if it won't fit into the provided data +mapgen_helper.place_schematic_on_data_if_it_fits = function(data, data_param2, area, pos, schematic, rotation, replacements, force_placement, flags) + local minbound, maxbound = mapgen_helper.get_schematic_bounding_box(pos, schematic, rotation, flags) + if mapgen_helper.is_box_within_box(minbound, maxbound, area.MinEdge, area.MaxEdge) then + return mapgen_helper.place_schematic_on_data(data, data_param2, area, pos, schematic, rotation, replacements, force_placement, flags) + end + return false +end + + -- wraps the above for convenience, so you can use this style of schematic in non-mapgen contexts as well mapgen_helper.place_schematic = function(pos, schematic, rotation, replacements, force_placement, flags) local minpos, maxpos = mapgen_helper.get_schematic_bounding_box(pos, schematic, rotation, flags)