Skip to content

Better Circle Scanning #910

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import at.petrak.hexcasting.api.casting.eval.vm.CastingImage;
import at.petrak.hexcasting.api.misc.Result;
import at.petrak.hexcasting.api.pigment.FrozenPigment;
import at.petrak.hexcasting.api.utils.ChunkScanning;
import at.petrak.hexcasting.api.utils.HexUtils;
import com.mojang.datafixers.util.Pair;
import net.minecraft.ChatFormatting;
Expand Down Expand Up @@ -95,13 +96,17 @@ protected CircleExecutionState(BlockPos impetusPos, Direction impetusDir, Set<Bl
todo.add(Pair.of(impetus.getStartDirection(), impetus.getBlockPos().relative(impetus.getStartDirection())));
var seenGoodPosSet = new HashSet<BlockPos>();
var seenGoodPositions = new ArrayList<BlockPos>();
var scanning = new ChunkScanning(level);

while (!todo.isEmpty()) {
var pair = todo.pop();
var enterDir = pair.getFirst();
var herePos = pair.getSecond();
var hereBs = scanning.getBlock(herePos);

var hereBs = level.getBlockState(herePos);
if (hereBs == null){
continue;
}
if (!(hereBs.getBlock() instanceof ICircleComponent cmp)) {
continue;
}
Expand All @@ -118,6 +123,7 @@ protected CircleExecutionState(BlockPos impetusPos, Direction impetusDir, Set<Bl
}
}
}
scanning.clearCache();

if (seenGoodPositions.isEmpty()) {
return new Result.Err<>(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package at.petrak.hexcasting.api.utils

import at.petrak.hexcasting.api.HexAPI
import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap
import net.minecraft.core.BlockPos
import net.minecraft.server.level.ServerLevel
import net.minecraft.world.level.ChunkPos
import net.minecraft.world.level.block.entity.BlockEntity
import net.minecraft.world.level.block.state.BlockState
import net.minecraft.world.level.chunk.ChunkStatus
import net.minecraft.world.level.chunk.ImposterProtoChunk

/**
* This is a helper class to efficiently scan chunks in ways Minecraft did not intend for. This is for only reading chunks, not writing
*/
class ChunkScanning(var level: ServerLevel) {
var chunks: Long2ObjectLinkedOpenHashMap<ImposterProtoChunk> = Long2ObjectLinkedOpenHashMap()

/**
* This attempts to cache a chunk to the local [chunks]
* @param ChunkPos the chunk to try to cache
* @return If the function could cache the chunk or not
*/
fun cacheChunk(chunk: ChunkPos): Boolean {
val chunkLong = chunk.toLong()
// We have the chunk already, so we can skip it
if (chunks.contains(chunkLong)){
return true
}
val future = level.chunkSource.getChunkFuture(chunk.x,chunk.z, ChunkStatus.EMPTY,true).get()
if (future.left().isPresent){
chunks.put(chunkLong, future.left().get() as ImposterProtoChunk)
return true
}
HexAPI.LOGGER.warn("Failed to get chunk at {}!",chunk)
return false
}

fun cacheChunk(chunk: Long): Boolean{
return cacheChunk(ChunkPos(chunk))
}

fun getBlock(blockPos: BlockPos): BlockState? {
val chunkPos = ChunkPos(blockPos).toLong()
if (!cacheChunk(chunkPos)){
return null
}
return chunks.get(chunkPos).getBlockState(blockPos)
}

fun getBlockEntity(blockPos: BlockPos): BlockEntity? {
val chunkPos = ChunkPos(blockPos).toLong()
if (!cacheChunk(chunkPos)){
return null
}
return chunks.get(chunkPos).getBlockEntity(blockPos)
}

// Maybe not required, but still not a bad idea to have a Clear method
fun clearCache(){
chunks.clear()
}

// Might not be needed
fun containsChunk(chunk: ChunkPos): Boolean{
return chunks.contains(chunk.toLong())
}
}