diff --git a/dist/index.d.ts b/dist/index.d.ts index b532efa..359e968 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -2032,7 +2032,9 @@ declare namespace Tag { private [OpaqueTagSymbol]: T; } } + type Id = string & Tag.OpaqueTag; + type fromId = T extends Id ? R : never; /** * `InterShardMemory` object provides an interface for communicating between shards. @@ -4057,6 +4059,27 @@ interface RoomTerrain { * @return number Number of terrain mask like: TERRAIN_MASK_SWAMP | TERRAIN_MASK_WALL */ get(x: number, y: number): 0 | TERRAIN_MASK_WALL | TERRAIN_MASK_SWAMP; + /** + * Get copy of underlying static terrain buffer. + * @param destinationArray (optional) A typed array view in which terrain will be copied to. + * @throws {RangeError} if `destinationArray` is provided, it must have a length of at least 2500 (`50*50`). + * @return Copy of underlying room terrain as a new typed array of size 2500. + */ + getRawBuffer< + T extends + | Int8Array + | Uint8Array + | Int16Array + | Uint16Array + | Int32Array + | Uint32Array + | Uint8ClampedArray + | Float32Array + | Float64Array, + >( + destinationArray: T, + ): T; + getRawBuffer(): Uint8Array; } interface RoomTerrainConstructor extends _Constructor { diff --git a/dist/screeps-tests.ts b/dist/screeps-tests.ts index 5f4e92d..15b1a81 100644 --- a/dist/screeps-tests.ts +++ b/dist/screeps-tests.ts @@ -1114,6 +1114,10 @@ function resources(o: GenericStore): ResourceConstant[] { const myTerrain = room.getTerrain(); + const otherTerrain = new Room.Terrain("E2S7"); + + const anotherTerrain = Game.map.getRoomTerrain("W2N5"); + const ret = myTerrain.get(5, 5); if (ret === 0) { /*plain*/ @@ -1125,7 +1129,28 @@ function resources(o: GenericStore): ResourceConstant[] { /*wall*/ } - const enemyTerrain = new Room.Terrain("W2N5"); + const myRawTerrain = myTerrain.getRawBuffer(); + + const otherRawTerrain = otherTerrain.getRawBuffer(new Int8Array(2500)); + + const anotherRawTerrain = anotherTerrain.getRawBuffer(new Uint16Array(2500)); + + for (const rawTerrain of [myRawTerrain, otherRawTerrain, anotherRawTerrain]) { + for (let y = 0; y < 50; y++) { + for (let x = 0; x < 50; x++) { + const code = rawTerrain[y * 50 + x]; + if (code === 0) { + /*plain*/ + } + if (code & TERRAIN_MASK_SWAMP) { + /*swamp*/ + } + if (code & TERRAIN_MASK_WALL) { + /*wall*/ + } + } + } + } } // Creep.body diff --git a/src/helpers.ts b/src/helpers.ts index ed93abc..5461fda 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -433,5 +433,7 @@ declare namespace Tag { private [OpaqueTagSymbol]: T; } } + type Id = string & Tag.OpaqueTag; + type fromId = T extends Id ? R : never; diff --git a/src/room-terrain.ts b/src/room-terrain.ts index 0ee753e..bc35211 100644 --- a/src/room-terrain.ts +++ b/src/room-terrain.ts @@ -9,6 +9,27 @@ interface RoomTerrain { * @return number Number of terrain mask like: TERRAIN_MASK_SWAMP | TERRAIN_MASK_WALL */ get(x: number, y: number): 0 | TERRAIN_MASK_WALL | TERRAIN_MASK_SWAMP; + /** + * Get copy of underlying static terrain buffer. + * @param destinationArray (optional) A typed array view in which terrain will be copied to. + * @throws {RangeError} if `destinationArray` is provided, it must have a length of at least 2500 (`50*50`). + * @return Copy of underlying room terrain as a new typed array of size 2500. + */ + getRawBuffer< + T extends + | Int8Array + | Uint8Array + | Int16Array + | Uint16Array + | Int32Array + | Uint32Array + | Uint8ClampedArray + | Float32Array + | Float64Array, + >( + destinationArray: T, + ): T; + getRawBuffer(): Uint8Array; } interface RoomTerrainConstructor extends _Constructor {