| 1 | package net.minecraft.world.level.lighting;
|
| 2 |
|
| 3 | import net.minecraft.core.BlockPos;
|
| 4 | import net.minecraft.core.Direction;
|
| 5 | import net.minecraft.core.SectionPos;
|
| 6 | import net.minecraft.util.BitStorage;
|
| 7 | import net.minecraft.util.Mth;
|
| 8 | import net.minecraft.util.SimpleBitStorage;
|
| 9 | import net.minecraft.world.level.BlockGetter;
|
| 10 | import net.minecraft.world.level.LevelHeightAccessor;
|
| 11 | import net.minecraft.world.level.block.Blocks;
|
| 12 | import net.minecraft.world.level.block.state.BlockState;
|
| 13 | import net.minecraft.world.level.chunk.ChunkAccess;
|
| 14 | import net.minecraft.world.level.chunk.LevelChunkSection;
|
| 15 | import net.minecraft.world.phys.shapes.Shapes;
|
| 16 | import net.minecraft.world.phys.shapes.VoxelShape;
|
| 17 |
|
| 18 | public class ChunkSkyLightSources {
|
| 19 | private static final int SIZE = 16;
|
| 20 | public static final int NEGATIVE_INFINITY = Integer.MIN_VALUE;
|
| 21 | private final int minY;
|
| 22 | private final BitStorage heightmap;
|
| 23 | private final BlockPos.MutableBlockPos mutablePos1 = new BlockPos.MutableBlockPos();
|
| 24 | private final BlockPos.MutableBlockPos mutablePos2 = new BlockPos.MutableBlockPos();
|
| 25 |
|
| 26 | public ChunkSkyLightSources(final LevelHeightAccessor level) {
|
| 27 | this.minY = level.getMinY() - 1;
|
| 28 | int maxY = level.getMaxY() + 1;
|
| 29 | int bits = Mth.ceillog2(maxY - this.minY + 1);
|
| 30 | this.heightmap = new SimpleBitStorage(bits, 256);
|
| 31 | }
|
| 32 |
|
| 33 | public void fillFrom(final ChunkAccess chunk) {
|
| 34 | int maxSectionIndex = chunk.getHighestFilledSectionIndex();
|
| 35 | if (maxSectionIndex == -1) {
|
| 36 | this.fill(this.minY);
|
| 37 | } else {
|
| 38 | for (int z = 0; z < 16; z++) {
|
| 39 | for (int x = 0; x < 16; x++) {
|
| 40 | int initialEdgeY = Math.max(this.findLowestSourceY(chunk, maxSectionIndex, x, z), this.minY);
|
| 41 | this.set(index(x, z), initialEdgeY);
|
| 42 | }
|
| 43 | }
|
| 44 | }
|
| 45 | }
|
| 46 |
|
| 47 | private int findLowestSourceY(final ChunkAccess chunk, final int topSectionIndex, final int x, final int z) {
|
| 48 | int topY = SectionPos.sectionToBlockCoord(chunk.getSectionYFromSectionIndex(topSectionIndex) + 1);
|
| 49 | BlockPos.MutableBlockPos topPos = this.mutablePos1.set(x, topY, z);
|
| 50 | BlockPos.MutableBlockPos bottomPos = this.mutablePos2.setWithOffset(topPos, Direction.DOWN);
|
| 51 | BlockState topState = Blocks.AIR.defaultBlockState();
|
| 52 |
|
| 53 | for (int sectionIndex = topSectionIndex; sectionIndex >= 0; sectionIndex--) {
|
| 54 | LevelChunkSection section = chunk.getSection(sectionIndex);
|
| 55 | if (section.hasOnlyAir()) {
|
| 56 | topState = Blocks.AIR.defaultBlockState();
|
| 57 | int sectionY = chunk.getSectionYFromSectionIndex(sectionIndex);
|
| 58 | topPos.setY(SectionPos.sectionToBlockCoord(sectionY));
|
| 59 | bottomPos.setY(topPos.getY() - 1);
|
| 60 | } else {
|
| 61 | for (int y = 15; y >= 0; y--) {
|
| 62 | BlockState bottomState = section.getBlockState(x, y, z);
|
| 63 | if (isEdgeOccluded(topState, bottomState)) {
|
| 64 | return topPos.getY();
|
| 65 | }
|
| 66 |
|
| 67 | topState = bottomState;
|
| 68 | topPos.set(bottomPos);
|
| 69 | bottomPos.move(Direction.DOWN);
|
| 70 | }
|
| 71 | }
|
| 72 | }
|
| 73 |
|
| 74 | return this.minY;
|
| 75 | }
|
| 76 |
|
| 77 | public boolean update(final BlockGetter level, final int x, final int y, final int z) {
|
| 78 | int upperEdgeY = y + 1;
|
| 79 | int index = index(x, z);
|
| 80 | int currentLowestSourceY = this.get(index);
|
| 81 | if (upperEdgeY < currentLowestSourceY) {
|
| 82 | return false;
|
| 83 | }
|
| 84 |
|
| 85 | BlockPos topPos = this.mutablePos1.set(x, y + 1, z);
|
| 86 | BlockState topState = level.getBlockState(topPos);
|
| 87 | BlockPos middlePos = this.mutablePos2.set(x, y, z);
|
| 88 | BlockState middleState = level.getBlockState(middlePos);
|
| 89 | if (this.updateEdge(level, index, currentLowestSourceY, topPos, topState, middlePos, middleState)) {
|
| 90 | return true;
|
| 91 | }
|
| 92 |
|
| 93 | BlockPos bottomPos = this.mutablePos1.set(x, y - 1, z);
|
| 94 | BlockState bottomState = level.getBlockState(bottomPos);
|
| 95 | return this.updateEdge(level, index, currentLowestSourceY, middlePos, middleState, bottomPos, bottomState);
|
| 96 | }
|
| 97 |
|
| 98 | private boolean updateEdge(
|
| 99 | final BlockGetter level,
|
| 100 | final int index,
|
| 101 | final int oldTopEdgeY,
|
| 102 | final BlockPos topPos,
|
| 103 | final BlockState topState,
|
| 104 | final BlockPos bottomPos,
|
| 105 | final BlockState bottomState
|
| 106 | ) {
|
| 107 | int checkedEdgeY = topPos.getY();
|
| 108 | if (isEdgeOccluded(topState, bottomState)) {
|
| 109 | if (checkedEdgeY > oldTopEdgeY) {
|
| 110 | this.set(index, checkedEdgeY);
|
| 111 | return true;
|
| 112 | }
|
| 113 | } else if (checkedEdgeY == oldTopEdgeY) {
|
| 114 | this.set(index, this.findLowestSourceBelow(level, bottomPos, bottomState));
|
| 115 | return true;
|
| 116 | }
|
| 117 |
|
| 118 | return false;
|
| 119 | }
|
| 120 |
|
| 121 | private int findLowestSourceBelow(final BlockGetter level, final BlockPos startPos, final BlockState startState) {
|
| 122 | BlockPos.MutableBlockPos topPos = this.mutablePos1.set(startPos);
|
| 123 | BlockPos.MutableBlockPos bottomPos = this.mutablePos2.setWithOffset(startPos, Direction.DOWN);
|
| 124 | BlockState topState = startState;
|
| 125 |
|
| 126 | while (bottomPos.getY() >= this.minY) {
|
| 127 | BlockState bottomState = level.getBlockState(bottomPos);
|
| 128 | if (isEdgeOccluded(topState, bottomState)) {
|
| 129 | return topPos.getY();
|
| 130 | }
|
| 131 |
|
| 132 | topState = bottomState;
|
| 133 | topPos.set(bottomPos);
|
| 134 | bottomPos.move(Direction.DOWN);
|
| 135 | }
|
| 136 |
|
| 137 | return this.minY;
|
| 138 | }
|
| 139 |
|
| 140 | private static boolean isEdgeOccluded(final BlockState topState, final BlockState bottomState) {
|
| 141 | if (bottomState.getLightDampening() != 0) {
|
| 142 | return true;
|
| 143 | }
|
| 144 |
|
| 145 | VoxelShape topShape = LightEngine.getOcclusionShape(topState, Direction.DOWN);
|
| 146 | VoxelShape bottomShape = LightEngine.getOcclusionShape(bottomState, Direction.UP);
|
| 147 | return Shapes.faceShapeOccludes(topShape, bottomShape);
|
| 148 | }
|
| 149 |
|
| 150 | public int getLowestSourceY(final int x, final int z) {
|
| 151 | int value = this.get(index(x, z));
|
| 152 | return this.extendSourcesBelowWorld(value);
|
| 153 | }
|
| 154 |
|
| 155 | public int getHighestLowestSourceY() {
|
| 156 | int maxValue = Integer.MIN_VALUE;
|
| 157 |
|
| 158 | for (int i = 0; i < this.heightmap.getSize(); i++) {
|
| 159 | int value = this.heightmap.get(i);
|
| 160 | if (value > maxValue) {
|
| 161 | maxValue = value;
|
| 162 | }
|
| 163 | }
|
| 164 |
|
| 165 | return this.extendSourcesBelowWorld(maxValue + this.minY);
|
| 166 | }
|
| 167 |
|
| 168 | private void fill(final int lowestSourceY) {
|
| 169 | int value = lowestSourceY - this.minY;
|
| 170 |
|
| 171 | for (int i = 0; i < this.heightmap.getSize(); i++) {
|
| 172 | this.heightmap.set(i, value);
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | private void set(final int index, final int value) {
|
| 177 | this.heightmap.set(index, value - this.minY);
|
| 178 | }
|
| 179 |
|
| 180 | private int get(final int index) {
|
| 181 | return this.heightmap.get(index) + this.minY;
|
| 182 | }
|
| 183 |
|
| 184 | private int extendSourcesBelowWorld(final int value) {
|
| 185 | return value == this.minY ? Integer.MIN_VALUE : value;
|
| 186 | }
|
| 187 |
|
| 188 | private static int index(final int x, final int z) {
|
| 189 | return x + z * 16;
|
| 190 | }
|
| 191 | }
|
| 192 |
|