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

View File

@ -37,9 +37,9 @@ public class Chunk implements Viewable {
private Biome biome;
private int chunkX, chunkZ;
// blocks id based on coord
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];
// blocks id based on coord, see Chunk#getBlockIndex
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];
// Used to get all blocks with data (no null)
// Key is still chunk coord
@ -98,7 +98,7 @@ public class Chunk implements Viewable {
} else {
// 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.blocksData.remove(index);
@ -147,17 +147,17 @@ public class Chunk implements Viewable {
}
public short getBlockId(int x, int y, int z) {
short id = blocksId[x][y][z];
short id = blocksId[getBlockIndex(x, y, z)];
return id;
}
public short getCustomBlockId(int x, int y, int z) {
short id = customBlocksId[x][y][z];
short id = customBlocksId[getBlockIndex(x, y, z)];
return id;
}
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;
}
@ -167,8 +167,8 @@ public class Chunk implements Viewable {
}
protected void refreshBlockValue(int x, int y, int z, short blockId, short customId) {
this.blocksId[x][y][z] = blockId;
this.customBlocksId[x][y][z] = customId;
this.blocksId[getBlockIndex(x, y, z)] = blockId;
this.customBlocksId[getBlockIndex(x, y, z)] = customId;
}
protected void refreshBlockValue(int x, int y, int z, short blockId) {
@ -340,4 +340,9 @@ public class Chunk implements Viewable {
public Set<Player> getViewers() {
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) {
Chunk chunk = getChunkAt(x, z);
synchronized (chunk) {
byte chunkX = (byte) (x % 16);
byte chunkY = (byte) y;
byte chunkZ = (byte) (z % 16);
int index = SerializerUtils.chunkCoordToIndex(chunkX, chunkY, chunkZ);
int chunkX = x % 16;
int chunkY = y;
int chunkZ = z % 16;
chunkX = ChunkUtils.refreshChunkXZ(chunkX);
chunkZ = ChunkUtils.refreshChunkXZ(chunkZ);
chunk.refreshBlockValue(chunkX, chunkY, chunkZ, blockId);