GameApi.getPlayerData(playerName)
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/1206
GameApi 的宣告檔 (package/dist/index.d.ts
) 中可以看到 getPlayers
的介面定義,方法僅回傳一個字串陣列:
#private;
mapApi: MapApi;
rulesApi: RulesApi;
isPlayerDefeated(playerName: string): boolean;
areAlliedPlayers(p1Name: string, p2Name: string): boolean;
canPlaceBuilding(playerName: string, buildingName: string, tile: Tile): boolean;
getBuildingPlacementData(objName: string): BuildingPlacementData;
getPlayers(): string[];
getPlayerData(playerName: string): PlayerData;
getAllTerrainObjects(): number[];
在壓縮後的實作檔 index.js
中,getPlayers
會取得「非中立」玩家後,回傳它們的名稱:
...foundationCenter}}getPlayers(){return Mp(this,Lp,"f").getNonNeutralPlayers().map(e=>e.name)}getPlayerData(e){let t=Mp(this,Lp,"f").getPlayerByName(e)...
專案內的使用範例
在 src/bot/logic/building/common.ts
裡,AI 透過 game.getPlayers()
取得所有玩家,再排除同盟者來計算防禦建築面向的位置:
const allNames = game.getPlayers();
// Create a list of positions that point roughly towards hostile player start locatoins.
const candidates = allNames
.filter((otherName) => otherName !== currentName && !game.areAlliedPlayers(otherName, currentName))
.map((otherName) => {
const enemyPlayer = game.getPlayerData(otherName);
return getPointTowardsOtherPoint(game, startLocation, enemyPlayer.startLocation, 4, 16, 1.5);
});
同樣地,在 awareness.ts
中也會先取得所有玩家,再依敵我關係篩選敵方名單,用於威脅評估與集結點更新:
const hostilePlayerNames = game
.getPlayers()
.map((name) => game.getPlayerData(name))
.filter(
(other) =>
other.name !== playerData.name &&
other.isCombatant &&
!game.areAlliedPlayers(playerData.name, other.name),
)
.map((other) => other.name);
基本使用示例
下列範例顯示如何在自訂 Bot 中使用 getPlayers()
列出遊戲中所有玩家:
import { cdapi, Bot, GameApi } from "@chronodivide/game-api";
class PlayerListBot extends Bot {
onGameStart(game: GameApi) {
const names = game.getPlayers();
console.log("目前玩家:", names.join(", "));
}
}
async function main() {
await cdapi.init(process.env.MIX_DIR!);
await cdapi.createGame({
agents: [new PlayerListBot("BotA", "Americans"), new PlayerListBot("BotB", "French")],
mapName: "mp03t4.map",
shortGame: true,
online: false,
});
}
main().catch(console.error);
在 onGameStart
時呼叫 getPlayers()
,即可獲得本局所有非中立玩家的名稱,可用來後續判斷敵我關係或進行統計。此方法在許多邏輯中都扮演了取得玩家清單的角色,例如計算防禦位置、更新全局威脅資訊等。通过搭配 areAlliedPlayers
、getPlayerData
等方法,Bot 可以進一步分析敵方陣營並採取相應策略。
GameApi.getPlayerData(playerName)
會依指定名稱回傳該玩家的資訊物件。在 index.d.ts
的介面中,可以看到它與其他查詢方法同列於 GameApi
類別。PlayerData
的定義則包含玩家國家、起始位置、電力資訊與資金等狀態。
壓縮後的 index.js
中可找到此方法的實作。它先根據名稱尋找 Player
物件,若未找到則拋出錯誤;接著組合各屬性(包含地圖起始位置、是否為 AI、資金及電力狀況等),最後回傳該物件。
在程式內常透過 getPlayerData
取得敵我雙方的詳細資料,例如計算防禦建築朝向時會先將玩家名稱映射成 PlayerData
物件。自身 Bot 啟動時也會取得自己的 PlayerData
來初始化各種系統。
基本示例
import { cdapi, Bot, GameApi } from "@chronodivide/game-api";
class PlayerInfoBot extends Bot {
onGameStart(game: GameApi) {
const myData = game.getPlayerData(this.name);
console.log(`Player ${myData.name} starts at (${myData.startLocation.x},${myData.startLocation.y})`);
console.log(`Credits: ${myData.credits} | Power: ${myData.power.total}`);
}
}
async function main() {
await cdapi.init(process.env.MIX_DIR!);
await cdapi.createGame({
agents: [new PlayerInfoBot("BotA", "Americans")],
mapName: "mp03t4.map",
shortGame: true,
online: false,
});
}
main().catch(console.error);
在 onGameStart
內呼叫 getPlayerData(this.name)
即可取得自己的完整資料,包括起始位置與當前資源,可依此進行後續邏輯判斷或記錄。透過此方法也能查詢其他玩家的資料,例如敵方或盟友的起始位置與電力狀態,進而做出策略決策。
→返回《@chronodivide/game-api 使用教學與完整 API 對照表》
This article was last edited at