GameApi.getAllSuperWeaponData()
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/1212
GameApi.getAllSuperWeaponData()
此方法會回傳場上所有玩家的超級武器狀態。
在 @chronodivide/game-api
的型別宣告中可以看到:
351 getUnitData(unitId: number): UnitData | undefined;
352 getAllSuperWeaponData(): SuperWeaponData[];
353 /** Returns a data structure that wraps the rules.ini [General] section */
SuperWeaponData
的結構如下,包含玩家名稱、超級武器類型、目前狀態與剩餘計時秒數:
export interface SuperWeaponData {
playerName: string;
type: SuperWeaponType;
status: SuperWeaponStatus;
timerSeconds: number;
}
SuperWeaponStatus
與 SuperWeaponType
亦在同檔定義,列舉所有可能的超級武器種類與狀態。
在壓縮後的實作檔中,getAllSuperWeaponData
會遍歷所有戰鬥玩家 (combatants),將其 superWeaponsTrait
裡的資料轉換為簡化的物件並展開成一個陣列:
getAllSuperWeaponData(){
return Mp(this,Lp,"f").getCombatants()
.map(t => t.superWeaponsTrait.getAll()
.map(e => ({
playerName: t.name,
type: e.rules.type,
status: e.status,
timerSeconds: e.getTimerSeconds()
})))
.flat()
}
【F:lib/package/dist/index.js†byte796350-796625】
典型用途
由於超級武器的啟動往往決定戰局,Bot 可以透過此方法時刻掌握己方與敵方的超級武器冷卻狀態,以做出相應的攻防決策。
範例程式
以下範例會在遊戲開始時列出所有玩家的超級武器資訊,並每隔一段時間再次更新。
import { cdapi, Bot, GameApi, SuperWeaponStatus } from "@chronodivide/game-api";
class SuperWeaponMonitorBot extends Bot {
report(game: GameApi) {
const swData = game.getAllSuperWeaponData();
swData.forEach((sw) => {
console.log(
`${sw.playerName} - ${sw.type} : ${sw.status}` +
` (timer ${sw.timerSeconds}s)`
);
if (sw.status === SuperWeaponStatus.Ready) {
console.log(`>> ${sw.playerName}'s ${sw.type} is ready!`);
}
});
}
onGameStart(game: GameApi) {
this.report(game);
}
onGameTick(game: GameApi) {
if (game.getCurrentTick() % 150 === 0) { // roughly every 10 seconds
this.report(game);
}
}
}
async function main() {
await cdapi.init(process.env.MIX_DIR!);
await cdapi.createGame({
agents: [new SuperWeaponMonitorBot("Observer", "Americans")],
mapName: "mp03t4.map",
shortGame: true,
online: false,
});
}
main().catch(console.error);
此 Bot 透過 getAllSuperWeaponData()
取得所有玩家的超級武器狀態,並在遊戲中定期輸出。
例如若某個玩家的狀態變成 Ready
,便立即顯示提示。藉由監控這些資訊,AI 可以更有效地規劃攻擊時機或預先部署防禦。
→返回《@chronodivide/game-api 使用教學與完整 API 對照表》
This article was last edited at