MapApi.getStartingLocations()
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/1224
MapApi.getStartingLocations()
會回傳地圖上各玩家的起始位置(以 tile 座標表示)。
在套件宣告檔的 MapApi
介面中,可看到這個方法與相關說明:
684 getRealMapSize(): Size;
685 /** Tile coordinates for each player starting position */
686 getStartingLocations(): Vector2[];
687 getTheaterType(): TheaterType;
壓縮後的實作僅將內部的 startingLocations
陣列轉成 Vector2
物件並回傳:
getStartingLocations() {
return Op(this, Cp, "f").startingLocations.map(e => new Os(e.x, e.y));
}
在本專案的 scout.ts
,此方法常用來取得可能的敵方出生點,並排除自己及已能看見的位置:
const unseenStartingLocations = gameApi.mapApi
.getStartingLocations()
.filter((startingLocation) => {
if (startingLocation == playerData.startLocation) {
return false;
}
let tile = gameApi.mapApi.getTile(startingLocation.x, startingLocation.y);
return tile ? !gameApi.mapApi.isVisibleTile(tile, playerData.name) : false;
});
簡易示例
下面的 Bot 範例展示如何在遊戲開始時列出所有起始位置:
import { cdapi, Bot, GameApi } from "@chronodivide/game-api";
class StartLoggerBot extends Bot {
onGameStart(game: GameApi) {
const starts = game.mapApi.getStartingLocations();
starts.forEach((loc, i) =>
console.log(`Start #${i + 1}: (${loc.x}, ${loc.y})`)
);
}
}
async function main() {
await cdapi.init(process.env.MIX_DIR!);
await cdapi.createGame({
agents: [new StartLoggerBot("Scout", "Americans")],
mapName: "mp03t4.map",
shortGame: true,
online: false,
});
}
main().catch(console.error);
此程式會在比賽開局列出每個起始座標,使 Bot 能進一步利用這些資訊安排偵查或部署策略。透過 getStartingLocations()
,我們可以輕鬆取得每張地圖的起始點清單並加以應用。
→返回《@chronodivide/game-api 使用教學與完整 API 對照表》
This article was last edited at