Merge pull request 'Better Hull Hider Algorithm' (#76) from FightSystem/HullHiderAlgo_v2 into main

Reviewed-on: https://steamwar.de/devlabs/SteamWar/SteamWar/pulls/76
Reviewed-by: Chaoscaot <chaos@chaoscaot.de>
This commit is contained in:
Lixfel
2024-12-11 13:16:39 +01:00
6 changed files with 106 additions and 98 deletions
@@ -26,6 +26,7 @@ import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import java.util.Map;
@@ -72,16 +73,16 @@ public class BlockIdWrapper14 implements BlockIdWrapper {
private static final Class<?> entityTracker = Reflection.getClass("{nms.server.level}.PlayerChunkMap$EntityTracker");
private static final Reflection.MethodInvoker updatePlayer = Reflection.getMethod(entityTracker, Core.getVersion() > 15 ? "b" : "updatePlayer", entityPlayer);
@Override
public void trackEntity(Player player, int entity) {
Object tracker = trackers.get(entity);
public void trackEntity(Player player, Entity entity) {
Object tracker = trackers.get(entity.getEntityId());
if(tracker != null)
updatePlayer.invoke(tracker, getPlayer.invoke(player));
}
private static final Reflection.MethodInvoker clearPlayer = Reflection.getMethod(entityTracker, Core.getVersion() > 15 ? "a" : "clear", entityPlayer);
@Override
public void untrackEntity(Player player, int entity) {
Object tracker = trackers.get(entity);
public void untrackEntity(Player player, Entity entity) {
Object tracker = trackers.get(entity.getEntityId());
if(tracker != null)
clearPlayer.invoke(tracker, getPlayer.invoke(player));
}
@@ -24,6 +24,7 @@ plugins {
dependencies {
compileOnly(project(":SpigotCore", "default"))
compileOnly(project(":FightSystem:FightSystem_Core", "default"))
compileOnly(project(":FightSystem:FightSystem_14", "default"))
compileOnly(libs.spigotapi)
@@ -0,0 +1,45 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2024 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.fightsystem.utils;
import com.comphenix.tinyprotocol.Reflection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
public class BlockIdWrapper18 extends BlockIdWrapper14 {
private static final Reflection.FieldAccessor<Map> hiddenEntities = Reflection.getField(Reflection.getClass("{obc}.entity.CraftPlayer"), Map.class, 0, UUID.class, Set.class);
@Override
public void trackEntity(Player player, Entity entity) {
hiddenEntities.get(player).remove(entity.getUniqueId());
super.trackEntity(player, entity);
}
@Override
public void untrackEntity(Player player, Entity entity) {
hiddenEntities.get(player).put(entity.getUniqueId(), Collections.emptySet());
super.untrackEntity(player, entity);
}
}
@@ -24,6 +24,7 @@ import de.steamwar.fightsystem.Config;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
public class BlockIdWrapper8 implements BlockIdWrapper {
@@ -57,16 +58,16 @@ public class BlockIdWrapper8 implements BlockIdWrapper {
private static final Reflection.MethodInvoker get = Reflection.getTypedMethod(intHashMap, "get", Object.class, int.class);
private static final Reflection.MethodInvoker updatePlayer = Reflection.getMethod(entityTrackerEntry, "updatePlayer", entityPlayer);
@Override
public void trackEntity(Player player, int entity) {
Object tracker = get.invoke(trackers, entity);
public void trackEntity(Player player, Entity entity) {
Object tracker = get.invoke(trackers, entity.getEntityId());
if(tracker != null)
updatePlayer.invoke(tracker, getPlayer.invoke(player));
}
private static final Reflection.MethodInvoker clearPlayer = Reflection.getMethod(entityTrackerEntry, "a", entityPlayer);
@Override
public void untrackEntity(Player player, int entity) {
Object tracker = get.invoke(trackers, entity);
public void untrackEntity(Player player, Entity entity) {
Object tracker = get.invoke(trackers, entity.getEntityId());
if(tracker != null)
clearPlayer.invoke(tracker, getPlayer.invoke(player));
}
@@ -25,6 +25,7 @@ import de.steamwar.fightsystem.FightSystem;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
public interface BlockIdWrapper {
@@ -41,6 +42,6 @@ public interface BlockIdWrapper {
int blockToId(Block block);
void setBlock(World world, int x, int y, int z, int blockState);
void trackEntity(Player player, int entity);
void untrackEntity(Player player, int entity);
void trackEntity(Player player, Entity entity);
void untrackEntity(Player player, Entity entity);
}
@@ -24,6 +24,7 @@ import de.steamwar.entity.REntity;
import de.steamwar.fightsystem.Config;
import de.steamwar.fightsystem.FightSystem;
import de.steamwar.fightsystem.fight.FightTeam;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.bukkit.Location;
@@ -35,6 +36,7 @@ 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 {
@@ -44,10 +46,13 @@ 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 Map<IntVector, Map<IntVector, BitSet>> blockVisibility = new HashMap<>();
private final BitSet primaryVisible;
private final Int2IntOpenHashMap visibilityDirections = new Int2IntOpenHashMap(); // Contains the visible directions of each occluding visible block
private final Set<IntVector> uncoveredSurface = new HashSet<>();
private final HashSet<Player> players = new HashSet<>();
@@ -57,41 +62,16 @@ public class Hull {
public Hull(FightTeam team) {
this.region = team.getSchemRegion();
this.groundVisible = region.getMinY() != Config.PlayerRegion.getMinY();
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());
this.primaryVisible = new BitSet(region.volume());
IntVector[] directions;
if (groundVisible) {
directions = new IntVector[]{
new IntVector(1, 0, 0),
new IntVector(-1, 0, 0),
new IntVector(0, 1, 0),
new IntVector(0, -1, 0),
new IntVector(0, 0, 1),
new IntVector(0, 0, -1)
};
} else {
directions = new IntVector[]{
new IntVector(1, 0, 0),
new IntVector(-1, 0, 0),
new IntVector(0, -1, 0),
new IntVector(0, 0, 1),
new IntVector(0, 0, -1)
};
}
// Generate quadrants for each direction
for (IntVector direction : directions) {
Map<IntVector, BitSet> map = new HashMap<>();
for (int z = (direction.z == 0 ? -1 : 0); z <= 1; z += 2) {
for (int y = (direction.y == 0 ? -1 : 0); y <= 1; y += 2) {
for (int x = (direction.x == 0 ? -1 : 0); x <= 1; x += 2) {
map.put(new IntVector(x, y, z), new BitSet(region.volume()));
}
}
}
blockVisibility.put(direction, map);
}
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 >= 0) // Not pointing away from 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) {
@@ -109,29 +89,29 @@ public class Hull {
public void addPlayer(Player player) {
if(players.add(player)) {
for(Entity entity : entities)
BlockIdWrapper.impl.untrackEntity(player, entity.getEntityId());
BlockIdWrapper.impl.untrackEntity(player, entity);
}
}
public void removePlayer(Player player, boolean activeRemoval) {
if(players.remove(player) && activeRemoval) {
for(Entity entity : entities)
BlockIdWrapper.impl.trackEntity(player, entity.getEntityId());
BlockIdWrapper.impl.trackEntity(player, entity);
// techhider triggers block change sending
}
}
public void checkEntity(Entity entity) {
Location location = entity.getLocation();
if(region.inRegion(location) && !visibility.get(new IntVector(location).toId(region))) { //TODO more precise
if(region.inRegion(location) && !visibility.get(new IntVector(location).toId(region))) {
if(entities.add(entity)) {
for(Player player : players)
BlockIdWrapper.impl.untrackEntity(player, entity.getEntityId());
BlockIdWrapper.impl.untrackEntity(player, entity);
}
} else {
if(entities.remove(entity)) {
for(Player player : players)
BlockIdWrapper.impl.trackEntity(player, entity.getEntityId());
BlockIdWrapper.impl.trackEntity(player, entity);
}
}
}
@@ -142,7 +122,7 @@ public class Hull {
public void checkREntity(REntity entity) {
Location location = new Location(Config.world, entity.getX(), entity.getY(), entity.getZ());
if(region.inRegion(location) && !visibility.get(new IntVector(location).toId(region))) { //TODO more precise
if(region.inRegion(location) && !visibility.get(new IntVector(location).toId(region))) {
if(rentities.add(entity))
entity.hide(true);
} else {
@@ -158,11 +138,8 @@ public class Hull {
public void initialize() {
visibility.clear();
occluding.clear();
uncoveredSurface.clear();
for (Map<IntVector, BitSet> direction : blockVisibility.values()) {
for (BitSet set : direction.values())
set.clear();
}
visibilityDirections.clear();
primaryVisible.clear();
long start = System.currentTimeMillis();
region.forEach((x, y, z) -> {
@@ -170,11 +147,7 @@ public class Hull {
if (isOccluding(Config.world.getBlockAt(x, y, z).getType()))
occluding.set(block.toId(region));
});
forEachBorder((root, direction) -> {
for (Map.Entry<IntVector, BitSet> quadrant : blockVisibility.get(direction).entrySet()) {
checkBlock(new NullList<>(), root, direction, quadrant.getKey(), quadrant.getValue());
}
});
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());
}
@@ -187,15 +160,15 @@ public class Hull {
if (!occluding.get(id) || isOccluding(changedType))
return;
List<IntVector> uncovered = new ArrayList<>();
occluding.clear(id);
for (Map.Entry<IntVector, Map<IntVector, BitSet>> direction : blockVisibility.entrySet()) {
for (Map.Entry<IntVector, BitSet> quadrant : direction.getValue().entrySet()) {
if (quadrant.getValue().get(id)) {
quadrant.getValue().clear(id);
checkBlock(uncovered, root, direction.getKey(), quadrant.getKey(), quadrant.getValue());
}
}
if(!visibility.get(id))
return;
List<IntVector> uncovered = new ArrayList<>();
int directions = visibilityDirections.remove(id);
for(IntVector direction : branchDirections) {
if((directionId(direction) & directions) != 0)
updateBlocks(uncovered, root, direction);
}
if(uncovered.isEmpty())
@@ -205,17 +178,17 @@ public class Hull {
Iterator<Entity> it = entities.iterator();
while(it.hasNext()) {
Entity entity = it.next();
if(uncoveredSet.contains(new IntVector(entity.getLocation()))) { //TODO more precise
if(uncoveredSet.contains(new IntVector(entity.getLocation()))) {
it.remove();
for(Player player : players)
BlockIdWrapper.impl.trackEntity(player, entity.getEntityId());
BlockIdWrapper.impl.trackEntity(player, entity);
}
}
Iterator<REntity> rit = rentities.iterator();
while(rit.hasNext()) {
REntity entity = rit.next();
if(uncoveredSet.contains(new IntVector(new Location(Config.world, entity.getX(), entity.getY(), entity.getZ())))) { //TODO more precise
if(uncoveredSet.contains(new IntVector(new Location(Config.world, entity.getX(), entity.getY(), entity.getZ())))) {
rit.remove();
entity.hide(false);
}
@@ -266,48 +239,34 @@ public class Hull {
}
}
private void checkBlock(List<IntVector> uncovered, IntVector block, IntVector direction, IntVector quadrant, BitSet quadVisibility) {
private void updateBlocks(List<IntVector> uncovered, IntVector block, IntVector direction) {
if (block.notInRegion(region))
return;
int id = block.toId(region);
if (quadVisibility.get(id))
return;
quadVisibility.set(id);
if (!visibility.get(id)) {
visibility.set(id);
uncovered.add(block);
}
if (occluding.get(id))
if (occluding.get(id)) {
visibilityDirections.compute(id, (pos, v) -> (v == null ? 0 : v) | directionId(direction));
return;
}
if(primaryVisible.get(id))
return;
IntVector neighbour = block.add(direction);
checkBlock(uncovered, neighbour, direction, quadrant, quadVisibility);
boolean neigbourTransparent = boundedNonOccluding(neighbour);
boolean diagonalReachable = false;
if (direction.x == 0 && (neigbourTransparent || boundedNonOccluding(block.add(quadrant.x, 0, 0)))) {
checkBlock(uncovered, neighbour.add(quadrant.x, 0, 0), direction, quadrant, quadVisibility);
diagonalReachable = boundedNonOccluding(neighbour.add(quadrant.x, 0, 0));
}
updateBlocks(uncovered, block.add(direction), direction);
if(!direction.equals(primaryDirection))
return;
if (direction.y == 0 && (neigbourTransparent || boundedNonOccluding(block.add(0, quadrant.y, 0)))) {
checkBlock(uncovered, neighbour.add(0, quadrant.y, 0), direction, quadrant, quadVisibility);
diagonalReachable = diagonalReachable || boundedNonOccluding(neighbour.add(0, quadrant.y, 0));
}
if (direction.z == 0 && (neigbourTransparent || boundedNonOccluding(block.add(0, 0, quadrant.z)))) {
checkBlock(uncovered, neighbour.add(0, 0, quadrant.z), direction, quadrant, quadVisibility);
diagonalReachable = diagonalReachable || boundedNonOccluding(neighbour.add(0, 0, quadrant.z));
}
if (diagonalReachable)
checkBlock(uncovered, neighbour.add(quadrant), direction, quadrant, quadVisibility);
primaryVisible.set(id);
for(IntVector branchDirection : branchDirections)
updateBlocks(uncovered, block.add(branchDirection), branchDirection);
}
private boolean boundedNonOccluding(IntVector block) {
return !(block.notInRegion(region) || occluding.get(block.toId(region)));
private int directionId(IntVector v) {
return 1 << (9*(v.z+1) + 3*(v.y+1) + (v.x+1));
}