baritone/src/main/java/baritone/utils/pathing/BetterBlockPos.java

129 lines
4.1 KiB
Java
Raw Normal View History

2018-08-14 17:47:31 +00:00
/*
* This file is part of Baritone.
*
* Baritone is free software: you can redistribute it and/or modify
* it under the terms of the GNU 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Baritone. If not, see <https://www.gnu.org/licenses/>.
*/
2018-08-22 20:15:56 +00:00
package baritone.utils.pathing;
2018-08-14 17:47:31 +00:00
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Vec3i;
/**
* A better BlockPos that has fewer hash collisions (and slightly more performant offsets)
*
* @author leijurv
*/
2018-09-03 23:14:14 +00:00
public final class BetterBlockPos extends BlockPos {
2018-08-29 18:29:26 +00:00
public final int x;
public final int y;
public final int z;
2018-08-29 20:21:54 +00:00
public final long hashCode;
2018-08-14 17:47:31 +00:00
public BetterBlockPos(int x, int y, int z) {
super(x, y, z);
this.x = x;
this.y = y;
this.z = z;
/*
* This is the hashcode implementation of Vec3i, the superclass of BlockPos
*
* public int hashCode() {
* return (this.getY() + this.getZ() * 31) * 31 + this.getX();
* }
*
* That is terrible and has tons of collisions and makes the HashMap terribly inefficient.
*
* That's why we grab out the X, Y, Z and calculate our own hashcode
*/
2018-08-29 20:21:54 +00:00
long hash = 3241;
hash = 3457689L * hash + x;
hash = 8734625L * hash + y;
hash = 2873465L * hash + z;
2018-08-14 17:47:31 +00:00
this.hashCode = hash;
}
public BetterBlockPos(BlockPos pos) {
this(pos.getX(), pos.getY(), pos.getZ());
}
2018-08-14 17:47:31 +00:00
@Override
2018-09-08 15:15:10 +00:00
public int hashCode() {
2018-08-29 20:21:54 +00:00
return (int) hashCode;
2018-08-14 17:47:31 +00:00
}
@Override
2018-09-08 15:15:10 +00:00
public boolean equals(Object o) {
2018-08-16 01:02:07 +00:00
if (o == null) {
return false;
}
2018-08-14 17:47:31 +00:00
if (o instanceof BetterBlockPos) {
BetterBlockPos oth = (BetterBlockPos) o;
if (oth.hashCode != hashCode) {
return false;
}
return oth.x == x && oth.y == y && oth.z == z;
}
// during path execution, like "if (whereShouldIBe.equals(whereAmI)) {"
// sometimes we compare a BlockPos to a BetterBlockPos
BlockPos oth = (BlockPos) o;
return oth.getX() == x && oth.getY() == y && oth.getZ() == z;
}
@Override
2018-09-09 15:58:36 +00:00
public BetterBlockPos up() {
2018-08-14 17:47:31 +00:00
// this is unimaginably faster than blockpos.up
// that literally calls
// this.up(1)
// which calls this.offset(EnumFacing.UP, 1)
// which does return n == 0 ? this : new BlockPos(this.getX() + facing.getXOffset() * n, this.getY() + facing.getYOffset() * n, this.getZ() + facing.getZOffset() * n);
// how many function calls is that? up(), up(int), offset(EnumFacing, int), new BlockPos, getX, getXOffset, getY, getYOffset, getZ, getZOffset
// that's ten.
// this is one function call.
return new BetterBlockPos(x, y + 1, z);
}
@Override
2018-09-09 15:58:36 +00:00
public BetterBlockPos up(int amt) {
2018-08-14 17:47:31 +00:00
// see comment in up()
2018-08-14 18:25:30 +00:00
return amt == 0 ? this : new BetterBlockPos(x, y + amt, z);
2018-08-14 17:47:31 +00:00
}
@Override
2018-09-09 15:58:36 +00:00
public BetterBlockPos down() {
2018-08-14 17:47:31 +00:00
// see comment in up()
return new BetterBlockPos(x, y - 1, z);
}
@Override
2018-09-09 15:58:36 +00:00
public BetterBlockPos down(int amt) {
2018-08-14 17:47:31 +00:00
// see comment in up()
return new BetterBlockPos(x, y - amt, z);
}
@Override
2018-09-09 15:58:36 +00:00
public BetterBlockPos offset(EnumFacing dir) {
2018-08-14 17:47:31 +00:00
Vec3i vec = dir.getDirectionVec();
return new BetterBlockPos(x + vec.getX(), y + vec.getY(), z + vec.getZ());
}
2018-09-06 14:48:27 +00:00
@Override
2018-09-09 15:58:36 +00:00
public BetterBlockPos offset(EnumFacing dir, int dist) {
2018-09-06 14:48:27 +00:00
Vec3i vec = dir.getDirectionVec();
return new BetterBlockPos(x + vec.getX() * dist, y + vec.getY() * dist, z + vec.getZ() * dist);
}
2018-08-14 17:47:31 +00:00
}