ActionsApi.queueForProduction(queueType, objName, objType, quantity)方法說明與使用範例
Copyright Notice: This article is an original work licensed under the CC 4.0 BY-NC-ND license.
If you wish to repost this article, please include the original source link and this copyright notice.
Source link: https://v2know.com/article/1193
以下程式碼來自 @chronodivide/game-api
的型別定義檔,ActionsApi
類別中公開了 queueForProduction
方法。它能讓外部程式要求遊戲把某個物件加入指定生產佇列:
6 sellBuilding(buildingId: number): void;
7 toggleRepairWrench(buildingId: number): void;
8 toggleAlliance(toPlayer: string, enabled: boolean): void;
9 pauseProduction(queueType: QueueType): void;
10 resumeProduction(queueType: QueueType): void;
11 queueForProduction(queueType: QueueType, objName: string, objType: ObjectType, quantity: number): void;
12 unqueueFromProduction(queueType: QueueType, objName: string, objType: ObjectType, quantity: number): void;
生產佇列的狀態與類型定義如下:
1086 export declare interface QueueData {
1087 size: number;
1088 maxSize: number;
1089 status: QueueStatus;
1090 type: QueueType;
1091 items: QueueItemData[];
1092 }
1094 export declare interface QueueItemData {
1095 rules: TechnoRules;
1096 quantity: number;
1097 }
1099 export declare enum QueueStatus {
1100 Idle = 0,
1101 Active = 1,
1102 /** Production is paused */
1103 OnHold = 2,
1104 /** First item is ready and is waiting to be removed before starting the next */
1105 Ready = 3
1106 }
1108 export declare enum QueueType {
1109 Structures = 0,
1110 Armory = 1,
1111 Infantry = 2,
1112 Vehicles = 3,
1113 Aircrafts = 4,
1114 Ships = 5
1115 }
ObjectType
列舉定義了可排入佇列的物件種類:
862 export declare enum ObjectType {
863 None = 0,
864 Aircraft = 1,
865 Building = 2,
866 Infantry = 3,
867 Overlay = 4,
868 Smudge = 5,
869 Terrain = 6,
870 Vehicle = 7,
871 Animation = 8,
872 Projectile = 9,
873 VoxelAnim = 10,
874 Debris = 11
875 }
方法在 AI 中的實際用法
queueForProduction
主要由 AI 的建造邏輯來呼叫。以下節錄自 src/bot/logic/building/queueController.ts
:
134 const myCredits = playerData.credits;
135
136 const queueData = productionApi.getQueueData(queueType);
137 if (queueData.status == QueueStatus.Idle) {
138 // Start building the decided item.
139 if (decision !== undefined) {
140 logger(`Decision (${queueTypeToName(queueType)}): ${decision.unit.name}`);
141 actionsApi.queueForProduction(queueType, decision.unit.name, decision.unit.type, 1);
142 }
當生產佇列處於 Idle
狀態,且決策邏輯挑選出要生產的目標時,AI 會以 queueForProduction
將該單位排入佇列。若佇列已在生產其他單位,或玩家資源不足,則不會呼叫此方法。
作用與流程
-
指定佇列:
queueType
決定要向哪一條生產線新增物件,例如QueueType.Infantry
、QueueType.Vehicles
等。 -
物件資訊:
objName
為規則檔中的名稱(如"E1"
表示盟軍的 GI),objType
則是前述ObjectType
枚舉中的種類。 -
數量:
quantity
表示同時排入多少個該物件。
送出 UpdateQueue
:從模組編譯後的程式碼片段可看出,方法會組合 UpdateQueue
的資料並送出:
...UpdateQueue,e=>{e.queueType=t,e.updateType=pc.Add,e.item=r,e.quantity=s}
其中 updateType=pc.Add
表示要加入佇列。
範例
以下示範如何在腳本中手動排入 5 隻步兵(GI)到步兵佇列:
import { ActionsApi, QueueType, ObjectType } from "@chronodivide/game-api";
function produceInfantry(actions: ActionsApi) {
actions.queueForProduction(
QueueType.Infantry, // 選擇步兵生產線
"E1", // GI 的規則名稱
ObjectType.Infantry, // 物件類型
5 // 同時排入 5 個
);
}
在遊戲執行過程中,如果這條佇列有足夠的空間且資源允許,這 5 隻步兵會按序進入生產流程。佇列資訊可透過 getQueueData(queueType)
取得,包含目前狀態、已排物件和數量等。
總結
queueForProduction
提供了將任意可建造物件加入指定生產佇列的能力,AI 或腳本可依照自身邏輯控制生產優先度、數量與時機。此方法不會立即完成建造,只是將項目加入佇列,後續還會依照佇列狀態和資源狀況決定何時實際製造或是否需要暫停。
→返回《@chronodivide/game-api 使用教學與完整 API 對照表》
This article was last edited at