oh boy i hope this won't conflict

pull/12/head
SuperCoder7979 2020-02-29 17:58:09 -05:00
parent ed528cb670
commit 96804781ad
7 changed files with 28 additions and 20 deletions

View File

@ -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);

View File

@ -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: <save dir>/<dim>/<chunkX>/<chunkZ>/<chunkY>.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;
}

View File

@ -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
}
}
}

View File

@ -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); }

View File

@ -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);
}

View File

@ -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;

View File

@ -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;
}