diff --git a/cubyz-client/src/io/cubyz/client/Cubyz.java b/cubyz-client/src/io/cubyz/client/Cubyz.java index 444e0f37..8f781d95 100644 --- a/cubyz-client/src/io/cubyz/client/Cubyz.java +++ b/cubyz-client/src/io/cubyz/client/Cubyz.java @@ -392,7 +392,7 @@ public class Cubyz implements IGameLogic { if (buildCooldown == 0) { buildCooldown = 10; Vector3i pos = msd.getEmptyPlace(ctx.getCamera().getPosition()); - Block b = inventory.getBlock(inventorySelection); // TODO: add inventory + Block b = inventory.getBlock(inventorySelection); if (b != null && pos != null) { world.placeBlock(pos.x, pos.y, pos.z, b); inventory.addBlock(b, -1); diff --git a/cubyz-client/src/io/cubyz/world/RemoteWorld.java b/cubyz-client/src/io/cubyz/world/RemoteWorld.java index 3737db93..d683c7a2 100644 --- a/cubyz-client/src/io/cubyz/world/RemoteWorld.java +++ b/cubyz-client/src/io/cubyz/world/RemoteWorld.java @@ -36,7 +36,7 @@ public class RemoteWorld extends World { public void removeBlock(int x, int y, int z) {} @Override - public void queueChunk(ChunkAction action) { + public void queueChunk(Chunk ch) { // LOAD would be loading from server, UNLOAD would be unloading from client, and GENERATE would do nothing } diff --git a/cubyz-common/src/io/cubyz/world/LocalWorld.java b/cubyz-common/src/io/cubyz/world/LocalWorld.java index 1bb7b9f8..d738e9a3 100644 --- a/cubyz-common/src/io/cubyz/world/LocalWorld.java +++ b/cubyz-common/src/io/cubyz/world/LocalWorld.java @@ -32,29 +32,26 @@ public class LocalWorld extends World { private ChunkGenerationThread thread; private class ChunkGenerationThread extends Thread { - Deque loadList = new ArrayDeque<>(); // FIFO order (First In, First Out) private static final int MAX_QUEUE_SIZE = 16; + Deque loadList = new ArrayDeque<>(MAX_QUEUE_SIZE); // FIFO order (First In, First Out) - public void queue(ChunkAction ca) { - if (!isQueued(ca)) { - if (loadList.size() > MAX_QUEUE_SIZE) { + public void queue(Chunk ch) { + if (!isQueued(ch)) { + if (loadList.size() == MAX_QUEUE_SIZE) { CubyzLogger.instance.info("Hang on, the Local-Chunk-Thread's queue is full, blocking!"); while (!loadList.isEmpty()) { System.out.print(""); // again, used as replacement to Thread.onSpinWait(), also necessary due to some JVM oddities } } - loadList.add(ca); + loadList.add(ch); } } - public boolean isQueued(ChunkAction ca) { - ChunkAction[] list = loadList.toArray(new ChunkAction[0]); - for (ChunkAction ch : list) { - if (ch != null) { - if (ch.chunk == ca.chunk) { - ch.type = ca.type; - return true; - } + public boolean isQueued(Chunk ch) { + Chunk[] list = loadList.toArray(new Chunk[0]); + for (Chunk ch2 : list) { + if (ch2 == ch) { + return true; } } return false; @@ -63,23 +60,11 @@ public class LocalWorld extends World { public void run() { while (true) { if (!loadList.isEmpty()) { - ChunkAction popped = loadList.pop(); - if (popped.type == ChunkActionType.GENERATE) { -// CubyzLogger.instance.fine("Generating " + popped.chunk.getX() + "," + popped.chunk.getZ()); - synchronousGenerate(popped.chunk); - popped.chunk.load(); - //seed = (int) System.currentTimeMillis(); // enable it if you want fun (don't forget to disable before commit!!!) - } - else if (popped.type == ChunkActionType.LOAD) { -// CubyzLogger.instance.fine("\"Loading\" " + popped.chunk.getX() + "," + popped.chunk.getZ()); - if(!popped.chunk.isLoaded()) { - popped.chunk.setLoaded(true); - } - } - else if (popped.type == ChunkActionType.UNLOAD) { -// CubyzLogger.instance.fine("\"Unloading\" " + popped.chunk.getX() + "," + popped.chunk.getZ()); - popped.chunk.setLoaded(false); - } + Chunk popped = loadList.pop(); +// CubyzLogger.instance.fine("Generating " + popped.chunk.getX() + "," + popped.chunk.getZ()); + synchronousGenerate(popped); + popped.load(); + //seed = (int) System.currentTimeMillis(); // enable it if you want fun (don't forget to disable before commit!!!) } System.out.print(""); } @@ -238,28 +223,29 @@ public class LocalWorld extends World { } @Override - public void queueChunk(ChunkAction action) { - thread.queue(action); + public void queueChunk(Chunk ch) { + thread.queue(ch); } @Override public void seek(int x, int z) { - int renderDistance/*minus 1*/ = 4; - int blockDistance = renderDistance*16; - for (int x1 = x - blockDistance-48; x1 <= x + blockDistance+48; x1 += 16) { - for (int z1 = z - blockDistance-48; z1 <= z + blockDistance+48; z1 += 16) { + int renderDistance = 5; + int blockDistance = renderDistance << 4; + int minX = x-blockDistance; // Avoid + int maxX = x+blockDistance; // recalculating + int minZ = z-blockDistance; // them + int maxZ = z+blockDistance; // . + for (int x1 = minX-48; x1 <= maxX+48; x1 += 16) { + for (int z1 = minZ-48; z1 <= maxZ+48; z1 += 16) { Chunk ch = getChunk(x1/16,z1/16); - if (x1>x-blockDistance&&x1z-blockDistance&&z1 minX && x1 < maxX && z1 > minZ && z1 < maxZ) { if (!ch.isGenerated()) { - queueChunk(new ChunkAction(ch, ChunkActionType.GENERATE)); - } - else { - queueChunk(new ChunkAction(ch, ChunkActionType.LOAD)); - } - } else if (x1 < x-blockDistance - 16 || x1 > x + blockDistance + 16 || z1 < z - blockDistance - 16 || z1 > z + blockDistance +16) { - if (ch.isLoaded()) { - queueChunk(new ChunkAction(ch, ChunkActionType.UNLOAD)); + queueChunk(ch); + } else { + ch.setLoaded(true); } + } else if (ch.isLoaded() && (x1 < minX || x1 > maxX || z1 < minZ || z1 > maxZ)) { + ch.setLoaded(false); } } } diff --git a/cubyz-common/src/io/cubyz/world/World.java b/cubyz-common/src/io/cubyz/world/World.java index b0577cef..9ccca8c0 100644 --- a/cubyz-common/src/io/cubyz/world/World.java +++ b/cubyz-common/src/io/cubyz/world/World.java @@ -33,16 +33,6 @@ public abstract class World { UNLOAD; } - public static class ChunkAction { - public Chunk chunk; - public ChunkActionType type; - - public ChunkAction(Chunk chunk, ChunkActionType type) { - this.chunk = chunk; - this.type = type; - } - } - public abstract Player getLocalPlayer(); public int getHeight() { @@ -60,7 +50,7 @@ public abstract class World { * * @param action - Chunk action */ - public abstract void queueChunk(ChunkAction action); + public abstract void queueChunk(Chunk ch); public abstract Chunk getChunk(int x, int z); public abstract BlockInstance getBlock(int x, int y, int z);