Skip to content

Commit

Permalink
Add CuboidTag.intersection Tag (#2410)
Browse files Browse the repository at this point in the history
* cleanup imports

* fixes (pr review 1)
  • Loading branch information
Hydroxycobalamin committed Dec 14, 2022
1 parent 4b47cf1 commit 3b7b074
Showing 1 changed file with 52 additions and 21 deletions.
Expand Up @@ -802,28 +802,40 @@ public static void register() {
return null;
}
CuboidTag cub2 = attribute.paramAsType(CuboidTag.class);
if (cub2 != null) {
boolean intersects = false;
whole_loop:
for (LocationPair pair : cuboid.pairs) {
for (LocationPair pair2 : cub2.pairs) {
if (!pair.low.getWorld().getName().equalsIgnoreCase(pair2.low.getWorld().getName())) {
return new ElementTag("false");
}
if (pair2.low.getX() <= pair.high.getX()
&& pair2.low.getY() <= pair.high.getY()
&& pair2.low.getZ() <= pair.high.getZ()
&& pair2.high.getX() >= pair.low.getX()
&& pair2.high.getY() >= pair.low.getY()
&& pair2.high.getZ() >= pair.low.getZ()) {
intersects = true;
break whole_loop;
}
}
}
return new ElementTag(intersects);
if (cub2 == null) {
return null;
}
return null;
return new ElementTag(cuboid.intersects(cub2));
});

// <--[tag]
// @attribute <CuboidTag.intersection[<cuboid>]>
// @returns CuboidTag
// @description
// Returns the intersection of two intersecting cuboids. Returns null if the cuboids do not intersect.
// @example
// # Notes the intersection as "intersecting_area".
// - note <cuboid[my_cuboid].intersection[my_second_cuboid]> as:intersecting_area
// @example
// # Highlights the outline of the intersecting_area.
// - debugblock <cuboid[intersecting_area].outline>
// -->
tagProcessor.registerTag(CuboidTag.class, CuboidTag.class, "intersection", (attribute, cuboid, cub2) -> {
if (!cuboid.intersects(cub2)) {
attribute.echoError("Cannot return intersection: The cuboids do not intersect.");
return null;
}
LocationPair pair = cuboid.pairs.get(0);
LocationPair pair2 = cub2.pairs.get(0);
int xHigh = Math.min(pair.high.getBlockX(), pair2.high.getBlockX());
int yHigh = Math.min(pair.high.getBlockY(), pair2.high.getBlockY());
int zHigh = Math.min(pair.high.getBlockZ(), pair2.high.getBlockZ());
int xLow = Math.max(pair.low.getBlockX(), pair2.low.getBlockX());
int yLow = Math.max(pair.low.getBlockY(), pair2.low.getBlockY());
int zLow = Math.max(pair.low.getBlockZ(), pair2.low.getBlockZ());
LocationTag locationMin = new LocationTag(xLow, yLow, zLow, pair.low.getWorldName());
LocationTag locationMax = new LocationTag(xHigh, yHigh, zHigh, pair.low.getWorldName());
return new CuboidTag(locationMin, locationMax);
});

// <--[tag]
Expand Down Expand Up @@ -1569,6 +1581,25 @@ public static void register() {
});
}

public boolean intersects(CuboidTag cub2) {
for (LocationPair pair : pairs) {
for (LocationPair pair2 : cub2.pairs) {
if (!pair.low.getWorldName().equalsIgnoreCase(pair2.low.getWorldName())) {
return false;
}
if (pair2.low.getX() <= pair.high.getX()
&& pair2.low.getY() <= pair.high.getY()
&& pair2.low.getZ() <= pair.high.getZ()
&& pair2.high.getX() >= pair.low.getX()
&& pair2.high.getY() >= pair.low.getY()
&& pair2.high.getZ() >= pair.low.getZ()) {
return true;
}
}
}
return false;
}

public CuboidTag shifted(LocationTag vec) {
CuboidTag cuboid = clone();
for (LocationPair pair : cuboid.pairs) {
Expand Down

0 comments on commit 3b7b074

Please sign in to comment.