Skip to content

Commit

Permalink
Code cleanup and added atomic chunks
Browse files Browse the repository at this point in the history
  • Loading branch information
Hilligans committed Oct 10, 2023
1 parent 88159c0 commit 1f411d0
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 71 deletions.
25 changes: 25 additions & 0 deletions src/main/java/dev/hilligans/ourcraft/Server/Concurrent/Lock.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,37 @@ public void notifyOfUnlock() {
}
}

public boolean hasLock(ChunkPos chunkPos) {
if(!hasAllLocks) {
return false;
}
for(ChunkPos chunkPos1 : chunkPositions) {
if(chunkPos1.equals(chunkPos)) {
return true;
}
}
return false;
}

public boolean hasAllLocks() {
return hasAllLocks;
}

public void acquire(long... positions) {}

public void acquire(ChunkPos chunkPos) {
for(ChunkPos chunkPos1 : chunkPositions) {
if(chunkPos1.equals(chunkPos)) {
acquire();
return;
}
}
ChunkPos[] chunkPosList = new ChunkPos[chunkPositions.length + 1];
System.arraycopy(chunkPositions,0, chunkPosList, 0, chunkPositions.length);
chunkPosList[chunkPositions.length] = chunkPos;
this.chunkPositions = chunkPosList;
}

public void acquire() {
chunkLocker.acquire(this);
}
Expand Down
69 changes: 1 addition & 68 deletions src/main/java/dev/hilligans/ourcraft/World/Chunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.glGetBufferSubData;

