MapApi.findPath(speedType, subCell, from, to, options?)

| Chrono Divide | 1 Reads

findPathMapApi 提供的路徑搜尋函式,其宣告位於 node_modules/@chronodivide/game-api/dist/index.d.ts

介面說明如下:

getTilesInRect(baseTile: Tile, size: Size): Tile[];
...
/**
 * Returns the path between two points on the map for the given SpeedType.
 *
 * Should only be used with ground, non-teleporting units.
 * This method has a big performance penalty and should be used with care.
 */
findPath(speedType: SpeedType, subCell: boolean, from: PathNode, to: PathNode, options?: PathFinderOptions): PathNode[];

/** @deprecated Use overload with explicit subCell instead */
findPath(speedType: SpeedType, from: PathNode, to: PathNode, options?: PathFinderOptions): PathNode[];

相關資料結構也在同檔定義:

  • PathFinderOptions

  • PathNode

  • SpeedType 列舉值

壓縮後的實作顯示,findPath 會呼叫內部的 map.terrain.computePath,並依 options 設定(如 bestEffortexcludeNodesmaxExpandedNodes)組合路徑,最後回傳 PathNode 陣列
【F:node_modules/@chronodivide/game-api/dist/index.js†byte787460-787910】

findPath(e,...t){
    const [subCell, from, to, opts] =
        typeof t[0] !== "boolean" ? [e === ti.Foot, ...t] : t;
    let nodes = Op(this,Ip,"f").map.terrain.computePath(
        e, subCell, from.tile, from.onBridge, to.tile, to.onBridge,
        {
            bestEffort: opts?.bestEffort,
            excludeTiles: opts?.excludeNodes ? 
                n => opts.excludeNodes({ tile: n.tile, onBridge: !!n.onBridge }) :
                undefined,
            maxExpandedNodes: opts?.maxExpandedNodes
        }
    );
    return nodes.map(n => ({ tile: n.tile, onBridge: !!n.onBridge }));
}

使用範例

下例在遊戲開始時嘗試為步兵計算從基地到某座標的路徑。若路徑搜尋失敗且 bestEfforttrue,將回傳部分結果。

import { cdapi, Bot, GameApi, SpeedType, PathNode } from "@chronodivide/game-api";

class PathTestBot extends Bot {
    onGameStart(game: GameApi) {
        const startTile = game.mapApi.getTile(30, 30);
        const destTile = game.mapApi.getTile(80, 40);
        if (!startTile || !destTile) return;

        const from: PathNode = { tile: startTile, onBridge: false };
        const to: PathNode = { tile: destTile, onBridge: false };

        // 設定搜尋上限及允許部分路徑
        const path = game.mapApi.findPath(
            SpeedType.Foot,
            true,        // 考慮次格
            from,
            to,
            { maxExpandedNodes: 1000, bestEffort: true }
        );

        console.log("Path length:", path.length);
        path.forEach((node, idx) =>
            console.log(`${idx}: (${node.tile.rx},${node.tile.ry}) bridge=${node.onBridge}`)
        );
    }
}

async function main() {
    await cdapi.init(process.env.MIX_DIR!);
    await cdapi.createGame({
        agents: [new PathTestBot("Scout", "Americans")],
        mapName: "mp03t4.map",
        shortGame: true,
        online: false,
    });
}
main().catch(console.error);

說明

  • speedType 決定使用何種移動模式(步行、履帶車、船隻…等)。

  • subCell 指定是否在 tile 內精確到「次格」。若省略,SpeedType.Foot 會預設為 true

  • fromto 都是 PathNode,包含起始/目標格與是否處在橋面。

  • options 可限制搜尋範圍 (maxExpandedNodes)、允許回傳部分路徑 (bestEffort),或以 excludeNodes 動態排除節點。

  • findPath 會回傳包含每一個節點(tile 與 onBridge 狀態)的陣列;若啟用 bestEffort 且目標無法到達,也會回傳目前找到的最接近路徑。

  • 由於路徑搜尋成本較高,官方註解建議僅在必要時使用,並盡量搭配限制條件以避免過度消耗計算資源。以上範例展示了基本呼叫流程,並說明各參數在實際應用中的意義。

 

→返回《@chronodivide/game-api 使用教學與完整 API 對照表》

This article was last edited at