diff --git a/src/main/java/net/minecraft/util/math/BlockPos.java b/src/main/java/net/minecraft/util/math/BlockPos.java index b9aa29c1b..f1904b4fe 100644 --- a/src/main/java/net/minecraft/util/math/BlockPos.java +++ b/src/main/java/net/minecraft/util/math/BlockPos.java @@ -1,18 +1,33 @@ package net.minecraft.util.math; -import com.google.common.collect.AbstractIterator; import java.util.Iterator; +import java.util.List; + +import javax.annotation.concurrent.Immutable; + +import net.minecraft.util.Vec3i; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import com.google.common.collect.AbstractIterator; +import com.google.common.collect.Lists; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; import net.minecraft.entity.Entity; import net.minecraft.util.EnumFacing; -import net.minecraft.util.Vec3; -import net.minecraft.util.Vec3i; +import net.minecraft.util.MathHelper; -/** This code is owned by Minecraft */ - -public class BlockPos extends Vec3i{ +/** + * Pasted in from 1.9, with the Vec3d constructor removed + */ +@Immutable +public class BlockPos extends Vec3i +{ + private static final Logger LOGGER = LogManager.getLogger(); /** The BlockPos with all coordinates 0 */ public static final BlockPos ORIGIN = new BlockPos(0, 0, 0); - private static final int NUM_X_BITS = 1 + 25;//MathHelper.calculateLogBaseTwo(MathHelper.roundUpToPowerOfTwo(30000000)); + private static final int NUM_X_BITS = 26; //1 + MathHelper.calculateLogBaseTwo(MathHelper.roundUpToPowerOfTwo(30000000)); //Manually calculated because of crash private static final int NUM_Z_BITS = NUM_X_BITS; private static final int NUM_Y_BITS = 64 - NUM_X_BITS - NUM_Z_BITS; private static final int Y_SHIFT = 0 + NUM_Z_BITS; @@ -36,11 +51,6 @@ public class BlockPos extends Vec3i{ this(source.posX, source.posY, source.posZ); } - public BlockPos(Vec3 source) - { - this(source.xCoord, source.yCoord, source.zCoord); - } - public BlockPos(Vec3i source) { this(source.getX(), source.getY(), source.getZ()); @@ -217,6 +227,9 @@ public class BlockPos extends Vec3i{ return new BlockPos(i, j, k); } + /** + * Create an Iterable that returns all positions in the box specified by the given corners + */ public static Iterable getAllInBox(BlockPos from, BlockPos to) { final BlockPos blockpos = new BlockPos(Math.min(from.getX(), to.getX()), Math.min(from.getY(), to.getY()), Math.min(from.getZ(), to.getZ())); @@ -270,6 +283,21 @@ public class BlockPos extends Vec3i{ }; } + /** + * Returns a version of this BlockPos that is guaranteed to be immutable. + * + *

When storing a BlockPos given to you for an extended period of time, make sure you + * use this in case the value is changed internally.

+ */ + public BlockPos toImmutable() + { + return this; + } + + /** + * Like getAllInBox but reuses a single MutableBlockPos instead. If this method is used, the resulting BlockPos + * instances can only be used inside the iteration loop. + */ public static Iterable getAllInBoxMutable(BlockPos from, BlockPos to) { final BlockPos blockpos = new BlockPos(Math.min(from.getX(), to.getX()), Math.min(from.getY(), to.getY()), Math.min(from.getZ(), to.getZ())); @@ -325,35 +353,25 @@ public class BlockPos extends Vec3i{ }; } - /** - * Returns a version of this BlockPos that is guaranteed to be Immutable. - * In most cases this will return 'this', but if 'this' is an instance of - * MutableBlockPos it will return a new instance of BlockPos with the same values. - * - * When storing a parameter given to you for an extended period of time, make sure you - * use this in case the value is changed internally. - * - * @return An immutable BlockPos. - */ - public BlockPos getImmutable() - { - return this; - } - - public static final class MutableBlockPos extends BlockPos + public static class MutableBlockPos extends BlockPos { /** Mutable X Coordinate */ - private int x; + protected int x; /** Mutable Y Coordinate */ - private int y; + protected int y; /** Mutable Z Coordinate */ - private int z; + protected int z; public MutableBlockPos() { this(0, 0, 0); } + public MutableBlockPos(BlockPos pos) + { + this(pos.getX(), pos.getY(), pos.getZ()); + } + public MutableBlockPos(int x_, int y_, int z_) { super(0, 0, 0); @@ -363,7 +381,7 @@ public class BlockPos extends Vec3i{ } /** - * Get the X coordinate + * Gets the X coordinate. */ public int getX() { @@ -371,7 +389,7 @@ public class BlockPos extends Vec3i{ } /** - * Get the Y coordinate + * Gets the Y coordinate. */ public int getY() { @@ -379,7 +397,7 @@ public class BlockPos extends Vec3i{ } /** - * Get the Z coordinate + * Gets the Z coordinate. */ public int getZ() { @@ -387,13 +405,9 @@ public class BlockPos extends Vec3i{ } /** - * Set the values - * - * @param xIn The X value - * @param yIn The Y value - * @param zIn The Z value + * Sets the position, MUST not be name 'set' as that causes obfusication conflicts with func_185343_d */ - public BlockPos.MutableBlockPos set(int xIn, int yIn, int zIn) + public BlockPos.MutableBlockPos setPos(int xIn, int yIn, int zIn) { this.x = xIn; this.y = yIn; @@ -401,6 +415,143 @@ public class BlockPos extends Vec3i{ return this; } - @Override public BlockPos getImmutable() { return new BlockPos(this); } + public BlockPos.MutableBlockPos setPos(double p_189532_1_, double p_189532_3_, double p_189532_5_) + { + return this.setPos(MathHelper.floor_double(p_189532_1_), MathHelper.floor_double(p_189532_3_), MathHelper.floor_double(p_189532_5_)); + } + + @SideOnly(Side.CLIENT) + public BlockPos.MutableBlockPos setPos(Entity p_189535_1_) + { + return this.setPos(p_189535_1_.posX, p_189535_1_.posY, p_189535_1_.posZ); + } + + public BlockPos.MutableBlockPos setPos(Vec3i p_189533_1_) + { + return this.setPos(p_189533_1_.getX(), p_189533_1_.getY(), p_189533_1_.getZ()); + } + + public BlockPos.MutableBlockPos move(EnumFacing p_189536_1_) + { + return this.move(p_189536_1_, 1); + } + + public BlockPos.MutableBlockPos move(EnumFacing p_189534_1_, int p_189534_2_) + { + return this.setPos(this.x + p_189534_1_.getFrontOffsetX() * p_189534_2_, this.y + p_189534_1_.getFrontOffsetY() * p_189534_2_, this.z + p_189534_1_.getFrontOffsetZ() * p_189534_2_); + } + + public void setY(int yIn) + { + this.y = yIn; + } + + /** + * Returns a version of this BlockPos that is guaranteed to be immutable. + * + *

When storing a BlockPos given to you for an extended period of time, make sure you + * use this in case the value is changed internally.

+ */ + public BlockPos toImmutable() + { + return new BlockPos(this); + } } -} \ No newline at end of file + + public static final class PooledMutableBlockPos extends BlockPos.MutableBlockPos + { + private boolean released; + private static final List POOL = Lists.newArrayList(); + + private PooledMutableBlockPos(int xIn, int yIn, int zIn) + { + super(xIn, yIn, zIn); + } + + public static BlockPos.PooledMutableBlockPos retain() + { + return retain(0, 0, 0); + } + + public static BlockPos.PooledMutableBlockPos retain(double xIn, double yIn, double zIn) + { + return retain(MathHelper.floor_double(xIn), MathHelper.floor_double(yIn), MathHelper.floor_double(zIn)); + } + + @SideOnly(Side.CLIENT) + public static BlockPos.PooledMutableBlockPos retain(Vec3i vec) + { + return retain(vec.getX(), vec.getY(), vec.getZ()); + } + + public static BlockPos.PooledMutableBlockPos retain(int xIn, int yIn, int zIn) + { + synchronized (POOL) + { + if (!POOL.isEmpty()) + { + BlockPos.PooledMutableBlockPos blockpos$pooledmutableblockpos = (BlockPos.PooledMutableBlockPos)POOL.remove(POOL.size() - 1); + + if (blockpos$pooledmutableblockpos != null && blockpos$pooledmutableblockpos.released) + { + blockpos$pooledmutableblockpos.released = false; + blockpos$pooledmutableblockpos.set(xIn, yIn, zIn); + return blockpos$pooledmutableblockpos; + } + } + } + + return new BlockPos.PooledMutableBlockPos(xIn, yIn, zIn); + } + + public void release() + { + synchronized (POOL) + { + if (POOL.size() < 100) + { + POOL.add(this); + } + + this.released = true; + } + } + + public BlockPos.PooledMutableBlockPos set(int xIn, int yIn, int zIn) + { + if (this.released) + { + BlockPos.LOGGER.error("PooledMutableBlockPosition modified after it was released.", new Throwable()); + this.released = false; + } + + return (BlockPos.PooledMutableBlockPos)super.setPos(xIn, yIn, zIn); + } + + @SideOnly(Side.CLIENT) + public BlockPos.PooledMutableBlockPos set(Entity p_189537_1_) + { + return (BlockPos.PooledMutableBlockPos)super.setPos(p_189537_1_); + } + + public BlockPos.PooledMutableBlockPos set(double xIn, double yIn, double zIn) + { + return (BlockPos.PooledMutableBlockPos)super.setPos(xIn, yIn, zIn); + } + + public BlockPos.PooledMutableBlockPos set(Vec3i vec) + { + return (BlockPos.PooledMutableBlockPos)super.setPos(vec); + } + + public BlockPos.PooledMutableBlockPos offsetMutable(EnumFacing facing) + { + return (BlockPos.PooledMutableBlockPos)super.move(facing); + } + + public BlockPos.PooledMutableBlockPos movePos(EnumFacing p_189538_1_, int p_189538_2_) + { + return (BlockPos.PooledMutableBlockPos)super.move(p_189538_1_, p_189538_2_); + } + } +} diff --git a/src/main/java/net/minecraft/util/math/IntHashMap.java b/src/main/java/net/minecraft/util/math/IntHashMap.java new file mode 100644 index 000000000..797013de5 --- /dev/null +++ b/src/main/java/net/minecraft/util/math/IntHashMap.java @@ -0,0 +1,255 @@ +package net.minecraft.util.math; + +public class IntHashMap { + private transient IntHashMap.Entry[] slots = new IntHashMap.Entry[16]; + /** + * The number of items stored in this map + */ + private transient int count; + /** + * The grow threshold + */ + private int threshold = 12; + /** + * The scale factor used to determine when to grow the table + */ + private final float growFactor = 0.75F; + + /** + * Makes the passed in integer suitable for hashing by a number of shifts + */ + private static int computeHash(int integer) { + integer = integer ^ integer >>> 20 ^ integer >>> 12; + return integer ^ integer >>> 7 ^ integer >>> 4; + } + + /** + * Computes the index of the slot for the hash and slot count passed in. + */ + private static int getSlotIndex(int hash, int slotCount) { + return hash & slotCount - 1; + } + + /** + * Returns the object associated to a key + */ + public V lookup(int p_76041_1_) { + int i = computeHash(p_76041_1_); + + for (IntHashMap.Entry entry = this.slots[getSlotIndex(i, this.slots.length)]; entry != null; entry = entry.nextEntry) { + if (entry.hashEntry == p_76041_1_) { + return entry.valueEntry; + } + } + + return (V) null; + } + + /** + * Returns true if this hash table contains the specified item. + */ + public boolean containsItem(int p_76037_1_) { + return this.lookupEntry(p_76037_1_) != null; + } + + final IntHashMap.Entry lookupEntry(int p_76045_1_) { + int i = computeHash(p_76045_1_); + + for (IntHashMap.Entry entry = this.slots[getSlotIndex(i, this.slots.length)]; entry != null; entry = entry.nextEntry) { + if (entry.hashEntry == p_76045_1_) { + return entry; + } + } + + return null; + } + + /** + * Adds a key and associated value to this map + */ + public void addKey(int p_76038_1_, V p_76038_2_) { + int i = computeHash(p_76038_1_); + int j = getSlotIndex(i, this.slots.length); + + for (IntHashMap.Entry entry = this.slots[j]; entry != null; entry = entry.nextEntry) { + if (entry.hashEntry == p_76038_1_) { + entry.valueEntry = p_76038_2_; + return; + } + } + + this.insert(i, p_76038_1_, p_76038_2_, j); + } + + /** + * Increases the number of hash slots + */ + private void grow(int p_76047_1_) { + IntHashMap.Entry[] entry = this.slots; + int i = entry.length; + + if (i == 1073741824) { + this.threshold = Integer.MAX_VALUE; + } else { + IntHashMap.Entry[] entry1 = new IntHashMap.Entry[p_76047_1_]; + this.copyTo(entry1); + this.slots = entry1; + this.threshold = (int) ((float) p_76047_1_ * this.growFactor); + } + } + + /** + * Copies the hash slots to a new array + */ + private void copyTo(IntHashMap.Entry[] p_76048_1_) { + IntHashMap.Entry[] entry = this.slots; + int i = p_76048_1_.length; + + for (int j = 0; j < entry.length; ++j) { + IntHashMap.Entry entry1 = entry[j]; + + if (entry1 != null) { + entry[j] = null; + + while (true) { + IntHashMap.Entry entry2 = entry1.nextEntry; + int k = getSlotIndex(entry1.slotHash, i); + entry1.nextEntry = p_76048_1_[k]; + p_76048_1_[k] = entry1; + entry1 = entry2; + + if (entry2 == null) { + break; + } + } + } + } + } + + /** + * Removes the specified object from the map and returns it + */ + public V removeObject(int p_76049_1_) { + IntHashMap.Entry entry = this.removeEntry(p_76049_1_); + return (V) (entry == null ? null : entry.valueEntry); + } + + final IntHashMap.Entry removeEntry(int p_76036_1_) { + int i = computeHash(p_76036_1_); + int j = getSlotIndex(i, this.slots.length); + IntHashMap.Entry entry = this.slots[j]; + IntHashMap.Entry entry1; + IntHashMap.Entry entry2; + + for (entry1 = entry; entry1 != null; entry1 = entry2) { + entry2 = entry1.nextEntry; + + if (entry1.hashEntry == p_76036_1_) { + --this.count; + + if (entry == entry1) { + this.slots[j] = entry2; + } else { + entry.nextEntry = entry2; + } + + return entry1; + } + + entry = entry1; + } + + return entry1; + } + + /** + * Removes all entries from the map + */ + public void clearMap() { + IntHashMap.Entry[] entry = this.slots; + + for (int i = 0; i < entry.length; ++i) { + entry[i] = null; + } + + this.count = 0; + } + + /** + * Adds an object to a slot + */ + private void insert(int p_76040_1_, int p_76040_2_, V p_76040_3_, int p_76040_4_) { + IntHashMap.Entry entry = this.slots[p_76040_4_]; + this.slots[p_76040_4_] = new IntHashMap.Entry(p_76040_1_, p_76040_2_, p_76040_3_, entry); + + if (this.count++ >= this.threshold) { + this.grow(2 * this.slots.length); + } + } + + static class Entry { + /** + * The hash code of this entry + */ + final int hashEntry; + /** + * The object stored in this entry + */ + V valueEntry; + IntHashMap.Entry nextEntry; + /** + * The id of the hash slot computed from the hash + */ + final int slotHash; + + Entry(int p_i1552_1_, int p_i1552_2_, V p_i1552_3_, IntHashMap.Entry p_i1552_4_) { + this.valueEntry = p_i1552_3_; + this.nextEntry = p_i1552_4_; + this.hashEntry = p_i1552_2_; + this.slotHash = p_i1552_1_; + } + + /** + * Returns the hash code for this entry + */ + public final int getHash() { + return this.hashEntry; + } + + /** + * Returns the object stored in this entry + */ + public final V getValue() { + return this.valueEntry; + } + + public final boolean equals(Object p_equals_1_) { + if (!(p_equals_1_ instanceof IntHashMap.Entry)) { + return false; + } else { + IntHashMap.Entry entry = (IntHashMap.Entry) p_equals_1_; + Object object = Integer.valueOf(this.getHash()); + Object object1 = Integer.valueOf(entry.getHash()); + + if (object == object1 || object != null && object.equals(object1)) { + Object object2 = this.getValue(); + Object object3 = entry.getValue(); + + if (object2 == object3 || object2 != null && object2.equals(object3)) { + return true; + } + } + + return false; + } + } + + public final int hashCode() { + return IntHashMap.computeHash(this.hashEntry); + } + + public final String toString() { + return this.getHash() + "=" + this.getValue(); + } + } +} \ No newline at end of file diff --git a/src/main/java/noppes/npcs/ai/pathfinder/FlyPathPoint.java b/src/main/java/noppes/npcs/ai/pathfinder/FlyPathPoint.java index 87af78ae7..e08dd1bd0 100644 --- a/src/main/java/noppes/npcs/ai/pathfinder/FlyPathPoint.java +++ b/src/main/java/noppes/npcs/ai/pathfinder/FlyPathPoint.java @@ -25,6 +25,8 @@ public class FlyPathPoint extends PathPoint public FlyPathPoint previous; /** Indicates this is the origin */ public boolean isFirst; + public float costMalus; + public PathNodeType nodeType = PathNodeType.BLOCKED; private static final String __OBFID = "CL_00000574"; public FlyPathPoint(int p_i2135_1_, int p_i2135_2_, int p_i2135_3_) @@ -44,7 +46,7 @@ public class FlyPathPoint extends PathPoint /** * Returns the linear distance to another path point */ - public float distanceTo(net.minecraft.pathfinding.PathPoint p_75829_1_) + public float distanceTo(FlyPathPoint p_75829_1_) { float f = (float)(p_75829_1_.xCoord - this.xCoord); float f1 = (float)(p_75829_1_.yCoord - this.yCoord); @@ -55,7 +57,7 @@ public class FlyPathPoint extends PathPoint /** * Returns the squared distance to another path point */ - public float distanceToSquared(net.minecraft.pathfinding.PathPoint p_75832_1_) + public float distanceToSquared(FlyPathPoint p_75832_1_) { float f = (float)(p_75832_1_.xCoord - this.xCoord); float f1 = (float)(p_75832_1_.yCoord - this.yCoord); diff --git a/src/main/java/noppes/npcs/ai/pathfinder/PathNavigateFlying.java b/src/main/java/noppes/npcs/ai/pathfinder/PathNavigateFlying.java index fc52af434..ee8b76e40 100644 --- a/src/main/java/noppes/npcs/ai/pathfinder/PathNavigateFlying.java +++ b/src/main/java/noppes/npcs/ai/pathfinder/PathNavigateFlying.java @@ -505,7 +505,7 @@ public class PathNavigateFlying extends PathNavigate { int i2 = j + l; int j2 = k + l; ChunkCache chunkcache = new ChunkCache(this.theEntity.worldObj, i1, j1, k1, l1, i2, j2, 0); - FlyPathEntity pathentity = (new FlyPathFinder(chunkcache, p_72865_4_, p_72865_5_, p_72865_6_, p_72865_7_)).createEntityPathTo(p_72865_1_, p_72865_2_, p_72865_3_); + FlyPathEntity pathentity = (new FlyPathFinder(chunkcache, p_72865_4_, p_72865_5_, p_72865_6_, p_72865_7_, p_72865_1_)).createEntityPathTo(p_72865_1_, p_72865_2_, p_72865_3_); this.theEntity.worldObj.theProfiler.endSection(); return pathentity; } @@ -524,7 +524,7 @@ public class PathNavigateFlying extends PathNavigate { int l2 = i1 + k1; int i3 = j1 + k1; ChunkCache chunkcache = new ChunkCache(this.theEntity.worldObj, l1, i2, j2, k2, l2, i3, 0); - FlyPathEntity pathentity = (new FlyPathFinder(chunkcache, p_72844_6_, p_72844_7_, p_72844_8_, p_72844_9_)).createEntityPathTo(p_72844_1_, p_72844_2_, p_72844_3_, p_72844_4_, p_72844_5_); + FlyPathEntity pathentity = (new FlyPathFinder(chunkcache, p_72844_6_, p_72844_7_, p_72844_8_, p_72844_9_, p_72844_1_)).createEntityPathTo(p_72844_1_, p_72844_2_, p_72844_3_, p_72844_4_, p_72844_5_); this.theEntity.worldObj.theProfiler.endSection(); return pathentity; } diff --git a/src/main/java/noppes/npcs/ai/pathfinder/PathNodeType.java b/src/main/java/noppes/npcs/ai/pathfinder/PathNodeType.java new file mode 100644 index 000000000..f49fe3c59 --- /dev/null +++ b/src/main/java/noppes/npcs/ai/pathfinder/PathNodeType.java @@ -0,0 +1,34 @@ +package noppes.npcs.ai.pathfinder; + +public enum PathNodeType +{ + BLOCKED(-1.0F), + OPEN(0.0F), + WALKABLE(0.0F), + TRAPDOOR(0.0F), + FENCE(-1.0F), + LAVA(-1.0F), + WATER(8.0F), + RAIL(0.0F), + DANGER_FIRE(8.0F), + DAMAGE_FIRE(16.0F), + DANGER_CACTUS(8.0F), + DAMAGE_CACTUS(-1.0F), + DANGER_OTHER(8.0F), + DAMAGE_OTHER(-1.0F), + DOOR_OPEN(0.0F), + DOOR_WOOD_CLOSED(-1.0F), + DOOR_IRON_CLOSED(-1.0F); + + private final float priority; + + private PathNodeType(float priorityIn) + { + this.priority = priorityIn; + } + + public float getPriority() + { + return this.priority; + } +} \ No newline at end of file