forked from SteamWar/SteamWar
Add MicroWarGear21Region
Add MiniWarGear21Region Add WarGear21Region Add WarShip21Region
This commit is contained in:
+67
-6
@@ -22,12 +22,20 @@ package de.steamwar.bausystem.region;
|
||||
import de.steamwar.bausystem.features.region.RegionCommand;
|
||||
import de.steamwar.bausystem.region.dynamic.DynamicRegion;
|
||||
import de.steamwar.bausystem.region.dynamic.TileUtils;
|
||||
import de.steamwar.bausystem.region.dynamic.normal.work.MicroWarGear21Region;
|
||||
import de.steamwar.bausystem.region.dynamic.normal.work.MiniWarGear21Region;
|
||||
import de.steamwar.bausystem.region.dynamic.normal.work.WarGear21Region;
|
||||
import de.steamwar.bausystem.region.dynamic.normal.work.WarShip21Region;
|
||||
import de.steamwar.bausystem.region.dynamic.path.PathRegion;
|
||||
import de.steamwar.bausystem.shared.Pair;
|
||||
import de.steamwar.bausystem.utils.PasteBuilder;
|
||||
import de.steamwar.command.AbstractSWCommand;
|
||||
import de.steamwar.command.SWCommand;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@AbstractSWCommand.PartOf(RegionCommand.class)
|
||||
@@ -38,15 +46,68 @@ public class DynamicRegionCommand extends SWCommand {
|
||||
}
|
||||
|
||||
@Register({"dynamic", "place"})
|
||||
public void placeRegion(Player player) {
|
||||
public void placeRegion(Player player, Placement placement) {
|
||||
Region region = DynamicRegionSystem.INSTANCE.get(player.getLocation());
|
||||
if (!region.getType().isGlobal()) return;
|
||||
Pair<Integer, Integer> tile = TileUtils.fromLocation(player.getLocation());
|
||||
Pair<Integer, Integer> min = TileUtils.toMinLocation(tile);
|
||||
PathRegion pathRegion = new PathRegion(min.getKey(), min.getValue());
|
||||
pathRegion.getArea().reset(null, false);
|
||||
DynamicRegionSystem.INSTANCE.getNeighbours(pathRegion).collect(Collectors.toList())
|
||||
.forEach(r -> r.update(pathRegion));
|
||||
Pair<Integer, Integer> placePosition = placePosition(tile, placement);
|
||||
if (placePosition == null) return;
|
||||
Pair<Integer, Integer> min = TileUtils.toMinLocation(placePosition);
|
||||
|
||||
DynamicRegion dynamicRegion = placement.constructor.apply(min.getKey(), min.getValue());
|
||||
dynamicRegion.getArea().reset(new PasteBuilder(new PasteBuilder.FileProvider(dynamicRegion.getArea().getResetFile())), false);
|
||||
DynamicRegionSystem.INSTANCE.getNeighbours(dynamicRegion).collect(Collectors.toList())
|
||||
.forEach(r -> r.update(dynamicRegion));
|
||||
}
|
||||
|
||||
private Pair<Integer, Integer> placePosition(Pair<Integer, Integer> sourceTile, Placement placement) {
|
||||
Map<Pair<Integer, Integer>, Region> regionCache = new HashMap<>();
|
||||
Set<Pair<Integer, Integer>> seen = new HashSet<>();
|
||||
LinkedHashSet<Pair<Integer, Integer>> currentTile = new LinkedHashSet<>();
|
||||
currentTile.add(sourceTile);
|
||||
while (!currentTile.isEmpty() && currentTile.size() < (placement.widthX * 2 / 19) * (placement.widthZ * 2 / 19)) {
|
||||
Pair<Integer, Integer> tile = currentTile.removeFirst();
|
||||
if (!seen.add(tile)) continue;
|
||||
if (canPlace(tile, placement, regionCache)) {
|
||||
return tile;
|
||||
}
|
||||
|
||||
for (int dx = -1; dx <= 1; dx++) {
|
||||
for (int dz = -1; dz <= 1; dz++) {
|
||||
if (dx == 0 && dz == 0) continue;
|
||||
Pair<Integer, Integer> nextTile = new Pair<>(tile.getKey() + dx, tile.getValue() + dz);
|
||||
Region region = regionCache.computeIfAbsent(nextTile, tilePair -> Region.getRegion(TileUtils.toMinLocation(tilePair.getKey(), tilePair.getValue())));
|
||||
if (region.getType().isGlobal()) currentTile.add(nextTile);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean canPlace(Pair<Integer, Integer> tile, Placement placement, Map<Pair<Integer, Integer>, Region> regionCache) {
|
||||
for (int x = tile.getKey(); x < tile.getKey() + placement.widthX / TileUtils.tileSize; x++) {
|
||||
for (int z = tile.getValue(); z < tile.getValue() + placement.widthZ / TileUtils.tileSize; z++) {
|
||||
Region region = regionCache.computeIfAbsent(new Pair<>(x, z), tilePair -> Region.getRegion(TileUtils.toMinLocation(tilePair.getKey(), tilePair.getValue())));
|
||||
if (region.getType().isGlobal()) continue;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@RequiredArgsConstructor
|
||||
public enum Placement {
|
||||
Path(PathRegion::new, 19, 19),
|
||||
WarGear21(WarGear21Region::new, WarGear21Region.widthX, WarGear21Region.widthZ),
|
||||
MiniWarGear21(MiniWarGear21Region::new, MiniWarGear21Region.widthX, MiniWarGear21Region.widthZ),
|
||||
WarShip21(WarShip21Region::new, WarShip21Region.widthX, WarShip21Region.widthZ),
|
||||
MicroWarGear21(MicroWarGear21Region::new, MicroWarGear21Region.widthX, MicroWarGear21Region.widthZ),
|
||||
;
|
||||
|
||||
private final BiFunction<Integer, Integer, DynamicRegion> constructor;
|
||||
private final int widthX;
|
||||
private final int widthZ;
|
||||
}
|
||||
|
||||
@Register({"dynamic", "delete"})
|
||||
|
||||
Reference in New Issue
Block a user