| 1 | package net.minecraft.world.level.levelgen.structure.placement;
|
| 2 |
|
| 3 | import com.mojang.datafixers.Products.P5;
|
| 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 com.mojang.serialization.codecs.RecordCodecBuilder.Mu;
|
| 9 | import java.util.Optional;
|
| 10 | import net.minecraft.core.Holder;
|
| 11 | import net.minecraft.core.Vec3i;
|
| 12 | import net.minecraft.core.registries.Registries;
|
| 13 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 14 | import net.minecraft.util.ExtraCodecs;
|
| 15 | import net.minecraft.util.StringRepresentable;
|
| 16 | import net.minecraft.world.level.chunk.ChunkGeneratorStructureState;
|
| 17 | import net.minecraft.world.level.levelgen.LegacyRandomSource;
|
| 18 | import net.minecraft.world.level.levelgen.WorldgenRandom;
|
| 19 | import net.minecraft.world.level.levelgen.structure.StructureSet;
|
| 20 |
|
| 21 | public abstract class AbstractSpreadingStructurePlacement implements StructurePlacement {
|
| 22 | private static final int HIGHLY_ARBITRARY_RANDOM_SALT = 10387320;
|
| 23 | private final Vec3i locateOffset;
|
| 24 | private final AbstractSpreadingStructurePlacement.FrequencyReductionMethod frequencyReductionMethod;
|
| 25 | private final float frequency;
|
| 26 | private final int salt;
|
| 27 | private final Optional<AbstractSpreadingStructurePlacement.ExclusionZone> exclusionZone;
|
| 28 |
|
| 29 | protected static <S extends AbstractSpreadingStructurePlacement> P5<Mu<S>, Vec3i, AbstractSpreadingStructurePlacement.FrequencyReductionMethod, Float, Integer, Optional<AbstractSpreadingStructurePlacement.ExclusionZone>> placementCodec(
|
| 30 | final Instance<S> i
|
| 31 | ) {
|
| 32 | return i.group(
|
| 33 | Vec3i.offsetCodec(16).optionalFieldOf("locate_offset", Vec3i.ZERO).forGetter(AbstractSpreadingStructurePlacement::locateOffset),
|
| 34 | AbstractSpreadingStructurePlacement.FrequencyReductionMethod.CODEC
|
| 35 | .optionalFieldOf("frequency_reduction_method", AbstractSpreadingStructurePlacement.FrequencyReductionMethod.DEFAULT)
|
| 36 | .forGetter(AbstractSpreadingStructurePlacement::frequencyReductionMethod),
|
| 37 | Codec.floatRange(0.0F, 1.0F).optionalFieldOf("frequency", 1.0F).forGetter(AbstractSpreadingStructurePlacement::frequency),
|
| 38 | ExtraCodecs.NON_NEGATIVE_INT.fieldOf("salt").forGetter(AbstractSpreadingStructurePlacement::salt),
|
| 39 | AbstractSpreadingStructurePlacement.ExclusionZone.CODEC.optionalFieldOf("exclusion_zone").forGetter(AbstractSpreadingStructurePlacement::exclusionZone)
|
| 40 | );
|
| 41 | }
|
| 42 |
|
| 43 | protected AbstractSpreadingStructurePlacement(
|
| 44 | final Vec3i locateOffset,
|
| 45 | final AbstractSpreadingStructurePlacement.FrequencyReductionMethod frequencyReductionMethod,
|
| 46 | final float frequency,
|
| 47 | final int salt,
|
| 48 | final Optional<AbstractSpreadingStructurePlacement.ExclusionZone> exclusionZone
|
| 49 | ) {
|
| 50 | this.locateOffset = locateOffset;
|
| 51 | this.frequencyReductionMethod = frequencyReductionMethod;
|
| 52 | this.frequency = frequency;
|
| 53 | this.salt = salt;
|
| 54 | this.exclusionZone = exclusionZone;
|
| 55 | }
|
| 56 |
|
| 57 | @Override
|
| 58 | public abstract MapCodec<? extends AbstractSpreadingStructurePlacement> codec();
|
| 59 |
|
| 60 | @Override
|
| 61 | public Vec3i locateOffset() {
|
| 62 | return this.locateOffset;
|
| 63 | }
|
| 64 |
|
| 65 | protected AbstractSpreadingStructurePlacement.FrequencyReductionMethod frequencyReductionMethod() {
|
| 66 | return this.frequencyReductionMethod;
|
| 67 | }
|
| 68 |
|
| 69 | protected float frequency() {
|
| 70 | return this.frequency;
|
| 71 | }
|
| 72 |
|
| 73 | protected int salt() {
|
| 74 | return this.salt;
|
| 75 | }
|
| 76 |
|
| 77 | protected Optional<AbstractSpreadingStructurePlacement.ExclusionZone> exclusionZone() {
|
| 78 | return this.exclusionZone;
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | public boolean isStructureChunk(final ChunkGeneratorStructureState state, final int sourceX, final int sourceZ) {
|
| 83 | return this.isPlacementChunk(state, sourceX, sourceZ)
|
| 84 | && this.applyAdditionalChunkRestrictions(sourceX, sourceZ, state.getLevelSeed())
|
| 85 | && this.applyInteractionsWithOtherStructures(state, sourceX, sourceZ);
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public boolean applyAdditionalChunkRestrictions(final int sourceX, final int sourceZ, final long levelSeed) {
|
| 90 | return !(this.frequency < 1.0F) || this.frequencyReductionMethod.shouldGenerate(levelSeed, this.salt, sourceX, sourceZ, this.frequency);
|
| 91 | }
|
| 92 |
|
| 93 | public boolean applyInteractionsWithOtherStructures(final ChunkGeneratorStructureState state, final int sourceX, final int sourceZ) {
|
| 94 | return !this.exclusionZone.isPresent() || !this.exclusionZone.get().isPlacementForbidden(state, sourceX, sourceZ);
|
| 95 | }
|
| 96 |
|
| 97 | protected abstract boolean isPlacementChunk(final ChunkGeneratorStructureState state, final int sourceX, final int sourceZ);
|
| 98 |
|
| 99 | private static boolean probabilityReducer(final long seed, final int salt, final int sourceX, final int sourceZ, final float probability) {
|
| 100 | WorldgenRandom random = new WorldgenRandom(new LegacyRandomSource(0L));
|
| 101 | random.setLargeFeatureWithSalt(seed, salt, sourceX, sourceZ);
|
| 102 | return random.nextFloat() < probability;
|
| 103 | }
|
| 104 |
|
| 105 | private static boolean legacyProbabilityReducerWithDouble(final long seed, final int salt, final int sourceX, final int sourceZ, final float probability) {
|
| 106 | WorldgenRandom random = new WorldgenRandom(new LegacyRandomSource(0L));
|
| 107 | random.setLargeFeatureSeed(seed, sourceX, sourceZ);
|
| 108 | return random.nextDouble() < probability;
|
| 109 | }
|
| 110 |
|
| 111 | private static boolean legacyArbitrarySaltProbabilityReducer(final long seed, final int salt, final int sourceX, final int sourceZ, final float probability) {
|
| 112 | WorldgenRandom random = new WorldgenRandom(new LegacyRandomSource(0L));
|
| 113 | random.setLargeFeatureWithSalt(seed, sourceX, sourceZ, 10387320);
|
| 114 | return random.nextFloat() < probability;
|
| 115 | }
|
| 116 |
|
| 117 | private static boolean legacyPillagerOutpostReducer(final long seed, final int salt, final int sourceX, final int sourceZ, final float probability) {
|
| 118 | int cx = sourceX >> 4;
|
| 119 | int cz = sourceZ >> 4;
|
| 120 | WorldgenRandom random = new WorldgenRandom(new LegacyRandomSource(0L));
|
| 121 | random.setSeed(cx ^ cz << 4 ^ seed);
|
| 122 | random.nextInt();
|
| 123 | return random.nextInt((int)(1.0F / probability)) == 0;
|
| 124 | }
|
| 125 |
|
| 126 | @Deprecated
|
| 127 | public record ExclusionZone(Holder<StructureSet> otherSet, int chunkCount) {
|
| 128 | public static final Codec<AbstractSpreadingStructurePlacement.ExclusionZone> CODEC = RecordCodecBuilder.create(
|
| 129 | i -> i.group(
|
| 130 | RegistryCodecs.holder(Registries.STRUCTURE_SET, StructureSet.DIRECT_CODEC, false)
|
| 131 | .fieldOf("other_set")
|
| 132 | .forGetter(AbstractSpreadingStructurePlacement.ExclusionZone::otherSet),
|
| 133 | Codec.intRange(1, 16).fieldOf("chunk_count").forGetter(AbstractSpreadingStructurePlacement.ExclusionZone::chunkCount)
|
| 134 | )
|
| 135 | .apply(i, AbstractSpreadingStructurePlacement.ExclusionZone::new)
|
| 136 | );
|
| 137 |
|
| 138 | private boolean isPlacementForbidden(final ChunkGeneratorStructureState state, final int sourceX, final int sourceZ) {
|
| 139 | return state.hasStructureChunkInRange(this.otherSet, sourceX, sourceZ, this.chunkCount);
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | @FunctionalInterface
|
| 144 | public interface FrequencyReducer {
|
| 145 | boolean shouldGenerate(long seed, final int salt, final int sourceX, final int sourceZ, float probability);
|
| 146 | }
|
| 147 |
|
| 148 | public enum FrequencyReductionMethod implements StringRepresentable {
|
| 149 | DEFAULT("default", AbstractSpreadingStructurePlacement::probabilityReducer),
|
| 150 | LEGACY_TYPE_1("legacy_type_1", AbstractSpreadingStructurePlacement::legacyPillagerOutpostReducer),
|
| 151 | LEGACY_TYPE_2("legacy_type_2", AbstractSpreadingStructurePlacement::legacyArbitrarySaltProbabilityReducer),
|
| 152 | LEGACY_TYPE_3("legacy_type_3", AbstractSpreadingStructurePlacement::legacyProbabilityReducerWithDouble);
|
| 153 |
|
| 154 | public static final Codec<AbstractSpreadingStructurePlacement.FrequencyReductionMethod> CODEC = StringRepresentable.fromEnum(
|
| 155 | AbstractSpreadingStructurePlacement.FrequencyReductionMethod::values
|
| 156 | );
|
| 157 | private final String name;
|
| 158 | private final AbstractSpreadingStructurePlacement.FrequencyReducer reducer;
|
| 159 |
|
| 160 | FrequencyReductionMethod(final String name, final AbstractSpreadingStructurePlacement.FrequencyReducer reducer) {
|
| 161 | this.name = name;
|
| 162 | this.reducer = reducer;
|
| 163 | }
|
| 164 |
|
| 165 | public boolean shouldGenerate(final long seed, final int salt, final int sourceX, final int sourceZ, final float probability) {
|
| 166 | return this.reducer.shouldGenerate(seed, salt, sourceX, sourceZ, probability);
|
| 167 | }
|
| 168 |
|
| 169 | @Override
|
| 170 | public String getSerializedName() {
|
| 171 | return this.name;
|
| 172 | }
|
| 173 | }
|
| 174 | }
|
| 175 |
|