9
0
mirror of https://github.com/Winds-Studio/Leaf.git synced 2025-12-19 15:09:25 +00:00
Files
Leaf/leaf-server/minecraft-patches/features/0211-Alternative-Brain-Behaviour.patch
2025-06-03 09:49:56 +08:00

73 lines
3.1 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Taiyou06 <kaandindar21@gmail.com>
Date: Fri, 14 Feb 2025 14:58:59 +0100
Subject: [PATCH] Alternative Brain Behaviour
In the test, this can give ~54.87% improvement (~25712ms -> ~11604ms),
under 1024 villagers situation.
diff --git a/net/minecraft/world/entity/ai/Brain.java b/net/minecraft/world/entity/ai/Brain.java
index e78cef4c0ee6cff7ec2f27ced12c96be67dc7a0e..e3d0186e681168840836690acd402b774bc464b5 100644
--- a/net/minecraft/world/entity/ai/Brain.java
+++ b/net/minecraft/world/entity/ai/Brain.java
@@ -268,23 +268,52 @@ public class Brain<E extends LivingEntity> {
return this.activeActivities;
}
+ // Leaf start - Alternative Brain Behaviour
+ private ObjectArrayList<BehaviorControl<? super E>> runningBehaviorsCache;
+ private long lastRunningBehaviorCheck = -1;
+ // Leaf end - Alternative Brain Behaviour
+
@Deprecated
@VisibleForDebug
public List<BehaviorControl<? super E>> getRunningBehaviors() {
- List<BehaviorControl<? super E>> list = new ObjectArrayList<>();
+ // Leaf start - Alternative Brain Behaviour
+ long currentTick = getCurrentTick();
+
+ // Use cached result if within update interval
+ if (runningBehaviorsCache != null && (currentTick - lastRunningBehaviorCheck) < org.dreeam.leaf.config.modules.opt.BrainRunningBehaviorCacheUpdate.interval) {
+ return runningBehaviorsCache;
+ }
+
+ // Initialize or reuse cache list
+ if (runningBehaviorsCache == null) {
+ runningBehaviorsCache = new ObjectArrayList<>(32);
+ } else {
+ runningBehaviorsCache.clear();
+ }
+
+ for (Map<Activity, Set<BehaviorControl<? super E>>> activityMap : availableBehaviorsByPriority.values()) {
+ for (Set<BehaviorControl<? super E>> behaviors : activityMap.values()) {
+ if (behaviors.isEmpty()) continue;
- for (Map<Activity, Set<BehaviorControl<? super E>>> map : this.availableBehaviorsByPriority.values()) {
- for (Set<BehaviorControl<? super E>> set : map.values()) {
- for (BehaviorControl<? super E> behaviorControl : set) {
- if (behaviorControl.getStatus() == Behavior.Status.RUNNING) {
- list.add(behaviorControl);
+ for (BehaviorControl<? super E> behavior : behaviors) {
+ if (behavior.getStatus() == Behavior.Status.RUNNING) {
+ runningBehaviorsCache.add(behavior);
}
}
}
}
- return list;
+ lastRunningBehaviorCheck = currentTick;
+
+ return runningBehaviorsCache;
+ }
+
+ // Helper method to get current tick
+ private long getCurrentTick() {
+ // This should be implemented to return the current game tick
+ return System.nanoTime() / 50_000_000; // Approximate tick time of 50ms
}
+ // Leaf end - Alternative Brain Behaviour
public void useDefaultActivity() {
this.setActiveActivity(this.defaultActivity);