Cache mask in a boolean[][][]
This commit is contained in:
53
src/api/java/baritone/api/schematic/CachedMaskSchematic.java
Normal file
53
src/api/java/baritone/api/schematic/CachedMaskSchematic.java
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* This file is part of Baritone.
|
||||
*
|
||||
* Baritone is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Baritone is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package baritone.api.schematic;
|
||||
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
|
||||
/**
|
||||
* @author Brady
|
||||
*/
|
||||
public abstract class CachedMaskSchematic extends MaskSchematic {
|
||||
|
||||
/**
|
||||
* Mask array with {@code y,z,x} indexing
|
||||
*/
|
||||
private final boolean[][][] mask;
|
||||
|
||||
public CachedMaskSchematic(ISchematic schematic, StaticMaskFunction maskFunction) {
|
||||
super(schematic);
|
||||
this.mask = new boolean[schematic.heightY()][schematic.lengthZ()][schematic.widthX()];
|
||||
for (int y = 0; y < schematic.heightY(); y++) {
|
||||
for (int z = 0; z < schematic.lengthZ(); z++) {
|
||||
for (int x = 0; x < schematic.widthX(); x++) {
|
||||
this.mask[y][z][x] = maskFunction.partOfMask(x, y, z);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected final boolean partOfMask(int x, int y, int z, IBlockState currentState) {
|
||||
return this.mask[y][z][x];
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface StaticMaskFunction {
|
||||
boolean partOfMask(int x, int y, int z);
|
||||
}
|
||||
}
|
||||
@@ -17,37 +17,30 @@
|
||||
|
||||
package baritone.api.schematic;
|
||||
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
|
||||
/**
|
||||
* @author Brady
|
||||
*/
|
||||
public class CylinderSchematic extends MaskSchematic {
|
||||
|
||||
private final double centerX;
|
||||
private final double centerZ;
|
||||
private final double radiusSqX;
|
||||
private final double radiusSqZ;
|
||||
private final boolean filled;
|
||||
public final class CylinderSchematic extends CachedMaskSchematic {
|
||||
|
||||
public CylinderSchematic(ISchematic schematic, boolean filled) {
|
||||
super(schematic);
|
||||
this.centerX = schematic.widthX() / 2.0;
|
||||
this.centerZ = schematic.lengthZ() / 2.0;
|
||||
this.radiusSqX = this.centerX * this.centerX;
|
||||
this.radiusSqZ = this.centerZ * this.centerZ;
|
||||
this.filled = filled;
|
||||
}
|
||||
super(schematic, new StaticMaskFunction() {
|
||||
|
||||
@Override
|
||||
protected boolean partOfMask(int x, int y, int z, IBlockState currentState) {
|
||||
double dx = Math.abs((x + 0.5) - this.centerX);
|
||||
double dz = Math.abs((z + 0.5) - this.centerZ);
|
||||
return !this.outside(dx, dz)
|
||||
&& (this.filled || outside(dx + 1, dz) || outside(dx, dz + 1));
|
||||
}
|
||||
private final double centerX = schematic.widthX() / 2.0;
|
||||
private final double centerZ = schematic.lengthZ() / 2.0;
|
||||
private final double radiusSqX = this.centerX * this.centerX;
|
||||
private final double radiusSqZ = this.centerZ * this.centerZ;
|
||||
|
||||
private boolean outside(double dx, double dz) {
|
||||
return dx * dx / this.radiusSqX + dz * dz / this.radiusSqZ > 1;
|
||||
@Override
|
||||
public boolean partOfMask(int x, int y, int z) {
|
||||
double dx = Math.abs((x + 0.5) - this.centerX);
|
||||
double dz = Math.abs((z + 0.5) - this.centerZ);
|
||||
return !this.outside(dx, dz)
|
||||
&& (filled || outside(dx + 1, dz) || outside(dx, dz + 1));
|
||||
}
|
||||
|
||||
private boolean outside(double dx, double dz) {
|
||||
return dx * dx / this.radiusSqX + dz * dz / this.radiusSqZ > 1;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,42 +17,33 @@
|
||||
|
||||
package baritone.api.schematic;
|
||||
|
||||
import net.minecraft.block.state.IBlockState;
|
||||
|
||||
/**
|
||||
* @author Brady
|
||||
*/
|
||||
public class SphereSchematic extends MaskSchematic {
|
||||
|
||||
private final double centerX;
|
||||
private final double centerY;
|
||||
private final double centerZ;
|
||||
private final double radiusSqX;
|
||||
private final double radiusSqY;
|
||||
private final double radiusSqZ;
|
||||
private final boolean filled;
|
||||
public final class SphereSchematic extends CachedMaskSchematic {
|
||||
|
||||
public SphereSchematic(ISchematic schematic, boolean filled) {
|
||||
super(schematic);
|
||||
this.centerX = schematic.widthX() / 2.0;
|
||||
this.centerY = schematic.heightY() / 2.0;
|
||||
this.centerZ = schematic.lengthZ() / 2.0;
|
||||
this.radiusSqX = this.centerX * this.centerX;
|
||||
this.radiusSqY = this.centerY * this.centerY;
|
||||
this.radiusSqZ = this.centerZ * this.centerZ;
|
||||
this.filled = filled;
|
||||
}
|
||||
super(schematic, new StaticMaskFunction() {
|
||||
|
||||
@Override
|
||||
protected boolean partOfMask(int x, int y, int z, IBlockState currentState) {
|
||||
double dx = Math.abs((x + 0.5) - this.centerX);
|
||||
double dy = Math.abs((y + 0.5) - this.centerY);
|
||||
double dz = Math.abs((z + 0.5) - this.centerZ);
|
||||
return !this.outside(dx, dy, dz)
|
||||
&& (this.filled || outside(dx + 1, dy, dz) || outside(dx, dy + 1, dz) || outside(dx, dy, dz + 1));
|
||||
}
|
||||
private final double centerX = schematic.widthX() / 2.0;
|
||||
private final double centerY = schematic.heightY() / 2.0;
|
||||
private final double centerZ = schematic.lengthZ() / 2.0;
|
||||
private final double radiusSqX = this.centerX * this.centerX;
|
||||
private final double radiusSqY = this.centerY * this.centerY;
|
||||
private final double radiusSqZ = this.centerZ * this.centerZ;
|
||||
|
||||
private boolean outside(double dx,double dy, double dz) {
|
||||
return dx * dx / this.radiusSqX + dy * dy / this.radiusSqY + dz * dz / this.radiusSqZ > 1;
|
||||
@Override
|
||||
public boolean partOfMask(int x, int y, int z) {
|
||||
double dx = Math.abs((x + 0.5) - this.centerX);
|
||||
double dy = Math.abs((y + 0.5) - this.centerY);
|
||||
double dz = Math.abs((z + 0.5) - this.centerZ);
|
||||
return !this.outside(dx, dy, dz)
|
||||
&& (filled || outside(dx + 1, dy, dz) || outside(dx, dy + 1, dz) || outside(dx, dy, dz + 1));
|
||||
}
|
||||
|
||||
private boolean outside(double dx,double dy, double dz) {
|
||||
return dx * dx / this.radiusSqX + dy * dy / this.radiusSqY + dz * dz / this.radiusSqZ > 1;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user