GameApi.getBuildingPlacementData(objName)

| Chrono Divide | 4 Reads

下列為 GameApi 在套件 @chronodivide/game-api 的型別宣告,可看到 getBuildingPlacementData() 的方法簽名:

export declare class GameApi {
    #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[];
    /** Queries all units and buildings on the map, regardless of owner */
}

getBuildingPlacementData() 回傳的型別 BuildingPlacementData 定義如下:

export declare interface BuildingPlacementData {
    /** The size of the building foundation in tiles */
    foundation: Size;
    /** The offset of the foundation center tile */
    foundationCenter: Vector2;
}

在壓縮後的實作檔 index.js,其核心邏輯為:從內部資料庫以物件名稱取得建築,再將其 foundationfoundationCenter 回傳

...canPlaceAt(t,i,{normalizedTile:!0})}
getBuildingPlacementData(e){
    e = Mp(this,Lp,"f").art.getObject(e,Et.Building);
    return { foundation: e.foundation, foundationCenter: e.foundationCenter }
}
getPlayers(){...

在專案中的使用情境

src/bot/logic/building/buildingRules.ts 中,getBuildingPlacementData 用來取得建築的基礎尺寸,進一步計算鄰接可放置區域:

function getAdjacencyTiles(
    game: GameApi,
    playerData: PlayerData,
    technoRules: TechnoRules,
    onWater: boolean,
    minimumSpace: number,
): Tile[] {
    const placementRules = game.getBuildingPlacementData(technoRules.name);
    const { width: newBuildingWidth, height: newBuildingHeight } = placementRules.foundation;
    const tiles = [];
    const buildings = game.getVisibleUnits(playerData.name, "self", (r: TechnoRules) => r.type === ObjectType.Building);
    ...

在同檔稍後的 getDefaultPlacementLocation 函式中也會先取得基礎尺寸,再嘗試搜尋合適的建造位置。


簡易使用範例

下面範例說明如何利用 getBuildingPlacementData 取得建築尺寸並輸出:

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

class PlacementInfoBot extends Bot {
    onGameStart(game: GameApi) {
        const building = "GAPOWR";             // Allied Power Plant
        const data = game.getBuildingPlacementData(building);
        console.log(`foundation: ${data.foundation.width}x${data.foundation.height}`);
        console.log(`center offset: (${data.foundationCenter.x}, ${data.foundationCenter.y})`);
    }
}

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

main().catch(console.error);

以上程式於 onGameStart 取得建築的 foundationfoundationCenter,能協助判斷建築佔用格數及中心點偏移量。

搭配 GameApi.canPlaceBuilding() 等方法,即可自行計算建築合適的放置位置或判斷是否會與其他物件衝突。透過 getBuildingPlacementData 取得的資訊,AI 可以更精確地控制建築佈局與位置選擇。

 

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

This article was last edited at