From c50bb64516c89471229f67c0cd5ec1cf4906dc07 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Tue, 10 Dec 2024 01:59:16 +0100 Subject: [PATCH] Performance improvements --- .../de/steamwar/fightsystem/utils/Hull.java | 53 +++++-------------- 1 file changed, 12 insertions(+), 41 deletions(-) diff --git a/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java b/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java index e69a7986..fd0113c0 100644 --- a/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java +++ b/FightSystem/FightSystem_Core/src/de/steamwar/fightsystem/utils/Hull.java @@ -24,8 +24,6 @@ import de.steamwar.entity.REntity; import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.fight.FightTeam; -import de.steamwar.techhider.BlockIds; -import it.unimi.dsi.fastutil.ints.Int2IntMap; import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; import lombok.AllArgsConstructor; import lombok.Getter; @@ -38,35 +36,10 @@ import org.bukkit.entity.Player; import java.util.*; import java.util.function.BiConsumer; import java.util.logging.Level; +import java.util.stream.IntStream; public class Hull { - private static final boolean DEBUG = false; - private static final int AIR = BlockIds.impl.materialToId(Material.AIR); - private static final int RED_GLASS = BlockIds.impl.materialToId(Material.STONE); - private static final int GLASS = BlockIds.impl.materialToId(Material.GLASS); - - private static final IntVector[] DIRECTIONS = new IntVector[]{ - new IntVector(-1, -1, 0), - new IntVector(-1, 0, -1), - new IntVector(-1, 0, 0), - new IntVector(-1, 0, 1), - new IntVector(-1, 1, 0), - new IntVector(0, -1, -1), - new IntVector(0, -1, 0), - new IntVector(0, -1, 1), - new IntVector(0, 0, -1), - new IntVector(0, 0, 1), - new IntVector(0, 1, -1), - new IntVector(0, 1, 0), - new IntVector(0, 1, 1), - new IntVector(1, -1, 0), - new IntVector(1, 0, -1), - new IntVector(1, 0, 0), - new IntVector(1, 0, 1), - new IntVector(1, 1, 0), - }; - private static boolean isOccluding(Material material) { return material.isOccluding() || Config.HiddenBlocks.contains(material); } @@ -74,10 +47,11 @@ public class Hull { private final Region region; private final boolean groundVisible; private final IntVector primaryDirection; + private final IntVector[] branchDirections; private final BitSet occluding; private final BitSet visibility; - private final Int2IntMap visibilityDirections = new Int2IntOpenHashMap(); // Contains the visible directions of each occluding visible block + private final Int2IntOpenHashMap visibilityDirections = new Int2IntOpenHashMap(); // Contains the visible directions of each occluding visible block private final Set uncoveredSurface = new HashSet<>(); private final HashSet players = new HashSet<>(); @@ -90,6 +64,12 @@ public class Hull { this.primaryDirection = new IntVector(0, 0, team.isBlue() == (Config.BlueToRedZ > 0) ? -1 : 1); this.occluding = new BitSet(region.volume()); this.visibility = new BitSet(region.volume()); + + branchDirections = IntStream.range(0, 27) + .mapToObj(v -> new IntVector(v%3 -1, (v/3)%3 -1, v/9 -1)) + .filter(v -> v.x*primaryDirection.x + v.y*primaryDirection.y + v.z*primaryDirection.z == 1) // Pointing towards primary direction + .filter(v -> v.x*v.x + v.y*v.y + v.z*v.z == 2) // Diagonal + .toArray(IntVector[]::new); } public boolean blockPrecise(Player player, int chunkX, int chunkY, int chunkZ) { @@ -166,13 +146,6 @@ public class Hull { }); forEachBorder((root, direction) -> updateBlocks(new NullList<>(), root, direction)); FightSystem.getPlugin().getLogger().log(Level.INFO, () -> "[HullHider] initialisation finished: " + (System.currentTimeMillis() - start) + " ms, visible blocks: " + visibility.cardinality()); - - if(DEBUG) { - region.forEach((x, y, z) -> { - int id = new IntVector(x, y, z).toId(region); - BlockIdWrapper.impl.setBlock(Config.world, x, y + Config.BlueExtendRegion.getSizeY(), z, visibility.get(id) ? (occluding.get(id) ? RED_GLASS : AIR) : GLASS); - }); - } } public void updateBlockVisibility(Block b, Material changedType) { @@ -190,7 +163,7 @@ public class Hull { List uncovered = new ArrayList<>(); int directions = visibilityDirections.remove(id); - for(IntVector direction : DIRECTIONS) { + for(IntVector direction : branchDirections) { if((directionId(direction) & directions) != 0) updateBlocks(uncovered, root, direction); } @@ -282,10 +255,8 @@ public class Hull { if(!direction.equals(primaryDirection)) return; - for(IntVector branchDirection : DIRECTIONS) { - if(!branchDirection.equals(direction) && direction.x*branchDirection.x + direction.y*branchDirection.y + direction.z*branchDirection.z == 1) // If branch direction diagonal pointing towards direction - updateBlocks(uncovered, block.add(branchDirection), branchDirection); - } + for(IntVector branchDirection : branchDirections) + updateBlocks(uncovered, block.add(branchDirection), branchDirection); } private int directionId(IntVector v) {