java
import java.util.*;
public class ExtendedAStar {
public static List<BlockPos> findPath(World world, BlockPos start, BlockPos end, double[][] costMap) {
PriorityQueue<Node> openSet = new PriorityQueue<>(Comparator.comparingDouble(n -> n.f));
Map<BlockPos, Node> cameFrom = new HashMap<>();
double[][] gScore = new double[costMap.length][costMap[0].length]; // 初期化: 無限大
for (int i = 0; i < gScore.length; i++) Arrays.fill(gScore[i], Double.POSITIVE_INFINITY);
gScore[start.getX()][start.getZ()] = 0; // スタート位置
openSet.add(new Node(start, 0, heuristic(start, end)));
while (!openSet.isEmpty()) {
Node current = openSet.poll();
if (current.pos.equals(end)) {
return reconstructPath(cameFrom, current.pos);
}
for (BlockPos neighbor : getNeighbors(world, current.pos)) { // 近傍取得
double tentativeG = gScore[current.pos.getX()][current.pos.getZ()] + costMap[neighbor.getX()][neighbor.getZ()];
if (tentativeG < gScore[neighbor.getX()][neighbor.getZ()]) {
cameFrom.put(neighbor, current);
gScore[neighbor.getX()][neighbor.getZ()] = tentativeG;
double fScore = tentativeG + heuristic(neighbor, end) + visibilityBonus(neighbor, end, world); // 視界ボーナス追加
openSet.add(new Node(neighbor, gScore[neighbor.getX()][neighbor.getZ()], fScore));
}
}
}
return null; // パスなし
}
private static double heuristic(BlockPos a, BlockPos b) {
return Math.sqrt(Math.pow(a.getX() - b.getX(), 2) + Math.pow(a.getZ() - b.getZ(), 2)); // ユークリッド距離
}
private static double visibilityBonus(BlockPos a, BlockPos b, World world) {
// 視界が良い場合ボーナス(例: RayTraceで遮蔽がないかチェック)
if (!world.rayTraceBlocks(a, b).typeOfHit.equals("BLOCK")) {
return -0.5; // 視界良好ボーナス
}
return 0;
}
// Nodeクラスと他のヘルパー方法は省略
}