public class Chunk implements IMeshSource, IChunk {
public class Chunk implements IChunk {

public ArrayList<SubChunk> chunks = new ArrayList<>();

Expand Down Expand Up @@ -80,14 +80,6 @@ public void tick() {
}
}

public int getTotalVertices() {
int vertices = 0;
for(SubChunk subChunk : chunks) {
vertices += subChunk.verticesCount;
}
return vertices;
}

public void scheduleTick(BlockPos pos, int time) {
if(world.isServer()) {
long futureTime = ((ServerWorld)world).server.getTime() + time;
Expand All @@ -103,40 +95,6 @@ public void setWorld(World world) {
}
}

///TODO fix
public void build(IGraphicsEngine<?,?,?> graphicsEngine) {
if(solidMesh.id != -1) {
graphicsEngine.getDefaultImpl().destroyMesh(null,null,solidMesh.id);
}
PrimitiveBuilder primitiveBuilder = new PrimitiveBuilder(GL_TRIANGLES, ShaderManager.worldShader);
for(int x = 0; x < 16; x++) {
for(int y = 0; y < 256; y++) {
for(int z = 0; z < 16; z++) {
BlockState block = getBlockState(x,y,z);
for(int a = 0; a < 6; a++) {
if(block.getBlock() != Blocks.AIR) {
BlockState blockState = getBlockState(new BlockPos(x, y, z).add(Block.getBlockPos(block.getBlock().getSide(block,a))));
if ((blockState.getBlock().blockProperties.transparent && (Settings.renderSameTransparent || block.getBlock() != blockState.getBlock())) || block.getBlock().blockProperties.alwaysRender) {
block.getBlock().addVertices(primitiveBuilder,a,1.0f,block,new BlockPos(x + this.x * 16,y,z + this.z * 16),x,z);
}
}
}
}
}
}
int id = (int) graphicsEngine.getDefaultImpl().createMesh(null,null,primitiveBuilder.toVertexMesh().setVertexFormat("position_color_texture"));
solidMesh.set(id,primitiveBuilder.indices.size());
}

public void destroy() {
for(SubChunk subChunk : chunks) {
subChunk.destroy();
}
blockTicks.clear();
entities.clear();
dataProviders.clear();
}

public void destroyMap(int newId) {
if(id != -1 && id != -2 && id != -3) {
VAOManager.destroyBuffer(id);
Expand Down Expand Up @@ -223,8 +181,6 @@ public int interpolate(int height, int xHeight, int zHeight) {
return Math.round(((float)height + xHeight + zHeight) / 3);
}

private final double size = 400;

public Biome getBiome1(int x, int z) {
return world.biomeMap.getBiome(x,z);
}
Expand Down Expand Up @@ -318,13 +274,6 @@ public BlockState getBlockState(long x, long y, long z) {
return subChunk.getBlock((int) (x & 15), (int) (y & 15), (int) (z & 15));
}

public BlockState getBlockStateChecked(int x, int y, int z) {
if(x > 15 || x < 0 || z > 15 || z < 0) {
return world.getBlockState(x,y,z);
}
return getBlockState(x,y,z);
}

public BlockState getBlockState(BlockPos blockPos) {
return getBlockState(blockPos.x,blockPos.y,blockPos.z);
}
Expand Down Expand Up @@ -437,14 +386,6 @@ public SubChunk getSubChunk(int pos) {
return chunks.get(pos);
}

public MeshHolder getSolidMesh() {
return solidMesh;
}

public MeshHolder getTranslucentMesh() {
return translucentMesh;
}

@Override
public String toString() {
return "Chunk{" +
Expand All @@ -453,14 +394,6 @@ public String toString() {
", z=" + z +
'}';
}

@Override
public VertexMesh buildMesh(Client client) {

VertexMesh vertexMesh = new VertexMesh("position_texture_globalColor");

return null;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import dev.hilligans.ourcraft.Block.BlockState.IBlockState;
import dev.hilligans.ourcraft.Block.Blocks;
import dev.hilligans.ourcraft.Data.Other.ChunkPos;
import dev.hilligans.ourcraft.Server.Concurrent.Lock;

import java.util.function.Consumer;

public class CubicChunk implements IChunk {
public class CubicChunk implements IAtomicChunk {

public int size;
public int x;
Expand Down Expand Up @@ -83,6 +85,40 @@ public void setBlockState(long x, long y, long z, IBlockState blockState) {
subChunk.setBlockState((int) (x & 15), (int) (y & 15), (int) (z & 15), blockState);
}

@Override
public void setBlockStateAtomic(Lock lock, long x, long y, long z, IBlockState blockState) {
if(!lock.hasLock(new ChunkPos(this.x, this.y, this.z))) {
int index = getIndex(x & 31, y & 31, z & 31);
synchronized (this) {
ISubChunk subChunk = subChunks[index];
if(subChunk instanceof IAtomicSubChunk atomicSubChunk) {
atomicSubChunk.setBlockStateAtomic((int) (x & 15), (int) (y & 15), (int) (z & 15), blockState);
return;
}
}
lock.acquire(new ChunkPos(this.x, this.y, this.z));
}
setBlockState(x, y, z, blockState);
}

@Override
public boolean swapBlockStateAtomic(Lock lock, long x, long y, long z, IBlockState expected, IBlockState to) {
if(!lock.hasLock(new ChunkPos(this.x, this.y, this.z))) {
int index = getIndex(x & 31, y & 31, z & 31);
synchronized (this) {
ISubChunk subChunk = subChunks[index];
if(subChunk instanceof IAtomicSubChunk atomicSubChunk) {
return atomicSubChunk.swapBlockStateAtomic((int) (x & 15), (int) (y & 15), (int) (z & 15), expected, to);
}
}
lock.acquire(new ChunkPos(this.x, this.y, this.z));
}
if(getBlockState1(x, y, z) == expected) {
setBlockState(x, y, z, to);
}
return true;
}

@Override
public void setChunkPosition(long x, long y, long z) {
this.x = (int) x;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package dev.hilligans.ourcraft.World.NewWorldSystem;

import dev.hilligans.ourcraft.Block.BlockState.IBlockState;
import dev.hilligans.ourcraft.Server.Concurrent.IAtomicSubChunk;

import java.lang.invoke.MethodHandles;
import java.lang.invoke.VarHandle;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.hilligans.ourcraft.World.NewWorldSystem;

import dev.hilligans.ourcraft.Block.BlockState.IBlockState;
import dev.hilligans.ourcraft.Server.Concurrent.Lock;

public interface IAtomicChunk extends IChunk {


/**
* @param lock a lock provided to the chunk in the event that a subchunk isn't atomic and needs to grab the lock, the lock must also be manually freed after
*/
void setBlockStateAtomic(Lock lock, long x, long y, long z, IBlockState blockState);


/**
* Used to replace an old blockstate with a new one
* useful if you want to do say a plant growing, so you can change the blockstate without having to grab the write lock
* @param lock a lock provided to the chunk in the event that a subchunk isn't atomic and needs to grab the lock, the lock must also be manually freed after
* @param x x pos in subchunk
* @param y y pos in subchunk
* @param z z pos in subchunk
* @param expected the blockstate to replace if it still exists
* @param to the new blockstate
* @return true if the expected blockstate match and the block was set, false otherwise
*/
boolean swapBlockStateAtomic(Lock lock, long x, long y, long z, IBlockState expected, IBlockState to);

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dev.hilligans.ourcraft.Server.Concurrent;
package dev.hilligans.ourcraft.World.NewWorldSystem;

import dev.hilligans.ourcraft.Block.BlockState.IBlockState;
import dev.hilligans.ourcraft.World.NewWorldSystem.ISubChunk;
Expand Down

0 comments on commit 1f411d0

Please sign in to comment.