| 1 | package net.minecraft.world.entity.ai.village.poi;
|
| 2 |
|
| 3 | import com.mojang.datafixers.DataFixer;
|
| 4 | import com.mojang.datafixers.util.Pair;
|
| 5 | import it.unimi.dsi.fastutil.longs.Long2ByteMap;
|
| 6 | import it.unimi.dsi.fastutil.longs.Long2ByteOpenHashMap;
|
| 7 | import it.unimi.dsi.fastutil.longs.LongOpenHashSet;
|
| 8 | import it.unimi.dsi.fastutil.longs.LongSet;
|
| 9 | import java.nio.file.Path;
|
| 10 | import java.util.Comparator;
|
| 11 | import java.util.List;
|
| 12 | import java.util.Optional;
|
| 13 | import java.util.function.BiConsumer;
|
| 14 | import java.util.function.BiPredicate;
|
| 15 | import java.util.function.BooleanSupplier;
|
| 16 | import java.util.function.Predicate;
|
| 17 | import java.util.stream.IntStream;
|
| 18 | import java.util.stream.Stream;
|
| 19 | import net.minecraft.core.BlockPos;
|
| 20 | import net.minecraft.core.Holder;
|
| 21 | import net.minecraft.core.RegistryAccess;
|
| 22 | import net.minecraft.core.SectionPos;
|
| 23 | import net.minecraft.resources.ResourceKey;
|
| 24 | import net.minecraft.server.level.SectionTracker;
|
| 25 | import net.minecraft.tags.PoiTypeTags;
|
| 26 | import net.minecraft.util.RandomSource;
|
| 27 | import net.minecraft.util.Util;
|
| 28 | import net.minecraft.util.VisibleForDebug;
|
| 29 | import net.minecraft.util.datafix.DataFixTypes;
|
| 30 | import net.minecraft.util.debug.DebugPoiInfo;
|
| 31 | import net.minecraft.world.level.ChunkPos;
|
| 32 | import net.minecraft.world.level.LevelHeightAccessor;
|
| 33 | import net.minecraft.world.level.LevelReader;
|
| 34 | import net.minecraft.world.level.block.state.BlockState;
|
| 35 | import net.minecraft.world.level.chunk.LevelChunkSection;
|
| 36 | import net.minecraft.world.level.chunk.status.ChunkStatus;
|
| 37 | import net.minecraft.world.level.chunk.storage.ChunkIOErrorReporter;
|
| 38 | import net.minecraft.world.level.chunk.storage.RegionStorageInfo;
|
| 39 | import net.minecraft.world.level.chunk.storage.SectionStorage;
|
| 40 | import net.minecraft.world.level.chunk.storage.SimpleRegionStorage;
|
| 41 | import org.jspecify.annotations.Nullable;
|
| 42 |
|
| 43 | public class PoiManager extends SectionStorage<PoiSection, PoiSection.Packed> {
|
| 44 | public static final int MAX_VILLAGE_DISTANCE = 6;
|
| 45 | public static final int VILLAGE_SECTION_SIZE = 1;
|
| 46 | private final PoiManager.DistanceTracker distanceTracker;
|
| 47 | private final LongSet loadedChunks = new LongOpenHashSet();
|
| 48 |
|
| 49 | public PoiManager(
|
| 50 | final RegionStorageInfo info,
|
| 51 | final Path folder,
|
| 52 | final DataFixer fixerUpper,
|
| 53 | final boolean sync,
|
| 54 | final RegistryAccess registryAccess,
|
| 55 | final ChunkIOErrorReporter errorReporter,
|
| 56 | final LevelHeightAccessor levelHeightAccessor
|
| 57 | ) {
|
| 58 | super(
|
| 59 | new SimpleRegionStorage(info, folder, fixerUpper, sync, DataFixTypes.POI_CHUNK),
|
| 60 | PoiSection.Packed.CODEC,
|
| 61 | PoiSection::pack,
|
| 62 | PoiSection.Packed::unpack,
|
| 63 | PoiSection::new,
|
| 64 | registryAccess,
|
| 65 | errorReporter,
|
| 66 | levelHeightAccessor
|
| 67 | );
|
| 68 | this.distanceTracker = new PoiManager.DistanceTracker();
|
| 69 | }
|
| 70 |
|
| 71 | public @Nullable PoiRecord add(final BlockPos pos, final Holder<PoiType> type) {
|
| 72 | return this.getOrCreate(SectionPos.asLong(pos)).add(pos, type);
|
| 73 | }
|
| 74 |
|
| 75 | public void remove(final BlockPos pos) {
|
| 76 | this.getOrLoad(SectionPos.asLong(pos)).ifPresent(poiSection -> poiSection.remove(pos));
|
| 77 | }
|
| 78 |
|
| 79 | public long getCountInRange(final Predicate<Holder<PoiType>> predicate, final BlockPos center, final int radius, final PoiManager.Occupancy occupancy) {
|
| 80 | return this.getInRange(predicate, center, radius, occupancy).count();
|
| 81 | }
|
| 82 |
|
| 83 | public boolean existsAtPosition(final ResourceKey<PoiType> poiType, final BlockPos blockPos) {
|
| 84 | return this.exists(blockPos, p -> p.is(poiType));
|
| 85 | }
|
| 86 |
|
| 87 | public Stream<PoiRecord> getInSquare(final Predicate<Holder<PoiType>> predicate, final BlockPos center, final int radius, final PoiManager.Occupancy occupancy) {
|
| 88 | int chunkRadius = Math.floorDiv(radius, 16) + 1;
|
| 89 | return ChunkPos.rangeClosed(ChunkPos.containing(center), chunkRadius).flatMap(pos -> this.getInChunk(predicate, pos, occupancy)).filter(record -> {
|
| 90 | BlockPos pos = record.getPos();
|
| 91 | return Math.abs(pos.getX() - center.getX()) <= radius && Math.abs(pos.getZ() - center.getZ()) <= radius;
|
| 92 | });
|
| 93 | }
|
| 94 |
|
| 95 | public Stream<PoiRecord> getInRange(final Predicate<Holder<PoiType>> predicate, final BlockPos center, final int radius, final PoiManager.Occupancy occupancy) {
|
| 96 | int radiusSqr = radius * radius;
|
| 97 | return this.getInSquare(predicate, center, radius, occupancy).filter(r -> r.getPos().distSqr(center) <= radiusSqr);
|
| 98 | }
|
| 99 |
|
| 100 | @VisibleForDebug
|
| 101 | public Stream<PoiRecord> getInChunk(final Predicate<Holder<PoiType>> predicate, final ChunkPos chunkPos, final PoiManager.Occupancy occupancy) {
|
| 102 | return IntStream.rangeClosed(this.levelHeightAccessor.getMinSectionY(), this.levelHeightAccessor.getMaxSectionY())
|
| 103 | .boxed()
|
| 104 | .map(sectionY -> this.getOrLoad(SectionPos.of(chunkPos, sectionY).asLong()))
|
| 105 | .filter(Optional::isPresent)
|
| 106 | .flatMap(poiSection -> poiSection.get().getRecords(predicate, occupancy));
|
| 107 | }
|
| 108 |
|
| 109 | public Stream<BlockPos> findAll(
|
| 110 | final Predicate<Holder<PoiType>> predicate, final Predicate<BlockPos> filter, final BlockPos center, final int radius, final PoiManager.Occupancy occupancy
|
| 111 | ) {
|
| 112 | return this.getInRange(predicate, center, radius, occupancy).map(PoiRecord::getPos).filter(filter);
|
| 113 | }
|
| 114 |
|
| 115 | public Stream<Pair<Holder<PoiType>, BlockPos>> findAllWithType(
|
| 116 | final Predicate<Holder<PoiType>> predicate, final Predicate<BlockPos> filter, final BlockPos center, final int radius, final PoiManager.Occupancy occupancy
|
| 117 | ) {
|
| 118 | return this.getInRange(predicate, center, radius, occupancy).filter(p -> filter.test(p.getPos())).map(p -> Pair.of(p.getPoiType(), p.getPos()));
|
| 119 | }
|
| 120 |
|
| 121 | public Stream<Pair<Holder<PoiType>, BlockPos>> findAllClosestFirstWithType(
|
| 122 | final Predicate<Holder<PoiType>> predicate, final Predicate<BlockPos> filter, final BlockPos center, final int radius, final PoiManager.Occupancy occupancy
|
| 123 | ) {
|
| 124 | return this.findAllWithType(predicate, filter, center, radius, occupancy).sorted(Comparator.comparingDouble(p -> p.getSecond().distSqr(center)));
|
| 125 | }
|
| 126 |
|
| 127 | public Optional<BlockPos> find(
|
| 128 | final Predicate<Holder<PoiType>> predicate, final Predicate<BlockPos> filter, final BlockPos center, final int radius, final PoiManager.Occupancy occupancy
|
| 129 | ) {
|
| 130 | return this.findAll(predicate, filter, center, radius, occupancy).findFirst();
|
| 131 | }
|
| 132 |
|
| 133 | public Optional<BlockPos> findClosest(
|
| 134 | final Predicate<Holder<PoiType>> predicate, final BlockPos center, final int radius, final PoiManager.Occupancy occupancy
|
| 135 | ) {
|
| 136 | return this.getInRange(predicate, center, radius, occupancy).map(PoiRecord::getPos).min(Comparator.comparingDouble(pos -> pos.distSqr(center)));
|
| 137 | }
|
| 138 |
|
| 139 | public Optional<Pair<Holder<PoiType>, BlockPos>> findClosestWithType(
|
| 140 | final Predicate<Holder<PoiType>> predicate, final BlockPos center, final int radius, final PoiManager.Occupancy occupancy
|
| 141 | ) {
|
| 142 | return this.getInRange(predicate, center, radius, occupancy)
|
| 143 | .min(Comparator.comparingDouble(r -> r.getPos().distSqr(center)))
|
| 144 | .map(p -> Pair.of(p.getPoiType(), p.getPos()));
|
| 145 | }
|
| 146 |
|
| 147 | public Optional<BlockPos> findClosest(
|
| 148 | final Predicate<Holder<PoiType>> predicate, final Predicate<BlockPos> filter, final BlockPos center, final int radius, final PoiManager.Occupancy occupancy
|
| 149 | ) {
|
| 150 | return this.getInRange(predicate, center, radius, occupancy)
|
| 151 | .map(PoiRecord::getPos)
|
| 152 | .filter(filter)
|
| 153 | .min(Comparator.comparingDouble(pos -> pos.distSqr(center)));
|
| 154 | }
|
| 155 |
|
| 156 | public Optional<BlockPos> take(
|
| 157 | final Predicate<Holder<PoiType>> predicate, final BiPredicate<Holder<PoiType>, BlockPos> filter, final BlockPos center, final int radius
|
| 158 | ) {
|
| 159 | return this.getInRange(predicate, center, radius, PoiManager.Occupancy.HAS_SPACE)
|
| 160 | .filter(poi -> filter.test(poi.getPoiType(), poi.getPos()))
|
| 161 | .findFirst()
|
| 162 | .map(r -> {
|
| 163 | r.acquireTicket();
|
| 164 | return r.getPos();
|
| 165 | });
|
| 166 | }
|
| 167 |
|
| 168 | public Optional<BlockPos> getRandom(
|
| 169 | final Predicate<Holder<PoiType>> predicate,
|
| 170 | final Predicate<BlockPos> filter,
|
| 171 | final PoiManager.Occupancy occupancy,
|
| 172 | final BlockPos center,
|
| 173 | final int radius,
|
| 174 | final RandomSource random
|
| 175 | ) {
|
| 176 | List<PoiRecord> collect = Util.toShuffledList(this.getInRange(predicate, center, radius, occupancy), random);
|
| 177 | return collect.stream().filter(poi -> filter.test(poi.getPos())).findFirst().map(PoiRecord::getPos);
|
| 178 | }
|
| 179 |
|
| 180 | public boolean release(final BlockPos pos) {
|
| 181 | return this.getOrLoad(SectionPos.asLong(pos))
|
| 182 | .map(section -> section.release(pos))
|
| 183 | .orElseThrow(() -> Util.pauseInIde(new IllegalStateException("POI never registered at " + pos)));
|
| 184 | }
|
| 185 |
|
| 186 | public boolean exists(final BlockPos pos, final Predicate<Holder<PoiType>> predicate) {
|
| 187 | return this.getOrLoad(SectionPos.asLong(pos)).map(s -> s.exists(pos, predicate)).orElse(false);
|
| 188 | }
|
| 189 |
|
| 190 | public Optional<Holder<PoiType>> getType(final BlockPos pos) {
|
| 191 | return this.getOrLoad(SectionPos.asLong(pos)).flatMap(section -> section.getType(pos));
|
| 192 | }
|
| 193 |
|
| 194 | @VisibleForDebug
|
| 195 | public @Nullable DebugPoiInfo getDebugPoiInfo(final BlockPos pos) {
|
| 196 | return this.getOrLoad(SectionPos.asLong(pos)).flatMap(section -> section.getDebugPoiInfo(pos)).orElse(null);
|
| 197 | }
|
| 198 |
|
| 199 | public int sectionsToVillage(final SectionPos sectionPos) {
|
| 200 | this.distanceTracker.runAllUpdates();
|
| 201 | return this.distanceTracker.getLevel(sectionPos.asLong());
|
| 202 | }
|
| 203 |
|
| 204 | private boolean isVillageCenter(final long sectionPos) {
|
| 205 | Optional<PoiSection> section = this.get(sectionPos);
|
| 206 | return section == null
|
| 207 | ? false
|
| 208 | : section.<Boolean>map(s -> s.getRecords(e -> e.is(PoiTypeTags.VILLAGE), PoiManager.Occupancy.IS_OCCUPIED).findAny().isPresent()).orElse(false);
|
| 209 | }
|
| 210 |
|
| 211 | @Override
|
| 212 | public void tick(final BooleanSupplier haveTime) {
|
| 213 | super.tick(haveTime);
|
| 214 | this.distanceTracker.runAllUpdates();
|
| 215 | }
|
| 216 |
|
| 217 | @Override
|
| 218 | protected void setDirty(final long sectionPos) {
|
| 219 | super.setDirty(sectionPos);
|
| 220 | this.distanceTracker.update(sectionPos, this.distanceTracker.getLevelFromSource(sectionPos), false);
|
| 221 | }
|
| 222 |
|
| 223 | @Override
|
| 224 | protected void onSectionLoad(final long sectionPos) {
|
| 225 | this.distanceTracker.update(sectionPos, this.distanceTracker.getLevelFromSource(sectionPos), false);
|
| 226 | }
|
| 227 |
|
| 228 | public void checkConsistencyWithBlocks(final SectionPos sectionPos, final LevelChunkSection blockSection) {
|
| 229 | Util.ifElse(this.getOrLoad(sectionPos.asLong()), section -> section.refresh(output -> {
|
| 230 | if (mayHavePoi(blockSection)) {
|
| 231 | this.updateFromSection(blockSection, sectionPos, output);
|
| 232 | }
|
| 233 | }), () -> {
|
| 234 | if (mayHavePoi(blockSection)) {
|
| 235 | PoiSection newSection = this.getOrCreate(sectionPos.asLong());
|
| 236 | this.updateFromSection(blockSection, sectionPos, newSection::add);
|
| 237 | }
|
| 238 | });
|
| 239 | }
|
| 240 |
|
| 241 | private static boolean mayHavePoi(final LevelChunkSection blockSection) {
|
| 242 | return blockSection.maybeHas(PoiTypes::hasPoi);
|
| 243 | }
|
| 244 |
|
| 245 | private void updateFromSection(final LevelChunkSection blockSection, final SectionPos pos, final BiConsumer<BlockPos, Holder<PoiType>> output) {
|
| 246 | pos.blocksInside()
|
| 247 | .forEach(
|
| 248 | blockPos -> {
|
| 249 | BlockState state = blockSection.getBlockState(
|
| 250 | SectionPos.sectionRelative(blockPos.getX()), SectionPos.sectionRelative(blockPos.getY()), SectionPos.sectionRelative(blockPos.getZ())
|
| 251 | );
|
| 252 | PoiTypes.forState(state).ifPresent(type -> output.accept(blockPos, (Holder<PoiType>)type));
|
| 253 | }
|
| 254 | );
|
| 255 | }
|
| 256 |
|
| 257 | public void ensureLoadedAndValid(final LevelReader reader, final BlockPos center, final int radius) {
|
| 258 | SectionPos.aroundChunk(
|
| 259 | ChunkPos.containing(center), Math.floorDiv(radius, 16), this.levelHeightAccessor.getMinSectionY(), this.levelHeightAccessor.getMaxSectionY()
|
| 260 | )
|
| 261 | .map(pos -> Pair.of(pos, this.getOrLoad(pos.asLong())))
|
| 262 | .filter(poiSection -> !poiSection.getSecond().map(PoiSection::isValid).orElse(false))
|
| 263 | .map(p -> p.getFirst().chunk())
|
| 264 | .filter(pos -> this.loadedChunks.add(pos.pack()))
|
| 265 | .forEach(pos -> reader.getChunk(pos.x(), pos.z(), ChunkStatus.EMPTY));
|
| 266 | }
|
| 267 |
|
| 268 | private final class DistanceTracker extends SectionTracker {
|
| 269 | private final Long2ByteMap levels = new Long2ByteOpenHashMap();
|
| 270 |
|
| 271 | DistanceTracker() {
|
| 272 | super(7, 16, 256);
|
| 273 | this.levels.defaultReturnValue((byte)7);
|
| 274 | }
|
| 275 |
|
| 276 | @Override
|
| 277 | protected int getLevelFromSource(final long to) {
|
| 278 | return PoiManager.this.isVillageCenter(to) ? 0 : 7;
|
| 279 | }
|
| 280 |
|
| 281 | @Override
|
| 282 | protected int getLevel(final long node) {
|
| 283 | return this.levels.get(node);
|
| 284 | }
|
| 285 |
|
| 286 | @Override
|
| 287 | protected void setLevel(final long node, final int level) {
|
| 288 | if (level > 6) {
|
| 289 | this.levels.remove(node);
|
| 290 | } else {
|
| 291 | this.levels.put(node, (byte)level);
|
| 292 | }
|
| 293 | }
|
| 294 |
|
| 295 | public void runAllUpdates() {
|
| 296 | super.runUpdates(Integer.MAX_VALUE);
|
| 297 | }
|
| 298 | }
|
| 299 |
|
| 300 | public enum Occupancy {
|
| 301 | HAS_SPACE(PoiRecord::hasSpace),
|
| 302 | IS_OCCUPIED(PoiRecord::isOccupied),
|
| 303 | ANY(poiRecord -> true);
|
| 304 |
|
| 305 | private final Predicate<? super PoiRecord> test;
|
| 306 |
|
| 307 | Occupancy(final Predicate<? super PoiRecord> test) {
|
| 308 | this.test = test;
|
| 309 | }
|
| 310 |
|
| 311 | public Predicate<? super PoiRecord> getTest() {
|
| 312 | return this.test;
|
| 313 | }
|
| 314 | }
|
| 315 | }
|
| 316 |
|