| 1 | package net.minecraft.world.level.lighting;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.longs.Long2ByteMap;
|
| 4 | import it.unimi.dsi.fastutil.longs.Long2ByteOpenHashMap;
|
| 5 | import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
|
| 6 | import it.unimi.dsi.fastutil.longs.Long2ObjectMaps;
|
| 7 | import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
|
| 8 | import it.unimi.dsi.fastutil.longs.LongIterator;
|
| 9 | import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
|
| 10 | import it.unimi.dsi.fastutil.longs.LongSet;
|
| 11 | import it.unimi.dsi.fastutil.longs.Long2ObjectMap.Entry;
|
| 12 | import it.unimi.dsi.fastutil.objects.ObjectIterator;
|
| 13 | import net.minecraft.core.BlockPos;
|
| 14 | import net.minecraft.core.SectionPos;
|
| 15 | import net.minecraft.world.level.LightLayer;
|
| 16 | import net.minecraft.world.level.chunk.DataLayer;
|
| 17 | import net.minecraft.world.level.chunk.LightChunkGetter;
|
| 18 | import org.jspecify.annotations.Nullable;
|
| 19 |
|
| 20 | public abstract class LayerLightSectionStorage<M extends DataLayerStorageMap<M>> {
|
| 21 | private final LightLayer layer;
|
| 22 | protected final LightChunkGetter chunkSource;
|
| 23 | protected final Long2ByteMap sectionStates = new Long2ByteOpenHashMap();
|
| 24 | private final LongSet columnsWithSources = new LongOpenHashSet();
|
| 25 | protected volatile M visibleSectionData;
|
| 26 | protected final M updatingSectionData;
|
| 27 | protected final LongSet changedSections = new LongOpenHashSet();
|
| 28 | protected final LongSet sectionsAffectedByLightUpdates = new LongOpenHashSet();
|
| 29 | protected final Long2ObjectMap<DataLayer> queuedSections = Long2ObjectMaps.synchronize(new Long2ObjectOpenHashMap<>());
|
| 30 | private final LongSet columnsToRetainQueuedDataFor = new LongOpenHashSet();
|
| 31 | private final LongSet toRemove = new LongOpenHashSet();
|
| 32 | protected volatile boolean hasInconsistencies;
|
| 33 |
|
| 34 | protected LayerLightSectionStorage(final LightLayer layer, final LightChunkGetter chunkSource, final M initialMap) {
|
| 35 | this.layer = layer;
|
| 36 | this.chunkSource = chunkSource;
|
| 37 | this.updatingSectionData = initialMap;
|
| 38 | this.visibleSectionData = initialMap.copy();
|
| 39 | this.visibleSectionData.disableCache();
|
| 40 | this.sectionStates.defaultReturnValue((byte)0);
|
| 41 | }
|
| 42 |
|
| 43 | protected boolean storingLightForSection(final long sectionNode) {
|
| 44 | return this.getDataLayer(sectionNode, true) != null;
|
| 45 | }
|
| 46 |
|
| 47 | protected @Nullable DataLayer getDataLayer(final long sectionNode, final boolean updating) {
|
| 48 | return this.getDataLayer(updating ? this.updatingSectionData : this.visibleSectionData, sectionNode);
|
| 49 | }
|
| 50 |
|
| 51 | protected @Nullable DataLayer getDataLayer(final M sections, final long sectionNode) {
|
| 52 | return sections.getLayer(sectionNode);
|
| 53 | }
|
| 54 |
|
| 55 | protected @Nullable DataLayer getDataLayerToWrite(final long sectionNode) {
|
| 56 | DataLayer dataLayer = this.updatingSectionData.getLayer(sectionNode);
|
| 57 | if (dataLayer == null) {
|
| 58 | return null;
|
| 59 | }
|
| 60 |
|
| 61 | if (this.changedSections.add(sectionNode)) {
|
| 62 | dataLayer = dataLayer.copy();
|
| 63 | this.updatingSectionData.setLayer(sectionNode, dataLayer);
|
| 64 | this.updatingSectionData.clearCache();
|
| 65 | }
|
| 66 |
|
| 67 | return dataLayer;
|
| 68 | }
|
| 69 |
|
| 70 | public @Nullable DataLayer getDataLayerData(final long sectionNode) {
|
| 71 | DataLayer layer = this.queuedSections.get(sectionNode);
|
| 72 | return layer != null ? layer : this.getDataLayer(sectionNode, false);
|
| 73 | }
|
| 74 |
|
| 75 | protected abstract int getLightValue(final long blockNode);
|
| 76 |
|
| 77 | protected int getStoredLevel(final long blockNode) {
|
| 78 | long sectionNode = SectionPos.blockToSection(blockNode);
|
| 79 | DataLayer layer = this.getDataLayer(sectionNode, true);
|
| 80 | return layer.get(
|
| 81 | SectionPos.sectionRelative(BlockPos.getX(blockNode)),
|
| 82 | SectionPos.sectionRelative(BlockPos.getY(blockNode)),
|
| 83 | SectionPos.sectionRelative(BlockPos.getZ(blockNode))
|
| 84 | );
|
| 85 | }
|
| 86 |
|
| 87 | protected void setStoredLevel(final long blockNode, final int level) {
|
| 88 | long sectionNode = SectionPos.blockToSection(blockNode);
|
| 89 | DataLayer layer;
|
| 90 | if (this.changedSections.add(sectionNode)) {
|
| 91 | layer = this.updatingSectionData.copyDataLayer(sectionNode);
|
| 92 | } else {
|
| 93 | layer = this.getDataLayer(sectionNode, true);
|
| 94 | }
|
| 95 |
|
| 96 | layer.set(
|
| 97 | SectionPos.sectionRelative(BlockPos.getX(blockNode)),
|
| 98 | SectionPos.sectionRelative(BlockPos.getY(blockNode)),
|
| 99 | SectionPos.sectionRelative(BlockPos.getZ(blockNode)),
|
| 100 | level
|
| 101 | );
|
| 102 | SectionPos.aroundAndAtBlockPos(blockNode, this.sectionsAffectedByLightUpdates::add);
|
| 103 | }
|
| 104 |
|
| 105 | protected void markSectionAndNeighborsAsAffected(final long sectionNode) {
|
| 106 | int x = SectionPos.x(sectionNode);
|
| 107 | int y = SectionPos.y(sectionNode);
|
| 108 | int z = SectionPos.z(sectionNode);
|
| 109 |
|
| 110 | for (int offsetZ = -1; offsetZ <= 1; offsetZ++) {
|
| 111 | for (int offsetX = -1; offsetX <= 1; offsetX++) {
|
| 112 | for (int offsetY = -1; offsetY <= 1; offsetY++) {
|
| 113 | this.sectionsAffectedByLightUpdates.add(SectionPos.asLong(x + offsetX, y + offsetY, z + offsetZ));
|
| 114 | }
|
| 115 | }
|
| 116 | }
|
| 117 | }
|
| 118 |
|
| 119 | protected DataLayer createDataLayer(final long sectionNode) {
|
| 120 | DataLayer queuedLayer = this.queuedSections.get(sectionNode);
|
| 121 | return queuedLayer != null ? queuedLayer : new DataLayer();
|
| 122 | }
|
| 123 |
|
| 124 | protected boolean hasInconsistencies() {
|
| 125 | return this.hasInconsistencies;
|
| 126 | }
|
| 127 |
|
| 128 | protected void markNewInconsistencies(final LightEngine<M, ?> engine) {
|
| 129 | if (this.hasInconsistencies) {
|
| 130 | this.hasInconsistencies = false;
|
| 131 |
|
| 132 | for (long node : this.toRemove) {
|
| 133 | DataLayer queued = this.queuedSections.remove(node);
|
| 134 | DataLayer stored = this.updatingSectionData.removeLayer(node);
|
| 135 | if (this.columnsToRetainQueuedDataFor.contains(SectionPos.getZeroNode(node))) {
|
| 136 | if (queued != null) {
|
| 137 | this.queuedSections.put(node, queued);
|
| 138 | } else if (stored != null) {
|
| 139 | this.queuedSections.put(node, stored);
|
| 140 | }
|
| 141 | }
|
| 142 | }
|
| 143 |
|
| 144 | this.updatingSectionData.clearCache();
|
| 145 |
|
| 146 | for (long node : this.toRemove) {
|
| 147 | this.onNodeRemoved(node);
|
| 148 | this.changedSections.add(node);
|
| 149 | }
|
| 150 |
|
| 151 | this.toRemove.clear();
|
| 152 | ObjectIterator<Entry<DataLayer>> iterator = Long2ObjectMaps.fastIterator(this.queuedSections);
|
| 153 |
|
| 154 | while (iterator.hasNext()) {
|
| 155 | Entry<DataLayer> entry = iterator.next();
|
| 156 | long sectionNode = entry.getLongKey();
|
| 157 | if (this.storingLightForSection(sectionNode)) {
|
| 158 | DataLayer data = entry.getValue();
|
| 159 | if (this.updatingSectionData.getLayer(sectionNode) != data) {
|
| 160 | this.updatingSectionData.setLayer(sectionNode, data);
|
| 161 | this.changedSections.add(sectionNode);
|
| 162 | }
|
| 163 |
|
| 164 | iterator.remove();
|
| 165 | }
|
| 166 | }
|
| 167 |
|
| 168 | this.updatingSectionData.clearCache();
|
| 169 | }
|
| 170 | }
|
| 171 |
|
| 172 | protected void onNodeAdded(final long sectionNode) {
|
| 173 | }
|
| 174 |
|
| 175 | protected void onNodeRemoved(final long sectionNode) {
|
| 176 | }
|
| 177 |
|
| 178 | protected void setLightEnabled(final long zeroNode, final boolean enable) {
|
| 179 | if (enable) {
|
| 180 | this.columnsWithSources.add(zeroNode);
|
| 181 | } else {
|
| 182 | this.columnsWithSources.remove(zeroNode);
|
| 183 | }
|
| 184 | }
|
| 185 |
|
| 186 | protected boolean lightOnInSection(final long sectionNode) {
|
| 187 | long zeroNode = SectionPos.getZeroNode(sectionNode);
|
| 188 | return this.columnsWithSources.contains(zeroNode);
|
| 189 | }
|
| 190 |
|
| 191 | protected boolean lightOnInColumn(final long sectionZeroNode) {
|
| 192 | return this.columnsWithSources.contains(sectionZeroNode);
|
| 193 | }
|
| 194 |
|
| 195 | public void retainData(final long zeroNode, final boolean retain) {
|
| 196 | if (retain) {
|
| 197 | this.columnsToRetainQueuedDataFor.add(zeroNode);
|
| 198 | } else {
|
| 199 | this.columnsToRetainQueuedDataFor.remove(zeroNode);
|
| 200 | }
|
| 201 | }
|
| 202 |
|
| 203 | protected void queueSectionData(final long sectionNode, final @Nullable DataLayer data) {
|
| 204 | if (data != null) {
|
| 205 | this.queuedSections.put(sectionNode, data);
|
| 206 | this.hasInconsistencies = true;
|
| 207 | } else {
|
| 208 | this.queuedSections.remove(sectionNode);
|
| 209 | }
|
| 210 | }
|
| 211 |
|
| 212 | protected void updateSectionStatus(final long sectionNode, final boolean sectionEmpty) {
|
| 213 | byte state = this.sectionStates.get(sectionNode);
|
| 214 | byte newState = LayerLightSectionStorage.SectionState.hasData(state, !sectionEmpty);
|
| 215 | if (state != newState) {
|
| 216 | this.putSectionState(sectionNode, newState);
|
| 217 | int neighborIncrement = sectionEmpty ? -1 : 1;
|
| 218 |
|
| 219 | for (int offsetX = -1; offsetX <= 1; offsetX++) {
|
| 220 | for (int offsetY = -1; offsetY <= 1; offsetY++) {
|
| 221 | for (int offsetZ = -1; offsetZ <= 1; offsetZ++) {
|
| 222 | if (offsetX != 0 || offsetY != 0 || offsetZ != 0) {
|
| 223 | long neighborNode = SectionPos.offset(sectionNode, offsetX, offsetY, offsetZ);
|
| 224 | byte neighborState = this.sectionStates.get(neighborNode);
|
| 225 | this.putSectionState(
|
| 226 | neighborNode,
|
| 227 | LayerLightSectionStorage.SectionState.neighborCount(
|
| 228 | neighborState, LayerLightSectionStorage.SectionState.neighborCount(neighborState) + neighborIncrement
|
| 229 | )
|
| 230 | );
|
| 231 | }
|
| 232 | }
|
| 233 | }
|
| 234 | }
|
| 235 | }
|
| 236 | }
|
| 237 |
|
| 238 | protected void putSectionState(final long sectionNode, final byte state) {
|
| 239 | if (state != 0) {
|
| 240 | if (this.sectionStates.put(sectionNode, state) == 0) {
|
| 241 | this.initializeSection(sectionNode);
|
| 242 | }
|
| 243 | } else if (this.sectionStates.remove(sectionNode) != 0) {
|
| 244 | this.removeSection(sectionNode);
|
| 245 | }
|
| 246 | }
|
| 247 |
|
| 248 | private void initializeSection(final long sectionNode) {
|
| 249 | if (!this.toRemove.remove(sectionNode)) {
|
| 250 | this.updatingSectionData.setLayer(sectionNode, this.createDataLayer(sectionNode));
|
| 251 | this.changedSections.add(sectionNode);
|
| 252 | this.onNodeAdded(sectionNode);
|
| 253 | this.markSectionAndNeighborsAsAffected(sectionNode);
|
| 254 | this.hasInconsistencies = true;
|
| 255 | }
|
| 256 | }
|
| 257 |
|
| 258 | private void removeSection(final long sectionNode) {
|
| 259 | this.toRemove.add(sectionNode);
|
| 260 | this.hasInconsistencies = true;
|
| 261 | }
|
| 262 |
|
| 263 | protected void swapSectionMap() {
|
| 264 | if (!this.changedSections.isEmpty()) {
|
| 265 | M copy = this.updatingSectionData.copy();
|
| 266 | copy.disableCache();
|
| 267 | this.visibleSectionData = copy;
|
| 268 | this.changedSections.clear();
|
| 269 | }
|
| 270 |
|
| 271 | if (!this.sectionsAffectedByLightUpdates.isEmpty()) {
|
| 272 | LongIterator iterator = this.sectionsAffectedByLightUpdates.iterator();
|
| 273 |
|
| 274 | while (iterator.hasNext()) {
|
| 275 | long sectionNode = iterator.nextLong();
|
| 276 | this.chunkSource.onLightUpdate(this.layer, SectionPos.of(sectionNode));
|
| 277 | }
|
| 278 |
|
| 279 | this.sectionsAffectedByLightUpdates.clear();
|
| 280 | }
|
| 281 | }
|
| 282 |
|
| 283 | public LayerLightSectionStorage.SectionType getDebugSectionType(final long sectionNode) {
|
| 284 | return LayerLightSectionStorage.SectionState.type(this.sectionStates.get(sectionNode));
|
| 285 | }
|
| 286 |
|
| 287 | protected static class SectionState {
|
| 288 | public static final byte EMPTY = 0;
|
| 289 | private static final int MIN_NEIGHBORS = 0;
|
| 290 | private static final int MAX_NEIGHBORS = 26;
|
| 291 | private static final byte HAS_DATA_BIT = 32;
|
| 292 | private static final byte NEIGHBOR_COUNT_BITS = 31;
|
| 293 |
|
| 294 | public static byte hasData(final byte state, final boolean hasData) {
|
| 295 | return (byte)(hasData ? state | 32 : state & -33);
|
| 296 | }
|
| 297 |
|
| 298 | public static byte neighborCount(final byte state, final int neighborCount) {
|
| 299 | if (neighborCount >= 0 && neighborCount <= 26) {
|
| 300 | return (byte)(state & -32 | neighborCount & 31);
|
| 301 | } else {
|
| 302 | throw new IllegalArgumentException("Neighbor count was not within range [0; 26]");
|
| 303 | }
|
| 304 | }
|
| 305 |
|
| 306 | public static boolean hasData(final byte state) {
|
| 307 | return (state & 32) != 0;
|
| 308 | }
|
| 309 |
|
| 310 | public static int neighborCount(final byte state) {
|
| 311 | return state & 31;
|
| 312 | }
|
| 313 |
|
| 314 | public static LayerLightSectionStorage.SectionType type(final byte state) {
|
| 315 | if (state == 0) {
|
| 316 | return LayerLightSectionStorage.SectionType.EMPTY;
|
| 317 | } else {
|
| 318 | return hasData(state) ? LayerLightSectionStorage.SectionType.LIGHT_AND_DATA : LayerLightSectionStorage.SectionType.LIGHT_ONLY;
|
| 319 | }
|
| 320 | }
|
| 321 | }
|
| 322 |
|
| 323 | public enum SectionType {
|
| 324 | EMPTY("2"),
|
| 325 | LIGHT_ONLY("1"),
|
| 326 | LIGHT_AND_DATA("0");
|
| 327 |
|
| 328 | private final String display;
|
| 329 |
|
| 330 | SectionType(final String display) {
|
| 331 | this.display = display;
|
| 332 | }
|
| 333 |
|
| 334 | public String display() {
|
| 335 | return this.display;
|
| 336 | }
|
| 337 | }
|
| 338 | }
|
| 339 |
|