mirror of
https://github.com/Winds-Studio/Leaf.git
synced 2025-12-26 10:29:13 +00:00
rewrite InsideBrownianWalk
This commit is contained in:
@@ -1,53 +0,0 @@
|
||||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||
From: Taiyou06 <kaandindar21@gmail.com>
|
||||
Date: Wed, 19 Mar 2025 15:18:58 +0100
|
||||
Subject: [PATCH] Remove streams on InsideBrownianWalk
|
||||
|
||||
This method is mainly visible when ton of villagers suddenly wants to sleep
|
||||
Safe optimization, no need to provide any numbers.
|
||||
|
||||
diff --git a/net/minecraft/world/entity/ai/behavior/InsideBrownianWalk.java b/net/minecraft/world/entity/ai/behavior/InsideBrownianWalk.java
|
||||
index cbde74f4b6d586a5f80cdd675573441636bf682d..2324b941c7513692608b5146bc90c8e6124d2ee6 100644
|
||||
--- a/net/minecraft/world/entity/ai/behavior/InsideBrownianWalk.java
|
||||
+++ b/net/minecraft/world/entity/ai/behavior/InsideBrownianWalk.java
|
||||
@@ -20,16 +20,31 @@ public class InsideBrownianWalk {
|
||||
return false;
|
||||
} else {
|
||||
BlockPos blockPos = mob.blockPosition();
|
||||
- List<BlockPos> list = BlockPos.betweenClosedStream(blockPos.offset(-1, -1, -1), blockPos.offset(1, 1, 1))
|
||||
- .map(BlockPos::immutable)
|
||||
- .collect(Util.toMutableList());
|
||||
+ // Leaf start - Remove streams on InsideBrownianWalk
|
||||
+ BlockPos minPos = blockPos.offset(-1, -1, -1);
|
||||
+ BlockPos maxPos = blockPos.offset(1, 1, 1);
|
||||
+ List<BlockPos> list = new java.util.ArrayList<>();
|
||||
+
|
||||
+ for (int x = minPos.getX(); x <= maxPos.getX(); x++) {
|
||||
+ for (int y = minPos.getY(); y <= maxPos.getY(); y++) {
|
||||
+ for (int z = minPos.getZ(); z <= maxPos.getZ(); z++) {
|
||||
+ list.add(new BlockPos(x, y, z).immutable());
|
||||
+ }
|
||||
+ }
|
||||
+ }
|
||||
+
|
||||
Collections.shuffle(list);
|
||||
- list.stream()
|
||||
- .filter(pos -> !level.canSeeSky(pos))
|
||||
- .filter(pos -> level.loadedAndEntityCanStandOn(pos, mob))
|
||||
- .filter(pos -> level.noCollision(mob))
|
||||
- .findFirst()
|
||||
- .ifPresent(pos -> walkTarget.set(new WalkTarget(pos, speedModifier, 0)));
|
||||
+
|
||||
+ for (BlockPos pos : list) {
|
||||
+ if (!level.canSeeSky(pos) &&
|
||||
+ level.loadedAndEntityCanStandOn(pos, mob) &&
|
||||
+ level.noCollision(mob)) {
|
||||
+ walkTarget.set(new WalkTarget(pos, speedModifier, 0));
|
||||
+ break;
|
||||
+ }
|
||||
+ }
|
||||
+ // Leaf end - Remove streams on InsideBrownianWalk
|
||||
+
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
||||
From: Taiyou06 <kaandindar21@gmail.com>
|
||||
Date: Wed, 19 Mar 2025 15:18:58 +0100
|
||||
Subject: [PATCH] rewrite InsideBrownianWalk
|
||||
|
||||
License: LGPL-3.0 (https://www.gnu.org/licenses/lgpl-3.0.html)
|
||||
|
||||
This method is mainly visible when ton of villagers suddenly wants to sleep.
|
||||
|
||||
diff --git a/net/minecraft/world/entity/ai/behavior/InsideBrownianWalk.java b/net/minecraft/world/entity/ai/behavior/InsideBrownianWalk.java
|
||||
index cbde74f4b6d586a5f80cdd675573441636bf682d..0e0a05b7ddbce22faef3ff1060c85c616b6bcf0b 100644
|
||||
--- a/net/minecraft/world/entity/ai/behavior/InsideBrownianWalk.java
|
||||
+++ b/net/minecraft/world/entity/ai/behavior/InsideBrownianWalk.java
|
||||
@@ -9,31 +9,42 @@ import net.minecraft.world.entity.ai.behavior.declarative.BehaviorBuilder;
|
||||
import net.minecraft.world.entity.ai.memory.MemoryModuleType;
|
||||
import net.minecraft.world.entity.ai.memory.WalkTarget;
|
||||
|
||||
+// Leaf start - rewrite InsideBrownianWalk
|
||||
public class InsideBrownianWalk {
|
||||
+ private static final java.util.Random RANDOM = new java.util.Random();
|
||||
+
|
||||
public static BehaviorControl<PathfinderMob> create(float speedModifier) {
|
||||
- return BehaviorBuilder.create(
|
||||
- instance -> instance.group(instance.absent(MemoryModuleType.WALK_TARGET))
|
||||
- .apply(
|
||||
- instance,
|
||||
- walkTarget -> (level, mob, gameTime) -> {
|
||||
- if (level.canSeeSky(mob.blockPosition())) {
|
||||
- return false;
|
||||
- } else {
|
||||
- BlockPos blockPos = mob.blockPosition();
|
||||
- List<BlockPos> list = BlockPos.betweenClosedStream(blockPos.offset(-1, -1, -1), blockPos.offset(1, 1, 1))
|
||||
- .map(BlockPos::immutable)
|
||||
- .collect(Util.toMutableList());
|
||||
- Collections.shuffle(list);
|
||||
- list.stream()
|
||||
- .filter(pos -> !level.canSeeSky(pos))
|
||||
- .filter(pos -> level.loadedAndEntityCanStandOn(pos, mob))
|
||||
- .filter(pos -> level.noCollision(mob))
|
||||
- .findFirst()
|
||||
- .ifPresent(pos -> walkTarget.set(new WalkTarget(pos, speedModifier, 0)));
|
||||
- return true;
|
||||
- }
|
||||
+ return BehaviorBuilder.create(instance -> instance.group(instance.absent(MemoryModuleType.WALK_TARGET))
|
||||
+ .apply(instance, walkTarget -> (level, mob, gameTime) -> {
|
||||
+ if (level.canSeeSky(mob.blockPosition())) {
|
||||
+ return false;
|
||||
+ }
|
||||
+
|
||||
+ BlockPos currentPos = mob.blockPosition();
|
||||
+ BlockPos[] nearbyBlocks = org.dreeam.leaf.util.BlockAreaUtils.getBlocksBetween(
|
||||
+ currentPos.offset(-1, -1, -1),
|
||||
+ currentPos.offset(1, 1, 1)
|
||||
+ );
|
||||
+
|
||||
+ // Fisher-Yates shuffle is faster for this case
|
||||
+ for (int i = nearbyBlocks.length - 1; i > 0; i--) {
|
||||
+ int j = RANDOM.nextInt(i + 1);
|
||||
+ BlockPos temp = nearbyBlocks[i];
|
||||
+ nearbyBlocks[i] = nearbyBlocks[j];
|
||||
+ nearbyBlocks[j] = temp;
|
||||
+ }
|
||||
+
|
||||
+ for (BlockPos pos : nearbyBlocks) {
|
||||
+ if (!level.canSeeSky(pos) &&
|
||||
+ level.loadedAndEntityCanStandOn(pos, mob) &&
|
||||
+ level.noCollision(mob)) {
|
||||
+ walkTarget.set(new WalkTarget(pos, speedModifier, 0));
|
||||
+ break;
|
||||
}
|
||||
- )
|
||||
+ }
|
||||
+ return true;
|
||||
+ })
|
||||
);
|
||||
}
|
||||
}
|
||||
+// Leaf end - rewrite InsideBrownianWalk
|
||||
@@ -0,0 +1,41 @@
|
||||
package org.dreeam.leaf.util;
|
||||
|
||||
import net.minecraft.core.BlockPos;
|
||||
|
||||
public final class BlockAreaUtils {
|
||||
private BlockAreaUtils() {}
|
||||
|
||||
public static BlockPos[] getBlocksBetween(BlockPos start, BlockPos end) {
|
||||
return generateBlockPosArray(
|
||||
Math.min(start.getX(), end.getX()),
|
||||
Math.min(start.getY(), end.getY()),
|
||||
Math.min(start.getZ(), end.getZ()),
|
||||
Math.max(start.getX(), end.getX()),
|
||||
Math.max(start.getY(), end.getY()),
|
||||
Math.max(start.getZ(), end.getZ())
|
||||
);
|
||||
}
|
||||
|
||||
private static BlockPos[] generateBlockPosArray(int minX, int minY, int minZ, int maxX, int maxY, int maxZ) {
|
||||
int width = maxX - minX + 1;
|
||||
int height = maxY - minY + 1;
|
||||
int depth = maxZ - minZ + 1;
|
||||
int size = width * height * depth;
|
||||
BlockPos[] result = new BlockPos[size];
|
||||
|
||||
int index = 0;
|
||||
for (int y = 0; y < height; y++) {
|
||||
int currentY = minY + y;
|
||||
for (int z = 0; z < depth; z++) {
|
||||
int currentZ = minZ + z;
|
||||
BlockPos[] xChunk = new BlockPos[width];
|
||||
for (int x = 0; x < width; x++) {
|
||||
xChunk[x] = new BlockPos(minX + x, currentY, currentZ);
|
||||
}
|
||||
System.arraycopy(xChunk, 0, result, index, width);
|
||||
index += width;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user