AutoStore 模组完整源码
这个提交包含在:
@@ -0,0 +1,467 @@
|
||||
/*
|
||||
* 本文件由 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.ArrayDeque;
|
||||
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 矩形区域扩地:完全按玩家圈选的矩形区域扩地。
|
||||
*
|
||||
* <p>先检查矩形区域是否与己方领土接壤(矩形内至少有一个未占领区块与领土相邻),
|
||||
* 若未接壤则返回空列表(不扩地)。接壤后,从接壤边界开始 BFS 逐层向内,
|
||||
* 将矩形内所有未被占领的区块全部纳入计划,保证每个区块都与已占领领土连通。
|
||||
*/
|
||||
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;
|
||||
// 检查矩形是否与己方领土接壤:矩形内至少有一个未占领区块与领土相邻
|
||||
boolean bordersOwn = false;
|
||||
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) {
|
||||
bordersOwn = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (bordersOwn) break;
|
||||
}
|
||||
if (!bordersOwn) {
|
||||
return List.of();
|
||||
}
|
||||
// 初始边界:矩形内与己方领土(原始领土)直接相邻的未占领区块
|
||||
HashSet<ChunkPos> plannedOwn = new HashSet<ChunkPos>(actualOwn);
|
||||
ArrayDeque<ChunkPos> frontier = new ArrayDeque<ChunkPos>();
|
||||
ArrayList<ChunkPos> result = 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) || plannedOwn.contains(chunk)) continue;
|
||||
if (this.ownNeighbors(chunk, actualOwn) > 0) {
|
||||
plannedOwn.add(chunk);
|
||||
frontier.add(chunk);
|
||||
}
|
||||
}
|
||||
}
|
||||
// BFS 逐层向内填满整个矩形
|
||||
while (!frontier.isEmpty() && result.size() < limit) {
|
||||
ChunkPos chunk = frontier.removeFirst();
|
||||
result.add(chunk);
|
||||
for (ChunkPos neighbor : this.cardinalNeighbors(chunk)) {
|
||||
if (neighbor.x() < minX || neighbor.x() > maxX || neighbor.z() < minZ || neighbor.z() > maxZ) continue;
|
||||
if (allClaimed.contains(neighbor) || plannedOwn.contains(neighbor)) continue;
|
||||
plannedOwn.add(neighbor);
|
||||
frontier.add(neighbor);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/** 在候选集中找到离当前游标最近的一个区块(曼哈顿距离),作为推进的前沿 */
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
在新工单中引用
屏蔽一个用户