| 1 | package net.minecraft.world.level.levelgen;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import it.unimi.dsi.fastutil.longs.Long2IntMap;
|
| 6 | import it.unimi.dsi.fastutil.longs.Long2IntOpenHashMap;
|
| 7 | import java.util.Arrays;
|
| 8 | import net.minecraft.SharedConstants;
|
| 9 | import net.minecraft.core.BlockPos;
|
| 10 | import net.minecraft.core.QuartPos;
|
| 11 | import net.minecraft.core.SectionPos;
|
| 12 | import net.minecraft.util.Mth;
|
| 13 | import net.minecraft.util.RandomSource;
|
| 14 | import net.minecraft.world.level.ChunkPos;
|
| 15 | import net.minecraft.world.level.block.Blocks;
|
| 16 | import net.minecraft.world.level.block.state.BlockState;
|
| 17 | import net.minecraft.world.level.dimension.DimensionType;
|
| 18 | import net.minecraft.world.level.levelgen.densityfunction.DensityFunction;
|
| 19 | import net.minecraft.world.level.levelgen.densityfunction.DensitySampler;
|
| 20 | import net.minecraft.world.level.levelgen.densityfunction.DensitySamplerSet;
|
| 21 | import net.minecraft.world.level.levelgen.densityfunction.DensityVolume;
|
| 22 | import net.minecraft.world.level.levelgen.densityfunction.ScopedDensityBuffer;
|
| 23 | import org.apache.commons.lang3.mutable.MutableDouble;
|
| 24 | import org.jspecify.annotations.Nullable;
|
| 25 |
|
| 26 | public interface Aquifer {
|
| 27 | static Aquifer createDisabled(final Aquifer.FluidPicker fluidRule) {
|
| 28 | return new Aquifer() {
|
| 29 | @Override
|
| 30 | public @Nullable BlockState computeSubstance(final int blockX, final int blockY, final int blockZ, final double density) {
|
| 31 | return density > 0.0 ? null : fluidRule.computeFluid(blockX, blockY, blockZ).at(blockY);
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public boolean shouldScheduleFluidUpdate() {
|
| 36 | return false;
|
| 37 | }
|
| 38 | };
|
| 39 | }
|
| 40 |
|
| 41 | @Nullable BlockState computeSubstance(int blockX, int blockY, int blockZ, double density);
|
| 42 |
|
| 43 | boolean shouldScheduleFluidUpdate();
|
| 44 |
|
| 45 | record Config(
|
| 46 | DensityFunction barrierNoise,
|
| 47 | DensityFunction fluidLevelFloodednessNoise,
|
| 48 | DensityFunction fluidLevelSpreadNoise,
|
| 49 | DensityFunction lavaNoise,
|
| 50 | DensityFunction exclusion,
|
| 51 | DensityFunction surfaceLevel
|
| 52 | ) {
|
| 53 | public static final Codec<Aquifer.Config> CODEC = RecordCodecBuilder.create(
|
| 54 | i -> i.group(
|
| 55 | DensityFunction.CODEC.fieldOf("barrier").forGetter(Aquifer.Config::barrierNoise),
|
| 56 | DensityFunction.CODEC.fieldOf("fluid_level_floodedness").forGetter(Aquifer.Config::fluidLevelFloodednessNoise),
|
| 57 | DensityFunction.CODEC.fieldOf("fluid_level_spread").forGetter(Aquifer.Config::fluidLevelSpreadNoise),
|
| 58 | DensityFunction.CODEC.fieldOf("lava").forGetter(Aquifer.Config::lavaNoise),
|
| 59 | DensityFunction.CODEC.fieldOf("exclusion").forGetter(Aquifer.Config::exclusion),
|
| 60 | DensityFunction.CODEC.fieldOf("surface_level").forGetter(Aquifer.Config::surfaceLevel)
|
| 61 | )
|
| 62 | .apply(i, Aquifer.Config::new)
|
| 63 | );
|
| 64 |
|
| 65 | public Aquifer create(
|
| 66 | final DensitySamplerSet cachingSamplers,
|
| 67 | final PositionalRandomFactory positionalRandomFactory,
|
| 68 | final DensityVolume volume,
|
| 69 | final Aquifer.FluidPicker fluidRule
|
| 70 | ) {
|
| 71 | return new Aquifer.NoiseBasedAquifer(cachingSamplers, this, positionalRandomFactory, volume, fluidRule);
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | interface FluidPicker {
|
| 76 | Aquifer.FluidStatus computeFluid(final int blockX, final int blockY, final int blockZ);
|
| 77 | }
|
| 78 |
|
| 79 | record FluidStatus(int fluidLevel, BlockState fluidType) {
|
| 80 | public BlockState at(final int blockY) {
|
| 81 | return blockY < this.fluidLevel ? this.fluidType : Blocks.AIR.defaultBlockState();
|
| 82 | }
|
| 83 | }
|
| 84 |
|
| 85 | class NoiseBasedAquifer implements Aquifer {
|
| 86 | private static final int X_RANGE = 10;
|
| 87 | private static final int Y_RANGE = 9;
|
| 88 | private static final int Z_RANGE = 10;
|
| 89 | private static final int X_SEPARATION = 6;
|
| 90 | private static final int Y_SEPARATION = 3;
|
| 91 | private static final int Z_SEPARATION = 6;
|
| 92 | private static final int X_SPACING = 16;
|
| 93 | private static final int Y_SPACING = 12;
|
| 94 | private static final int Z_SPACING = 16;
|
| 95 | private static final int X_SPACING_SHIFT = 4;
|
| 96 | private static final int Z_SPACING_SHIFT = 4;
|
| 97 | private static final int MAX_REASONABLE_DISTANCE_TO_AQUIFER_CENTER = 11;
|
| 98 | private static final double FLOWING_UPDATE_SIMULARITY = similarity(Mth.square(10), Mth.square(12));
|
| 99 | private static final int SAMPLE_OFFSET_X = -5;
|
| 100 | private static final int SAMPLE_OFFSET_Y = 1;
|
| 101 | private static final int SAMPLE_OFFSET_Z = -5;
|
| 102 | private static final int MIN_CELL_SAMPLE_X = 0;
|
| 103 | private static final int MIN_CELL_SAMPLE_Y = -1;
|
| 104 | private static final int MIN_CELL_SAMPLE_Z = 0;
|
| 105 | private static final int MAX_CELL_SAMPLE_X = 1;
|
| 106 | private static final int MAX_CELL_SAMPLE_Y = 1;
|
| 107 | private static final int MAX_CELL_SAMPLE_Z = 1;
|
| 108 | private final DensitySampler.Bound barrierNoise;
|
| 109 | private final DensitySampler.Bound fluidLevelFloodednessNoise;
|
| 110 | private final DensitySampler.Bound fluidLevelSpreadNoise;
|
| 111 | private final DensitySampler.Bound lavaNoise;
|
| 112 | private final PositionalRandomFactory positionalRandomFactory;
|
| 113 | private final Aquifer.@Nullable FluidStatus[] aquiferCache;
|
| 114 | private final long[] aquiferLocationCache;
|
| 115 | private final Aquifer.FluidPicker globalFluidPicker;
|
| 116 | private final DensitySampler.Bound exclusion;
|
| 117 | private boolean shouldScheduleFluidUpdate;
|
| 118 | private final int skipSamplingAboveY;
|
| 119 | private final int minGridX;
|
| 120 | private final int minGridY;
|
| 121 | private final int minGridZ;
|
| 122 | private final int gridSizeX;
|
| 123 | private final int gridSizeZ;
|
| 124 | private final DensitySampler.Bound surfaceLevel;
|
| 125 | private final Long2IntMap surfaceLevelCache = new Long2IntOpenHashMap();
|
| 126 | private static final int[][] SURFACE_SAMPLING_OFFSETS_IN_CHUNKS = new int[][]{
|
| 127 | {0, 0}, {-2, -1}, {-1, -1}, {0, -1}, {1, -1}, {-3, 0}, {-2, 0}, {-1, 0}, {1, 0}, {-2, 1}, {-1, 1}, {0, 1}, {1, 1}
|
| 128 | };
|
| 129 |
|
| 130 | private NoiseBasedAquifer(
|
| 131 | final DensitySamplerSet cachingSamplers,
|
| 132 | final Aquifer.Config config,
|
| 133 | final PositionalRandomFactory positionalRandomFactory,
|
| 134 | final DensityVolume volume,
|
| 135 | final Aquifer.FluidPicker globalFluidPicker
|
| 136 | ) {
|
| 137 | this.barrierNoise = cachingSamplers.get(config.barrierNoise());
|
| 138 | this.fluidLevelFloodednessNoise = cachingSamplers.get(config.fluidLevelFloodednessNoise());
|
| 139 | this.fluidLevelSpreadNoise = cachingSamplers.get(config.fluidLevelSpreadNoise());
|
| 140 | this.lavaNoise = cachingSamplers.get(config.lavaNoise());
|
| 141 | this.exclusion = cachingSamplers.get(config.exclusion());
|
| 142 | this.surfaceLevel = cachingSamplers.get(config.surfaceLevel());
|
| 143 | this.positionalRandomFactory = positionalRandomFactory;
|
| 144 | this.minGridX = gridX(volume.minBlockX() + -5) + 0;
|
| 145 | this.globalFluidPicker = globalFluidPicker;
|
| 146 | int maxGridX = gridX(volume.maxBlockX() + -5) + 1;
|
| 147 | this.gridSizeX = maxGridX - this.minGridX + 1;
|
| 148 | this.minGridY = gridY(volume.minBlockY() + 1) + -1;
|
| 149 | int maxGridY = gridY(volume.maxBlockY() + 1) + 1;
|
| 150 | int gridSizeY = maxGridY - this.minGridY + 1;
|
| 151 | this.minGridZ = gridZ(volume.minBlockZ() + -5) + 0;
|
| 152 | int maxGridZ = gridZ(volume.maxBlockZ() + -5) + 1;
|
| 153 | this.gridSizeZ = maxGridZ - this.minGridZ + 1;
|
| 154 | int totalGridSize = this.gridSizeX * gridSizeY * this.gridSizeZ;
|
| 155 | this.aquiferCache = new Aquifer.FluidStatus[totalGridSize];
|
| 156 | this.aquiferLocationCache = new long[totalGridSize];
|
| 157 | Arrays.fill(this.aquiferLocationCache, Long.MAX_VALUE);
|
| 158 | int maxAdjustedSurfaceLevel = this.adjustSurfaceLevel(
|
| 159 | this.maxSurfaceLevel(fromGridX(this.minGridX, 0), fromGridZ(this.minGridZ, 0), fromGridX(maxGridX, 9), fromGridZ(maxGridZ, 9))
|
| 160 | );
|
| 161 | int skipSamplingAboveGridY = gridY(maxAdjustedSurfaceLevel + 12) - -1;
|
| 162 | this.skipSamplingAboveY = fromGridY(skipSamplingAboveGridY, 11) - 1;
|
| 163 | }
|
| 164 |
|
| 165 | private int surfaceLevel(final int blockX, final int blockZ) {
|
| 166 | int quantizedX = QuartPos.toBlock(QuartPos.fromBlock(blockX));
|
| 167 | int quantizedZ = QuartPos.toBlock(QuartPos.fromBlock(blockZ));
|
| 168 | return this.surfaceLevelCache
|
| 169 | .computeIfAbsent(ChunkPos.pack(quantizedX, quantizedZ), k -> Mth.floor(this.surfaceLevel.sampleValue(quantizedX, 0, quantizedZ)));
|
| 170 | }
|
| 171 |
|
| 172 | private int maxSurfaceLevel(final int minBlockX, final int minBlockZ, final int maxBlockX, final int maxBlockZ) {
|
| 173 | int minQuartX = QuartPos.fromBlock(minBlockX);
|
| 174 | int maxQuartX = QuartPos.fromBlock(maxBlockX);
|
| 175 | int minQuartZ = QuartPos.fromBlock(minBlockZ);
|
| 176 | int maxQuartZ = QuartPos.fromBlock(maxBlockZ);
|
| 177 | DensityVolume volume = new DensityVolume(
|
| 178 | maxQuartX - minQuartX + 1, 1, maxQuartZ - minQuartZ + 1, QuartPos.toBlock(minQuartX), 0, QuartPos.toBlock(minQuartZ), 4, 1, 4
|
| 179 | );
|
| 180 |
|
| 181 | try (ScopedDensityBuffer buffer = this.surfaceLevel.sampleVolume(volume)) {
|
| 182 | int maxY = Integer.MIN_VALUE;
|
| 183 |
|
| 184 | for (int z = 0; z < volume.sizeZ(); z++) {
|
| 185 | for (int x = 0; x < volume.sizeX(); x++) {
|
| 186 | int surfaceLevel = Mth.floor(buffer.get(volume.indexUnchecked(x, 0, z)));
|
| 187 | this.surfaceLevelCache.put(ChunkPos.pack(volume.blockX(x), volume.blockZ(z)), surfaceLevel);
|
| 188 | if (surfaceLevel > maxY) {
|
| 189 | maxY = surfaceLevel;
|
| 190 | }
|
| 191 | }
|
| 192 | }
|
| 193 |
|
| 194 | return maxY;
|
| 195 | }
|
| 196 | }
|
| 197 |
|
| 198 | private int getIndex(final int gridX, final int gridY, final int gridZ) {
|
| 199 | int x = gridX - this.minGridX;
|
| 200 | int y = gridY - this.minGridY;
|
| 201 | int z = gridZ - this.minGridZ;
|
| 202 | return (y * this.gridSizeZ + z) * this.gridSizeX + x;
|
| 203 | }
|
| 204 |
|
| 205 | @Override
|
| 206 | public @Nullable BlockState computeSubstance(final int blockX, final int blockY, final int blockZ, final double density) {
|
| 207 | if (density > 0.0) {
|
| 208 | this.shouldScheduleFluidUpdate = false;
|
| 209 | return null;
|
| 210 | }
|
| 211 |
|
| 212 | Aquifer.FluidStatus globalFluid = this.globalFluidPicker.computeFluid(blockX, blockY, blockZ);
|
| 213 | if (blockY > this.skipSamplingAboveY) {
|
| 214 | this.shouldScheduleFluidUpdate = false;
|
| 215 | return globalFluid.at(blockY);
|
| 216 | }
|
| 217 |
|
| 218 | if (globalFluid.at(blockY).is(Blocks.LAVA)) {
|
| 219 | this.shouldScheduleFluidUpdate = false;
|
| 220 | return SharedConstants.DEBUG_DISABLE_FLUID_GENERATION ? Blocks.AIR.defaultBlockState() : Blocks.LAVA.defaultBlockState();
|
| 221 | }
|
| 222 |
|
| 223 | int xAnchor = gridX(blockX + -5);
|
| 224 | int yAnchor = gridY(blockY + 1);
|
| 225 | int zAnchor = gridZ(blockZ + -5);
|
| 226 | int distanceSqr1 = Integer.MAX_VALUE;
|
| 227 | int distanceSqr2 = Integer.MAX_VALUE;
|
| 228 | int distanceSqr3 = Integer.MAX_VALUE;
|
| 229 | int distanceSqr4 = Integer.MAX_VALUE;
|
| 230 | int closestIndex1 = 0;
|
| 231 | int closestIndex2 = 0;
|
| 232 | int closestIndex3 = 0;
|
| 233 | int closestIndex4 = 0;
|
| 234 |
|
| 235 | for (int x1 = 0; x1 <= 1; x1++) {
|
| 236 | for (int y1 = -1; y1 <= 1; y1++) {
|
| 237 | for (int z1 = 0; z1 <= 1; z1++) {
|
| 238 | int spacedGridX = xAnchor + x1;
|
| 239 | int spacedGridY = yAnchor + y1;
|
| 240 | int spacedGridZ = zAnchor + z1;
|
| 241 | int index = this.getIndex(spacedGridX, spacedGridY, spacedGridZ);
|
| 242 | long existingLocation = this.aquiferLocationCache[index];
|
| 243 | long location;
|
| 244 | if (existingLocation != Long.MAX_VALUE) {
|
| 245 | location = existingLocation;
|
| 246 | } else {
|
| 247 | RandomSource random = this.positionalRandomFactory.at(spacedGridX, spacedGridY, spacedGridZ);
|
| 248 | location = BlockPos.asLong(
|
| 249 | fromGridX(spacedGridX, random.nextInt(10)), fromGridY(spacedGridY, random.nextInt(9)), fromGridZ(spacedGridZ, random.nextInt(10))
|
| 250 | );
|
| 251 | this.aquiferLocationCache[index] = location;
|
| 252 | }
|
| 253 |
|
| 254 | int dx = BlockPos.getX(location) - blockX;
|
| 255 | int dy = BlockPos.getY(location) - blockY;
|
| 256 | int dz = BlockPos.getZ(location) - blockZ;
|
| 257 | int newDistance = dx * dx + dy * dy + dz * dz;
|
| 258 | if (distanceSqr1 >= newDistance) {
|
| 259 | closestIndex4 = closestIndex3;
|
| 260 | closestIndex3 = closestIndex2;
|
| 261 | closestIndex2 = closestIndex1;
|
| 262 | closestIndex1 = index;
|
| 263 | distanceSqr4 = distanceSqr3;
|
| 264 | distanceSqr3 = distanceSqr2;
|
| 265 | distanceSqr2 = distanceSqr1;
|
| 266 | distanceSqr1 = newDistance;
|
| 267 | } else if (distanceSqr2 >= newDistance) {
|
| 268 | closestIndex4 = closestIndex3;
|
| 269 | closestIndex3 = closestIndex2;
|
| 270 | closestIndex2 = index;
|
| 271 | distanceSqr4 = distanceSqr3;
|
| 272 | distanceSqr3 = distanceSqr2;
|
| 273 | distanceSqr2 = newDistance;
|
| 274 | } else if (distanceSqr3 >= newDistance) {
|
| 275 | closestIndex4 = closestIndex3;
|
| 276 | closestIndex3 = index;
|
| 277 | distanceSqr4 = distanceSqr3;
|
| 278 | distanceSqr3 = newDistance;
|
| 279 | } else if (distanceSqr4 >= newDistance) {
|
| 280 | closestIndex4 = index;
|
| 281 | distanceSqr4 = newDistance;
|
| 282 | }
|
| 283 | }
|
| 284 | }
|
| 285 | }
|
| 286 |
|
| 287 | Aquifer.FluidStatus closestStatus1 = this.getAquiferStatus(closestIndex1);
|
| 288 | double similarity12 = similarity(distanceSqr1, distanceSqr2);
|
| 289 | BlockState fluidState = closestStatus1.at(blockY);
|
| 290 | BlockState actualFluidState = SharedConstants.DEBUG_DISABLE_FLUID_GENERATION ? Blocks.AIR.defaultBlockState() : fluidState;
|
| 291 | if (similarity12 <= 0.0) {
|
| 292 | if (similarity12 >= FLOWING_UPDATE_SIMULARITY) {
|
| 293 | Aquifer.FluidStatus closestStatus2 = this.getAquiferStatus(closestIndex2);
|
| 294 | this.shouldScheduleFluidUpdate = !closestStatus1.equals(closestStatus2);
|
| 295 | } else {
|
| 296 | this.shouldScheduleFluidUpdate = false;
|
| 297 | }
|
| 298 |
|
| 299 | return actualFluidState;
|
| 300 | } else {
|
| 301 | if (fluidState.is(Blocks.WATER) && this.globalFluidPicker.computeFluid(blockX, blockY - 1, blockZ).at(blockY - 1).is(Blocks.LAVA)) {
|
| 302 | this.shouldScheduleFluidUpdate = true;
|
| 303 | return actualFluidState;
|
| 304 | }
|
| 305 |
|
| 306 | MutableDouble barrierNoiseValue = new MutableDouble(Double.NaN);
|
| 307 | Aquifer.FluidStatus closestStatus2 = this.getAquiferStatus(closestIndex2);
|
| 308 | double barrier12 = similarity12 * this.calculatePressure(blockX, blockY, blockZ, barrierNoiseValue, closestStatus1, closestStatus2);
|
| 309 | if (density + barrier12 > 0.0) {
|
| 310 | this.shouldScheduleFluidUpdate = false;
|
| 311 | return null;
|
| 312 | }
|
| 313 |
|
| 314 | Aquifer.FluidStatus closestStatus3 = this.getAquiferStatus(closestIndex3);
|
| 315 | double similarity13 = similarity(distanceSqr1, distanceSqr3);
|
| 316 | if (similarity13 > 0.0) {
|
| 317 | double barrier13 = similarity12 * similarity13 * this.calculatePressure(blockX, blockY, blockZ, barrierNoiseValue, closestStatus1, closestStatus3);
|
| 318 | if (density + barrier13 > 0.0) {
|
| 319 | this.shouldScheduleFluidUpdate = false;
|
| 320 | return null;
|
| 321 | }
|
| 322 | }
|
| 323 |
|
| 324 | double similarity23 = similarity(distanceSqr2, distanceSqr3);
|
| 325 | if (similarity23 > 0.0) {
|
| 326 | double barrier23 = similarity12 * similarity23 * this.calculatePressure(blockX, blockY, blockZ, barrierNoiseValue, closestStatus2, closestStatus3);
|
| 327 | if (density + barrier23 > 0.0) {
|
| 328 | this.shouldScheduleFluidUpdate = false;
|
| 329 | return null;
|
| 330 | }
|
| 331 | }
|
| 332 |
|
| 333 | boolean mayFlow12 = !closestStatus1.equals(closestStatus2);
|
| 334 | boolean mayFlow23 = similarity23 >= FLOWING_UPDATE_SIMULARITY && !closestStatus2.equals(closestStatus3);
|
| 335 | boolean mayFlow13 = similarity13 >= FLOWING_UPDATE_SIMULARITY && !closestStatus1.equals(closestStatus3);
|
| 336 | if (!mayFlow12 && !mayFlow23 && !mayFlow13) {
|
| 337 | this.shouldScheduleFluidUpdate = similarity13 >= FLOWING_UPDATE_SIMULARITY
|
| 338 | && similarity(distanceSqr1, distanceSqr4) >= FLOWING_UPDATE_SIMULARITY
|
| 339 | && !closestStatus1.equals(this.getAquiferStatus(closestIndex4));
|
| 340 | } else {
|
| 341 | this.shouldScheduleFluidUpdate = true;
|
| 342 | }
|
| 343 |
|
| 344 | return actualFluidState;
|
| 345 | }
|
| 346 | }
|
| 347 |
|
| 348 | @Override
|
| 349 | public boolean shouldScheduleFluidUpdate() {
|
| 350 | return this.shouldScheduleFluidUpdate;
|
| 351 | }
|
| 352 |
|
| 353 | private static double similarity(final int distanceSqr1, final int distanceSqr2) {
|
| 354 | double threshold = 25.0;
|
| 355 | return 1.0 - (distanceSqr2 - distanceSqr1) / 25.0;
|
| 356 | }
|
| 357 |
|
| 358 | private double calculatePressure(
|
| 359 | final int blockX,
|
| 360 | final int blockY,
|
| 361 | final int blockZ,
|
| 362 | final MutableDouble barrierNoiseValue,
|
| 363 | final Aquifer.FluidStatus statusClosest1,
|
| 364 | final Aquifer.FluidStatus statusClosest2
|
| 365 | ) {
|
| 366 | BlockState type1 = statusClosest1.at(blockY);
|
| 367 | BlockState type2 = statusClosest2.at(blockY);
|
| 368 | if ((!type1.is(Blocks.LAVA) || !type2.is(Blocks.WATER)) && (!type1.is(Blocks.WATER) || !type2.is(Blocks.LAVA))) {
|
| 369 | int fluidYDiff = Math.abs(statusClosest1.fluidLevel - statusClosest2.fluidLevel);
|
| 370 | if (fluidYDiff == 0) {
|
| 371 | return 0.0;
|
| 372 | }
|
| 373 |
|
| 374 | double averageFluidY = 0.5 * (statusClosest1.fluidLevel + statusClosest2.fluidLevel);
|
| 375 | double howFarAboveAverageFluidPoint = blockY + 0.5 - averageFluidY;
|
| 376 | double baseValue = fluidYDiff / 2.0;
|
| 377 | double topBias = 0.0;
|
| 378 | double furthestRocksFromTopBias = 2.5;
|
| 379 | double furthestHolesFromTopBias = 1.5;
|
| 380 | double bottomBias = 3.0;
|
| 381 | double furthestRocksFromBottomBias = 10.0;
|
| 382 | double furthestHolesFromBottomBias = 3.0;
|
| 383 | double distanceFromBarrierEdgeTowardsMiddle = baseValue - Math.abs(howFarAboveAverageFluidPoint);
|
| 384 | double gradient;
|
| 385 | if (howFarAboveAverageFluidPoint > 0.0) {
|
| 386 | double centerPoint = 0.0 + distanceFromBarrierEdgeTowardsMiddle;
|
| 387 | if (centerPoint > 0.0) {
|
| 388 | gradient = centerPoint / 1.5;
|
| 389 | } else {
|
| 390 | gradient = centerPoint / 2.5;
|
| 391 | }
|
| 392 | } else {
|
| 393 | double centerPoint = 3.0 + distanceFromBarrierEdgeTowardsMiddle;
|
| 394 | if (centerPoint > 0.0) {
|
| 395 | gradient = centerPoint / 3.0;
|
| 396 | } else {
|
| 397 | gradient = centerPoint / 10.0;
|
| 398 | }
|
| 399 | }
|
| 400 |
|
| 401 | double amplitude = 2.0;
|
| 402 | double noiseValue;
|
| 403 | if (!(gradient < -2.0) && !(gradient > 2.0)) {
|
| 404 | double currentNoiseValue = barrierNoiseValue.doubleValue();
|
| 405 | if (Double.isNaN(currentNoiseValue)) {
|
| 406 | double barrierNoise = this.barrierNoise.sampleValue(blockX, blockY, blockZ);
|
| 407 | barrierNoiseValue.setValue(barrierNoise);
|
| 408 | noiseValue = barrierNoise;
|
| 409 | } else {
|
| 410 | noiseValue = currentNoiseValue;
|
| 411 | }
|
| 412 | } else {
|
| 413 | noiseValue = 0.0;
|
| 414 | }
|
| 415 |
|
| 416 | return 2.0 * (noiseValue + gradient);
|
| 417 | } else {
|
| 418 | return 2.0;
|
| 419 | }
|
| 420 | }
|
| 421 |
|
| 422 | private static int gridX(final int blockCoord) {
|
| 423 | return blockCoord >> 4;
|
| 424 | }
|
| 425 |
|
| 426 | private static int fromGridX(final int gridCoord, final int blockOffset) {
|
| 427 | return (gridCoord << 4) + blockOffset;
|
| 428 | }
|
| 429 |
|
| 430 | private static int gridY(final int blockCoord) {
|
| 431 | return Math.floorDiv(blockCoord, 12);
|
| 432 | }
|
| 433 |
|
| 434 | private static int fromGridY(final int gridCoord, final int blockOffset) {
|
| 435 | return gridCoord * 12 + blockOffset;
|
| 436 | }
|
| 437 |
|
| 438 | private static int gridZ(final int blockCoord) {
|
| 439 | return blockCoord >> 4;
|
| 440 | }
|
| 441 |
|
| 442 | private static int fromGridZ(final int gridCoord, final int blockOffset) {
|
| 443 | return (gridCoord << 4) + blockOffset;
|
| 444 | }
|
| 445 |
|
| 446 | private Aquifer.FluidStatus getAquiferStatus(final int index) {
|
| 447 | Aquifer.FluidStatus oldStatus = this.aquiferCache[index];
|
| 448 | if (oldStatus != null) {
|
| 449 | return oldStatus;
|
| 450 | }
|
| 451 |
|
| 452 | long location = this.aquiferLocationCache[index];
|
| 453 | Aquifer.FluidStatus status = this.computeFluid(BlockPos.getX(location), BlockPos.getY(location), BlockPos.getZ(location));
|
| 454 | this.aquiferCache[index] = status;
|
| 455 | return status;
|
| 456 | }
|
| 457 |
|
| 458 | private Aquifer.FluidStatus computeFluid(final int x, final int y, final int z) {
|
| 459 | Aquifer.FluidStatus globalFluid = this.globalFluidPicker.computeFluid(x, y, z);
|
| 460 | int lowestPreliminarySurface = Integer.MAX_VALUE;
|
| 461 | int topOfAquiferCell = y + 12;
|
| 462 | int bottomOfAquiferCell = y - 12;
|
| 463 | boolean surfaceAtCenterIsUnderGlobalFluidLevel = false;
|
| 464 |
|
| 465 | for (int[] offset : SURFACE_SAMPLING_OFFSETS_IN_CHUNKS) {
|
| 466 | int sampleX = x + SectionPos.sectionToBlockCoord(offset[0]);
|
| 467 | int sampleZ = z + SectionPos.sectionToBlockCoord(offset[1]);
|
| 468 | int surfaceLevel = this.surfaceLevel(sampleX, sampleZ);
|
| 469 | int adjustedSurfaceLevel = this.adjustSurfaceLevel(surfaceLevel);
|
| 470 | boolean start = offset[0] == 0 && offset[1] == 0;
|
| 471 | if (start && bottomOfAquiferCell > adjustedSurfaceLevel) {
|
| 472 | return globalFluid;
|
| 473 | }
|
| 474 |
|
| 475 | boolean topOfAquiferCellPokesAboveSurface = topOfAquiferCell > adjustedSurfaceLevel;
|
| 476 | if (topOfAquiferCellPokesAboveSurface || start) {
|
| 477 | Aquifer.FluidStatus globalFluidAtSurface = this.globalFluidPicker.computeFluid(sampleX, adjustedSurfaceLevel, sampleZ);
|
| 478 | if (!globalFluidAtSurface.at(adjustedSurfaceLevel).isAir()) {
|
| 479 | if (start) {
|
| 480 | surfaceAtCenterIsUnderGlobalFluidLevel = true;
|
| 481 | }
|
| 482 |
|
| 483 | if (topOfAquiferCellPokesAboveSurface) {
|
| 484 | return globalFluidAtSurface;
|
| 485 | }
|
| 486 | }
|
| 487 | }
|
| 488 |
|
| 489 | lowestPreliminarySurface = Math.min(lowestPreliminarySurface, surfaceLevel);
|
| 490 | }
|
| 491 |
|
| 492 | int fluidSurfaceLevel = this.computeSurfaceLevel(x, y, z, globalFluid, lowestPreliminarySurface, surfaceAtCenterIsUnderGlobalFluidLevel);
|
| 493 | return new Aquifer.FluidStatus(fluidSurfaceLevel, this.computeFluidType(x, y, z, globalFluid, fluidSurfaceLevel));
|
| 494 | }
|
| 495 |
|
| 496 | private int adjustSurfaceLevel(final int preliminarySurfaceLevel) {
|
| 497 | return preliminarySurfaceLevel + 8;
|
| 498 | }
|
| 499 |
|
| 500 | private int computeSurfaceLevel(
|
| 501 | final int x,
|
| 502 | final int y,
|
| 503 | final int z,
|
| 504 | final Aquifer.FluidStatus globalFluid,
|
| 505 | final int lowestSurfaceLevel,
|
| 506 | final boolean surfaceAtCenterIsUnderGlobalFluidLevel
|
| 507 | ) {
|
| 508 | double partiallyFloodedness;
|
| 509 | double fullyFloodidness;
|
| 510 | if (this.exclusion.sampleValue(x, y, z) > 0.0) {
|
| 511 | partiallyFloodedness = -1.0;
|
| 512 | fullyFloodidness = -1.0;
|
| 513 | } else {
|
| 514 | int distanceBelowSurface = this.adjustSurfaceLevel(lowestSurfaceLevel) - y;
|
| 515 | int floodednessMaxDepth = 64;
|
| 516 | double floodednessFactor = surfaceAtCenterIsUnderGlobalFluidLevel ? Mth.clampedMap(distanceBelowSurface, 0.0, 64.0, 1.0, 0.0) : 0.0;
|
| 517 | double floodednessNoiseValue = Mth.clamp(this.fluidLevelFloodednessNoise.sampleValue(x, y, z), -1.0, 1.0);
|
| 518 | double fullyFloodedThreshold = Mth.map(floodednessFactor, 1.0, 0.0, -0.3, 0.8);
|
| 519 | double partiallyFloodedThreshold = Mth.map(floodednessFactor, 1.0, 0.0, -0.8, 0.4);
|
| 520 | partiallyFloodedness = floodednessNoiseValue - partiallyFloodedThreshold;
|
| 521 | fullyFloodidness = floodednessNoiseValue - fullyFloodedThreshold;
|
| 522 | }
|
| 523 |
|
| 524 | int fluidSurfaceLevel;
|
| 525 | if (fullyFloodidness > 0.0) {
|
| 526 | fluidSurfaceLevel = globalFluid.fluidLevel;
|
| 527 | } else if (partiallyFloodedness > 0.0) {
|
| 528 | fluidSurfaceLevel = this.computeRandomizedFluidSurfaceLevel(x, y, z, lowestSurfaceLevel);
|
| 529 | } else {
|
| 530 | fluidSurfaceLevel = DimensionType.WAY_BELOW_MIN_Y;
|
| 531 | }
|
| 532 |
|
| 533 | return fluidSurfaceLevel;
|
| 534 | }
|
| 535 |
|
| 536 | private int computeRandomizedFluidSurfaceLevel(final int x, final int y, final int z, final int lowestSurfaceLevel) {
|
| 537 | int fluidCellWidth = 16;
|
| 538 | int fluidCellHeight = 40;
|
| 539 | int fluidLevelCellX = Math.floorDiv(x, 16);
|
| 540 | int fluidLevelCellY = Math.floorDiv(y, 40);
|
| 541 | int fluidLevelCellZ = Math.floorDiv(z, 16);
|
| 542 | int fluidCellMiddleY = fluidLevelCellY * 40 + 20;
|
| 543 | int maxSpread = 10;
|
| 544 | double fluidLevelSpread = this.fluidLevelSpreadNoise.sampleValue(fluidLevelCellX, fluidLevelCellY, fluidLevelCellZ) * 10.0F;
|
| 545 | int fluidLevelSpreadQuantized = Mth.quantize(fluidLevelSpread, 3);
|
| 546 | int targetFluidSurfaceLevel = fluidCellMiddleY + fluidLevelSpreadQuantized;
|
| 547 | return Math.min(lowestSurfaceLevel, targetFluidSurfaceLevel);
|
| 548 | }
|
| 549 |
|
| 550 | private BlockState computeFluidType(final int x, final int y, final int z, final Aquifer.FluidStatus globalFluid, final int fluidSurfaceLevel) {
|
| 551 | BlockState fluidType = globalFluid.fluidType;
|
| 552 | if (fluidSurfaceLevel <= -10 && fluidSurfaceLevel != DimensionType.WAY_BELOW_MIN_Y && globalFluid.fluidType != Blocks.LAVA.defaultBlockState()) {
|
| 553 | int fluidTypeCellWidth = 64;
|
| 554 | int fluidTypeCellHeight = 40;
|
| 555 | int fluidTypeCellX = Math.floorDiv(x, 64);
|
| 556 | int fluidTypeCellY = Math.floorDiv(y, 40);
|
| 557 | int fluidTypeCellZ = Math.floorDiv(z, 64);
|
| 558 | double lavaNoiseValue = this.lavaNoise.sampleValue(fluidTypeCellX, fluidTypeCellY, fluidTypeCellZ);
|
| 559 | if (Math.abs(lavaNoiseValue) > 0.3) {
|
| 560 | fluidType = Blocks.LAVA.defaultBlockState();
|
| 561 | }
|
| 562 | }
|
| 563 |
|
| 564 | return fluidType;
|
| 565 | }
|
| 566 | }
|
| 567 | }
|
| 568 |
|