| 1 | package net.minecraft.world.level.lighting;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import java.util.Objects;
|
| 5 | import net.minecraft.core.BlockPos;
|
| 6 | import net.minecraft.core.Direction;
|
| 7 | import net.minecraft.core.SectionPos;
|
| 8 | import net.minecraft.world.level.ChunkPos;
|
| 9 | import net.minecraft.world.level.block.Blocks;
|
| 10 | import net.minecraft.world.level.block.state.BlockState;
|
| 11 | import net.minecraft.world.level.chunk.DataLayer;
|
| 12 | import net.minecraft.world.level.chunk.LightChunk;
|
| 13 | import net.minecraft.world.level.chunk.LightChunkGetter;
|
| 14 | import org.jspecify.annotations.Nullable;
|
| 15 |
|
| 16 | public final class SkyLightEngine extends LightEngine<SkyLightSectionStorage.SkyDataLayerStorageMap, SkyLightSectionStorage> {
|
| 17 | private static final long REMOVE_TOP_SKY_SOURCE_ENTRY = LightEngine.QueueEntry.decreaseAllDirections(15);
|
| 18 | private static final long REMOVE_SKY_SOURCE_ENTRY = LightEngine.QueueEntry.decreaseSkipOneDirection(15, Direction.UP);
|
| 19 | private static final long ADD_SKY_SOURCE_ENTRY = LightEngine.QueueEntry.increaseSkipOneDirection(15, false, Direction.UP);
|
| 20 | private final BlockPos.MutableBlockPos mutablePos = new BlockPos.MutableBlockPos();
|
| 21 | private final ChunkSkyLightSources emptyChunkSources;
|
| 22 |
|
| 23 | public SkyLightEngine(final LightChunkGetter chunkSource) {
|
| 24 | this(chunkSource, new SkyLightSectionStorage(chunkSource));
|
| 25 | }
|
| 26 |
|
| 27 | @VisibleForTesting
|
| 28 | SkyLightEngine(final LightChunkGetter chunkSource, final SkyLightSectionStorage storage) {
|
| 29 | super(chunkSource, storage);
|
| 30 | this.emptyChunkSources = new ChunkSkyLightSources(chunkSource.getLevel());
|
| 31 | }
|
| 32 |
|
| 33 | private static boolean isSourceLevel(final int value) {
|
| 34 | return value == 15;
|
| 35 | }
|
| 36 |
|
| 37 | private int getLowestSourceY(final int x, final int z, final int defaultValue) {
|
| 38 | ChunkSkyLightSources sources = this.getChunkSources(SectionPos.blockToSectionCoord(x), SectionPos.blockToSectionCoord(z));
|
| 39 | return sources == null ? defaultValue : sources.getLowestSourceY(SectionPos.sectionRelative(x), SectionPos.sectionRelative(z));
|
| 40 | }
|
| 41 |
|
| 42 | private @Nullable ChunkSkyLightSources getChunkSources(final int chunkX, final int chunkZ) {
|
| 43 | LightChunk chunk = this.chunkSource.getChunkForLighting(chunkX, chunkZ);
|
| 44 | return chunk != null ? chunk.getSkyLightSources() : null;
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | protected void checkNode(final long blockNode) {
|
| 49 | int x = BlockPos.getX(blockNode);
|
| 50 | int y = BlockPos.getY(blockNode);
|
| 51 | int z = BlockPos.getZ(blockNode);
|
| 52 | long sectionNode = SectionPos.blockToSection(blockNode);
|
| 53 | int lowestSourceY = this.storage.lightOnInSection(sectionNode) ? this.getLowestSourceY(x, z, Integer.MAX_VALUE) : Integer.MAX_VALUE;
|
| 54 | if (lowestSourceY != Integer.MAX_VALUE) {
|
| 55 | this.updateSourcesInColumn(x, z, lowestSourceY);
|
| 56 | }
|
| 57 |
|
| 58 | if (this.storage.storingLightForSection(sectionNode)) {
|
| 59 | boolean isSource = y >= lowestSourceY;
|
| 60 | if (isSource) {
|
| 61 | this.enqueueDecrease(blockNode, REMOVE_SKY_SOURCE_ENTRY);
|
| 62 | this.enqueueIncrease(blockNode, ADD_SKY_SOURCE_ENTRY);
|
| 63 | } else {
|
| 64 | int oldLevel = this.storage.getStoredLevel(blockNode);
|
| 65 | if (oldLevel > 0) {
|
| 66 | this.storage.setStoredLevel(blockNode, 0);
|
| 67 | this.enqueueDecrease(blockNode, LightEngine.QueueEntry.decreaseAllDirections(oldLevel));
|
| 68 | } else {
|
| 69 | this.enqueueDecrease(blockNode, PULL_LIGHT_IN_ENTRY);
|
| 70 | }
|
| 71 | }
|
| 72 | }
|
| 73 | }
|
| 74 |
|
| 75 | private void updateSourcesInColumn(final int x, final int z, final int lowestSourceY) {
|
| 76 | int worldBottomY = SectionPos.sectionToBlockCoord(this.storage.getBottomSectionY());
|
| 77 | this.removeSourcesBelow(x, z, lowestSourceY, worldBottomY);
|
| 78 | this.addSourcesAbove(x, z, lowestSourceY, worldBottomY);
|
| 79 | }
|
| 80 |
|
| 81 | private void removeSourcesBelow(final int x, final int z, final int lowestSourceY, final int worldBottomY) {
|
| 82 | if (lowestSourceY > worldBottomY) {
|
| 83 | int sectionX = SectionPos.blockToSectionCoord(x);
|
| 84 | int sectionZ = SectionPos.blockToSectionCoord(z);
|
| 85 | int startY = lowestSourceY - 1;
|
| 86 |
|
| 87 | for (int sectionY = SectionPos.blockToSectionCoord(startY); this.storage.hasLightDataAtOrBelow(sectionY); sectionY--) {
|
| 88 | if (this.storage.storingLightForSection(SectionPos.asLong(sectionX, sectionY, sectionZ))) {
|
| 89 | int sectionBottomY = SectionPos.sectionToBlockCoord(sectionY);
|
| 90 | int sectionTopY = sectionBottomY + 15;
|
| 91 |
|
| 92 | for (int y = Math.min(sectionTopY, startY); y >= sectionBottomY; y--) {
|
| 93 | long blockNode = BlockPos.asLong(x, y, z);
|
| 94 | if (!isSourceLevel(this.storage.getStoredLevel(blockNode))) {
|
| 95 | return;
|
| 96 | }
|
| 97 |
|
| 98 | this.storage.setStoredLevel(blockNode, 0);
|
| 99 | this.enqueueDecrease(blockNode, y == lowestSourceY - 1 ? REMOVE_TOP_SKY_SOURCE_ENTRY : REMOVE_SKY_SOURCE_ENTRY);
|
| 100 | }
|
| 101 | }
|
| 102 | }
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | private void addSourcesAbove(final int x, final int z, final int lowestSourceY, final int worldBottomY) {
|
| 107 | int sectionX = SectionPos.blockToSectionCoord(x);
|
| 108 | int sectionZ = SectionPos.blockToSectionCoord(z);
|
| 109 | int neighborLowestSourceY = Math.max(
|
| 110 | Math.max(this.getLowestSourceY(x - 1, z, Integer.MIN_VALUE), this.getLowestSourceY(x + 1, z, Integer.MIN_VALUE)),
|
| 111 | Math.max(this.getLowestSourceY(x, z - 1, Integer.MIN_VALUE), this.getLowestSourceY(x, z + 1, Integer.MIN_VALUE))
|
| 112 | );
|
| 113 | int startY = Math.max(lowestSourceY, worldBottomY);
|
| 114 |
|
| 115 | for (long sectionNode = SectionPos.asLong(sectionX, SectionPos.blockToSectionCoord(startY), sectionZ);
|
| 116 | !this.storage.isAboveData(sectionNode);
|
| 117 | sectionNode = SectionPos.offset(sectionNode, Direction.UP)
|
| 118 | ) {
|
| 119 | if (this.storage.storingLightForSection(sectionNode)) {
|
| 120 | int sectionBottomY = SectionPos.sectionToBlockCoord(SectionPos.y(sectionNode));
|
| 121 | int sectionTopY = sectionBottomY + 15;
|
| 122 |
|
| 123 | for (int y = Math.max(sectionBottomY, startY); y <= sectionTopY; y++) {
|
| 124 | long blockNode = BlockPos.asLong(x, y, z);
|
| 125 | if (isSourceLevel(this.storage.getStoredLevel(blockNode))) {
|
| 126 | return;
|
| 127 | }
|
| 128 |
|
| 129 | this.storage.setStoredLevel(blockNode, 15);
|
| 130 | if (y < neighborLowestSourceY || y == lowestSourceY) {
|
| 131 | this.enqueueIncrease(blockNode, ADD_SKY_SOURCE_ENTRY);
|
| 132 | }
|
| 133 | }
|
| 134 | }
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | protected void propagateIncrease(final long fromNode, final long increaseData, final int fromLevel) {
|
| 140 | BlockState fromState = null;
|
| 141 | int emptySectionsBelow = this.countEmptySectionsBelowIfAtBorder(fromNode);
|
| 142 |
|
| 143 | for (Direction propagationDirection : PROPAGATION_DIRECTIONS) {
|
| 144 | if (LightEngine.QueueEntry.shouldPropagateInDirection(increaseData, propagationDirection)) {
|
| 145 | long toNode = BlockPos.offset(fromNode, propagationDirection);
|
| 146 | if (this.storage.storingLightForSection(SectionPos.blockToSection(toNode))) {
|
| 147 | int toLevel = this.storage.getStoredLevel(toNode);
|
| 148 | int maxPossibleNewToLevel = fromLevel - 1;
|
| 149 | if (maxPossibleNewToLevel > toLevel) {
|
| 150 | this.mutablePos.set(toNode);
|
| 151 | BlockState toState = this.getState(this.mutablePos);
|
| 152 | int newToLevel = fromLevel - this.getOpacity(toState);
|
| 153 | if (newToLevel > toLevel) {
|
| 154 | if (fromState == null) {
|
| 155 | fromState = LightEngine.QueueEntry.isFromEmptyShape(increaseData) ? Blocks.AIR.defaultBlockState() : this.getState(this.mutablePos.set(fromNode));
|
| 156 | }
|
| 157 |
|
| 158 | if (!this.shapeOccludes(fromState, toState, propagationDirection)) {
|
| 159 | this.storage.setStoredLevel(toNode, newToLevel);
|
| 160 | if (newToLevel > 1) {
|
| 161 | this.enqueueIncrease(toNode, LightEngine.QueueEntry.increaseSkipOneDirection(newToLevel, isEmptyShape(toState), propagationDirection.getOpposite()));
|
| 162 | }
|
| 163 |
|
| 164 | this.propagateFromEmptySections(toNode, propagationDirection, newToLevel, true, emptySectionsBelow);
|
| 165 | }
|
| 166 | }
|
| 167 | }
|
| 168 | }
|
| 169 | }
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | @Override
|
| 174 | protected void propagateDecrease(final long fromNode, final long decreaseData) {
|
| 175 | int emptySectionsBelow = this.countEmptySectionsBelowIfAtBorder(fromNode);
|
| 176 | int oldFromLevel = LightEngine.QueueEntry.getFromLevel(decreaseData);
|
| 177 |
|
| 178 | for (Direction propagationDirection : PROPAGATION_DIRECTIONS) {
|
| 179 | if (LightEngine.QueueEntry.shouldPropagateInDirection(decreaseData, propagationDirection)) {
|
| 180 | long toNode = BlockPos.offset(fromNode, propagationDirection);
|
| 181 | if (this.storage.storingLightForSection(SectionPos.blockToSection(toNode))) {
|
| 182 | int toLevel = this.storage.getStoredLevel(toNode);
|
| 183 | if (toLevel != 0) {
|
| 184 | if (toLevel <= oldFromLevel - 1) {
|
| 185 | this.storage.setStoredLevel(toNode, 0);
|
| 186 | this.enqueueDecrease(toNode, LightEngine.QueueEntry.decreaseSkipOneDirection(toLevel, propagationDirection.getOpposite()));
|
| 187 | this.propagateFromEmptySections(toNode, propagationDirection, toLevel, false, emptySectionsBelow);
|
| 188 | } else {
|
| 189 | this.enqueueIncrease(toNode, LightEngine.QueueEntry.increaseOnlyOneDirection(toLevel, false, propagationDirection.getOpposite()));
|
| 190 | }
|
| 191 | }
|
| 192 | }
|
| 193 | }
|
| 194 | }
|
| 195 | }
|
| 196 |
|
| 197 | private int countEmptySectionsBelowIfAtBorder(final long blockNode) {
|
| 198 | int y = BlockPos.getY(blockNode);
|
| 199 | int localY = SectionPos.sectionRelative(y);
|
| 200 | if (localY != 0) {
|
| 201 | return 0;
|
| 202 | }
|
| 203 |
|
| 204 | int x = BlockPos.getX(blockNode);
|
| 205 | int z = BlockPos.getZ(blockNode);
|
| 206 | int localX = SectionPos.sectionRelative(x);
|
| 207 | int localZ = SectionPos.sectionRelative(z);
|
| 208 | if (localX != 0 && localX != 15 && localZ != 0 && localZ != 15) {
|
| 209 | return 0;
|
| 210 | }
|
| 211 |
|
| 212 | int sectionX = SectionPos.blockToSectionCoord(x);
|
| 213 | int sectionY = SectionPos.blockToSectionCoord(y);
|
| 214 | int sectionZ = SectionPos.blockToSectionCoord(z);
|
| 215 | int emptySectionsBelow = 0;
|
| 216 |
|
| 217 | while (
|
| 218 | !this.storage.storingLightForSection(SectionPos.asLong(sectionX, sectionY - emptySectionsBelow - 1, sectionZ))
|
| 219 | && this.storage.hasLightDataAtOrBelow(sectionY - emptySectionsBelow - 1)
|
| 220 | ) {
|
| 221 | emptySectionsBelow++;
|
| 222 | }
|
| 223 |
|
| 224 | return emptySectionsBelow;
|
| 225 | }
|
| 226 |
|
| 227 | private void propagateFromEmptySections(
|
| 228 | final long toNode, final Direction propagationDirection, final int toLevel, final boolean increase, final int emptySectionsBelow
|
| 229 | ) {
|
| 230 | if (emptySectionsBelow != 0) {
|
| 231 | int x = BlockPos.getX(toNode);
|
| 232 | int z = BlockPos.getZ(toNode);
|
| 233 | if (crossedSectionEdge(propagationDirection, SectionPos.sectionRelative(x), SectionPos.sectionRelative(z))) {
|
| 234 | int y = BlockPos.getY(toNode);
|
| 235 | int sectionX = SectionPos.blockToSectionCoord(x);
|
| 236 | int sectionZ = SectionPos.blockToSectionCoord(z);
|
| 237 | int sectionY = SectionPos.blockToSectionCoord(y) - 1;
|
| 238 | int bottomSectionY = sectionY - emptySectionsBelow + 1;
|
| 239 |
|
| 240 | while (sectionY >= bottomSectionY) {
|
| 241 | if (!this.storage.storingLightForSection(SectionPos.asLong(sectionX, sectionY, sectionZ))) {
|
| 242 | sectionY--;
|
| 243 | } else {
|
| 244 | int sectionMinY = SectionPos.sectionToBlockCoord(sectionY);
|
| 245 |
|
| 246 | for (int localY = 15; localY >= 0; localY--) {
|
| 247 | long blockNode = BlockPos.asLong(x, sectionMinY + localY, z);
|
| 248 | if (increase) {
|
| 249 | this.storage.setStoredLevel(blockNode, toLevel);
|
| 250 | if (toLevel > 1) {
|
| 251 | this.enqueueIncrease(blockNode, LightEngine.QueueEntry.increaseSkipOneDirection(toLevel, true, propagationDirection.getOpposite()));
|
| 252 | }
|
| 253 | } else {
|
| 254 | this.storage.setStoredLevel(blockNode, 0);
|
| 255 | this.enqueueDecrease(blockNode, LightEngine.QueueEntry.decreaseSkipOneDirection(toLevel, propagationDirection.getOpposite()));
|
| 256 | }
|
| 257 | }
|
| 258 |
|
| 259 | sectionY--;
|
| 260 | }
|
| 261 | }
|
| 262 | }
|
| 263 | }
|
| 264 | }
|
| 265 |
|
| 266 | private static boolean crossedSectionEdge(final Direction propagationDirection, final int x, final int z) {
|
| 267 | return switch (propagationDirection) {
|
| 268 | case NORTH -> z == 15;
|
| 269 | case SOUTH -> z == 0;
|
| 270 | case WEST -> x == 15;
|
| 271 | case EAST -> x == 0;
|
| 272 | default -> false;
|
| 273 | };
|
| 274 | }
|
| 275 |
|
| 276 | @Override
|
| 277 | public void setLightEnabled(final ChunkPos pos, final boolean enable) {
|
| 278 | super.setLightEnabled(pos, enable);
|
| 279 | if (enable) {
|
| 280 | ChunkSkyLightSources sources = Objects.requireNonNullElse(this.getChunkSources(pos.x(), pos.z()), this.emptyChunkSources);
|
| 281 | int highestNonSourceY = sources.getHighestLowestSourceY() - 1;
|
| 282 | int lowestFullySourceSectionY = SectionPos.blockToSectionCoord(highestNonSourceY) + 1;
|
| 283 | long zeroNode = SectionPos.getZeroNode(pos.x(), pos.z());
|
| 284 | int topSectionY = this.storage.getTopSectionY(zeroNode);
|
| 285 | int bottomSectionY = Math.max(this.storage.getBottomSectionY(), lowestFullySourceSectionY);
|
| 286 |
|
| 287 | for (int sectionY = topSectionY - 1; sectionY >= bottomSectionY; sectionY--) {
|
| 288 | DataLayer dataLayer = this.storage.getDataLayerToWrite(SectionPos.asLong(pos.x(), sectionY, pos.z()));
|
| 289 | if (dataLayer != null && dataLayer.isEmpty()) {
|
| 290 | dataLayer.fill(15);
|
| 291 | }
|
| 292 | }
|
| 293 | }
|
| 294 | }
|
| 295 |
|
| 296 | @Override
|
| 297 | public void propagateLightSources(final ChunkPos pos) {
|
| 298 | long zeroNode = SectionPos.getZeroNode(pos.x(), pos.z());
|
| 299 | this.storage.setLightEnabled(zeroNode, true);
|
| 300 | ChunkSkyLightSources sources = Objects.requireNonNullElse(this.getChunkSources(pos.x(), pos.z()), this.emptyChunkSources);
|
| 301 | ChunkSkyLightSources northSources = Objects.requireNonNullElse(this.getChunkSources(pos.x(), pos.z() - 1), this.emptyChunkSources);
|
| 302 | ChunkSkyLightSources southSources = Objects.requireNonNullElse(this.getChunkSources(pos.x(), pos.z() + 1), this.emptyChunkSources);
|
| 303 | ChunkSkyLightSources westSources = Objects.requireNonNullElse(this.getChunkSources(pos.x() - 1, pos.z()), this.emptyChunkSources);
|
| 304 | ChunkSkyLightSources eastSources = Objects.requireNonNullElse(this.getChunkSources(pos.x() + 1, pos.z()), this.emptyChunkSources);
|
| 305 | int topSectionY = this.storage.getTopSectionY(zeroNode);
|
| 306 | int bottomSectionY = this.storage.getBottomSectionY();
|
| 307 | int sectionMinX = SectionPos.sectionToBlockCoord(pos.x());
|
| 308 | int sectionMinZ = SectionPos.sectionToBlockCoord(pos.z());
|
| 309 |
|
| 310 | for (int sectionY = topSectionY - 1; sectionY >= bottomSectionY; sectionY--) {
|
| 311 | long sectionNode = SectionPos.asLong(pos.x(), sectionY, pos.z());
|
| 312 | DataLayer dataLayer = this.storage.getDataLayerToWrite(sectionNode);
|
| 313 | if (dataLayer != null) {
|
| 314 | int sectionMinY = SectionPos.sectionToBlockCoord(sectionY);
|
| 315 | int sectionMaxY = sectionMinY + 15;
|
| 316 | boolean sourcesBelow = false;
|
| 317 |
|
| 318 | for (int z = 0; z < 16; z++) {
|
| 319 | for (int x = 0; x < 16; x++) {
|
| 320 | int lowestSourceY = sources.getLowestSourceY(x, z);
|
| 321 | if (lowestSourceY <= sectionMaxY) {
|
| 322 | int northLowestSourceY = z == 0 ? northSources.getLowestSourceY(x, 15) : sources.getLowestSourceY(x, z - 1);
|
| 323 | int southLowestSourceY = z == 15 ? southSources.getLowestSourceY(x, 0) : sources.getLowestSourceY(x, z + 1);
|
| 324 | int westLowestSourceY = x == 0 ? westSources.getLowestSourceY(15, z) : sources.getLowestSourceY(x - 1, z);
|
| 325 | int eastLowestSourceY = x == 15 ? eastSources.getLowestSourceY(0, z) : sources.getLowestSourceY(x + 1, z);
|
| 326 | int neighborLowestSourceY = Math.max(Math.max(northLowestSourceY, southLowestSourceY), Math.max(westLowestSourceY, eastLowestSourceY));
|
| 327 |
|
| 328 | for (int y = sectionMaxY; y >= Math.max(sectionMinY, lowestSourceY); y--) {
|
| 329 | dataLayer.set(x, SectionPos.sectionRelative(y), z, 15);
|
| 330 | if (y == lowestSourceY || y < neighborLowestSourceY) {
|
| 331 | long blockNode = BlockPos.asLong(sectionMinX + x, y, sectionMinZ + z);
|
| 332 | this.enqueueIncrease(
|
| 333 | blockNode,
|
| 334 | LightEngine.QueueEntry.increaseSkySourceInDirections(
|
| 335 | y == lowestSourceY, y < northLowestSourceY, y < southLowestSourceY, y < westLowestSourceY, y < eastLowestSourceY
|
| 336 | )
|
| 337 | );
|
| 338 | }
|
| 339 | }
|
| 340 |
|
| 341 | if (lowestSourceY < sectionMinY) {
|
| 342 | sourcesBelow = true;
|
| 343 | }
|
| 344 | }
|
| 345 | }
|
| 346 | }
|
| 347 |
|
| 348 | if (!sourcesBelow) {
|
| 349 | break;
|
| 350 | }
|
| 351 | }
|
| 352 | }
|
| 353 | }
|
| 354 | }
|
| 355 |
|