|
| 1 | +package org.embeddedt.modernfix.world.gen; |
| 2 | + |
| 3 | +import net.minecraft.core.Holder; |
| 4 | +import net.minecraft.core.QuartPos; |
| 5 | +import net.minecraft.world.level.biome.Biome; |
| 6 | +import net.minecraft.world.level.biome.BiomeManager; |
| 7 | + |
| 8 | +public class CachingNoiseBiomeSource implements BiomeManager.NoiseBiomeSource { |
| 9 | + private static final boolean DEBUG_OOB = false; |
| 10 | + private static final int HORIZONTAL_CELLS_PER_CHUNK = 8; |
| 11 | + private static final int CELL_SHIFT = 2; |
| 12 | + private final BiomeManager.NoiseBiomeSource biomeSource; |
| 13 | + private final Holder<Biome>[] biomesInCells; |
| 14 | + private final int numVerticalCells; |
| 15 | + private final int verticalShift; |
| 16 | + private final int chunkOriginX, chunkOriginZ; |
| 17 | + |
| 18 | + public CachingNoiseBiomeSource(BiomeManager.NoiseBiomeSource backingSource, int chunkOriginX, int chunkOriginZ, int worldHeight, int numBlocksBelowYZero) { |
| 19 | + this.biomeSource = backingSource; |
| 20 | + this.numVerticalCells = worldHeight / QuartPos.SIZE + 2; |
| 21 | + this.chunkOriginX = chunkOriginX; |
| 22 | + this.chunkOriginZ = chunkOriginZ; |
| 23 | + this.verticalShift = numBlocksBelowYZero / QuartPos.SIZE; |
| 24 | + this.biomesInCells = new Holder[numVerticalCells * HORIZONTAL_CELLS_PER_CHUNK * HORIZONTAL_CELLS_PER_CHUNK]; |
| 25 | + } |
| 26 | + |
| 27 | + private static int getLocalIndex(int relX, int relY, int relZ) { |
| 28 | + return (relY * HORIZONTAL_CELLS_PER_CHUNK * HORIZONTAL_CELLS_PER_CHUNK) + (relZ * HORIZONTAL_CELLS_PER_CHUNK) + relX; |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public Holder<Biome> getNoiseBiome(int x, int y, int z) { |
| 33 | + int relX = x + CELL_SHIFT - this.chunkOriginX; |
| 34 | + int relZ = z + CELL_SHIFT - this.chunkOriginZ; |
| 35 | + int relY = y + this.verticalShift + 1; |
| 36 | + if (((relX | relZ) & ~(HORIZONTAL_CELLS_PER_CHUNK - 1)) != 0 || relY < 0 || relY >= this.numVerticalCells) { |
| 37 | + return this.biomeSource.getNoiseBiome(x, y, z); |
| 38 | + } |
| 39 | + int i = getLocalIndex(relX, relY, relZ); |
| 40 | + if (DEBUG_OOB) { |
| 41 | + if (i < 0 || i >= this.biomesInCells.length) { |
| 42 | + throw new ArrayIndexOutOfBoundsException("Error: Requested noise biome cell is out of range: (%d, %d, %d)".formatted(relX, relY, relZ)); |
| 43 | + } |
| 44 | + } |
| 45 | + var biomes = this.biomesInCells; |
| 46 | + var result = biomes[i]; |
| 47 | + if (result == null) { |
| 48 | + result = this.biomeSource.getNoiseBiome(x, y, z); |
| 49 | + biomes[i] = result; |
| 50 | + } |
| 51 | + return result; |
| 52 | + } |
| 53 | +} |
0 commit comments