/*
* 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 .
*/
package de.steamwar.bausystem.region;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.region.dynamic.DynamicRegion;
import de.steamwar.bausystem.region.dynamic.RegionConstructorData;
import de.steamwar.bausystem.region.dynamic.Tile;
import de.steamwar.bausystem.region.dynamic.global.GlobalRegion;
import lombok.NonNull;
import org.bukkit.Location;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Stream;
public class DynamicRegionSystem implements RegionSystem {
public static DynamicRegionSystem INSTANCE;
private static final Map regionCache = new LinkedHashMap<>(16, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry eldest) {
return size() > 8192; // Tweak this number if needed!
}
}; // Will be cleared on region add/delete/remove!
private static final Map regionMap = new HashMap<>();
private static final Map> 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);
}
@Override
public void load() {
INSTANCE = this;
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);
}
})
.forEach(clazz -> {
RegionConstructorData regionConstructorData = clazz.getAnnotation(RegionConstructorData.class);
if (regionConstructorData == null) return;
// TODO: Save regionConstructorData together with clazz
});
}
@Override
public @NonNull Location getWorldSpawn() {
return null;
}
@Override
public @NonNull Region getGlobalRegion() {
return GlobalRegion.INSTANCE;
}
private Region get(int x, int z, boolean fastCache, Collection 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);
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 getRegion(@NonNull UUID id) {
return Optional.ofNullable(regionMap.get(id));
}
@Override
public @NonNull Stream getRegions() {
return regionMap.values().stream();
}
private Stream getNeighbours(Region region, boolean noCorners, boolean fastCache, Collection regions) {
Point minPoint = region.getArea().getMinPoint(false).subtract(18, 0, 18);
Point maxPoint = region.getArea().getMaxPoint(false).add(19, 0, 19);
Set neighbours = new HashSet<>();
for (int x = minPoint.getX() + (noCorners ? 18 : 0); x <= maxPoint.getX() - (noCorners ? 19 : 0); x += 19) {
int minZ = minPoint.getZ();
int maxZ = maxPoint.getZ();
neighbours.add(get(x, minZ, fastCache, regions));
neighbours.add(get(x, maxZ, fastCache, regions));
}
for (int z = minPoint.getZ() + 18; z <= maxPoint.getZ() - 19; z += 19) {
int minX = minPoint.getX();
int maxX = maxPoint.getX();
neighbours.add(get(minX, z, fastCache, regions));
neighbours.add(get(maxX, z, fastCache, regions));
}
neighbours.remove(getGlobalRegion());
return neighbours.stream();
}
public Stream getNeighbours(Region region) {
return getNeighbours(region, false, true, regionMap.values());
}
public Stream getConnectedRegions(Region region) {
Set regions = regionTypeMap.get(region.getType());
Set connected = new HashSet<>();
LinkedHashSet current = new LinkedHashSet<>();
current.add(region);
while (!current.isEmpty()) {
Region r = current.removeFirst();
if (!connected.add(r)) continue;
getNeighbours(r, true, false, regions).forEach(current::add);
}
return connected.stream();
}
}