Skip to content

Commit

Permalink
add place-schematic variant that aborts if the schematic doesn't fit
Browse files Browse the repository at this point in the history
  • Loading branch information
FaceDeer committed Aug 10, 2019
1 parent 32f4843 commit 970defb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions bounding_boxes.lua
Expand Up @@ -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
10 changes: 10 additions & 0 deletions place_schematic.lua
Expand Up @@ -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)
Expand Down

0 comments on commit 970defb

Please sign in to comment.