forked from SteamWar/SteamWar
218 lines
9.0 KiB
Java
218 lines
9.0 KiB
Java
/*
|
|
* This file is a part of the SteamWar software.
|
|
*
|
|
* Copyright (C) 2020 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.bausystem.region;
|
|
|
|
import de.steamwar.bausystem.BauSystem;
|
|
import de.steamwar.bausystem.features.region.WireframeCommand;
|
|
import de.steamwar.bausystem.region.dynamic.*;
|
|
import de.steamwar.bausystem.region.dynamic.global.GlobalRegion;
|
|
import de.steamwar.bausystem.shared.Pair;
|
|
import lombok.NonNull;
|
|
import org.bukkit.Bukkit;
|
|
import org.bukkit.Location;
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
import javax.annotation.Nullable;
|
|
import java.io.BufferedReader;
|
|
import java.io.InputStreamReader;
|
|
import java.util.*;
|
|
import java.util.function.Function;
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Stream;
|
|
|
|
public class DynamicRegionSystem implements RegionSystem {
|
|
|
|
private static final int TILE_SIZE_ADJUSTED = Tile.tileSize - 1;
|
|
public static DynamicRegionSystem INSTANCE;
|
|
|
|
private static final Map<Long, Region> regionCache = new LinkedHashMap<>(16, 0.75f, true) {
|
|
@Override
|
|
protected boolean removeEldestEntry(Map.Entry<Long, Region> eldest) {
|
|
return size() > 8192; // Tweak this number if needed!
|
|
}
|
|
}; // Will be cleared on region add/delete/remove!
|
|
private static final Map<UUID, Region> regionMap = new HashMap<>();
|
|
private static final Map<RegionType, Set<Region>> regionTypeMap = new EnumMap<>(RegionType.class);
|
|
|
|
public void add(DynamicRegion region) {
|
|
regionCache.clear();
|
|
regionMap.put(region.getID(), region);
|
|
regionTypeMap.computeIfAbsent(region.getType(), __ -> new HashSet<>()).add(region);
|
|
}
|
|
|
|
public void remove(DynamicRegion region) {
|
|
regionCache.clear();
|
|
regionMap.remove(region.getID());
|
|
regionTypeMap.getOrDefault(region.getType(), Collections.emptySet()).remove(region);
|
|
}
|
|
|
|
public static Map<Class<? extends DynamicRegion>, RegionConstructorData> constructorDataMap = new HashMap<>();
|
|
public static Map<String, Class<? extends DynamicRegion>> identifierDataMap = new HashMap<>();
|
|
|
|
@Override
|
|
public void load() {
|
|
INSTANCE = this;
|
|
|
|
// Loading all Region Constructor Data that are defined inside the Code
|
|
constructorDataMap = new BufferedReader(new InputStreamReader(BauSystem.getInstance().getClass().getResourceAsStream("/META-INF/annotations/de.steamwar.bausystem.region.dynamic.RegionConstructorData")))
|
|
.lines()
|
|
.map(s -> {
|
|
try {
|
|
return Class.forName(s, false, BauSystem.getInstance().getClass().getClassLoader());
|
|
} catch (ClassNotFoundException | NoClassDefFoundError e) {
|
|
throw new SecurityException(e.getMessage(), e);
|
|
}
|
|
})
|
|
.filter(DynamicRegion.class::isAssignableFrom)
|
|
.map(clazz -> (Class<? extends DynamicRegion>) clazz)
|
|
.collect(Collectors.toUnmodifiableMap(Function.identity(), clazz -> clazz.getAnnotation(RegionConstructorData.class)));
|
|
identifierDataMap = constructorDataMap.entrySet()
|
|
.stream()
|
|
.collect(Collectors.toUnmodifiableMap(entry -> entry.getValue().identifier(), Map.Entry::getKey));
|
|
|
|
DynamicRegionRepository.loadRegions();
|
|
new DynamicRegionCommand();
|
|
new WireframeCommand();
|
|
}
|
|
|
|
@Override
|
|
public @NonNull Location getWorldSpawn() {
|
|
return Bukkit.getWorlds().get(0).getSpawnLocation(); // TODO: Temporary
|
|
}
|
|
|
|
@Override
|
|
public @NonNull Region getGlobalRegion() {
|
|
return GlobalRegion.INSTANCE;
|
|
}
|
|
|
|
public Set<Tile> getTilesOfRegion(@NonNull Region region) {
|
|
Point minPoint = region.getArea().getMinPoint(false);
|
|
Point maxPoint = region.getArea().getMaxPoint(false);
|
|
|
|
Set<Tile> tiles = new HashSet<>();
|
|
for (int x = minPoint.getX(); x < maxPoint.getX(); x += Tile.tileSize) {
|
|
for (int z = minPoint.getZ(); z < maxPoint.getZ(); z += Tile.tileSize) {
|
|
tiles.add(Tile.fromXZ(x, z).orElse(null));
|
|
}
|
|
}
|
|
tiles.remove(null);
|
|
return Collections.unmodifiableSet(tiles);
|
|
}
|
|
|
|
public @NonNull Region get(@Nullable Tile tile) {
|
|
if (tile == null) return getGlobalRegion();
|
|
return get(tile.getCenterLocation().getBlockX(), tile.getCenterLocation().getBlockZ(), true, regionMap.values());
|
|
}
|
|
|
|
private Region get(int x, int z, boolean fastCache, Collection<Region> regions) {
|
|
Tile tile = Tile.fromXZ(x, z).orElse(null);
|
|
if (tile == null) {
|
|
return getGlobalRegion();
|
|
}
|
|
if (regionCache.containsKey(tile.getId())) {
|
|
Region region = regionCache.get(tile.getId());
|
|
if (fastCache || regions.contains(region)) return region;
|
|
}
|
|
Region region = regions.stream()
|
|
.filter(rg -> rg.getArea().inRegion(x, z, false))
|
|
.findFirst()
|
|
.orElseGet(this::getGlobalRegion);
|
|
if (fastCache || regions.contains(region)) {
|
|
regionCache.put(tile.getId(), region);
|
|
}
|
|
return region;
|
|
}
|
|
|
|
@Override
|
|
public @NonNull Region get(@NonNull Location location) {
|
|
return get(location.getBlockX(), location.getBlockZ(), true, regionMap.values());
|
|
}
|
|
|
|
@Override
|
|
public Optional<Region> getRegion(@NonNull UUID id) {
|
|
return Optional.ofNullable(regionMap.get(id));
|
|
}
|
|
|
|
@Override
|
|
public @NonNull Stream<Region> getRegions() {
|
|
return regionMap.values().stream();
|
|
}
|
|
|
|
public @NonNull Stream<Region> getRegionsByType(RegionType type) {
|
|
return regionTypeMap.getOrDefault(type, Collections.emptySet()).stream();
|
|
}
|
|
|
|
private Stream<Pair<Region, NeighbourDirection>> getNeighbours(Region region, boolean noCorners, boolean fastCache, Collection<Region> regions) {
|
|
Point minPoint = region.getArea().getMinPoint(false).subtract(TILE_SIZE_ADJUSTED, 0, TILE_SIZE_ADJUSTED);
|
|
Point maxPoint = region.getArea().getMaxPoint(false).add(Tile.tileSize, 0, Tile.tileSize);
|
|
Set<Pair<Region, NeighbourDirection>> neighbours = new HashSet<>();
|
|
|
|
for (int x = minPoint.getX() + (noCorners ? TILE_SIZE_ADJUSTED : 0); x <= maxPoint.getX() - (noCorners ? Tile.tileSize : 0); x += Tile.tileSize) {
|
|
NeighbourDirection minZ = NeighbourDirection.North;
|
|
if (!noCorners) {
|
|
if (x == minPoint.getX()) minZ = NeighbourDirection.NorthWest;
|
|
if (x > maxPoint.getX() - TILE_SIZE_ADJUSTED) minZ = NeighbourDirection.NorthEast;
|
|
}
|
|
neighbours.add(new Pair<>(get(x, minPoint.getZ(), fastCache, regions), minZ));
|
|
|
|
NeighbourDirection maxZ = NeighbourDirection.South;
|
|
if (!noCorners) {
|
|
if (x == minPoint.getX()) maxZ = NeighbourDirection.SouthWest;
|
|
if (x > maxPoint.getX() - TILE_SIZE_ADJUSTED) maxZ = NeighbourDirection.SouthEast;
|
|
}
|
|
neighbours.add(new Pair<>(get(x, maxPoint.getZ(), fastCache, regions), maxZ));
|
|
}
|
|
|
|
for (int z = minPoint.getZ() + TILE_SIZE_ADJUSTED; z <= maxPoint.getZ() - Tile.tileSize; z += Tile.tileSize) {
|
|
neighbours.add(new Pair<>(get(minPoint.getX(), z, fastCache, regions), NeighbourDirection.West));
|
|
neighbours.add(new Pair<>(get(maxPoint.getX(), z, fastCache, regions), NeighbourDirection.East));
|
|
}
|
|
|
|
neighbours.removeIf(data -> data.getKey().getType().isGlobal());
|
|
return neighbours.stream();
|
|
}
|
|
|
|
public Stream<Pair<DynamicRegion, NeighbourDirection>> getNeighbours(Region region) {
|
|
return getNeighbours(region, false, true, regionMap.values())
|
|
.filter(data -> data.getKey() instanceof DynamicRegion)
|
|
.map(data -> (Pair<DynamicRegion, NeighbourDirection>) (Pair) data);
|
|
}
|
|
|
|
@Override
|
|
@NotNull
|
|
public Stream<Region> getConnectedRegions(Region region) {
|
|
Set<Region> regions = regionTypeMap.get(region.getType());
|
|
|
|
Set<Region> connected = new HashSet<>();
|
|
LinkedHashSet<Region> current = new LinkedHashSet<>();
|
|
current.add(region);
|
|
|
|
while (!current.isEmpty()) {
|
|
Region r = current.removeFirst();
|
|
if (!connected.add(r)) continue;
|
|
getNeighbours(r, true, false, regions)
|
|
.map(Pair::getKey)
|
|
.forEach(current::add);
|
|
}
|
|
|
|
return connected.stream();
|
|
}
|
|
}
|