MapApi.getRealMapSize()

| Chrono Divide | 3 Reads

以下程式片段顯示倉庫中的 map.ts 利用 MapApi.getRealMapSize() 取得地圖尺寸,並作為後續邏輯的基礎:

import { GameApi, GameMath, MapApi, PlayerData, Size, Tile, UnitData, Vector2 } from "@chronodivide/game-api";
import { maxBy } from "../common/utils.js";

export function determineMapBounds(mapApi: MapApi): Size {
    return mapApi.getRealMapSize();
}

此方法通常回傳 Size 物件,包含 widthheight 等欄位,代表實際可遊玩的地圖尺寸(以格數計)。在本專案中,determineMapBounds 會在建立區塊快取或進行範圍計算時使用這個尺寸。


作用

getRealMapSize() 提供目前地圖的實際大小,通常以地圖左上角為原點,度量單位是 tile。

常搭配 getTile(x, y)isVisibleTile(tile, playerName)MapApi 其他查詢方法使用。


簡易示例

import { cdapi, Bot, GameApi, MapApi, Size } from "@chronodivide/game-api";

class MapSizeBot extends Bot {
    onGameStart(game: GameApi) {
        const mapApi: MapApi = game.mapApi;
        const size: Size = mapApi.getRealMapSize();
        console.log(`Map size: ${size.width} x ${size.height} tiles`);
    }
}

async function main() {
    await cdapi.init(process.env.MIX_DIR!);
    await cdapi.createGame({
        agents: [new MapSizeBot("Inspector", "Americans")],
        mapName: "mp03t4.map",
        shortGame: true,
        online: false,
    });
}
main().catch(console.error);

onGameStart 中取得 MapApi 實例後,呼叫 getRealMapSize() 即可得知地圖的真實寬高。此資訊通常用於:

  • 建立分區或「sector」資料結構以便巡邏、偵查。

  • 在搜尋建築位置或規劃路徑時確認邊界。

  • 掌握整體視野更新比例,評估未探索區域。

透過此方法配合其他 MapApi 函式,即可精準地在地圖上定位及計算範圍,協助 AI 或工具做更詳盡的策略判斷。

 

→返回《@chronodivide/game-api 使用教學與完整 API 對照表》

This article was last edited at