Skip to content

Commit

Permalink
Rewrite some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Lora4967 committed Mar 31, 2024
1 parent 3ceeb53 commit bbc7b71
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 23 deletions.
35 changes: 21 additions & 14 deletions patches/minecraft/net/minecraft/world/ticks/LevelTicks.java.patch
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import it.unimi.dsi.fastutil.objects.ObjectIterator;
import it.unimi.dsi.fastutil.objects.ObjectOpenCustomHashSet;
import java.util.ArrayDeque;
@@ -16,114 +_,143 @@
@@ -16,114 +_,142 @@
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Set;
Expand Down Expand Up @@ -149,9 +149,9 @@
+ //profilerFiller.incrementCounter("ticksToRun", this.toRunThisTick.size()); // Purpur
this.m_193272_(p_193228_);
- profilerfiller.m_6182_("cleanup");
+ //profilerFiller.popPush("cleanup"); // Purpur
this.m_193284_();
- this.m_193284_();
- profilerfiller.m_7238_();
+ //profilerFiller.popPush("cleanup"); // Purpur
+ //profilerFiller.pop(); // Purpur
}

Expand Down Expand Up @@ -297,21 +297,28 @@
-
- this.f_193206_.add(scheduledtick);
- p_193273_.accept(scheduledtick.f_193377_(), scheduledtick.f_193376_());
- }
-
- }
-
- private void m_193284_() {
- this.f_193205_.clear();
- this.f_193204_.clear();
- this.f_193206_.clear();
- this.f_193207_.clear();
- }
-
+ }finally {
+ this.accessLock.writeLock().unlock();
}
+ }
+
+ for (ScheduledTick<T> scheduledTick : toRun){
+ p_193273_.accept(scheduledTick.pos(), scheduledTick.type());
+ }*/
+ ServerThreadingHooks.postRunScheduledTicks(this,p_193273_);
}

