Skip to content

Commit

Permalink
Prevent the initial pool bounds from being mutated (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 authored Jul 10, 2024
1 parent 18b0f22 commit b0ebdd4
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fr.catcore.deacoudre.game.map;

import net.minecraft.util.math.BlockPos;
import xyz.nucleoid.map_templates.BlockBounds;

import java.util.function.Consumer;

public class BlockBoundsBuilder implements Consumer<BlockPos> {
private final Consumer<BlockPos> delegate;

private BlockBounds bounds = null;

public BlockBoundsBuilder(Consumer<BlockPos> delegate) {
this.delegate = delegate;
}

@Override
public void accept(BlockPos pos) {
var addedBounds = BlockBounds.ofBlock(pos.toImmutable());
this.bounds = this.bounds == null ? addedBounds : this.bounds.union(addedBounds);

this.delegate.accept(pos);
}

public BlockBounds getBounds() {
return this.bounds;
}
}
89 changes: 39 additions & 50 deletions src/main/java/fr/catcore/deacoudre/game/map/DeACoudreMapConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import xyz.nucleoid.map_templates.BlockBounds;
import xyz.nucleoid.map_templates.MapTemplate;

import java.util.function.Consumer;

public record DeACoudreMapConfig(int radius, int height, String shape, int inCircleRadius,
BlockState spawnBlock,
BlockState poolOutlineBlock,
Expand Down Expand Up @@ -36,104 +39,90 @@ public DeACoudreMapConfig(int radius, int height, String shape, int inCircleRadi
}

public enum MapShape {
square((config, builder, mutablePosWater, mutablePosBorder) -> {
BlockBounds blockBounds = null;
square((config, setWater, setBorder) -> {
var pos = new BlockPos.Mutable();
for (int z = 5; z <= 5 + (2 * config.radius); z++) {
for (int x = -config.radius; x <= config.radius; x++) {
mutablePosBorder.set(x, 1, z);
builder.setBlockState(mutablePosBorder, config.poolOutlineBlock);
pos.set(x, 1, z);
setBorder.accept(pos);
}
}
for (int z = 5; z <= 5 + (2 * config.radius); z++) {
for (int x = -config.radius; x <= config.radius; x++) {
mutablePosBorder.set(x, 2, z);
mutablePosWater.set(x, 2, z);
if (z == 5 || z == 5 + (2 * config.radius) || x == -config.radius || x == config.radius)
builder.setBlockState(mutablePosBorder, config.poolOutlineBlock);
else {
builder.setBlockState(mutablePosWater, Blocks.WATER.getDefaultState());
blockBounds = blockBounds != null ?
blockBounds.union(BlockBounds.ofBlock(mutablePosWater))
: BlockBounds.ofBlock(mutablePosWater);
pos.set(x, 2, z);
if (z == 5 || z == 5 + (2 * config.radius) || x == -config.radius || x == config.radius) {
setBorder.accept(pos);
} else {
setWater.accept(pos);
}
}
}

return blockBounds;
}),
circle((config, builder, mutablePosWater, mutablePosBorder) -> {
BlockBounds blockBounds = null;
circle((config, setWater, setBorder) -> {
var pos = new BlockPos.Mutable();
int radius2 = config.radius * config.radius;
int outlineRadius2 = (config.radius - 1) * (config.radius - 1);
for (int z = -config.radius; z <= config.radius; z++) {
for (int x = -config.radius; x <= config.radius; x++) {
int distance2 = x * x + z * z;

mutablePosBorder.set(x, 1, getRightZ(config, z));
builder.setBlockState(mutablePosBorder, config.poolOutlineBlock);
pos.set(x, 1, getRightZ(config, z));
setBorder.accept(pos);

pos.move(Direction.UP);

if (distance2 <= outlineRadius2) {
mutablePosWater.set(x, 2, getRightZ(config, z));
builder.setBlockState(mutablePosWater, Blocks.WATER.getDefaultState());
blockBounds = blockBounds != null ?
blockBounds.union(BlockBounds.ofBlock(mutablePosWater))
: BlockBounds.ofBlock(mutablePosWater);
setWater.accept(pos);
} else {
mutablePosBorder.set(x, 2, getRightZ(config, z));
builder.setBlockState(mutablePosBorder, config.poolOutlineBlock);
setBorder.accept(pos);
}
}
}

return blockBounds;
}),
donut((config, builder, mutablePosWater, mutablePosBorder) -> {
BlockBounds blockBounds = null;
donut((config, setWater, setBorder) -> {
var pos = new BlockPos.Mutable();
int radius2 = config.radius * config.radius;
int outlineRadius2 = (config.radius - 1) * (config.radius - 1);
int inlineRadius = (config.inCircleRadius - 1) * (config.inCircleRadius - 1);
for (int z = -config.radius; z <= config.radius; z++) {
for (int x = -config.radius; x <= config.radius; x++) {
int distance2 = x * x + z * z;

mutablePosBorder.set(x, 1, getRightZ(config, z));
builder.setBlockState(mutablePosBorder, config.poolOutlineBlock);
pos.set(x, 1, getRightZ(config, z));
setBorder.accept(pos);

pos.move(Direction.UP);

if (distance2 <= outlineRadius2 && distance2 > inlineRadius) {
mutablePosWater.set(x, 2, getRightZ(config, z));
builder.setBlockState(mutablePosWater, Blocks.WATER.getDefaultState());
blockBounds = blockBounds != null ?
blockBounds.union(BlockBounds.ofBlock(mutablePosWater))
: BlockBounds.ofBlock(mutablePosWater);
setWater.accept(pos);
} else {
mutablePosBorder.set(x, 2, getRightZ(config, z));
builder.setBlockState(mutablePosBorder, config.poolOutlineBlock);
setBorder.accept(pos);
}
}
}

return blockBounds;
});

private final GeneratePool generatePool;
private final PoolGenerator generator;

MapShape(GeneratePool generatePool) {
this.generatePool = generatePool;
MapShape(PoolGenerator generator) {
this.generator = generator;
}

public BlockBounds generatePool(DeACoudreMapConfig config, MapTemplate builder) {
BlockPos.Mutable mutablePosWater = new BlockPos.Mutable();
BlockPos.Mutable mutablePosBorder = new BlockPos.Mutable();
return this.generatePool.generatePool(config, builder, mutablePosWater, mutablePosBorder);
var setWater = new BlockBoundsBuilder(pos -> builder.setBlockState(pos, Blocks.WATER.getDefaultState()));
Consumer<BlockPos> setBorder = pos -> builder.setBlockState(pos, config.poolOutlineBlock);

this.generator.generatePool(config, setWater, setBorder);

return setWater.getBounds();
}

private static int getRightZ(DeACoudreMapConfig config, int z) {
return 5 + (z - -config.radius);
}

private interface GeneratePool {

BlockBounds generatePool(DeACoudreMapConfig config, MapTemplate builder, BlockPos.Mutable mutablePosWater, BlockPos.Mutable mutablePosBorder);
private interface PoolGenerator {
void generatePool(DeACoudreMapConfig config, Consumer<BlockPos> setWater, Consumer<BlockPos> setBorder);
}
}
}

0 comments on commit b0ebdd4

Please sign in to comment.