557 行
26 KiB
Java
557 行
26 KiB
Java
/*
|
|
* 本文件由 autostore-1.0-SNAPSHOT.jar 反编译并整理而来(CFR 0.152),
|
|
* 混淆名已替换为 Minecraft 1.21.4 Yarn 可读映射,并补充中文注释。
|
|
*/
|
|
package autostore.client.planner;
|
|
|
|
import autostore.client.config.ExpansionConfig;
|
|
import autostore.client.territory.ChunkPos;
|
|
import autostore.client.territory.TerritoryModel;
|
|
import autostore.client.territory.TerritoryState;
|
|
import java.util.ArrayList;
|
|
import java.util.Comparator;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.Environment;
|
|
|
|
@Environment(value=EnvType.CLIENT)
|
|
/**
|
|
* 扩地规划器:根据当前邦国领土、配置与玩家位置,计算下一步要占领的区块列表。
|
|
*
|
|
* <p>支持三种扩地策略:
|
|
* <ul>
|
|
* <li>策略 1:方形外扩(优先填内部空洞,再向外一圈圈扩展)</li>
|
|
* <li>策略 4:绕国一圈(沿领土边界按角度均匀向外扩张)</li>
|
|
* <li>其他(2/3):直线推进(沿一个方向连续向外延伸)</li>
|
|
* </ul>
|
|
*/
|
|
public final class ExpansionPlanner {
|
|
/** 世界边界半径(区块数):Minecraft 1.21.4 默认世界边界为 ±2048 区块,此处取 1280 作为安全上限 */
|
|
private static final int WORLD_BORDER_CHUNK_RADIUS = 1280;
|
|
|
|
/**
|
|
* 根据配置的策略分发到对应的扩地算法。
|
|
*
|
|
* @param model 当前邦国领土模型(含所有邦国已占领区块)
|
|
* @param config 扩地配置(策略、目标数量、区域范围等)
|
|
* @param playerChunk 玩家当前所在区块
|
|
* @return 建议占领的区块列表(按优先级排序)
|
|
*/
|
|
public List<ChunkPos> generate(TerritoryModel model, ExpansionConfig config, ChunkPos playerChunk) {
|
|
if (config.strategy == 1) {
|
|
return this.generateHolePriority(model, config, playerChunk);
|
|
}
|
|
if (config.strategy == 4) {
|
|
return this.generatePerimeter(model, config, playerChunk);
|
|
}
|
|
// 策略 2 区域扩地:玩家圈选了矩形时,完全按矩形区域扩地
|
|
if (config.strategy == 2 && config.regionMaxX >= config.regionMinX) {
|
|
return this.generateRectangle(model, config, playerChunk);
|
|
}
|
|
return this.generateLine(model, config, playerChunk);
|
|
}
|
|
|
|
/**
|
|
* 策略 1:方形外扩(优先填洞)。
|
|
*
|
|
* <p>以王城为中心,先计算当前领土的覆盖半径,然后从内向外逐圈扫描候选区块。
|
|
* 候选区块必须:未被任何邦国占领、与已有领土相邻(合法)、且在配置的区域内。
|
|
* 找到候选后优先排"领土包围圈内部"的空洞,再排外部边界,最后按距离与邻居数排序。
|
|
*/
|
|
private List<ChunkPos> generateHolePriority(TerritoryModel model, ExpansionConfig config, ChunkPos playerChunk) {
|
|
Set<ChunkPos> actualOwn = this.ownChunks(model);
|
|
Set<ChunkPos> allClaimed = model.allClaimedChunks();
|
|
if (actualOwn.isEmpty()) {
|
|
return List.of();
|
|
}
|
|
int limit = config.targetCount > 0 ? config.targetCount : 256;
|
|
// 以己方领土的外接矩形为基准,逐圈向外扩展搜索,保证候选区块与领土相邻且覆盖完整
|
|
int[] ownBounds = this.bounds(actualOwn);
|
|
int maxExpansions = config.maxRings > 0 ? config.maxRings + 1 : 16;
|
|
for (int expansion = 0; expansion < maxExpansions; ++expansion) {
|
|
HashSet<ChunkPos> candidates = new HashSet<ChunkPos>();
|
|
int minX = ownBounds[0] - expansion;
|
|
int maxX = ownBounds[1] + expansion;
|
|
int minZ = ownBounds[2] - expansion;
|
|
int maxZ = ownBounds[3] + expansion;
|
|
for (int x = minX; x <= maxX; ++x) {
|
|
for (int z = minZ; z <= maxZ; ++z) {
|
|
ChunkPos chunk = new ChunkPos(x, z);
|
|
if (actualOwn.contains(chunk) || !this.isLegal(chunk, actualOwn, allClaimed) || !this.isInRegion(chunk, config)) continue;
|
|
candidates.add(chunk);
|
|
}
|
|
}
|
|
if (!candidates.isEmpty()) {
|
|
Comparator<ChunkPos> comparator = Comparator.comparingInt(playerChunk::manhattanDistance).thenComparingInt(chunk -> -this.ownNeighbors(chunk, actualOwn));
|
|
ArrayList<ChunkPos> inside = new ArrayList<ChunkPos>();
|
|
ArrayList<ChunkPos> outside = new ArrayList<ChunkPos>();
|
|
for (ChunkPos chunk : candidates) {
|
|
if (chunk.x() >= ownBounds[0] && chunk.x() <= ownBounds[1] && chunk.z() >= ownBounds[2] && chunk.z() <= ownBounds[3]) {
|
|
inside.add(chunk);
|
|
continue;
|
|
}
|
|
outside.add(chunk);
|
|
}
|
|
inside.sort(comparator);
|
|
outside.sort(comparator);
|
|
ArrayList<ChunkPos> result = new ArrayList<ChunkPos>();
|
|
result.addAll(inside);
|
|
result.addAll(outside);
|
|
return result.stream().limit(limit).toList();
|
|
}
|
|
}
|
|
return List.of();
|
|
}
|
|
|
|
/**
|
|
* 策略 4:绕国一圈。
|
|
*
|
|
* <p>收集所有与己方领土相邻的合法区块,以领土质心(或王城)为中心,
|
|
* 按方位角(atan2)排序,使扩张点均匀分布在领土四周,形成一圈向外扩散的效果。
|
|
*/
|
|
private List<ChunkPos> generatePerimeter(TerritoryModel model, ExpansionConfig config, ChunkPos playerChunk) {
|
|
Set<ChunkPos> actualOwn = this.ownChunks(model);
|
|
Set<ChunkPos> allClaimed = model.allClaimedChunks();
|
|
HashSet<ChunkPos> plannedOwn = new HashSet<ChunkPos>(actualOwn);
|
|
HashSet<ChunkPos> candidates = new HashSet<ChunkPos>();
|
|
ChunkPos center = this.capital(model, this.centroid(actualOwn, playerChunk));
|
|
int limit = config.targetCount > 0 ? config.targetCount : 128;
|
|
// 支持多圈扩张:每圈取当前领土的合法邻居,按方位角均匀排序后入队
|
|
int rings = config.maxRings > 0 ? config.maxRings : 1;
|
|
ArrayList<ChunkPos> result = new ArrayList<ChunkPos>();
|
|
for (int ring = 0; ring < rings && result.size() < limit; ++ring) {
|
|
this.addNeighbors(plannedOwn, candidates, allClaimed, config);
|
|
if (candidates.isEmpty()) break;
|
|
Comparator<ChunkPos> comparator = Comparator.comparingDouble((ChunkPos chunk) -> this.angle(chunk, center)).thenComparingInt(chunk -> chunk.manhattanDistance(playerChunk));
|
|
List<ChunkPos> ringChunks = candidates.stream().sorted(comparator).limit((long)(limit - result.size())).toList();
|
|
result.addAll(ringChunks);
|
|
plannedOwn.addAll(ringChunks);
|
|
candidates.clear();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 策略 2/3:直线推进。
|
|
*
|
|
* <p>从玩家当前位置出发,沿一个方向连续向外延伸占领区块(形成一条"线")。
|
|
* 每步从候选集中挑选评分最优的区块:距离越近、越贴合当前方向、邻居越多、形状越规整越好。
|
|
* 当领土形状严重失衡(长宽比超过 2:1)时,会切换方向补短边,避免领土过于细长。
|
|
*/
|
|
private List<ChunkPos> generateLine(TerritoryModel model, ExpansionConfig config, ChunkPos playerChunk) {
|
|
Set<ChunkPos> actualOwn = this.ownChunks(model);
|
|
Set<ChunkPos> allClaimed = model.allClaimedChunks();
|
|
HashSet<ChunkPos> plannedOwn = new HashSet<ChunkPos>(actualOwn);
|
|
HashSet<ChunkPos> candidates = new HashSet<ChunkPos>();
|
|
// 以己方领土中离玩家最近的区块作为推进起点,保证扩张从领土边缘开始
|
|
ChunkPos cursor = this.nearestOwnToPlayer(actualOwn, playerChunk);
|
|
ChunkPos direction = null;
|
|
int limit = config.targetCount > 0 ? config.targetCount : 128;
|
|
this.addNeighbors(plannedOwn, candidates, allClaimed, config);
|
|
ArrayList<ChunkPos> result = new ArrayList<ChunkPos>();
|
|
// 本次扩张新增区块的边界(用于形状判断,避免被原有领土形状干扰)
|
|
int expMinX = Integer.MAX_VALUE;
|
|
int expMaxX = Integer.MIN_VALUE;
|
|
int expMinZ = Integer.MAX_VALUE;
|
|
int expMaxZ = Integer.MIN_VALUE;
|
|
while (result.size() < limit) {
|
|
ChunkPos nearest;
|
|
ChunkPos best;
|
|
if (candidates.isEmpty()) {
|
|
this.addNeighbors(plannedOwn, candidates, allClaimed, config);
|
|
if (candidates.isEmpty()) break;
|
|
}
|
|
nearest = this.nearestFrontier(candidates, cursor);
|
|
// 首次迭代确定推进方向;之后保持方向稳定,避免来回摆动
|
|
if (direction == null) {
|
|
direction = this.chooseDirection(cursor, nearest, plannedOwn);
|
|
}
|
|
best = this.chooseLineCandidate(candidates, plannedOwn, cursor, direction);
|
|
if (best == null) {
|
|
best = nearest;
|
|
}
|
|
candidates.remove(best);
|
|
result.add(best);
|
|
plannedOwn.add(best);
|
|
cursor = best;
|
|
expMinX = Math.min(expMinX, best.x());
|
|
expMaxX = Math.max(expMaxX, best.x());
|
|
expMinZ = Math.min(expMinZ, best.z());
|
|
expMaxZ = Math.max(expMaxZ, best.z());
|
|
this.addNeighbors(best, plannedOwn, candidates, allClaimed, config);
|
|
// 本次扩张形状失衡时转向补短边,并保持该方向
|
|
if (this.expansionImbalanced(expMinX, expMaxX, expMinZ, expMaxZ)) {
|
|
direction = this.shortSideDirection(expMinX, expMaxX, expMinZ, expMaxZ);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 策略 2 矩形区域扩地:在玩家圈选的矩形内,从与现有领土接壤的区块开始,
|
|
* 横向或纵向 S 形扫过整个矩形。
|
|
*
|
|
* <p>与旧版"沿邦国边界逐层向内 BFS"不同,本算法不再贴着邦国现有边界,
|
|
* 而是生成一条贯穿矩形的 S 形路径:从接壤起点开始,逐行/逐列交替反向扫过,
|
|
* 保证路径上每个区块都与已占领领土(含己方领土、本次会话新占领)接壤,
|
|
* 矩形内已占领的区块自动跳过、不重复占领。
|
|
*/
|
|
private List<ChunkPos> generateRectangle(TerritoryModel model, ExpansionConfig config, ChunkPos playerChunk) {
|
|
Set<ChunkPos> actualOwn = this.ownChunks(model);
|
|
Set<ChunkPos> allClaimed = model.allClaimedChunks();
|
|
if (actualOwn.isEmpty()) {
|
|
return List.of();
|
|
}
|
|
int minX = config.regionMinX;
|
|
int maxX = config.regionMaxX;
|
|
int minZ = config.regionMinZ;
|
|
int maxZ = config.regionMaxZ;
|
|
int limit = config.targetCount > 0 ? config.targetCount : Integer.MAX_VALUE;
|
|
// 找出矩形内所有与己方领土接壤的未占领区块(作为 S 形起点候选)
|
|
ArrayList<ChunkPos> borderChunks = new ArrayList<ChunkPos>();
|
|
for (int x = minX; x <= maxX; ++x) {
|
|
for (int z = minZ; z <= maxZ; ++z) {
|
|
ChunkPos chunk = new ChunkPos(x, z);
|
|
if (allClaimed.contains(chunk)) continue;
|
|
if (this.ownNeighbors(chunk, actualOwn) > 0) {
|
|
borderChunks.add(chunk);
|
|
}
|
|
}
|
|
}
|
|
if (borderChunks.isEmpty()) {
|
|
return List.of();
|
|
}
|
|
// 选择起点:优先矩形角落附近与领土接壤的区块,使 S 形能自然覆盖整个矩形
|
|
ChunkPos start = this.chooseStart(borderChunks, minX, maxX, minZ, maxZ);
|
|
// 生成 S 形扫描路径(覆盖矩形内所有区块,含已占领的)
|
|
List<ChunkPos> sPath = this.buildSPath(start, minX, maxX, minZ, maxZ);
|
|
// 已占领集合:己方领土 + 所有已占领 + 本次规划已纳入的区块
|
|
HashSet<ChunkPos> claimed = new HashSet<ChunkPos>(actualOwn);
|
|
claimed.addAll(allClaimed);
|
|
ArrayList<ChunkPos> result = new ArrayList<ChunkPos>();
|
|
for (ChunkPos chunk : sPath) {
|
|
if (result.size() >= limit) break;
|
|
if (claimed.contains(chunk)) continue;
|
|
// 必须与已占领领土接壤,保证能够开拓
|
|
if (this.ownNeighbors(chunk, claimed) == 0) continue;
|
|
result.add(chunk);
|
|
claimed.add(chunk);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 从接壤候选区块中选择 S 形扫描的起点:
|
|
* 优先选择矩形角落上的接壤区块;没有角落接壤时,选择离任一角落最近的接壤区块。
|
|
*/
|
|
private ChunkPos chooseStart(List<ChunkPos> borderChunks, int minX, int maxX, int minZ, int maxZ) {
|
|
ChunkPos[] corners = new ChunkPos[]{new ChunkPos(minX, minZ), new ChunkPos(maxX, minZ), new ChunkPos(minX, maxZ), new ChunkPos(maxX, maxZ)};
|
|
for (ChunkPos corner : corners) {
|
|
if (borderChunks.contains(corner)) {
|
|
return corner;
|
|
}
|
|
}
|
|
ChunkPos best = null;
|
|
int bestDist = Integer.MAX_VALUE;
|
|
for (ChunkPos chunk : borderChunks) {
|
|
for (ChunkPos corner : corners) {
|
|
int dist = Math.abs(chunk.x() - corner.x()) + Math.abs(chunk.z() - corner.z());
|
|
if (dist < bestDist) {
|
|
bestDist = dist;
|
|
best = chunk;
|
|
}
|
|
}
|
|
}
|
|
return best;
|
|
}
|
|
|
|
/**
|
|
* 生成从起点开始的 S 形扫描路径,覆盖矩形内所有区块。
|
|
*
|
|
* <p>扫描主轴根据起点所在边缘决定:起点在顶/底行时横向逐行扫描,
|
|
* 起点在左/右列时纵向逐列扫描。每行/列内方向交替反向,形成 S 形。
|
|
* 起点所在行/列从起点向两侧铺开,保证起点是路径第一个区块。
|
|
*/
|
|
private List<ChunkPos> buildSPath(ChunkPos start, int minX, int maxX, int minZ, int maxZ) {
|
|
ArrayList<ChunkPos> path = new ArrayList<ChunkPos>();
|
|
boolean horizontal = start.z() == minZ || start.z() == maxZ;
|
|
if (horizontal) {
|
|
int startRow = start.z();
|
|
// 起点行:从起点向一侧到边缘,再反向到另一侧,完整覆盖该行
|
|
boolean leftToRight = start.x() == minX;
|
|
if (leftToRight) {
|
|
for (int x = start.x(); x <= maxX; ++x) path.add(new ChunkPos(x, startRow));
|
|
for (int x = start.x() - 1; x >= minX; --x) path.add(new ChunkPos(x, startRow));
|
|
} else {
|
|
for (int x = start.x(); x >= minX; --x) path.add(new ChunkPos(x, startRow));
|
|
for (int x = start.x() + 1; x <= maxX; ++x) path.add(new ChunkPos(x, startRow));
|
|
}
|
|
// 其余行:先向下(+z)再向上(-z),每行方向交替,形成 S 形
|
|
boolean ltr = leftToRight;
|
|
for (int z = startRow + 1; z <= maxZ; ++z) {
|
|
this.appendRow(path, z, minX, maxX, ltr);
|
|
ltr = !ltr;
|
|
}
|
|
for (int z = startRow - 1; z >= minZ; --z) {
|
|
this.appendRow(path, z, minX, maxX, ltr);
|
|
ltr = !ltr;
|
|
}
|
|
} else {
|
|
int startCol = start.x();
|
|
// 起点列:从起点向一侧到边缘,再反向到另一侧,完整覆盖该列
|
|
boolean topToBottom = start.z() == minZ;
|
|
if (topToBottom) {
|
|
for (int z = start.z(); z <= maxZ; ++z) path.add(new ChunkPos(startCol, z));
|
|
for (int z = start.z() - 1; z >= minZ; --z) path.add(new ChunkPos(startCol, z));
|
|
} else {
|
|
for (int z = start.z(); z >= minZ; --z) path.add(new ChunkPos(startCol, z));
|
|
for (int z = start.z() + 1; z <= maxZ; ++z) path.add(new ChunkPos(startCol, z));
|
|
}
|
|
// 其余列:先向右(+x)再向左(-x),每列方向交替,形成 S 形
|
|
boolean ttb = topToBottom;
|
|
for (int x = startCol + 1; x <= maxX; ++x) {
|
|
this.appendCol(path, x, minZ, maxZ, ttb);
|
|
ttb = !ttb;
|
|
}
|
|
for (int x = startCol - 1; x >= minX; --x) {
|
|
this.appendCol(path, x, minZ, maxZ, ttb);
|
|
ttb = !ttb;
|
|
}
|
|
}
|
|
return path;
|
|
}
|
|
|
|
/** 追加一行区块到路径(leftToRight=true 时从左到右,否则从右到左) */
|
|
private void appendRow(List<ChunkPos> path, int row, int minX, int maxX, boolean leftToRight) {
|
|
if (leftToRight) {
|
|
for (int x = minX; x <= maxX; ++x) path.add(new ChunkPos(x, row));
|
|
} else {
|
|
for (int x = maxX; x >= minX; --x) path.add(new ChunkPos(x, row));
|
|
}
|
|
}
|
|
|
|
/** 追加一列区块到路径(topToBottom=true 时从上到下,否则从下到上) */
|
|
private void appendCol(List<ChunkPos> path, int col, int minZ, int maxZ, boolean topToBottom) {
|
|
if (topToBottom) {
|
|
for (int z = minZ; z <= maxZ; ++z) path.add(new ChunkPos(col, z));
|
|
} else {
|
|
for (int z = maxZ; z >= minZ; --z) path.add(new ChunkPos(col, z));
|
|
}
|
|
}
|
|
|
|
/** 在候选集中找到离当前游标最近的一个区块(曼哈顿距离),作为推进的前沿 */
|
|
private ChunkPos nearestFrontier(Set<ChunkPos> candidates, ChunkPos cursor) {
|
|
return candidates.stream().min(Comparator.comparingInt(cursor::manhattanDistance)).orElse(null);
|
|
}
|
|
|
|
/**
|
|
* 计算推进方向:
|
|
* 若前沿与游标重合,则优先朝一个未占领的"正方向邻居"走;
|
|
* 否则返回从游标指向前沿的符号方向(-1/0/1)。
|
|
*/
|
|
private ChunkPos chooseDirection(ChunkPos cursor, ChunkPos nearest, Set<ChunkPos> own) {
|
|
if (nearest == null) {
|
|
return new ChunkPos(0, 0);
|
|
}
|
|
if (cursor.equals(nearest)) {
|
|
for (ChunkPos candidate : this.cardinalNeighbors(nearest)) {
|
|
if (own.contains(candidate)) continue;
|
|
return new ChunkPos(candidate.x() - nearest.x(), candidate.z() - nearest.z());
|
|
}
|
|
}
|
|
return new ChunkPos(Integer.signum(nearest.x() - cursor.x()), Integer.signum(nearest.z() - cursor.z()));
|
|
}
|
|
|
|
/**
|
|
* 直线推进的候选评分:分数越低越优先。
|
|
* 距离近、方向一致(方向点积大)、邻居多、形状影响小 的区块得分更优。
|
|
*/
|
|
private ChunkPos chooseLineCandidate(Set<ChunkPos> candidates, Set<ChunkPos> plannedOwn, ChunkPos cursor, ChunkPos direction) {
|
|
if (direction == null || direction.x() == 0 && direction.z() == 0) {
|
|
return null;
|
|
}
|
|
ChunkPos best = null;
|
|
double bestScore = Double.MAX_VALUE;
|
|
for (ChunkPos candidate : candidates) {
|
|
int dx = candidate.x() - cursor.x();
|
|
int dz = candidate.z() - cursor.z();
|
|
double directionBonus = dx * direction.x() + dz * direction.z();
|
|
double distance = cursor.manhattanDistance(candidate);
|
|
double shape = this.shapePenalty(candidate, plannedOwn);
|
|
double score = distance * 800.0 - directionBonus * 600.0 - this.ownNeighbors(candidate, plannedOwn) * 260.0 + shape * 200.0;
|
|
if (!(score < bestScore)) continue;
|
|
bestScore = score;
|
|
best = candidate;
|
|
}
|
|
return best;
|
|
}
|
|
|
|
/** 判断本次扩张的形状是否失衡:长边超过短边的 2 倍时视为"过瘦",需要转向补短边 */
|
|
private boolean expansionImbalanced(int minX, int maxX, int minZ, int maxZ) {
|
|
if (minX == Integer.MAX_VALUE) {
|
|
return false;
|
|
}
|
|
int width = maxX - minX + 1;
|
|
int height = maxZ - minZ + 1;
|
|
return Math.max(width, height) > Math.min(width, height) * 2;
|
|
}
|
|
|
|
/** 返回本次扩张短边对应的推进方向:宽 < 高 时向 X 方向补,否则向 Z 方向补 */
|
|
private ChunkPos shortSideDirection(int minX, int maxX, int minZ, int maxZ) {
|
|
int width = maxX - minX + 1;
|
|
int height = maxZ - minZ + 1;
|
|
return width < height ? new ChunkPos(1, 0) : new ChunkPos(0, 1);
|
|
}
|
|
|
|
/** 返回己方领土中离玩家最近的区块;领土为空时返回玩家所在区块 */
|
|
private ChunkPos nearestOwnToPlayer(Set<ChunkPos> own, ChunkPos playerChunk) {
|
|
if (own.isEmpty()) {
|
|
return playerChunk;
|
|
}
|
|
return own.stream().min(Comparator.comparingInt(playerChunk::manhattanDistance)).orElse(playerChunk);
|
|
}
|
|
|
|
/** 计算一组区块的外接矩形边界:[minX, maxX, minZ, maxZ] */
|
|
private int[] bounds(Set<ChunkPos> own) {
|
|
int minX = Integer.MAX_VALUE;
|
|
int maxX = Integer.MIN_VALUE;
|
|
int minZ = Integer.MAX_VALUE;
|
|
int maxZ = Integer.MIN_VALUE;
|
|
for (ChunkPos chunk : own) {
|
|
minX = Math.min(minX, chunk.x());
|
|
maxX = Math.max(maxX, chunk.x());
|
|
minZ = Math.min(minZ, chunk.z());
|
|
maxZ = Math.max(maxZ, chunk.z());
|
|
}
|
|
return new int[]{minX, maxX, minZ, maxZ};
|
|
}
|
|
|
|
/** 返回指定区块的四个正方向邻居(上下左右) */
|
|
private List<ChunkPos> cardinalNeighbors(ChunkPos chunk) {
|
|
return List.of(new ChunkPos(chunk.x() + 1, chunk.z()), new ChunkPos(chunk.x() - 1, chunk.z()), new ChunkPos(chunk.x(), chunk.z() + 1), new ChunkPos(chunk.x(), chunk.z() - 1));
|
|
}
|
|
|
|
/** 将某区块的合法邻居加入候选集(单个区块版本) */
|
|
private void addNeighbors(ChunkPos chunk, Set<ChunkPos> plannedOwn, Set<ChunkPos> candidates, Set<ChunkPos> allClaimed, ExpansionConfig config) {
|
|
for (ChunkPos candidate : this.cardinalNeighbors(chunk)) {
|
|
if (!this.isLegal(candidate, plannedOwn, allClaimed) || !this.isInRegion(candidate, config)) continue;
|
|
candidates.add(candidate);
|
|
}
|
|
}
|
|
|
|
/** 将一组区块的所有合法邻居加入候选集(批量版本) */
|
|
private void addNeighbors(Set<ChunkPos> own, Set<ChunkPos> candidates, Set<ChunkPos> allClaimed, ExpansionConfig config) {
|
|
for (ChunkPos base : own) {
|
|
this.addNeighbors(base, own, candidates, allClaimed, config);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 判断区块是否"合法"(可被占领):
|
|
* 必须在世界边界内、未被任何邦国占领、未被本次规划占用,且至少与一个己方区块相邻。
|
|
*/
|
|
private boolean isLegal(ChunkPos chunk, Set<ChunkPos> plannedOwn, Set<ChunkPos> allClaimed) {
|
|
if (chunk.x() < -1280 || chunk.x() >= 1280 || chunk.z() < -1280 || chunk.z() >= 1280) {
|
|
return false;
|
|
}
|
|
if (allClaimed.contains(chunk) || plannedOwn.contains(chunk)) {
|
|
return false;
|
|
}
|
|
return this.ownNeighbors(chunk, plannedOwn) > 0;
|
|
}
|
|
|
|
/** 判断区块是否在配置的扩地区域内(仅策略 2 区域扩地有限制,其他策略恒为 true) */
|
|
private boolean isInRegion(ChunkPos chunk, ExpansionConfig config) {
|
|
int dz;
|
|
if (config.strategy != 2) {
|
|
return true;
|
|
}
|
|
// 优先使用矩形边界(圈选功能)
|
|
if (config.regionMaxX >= config.regionMinX) {
|
|
return chunk.x() >= config.regionMinX && chunk.x() <= config.regionMaxX
|
|
&& chunk.z() >= config.regionMinZ && chunk.z() <= config.regionMaxZ;
|
|
}
|
|
// 回退到圆形区域(中心+半径)
|
|
int dx = Math.abs(chunk.x() - config.regionCenterX);
|
|
return Math.max(dx, dz = Math.abs(chunk.z() - config.regionCenterZ)) <= Math.max(1, config.regionRadius);
|
|
}
|
|
|
|
/** 统计某区块周围(上下左右)属于给定集合的邻居数量 */
|
|
private int ownNeighbors(ChunkPos chunk, Set<ChunkPos> own) {
|
|
int count = 0;
|
|
for (ChunkPos neighbor : this.cardinalNeighbors(chunk)) {
|
|
if (!own.contains(neighbor)) continue;
|
|
++count;
|
|
}
|
|
return count;
|
|
}
|
|
|
|
/**
|
|
* 形状惩罚分:评估把候选区块纳入后领土的"规整程度"。
|
|
* 用外接矩形的面积与已有区块数的比值衡量——比值越大说明扩张越"浪费"(形状越散),惩罚越高。
|
|
*/
|
|
private double shapePenalty(ChunkPos candidate, Set<ChunkPos> ownChunks) {
|
|
if (ownChunks.isEmpty()) {
|
|
return 1.0;
|
|
}
|
|
int minX = Integer.MAX_VALUE;
|
|
int maxX = Integer.MIN_VALUE;
|
|
int minZ = Integer.MAX_VALUE;
|
|
int maxZ = Integer.MIN_VALUE;
|
|
for (ChunkPos chunk : ownChunks) {
|
|
minX = Math.min(minX, chunk.x());
|
|
maxX = Math.max(maxX, chunk.x());
|
|
minZ = Math.min(minZ, chunk.z());
|
|
maxZ = Math.max(maxZ, chunk.z());
|
|
}
|
|
minX = Math.min(minX, candidate.x());
|
|
maxX = Math.max(maxX, candidate.x());
|
|
minZ = Math.min(minZ, candidate.z());
|
|
maxZ = Math.max(maxZ, candidate.z());
|
|
long width = (maxX - minX) + 1L;
|
|
long height = (maxZ - minZ) + 1L;
|
|
long longSide = Math.max(width, height);
|
|
double shortSide = Math.max(Math.min(width, height), longSide / 2.0);
|
|
double normalizedArea = longSide * shortSide;
|
|
return normalizedArea / (ownChunks.size() + 1.0);
|
|
}
|
|
|
|
/** 计算区块相对中心点的方位角(弧度),用于绕国一圈策略的均匀排序 */
|
|
private double angle(ChunkPos chunk, ChunkPos center) {
|
|
return Math.atan2(chunk.z() - center.z(), chunk.x() - center.x());
|
|
}
|
|
|
|
/** 计算一组区块的质心(坐标平均值);集合为空时回退到 fallback */
|
|
private ChunkPos centroid(Set<ChunkPos> chunks, ChunkPos fallback) {
|
|
if (chunks.isEmpty()) {
|
|
return fallback;
|
|
}
|
|
long x = 0L;
|
|
long z = 0L;
|
|
for (ChunkPos chunk : chunks) {
|
|
x += chunk.x();
|
|
z += chunk.z();
|
|
}
|
|
return new ChunkPos((int)(x / chunks.size()), (int)(z / chunks.size()));
|
|
}
|
|
|
|
/** 汇总"己方"区块:本次会话新占领的区块 + 己方邦国已拥有的区块 */
|
|
private Set<ChunkPos> ownChunks(TerritoryModel model) {
|
|
HashSet<ChunkPos> result = new HashSet<ChunkPos>(model.sessionClaims());
|
|
TerritoryState self = model.selfState();
|
|
if (self != null) {
|
|
result.addAll(self.chunks());
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/** 获取己方邦国的王城位置;未识别邦国时回退到 fallback */
|
|
private ChunkPos capital(TerritoryModel model, ChunkPos fallback) {
|
|
TerritoryState self = model.selfState();
|
|
return self == null ? fallback : self.capital();
|
|
}
|
|
}
|
|
|