From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Martijn Muijsers Date: Wed, 30 Aug 2023 20:01:31 +0200 Subject: [PATCH] Reduce line of sight updates and cache lookups License: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html) Gale - https://galemc.org This patch is based on the following patch: "feat: reduce sensor work" By: peaches94 As part of: Petal (https://github.com/Bloom-host/Petal) Licensed under: GPL-3.0 (https://www.gnu.org/licenses/gpl-3.0.html) * Petal description * 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/ai/sensing/Sensing.java b/src/main/java/net/minecraft/world/entity/ai/sensing/Sensing.java index 57472cb54e9fd83e980e3c57f28d6e6643f422e8..ce50852a9b18679a80e9393e8d904935917e0439 100644 --- a/src/main/java/net/minecraft/world/entity/ai/sensing/Sensing.java +++ b/src/main/java/net/minecraft/world/entity/ai/sensing/Sensing.java @@ -1,39 +1,81 @@ package net.minecraft.world.entity.ai.sensing; -import it.unimi.dsi.fastutil.ints.IntOpenHashSet; -import it.unimi.dsi.fastutil.ints.IntSet; +import it.unimi.dsi.fastutil.ints.Int2IntMap; +import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; +import it.unimi.dsi.fastutil.ints.IntArrayList; +import it.unimi.dsi.fastutil.ints.IntList; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Mob; +import org.galemc.gale.configuration.GaleGlobalConfiguration; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public class Sensing { private final Mob mob; - // Gale start - initialize line of sight cache with low capacity - private final IntSet seen = new IntOpenHashSet(2); - private final IntSet unseen = new IntOpenHashSet(2); - // Gale end - initialize line of sight cache with low capacity + private final Int2IntMap seen = new Int2IntOpenHashMap(2); // Gale end - initialize line of sight cache with low capacity // Gale - Petal - reduce line of sight cache lookups - merge sets + + // Gale start - Petal - reduce line of sight updates - expiring entity id lists + private final @NotNull IntList @Nullable [] expiring; + private int currentCacheAddIndex = 0; + private int nextToExpireIndex = 1; + // Gale end - Petal - reduce line of sight updates - expiring entity id lists public Sensing(Mob owner) { this.mob = owner; + // Gale start - Petal - reduce line of sight updates - expiring entity id lists + int updateLineOfSightInterval = GaleGlobalConfiguration.get().smallOptimizations.reducedIntervals.updateEntityLineOfSight; + if (updateLineOfSightInterval <= 1) { + this.expiring = null; + } else { + this.expiring = new IntList[updateLineOfSightInterval]; + for (int i = 0; i < updateLineOfSightInterval; i++) { + this.expiring[i] = new IntArrayList(0); + } + } + // Gale end - Petal - reduce line of sight updates - expiring entity id lists } public void tick() { + if (this.expiring == null) { // Gale - Petal - reduce line of sight updates this.seen.clear(); - this.unseen.clear(); + // Gale start - Petal - reduce line of sight updates + } else { + var expiringNow = this.expiring[this.nextToExpireIndex]; + expiringNow.forEach(this.seen::remove); + expiringNow.clear(); + this.currentCacheAddIndex++; + if (this.currentCacheAddIndex == this.expiring.length) { + this.currentCacheAddIndex = 0; + } + this.nextToExpireIndex++; + if (this.nextToExpireIndex == this.expiring.length) { + this.nextToExpireIndex = 0; + } + } + // Gale end - Petal - reduce line of sight updates } public boolean hasLineOfSight(Entity entity) { int i = entity.getId(); - if (this.seen.contains(i)) { + // Gale start - Petal - reduce line of sight cache lookups - merge sets + int cached = this.seen.get(i); + if (cached == 1) { + // Gale end - Petal - reduce line of sight cache lookups - merge sets return true; - } else if (this.unseen.contains(i)) { + } else if (cached == 2) { // Gale - Petal - reduce line of sight cache lookups - merge sets return false; } else { boolean bl = this.mob.hasLineOfSight(entity); if (bl) { - this.seen.add(i); + this.seen.put(i, 1); // Gale - Petal - reduce line of sight cache lookups - merge sets } else { - this.unseen.add(i); + this.seen.put(i, 2); // Gale - Petal - reduce line of sight cache lookups - merge sets + } + // Gale start - Petal - reduce line of sight updates + if (this.expiring != null) { + this.expiring[this.currentCacheAddIndex].add(i); } + // Gale end - Petal - reduce line of sight updates return bl; } diff --git a/src/main/java/org/galemc/gale/configuration/GaleGlobalConfiguration.java b/src/main/java/org/galemc/gale/configuration/GaleGlobalConfiguration.java index c57ebf0f37aba2d118e54f3c71697f29bb9a57a0..417dda8dc61af144fe07963b66080e5c9f2750b4 100644 --- a/src/main/java/org/galemc/gale/configuration/GaleGlobalConfiguration.java +++ b/src/main/java/org/galemc/gale/configuration/GaleGlobalConfiguration.java @@ -34,6 +34,7 @@ public class GaleGlobalConfiguration extends ConfigurationPart { public class ReducedIntervals extends ConfigurationPart { public int increaseTimeStatistics = 20; // Gale - Hydrinity - increase time statistics in intervals + public int updateEntityLineOfSight = 4; // Gale - Petal - reduce line of sight updates @PostProcess public void postProcess() {