| 1 | package net.minecraft.world.level.chunk;
|
| 2 |
|
| 3 | import net.minecraft.core.IdMap;
|
| 4 | import net.minecraft.util.Mth;
|
| 5 |
|
| 6 | public abstract class Strategy<T> {
|
| 7 | private static final Palette.Factory SINGLE_VALUE_PALETTE_FACTORY = SingleValuePalette::create;
|
| 8 | private static final Palette.Factory LINEAR_PALETTE_FACTORY = LinearPalette::create;
|
| 9 | private static final Palette.Factory HASHMAP_PALETTE_FACTORY = HashMapPalette::create;
|
| 10 | private static final Configuration ZERO_BITS = new Configuration.Simple(SINGLE_VALUE_PALETTE_FACTORY, 0);
|
| 11 | private static final Configuration ONE_BIT_LINEAR = new Configuration.Simple(LINEAR_PALETTE_FACTORY, 1);
|
| 12 | private static final Configuration TWO_BITS_LINEAR = new Configuration.Simple(LINEAR_PALETTE_FACTORY, 2);
|
| 13 | private static final Configuration THREE_BITS_LINEAR = new Configuration.Simple(LINEAR_PALETTE_FACTORY, 3);
|
| 14 | private static final Configuration FOUR_BITS_LINEAR = new Configuration.Simple(LINEAR_PALETTE_FACTORY, 4);
|
| 15 | private static final Configuration FIVE_BITS_HASHMAP = new Configuration.Simple(HASHMAP_PALETTE_FACTORY, 5);
|
| 16 | private static final Configuration SIX_BITS_HASHMAP = new Configuration.Simple(HASHMAP_PALETTE_FACTORY, 6);
|
| 17 | private static final Configuration SEVEN_BITS_HASHMAP = new Configuration.Simple(HASHMAP_PALETTE_FACTORY, 7);
|
| 18 | private static final Configuration EIGHT_BITS_HASHMAP = new Configuration.Simple(HASHMAP_PALETTE_FACTORY, 8);
|
| 19 | private final IdMap<T> globalMap;
|
| 20 | private final GlobalPalette<T> globalPalette;
|
| 21 | protected final int globalPaletteBitsInMemory;
|
| 22 | private final int bitsPerAxis;
|
| 23 | private final int entryCount;
|
| 24 |
|
| 25 | private Strategy(final IdMap<T> globalMap, final int bitsPerAxis) {
|
| 26 | this.globalMap = globalMap;
|
| 27 | this.globalPalette = new GlobalPalette<>(globalMap);
|
| 28 | this.globalPaletteBitsInMemory = minimumBitsRequiredForDistinctValues(globalMap.size());
|
| 29 | this.bitsPerAxis = bitsPerAxis;
|
| 30 | this.entryCount = 1 << bitsPerAxis * 3;
|
| 31 | }
|
| 32 |
|
| 33 | public static <T> Strategy<T> createForBlockStates(final IdMap<T> registry) {
|
| 34 | return new Strategy<T>(registry, 4) {
|
| 35 | @Override
|
| 36 | public Configuration getConfigurationForBitCount(final int entryBits) {
|
| 37 | return switch (entryBits) {
|
| 38 | case 0 -> Strategy.ZERO_BITS;
|
| 39 | case 1, 2, 3, 4 -> Strategy.FOUR_BITS_LINEAR;
|
| 40 | case 5 -> Strategy.FIVE_BITS_HASHMAP;
|
| 41 | case 6 -> Strategy.SIX_BITS_HASHMAP;
|
| 42 | case 7 -> Strategy.SEVEN_BITS_HASHMAP;
|
| 43 | case 8 -> Strategy.EIGHT_BITS_HASHMAP;
|
| 44 | default -> new Configuration.Global(this.globalPaletteBitsInMemory, entryBits);
|
| 45 | };
|
| 46 | }
|
| 47 | };
|
| 48 | }
|
| 49 |
|
| 50 | public static <T> Strategy<T> createForBiomes(final IdMap<T> registry) {
|
| 51 | return new Strategy<T>(registry, 2) {
|
| 52 | @Override
|
| 53 | public Configuration getConfigurationForBitCount(final int entryBits) {
|
| 54 | return switch (entryBits) {
|
| 55 | case 0 -> Strategy.ZERO_BITS;
|
| 56 | case 1 -> Strategy.ONE_BIT_LINEAR;
|
| 57 | case 2 -> Strategy.TWO_BITS_LINEAR;
|
| 58 | case 3 -> Strategy.THREE_BITS_LINEAR;
|
| 59 | default -> new Configuration.Global(this.globalPaletteBitsInMemory, entryBits);
|
| 60 | };
|
| 61 | }
|
| 62 | };
|
| 63 | }
|
| 64 |
|
| 65 | public int entryCount() {
|
| 66 | return this.entryCount;
|
| 67 | }
|
| 68 |
|
| 69 | public int getIndex(final int x, final int y, final int z) {
|
| 70 | return (y << this.bitsPerAxis | z) << this.bitsPerAxis | x;
|
| 71 | }
|
| 72 |
|
| 73 | public IdMap<T> globalMap() {
|
| 74 | return this.globalMap;
|
| 75 | }
|
| 76 |
|
| 77 | public GlobalPalette<T> globalPalette() {
|
| 78 | return this.globalPalette;
|
| 79 | }
|
| 80 |
|
| 81 | protected abstract Configuration getConfigurationForBitCount(int entryBits);
|
| 82 |
|
| 83 | protected Configuration getConfigurationForPaletteSize(final int paletteSize) {
|
| 84 | int bits = minimumBitsRequiredForDistinctValues(paletteSize);
|
| 85 | return this.getConfigurationForBitCount(bits);
|
| 86 | }
|
| 87 |
|
| 88 | private static int minimumBitsRequiredForDistinctValues(final int count) {
|
| 89 | return Mth.ceillog2(count);
|
| 90 | }
|
| 91 | }
|
| 92 |
|