Files
PetalMC/patches/server/0005-feat-reduce-sensor-work.patch
peaches94 3e539e3144 initial
2022-07-17 15:03:03 -05:00

77 lines
4.4 KiB
Diff

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: peaches94 <peachescu94@gmail.com>
Date: Sun, 10 Jul 2022 15:44:38 -0500
Subject: [PATCH] feat: reduce sensor work
this patch is focused around the sensors used for ai
delete the line of sight cache less often and use a faster nearby comparison
diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java
index 411593b1b105d62440d76b7bd1b8c74b701e3e75..aec8b3e33dd41978d3542634567a0ad6996f9647 100644
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
@@ -1012,20 +1012,22 @@ public abstract class LivingEntity extends Entity {
}
if (entity != null) {
- ItemStack itemstack = this.getItemBySlot(EquipmentSlot.HEAD);
+ // petal start - only do itemstack lookup if we need to
+ //ItemStack itemstack = this.getItemBySlot(EquipmentSlot.HEAD);
EntityType<?> entitytypes = entity.getType();
// Purpur start
- if (entitytypes == EntityType.SKELETON && itemstack.is(Items.SKELETON_SKULL)) {
+ if (entitytypes == EntityType.SKELETON && this.getItemBySlot(EquipmentSlot.HEAD).is(Items.SKELETON_SKULL)) {
d0 *= entity.level.purpurConfig.skeletonHeadVisibilityPercent;
}
- else if (entitytypes == EntityType.ZOMBIE && itemstack.is(Items.ZOMBIE_HEAD)) {
+ else if (entitytypes == EntityType.ZOMBIE && this.getItemBySlot(EquipmentSlot.HEAD).is(Items.ZOMBIE_HEAD)) {
d0 *= entity.level.purpurConfig.zombieHeadVisibilityPercent;
}
- else if (entitytypes == EntityType.CREEPER && itemstack.is(Items.CREEPER_HEAD)) {
+ else if (entitytypes == EntityType.CREEPER && this.getItemBySlot(EquipmentSlot.HEAD).is(Items.CREEPER_HEAD)) {
d0 *= entity.level.purpurConfig.creeperHeadVisibilityPercent;
}
// Purpur end
+ // petal end
// Purpur start
if (entity instanceof LivingEntity entityliving) {
diff --git a/src/main/java/net/minecraft/world/entity/Mob.java b/src/main/java/net/minecraft/world/entity/Mob.java
index 34db1bd524bb97fbbe0f86b088a2ac343e730f5e..1df1e912194c4d2b934859d999e6d09e802a5978 100644
--- a/src/main/java/net/minecraft/world/entity/Mob.java
+++ b/src/main/java/net/minecraft/world/entity/Mob.java
@@ -878,10 +878,10 @@ public abstract class Mob extends LivingEntity {
return;
}
// Paper end
+ int i = this.level.getServer().getTickCount() + this.getId(); // petal - move up
this.level.getProfiler().push("sensing");
- this.sensing.tick();
+ if (i % 10 == 0) this.sensing.tick(); // petal - only refresh line of sight cache every half second
this.level.getProfiler().pop();
- int i = this.level.getServer().getTickCount() + this.getId();
if (i % 2 != 0 && this.tickCount > 1) {
this.level.getProfiler().push("targetSelector");
diff --git a/src/main/java/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java b/src/main/java/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java
index d8cf99a3014a4b8152ae819fa663c2fdf34dce57..6cce3def9c52cb365c7e831be42eceb1e0710cb4 100644
--- a/src/main/java/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java
+++ b/src/main/java/net/minecraft/world/entity/ai/sensing/NearestLivingEntitySensor.java
@@ -18,7 +18,14 @@ public class NearestLivingEntitySensor<T extends LivingEntity> extends Sensor<T>
List<LivingEntity> list = world.getEntitiesOfClass(LivingEntity.class, aABB, (e) -> {
return e != entity && e.isAlive();
});
- list.sort(Comparator.comparingDouble(entity::distanceToSqr));
+ // petal start - use manual comparison because we don't need the accuracy of Double.compare
+ list.sort((e1, e2) -> {
+ double dist1 = entity.distanceToSqr(e1), dist2 = entity.distanceToSqr(e2);
+ if (dist1 < dist2) return -1;
+ if (dist1 > dist2) return 1;
+ return 0;
+ });
+ // petal end
Brain<?> brain = entity.getBrain();
brain.setMemory(MemoryModuleType.NEAREST_LIVING_ENTITIES, list);
brain.setMemory(MemoryModuleType.NEAREST_VISIBLE_LIVING_ENTITIES, new NearestVisibleLivingEntities(entity, list));