| 1 | package net.minecraft.world.level.lighting;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.longs.LongArrayFIFOQueue;
|
| 4 | import it.unimi.dsi.fastutil.longs.LongIterator;
|
| 5 | import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
|
| 6 | import java.util.Arrays;
|
| 7 | import net.minecraft.core.BlockPos;
|
| 8 | import net.minecraft.core.Direction;
|
| 9 | import net.minecraft.core.SectionPos;
|
| 10 | import net.minecraft.world.level.ChunkPos;
|
| 11 | import net.minecraft.world.level.block.Blocks;
|
| 12 | import net.minecraft.world.level.block.state.BlockState;
|
| 13 | import net.minecraft.world.level.chunk.DataLayer;
|
| 14 | import net.minecraft.world.level.chunk.LightChunk;
|
| 15 | import net.minecraft.world.level.chunk.LightChunkGetter;
|
| 16 | import net.minecraft.world.phys.shapes.Shapes;
|
| 17 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 |
|
| 20 | public abstract class LightEngine<M extends DataLayerStorageMap<M>, S extends LayerLightSectionStorage<M>> implements LayerLightEventListener {
|
| 21 | public static final int MAX_LEVEL = 15;
|
| 22 | protected static final int MIN_OPACITY = 1;
|
| 23 | protected static final long PULL_LIGHT_IN_ENTRY = LightEngine.QueueEntry.decreaseAllDirections(1);
|
| 24 | private static final int MIN_QUEUE_SIZE = 512;
|
| 25 | protected static final Direction[] PROPAGATION_DIRECTIONS = Direction.values();
|
| 26 | protected final LightChunkGetter chunkSource;
|
| 27 | protected final S storage;
|
| 28 | private final LongOpenHashSet blockNodesToCheck = new LongOpenHashSet(512, 0.5F);
|
| 29 | private final LongArrayFIFOQueue decreaseQueue = new LongArrayFIFOQueue();
|
| 30 | private final LongArrayFIFOQueue increaseQueue = new LongArrayFIFOQueue();
|
| 31 | private static final int CACHE_SIZE = 2;
|
| 32 | private final long[] lastChunkPos = new long[2];
|
| 33 | private final LightChunk[] lastChunk = new LightChunk[2];
|
| 34 |
|
| 35 | protected LightEngine(final LightChunkGetter chunkSource, final S storage) {
|
| 36 | this.chunkSource = chunkSource;
|
| 37 | this.storage = storage;
|
| 38 | this.clearChunkCache();
|
| 39 | }
|
| 40 |
|
| 41 | public static boolean hasDifferentLightProperties(final BlockState oldState, final BlockState newState) {
|
| 42 | return newState == oldState
|
| 43 | ? false
|
| 44 | : newState.getLightDampening() != oldState.getLightDampening()
|
| 45 | || newState.getLightEmission() != oldState.getLightEmission()
|
| 46 | || newState.useShapeForLightOcclusion()
|
| 47 | || oldState.useShapeForLightOcclusion();
|
| 48 | }
|
| 49 |
|
| 50 | public static int getLightDampeningInto(final BlockState fromState, final BlockState toState, final Direction direction, final int simpleOpacity) {
|
| 51 | boolean fromEmpty = isEmptyShape(fromState);
|
| 52 | boolean toEmpty = isEmptyShape(toState);
|
| 53 | if (fromEmpty && toEmpty) {
|
| 54 | return simpleOpacity;
|
| 55 | }
|
| 56 |
|
| 57 | VoxelShape fromShape = fromEmpty ? Shapes.empty() : fromState.getOcclusionShape();
|
| 58 | VoxelShape toShape = toEmpty ? Shapes.empty() : toState.getOcclusionShape();
|
| 59 | return Shapes.mergedFaceOccludes(fromShape, toShape, direction) ? 16 : simpleOpacity;
|
| 60 | }
|
| 61 |
|
| 62 | public static VoxelShape getOcclusionShape(final BlockState state, final Direction direction) {
|
| 63 | return isEmptyShape(state) ? Shapes.empty() : state.getFaceOcclusionShape(direction);
|
| 64 | }
|
| 65 |
|
| 66 | protected static boolean isEmptyShape(final BlockState state) {
|
| 67 | return !state.canOcclude() || !state.useShapeForLightOcclusion();
|
| 68 | }
|
| 69 |
|
| 70 | protected BlockState getState(final BlockPos pos) {
|
| 71 | int chunkX = SectionPos.blockToSectionCoord(pos.getX());
|
| 72 | int chunkZ = SectionPos.blockToSectionCoord(pos.getZ());
|
| 73 | LightChunk chunk = this.getChunk(chunkX, chunkZ);
|
| 74 | return chunk == null ? Blocks.BEDROCK.defaultBlockState() : chunk.getBlockState(pos);
|
| 75 | }
|
| 76 |
|
| 77 | protected int getOpacity(final BlockState state) {
|
| 78 | return Math.max(1, state.getLightDampening());
|
| 79 | }
|
| 80 |
|
| 81 | protected boolean shapeOccludes(final BlockState fromState, final BlockState toState, final Direction direction) {
|
| 82 | VoxelShape fromShape = getOcclusionShape(fromState, direction);
|
| 83 | VoxelShape toShape = getOcclusionShape(toState, direction.getOpposite());
|
| 84 | return Shapes.faceShapeOccludes(fromShape, toShape);
|
| 85 | }
|
| 86 |
|
| 87 | protected @Nullable LightChunk getChunk(final int chunkX, final int chunkZ) {
|
| 88 | long pos = ChunkPos.pack(chunkX, chunkZ);
|
| 89 |
|
| 90 | for (int i = 0; i < 2; i++) {
|
| 91 | if (pos == this.lastChunkPos[i]) {
|
| 92 | return this.lastChunk[i];
|
| 93 | }
|
| 94 | }
|
| 95 |
|
| 96 | LightChunk chunk = this.chunkSource.getChunkForLighting(chunkX, chunkZ);
|
| 97 |
|
| 98 | for (int i = 1; i > 0; i--) {
|
| 99 | this.lastChunkPos[i] = this.lastChunkPos[i - 1];
|
| 100 | this.lastChunk[i] = this.lastChunk[i - 1];
|
| 101 | }
|
| 102 |
|
| 103 | this.lastChunkPos[0] = pos;
|
| 104 | this.lastChunk[0] = chunk;
|
| 105 | return chunk;
|
| 106 | }
|
| 107 |
|
| 108 | private void clearChunkCache() {
|
| 109 | Arrays.fill(this.lastChunkPos, ChunkPos.INVALID_CHUNK_POS);
|
| 110 | Arrays.fill(this.lastChunk, null);
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public void checkBlock(final BlockPos pos) {
|
| 115 | this.blockNodesToCheck.add(pos.asLong());
|
| 116 | }
|
| 117 |
|
| 118 | public void queueSectionData(final long pos, final @Nullable DataLayer data) {
|
| 119 | this.storage.queueSectionData(pos, data);
|
| 120 | }
|
| 121 |
|
| 122 | public void retainData(final ChunkPos pos, final boolean retain) {
|
| 123 | this.storage.retainData(SectionPos.getZeroNode(pos.x(), pos.z()), retain);
|
| 124 | }
|
| 125 |
|
| 126 | @Override
|
| 127 | public void updateSectionStatus(final SectionPos pos, final boolean sectionEmpty) {
|
| 128 | this.storage.updateSectionStatus(pos.asLong(), sectionEmpty);
|
| 129 | }
|
| 130 |
|
| 131 | @Override
|
| 132 | public void setLightEnabled(final ChunkPos pos, final boolean enable) {
|
| 133 | this.storage.setLightEnabled(SectionPos.getZeroNode(pos.x(), pos.z()), enable);
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | public int runLightUpdates() {
|
| 138 | LongIterator iterator = this.blockNodesToCheck.iterator();
|
| 139 |
|
| 140 | while (iterator.hasNext()) {
|
| 141 | this.checkNode(iterator.nextLong());
|
| 142 | }
|
| 143 |
|
| 144 | this.blockNodesToCheck.clear();
|
| 145 | this.blockNodesToCheck.trim(512);
|
| 146 | int count = 0;
|
| 147 | count += this.propagateDecreases();
|
| 148 | count += this.propagateIncreases();
|
| 149 | this.clearChunkCache();
|
| 150 | this.storage.markNewInconsistencies(this);
|
| 151 | this.storage.swapSectionMap();
|
| 152 | return count;
|
| 153 | }
|
| 154 |
|
| 155 | private int propagateIncreases() {
|
| 156 | int count;
|
| 157 | for (count = 0; !this.increaseQueue.isEmpty(); count++) {
|
| 158 | long fromNode = this.increaseQueue.dequeueLong();
|
| 159 | long increaseData = this.increaseQueue.dequeueLong();
|
| 160 | int fromLevel = this.storage.getStoredLevel(fromNode);
|
| 161 | int fromTargetLevel = LightEngine.QueueEntry.getFromLevel(increaseData);
|
| 162 | if (LightEngine.QueueEntry.isIncreaseFromEmission(increaseData) && fromLevel < fromTargetLevel) {
|
| 163 | this.storage.setStoredLevel(fromNode, fromTargetLevel);
|
| 164 | fromLevel = fromTargetLevel;
|
| 165 | }
|
| 166 |
|
| 167 | if (fromLevel == fromTargetLevel) {
|
| 168 | this.propagateIncrease(fromNode, increaseData, fromLevel);
|
| 169 | }
|
| 170 | }
|
| 171 |
|
| 172 | return count;
|
| 173 | }
|
| 174 |
|
| 175 | private int propagateDecreases() {
|
| 176 | int count;
|
| 177 | for (count = 0; !this.decreaseQueue.isEmpty(); count++) {
|
| 178 | long fromNode = this.decreaseQueue.dequeueLong();
|
| 179 | long decreaseData = this.decreaseQueue.dequeueLong();
|
| 180 | this.propagateDecrease(fromNode, decreaseData);
|
| 181 | }
|
| 182 |
|
| 183 | return count;
|
| 184 | }
|
| 185 |
|
| 186 | protected void enqueueDecrease(final long fromNode, final long decreaseData) {
|
| 187 | this.decreaseQueue.enqueue(fromNode);
|
| 188 | this.decreaseQueue.enqueue(decreaseData);
|
| 189 | }
|
| 190 |
|
| 191 | protected void enqueueIncrease(final long fromNode, final long increaseData) {
|
| 192 | this.increaseQueue.enqueue(fromNode);
|
| 193 | this.increaseQueue.enqueue(increaseData);
|
| 194 | }
|
| 195 |
|
| 196 | @Override
|
| 197 | public boolean hasLightWork() {
|
| 198 | return this.storage.hasInconsistencies() || !this.blockNodesToCheck.isEmpty() || !this.decreaseQueue.isEmpty() || !this.increaseQueue.isEmpty();
|
| 199 | }
|
| 200 |
|
| 201 | @Override
|
| 202 | public @Nullable DataLayer getDataLayerData(final SectionPos pos) {
|
| 203 | return this.storage.getDataLayerData(pos.asLong());
|
| 204 | }
|
| 205 |
|
| 206 | @Override
|
| 207 | public int getLightValue(final BlockPos pos) {
|
| 208 | return this.storage.getLightValue(pos.asLong());
|
| 209 | }
|
| 210 |
|
| 211 | public String getDebugData(final long sectionNode) {
|
| 212 | return this.getDebugSectionType(sectionNode).display();
|
| 213 | }
|
| 214 |
|
| 215 | public LayerLightSectionStorage.SectionType getDebugSectionType(final long sectionNode) {
|
| 216 | return this.storage.getDebugSectionType(sectionNode);
|
| 217 | }
|
| 218 |
|
| 219 | protected abstract void checkNode(long blockNode);
|
| 220 |
|
| 221 | protected abstract void propagateIncrease(long fromNode, long increaseData, int fromLevel);
|
| 222 |
|
| 223 | protected abstract void propagateDecrease(long fromNode, long decreaseData);
|
| 224 |
|
| 225 | public static class QueueEntry {
|
| 226 | private static final int FROM_LEVEL_BITS = 4;
|
| 227 | private static final int DIRECTION_BITS = 6;
|
| 228 | private static final long LEVEL_MASK = 15L;
|
| 229 | private static final long DIRECTIONS_MASK = 1008L;
|
| 230 | private static final long FLAG_FROM_EMPTY_SHAPE = 1024L;
|
| 231 | private static final long FLAG_INCREASE_FROM_EMISSION = 2048L;
|
| 232 |
|
| 233 | public static long decreaseSkipOneDirection(final int oldFromLevel, final Direction skipDirection) {
|
| 234 | long decreaseData = withoutDirection(1008L, skipDirection);
|
| 235 | return withLevel(decreaseData, oldFromLevel);
|
| 236 | }
|
| 237 |
|
| 238 | public static long decreaseAllDirections(final int oldFromLevel) {
|
| 239 | return withLevel(1008L, oldFromLevel);
|
| 240 | }
|
| 241 |
|
| 242 | public static long increaseLightFromEmission(final int newFromLevel, final boolean fromEmptyShape) {
|
| 243 | long increaseData = 1008L;
|
| 244 | increaseData |= 2048L;
|
| 245 | if (fromEmptyShape) {
|
| 246 | increaseData |= 1024L;
|
| 247 | }
|
| 248 |
|
| 249 | return withLevel(increaseData, newFromLevel);
|
| 250 | }
|
| 251 |
|
| 252 | public static long increaseSkipOneDirection(final int newFromLevel, final boolean fromEmptyShape, final Direction skipDirection) {
|
| 253 | long increaseData = withoutDirection(1008L, skipDirection);
|
| 254 | if (fromEmptyShape) {
|
| 255 | increaseData |= 1024L;
|
| 256 | }
|
| 257 |
|
| 258 | return withLevel(increaseData, newFromLevel);
|
| 259 | }
|
| 260 |
|
| 261 | public static long increaseOnlyOneDirection(final int newFromLevel, final boolean fromEmptyShape, final Direction direction) {
|
| 262 | long increaseData = 0L;
|
| 263 | if (fromEmptyShape) {
|
| 264 | increaseData |= 1024L;
|
| 265 | }
|
| 266 |
|
| 267 | increaseData = withDirection(increaseData, direction);
|
| 268 | return withLevel(increaseData, newFromLevel);
|
| 269 | }
|
| 270 |
|
| 271 | public static long increaseSkySourceInDirections(final boolean down, final boolean north, final boolean south, final boolean west, final boolean east) {
|
| 272 | long increaseData = withLevel(0L, 15);
|
| 273 | if (down) {
|
| 274 | increaseData = withDirection(increaseData, Direction.DOWN);
|
| 275 | }
|
| 276 |
|
| 277 | if (north) {
|
| 278 | increaseData = withDirection(increaseData, Direction.NORTH);
|
| 279 | }
|
| 280 |
|
| 281 | if (south) {
|
| 282 | increaseData = withDirection(increaseData, Direction.SOUTH);
|
| 283 | }
|
| 284 |
|
| 285 | if (west) {
|
| 286 | increaseData = withDirection(increaseData, Direction.WEST);
|
| 287 | }
|
| 288 |
|
| 289 | if (east) {
|
| 290 | increaseData = withDirection(increaseData, Direction.EAST);
|
| 291 | }
|
| 292 |
|
| 293 | return increaseData;
|
| 294 | }
|
| 295 |
|
| 296 | public static int getFromLevel(final long entry) {
|
| 297 | return (int)(entry & 15L);
|
| 298 | }
|
| 299 |
|
| 300 | public static boolean isFromEmptyShape(final long entry) {
|
| 301 | return (entry & 1024L) != 0L;
|
| 302 | }
|
| 303 |
|
| 304 | public static boolean isIncreaseFromEmission(final long entry) {
|
| 305 | return (entry & 2048L) != 0L;
|
| 306 | }
|
| 307 |
|
| 308 | public static boolean shouldPropagateInDirection(final long entry, final Direction direction) {
|
| 309 | return (entry & 1L << direction.ordinal() + 4) != 0L;
|
| 310 | }
|
| 311 |
|
| 312 | private static long withLevel(final long entry, final int level) {
|
| 313 | return entry & -16L | level & 15L;
|
| 314 | }
|
| 315 |
|
| 316 | private static long withDirection(final long entry, final Direction direction) {
|
| 317 | return entry | 1L << direction.ordinal() + 4;
|
| 318 | }
|
| 319 |
|
| 320 | private static long withoutDirection(final long entry, final Direction direction) {
|
| 321 | return entry & ~(1L << direction.ordinal() + 4);
|
| 322 | }
|
| 323 | }
|
| 324 | }
|
| 325 |
|