Skip to content

Commit 36eb042

Browse files
committed
Fix #490 (again)
1 parent 895a7bf commit 36eb042

File tree

1 file changed

+45
-37
lines changed

1 file changed

+45
-37
lines changed

src/main/java/com/minelittlepony/unicopia/server/world/gen/CloudCarver.java

+45-37
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@
2727
import net.minecraft.world.gen.densityfunction.DensityFunction.NoisePos;
2828

2929
public class CloudCarver extends CaveCarver {
30-
31-
private Random random;
32-
33-
private final LongSet topWrittenPositions = new LongOpenHashSet();
34-
3530
public CloudCarver(Codec<CaveCarverConfig> codec) {
3631
super(codec);
3732
}
@@ -62,25 +57,13 @@ public boolean carve(
6257
ChunkPos chunkPos,
6358
CarvingMask carvingMask
6459
) {
65-
this.random = random;
66-
boolean result = super.carve(context, config, chunk, function, random, new AquiferSampler() {
67-
@Override
68-
public BlockState apply(NoisePos pos, double density) {
69-
BlockState state = sampler.apply(pos, density);
70-
return state != null && state.isAir() ? UBlocks.CLOUD.getDefaultState() : state;
71-
}
72-
73-
@Override
74-
public boolean needsFluidTick() {
75-
return sampler.needsFluidTick();
76-
}
77-
78-
}, chunkPos, carvingMask);
60+
CloudCarverContext carverContext = new CloudCarverContext(sampler, random);
61+
boolean result = super.carve(context, config, chunk, function, random, carverContext, chunkPos, carvingMask);
7962
BlockPos.Mutable mutable = new BlockPos.Mutable();
80-
topWrittenPositions.forEach(l -> {
63+
carverContext.topWrittenPositions.forEach(l -> {
8164
processSurfaceBlocks(mutable.set(l), context, config, chunk, random);
8265
});
83-
topWrittenPositions.clear();
66+
carverContext.topWrittenPositions.clear();
8467
return result;
8568
}
8669

@@ -99,17 +82,17 @@ protected void carveCave(
9982
CarvingMask mask,
10083
Carver.SkipPredicate skipPredicate
10184
) {
102-
if (random == null) {
85+
if (!(aquiferSampler instanceof CloudCarverContext c)) {
10386
return;
10487
}
10588
int maxY = context.getMinY() + context.getHeight();
10689

107-
int bubbleCount = 10 + random.nextInt(12);
90+
int bubbleCount = 10 + c.random.nextInt(12);
10891
for (int i = 0; i < bubbleCount; i++) {
109-
double width = 1.5 * xScale + random.nextTriangular(3, 2);
110-
double height = Math.min(width * yScale * (1 + random.nextFloat() * 2) + MathHelper.sin((float) (Math.PI / 2)) * xScale, maxY - y);
111-
double bubbleX = x + (random.nextFloat() * 2 - 1) * width;
112-
double bubbleZ = z + (random.nextFloat() * 2 - 1) * width;
92+
double width = 1.5 * xScale +c.random.nextTriangular(3, 2);
93+
double height = Math.min(width * yScale * (1 + c.random.nextFloat() * 2) + MathHelper.sin((float) (Math.PI / 2)) * xScale, maxY - y);
94+
double bubbleX = x + (c.random.nextFloat() * 2 - 1) * width;
95+
double bubbleZ = z + (c.random.nextFloat() * 2 - 1) * width;
11396
carveRegion(context, config, chunk, posToBiome, aquiferSampler, bubbleX + 1.0, y, bubbleZ, width, height, mask, skipPredicate);
11497
}
11598
}
@@ -136,17 +119,17 @@ protected void carveTunnels(
136119
CarvingMask mask,
137120
Carver.SkipPredicate skipPredicate
138121
) {
139-
if (random == null) {
122+
if (!(aquiferSampler instanceof CloudCarverContext c)) {
140123
return;
141124
}
142125
int maxY = context.getMinY() + context.getHeight();
143-
int bubbleCount = 10 + random.nextInt(12);
126+
int bubbleCount = 10 + c.random.nextInt(12);
144127
for (int i = 0; i < bubbleCount; i++) {
145-
double width = /*1.5 + MathHelper.sin((float) (Math.PI / 2)) * xScale +*/ 1.5 * horizontalScale + random.nextInt(3) + w;
146-
double height = width * (1 + random.nextFloat() * 2) * verticalScale * 0.2;
147-
double bubbleX = x + (random.nextFloat() * 2 - 1) * width * 1.5;
148-
double bubbleZ = z + (random.nextFloat() * 2 - 1) * width * 1.5;
149-
double bubbleY = y + random.nextFloat() * height * 0.5;
128+
double width = /*1.5 + MathHelper.sin((float) (Math.PI / 2)) * xScale +*/ 1.5 * horizontalScale + c.random.nextInt(3) + w;
129+
double height = width * (1 + c.random.nextFloat() * 2) * verticalScale * 0.2;
130+
double bubbleX = x + (c.random.nextFloat() * 2 - 1) * width * 1.5;
131+
double bubbleZ = z + (c.random.nextFloat() * 2 - 1) * width * 1.5;
132+
double bubbleY = y + c.random.nextFloat() * height * 0.5;
150133
if (bubbleY + height < maxY) {
151134
carveRegion(context, config, chunk, posToBiome, aquiferSampler, bubbleX, bubbleY, bubbleZ, width, height, mask, skipPredicate);
152135
}
@@ -168,10 +151,12 @@ protected boolean carveAtPoint(
168151
) {
169152
if (super.carveAtPoint(context, config, chunk, posToBiome, mask, pos, tmp, aquiferSampler, replacedGrassy)) {
170153
tmp.set(pos).move(Direction.DOWN);
171-
if (!topWrittenPositions.isEmpty()) {
172-
topWrittenPositions.remove(tmp.asLong());
154+
if (aquiferSampler instanceof CloudCarverContext c) {
155+
if (!c.topWrittenPositions.isEmpty()) {
156+
c.topWrittenPositions.remove(tmp.asLong());
157+
}
158+
c.topWrittenPositions.add(pos.asLong());
173159
}
174-
topWrittenPositions.add(pos.asLong());
175160
if (chunk.getBlockState(tmp).isOf(UBlocks.SOGGY_CLOUD)) {
176161
chunk.setBlockState(tmp, UBlocks.CLOUD.getDefaultState(), false);
177162
}
@@ -185,4 +170,27 @@ protected void processSurfaceBlocks(BlockPos.Mutable pos, CarverContext context,
185170
chunk.setBlockState(pos.move(Direction.DOWN), UBlocks.SOGGY_CLOUD.getDefaultState(), false);
186171
}
187172
}
173+
174+
static class CloudCarverContext implements AquiferSampler {
175+
176+
private final AquiferSampler sampler;
177+
public final LongSet topWrittenPositions = new LongOpenHashSet();
178+
public final Random random;
179+
180+
public CloudCarverContext(AquiferSampler sampler, Random random) {
181+
this.sampler = sampler;
182+
this.random = random;
183+
}
184+
185+
@Override
186+
public BlockState apply(NoisePos pos, double density) {
187+
BlockState state = sampler.apply(pos, density);
188+
return state != null && state.isAir() ? UBlocks.CLOUD.getDefaultState() : state;
189+
}
190+
191+
@Override
192+
public boolean needsFluidTick() {
193+
return sampler.needsFluidTick();
194+
}
195+
}
188196
}

0 commit comments

Comments
 (0)