private void m_193284_() {
- this.f_193205_.clear();
- this.f_193204_.clear();
- this.f_193206_.clear();
- this.f_193207_.clear();
+ }
+
+ public void m_193284_() {
+ this.accessLock.writeLock().lock();
+ try {
+ this.f_193205_.clear();
Expand All @@ -321,8 +328,8 @@
+ }finally {
+ this.accessLock.writeLock().unlock();
+ }
}
+ }
+
+ @Override
public boolean m_183582_(BlockPos p_193254_, T p_193255_) {
- LevelChunkTicks<T> levelchunkticks = this.f_193202_.get(ChunkPos.m_151388_(p_193254_));
Expand Down
93 changes: 86 additions & 7 deletions src/main/java/me/earthme/millacat/hook/ServerThreadingHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public static void callGlobalTileEntityTickTask(ServerLevel levelIn){
}

final AtomicInteger taskCounter = worldTaskCount.get(levelIn);
for (List<Locatable> split : remerge(groupByChunk(levelIn.blockEntityTickers))){
for (List<Locatable> split : mergePointsWithinDistance(groupByChunk(levelIn.blockEntityTickers)).values()){
taskCounter.getAndIncrement();
tileEntityThreadPool.execute(()->{
try {
Expand Down Expand Up @@ -171,7 +171,7 @@ public static void postChunkTickTask(@NotNull LevelChunk chunkIn, int randomTick
});
}

public static Map<Long, List<Locatable>> groupByChunk(Collection<? extends Locatable> dataList) {
public static Map<ChunkPos, List<Locatable>> groupByChunk(Collection<? extends Locatable> dataList) {
return dataList.stream().collect(Collectors.groupingBy(Locatable::getChunkKey, LinkedHashMap::new, Collectors.toList()));
}

Expand All @@ -187,6 +187,84 @@ public static List<ChunkPos> getNeighbors(ChunkPos pos) {
return neighbors;
}

public static Map<ChunkPos, List<Locatable>> mergePointsWithinDistance(Map<ChunkPos, List<Locatable>> map) {
Map<ChunkPos, List<Locatable>> result = new HashMap<>();
Set<Set<ChunkPos>> mergedPointSets = new HashSet<>();

for (ChunkPos startPoint : map.keySet()) {
if (!isMerged(startPoint, mergedPointSets)) {
Set<ChunkPos> mergedPoints = bfs(startPoint, map);
mergedPointSets.add(mergedPoints);

List<Locatable> mergedData = new ArrayList<>();
for (ChunkPos p : mergedPoints) {
mergedData.addAll(map.get(p));
}

ChunkPos centroid = calculateCentroid(mergedPoints);
result.put(centroid, mergedData);
}
}

return result;
}

private static boolean isMerged(ChunkPos point, Set<Set<ChunkPos>> mergedPointSets) {
for (Set<ChunkPos> mergedPoints : mergedPointSets) {
if (mergedPoints.contains(point)) {
return true;
}
}
return false;
}

private static Set<ChunkPos> bfs(ChunkPos startPoint, Map<ChunkPos, List<Locatable>> map) {
Set<ChunkPos> visited = new HashSet<>();
Queue<ChunkPos> queue = new LinkedList<>();
Set<ChunkPos> mergedPoints = new HashSet<>();
double threshold = 1.5;

queue.offer(startPoint);
visited.add(startPoint);

while (!queue.isEmpty()) {
ChunkPos currentPoint = queue.poll();
mergedPoints.add(currentPoint);

for (ChunkPos neighbor : map.keySet()) {
if (!visited.contains(neighbor) && calculateDistance(currentPoint, neighbor) < threshold) {
visited.add(neighbor);
queue.offer(neighbor);
}
}
}

return mergedPoints;
}

private static double calculateDistance(ChunkPos p1, ChunkPos p2) {
// Calculate distance between two points
return Math.sqrt(Math.pow(p1.x - p2.x, 2) + Math.pow(p1.z - p2.z, 2));
}


private static ChunkPos calculateCentroid(Set<ChunkPos> points) {
int totalX = 0;
int totalZ = 0;

for (ChunkPos p : points) {
totalX += p.x;
totalZ += p.z;
}

int centroidX = totalX / points.size();
int centroidY = totalZ / points.size();

return new ChunkPos(centroidX, centroidY);
}



public static Collection<List<Locatable>> remerge(Map<Long, List<Locatable>> map) {
Map<Long, List<Locatable>> aggregatedMap = new HashMap<>();
Set<Long> visited = new HashSet<>();
Expand All @@ -198,15 +276,15 @@ public static Collection<List<Locatable>> remerge(Map<Long, List<Locatable>> map

List<Locatable> locatables = new ArrayList<>();
Queue<ChunkPos> queue = new LinkedList<>();
queue.offer(new ChunkPos(chunkPos));
queue.offer(new ChunkPos((int) (chunkPos >> 32), (int) (chunkPos & 0xffffffffL)));
visited.add(chunkPos);

while (!queue.isEmpty()) {
ChunkPos currentPos = queue.poll();
locatables.addAll(map.get(chunkPos));

for (ChunkPos neighbor : getNeighbors(currentPos)) {
Long neighborKey = neighbor.toLong();
Long neighborKey = (((long) neighbor.x << 32) | neighbor.z);
if (!visited.contains(neighborKey) && map.containsKey(neighborKey) &&
distance(currentPos,neighbor) <= 2) {
queue.offer(neighbor);
Expand All @@ -219,7 +297,6 @@ public static Collection<List<Locatable>> remerge(Map<Long, List<Locatable>> map
aggregatedMap.put(newKey, locatables);
}


return aggregatedMap.values();
}

Expand Down Expand Up @@ -252,7 +329,7 @@ public static <T> void postRunScheduledTicks(LevelTicks<T> ticks, BiConsumer<Blo

final List<Runnable> toRun = new ArrayList<>();

for (List<Locatable> split : remerge(groupByChunk(scheduledTicks))){
for (List<Locatable> split : mergePointsWithinDistance(groupByChunk(scheduledTicks)).values()){
taskCounter.getAndIncrement();
toRun.add(() -> {
try {
Expand All @@ -270,13 +347,15 @@ public static <T> void postRunScheduledTicks(LevelTicks<T> ticks, BiConsumer<Blo
for (Runnable task : toRun){
miscThreadPool.execute(task);
}

worldCallbackTasks.get(ticks.level).offer(ticks::cleanupAfterTick);
}

public static void postEntityTicKTask(ServerLevel level){
final AtomicInteger taskCounter = worldTaskCount.get(level);
final List<Runnable> toRun = new ArrayList<>();

for (List<Locatable> split : remerge(groupByChunk(level.entityTickList.entities))){
for (List<Locatable> split : mergePointsWithinDistance(groupByChunk(level.entityTickList.entities)).values()){
taskCounter.getAndIncrement();
toRun.add(() -> {
try {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/me/earthme/millacat/utils/Locatable.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public interface Locatable {

int chunkZ();

default long getChunkKey(){
return ChunkPos.asLong(this.chunkX(),this.chunkZ());
default ChunkPos getChunkKey(){
return new ChunkPos(this.chunkX(),this.chunkZ());
}
}

0 comments on commit bbc7b71

Please sign in to comment.