ProductionApi.getQueueTypeForObject(objRules)

| Chrono Divide | 3 Reads

ProductionApi.getQueueTypeForObject(objRules) 會根據物件的規則判斷它該被放入哪一種生產佇列。
在套件的型別宣告中可看到此方法及相關列舉:

export declare class ProductionApi {
    #private;
    isAvailableForProduction(objRules: TechnoRules): boolean;
    getAvailableObjects(queueType?: QueueType): TechnoRules[];
    getQueueTypeForObject(objRules: TechnoRules): QueueType;
    getQueueData(queueType: QueueType): QueueData;
}

QueueType 的定義如下:

export declare enum QueueType {
    Structures = 0,
    Armory = 1,
    Infantry = 2,
    Vehicles = 3,
    Aircrafts = 4,
    Ships = 5
}

ObjectTypeBuildCat(建築分類)分別為:

export declare enum ObjectType {
    None = 0,
    Aircraft = 1,
    Building = 2,
    Infantry = 3,
    Overlay = 4,
    Smudge = 5,
    Terrain = 6,
    Vehicle = 7,
    Animation = 8,
    Projectile = 9
}

export declare enum BuildCat {
    Combat = 0,
    Tech = 1,
    Resource = 2,
    Power = 3
}

在編譯後的 index.js 中,getQueueTypeForObject 的核心邏輯如下:

getQueueTypeForObject(e){
    if (e.type === Et.Building)
        return e.buildCat === es.Combat ? wn.Armory : wn.Structures;
    if (e.type === Et.Infantry)
        return wn.Infantry;
    if (e.type === Et.Vehicle)
        return e.naval ? wn.Ships : wn.Vehicles;
    if (e.type === Et.Aircraft)
        return wn.Aircrafts;
    throw new Error(Unsupported object type ${Et[e.type]})
}

【F:node_modules/@chronodivide/game-api/dist/index.js†byte634320-634800】


簡單來說,方法依物件的 type 與建築的 buildCat(若適用)判定該物件應屬於:

  • Structures:非戰鬥類建築

  • Armory:戰鬥類建築(buildCatCombat

  • Infantry:步兵

  • Vehicles:陸上載具

  • Ships:若載具具備 naval 屬性

  • Aircrafts:飛機

若物件類型不在上述範圍,則會拋出錯誤。


使用範例

下列 Bot 會在開局時列出幾個物件各自對應的佇列類型:

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

class QueueInspectorBot extends Bot {
    onGameStart(game: GameApi) {
        const prod = game.productionApi;
        const rules = game.rulesApi;

        const samples = [
            rules.getObject("GAPOWR", ObjectType.Building),  // 發電廠
            rules.getObject("E1", ObjectType.Infantry),      // 步槍兵
            rules.getObject("GTNK", ObjectType.Vehicle),     // 重型坦克
            rules.getObject("HORV", ObjectType.Aircraft),    // 直昇機
        ];

        for (const r of samples) {
            const q = prod.getQueueTypeForObject(r);
            console.log(`${r.name} -> ${QueueType[q]}`);
        }
    }
}

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

程式中先透過 rulesApi.getObject 取得各種規則物件,再以 productionApi.getQueueTypeForObject 判定它們屬於哪條生產線並印出結果。
依照物件類型與建築分類,方法會分別回傳 StructuresArmoryInfantryVehiclesAircraftsShips
這能幫助 Bot 決定要將單位送入哪個佇列建造,或是依佇列性質分配資源。

 

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

This article was last edited at