Performance improvements

This commit is contained in:
Lixfel
2024-12-10 01:59:16 +01:00
parent b1c0e36cee
commit c50bb64516

View File

@@ -24,8 +24,6 @@ import de.steamwar.entity.REntity;
import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.FightSystem;
import de.steamwar.fightsystem.fight.FightTeam; 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 it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
@@ -38,35 +36,10 @@ import org.bukkit.entity.Player;
import java.util.*; import java.util.*;
import java.util.function.BiConsumer; import java.util.function.BiConsumer;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.stream.IntStream;
public class Hull { 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) { private static boolean isOccluding(Material material) {
return material.isOccluding() || Config.HiddenBlocks.contains(material); return material.isOccluding() || Config.HiddenBlocks.contains(material);
} }
@@ -74,10 +47,11 @@ public class Hull {
private final Region region; private final Region region;
private final boolean groundVisible; private final boolean groundVisible;
private final IntVector primaryDirection; private final IntVector primaryDirection;
private final IntVector[] branchDirections;
private final BitSet occluding; private final BitSet occluding;
private final BitSet visibility; 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<IntVector> uncoveredSurface = new HashSet<>(); private final Set<IntVector> uncoveredSurface = new HashSet<>();
private final HashSet<Player> players = new HashSet<>(); private final HashSet<Player> players = new HashSet<>();
@@ -90,6 +64,12 @@ public class Hull {
this.primaryDirection = new IntVector(0, 0, team.isBlue() == (Config.BlueToRedZ > 0) ? -1 : 1); this.primaryDirection = new IntVector(0, 0, team.isBlue() == (Config.BlueToRedZ > 0) ? -1 : 1);
this.occluding = new BitSet(region.volume()); this.occluding = new BitSet(region.volume());
this.visibility = 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) { 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)); 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()); 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) { public void updateBlockVisibility(Block b, Material changedType) {
@@ -190,7 +163,7 @@ public class Hull {
List<IntVector> uncovered = new ArrayList<>(); List<IntVector> uncovered = new ArrayList<>();
int directions = visibilityDirections.remove(id); int directions = visibilityDirections.remove(id);
for(IntVector direction : DIRECTIONS) { for(IntVector direction : branchDirections) {
if((directionId(direction) & directions) != 0) if((directionId(direction) & directions) != 0)
updateBlocks(uncovered, root, direction); updateBlocks(uncovered, root, direction);
} }
@@ -282,10 +255,8 @@ public class Hull {
if(!direction.equals(primaryDirection)) if(!direction.equals(primaryDirection))
return; return;
for(IntVector branchDirection : DIRECTIONS) { for(IntVector branchDirection : branchDirections)
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);
updateBlocks(uncovered, block.add(branchDirection), branchDirection);
}
} }
private int directionId(IntVector v) { private int directionId(IntVector v) {