Amazingly fast block getter

This commit is contained in:
Felix Cravic 2020-04-26 16:51:00 +02:00
parent 005d95e97b
commit 1639a4947c
3 changed files with 22 additions and 13 deletions

View File

@ -21,6 +21,7 @@ public class AStarPathfinder {
public static LinkedList<BlockPosition> getPath(Instance instance, public static LinkedList<BlockPosition> getPath(Instance instance,
BlockPosition start, BlockPosition end, BlockPosition start, BlockPosition end,
int maxCheck) { int maxCheck) {
long time = System.nanoTime();
List<Node> open = new ArrayList<>(); List<Node> open = new ArrayList<>();
List<Node> closed = new ArrayList<>(); List<Node> closed = new ArrayList<>();
@ -37,6 +38,7 @@ public class AStarPathfinder {
closed.add(current); closed.add(current);
if (isTargetNode(end, current)) { if (isTargetNode(end, current)) {
System.out.println("found in: " + (System.nanoTime() - time) + " ns");
return buildPath(current); return buildPath(current);
} }

View File

@ -37,9 +37,9 @@ public class Chunk implements Viewable {
private Biome biome; private Biome biome;
private int chunkX, chunkZ; private int chunkX, chunkZ;
// blocks id based on coord // blocks id based on coord, see Chunk#getBlockIndex
private short[][][] blocksId = new short[CHUNK_SIZE_X][CHUNK_SIZE_Y][CHUNK_SIZE_Z]; private short[] blocksId = new short[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z];
private short[][][] customBlocksId = new short[CHUNK_SIZE_X][CHUNK_SIZE_Y][CHUNK_SIZE_Z]; private short[] customBlocksId = new short[CHUNK_SIZE_X * CHUNK_SIZE_Y * CHUNK_SIZE_Z];
// Used to get all blocks with data (no null) // Used to get all blocks with data (no null)
// Key is still chunk coord // Key is still chunk coord
@ -98,7 +98,7 @@ public class Chunk implements Viewable {
} else { } else {
// Block has been deleted, clear cache and return // Block has been deleted, clear cache and return
this.blocksId[x][y][z] = 0; // Set to air this.blocksId[getBlockIndex(x, y, z)] = 0; // Set to air
//this.blocks.remove(index); //this.blocks.remove(index);
this.blocksData.remove(index); this.blocksData.remove(index);
@ -147,17 +147,17 @@ public class Chunk implements Viewable {
} }
public short getBlockId(int x, int y, int z) { public short getBlockId(int x, int y, int z) {
short id = blocksId[x][y][z]; short id = blocksId[getBlockIndex(x, y, z)];
return id; return id;
} }
public short getCustomBlockId(int x, int y, int z) { public short getCustomBlockId(int x, int y, int z) {
short id = customBlocksId[x][y][z]; short id = customBlocksId[getBlockIndex(x, y, z)];
return id; return id;
} }
public CustomBlock getCustomBlock(int x, int y, int z) { public CustomBlock getCustomBlock(int x, int y, int z) {
short id = customBlocksId[x][y][z]; short id = customBlocksId[getBlockIndex(x, y, z)];
return id != 0 ? BLOCK_MANAGER.getBlock(id) : null; return id != 0 ? BLOCK_MANAGER.getBlock(id) : null;
} }
@ -167,8 +167,8 @@ public class Chunk implements Viewable {
} }
protected void refreshBlockValue(int x, int y, int z, short blockId, short customId) { protected void refreshBlockValue(int x, int y, int z, short blockId, short customId) {
this.blocksId[x][y][z] = blockId; this.blocksId[getBlockIndex(x, y, z)] = blockId;
this.customBlocksId[x][y][z] = customId; this.customBlocksId[getBlockIndex(x, y, z)] = customId;
} }
protected void refreshBlockValue(int x, int y, int z, short blockId) { protected void refreshBlockValue(int x, int y, int z, short blockId) {
@ -340,4 +340,9 @@ public class Chunk implements Viewable {
public Set<Player> getViewers() { public Set<Player> getViewers() {
return Collections.unmodifiableSet(viewers); return Collections.unmodifiableSet(viewers);
} }
private int getBlockIndex(int x, int y, int z) {
int index = (((y * 16) + x) * 16) + z;
return index;
}
} }

View File

@ -106,10 +106,12 @@ public class InstanceContainer extends Instance {
public void refreshBlockId(int x, int y, int z, short blockId) { public void refreshBlockId(int x, int y, int z, short blockId) {
Chunk chunk = getChunkAt(x, z); Chunk chunk = getChunkAt(x, z);
synchronized (chunk) { synchronized (chunk) {
byte chunkX = (byte) (x % 16); int chunkX = x % 16;
byte chunkY = (byte) y; int chunkY = y;
byte chunkZ = (byte) (z % 16); int chunkZ = z % 16;
int index = SerializerUtils.chunkCoordToIndex(chunkX, chunkY, chunkZ);
chunkX = ChunkUtils.refreshChunkXZ(chunkX);
chunkZ = ChunkUtils.refreshChunkXZ(chunkZ);
chunk.refreshBlockValue(chunkX, chunkY, chunkZ, blockId); chunk.refreshBlockValue(chunkX, chunkY, chunkZ, blockId);