如何用 Chrono Divide Bot API 建造 Pillbox — 最簡範例教學

| Chrono Divide | 4 Reads

Chrono Divide 提供了強大的 bot API,允許我們透過 TypeScript 控制 AI 進行建造、移動、攻擊等行為。不過,在官方提供的 supalosa-chronodivide-bot 倉庫中,卻缺少一個從零開始、只示範建造一個 Pillbox(GAPILL)的簡易範例。

對新手來說,若想了解建造防禦建築的流程,必須翻閱多個檔案,例如 AntiGroundStaticDefence.tsQueueController.tsbuildingRules.ts 等,學習曲線相當陡峭。

本篇將示範最小可執行的腳本,教你如何使用 API 建造一個 GAPILL,並解釋每個步驟背後的關鍵概念。


🧱 建造 Pillbox 的流程概覽

  1. 初始化 cdapi 並建立離線對戰

  2. 查詢是否能生產 GAPILL

  3. 使用 queueForProduction 將其加入建造序列

  4. 等待建造完成後,決定放置座標

  5. 使用 placeBuilding 將其放置到地圖上


💻 範例程式碼(index.ts)

import "dotenv/config";
import {
    Bot,
    cdapi,
    CreateBaseOpts,
    CreateOfflineOpts,
    QueueType,
    QueueStatus,
    ObjectType,
    GameApi,
} from "@chronodivide/game-api";
import { getStaticDefencePlacement } from "./bot/logic/building/common.js";
import { getDefaultPlacementLocation } from "./bot/logic/building/buildingRules.js";
import { Countries } from "./bot/logic/common/utils.js";

// 👷 Bot 實作:每一個 game tick 都檢查是否需要建造 Pillbox
class PillboxBot extends Bot {
    onGameTick(game: GameApi) {
        const playerData = game.getPlayerData(this.name);
        const pillboxRules = game.rulesApi.getBuilding("GAPILL");
        const queue = this.productionApi.getQueueData(QueueType.Structures);

        // 若目前閒置,則嘗試建造 Pillbox
        if (queue.status === QueueStatus.Idle) {
            if (this.productionApi.isAvailableForProduction(pillboxRules)) {
                this.actionsApi.queueForProduction(
                    QueueType.Structures,
                    pillboxRules.name,
                    ObjectType.Building,
                    1
                );
            }
        }
        // 若建造完成,則放置到合適位置
        else if (queue.status === QueueStatus.Ready && queue.items.length > 0) {
            let location = getStaticDefencePlacement(game, playerData, pillboxRules);
            if (!location) {
                location = getDefaultPlacementLocation(
                    game,
                    playerData,
                    playerData.startLocation,
                    pillboxRules
                );
            }
            if (location) {
                this.actionsApi.placeBuilding(pillboxRules.name, location.rx, location.ry);
            }
        }
    }
}

// 🧪 建立單機遊戲環境
async function main() {
    const mapName = "heckcorners_b.map";
    await cdapi.init(process.env.MIX_DIR || "./");

    const baseSettings: CreateBaseOpts = {
        buildOffAlly: false,
        cratesAppear: false,
        credits: 10000,
        gameMode: cdapi.getAvailableGameModes(mapName)[0],
        gameSpeed: 6,
        mapName,
        mcvRepacks: true,
        shortGame: true,
        superWeapons: false,
        unitCount: 0,
    };

    const opts: CreateOfflineOpts = {
        ...baseSettings,
        online: false,
        agents: [new PillboxBot("Pillboxer", Countries.USA)],
    };

    const game = await cdapi.createGame(opts);
    while (!game.isFinished()) {
        await game.update();
    }
    game.saveReplay();
    game.dispose();
}

main().catch((e) => {
    console.error(e);
    process.exit(1);
});

🔍 重點解析

this.productionApi.isAvailableForProduction(pillboxRules)

確保建築隊列允許建造 GAPILL。這通常會根據玩家是否已建造基礎建築(如Barracks)而有所限制。

getStaticDefencePlacement(...)

這是判斷防禦建築應放在哪個位置的邏輯。若無明確位置,則回退到預設起始地點。

this.actionsApi.placeBuilding(...)

在地圖的具體 tile 上放置建築。座標通常是絕對 tile 座標 (rx, ry)。


🧩 延伸閱讀與原始碼位置

  • BUILDING_NAME_TO_RULES["GAPILL"]: 建築名稱與規則對應表。

  • AntiGroundStaticDefence.ts: 包含靜態防禦建築的選址與優先級邏輯。

  • QueueController.ts: 建築排隊系統核心。


✍ 結語

這份最小範例是學習 Chrono Divide bot API 建造系統的理想起點。你可以從這裡擴充邏輯,例如建造多個 Pillbox、根據敵人位置選址、或混合多種建築物管理。

下一步,不妨試著:

  • 將建造邏輯改為觸發式(有敵人靠近才建)

  • 建造多個不同類型的防禦建築(如 GATWR


如需完整程式碼與可執行範例,可參考 GitHub 倉庫補丁提交:
🔗 https://github.com/Supalosa/supalosa-chronodivide-bot


 

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

This article was last edited at