GameApi.getGeneralRules()

| Chrono Divide | 3 Reads

以下是一個範例,說明 GameApi 如何公開遊戲的 INI 資料。
這些輔助方法的宣告出現在 node_modules/@chronodivide/game-api/dist/index.d.ts 檔案中:

352  getAllSuperWeaponData(): SuperWeaponData[];
353  /** 回傳一個包裝 rules.ini 中 [General] 區段的資料結構 */
354  getGeneralRules(): GeneralRules;
355  getRulesIni(): IniFile;
356  getArtIni(): IniFile;
357  getAiIni(): IniFile;

部分壓縮後的實作證實每個呼叫都只是轉發請求到底層的 rules/INI 管理器:

... getAllSuperWeaponData(){ ... }).flat()}
getGeneralRules(){return Mp(this,Lp,"f").rules.general}
getRulesIni(){return Mp(this,Lp,"f").rules.getIni()}
getArtIni(){return Mp(this,Lp,"f").art.getIni()}
getAiIni(){return Mp(this,Lp,"f").ai.getIni()}

所回傳的 GeneralRules 物件包含從 rules.ini[General] 區段中解析出來的許多欄位,包括建造基地建築場(Construction Yard)所需單位的列表:

   alliedDisguise: string;
   baseUnit: string[];
   bridgeVoxelMax: number;
   /** 通用建造速度(以分鐘為單位,建造一個 1000 金額成本項目所需時間) */
   buildSpeed: number;
   /** 建築的建造動畫平均所需時間(分鐘) */
   buildupTime: number;
   chronoDelay: number;
   ...

getRulesIni()getArtIni()getAiIni() 分別回傳一個 IniFile 包裝器。
IniFile 可用來查詢和合併 INI 區段:

export declare class IniFile {
    getOrCreateSection(name: string): IniSection;
    getSection(name: string): IniSection | undefined;
    mergeWith(ini: IniFile): this;
    ...
}

範例:檢查 rules.iniart.iniai.ini

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

class RuleDumpBot extends Bot {
    onGameStart(game: GameApi) {
        // 1) 直接存取 [General] 區段
        const general = game.getGeneralRules();
        console.log("Base deploy units:", general.baseUnit.join(", "));

        // 2) 從 rules.ini 讀取任意區段
        const rulesIni: IniFile = game.getRulesIni();
        const e1Rules = rulesIni.getSection("E1");
        if (e1Rules) {
            console.log("Cost of E1:", e1Rules.getNumber("Cost", 0));
        }

        // 3) 從 art.ini 查詢單位的美術屬性
        const artIni = game.getArtIni();
        const prismArt = artIni.getSection("GAPRAG");
        if (prismArt) {
            console.log("Prism tower sequence:",
                prismArt.getString("Sequence", "none"));
        }

        // 4) 輸出 ai.ini 的一部分
        const aiIni = game.getAiIni();
        const teamTypes = aiIni.getSection("TeamTypes");
        if (teamTypes) {
            console.log("First team type key:", teamTypes.getOrderedSections()[0]?.name);
        }
    }
}

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

說明:

  • getGeneralRules() 提供一個已解析的 GeneralRules 物件,讓 bot 能檢查如 baseUnit 等重要全域參數。

  • getRulesIni()getArtIni()getAiIni() 分別提供代表其各自設定檔完整內容的 IniFile

  • 透過 IniFile 的輔助方法(如 getSectiongetStringgetNumber 等),bot 可擷取任何特定條目。

當 bot 需要超出基本 API 所提供內容的單位數值或 AI 行為詳細資訊時,這些方法就非常有用。在比賽開始時使用這些方法,可以快取規則資料供日後決策使用。

 

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

This article was last edited at