-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
180 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/java/dev/hilligans/ourcraft/Server/Concurrent/IAtomicSubChunk.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package dev.hilligans.ourcraft.Server.Concurrent; | ||
|
||
import dev.hilligans.ourcraft.Block.BlockState.IBlockState; | ||
import dev.hilligans.ourcraft.World.NewWorldSystem.ISubChunk; | ||
|
||
public interface IAtomicSubChunk extends ISubChunk { | ||
|
||
IBlockState setBlockStateAtomic(int x, int y, int 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 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(int x, int y, int z, IBlockState expected, IBlockState to); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
src/main/java/dev/hilligans/ourcraft/World/NewWorldSystem/GlobalPaletteAtomicSubChunk.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
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; | ||
import java.util.ArrayList; | ||
|
||
public class GlobalPaletteAtomicSubChunk implements IAtomicSubChunk { | ||
|
||
static final VarHandle ARRAY_HANDLE = MethodHandles.arrayElementVarHandle(int[].class); | ||
public ArrayList<IBlockState> blockStates; | ||
public int[] blocks = new int[16 * 16 * 16 / 4]; | ||
|
||
public GlobalPaletteAtomicSubChunk(ArrayList<IBlockState> blockStates) { | ||
this.blockStates = blockStates; | ||
if(blockStates.size() > Short.MAX_VALUE * 2) { | ||
throw new RuntimeException("Unable to use this subchunk impl"); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public int getWidth() { | ||
return 16; | ||
} | ||
|
||
@Override | ||
public int getHeight() { | ||
return 16; | ||
} | ||
|
||
@Override | ||
public IBlockState getBlockState(int x, int y, int z) { | ||
return blockStates.get((blocks[getIndex(x, y, z)] >> 16 * (z & 0b11)) & 0xFFFF); | ||
} | ||
|
||
@Override | ||
public IBlockState setBlockState(int x, int y, int z, IBlockState blockState) { | ||
int shift = 16 * (z & 0b11); | ||
int id = blockState.getBlockStateID() << shift; | ||
int and = 0xFFFF << shift; | ||
int index = getIndex(x, y, z); | ||
int v = blocks[index]; | ||
blocks[index] = v & and | id; | ||
return blockStates.get((v >> shift) & 0xFFFF); | ||
} | ||
|
||
@Override | ||
public IBlockState setBlockStateAtomic(int x, int y, int z, IBlockState blockState) { | ||
int shift = 16 * (z & 0b11); | ||
int id = blockState.getBlockStateID() << shift; | ||
int and = 0xFFFF << shift; | ||
int index = getIndex(x, y, z); | ||
int v; | ||
do { | ||
v = (int) ARRAY_HANDLE.getVolatile(blocks, index); | ||
} while (!ARRAY_HANDLE.weakCompareAndSet(blocks, index, v, v & and | id)); | ||
|
||
return blockStates.get((v >> shift) & 0xFFFF); | ||
} | ||
|
||
@Override | ||
public boolean swapBlockStateAtomic(int x, int y, int z, IBlockState expected, IBlockState to) { | ||
int shift = 16 * (z & 0b11); | ||
int newID = to.getBlockStateID() << shift; | ||
int oldID = expected.getBlockStateID() << shift; | ||
int and = 0xFFFF << shift; | ||
int index = getIndex(x, y, z); | ||
int v; | ||
do { | ||
v = (int) ARRAY_HANDLE.getVolatile(blocks, index); | ||
if((v & and) != oldID) { | ||
return false; | ||
} | ||
} while (!ARRAY_HANDLE.weakCompareAndSet(blocks, index, v, v & and | newID)); | ||
return true; | ||
} | ||
|
||
public static int getIndex(int x, int y, int z) { | ||
return ((x * 16) + y) * 4 + (z >> 2); | ||
} | ||
|
||
@Override | ||
public boolean isEmpty() { | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters