ActionsApi.toggleRepairWrench(buildingId)方法說明與使用範例
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/1187
【Repository: supalosa-chronodivide-bot】
解釋
ActionsApi.toggleRepairWrench(buildingId)
是 Chrono Divide API 用來切換建築維修狀態的方法。
在套件的型別定義檔中可看到此方法的宣告:
export declare class ActionsApi {
#private;
placeBuilding(buildingName: string, rx: number, ry: number): void;
sellObject(objectId: number): void;
/** @deprecated use {@link ActionsApi.sellObject} instead */
sellBuilding(buildingId: number): void;
toggleRepairWrench(buildingId: number): void;
...
}
其對應的 JavaScript 實作會送出 ToggleRepair
指令,將指定建築的扳手狀態(維修開/關)做切換:
...sellBuilding(e){this.sellObject(e)}
toggleRepairWrench(t){
np(this,pp,"m",mp).call(this,oc.ToggleRepair,e=>{e.buildingId=t})
}
toggleAlliance...
在此專案中,toggleRepairWrench
主要於 QueueController
內自動維修受損建築時被呼叫。
當有錢並偵測到己方建築受損,便會自動切換維修扳手:
// Repair is simple - just repair everything that's damaged.
if (playerData.credits > 0) {
game.getVisibleUnits(playerData.name, "self", (r) => r.repairable).forEach((unitId) => {
const unit = game.getUnitData(unitId);
if (!unit || !unit.hitPoints || !unit.maxHitPoints || unit.hasWrenchRepair) {
return;
}
if (unit.hitPoints < unit.maxHitPoints) {
actionsApi.toggleRepairWrench(unitId);
}
});
}
示例
以下 TypeScript 範例說明如何在遊戲開始後判斷某建築是否受損,並用 toggleRepairWrench
啟動維修:
import { Bot, GameApi } from "@chronodivide/game-api";
class RepairDemoBot extends Bot {
override onGameTick(game: GameApi) {
// 取得己方所有可維修的建築
const repairables = game.getVisibleUnits(
this.name,
"self",
r => r.repairable
);
for (const id of repairables) {
const obj = game.getUnitData(id);
// 只處理明顯受損、尚未啟用扳手的建築
if (obj && obj.hitPoints < obj.maxHitPoints && !obj.hasWrenchRepair) {
this.actionsApi.toggleRepairWrench(id);
}
}
}
}
程式流程
-
每個遊戲刻取回所有具
repairable
標誌的己方建築。 -
檢查其生命值與
hasWrenchRepair
狀態,若建築受損且未在維修中,則呼叫toggleRepairWrench(buildingId)
啟動修理。 -
若再次呼叫同方法,則會將扳手關閉,因此需自行根據需求控制開啟或關閉。
-
該方法等同於遊戲中手動點擊建築的維修扳手,可讓 AI 自主管理維修流程。
-
透過在遊戲迴圈中定期檢查建築狀態,即可維持基地健康度並避免資源浪費。
執行環境
為取得 API 定義,本次使用 npm pack
下載套件檔案:
chronodivide-game-api-0.72.0.tgz
透過解壓後的 dist/index.d.ts
和 index.js
文件確認了方法定義與實作。
以上即為 ActionsApi.toggleRepairWrench
的用途與使用方式。
→返回《@chronodivide/game-api 使用教學與完整 API 對照表》
This article was last edited at