| 1 | package net.minecraft.world.level.levelgen;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import com.mojang.serialization.Codec;
|
| 5 | import io.netty.buffer.ByteBuf;
|
| 6 | import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
| 7 | import java.util.EnumSet;
|
| 8 | import java.util.List;
|
| 9 | import java.util.Set;
|
| 10 | import java.util.function.IntFunction;
|
| 11 | import java.util.function.Predicate;
|
| 12 | import net.minecraft.core.BlockPos;
|
| 13 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 14 | import net.minecraft.network.codec.StreamCodec;
|
| 15 | import net.minecraft.tags.BlockTags;
|
| 16 | import net.minecraft.util.BitStorage;
|
| 17 | import net.minecraft.util.ByIdMap;
|
| 18 | import net.minecraft.util.Mth;
|
| 19 | import net.minecraft.util.SimpleBitStorage;
|
| 20 | import net.minecraft.util.StringRepresentable;
|
| 21 | import net.minecraft.world.level.block.Blocks;
|
| 22 | import net.minecraft.world.level.block.state.BlockState;
|
| 23 | import net.minecraft.world.level.chunk.ChunkAccess;
|
| 24 | import org.slf4j.Logger;
|
| 25 |
|
| 26 | public class Heightmap {
|
| 27 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 28 | private static final Predicate<BlockState> NOT_AIR = input -> !input.isAir();
|
| 29 | private static final Predicate<BlockState> MATERIAL_MOTION_BLOCKING = state -> state.is(BlockTags.BLOCKS_MOTION_IN_HEIGHTMAP);
|
| 30 | private final BitStorage data;
|
| 31 | private final Predicate<BlockState> isOpaque;
|
| 32 | private final ChunkAccess chunk;
|
| 33 |
|
| 34 | public Heightmap(final ChunkAccess chunk, final Heightmap.Types heightmapType) {
|
| 35 | this.isOpaque = heightmapType.isOpaque();
|
| 36 | this.chunk = chunk;
|
| 37 | int heightBits = Mth.ceillog2(chunk.getHeight() + 1);
|
| 38 | this.data = new SimpleBitStorage(heightBits, 256);
|
| 39 | }
|
| 40 |
|
| 41 | public static void primeHeightmaps(final ChunkAccess chunk, final Set<Heightmap.Types> types) {
|
| 42 | if (!types.isEmpty()) {
|
| 43 | int size = types.size();
|
| 44 | List<Heightmap> allHeightmaps = new ObjectArrayList<>(size);
|
| 45 |
|
| 46 | for (Heightmap.Types type : types) {
|
| 47 | allHeightmaps.add(chunk.getOrCreateHeightmapUnprimed(type));
|
| 48 | }
|
| 49 |
|
| 50 | List<Heightmap> remainingHeightmaps = new ObjectArrayList<>(size);
|
| 51 | int highestSectionPosition = chunk.getHighestSectionPosition() + 16;
|
| 52 | BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
|
| 53 |
|
| 54 | for (int x = 0; x < 16; x++) {
|
| 55 | for (int z = 0; z < 16; z++) {
|
| 56 | remainingHeightmaps.clear();
|
| 57 | remainingHeightmaps.addAll(allHeightmaps);
|
| 58 |
|
| 59 | for (int y = highestSectionPosition - 1; y >= chunk.getMinY(); y--) {
|
| 60 | pos.set(x, y, z);
|
| 61 | BlockState state = chunk.getBlockState(pos);
|
| 62 | if (!state.is(Blocks.AIR)) {
|
| 63 | for (int i = 0; i < remainingHeightmaps.size(); i++) {
|
| 64 | Heightmap heightmap = remainingHeightmaps.get(i);
|
| 65 | if (heightmap.isOpaque.test(state)) {
|
| 66 | heightmap.setHeight(x, z, y + 1);
|
| 67 | remainingHeightmaps.remove(i);
|
| 68 | i--;
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | if (remainingHeightmaps.isEmpty()) {
|
| 73 | break;
|
| 74 | }
|
| 75 | }
|
| 76 | }
|
| 77 | }
|
| 78 | }
|
| 79 | }
|
| 80 | }
|
| 81 |
|
| 82 | public boolean update(final int localX, final int localY, final int localZ, final BlockState state) {
|
| 83 | int firstAvailable = this.getFirstAvailable(localX, localZ);
|
| 84 | if (localY <= firstAvailable - 2) {
|
| 85 | return false;
|
| 86 | }
|
| 87 |
|
| 88 | if (this.isOpaque.test(state)) {
|
| 89 | if (localY >= firstAvailable) {
|
| 90 | this.setHeight(localX, localZ, localY + 1);
|
| 91 | return true;
|
| 92 | }
|
| 93 | } else if (firstAvailable - 1 == localY) {
|
| 94 | BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
|
| 95 |
|
| 96 | for (int y = localY - 1; y >= this.chunk.getMinY(); y--) {
|
| 97 | pos.set(localX, y, localZ);
|
| 98 | if (this.isOpaque.test(this.chunk.getBlockState(pos))) {
|
| 99 | this.setHeight(localX, localZ, y + 1);
|
| 100 | return true;
|
| 101 | }
|
| 102 | }
|
| 103 |
|
| 104 | this.setHeight(localX, localZ, this.chunk.getMinY());
|
| 105 | return true;
|
| 106 | }
|
| 107 |
|
| 108 | return false;
|
| 109 | }
|
| 110 |
|
| 111 | public int getFirstAvailable(final int x, final int z) {
|
| 112 | return this.getFirstAvailable(getIndex(x, z));
|
| 113 | }
|
| 114 |
|
| 115 | public int getHighestTaken(final int x, final int z) {
|
| 116 | return this.getFirstAvailable(getIndex(x, z)) - 1;
|
| 117 | }
|
| 118 |
|
| 119 | private int getFirstAvailable(final int index) {
|
| 120 | return this.data.get(index) + this.chunk.getMinY();
|
| 121 | }
|
| 122 |
|
| 123 | private void setHeight(final int x, final int z, final int height) {
|
| 124 | this.data.set(getIndex(x, z), height - this.chunk.getMinY());
|
| 125 | }
|
| 126 |
|
| 127 | public void setRawData(final ChunkAccess chunk, final Heightmap.Types type, final long[] data) {
|
| 128 | long[] rawData = this.data.getRaw();
|
| 129 | if (rawData.length == data.length) {
|
| 130 | System.arraycopy(data, 0, rawData, 0, data.length);
|
| 131 | } else {
|
| 132 | LOGGER.warn("Ignoring heightmap data for chunk {}, size does not match; expected: {}, got: {}", chunk.getPos(), rawData.length, data.length);
|
| 133 | primeHeightmaps(chunk, EnumSet.of(type));
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | public long[] getRawData() {
|
| 138 | return this.data.getRaw();
|
| 139 | }
|
| 140 |
|
| 141 | private static int getIndex(final int x, final int z) {
|
| 142 | return x + z * 16;
|
| 143 | }
|
| 144 |
|
| 145 | public enum Types implements StringRepresentable {
|
| 146 | WORLD_SURFACE_WG(0, "WORLD_SURFACE_WG", Heightmap.Usage.WORLDGEN, Heightmap.NOT_AIR),
|
| 147 | WORLD_SURFACE(1, "WORLD_SURFACE", Heightmap.Usage.CLIENT, Heightmap.NOT_AIR),
|
| 148 | OCEAN_FLOOR_WG(2, "OCEAN_FLOOR_WG", Heightmap.Usage.WORLDGEN, Heightmap.MATERIAL_MOTION_BLOCKING),
|
| 149 | OCEAN_FLOOR(3, "OCEAN_FLOOR", Heightmap.Usage.LIVE_WORLD, Heightmap.MATERIAL_MOTION_BLOCKING),
|
| 150 | MOTION_BLOCKING(4, "MOTION_BLOCKING", Heightmap.Usage.CLIENT, input -> input.is(BlockTags.BLOCKS_MOTION_IN_HEIGHTMAP) || !input.getFluidState().isEmpty()),
|
| 151 | MOTION_BLOCKING_NO_LEAVES(
|
| 152 | 5,
|
| 153 | "MOTION_BLOCKING_NO_LEAVES",
|
| 154 | Heightmap.Usage.CLIENT,
|
| 155 | input -> input.is(BlockTags.BLOCKS_MOTION_IN_HEIGHTMAP_NO_LEAVES) || !input.getFluidState().isEmpty()
|
| 156 | );
|
| 157 |
|
| 158 | public static final Codec<Heightmap.Types> CODEC = StringRepresentable.fromEnum(Heightmap.Types::values);
|
| 159 | private static final IntFunction<Heightmap.Types> BY_ID = ByIdMap.continuous(t -> t.id, values(), ByIdMap.OutOfBoundsStrategy.ZERO);
|
| 160 | public static final StreamCodec<ByteBuf, Heightmap.Types> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, t -> t.id);
|
| 161 | private final int id;
|
| 162 | private final String serializationKey;
|
| 163 | private final Heightmap.Usage usage;
|
| 164 | private final Predicate<BlockState> isOpaque;
|
| 165 |
|
| 166 | Types(final int id, final String serializationKey, final Heightmap.Usage usage, final Predicate<BlockState> isOpaque) {
|
| 167 | this.id = id;
|
| 168 | this.serializationKey = serializationKey;
|
| 169 | this.usage = usage;
|
| 170 | this.isOpaque = isOpaque;
|
| 171 | }
|
| 172 |
|
| 173 | public String getSerializationKey() {
|
| 174 | return this.serializationKey;
|
| 175 | }
|
| 176 |
|
| 177 | public boolean sendToClient() {
|
| 178 | return this.usage == Heightmap.Usage.CLIENT;
|
| 179 | }
|
| 180 |
|
| 181 | public boolean keepAfterWorldgen() {
|
| 182 | return this.usage != Heightmap.Usage.WORLDGEN;
|
| 183 | }
|
| 184 |
|
| 185 | public Predicate<BlockState> isOpaque() {
|
| 186 | return this.isOpaque;
|
| 187 | }
|
| 188 |
|
| 189 | @Override
|
| 190 | public String getSerializedName() {
|
| 191 | return this.serializationKey;
|
| 192 | }
|
| 193 | }
|
| 194 |
|
| 195 | public enum Usage {
|
| 196 | WORLDGEN,
|
| 197 | LIVE_WORLD,
|
| 198 | CLIENT;
|
| 199 | }
|
| 200 | }
|
| 201 |
|