ActionsApi.quitGame()方法說明與使用範例
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/1200
下面的 TypeScript 定義文件節錄了 ActionsApi
的方法列表,其中 quitGame
是最後一個方法,無參數、回傳 void
:
export declare class ActionsApi {
...
setUnitDebugText(unitId: number, text: string | undefined): void;
quitGame(): void;
}
在專案的 SupalosaBot
中,quitGame()
被用在判斷「沒有軍隊或生產建築」時主動退出比賽:
if (armyUnits.length == 0 && productionBuildings.length == 0 && mcvUnits.length == 0) {
this.logBotStatus(`No army or production left, quitting.`);
this.actionsApi.quitGame();
}
功能說明
ActionsApi.quitGame()
的作用是向遊戲宣告「本方投降」,遊戲引擎在收到此動作後會立即結束該玩家的比賽。
常見應用是在 AI 偵測到無法繼續作戰或需要快速結束遊戲時呼叫。
簡化使用範例
以下是一個簡化版本,展示如何在自訂 Bot 邏輯中使用此方法:
import { Bot, GameApi, ObjectType, FactoryType } from "@chronodivide/game-api";
class ResignWhenDefeatedBot extends Bot {
override onGameTick(game: GameApi) {
const myPlayer = game.getPlayerData(this.name);
const armyUnits = game.getVisibleUnits(this.name, "self", u => u.isSelectableCombatant);
const productionBuildings = game.getVisibleUnits(
this.name, "self",
u => u.type === ObjectType.Building && u.factory !== FactoryType.None
);
const mcvUnits = game.getVisibleUnits(
this.name, "self",
u => !!u.deploysInto && game.getGeneralRules().baseUnit.includes(u.name)
);
if (armyUnits.length === 0 && productionBuildings.length === 0 && mcvUnits.length === 0) {
this.actionsApi.quitGame(); // 主動投降
}
}
}
建立比賽時,只需將此 Bot 實例傳入即可。當條件成立時,quitGame()
將使該 Bot 結束戰局。
整體流程與專案中 SupalosaBot
的實作方式相同,只是簡化了判斷與輸出。
小結
ActionsApi.quitGame()
可視為「投降」或「退出」指令,方便在適當情況下由 Bot 結束比賽。
如何觸發該條件完全取決於 Bot 的邏輯,可能包括:
-
✅ 兵力全滅
-
✅ 沒有生產建築
-
✅ 比賽時間過長
-
✅ 其他自定義規則
對人類對手來說,這與在 UI 介面中選擇「退出/投降」的效果相同。
→返回《@chronodivide/game-api 使用教學與完整 API 對照表》
This article was last edited at