chunk: handle setting blocks via Chunk

Instead of directly calling IntervalTree. This allows clamping indices to avoid out of bounds when converting to and from an array
treemaps-queues-iteration
EmaMaker 2022-08-11 16:44:04 +02:00
parent c18de53919
commit 61838a93b2
1 changed files with 22 additions and 13 deletions

View File

@ -44,27 +44,27 @@ public class Chunk {
public void generatePlane() {
// blocks.print();
// blocks.insertValue(Blocks.STONE, new Interval(0,
// this.setBlocks(Blocks.STONE, 0,
// Config.CHUNK_3DCOORD_MAX_INDEX));
for (int i = 0; i < Config.CHUNK_SIZE; i++) blocks.insertValue(Blocks.STONE, new Interval(coord3DTo1D(0, 0, i), coord3DTo1D(Config.CHUNK_SIZE - 1, 0, i)));
for (int i = 0; i < Config.CHUNK_SIZE; i++) blocks.insertValue(Blocks.DIRT, new Interval(coord3DTo1D(0, 1, i), coord3DTo1D(Config.CHUNK_SIZE - 1, 1, i)));
for (int i = 0; i < Config.CHUNK_SIZE; i++) blocks.insertValue(Blocks.GRASS, new Interval(coord3DTo1D(0, 2, i), coord3DTo1D(Config.CHUNK_SIZE - 1, 2, i)));
for (int i = 0; i < Config.CHUNK_SIZE; i++) this.setBlocks(Blocks.STONE, coord3DTo1D(0, 0, i), coord3DTo1D(Config.CHUNK_SIZE - 1, 0, i));
for (int i = 0; i < Config.CHUNK_SIZE; i++) this.setBlocks(Blocks.DIRT, coord3DTo1D(0, 1, i), coord3DTo1D(Config.CHUNK_SIZE - 1, 1, i));
for (int i = 0; i < Config.CHUNK_SIZE; i++) this.setBlocks(Blocks.GRASS, coord3DTo1D(0, 2, i), coord3DTo1D(Config.CHUNK_SIZE - 1, 2, i));
for (int i = 0; i < Config.CHUNK_SIZE; i++) blocks.insertValue(Blocks.STONE, new Interval(coord3DTo1D(0, 3, i), coord3DTo1D(Config.CHUNK_SIZE - 1, 3, i)));
for (int i = 0; i < Config.CHUNK_SIZE; i++) blocks.insertValue(Blocks.DIRT, new Interval(coord3DTo1D(0, 4, i), coord3DTo1D(Config.CHUNK_SIZE - 1, 4, i)));
for (int i = 0; i < Config.CHUNK_SIZE; i++) blocks.insertValue(Blocks.GRASS, new Interval(coord3DTo1D(0, 5, i), coord3DTo1D(Config.CHUNK_SIZE - 1, 5, i)));
for (int i = 0; i < Config.CHUNK_SIZE; i++) this.setBlocks(Blocks.STONE, coord3DTo1D(0, 3, i), coord3DTo1D(Config.CHUNK_SIZE - 1, 3, i));
for (int i = 0; i < Config.CHUNK_SIZE; i++) this.setBlocks(Blocks.DIRT, coord3DTo1D(0, 4, i), coord3DTo1D(Config.CHUNK_SIZE - 1, 4, i));
for (int i = 0; i < Config.CHUNK_SIZE; i++) this.setBlocks(Blocks.GRASS, coord3DTo1D(0, 5, i), coord3DTo1D(Config.CHUNK_SIZE - 1, 5, i));
}
public void generateStair() {
for (int i = 0; i < Config.CHUNK_SIZE; i++) {
for (int j = 0; j < Config.CHUNK_SIZE; j++) {
blocks.insertValue(j < Config.CHUNK_SIZE - i - 1 ? Blocks.STONE : Blocks.GRASS, new Interval(coord3DTo1D(0, i, j), coord3DTo1D(Config.CHUNK_SIZE - 1, i, j)));
this.setBlocks(j < Config.CHUNK_SIZE - i - 1 ? Blocks.STONE : Blocks.GRASS, coord3DTo1D(0, i, j), coord3DTo1D(Config.CHUNK_SIZE - 1, i, j));
}
}
blocks.insertValue(Blocks.DIRT, new Interval(Chunk.coord3DTo1D(0, 0, 0), Chunk.coord3DTo1D(2, 0, 0)));
blocks.insertValue(Blocks.DIRT, new Interval(Chunk.coord3DTo1D(1, 0, 2), Chunk.coord3DTo1D(2, 0, 2)));
this.setBlocks(Blocks.DIRT, Chunk.coord3DTo1D(0, 0, 0), Chunk.coord3DTo1D(2, 0, 0));
this.setBlocks(Blocks.DIRT, Chunk.coord3DTo1D(1, 0, 2), Chunk.coord3DTo1D(2, 0, 2));
}
public void arrayGenerateCorner() {
@ -91,12 +91,21 @@ public class Chunk {
this.treeFrom1DArray(array);
}
/* Set blocks. Interval to be intended in the same way as in interval tree (flatten 1d index of 3d coords) */
public void setBlocks(Blocks block, Interval interval){
this.blocks.insertValue(block, new Interval(Math.max(0, interval.getLow()), Math.min(Config.CHUNK_3DCOORD_MAX_INDEX, interval.getHigh())));
}
public void setBlocks(Blocks block, int intLow, int intHigh){
this.blocks.insertValue(block, new Interval(Math.max(0, intLow), Math.min(Config.CHUNK_3DCOORD_MAX_INDEX, intHigh)));
}
public Blocks[] treeTo1DArray() {
// System.out.println(Config.CHUNK_3DCOORD_MAX_INDEX);
Blocks[] result = new Blocks[Config.CHUNK_3DCOORD_MAX_INDEX + 1];
Queue<IntervalTree<Blocks>.TreeNode> queue = new ArrayDeque();
queue.add(blocks.getRoot());
if(blocks.getRoot() != null) queue.add(blocks.getRoot());
IntervalTree<Blocks>.TreeNode t;
@ -119,7 +128,7 @@ public class Chunk {
for (int i = 0; i < array.length; i++) {
if (array[i] != prev) {
// System.out.println("From " + start + " to " + i + ": " + prev);
this.blocks.insertValue(prev == null ? Blocks.AIR : prev, new Interval(start, i));
this.setBlocks(prev == null ? Blocks.AIR : prev, start, i);
start = i;
}
@ -127,7 +136,7 @@ public class Chunk {
}
// System.out.println("From " + start + " to end :" + prev);
this.blocks.insertValue(prev, new Interval(start, array.length - 1));
this.setBlocks(prev, start, array.length - 1);
}
}