From 96804781adbad775508d30f1f7819cd3203ff5c7 Mon Sep 17 00:00:00 2001 From: SuperCoder7979 Date: Sat, 29 Feb 2020 17:58:09 -0500 Subject: [PATCH] oh boy i hope this won't conflict --- ...{DataStorage.java => SODSerializable.java} | 2 +- .../litecraft/save/LitecraftSave.java | 4 ++-- .../halotroop/litecraft/world/Chunk.java | 22 ++++++++++++------- .../halotroop/litecraft/world/World.java | 10 ++++----- .../litecraft/world/gen/ChunkGenerator.java | 3 ++- .../world/gen/EarthChunkGenerator.java | 5 +++-- .../hydos/ginger/main/settings/Constants.java | 2 +- 7 files changed, 28 insertions(+), 20 deletions(-) rename src/main/java/com/github/halotroop/litecraft/logic/{DataStorage.java => SODSerializable.java} (81%) diff --git a/src/main/java/com/github/halotroop/litecraft/logic/DataStorage.java b/src/main/java/com/github/halotroop/litecraft/logic/SODSerializable.java similarity index 81% rename from src/main/java/com/github/halotroop/litecraft/logic/DataStorage.java rename to src/main/java/com/github/halotroop/litecraft/logic/SODSerializable.java index b58c192..995bc61 100644 --- a/src/main/java/com/github/halotroop/litecraft/logic/DataStorage.java +++ b/src/main/java/com/github/halotroop/litecraft/logic/SODSerializable.java @@ -2,7 +2,7 @@ package com.github.halotroop.litecraft.logic; import tk.valoeghese.sod.BinaryData; -public interface DataStorage +public interface SODSerializable { void read(BinaryData data); diff --git a/src/main/java/com/github/halotroop/litecraft/save/LitecraftSave.java b/src/main/java/com/github/halotroop/litecraft/save/LitecraftSave.java index bab5368..6490035 100644 --- a/src/main/java/com/github/halotroop/litecraft/save/LitecraftSave.java +++ b/src/main/java/com/github/halotroop/litecraft/save/LitecraftSave.java @@ -57,7 +57,7 @@ public final class LitecraftSave } } - public Chunk readChunk(int chunkX, int chunkY, int chunkZ, int dimension) + public Chunk readChunk(World world, int chunkX, int chunkY, int chunkZ, int dimension) { // format: ////.sod File chunkFile = new File(new StringBuilder(this.file.getPath()) @@ -68,7 +68,7 @@ public final class LitecraftSave if (chunkFile.isFile()) { BinaryData data = BinaryData.read(chunkFile); - Chunk result = new Chunk(chunkX, chunkY, chunkZ, dimension); // create chunk + Chunk result = new Chunk(world, chunkX, chunkY, chunkZ, dimension); // create chunk result.read(data); // load the chunk data we have just read into the chunk return result; } diff --git a/src/main/java/com/github/halotroop/litecraft/world/Chunk.java b/src/main/java/com/github/halotroop/litecraft/world/Chunk.java index e8e7cae..02e6daa 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/Chunk.java +++ b/src/main/java/com/github/halotroop/litecraft/world/Chunk.java @@ -1,10 +1,11 @@ package com.github.halotroop.litecraft.world; +import java.util.Arrays; import java.util.function.ToIntFunction; import org.joml.Vector3f; -import com.github.halotroop.litecraft.logic.DataStorage; +import com.github.halotroop.litecraft.logic.SODSerializable; import com.github.halotroop.litecraft.types.block.*; import com.github.halotroop.litecraft.world.block.BlockRenderer; import com.github.halotroop.litecraft.world.gen.WorldGenConstants; @@ -13,7 +14,7 @@ import it.unimi.dsi.fastutil.ints.*; import it.unimi.dsi.fastutil.objects.*; import tk.valoeghese.sod.*; -public class Chunk implements BlockAccess, WorldGenConstants, DataStorage +public class Chunk implements BlockAccess, WorldGenConstants, SODSerializable { /** @param x in-chunk x coordinate. * @param y in-chunk y coordinate. @@ -30,9 +31,10 @@ public class Chunk implements BlockAccess, WorldGenConstants, DataStorage private boolean fullyGenerated = false; public final int dimension; private boolean dirty = true; - private BlockInstance[] renderedBlocks; + private World world; + private BlockInstance[] renderedBlocks = new BlockInstance[CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE]; - public Chunk(int chunkX, int chunkY, int chunkZ, int dimension) + public Chunk(World world, int chunkX, int chunkY, int chunkZ, int dimension) { this.chunkX = chunkX; this.chunkY = chunkY; @@ -41,6 +43,7 @@ public class Chunk implements BlockAccess, WorldGenConstants, DataStorage this.chunkStartY = chunkY << POS_SHIFT; this.chunkStartZ = chunkZ << POS_SHIFT; this.dimension = dimension; + this.world = world; } public boolean doRender() @@ -53,14 +56,14 @@ public class Chunk implements BlockAccess, WorldGenConstants, DataStorage public Block getBlock(int x, int y, int z) { if (x > CHUNK_SIZE || y > CHUNK_SIZE || z > CHUNK_SIZE || x < 0 || y < 0 || z < 0) - { throw new RuntimeException("Block [" + x + ", " + y + ", " + z + ", " + "] out of chunk bounds!"); } + { throw new RuntimeException("Block [" + x + ", " + y + ", " + z + "] out of chunk bounds!"); } return blocks[index(x, y, z)]; } public BlockInstance getBlockInstance(int x, int y, int z) { if (x > CHUNK_SIZE || y > CHUNK_SIZE || z > CHUNK_SIZE || x < 0 || y < 0 || z < 0) - { throw new RuntimeException("Block [" + x + ", " + y + ", " + z + ", " + "] out of chunk bounds!"); } + { throw new RuntimeException("Block [" + x + ", " + y + ", " + z + "] out of chunk bounds!"); } return this.blockEntities[index(x, y, z)]; } @@ -71,7 +74,7 @@ public class Chunk implements BlockAccess, WorldGenConstants, DataStorage if (dirty) { dirty = false; - renderedBlocks = new BlockInstance[CHUNK_SIZE * CHUNK_SIZE * CHUNK_SIZE]; + Arrays.fill(renderedBlocks, null); for (int x = 0; x < CHUNK_SIZE; x++) { for (int y = 0; y < CHUNK_SIZE; y++) @@ -79,6 +82,7 @@ public class Chunk implements BlockAccess, WorldGenConstants, DataStorage for (int z = 0; z < CHUNK_SIZE; z++) { BlockInstance block = getBlockInstance(x, y, z); + if (x == 0 || x == CHUNK_SIZE - 1 || z == 0 || z == CHUNK_SIZE - 1 || y == 0 || y == CHUNK_SIZE - 1) { renderedBlocks[index(x, y, z)] = block; @@ -89,11 +93,13 @@ public class Chunk implements BlockAccess, WorldGenConstants, DataStorage if (getBlockInstance(x - 1, y, z).getModel() == null || getBlockInstance(x + 1, y, z).getModel() == null || getBlockInstance(x, y - 1, z).getModel() == null || getBlockInstance(x, y + 1, z).getModel() == null || getBlockInstance(x, y, z - 1).getModel() == null || getBlockInstance(x, y, z + 1).getModel() == null) + { renderedBlocks[index(x, y, z)] = block; + } } catch (NullPointerException e) { // this seems to be a hotspot for errors e.printStackTrace(); // so I can add a debug breakpoint on this line - throw e; + throw e; // e } } } diff --git a/src/main/java/com/github/halotroop/litecraft/world/World.java b/src/main/java/com/github/halotroop/litecraft/world/World.java index 602c088..dc3a411 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/World.java +++ b/src/main/java/com/github/halotroop/litecraft/world/World.java @@ -93,8 +93,8 @@ public class World implements BlockAccess, WorldGenConstants { Chunk chunk = this.chunks.computeIfAbsent(posHash(chunkX, chunkY, chunkZ), pos -> { - Chunk readChunk = save.readChunk(chunkX, chunkY, chunkZ, this.dimension); - return readChunk == null ? this.chunkGenerator.generateChunk(chunkX, chunkY, chunkZ) : readChunk; + Chunk readChunk = save.readChunk(this, chunkX, chunkY, chunkZ, this.dimension); + return readChunk == null ? this.chunkGenerator.generateChunk(this, chunkX, chunkY, chunkZ) : readChunk; }); if (chunk.isFullyGenerated()) return chunk; this.populateChunk(chunkX, chunkY, chunkZ, chunk.chunkStartX, chunk.chunkStartY, chunk.chunkStartZ); @@ -109,9 +109,9 @@ public class World implements BlockAccess, WorldGenConstants if (result != null) return result; // try read a chunk from memory - result = save.readChunk(chunkX, chunkY, chunkZ, this.dimension); + result = save.readChunk(this, chunkX, chunkY, chunkZ, this.dimension); // if neither of those work, generate the chunk - return result == null ? this.chunkGenerator.generateChunk(chunkX, chunkY, chunkZ) : result; + return result == null ? this.chunkGenerator.generateChunk(this, chunkX, chunkY, chunkZ) : result; } /** @return whether the chunk was unloaded without errors. Will often, but not always, be equal to whether the chunk was already in memory. */ @@ -140,7 +140,7 @@ public class World implements BlockAccess, WorldGenConstants /** @return a chunk that has not neccesarily gone through chunk populating. Used in chunk populating to prevent infinite recursion. */ Chunk getGenChunk(int chunkX, int chunkY, int chunkZ) - { return this.chunks.computeIfAbsent(posHash(chunkX, chunkY, chunkZ), pos -> this.chunkGenerator.generateChunk(chunkX, chunkY, chunkZ)); } + { return this.chunks.computeIfAbsent(posHash(chunkX, chunkY, chunkZ), pos -> this.chunkGenerator.generateChunk(this, chunkX, chunkY, chunkZ)); } long posHash(int chunkX, int chunkY, int chunkZ) { return ((long) chunkX & 0x3FF) | (((long) chunkY & 0x3FF) << 10) | (((long) chunkZ & 0x3FF) << 20); } diff --git a/src/main/java/com/github/halotroop/litecraft/world/gen/ChunkGenerator.java b/src/main/java/com/github/halotroop/litecraft/world/gen/ChunkGenerator.java index 16b0016..3903540 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/gen/ChunkGenerator.java +++ b/src/main/java/com/github/halotroop/litecraft/world/gen/ChunkGenerator.java @@ -1,8 +1,9 @@ package com.github.halotroop.litecraft.world.gen; import com.github.halotroop.litecraft.world.Chunk; +import com.github.halotroop.litecraft.world.World; public interface ChunkGenerator { - Chunk generateChunk(int chunkX, int chunkY, int chunkZ); + Chunk generateChunk(World world, int chunkX, int chunkY, int chunkZ); } diff --git a/src/main/java/com/github/halotroop/litecraft/world/gen/EarthChunkGenerator.java b/src/main/java/com/github/halotroop/litecraft/world/gen/EarthChunkGenerator.java index d398d8f..c1947cf 100644 --- a/src/main/java/com/github/halotroop/litecraft/world/gen/EarthChunkGenerator.java +++ b/src/main/java/com/github/halotroop/litecraft/world/gen/EarthChunkGenerator.java @@ -5,6 +5,7 @@ import java.util.Random; import com.github.halotroop.litecraft.types.block.*; import com.github.halotroop.litecraft.util.noise.OctaveSimplexNoise; import com.github.halotroop.litecraft.world.Chunk; +import com.github.halotroop.litecraft.world.World; public class EarthChunkGenerator implements ChunkGenerator, WorldGenConstants { @@ -21,9 +22,9 @@ public class EarthChunkGenerator implements ChunkGenerator, WorldGenConstants private final int dimension; @Override - public Chunk generateChunk(int chunkX, int chunkY, int chunkZ) + public Chunk generateChunk(World world, int chunkX, int chunkY, int chunkZ) { - Chunk chunk = new Chunk(chunkX, chunkY, chunkZ, this.dimension); + Chunk chunk = new Chunk(world, chunkX, chunkY, chunkZ, this.dimension); for (int x = 0; x < CHUNK_SIZE; x++) { double totalX = x + chunk.chunkStartX; diff --git a/src/main/java/com/github/hydos/ginger/main/settings/Constants.java b/src/main/java/com/github/hydos/ginger/main/settings/Constants.java index 796cbb7..2422081 100644 --- a/src/main/java/com/github/hydos/ginger/main/settings/Constants.java +++ b/src/main/java/com/github/hydos/ginger/main/settings/Constants.java @@ -8,5 +8,5 @@ public class Constants public static Vector3f gravity = new Vector3f(0, 0, 0); public static float jumpPower = 0; public static float turnSpeed = 0; - public static double movementSpeed = 1; + public static double movementSpeed = 2; }