| 1 | package net.minecraft.world.level.levelgen.structure;
|
| 2 |
|
| 3 | import com.mojang.datafixers.util.Either;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import com.mojang.serialization.MapCodec;
|
| 6 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 7 | import com.mojang.serialization.codecs.RecordCodecBuilder.Instance;
|
| 8 | import java.util.Map;
|
| 9 | import java.util.Optional;
|
| 10 | import java.util.function.Consumer;
|
| 11 | import java.util.function.Function;
|
| 12 | import java.util.function.Predicate;
|
| 13 | import net.minecraft.core.BlockPos;
|
| 14 | import net.minecraft.core.Holder;
|
| 15 | import net.minecraft.core.HolderSet;
|
| 16 | import net.minecraft.core.QuartPos;
|
| 17 | import net.minecraft.core.RegistryAccess;
|
| 18 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 19 | import net.minecraft.core.registries.Registries;
|
| 20 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 21 | import net.minecraft.resources.ResourceKey;
|
| 22 | import net.minecraft.util.RandomSource;
|
| 23 | import net.minecraft.util.StringRepresentable;
|
| 24 | import net.minecraft.util.profiling.jfr.JvmProfiler;
|
| 25 | import net.minecraft.util.profiling.jfr.callback.ProfiledDuration;
|
| 26 | import net.minecraft.world.entity.MobCategory;
|
| 27 | import net.minecraft.world.level.ChunkPos;
|
| 28 | import net.minecraft.world.level.Level;
|
| 29 | import net.minecraft.world.level.LevelHeightAccessor;
|
| 30 | import net.minecraft.world.level.StructureManager;
|
| 31 | import net.minecraft.world.level.WorldGenLevel;
|
| 32 | import net.minecraft.world.level.biome.Biome;
|
| 33 | import net.minecraft.world.level.biome.BiomeResolver;
|
| 34 | import net.minecraft.world.level.biome.BiomeSource;
|
| 35 | import net.minecraft.world.level.biome.Climate;
|
| 36 | import net.minecraft.world.level.block.Rotation;
|
| 37 | import net.minecraft.world.level.chunk.ChunkGenerator;
|
| 38 | import net.minecraft.world.level.levelgen.GenerationStep;
|
| 39 | import net.minecraft.world.level.levelgen.Heightmap;
|
| 40 | import net.minecraft.world.level.levelgen.LegacyRandomSource;
|
| 41 | import net.minecraft.world.level.levelgen.RandomState;
|
| 42 | import net.minecraft.world.level.levelgen.WorldgenRandom;
|
| 43 | import net.minecraft.world.level.levelgen.structure.pieces.PiecesContainer;
|
| 44 | import net.minecraft.world.level.levelgen.structure.pieces.StructurePiecesBuilder;
|
| 45 | import net.minecraft.world.level.levelgen.structure.templatesystem.StructureTemplateManager;
|
| 46 |
|
| 47 | public abstract class Structure {
|
| 48 | public static final Codec<Structure> DIRECT_CODEC = BuiltInRegistries.STRUCTURE_TYPE.byNameCodec().dispatch(Structure::type, StructureType::codec);
|
| 49 | public static final Codec<Holder<Structure>> CODEC = RegistryCodecs.holder(Registries.STRUCTURE, DIRECT_CODEC);
|
| 50 | protected final Structure.StructureSettings settings;
|
| 51 |
|
| 52 | public static <S extends Structure> RecordCodecBuilder<S, Structure.StructureSettings> settingsCodec(final Instance<S> i) {
|
| 53 | return Structure.StructureSettings.CODEC.forGetter(e -> e.settings);
|
| 54 | }
|
| 55 |
|
| 56 | public static <S extends Structure> MapCodec<S> simpleCodec(final Function<Structure.StructureSettings, S> constructor) {
|
| 57 | return RecordCodecBuilder.mapCodec(i -> i.group(settingsCodec(i)).apply(i, constructor));
|
| 58 | }
|
| 59 |
|
| 60 | protected Structure(final Structure.StructureSettings settings) {
|
| 61 | this.settings = settings;
|
| 62 | }
|
| 63 |
|
| 64 | public HolderSet<Biome> biomes() {
|
| 65 | return this.settings.biomes;
|
| 66 | }
|
| 67 |
|
| 68 | public Map<MobCategory, StructureSpawnOverride> spawnOverrides() {
|
| 69 | return this.settings.spawnOverrides;
|
| 70 | }
|
| 71 |
|
| 72 | public GenerationStep.Decoration step() {
|
| 73 | return this.settings.step;
|
| 74 | }
|
| 75 |
|
| 76 | public TerrainAdjustment terrainAdaptation() {
|
| 77 | return this.settings.terrainAdaptation;
|
| 78 | }
|
| 79 |
|
| 80 | public BoundingBox adjustBoundingBox(final BoundingBox boundingBox) {
|
| 81 | return this.terrainAdaptation() != TerrainAdjustment.NONE ? boundingBox.inflatedBy(12) : boundingBox;
|
| 82 | }
|
| 83 |
|
| 84 | public StructureStart generate(
|
| 85 | final Holder<Structure> selected,
|
| 86 | final ResourceKey<Level> dimension,
|
| 87 | final RegistryAccess registryAccess,
|
| 88 | final ChunkGenerator chunkGenerator,
|
| 89 | final BiomeSource biomeSource,
|
| 90 | final Climate.Sampler climateSampler,
|
| 91 | final RandomState randomState,
|
| 92 | final StructureTemplateManager structureTemplateManager,
|
| 93 | final long seed,
|
| 94 | final ChunkPos sourceChunkPos,
|
| 95 | final int references,
|
| 96 | final LevelHeightAccessor heightAccessor,
|
| 97 | final Predicate<Holder<Biome>> validBiome
|
| 98 | ) {
|
| 99 | ProfiledDuration profiled = JvmProfiler.INSTANCE.onStructureGenerate(sourceChunkPos, dimension, selected);
|
| 100 | Structure.GenerationContext context = new Structure.GenerationContext(
|
| 101 | registryAccess, chunkGenerator, biomeSource, climateSampler, randomState, structureTemplateManager, seed, sourceChunkPos, heightAccessor, validBiome
|
| 102 | );
|
| 103 | Optional<Structure.GenerationStub> generation = this.findValidGenerationPoint(context);
|
| 104 | if (generation.isPresent()) {
|
| 105 | StructurePiecesBuilder builder = generation.get().getPiecesBuilder();
|
| 106 | StructureStart testStart = new StructureStart(this, sourceChunkPos, references, builder.build());
|
| 107 | if (testStart.isValid()) {
|
| 108 | if (profiled != null) {
|
| 109 | profiled.finish(true);
|
| 110 | }
|
| 111 |
|
| 112 | return testStart;
|
| 113 | }
|
| 114 | }
|
| 115 |
|
| 116 | if (profiled != null) {
|
| 117 | profiled.finish(false);
|
| 118 | }
|
| 119 |
|
| 120 | return StructureStart.INVALID_START;
|
| 121 | }
|
| 122 |
|
| 123 | protected static Optional<Structure.GenerationStub> onTopOfChunkCenter(
|
| 124 | final Structure.GenerationContext context, final Heightmap.Types heightmap, final Consumer<StructurePiecesBuilder> generator
|
| 125 | ) {
|
| 126 | return !context.couldValidBiomeExistOnTopOfChunkCenter() ? Optional.empty() : onTopOfChunkCenterWithoutBiomeCheck(context, heightmap, generator);
|
| 127 | }
|
| 128 |
|
| 129 | protected static Optional<Structure.GenerationStub> onTopOfChunkCenterWithoutBiomeCheck(
|
| 130 | final Structure.GenerationContext context, final Heightmap.Types heightmap, final Consumer<StructurePiecesBuilder> generator
|
| 131 | ) {
|
| 132 | ChunkPos chunkPos = context.chunkPos();
|
| 133 | int blockX = chunkPos.getMiddleBlockX();
|
| 134 | int blockZ = chunkPos.getMiddleBlockZ();
|
| 135 | int blockY = context.chunkGenerator().getFirstOccupiedHeight(blockX, blockZ, heightmap, context.heightAccessor(), context.randomState());
|
| 136 | return Optional.of(new Structure.GenerationStub(new BlockPos(blockX, blockY, blockZ), generator));
|
| 137 | }
|
| 138 |
|
| 139 | public void afterPlace(
|
| 140 | final WorldGenLevel level,
|
| 141 | final StructureManager structureManager,
|
| 142 | final ChunkGenerator generator,
|
| 143 | final RandomSource random,
|
| 144 | final BoundingBox chunkBB,
|
| 145 | final ChunkPos chunkPos,
|
| 146 | final PiecesContainer pieces
|
| 147 | ) {
|
| 148 | }
|
| 149 |
|
| 150 | private static int[] getCornerHeights(final Structure.GenerationContext context, final int minX, final int sizeX, final int minZ, final int sizeZ) {
|
| 151 | ChunkGenerator chunkGenerator = context.chunkGenerator();
|
| 152 | LevelHeightAccessor heightAccessor = context.heightAccessor();
|
| 153 | RandomState randomState = context.randomState();
|
| 154 | return new int[]{
|
| 155 | chunkGenerator.getFirstOccupiedHeight(minX, minZ, Heightmap.Types.WORLD_SURFACE_WG, heightAccessor, randomState),
|
| 156 | chunkGenerator.getFirstOccupiedHeight(minX, minZ + sizeZ, Heightmap.Types.WORLD_SURFACE_WG, heightAccessor, randomState),
|
| 157 | chunkGenerator.getFirstOccupiedHeight(minX + sizeX, minZ, Heightmap.Types.WORLD_SURFACE_WG, heightAccessor, randomState),
|
| 158 | chunkGenerator.getFirstOccupiedHeight(minX + sizeX, minZ + sizeZ, Heightmap.Types.WORLD_SURFACE_WG, heightAccessor, randomState)
|
| 159 | };
|
| 160 | }
|
| 161 |
|
| 162 | public static int getMeanFirstOccupiedHeight(final Structure.GenerationContext context, final int minX, final int sizeX, final int minZ, final int sizeZ) {
|
| 163 | int[] cornerHeights = getCornerHeights(context, minX, sizeX, minZ, sizeZ);
|
| 164 | return (cornerHeights[0] + cornerHeights[1] + cornerHeights[2] + cornerHeights[3]) / 4;
|
| 165 | }
|
| 166 |
|
| 167 | protected static int getLowestY(final Structure.GenerationContext context, final int sizeX, final int sizeZ) {
|
| 168 | ChunkPos chunkPos = context.chunkPos();
|
| 169 | int minX = chunkPos.getMinBlockX();
|
| 170 | int minZ = chunkPos.getMinBlockZ();
|
| 171 | return getLowestY(context, minX, minZ, sizeX, sizeZ);
|
| 172 | }
|
| 173 |
|
| 174 | protected static int getLowestY(final Structure.GenerationContext context, final int minX, final int minZ, final int sizeX, final int sizeZ) {
|
| 175 | int[] cornerHeights = getCornerHeights(context, minX, sizeX, minZ, sizeZ);
|
| 176 | return Math.min(Math.min(cornerHeights[0], cornerHeights[1]), Math.min(cornerHeights[2], cornerHeights[3]));
|
| 177 | }
|
| 178 |
|
| 179 | @Deprecated
|
| 180 | protected BlockPos getLowestYIn5by5Box(final Structure.GenerationContext context, final int blockX, final int blockZ, final Rotation rotation) {
|
| 181 | int offsetX = 5;
|
| 182 | int offsetZ = 5;
|
| 183 | if (rotation == Rotation.CLOCKWISE_90) {
|
| 184 | offsetX = -5;
|
| 185 | } else if (rotation == Rotation.CLOCKWISE_180) {
|
| 186 | offsetX = -5;
|
| 187 | offsetZ = -5;
|
| 188 | } else if (rotation == Rotation.COUNTERCLOCKWISE_90) {
|
| 189 | offsetZ = -5;
|
| 190 | }
|
| 191 |
|
| 192 | return new BlockPos(blockX, getLowestY(context, blockX, blockZ, offsetX, offsetZ), blockZ);
|
| 193 | }
|
| 194 |
|
| 195 | protected abstract Optional<Structure.GenerationStub> findGenerationPoint(final Structure.GenerationContext context);
|
| 196 |
|
| 197 | public Optional<Structure.GenerationStub> findValidGenerationPoint(final Structure.GenerationContext context) {
|
| 198 | return this.findGenerationPoint(context).filter(context::isValidBiome);
|
| 199 | }
|
| 200 |
|
| 201 | public abstract StructureType<?> type();
|
| 202 |
|
| 203 | public record GenerationContext(
|
| 204 | RegistryAccess registryAccess,
|
| 205 | ChunkGenerator chunkGenerator,
|
| 206 | BiomeSource biomeSource,
|
| 207 | Climate.Sampler climateSampler,
|
| 208 | BiomeResolver biomeResolver,
|
| 209 | RandomState randomState,
|
| 210 | StructureTemplateManager structureTemplateManager,
|
| 211 | WorldgenRandom random,
|
| 212 | long seed,
|
| 213 | ChunkPos chunkPos,
|
| 214 | LevelHeightAccessor heightAccessor,
|
| 215 | Predicate<Holder<Biome>> validBiome
|
| 216 | ) {
|
| 217 | public GenerationContext(
|
| 218 | final RegistryAccess registryAccess,
|
| 219 | final ChunkGenerator chunkGenerator,
|
| 220 | final BiomeSource biomeSource,
|
| 221 | final Climate.Sampler climateSampler,
|
| 222 | final RandomState randomState,
|
| 223 | final StructureTemplateManager structureTemplateManager,
|
| 224 | final long seed,
|
| 225 | final ChunkPos chunkPos,
|
| 226 | final LevelHeightAccessor heightAccessor,
|
| 227 | final Predicate<Holder<Biome>> validBiome
|
| 228 | ) {
|
| 229 | this(
|
| 230 | registryAccess,
|
| 231 | chunkGenerator,
|
| 232 | biomeSource,
|
| 233 | climateSampler,
|
| 234 | biomeSource.createResolver(climateSampler),
|
| 235 | randomState,
|
| 236 | structureTemplateManager,
|
| 237 | makeRandom(seed, chunkPos),
|
| 238 | seed,
|
| 239 | chunkPos,
|
| 240 | heightAccessor,
|
| 241 | validBiome
|
| 242 | );
|
| 243 | }
|
| 244 |
|
| 245 | private static WorldgenRandom makeRandom(final long seed, final ChunkPos chunkPos) {
|
| 246 | WorldgenRandom random = new WorldgenRandom(new LegacyRandomSource(0L));
|
| 247 | random.setLargeFeatureSeed(seed, chunkPos.x(), chunkPos.z());
|
| 248 | return random;
|
| 249 | }
|
| 250 |
|
| 251 | public boolean isValidBiome(final Structure.GenerationStub stub) {
|
| 252 | BlockPos startPos = stub.position();
|
| 253 | return this.validBiome
|
| 254 | .test(this.biomeResolver.getNoiseBiome(QuartPos.fromBlock(startPos.getX()), QuartPos.fromBlock(startPos.getY()), QuartPos.fromBlock(startPos.getZ())));
|
| 255 | }
|
| 256 |
|
| 257 | public boolean couldStructureExistInColumn(final int blockX, final int blockZ, final int minBlockY, final int maxBlockY) {
|
| 258 | int quartX = QuartPos.fromBlock(blockX);
|
| 259 | int quartZ = QuartPos.fromBlock(blockZ);
|
| 260 | int minQuartY = QuartPos.fromBlock(minBlockY);
|
| 261 | int maxQuartY = QuartPos.fromBlock(maxBlockY);
|
| 262 | BiomeResolver columnResolver = this.biomeSource.createResolverForChunk(this.climateSampler, quartX, minQuartY, quartZ, 1, maxQuartY - minQuartY + 1, 1);
|
| 263 |
|
| 264 | for (int quartY = minQuartY; quartY <= maxQuartY; quartY++) {
|
| 265 | if (this.validBiome.test(columnResolver.getNoiseBiome(quartX, quartY, quartZ))) {
|
| 266 | return true;
|
| 267 | }
|
| 268 | }
|
| 269 |
|
| 270 | return false;
|
| 271 | }
|
| 272 |
|
| 273 | public boolean couldValidBiomeExistInTerrainColumn(final int blockX, final int blockZ) {
|
| 274 | return this.couldStructureExistInColumn(blockX, blockZ, this.heightAccessor.getMinY() - 1, this.heightAccessor.getMaxY());
|
| 275 | }
|
| 276 |
|
| 277 | public boolean couldValidBiomeExistOnTopOfChunkCenter() {
|
| 278 | return this.couldValidBiomeExistInTerrainColumn(this.chunkPos.getMiddleBlockX(), this.chunkPos.getMiddleBlockZ());
|
| 279 | }
|
| 280 | }
|
| 281 |
|
| 282 | public record GenerationStub(BlockPos position, Either<Consumer<StructurePiecesBuilder>, StructurePiecesBuilder> generator) {
|
| 283 | public GenerationStub(final BlockPos position, final Consumer<StructurePiecesBuilder> generator) {
|
| 284 | this(position, Either.left(generator));
|
| 285 | }
|
| 286 |
|
| 287 | public StructurePiecesBuilder getPiecesBuilder() {
|
| 288 | return this.generator.map(pieceGenerator -> {
|
| 289 | StructurePiecesBuilder newBuilder = new StructurePiecesBuilder();
|
| 290 | pieceGenerator.accept(newBuilder);
|
| 291 | return newBuilder;
|
| 292 | }, previousBuilder -> (StructurePiecesBuilder)previousBuilder);
|
| 293 | }
|
| 294 | }
|
| 295 |
|
| 296 | public record StructureSettings(
|
| 297 | HolderSet<Biome> biomes, Map<MobCategory, StructureSpawnOverride> spawnOverrides, GenerationStep.Decoration step, TerrainAdjustment terrainAdaptation
|
| 298 | ) {
|
| 299 | private static final Structure.StructureSettings DEFAULT = new Structure.StructureSettings(
|
| 300 | HolderSet.empty(), Map.of(), GenerationStep.Decoration.SURFACE_STRUCTURES, TerrainAdjustment.NONE
|
| 301 | );
|
| 302 | public static final MapCodec<Structure.StructureSettings> CODEC = RecordCodecBuilder.mapCodec(
|
| 303 | i -> i.group(
|
| 304 | RegistryCodecs.holderSet(Registries.BIOME).fieldOf("biomes").forGetter(Structure.StructureSettings::biomes),
|
| 305 | Codec.simpleMap(MobCategory.CODEC, StructureSpawnOverride.CODEC, StringRepresentable.keys(MobCategory.values()))
|
| 306 | .fieldOf("spawn_overrides")
|
| 307 | .forGetter(Structure.StructureSettings::spawnOverrides),
|
| 308 | GenerationStep.Decoration.CODEC.fieldOf("step").forGetter(Structure.StructureSettings::step),
|
| 309 | TerrainAdjustment.CODEC.optionalFieldOf("terrain_adaptation", DEFAULT.terrainAdaptation).forGetter(Structure.StructureSettings::terrainAdaptation)
|
| 310 | )
|
| 311 | .apply(i, Structure.StructureSettings::new)
|
| 312 | );
|
| 313 |
|
| 314 | public StructureSettings(final HolderSet<Biome> biomes) {
|
| 315 | this(biomes, DEFAULT.spawnOverrides, DEFAULT.step, DEFAULT.terrainAdaptation);
|
| 316 | }
|
| 317 |
|
| 318 | public static class Builder {
|
| 319 | private final HolderSet<Biome> biomes;
|
| 320 | private Map<MobCategory, StructureSpawnOverride> spawnOverrides;
|
| 321 | private GenerationStep.Decoration step;
|
| 322 | private TerrainAdjustment terrainAdaption;
|
| 323 |
|
| 324 | public Builder(final HolderSet<Biome> biomes) {
|
| 325 | this.spawnOverrides = Structure.StructureSettings.DEFAULT.spawnOverrides;
|
| 326 | this.step = Structure.StructureSettings.DEFAULT.step;
|
| 327 | this.terrainAdaption = Structure.StructureSettings.DEFAULT.terrainAdaptation;
|
| 328 | this.biomes = biomes;
|
| 329 | }
|
| 330 |
|
| 331 | public Structure.StructureSettings.Builder spawnOverrides(final Map<MobCategory, StructureSpawnOverride> spawnOverrides) {
|
| 332 | this.spawnOverrides = spawnOverrides;
|
| 333 | return this;
|
| 334 | }
|
| 335 |
|
| 336 | public Structure.StructureSettings.Builder generationStep(final GenerationStep.Decoration step) {
|
| 337 | this.step = step;
|
| 338 | return this;
|
| 339 | }
|
| 340 |
|
| 341 | public Structure.StructureSettings.Builder terrainAdapation(final TerrainAdjustment terrainAdaption) {
|
| 342 | this.terrainAdaption = terrainAdaption;
|
| 343 | return this;
|
| 344 | }
|
| 345 |
|
| 346 | public Structure.StructureSettings build() {
|
| 347 | return new Structure.StructureSettings(this.biomes, this.spawnOverrides, this.step, this.terrainAdaption);
|
| 348 | }
|
| 349 | }
|
| 350 | }
|
| 351 | }
|
| 352 |
|