| 1 | package net.minecraft.world.level.chunk;
|
| 2 |
|
| 3 | import com.google.common.base.Stopwatch;
|
| 4 | import com.mojang.datafixers.util.Pair;
|
| 5 | import com.mojang.logging.LogUtils;
|
| 6 | import it.unimi.dsi.fastutil.objects.Object2ObjectArrayMap;
|
| 7 | import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
|
| 8 | import java.util.ArrayList;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Map;
|
| 11 | import java.util.Set;
|
| 12 | import java.util.concurrent.CompletableFuture;
|
| 13 | import java.util.concurrent.TimeUnit;
|
| 14 | import java.util.stream.Collectors;
|
| 15 | import java.util.stream.Stream;
|
| 16 | import net.minecraft.core.BlockPos;
|
| 17 | import net.minecraft.core.Holder;
|
| 18 | import net.minecraft.core.HolderLookup;
|
| 19 | import net.minecraft.core.HolderSet;
|
| 20 | import net.minecraft.core.SectionPos;
|
| 21 | import net.minecraft.util.RandomSource;
|
| 22 | import net.minecraft.util.Util;
|
| 23 | import net.minecraft.world.level.ChunkPos;
|
| 24 | import net.minecraft.world.level.biome.Biome;
|
| 25 | import net.minecraft.world.level.biome.BiomeSource;
|
| 26 | import net.minecraft.world.level.levelgen.RandomState;
|
| 27 | import net.minecraft.world.level.levelgen.structure.Structure;
|
| 28 | import net.minecraft.world.level.levelgen.structure.StructureSet;
|
| 29 | import net.minecraft.world.level.levelgen.structure.placement.ConcentricRingsStructurePlacement;
|
| 30 | import net.minecraft.world.level.levelgen.structure.placement.StructurePlacement;
|
| 31 | import org.jspecify.annotations.Nullable;
|
| 32 | import org.slf4j.Logger;
|
| 33 |
|
| 34 | public class ChunkGeneratorStructureState {
|
| 35 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 36 | private final RandomState randomState;
|
| 37 | private final BiomeSource biomeSource;
|
| 38 | private final long levelSeed;
|
| 39 | private final long concentricRingsSeed;
|
| 40 | private final Map<Structure, List<StructurePlacement>> placementsForStructure = new Object2ObjectOpenHashMap<>();
|
| 41 | private final Map<ConcentricRingsStructurePlacement, CompletableFuture<List<ChunkPos>>> ringPositions = new Object2ObjectArrayMap<>();
|
| 42 | private boolean hasGeneratedPositions;
|
| 43 | private final ChunkPos origin;
|
| 44 | private final List<Holder<StructureSet>> possibleStructureSets;
|
| 45 |
|
| 46 | public static ChunkGeneratorStructureState createForFlat(
|
| 47 | final RandomState randomState,
|
| 48 | final long levelSeed,
|
| 49 | final ChunkPos origin,
|
| 50 | final BiomeSource biomeSource,
|
| 51 | final Stream<Holder<StructureSet>> structureOverrides
|
| 52 | ) {
|
| 53 | List<Holder<StructureSet>> structures = structureOverrides.filter(structureSet -> hasBiomesForStructureSet(structureSet.value(), biomeSource)).toList();
|
| 54 | return new ChunkGeneratorStructureState(randomState, biomeSource, levelSeed, origin, 0L, structures);
|
| 55 | }
|
| 56 |
|
| 57 | public static ChunkGeneratorStructureState createForNormal(
|
| 58 | final RandomState randomState, final long levelSeed, final ChunkPos origin, final BiomeSource biomeSource, final HolderLookup<StructureSet> allStructures
|
| 59 | ) {
|
| 60 | List<Holder<StructureSet>> structures = allStructures.listElements()
|
| 61 | .filter(structureSet -> hasBiomesForStructureSet(structureSet.value(), biomeSource))
|
| 62 | .collect(Collectors.toUnmodifiableList());
|
| 63 | return new ChunkGeneratorStructureState(randomState, biomeSource, levelSeed, origin, levelSeed, structures);
|
| 64 | }
|
| 65 |
|
| 66 | private static boolean hasBiomesForStructureSet(final StructureSet structureSet, final BiomeSource biomeSource) {
|
| 67 | Stream<Holder<Biome>> structureBiomes = structureSet.structures().stream().flatMap(entry -> {
|
| 68 | Structure structure = entry.structure().value();
|
| 69 | return structure.biomes().stream();
|
| 70 | });
|
| 71 | return structureBiomes.anyMatch(biomeSource.possibleBiomes()::contains);
|
| 72 | }
|
| 73 |
|
| 74 | private ChunkGeneratorStructureState(
|
| 75 | final RandomState randomState,
|
| 76 | final BiomeSource biomeSource,
|
| 77 | final long levelSeed,
|
| 78 | final ChunkPos origin,
|
| 79 | final long concentricRingsSeed,
|
| 80 | final List<Holder<StructureSet>> possibleStructureSets
|
| 81 | ) {
|
| 82 | this.randomState = randomState;
|
| 83 | this.levelSeed = levelSeed;
|
| 84 | this.origin = origin;
|
| 85 | this.biomeSource = biomeSource;
|
| 86 | this.concentricRingsSeed = concentricRingsSeed;
|
| 87 | this.possibleStructureSets = possibleStructureSets;
|
| 88 | }
|
| 89 |
|
| 90 | public List<Holder<StructureSet>> possibleStructureSets() {
|
| 91 | return this.possibleStructureSets;
|
| 92 | }
|
| 93 |
|
| 94 | private void generatePositions() {
|
| 95 | Set<Holder<Biome>> possibleBiomes = this.biomeSource.possibleBiomes();
|
| 96 | this.possibleStructureSets().forEach(setHolder -> {
|
| 97 | StructureSet set = setHolder.value();
|
| 98 | boolean hasAnyPlaceableStructures = false;
|
| 99 |
|
| 100 | for (StructureSet.StructureSelectionEntry entry : set.structures()) {
|
| 101 | Structure structure = entry.structure().value();
|
| 102 | if (structure.biomes().stream().anyMatch(possibleBiomes::contains)) {
|
| 103 | this.placementsForStructure.computeIfAbsent(structure, s -> new ArrayList<>()).add(set.placement());
|
| 104 | hasAnyPlaceableStructures = true;
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | if (hasAnyPlaceableStructures && set.placement() instanceof ConcentricRingsStructurePlacement ringsPlacement) {
|
| 109 | this.ringPositions.put(ringsPlacement, this.generateRingPositions((Holder<StructureSet>)setHolder, ringsPlacement));
|
| 110 | }
|
| 111 | });
|
| 112 | }
|
| 113 |
|
| 114 | private CompletableFuture<List<ChunkPos>> generateRingPositions(final Holder<StructureSet> structureSet, final ConcentricRingsStructurePlacement placement) {
|
| 115 | if (placement.count() == 0) {
|
| 116 | return CompletableFuture.completedFuture(List.of());
|
| 117 | }
|
| 118 |
|
| 119 | Stopwatch stopwatch = Stopwatch.createStarted(Util.TICKER);
|
| 120 | int distance = placement.distance();
|
| 121 | int count = placement.count();
|
| 122 | List<CompletableFuture<ChunkPos>> tasks = new ArrayList<>(count);
|
| 123 | int spread = placement.spread();
|
| 124 | HolderSet<Biome> preferredBiomes = placement.preferredBiomes();
|
| 125 | RandomSource random = RandomSource.create();
|
| 126 | random.setSeed(this.concentricRingsSeed);
|
| 127 | double angle = random.nextDouble() * Math.PI * 2.0;
|
| 128 | int positionInCircle = 0;
|
| 129 | int circle = 0;
|
| 130 |
|
| 131 | for (int i = 0; i < count; i++) {
|
| 132 | double dist = 4 * distance + distance * circle * 6 + (random.nextDouble() - 0.5) * (distance * 2.5);
|
| 133 | int initialX = (int)Math.round(Math.cos(angle) * dist);
|
| 134 | int initialZ = (int)Math.round(Math.sin(angle) * dist);
|
| 135 | RandomSource biomeSearchGenerator = random.fork();
|
| 136 | tasks.add(
|
| 137 | CompletableFuture.supplyAsync(
|
| 138 | () -> {
|
| 139 | Pair<BlockPos, Holder<Biome>> closestBiome = this.biomeSource
|
| 140 | .findBiomeHorizontal(
|
| 141 | SectionPos.sectionToBlockCoord(initialX, 8),
|
| 142 | 0,
|
| 143 | SectionPos.sectionToBlockCoord(initialZ, 8),
|
| 144 | 112,
|
| 145 | preferredBiomes::contains,
|
| 146 | biomeSearchGenerator,
|
| 147 | this.randomState
|
| 148 | );
|
| 149 | if (closestBiome != null) {
|
| 150 | BlockPos position = closestBiome.getFirst();
|
| 151 | return new ChunkPos(SectionPos.blockToSectionCoord(position.getX()), SectionPos.blockToSectionCoord(position.getZ()));
|
| 152 | } else {
|
| 153 | return new ChunkPos(initialX, initialZ);
|
| 154 | }
|
| 155 | },
|
| 156 | Util.backgroundExecutor().forName("structureRings")
|
| 157 | )
|
| 158 | );
|
| 159 | angle += (Math.PI * 2) / spread;
|
| 160 | if (++positionInCircle == spread) {
|
| 161 | circle++;
|
| 162 | positionInCircle = 0;
|
| 163 | spread += 2 * spread / (circle + 1);
|
| 164 | spread = Math.min(spread, count - i);
|
| 165 | angle += random.nextDouble() * Math.PI * 2.0;
|
| 166 | }
|
| 167 | }
|
| 168 |
|
| 169 | return Util.sequence(tasks).thenApply(ringPositions -> {
|
| 170 | double elapsedSeconds = stopwatch.stop().elapsed(TimeUnit.MILLISECONDS) / 1000.0;
|
| 171 | LOGGER.debug("Calculation for {} took {}s", structureSet, elapsedSeconds);
|
| 172 | return ringPositions;
|
| 173 | });
|
| 174 | }
|
| 175 |
|
| 176 | public ChunkPos getDimensionOrigin() {
|
| 177 | return this.origin;
|
| 178 | }
|
| 179 |
|
| 180 | public void ensureStructuresGenerated() {
|
| 181 | if (!this.hasGeneratedPositions) {
|
| 182 | this.generatePositions();
|
| 183 | this.hasGeneratedPositions = true;
|
| 184 | }
|
| 185 | }
|
| 186 |
|
| 187 | public @Nullable List<ChunkPos> getRingPositionsFor(final ConcentricRingsStructurePlacement placement) {
|
| 188 | this.ensureStructuresGenerated();
|
| 189 | CompletableFuture<List<ChunkPos>> result = this.ringPositions.get(placement);
|
| 190 | return result != null ? result.join() : null;
|
| 191 | }
|
| 192 |
|
| 193 | public List<StructurePlacement> getPlacementsForStructure(final Holder<Structure> structure) {
|
| 194 | this.ensureStructuresGenerated();
|
| 195 | return this.placementsForStructure.getOrDefault(structure.value(), List.of());
|
| 196 | }
|
| 197 |
|
| 198 | public RandomState randomState() {
|
| 199 | return this.randomState;
|
| 200 | }
|
| 201 |
|
| 202 | public boolean hasStructureChunkInRange(final Holder<StructureSet> structureSet, final int sourceX, final int sourceZ, final int range) {
|
| 203 | StructurePlacement placement = structureSet.value().placement();
|
| 204 |
|
| 205 | for (int testX = sourceX - range; testX <= sourceX + range; testX++) {
|
| 206 | for (int testZ = sourceZ - range; testZ <= sourceZ + range; testZ++) {
|
| 207 | if (placement.isStructureChunk(this, testX, testZ)) {
|
| 208 | return true;
|
| 209 | }
|
| 210 | }
|
| 211 | }
|
| 212 |
|
| 213 | return false;
|
| 214 | }
|
| 215 |
|
| 216 | public long getLevelSeed() {
|
| 217 | return this.levelSeed;
|
| 218 | }
|
| 219 | }
|
| 220 |
|