ActionsApi.pauseProduction(queueType)、ActionsApi.resumeProduction(queueType)方法說明與使用範例
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/1190
以下程式碼片段節錄自 queueController.ts
,展示了當前建造佇列(Queue)根據不同情況被暫停或恢復的邏輯:
184 // Not changing our mind, but maybe other queues are more important for now.
185 if (totalCostAcrossQueues > myCredits && decision.priority < totalWeightAcrossQueues * 0.25) {
186 logger(
187 `Pausing queue ${queueTypeToName(queueData.type)} because weight is low (${
188 decision.priority
189 }/${totalWeightAcrossQueues})`,
190 );
191 actionsApi.pauseProduction(queueData.type);
192 }
...
194 } else if (queueData.status == QueueStatus.OnHold) {
195 // Consider resuming queue if priority is high relative to other queues.
196 if (myCredits >= totalCostAcrossQueues) {
197 logger(`Resuming queue ${queueTypeToName(queueData.type)} because credits are high`);
198 actionsApi.resumeProduction(queueData.type);
199 } else if (decision && decision.priority >= totalWeightAcrossQueues * 0.25) {
200 logger(
201 `Resuming queue ${queueTypeToName(queueData.type)} because weight is high (${
202 decision.priority
203 }/${totalWeightAcrossQueues})`,
204 );
205 actionsApi.resumeProduction(queueData.type);
206 }
在此段程式碼中,QueueController
會根據全域資源狀態和佇列重要度,決定是否暫停或恢復特定生產佇列。
若總建造成本高於目前資金,且該佇列的重要度相對低(decision.priority < totalWeightAcrossQueues * 0.25
),便呼叫 actionsApi.pauseProduction(queueData.type)
暫停生產。
反之,若佇列目前處於 QueueStatus.OnHold
(暫停),當玩家擁有足夠資金,或該佇列的重要度提升到一定比例以上,便會呼叫 actionsApi.resumeProduction(queueData.type)
恢復生產。
ActionsApi 中的相關方法
TypeScript 定義檔 (index.d.ts
) 中 ActionsApi
類別公開了以下方法:
export declare class ActionsApi {
...
pauseProduction(queueType: QueueType): void;
resumeProduction(queueType: QueueType): void;
queueForProduction(queueType: QueueType, objName: string, objType: ObjectType, quantity: number): void;
unqueueFromProduction(queueType: QueueType, objName: string, objType: ObjectType, quantity: number): void;
...
}
QueueType 及 QueueStatus 列舉
這些列舉描述了生產佇列的類型與狀態:
export declare enum QueueType {
Structures = 0,
Armory = 1,
Infantry = 2,
Vehicles = 3,
Aircrafts = 4,
Ships = 5
}
export declare enum QueueStatus {
Idle = 0,
Active = 1,
/** Production is paused */
OnHold = 2,
/** First item is ready and is waiting to be removed before starting the next */
Ready = 3
}
使用示例
以下示範如何在外部程式碼中直接控制生產佇列。假設我們想在資金低於 1000 時暫停載具佇列,並在資金回升後恢復:
import { ActionsApi, GameApi, QueueType } from "@chronodivide/game-api";
function manageProduction(game: GameApi, actions: ActionsApi) {
const myMoney = game.getPlayerData("bot").credits;
if (myMoney < 1000) {
// 資金不足,暫停載具生產佇列
actions.pauseProduction(QueueType.Vehicles);
} else {
// 確保載具佇列處於啟動狀態
const queueData = game.getProductionApi().getQueueData(QueueType.Vehicles);
if (queueData.status === QueueStatus.OnHold) {
actions.resumeProduction(QueueType.Vehicles);
}
}
}
此範例說明了 pauseProduction
與 resumeProduction
主要是向遊戲送出暫停 / 繼續指令,並不會移除佇列內已排定的項目。實際上它們對應到 UpdateQueue
這一種玩家動作,分別設定更新類型為 Pause
或 Resume
。
小結
-
pauseProduction(queueType)
:將指定的生產佇列標記為OnHold
狀態,佇列內容仍會保留但不繼續生產。 -
resumeProduction(queueType)
:把先前暫停的佇列恢復成Active
狀態,生產流程會繼續進行。
在 queueController.ts
中,這兩個方法根據資源與佇列的重要度自動呼叫,用來平衡不同佇列之間的產量需求。這些介面能讓開發者對 AI 的生產節奏進行更細緻的控制。
→返回《@chronodivide/game-api 使用教學與完整 API 對照表》
This article was last edited at