ActionsApi.orderUnits(unitIds, orderType, ...)方法說明與使用範例

| Chrono Divide | 8 Reads

ActionsApi 來自外部函式庫 @chronodivide/game-api。在下載的套件 chronodivide-game-api-0.72.0.tgz 中,TypeScript 的宣告列出了 orderUnits 的多載版本:

orderUnits(unitIds: number[], orderType: OrderType): void;
orderUnits(unitIds: number[], orderType: OrderType, targetUnit: number): void;
orderUnits(unitIds: number[], orderType: OrderType, rx: number, ry: number, onBridge?: boolean): void;

OrderType 列舉

OrderType 列舉了不同種類的指令:

export declare enum OrderType {
    Move = 0,
    ForceMove = 1,
    Attack = 2,
    ForceAttack = 3,
    AttackMove = 4,
    Guard = 5,
    GuardArea = 6,
    Capture = 7,
    Occupy = 8,
    Deploy = 9,
    DeploySelected = 10,
    Stop = 11,
    Cheer = 12,
    Dock = 13,
    Gather = 14,
    Repair = 15,
    Scatter = 16,
    EnterTransport = 17,
    PlaceBomb = 18
}

使用範例(Example Usage)

1. RetreatMission – 撤退任務

藉由發送 AttackMove 到座標,讓一組單位撤退:

actionsApi.orderUnits(
    this.getUnitIds(),
    OrderType.AttackMove,
    this.retreatToPoint.x,
    this.retreatToPoint.y,
);

2. EngineerMission – 工程師佔領建築

命令工程師透過目標物件 ID 來佔領建築:

actionsApi.orderUnits(
    engineers.map((engineer) => engineer.id),
    OrderType.Capture,
    this.captureTargetId,
);

3. ScoutingMission – 偵查任務

命令個別單位向偵查目標移動:

actionsApi.orderUnits(
    [unit.id],
    OrderType.AttackMove,
    this.scoutTarget.x,
    this.scoutTarget.y
);

4. ActionBatcher – 指令批次器

將多個單位指令批次成單一呼叫,依指令類型與目標分組:

actionsApi.orderUnits(
    unitCommands.map((command) => command.unitId),
    commandType,
    vector.x,
    vector.y,
);

方法運作原理(How the Method Works)

根據壓縮後的實作,orderUnits 的運作流程如下:

  1. 先發送一個 SelectUnits 動作,帶入提供的單位 ID。

  2. 建立一個內部目標物件(可以是來自單位 ID,或是地圖座標)。

  3. 最後發送一個 OrderUnits 動作,指定 OrderType 與目標。

如果給定的是瓦片座標且 onBridgetrue,它會在建立目標之前解析橋面瓦片。


總結(Summary)

ActionsApi.orderUnits 是用於向 Chrono Divide 機器人 API 中單位下達指令的核心方法。它支援三種多載版本:

  • 無目標版本
    僅傳入指令類型(例如 OrderType.Stop
    → 影響已選擇的單位,無需目標。

  • 目標單位版本
    傳入一個目標單位 ID
    → 用於像是 AttackRepair 這類動作。

  • 座標版本
    傳入地圖座標 (rx, ry) 與可選的 onBridge 標誌
    → 用於移動、佔領、攻擊等區域性行為。

機器人程式碼透過這個方法控制任務中單位的行為,例如:

  • 撤退(Retreat)

  • 佔領建築(Capture)

  • 偵查(Scout)

  • 協同攻擊(Attack via ActionBatcher

每次呼叫都會選取指定單位並在一次動作中下達指令,是實作任務邏輯的基礎工具。

 

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

This article was last edited at