1818import net .minecraft .world .level .block .state .BlockState ;
1919
2020public final class GeomeGeology {
21+ private static final double GEOME_TRANSITION_SCORE_WIDTH = 0.125D ;
2122 private static final int [] LITHOLOGY_PHASES = {
2223 0 , 2 , 1 , 3 ,
2324 3 , 1 , 2 , 0 ,
@@ -34,6 +35,7 @@ public final class GeomeGeology {
3435 private final short [] whiteNoiseArray ;
3536 private final boolean [] globallyContinuousLayers ;
3637 private final boolean [] regionallyVariedRocks ;
38+ private final int geomeTransitionPhase ;
3739 private final int layerThickness ;
3840 private final int formationRegionScale ;
3941 private final int familyDiversitySlots ;
@@ -67,6 +69,7 @@ public GeomeGeology(long seed, BakedGeomeConfig config) {
6769 }
6870
6971 Random random = new Random (seed ^ 0x5EEDBEEFL );
72+ geomeTransitionPhase = new Random (seed ^ 0x47454F4D4554524EL ).nextInt (256 );
7073 whiteNoiseArray = new short [256 ];
7174 for (int i = 0 ; i < whiteNoiseArray .length ; i ++) {
7275 whiteNoiseArray [i ] = (short ) random .nextInt (0x7FFF );
@@ -109,7 +112,9 @@ public void replaceStoneInChunk(LevelAccessor world, ChunkAccess chunk, BakedTer
109112 long formationRegion = formationRegionAt (x , z );
110113
111114 if (stableLayers ) {
112- changed |= replaceStableColumn (chunk , cursor , geomeIndex , baseRockValue ,
115+ int secondGeome = runnerUpGeome (regionalValues , geomeIndex );
116+ changed |= replaceStableColumn (chunk , cursor , geomeIndex , secondGeome ,
117+ regionalValues , baseRockValue ,
113118 formationRegion , x , z , surfaceY , terrain );
114119 } else {
115120 for (int y = surfaceY ; y >= chunk .getMinBuildHeight (); y --) {
@@ -130,11 +135,14 @@ public void replaceStoneInChunk(LevelAccessor world, ChunkAccess chunk, BakedTer
130135 }
131136
132137 private boolean replaceStableColumn (ChunkAccess chunk , BlockPos .MutableBlockPos cursor , int geomeIndex ,
133- int baseRockValue , long formationRegion , int x , int z , int surfaceY ,
138+ int secondGeome , double [] geomeScores , int baseRockValue ,
139+ long formationRegion , int x , int z , int surfaceY ,
134140 BakedTerrainDimension terrain ) {
135141 int layerIndex = Math .floorDiv (baseRockValue + surfaceY , layerThickness );
136142 int layerStart = layerIndex * layerThickness ;
137- BlockState replacement = pickStableReplacement (geomeIndex , formationRegion , layerIndex );
143+ int layerGeome = pickStableLayerGeome (geomeScores , geomeIndex , secondGeome ,
144+ layerIndex , geomeTransitionPhase );
145+ BlockState replacement = pickStableReplacement (layerGeome , formationRegion , layerIndex );
138146 boolean changed = false ;
139147 cursor .set (x , surfaceY , z );
140148
@@ -143,7 +151,9 @@ private boolean replaceStableColumn(ChunkAccess chunk, BlockPos.MutableBlockPos
143151 if (stratum < layerStart ) {
144152 layerIndex --;
145153 layerStart -= layerThickness ;
146- replacement = pickStableReplacement (geomeIndex , formationRegion , layerIndex );
154+ layerGeome = pickStableLayerGeome (geomeScores , geomeIndex , secondGeome ,
155+ layerIndex , geomeTransitionPhase );
156+ replacement = pickStableReplacement (layerGeome , formationRegion , layerIndex );
147157 }
148158 cursor .setY (y );
149159 if (terrain .isReplaceable (chunk .getBlockState (cursor ))) {
@@ -157,7 +167,14 @@ private boolean replaceStableColumn(ChunkAccess chunk, BlockPos.MutableBlockPos
157167 public Block getStoneAt (Biome biome , int x , int y , int z , int surfaceY ) {
158168 double [] regionalValues = new double [config .geomeCount ()];
159169 int geomeIndex = classifyColumn (biome , x , z , regionalValues );
160- return pickReplacement (geomeIndex , stratumOffsetAt (x , z ), formationRegionAt (x , z ), x , y , z ).getBlock ();
170+ int stratumOffset = stratumOffsetAt (x , z );
171+ long formationRegion = formationRegionAt (x , z );
172+ if (stableLayers ) {
173+ int layerIndex = Math .floorDiv (stratumOffset + y , layerThickness );
174+ geomeIndex = pickStableLayerGeome (regionalValues , geomeIndex ,
175+ runnerUpGeome (regionalValues , geomeIndex ), layerIndex , geomeTransitionPhase );
176+ }
177+ return pickReplacement (geomeIndex , stratumOffset , formationRegion , x , y , z ).getBlock ();
161178 }
162179
163180 public String getGeomeName (Biome biome , int x , int z ) {
@@ -176,18 +193,24 @@ int classifyColumn(Biome biome, int x, int z, double[] regionalValues) {
176193 public ColumnSample sampleColumn (Biome biome , ResourceLocation biomeId , int x , int z ) {
177194 double [] regionalValues = new double [config .geomeCount ()];
178195 int geomeIndex = classifyColumn (biome , biomeId , x , z , regionalValues );
179- return new ColumnSample (geomeIndex , stratumOffsetAt (x , z ), formationRegionAt (x , z ), x , z );
196+ return new ColumnSample (geomeIndex , runnerUpGeome (regionalValues , geomeIndex ),
197+ regionalValues , stratumOffsetAt (x , z ), formationRegionAt (x , z ), x , z );
180198 }
181199
182200 public final class ColumnSample {
183201 private final int geomeIndex ;
202+ private final int secondGeome ;
203+ private final double [] geomeScores ;
184204 private final int stratumOffset ;
185205 private final long formationRegion ;
186206 private final int x ;
187207 private final int z ;
188208
189- private ColumnSample (int geomeIndex , int stratumOffset , long formationRegion , int x , int z ) {
209+ private ColumnSample (int geomeIndex , int secondGeome , double [] geomeScores ,
210+ int stratumOffset , long formationRegion , int x , int z ) {
190211 this .geomeIndex = geomeIndex ;
212+ this .secondGeome = secondGeome ;
213+ this .geomeScores = geomeScores ;
191214 this .stratumOffset = stratumOffset ;
192215 this .formationRegion = formationRegion ;
193216 this .x = x ;
@@ -199,21 +222,27 @@ public String geomeName() {
199222 }
200223
201224 public BlockState rockAt (int y ) {
202- return pickReplacement (geomeIndex , stratumOffset , formationRegion , x , y , z );
225+ int selectedGeome = geomeIndex ;
226+ if (stableLayers ) {
227+ int layerIndex = Math .floorDiv (stratumOffset + y , layerThickness );
228+ selectedGeome = pickStableLayerGeome (geomeScores , geomeIndex , secondGeome ,
229+ layerIndex , geomeTransitionPhase );
230+ }
231+ return pickReplacement (selectedGeome , stratumOffset , formationRegion , x , y , z );
203232 }
204233
205234 public RockFamily familyAt (int y ) {
206235 return config .familyOf (rockAt (y ));
207236 }
208237 }
209238
210- private int classifyColumn (Biome biome , ResourceLocation biomeId , int x , int z , double [] regionalValues ) {
239+ int classifyColumn (Biome biome , ResourceLocation biomeId , int x , int z , double [] regionalValues ) {
211240 for (int i = 0 ; i < regionalValues .length ; i ++) {
212241 regionalValues [i ] = regionalNoise .valueAt (x + config .noiseOffsetX [i ], z + config .noiseOffsetZ [i ]);
213242 }
214243
215244 double boundary = boundaryNoise .valueAt (x , z );
216- return config .pickGeome (biome , biomeId , regionalValues , boundary );
245+ return config .scoreGeomes (biome , biomeId , regionalValues , boundary );
217246 }
218247
219248 private net .minecraft .world .level .block .state .BlockState pickReplacement (int geomeIndex , int baseRockValue ,
@@ -269,7 +298,13 @@ int stratumLayerAt(int x, int y, int z) {
269298 return Math .floorDiv (stratumOffsetAt (x , z ) + y , layerThickness );
270299 }
271300
272- Block getStoneAt (int geomeIndex , int stratumOffset , long formationRegion , int x , int y , int z ) {
301+ Block getStoneAt (int geomeIndex , double [] geomeScores , int stratumOffset ,
302+ long formationRegion , int x , int y , int z ) {
303+ if (stableLayers ) {
304+ int layerIndex = Math .floorDiv (stratumOffset + y , layerThickness );
305+ geomeIndex = pickStableLayerGeome (geomeScores , geomeIndex ,
306+ runnerUpGeome (geomeScores , geomeIndex ), layerIndex , geomeTransitionPhase );
307+ }
273308 return pickReplacement (geomeIndex , stratumOffset , formationRegion , x , y , z ).getBlock ();
274309 }
275310
@@ -298,6 +333,44 @@ private static int mixRegion(int cellX, int cellZ, int contour) {
298333 return hash ^ (hash >>> 16 );
299334 }
300335
336+ static int pickStableLayerGeome (double [] geomeScores , int firstGeome , int secondGeome ,
337+ int layerIndex , int phase ) {
338+ if (firstGeome == secondGeome ) {
339+ return firstGeome ;
340+ }
341+ int lowerGeome = Math .min (firstGeome , secondGeome );
342+ int higherGeome = Math .max (firstGeome , secondGeome );
343+ double higherFraction = 0.5D + ((geomeScores [higherGeome ] - geomeScores [lowerGeome ])
344+ / (2.0D * GEOME_TRANSITION_SCORE_WIDTH ));
345+ if (higherFraction <= 0.0D ) {
346+ return lowerGeome ;
347+ }
348+ if (higherFraction >= 1.0D ) {
349+ return higherGeome ;
350+ }
351+
352+ // Bit reversal supplies an allocation-free low-discrepancy sequence. Nearby
353+ // layers therefore cross a close geome boundary at different horizontal
354+ // positions instead of moving as one full-height wall.
355+ int pairPhase = (lowerGeome * 53 ) + (higherGeome * 97 );
356+ int layerBucket = (layerIndex + phase + pairPhase ) & 0xFF ;
357+ int threshold = Integer .reverse (layerBucket ) >>> 24 ;
358+ return ((threshold + 0.5D ) / 256.0D ) < higherFraction ? higherGeome : lowerGeome ;
359+ }
360+
361+ private static int runnerUpGeome (double [] geomeScores , int bestGeome ) {
362+ if (geomeScores .length < 2 ) {
363+ return bestGeome ;
364+ }
365+ int second = bestGeome == 0 ? 1 : 0 ;
366+ for (int i = 0 ; i < geomeScores .length ; i ++) {
367+ if (i != bestGeome && geomeScores [i ] > geomeScores [second ]) {
368+ second = i ;
369+ }
370+ }
371+ return second ;
372+ }
373+
301374 private static double faciesFraction (double regionScale ) {
302375 if (regionScale <= 100.0D ) {
303376 return Math .max (0.0D , (regionScale - 50.0D ) / 350.0D );
0 commit comments