Skip to content

Commit

Permalink
feat: add RoomTerrain.getRawBuffer() (#247)
Browse files Browse the repository at this point in the history
* feat: add RoomTerrain.getRawBuffer()

* fix: incorporate changes recommended by @DiamondMofend and update tests

* fix: replace TypedArray helper with inline type
  • Loading branch information
admon84 authored Dec 2, 2023
1 parent 1e39c4f commit 9d727de
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 1 deletion.
23 changes: 23 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2032,7 +2032,9 @@ declare namespace Tag {
private [OpaqueTagSymbol]: T;
}
}

type Id<T extends _HasId> = string & Tag.OpaqueTag<T>;

type fromId<T> = T extends Id<infer R> ? R : never;
/**
* `InterShardMemory` object provides an interface for communicating between shards.
Expand Down Expand Up @@ -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<RoomTerrain> {
Expand Down
27 changes: 26 additions & 1 deletion dist/screeps-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,5 +433,7 @@ declare namespace Tag {
private [OpaqueTagSymbol]: T;
}
}

type Id<T extends _HasId> = string & Tag.OpaqueTag<T>;

type fromId<T> = T extends Id<infer R> ? R : never;
21 changes: 21 additions & 0 deletions src/room-terrain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<RoomTerrain> {
Expand Down

0 comments on commit 9d727de

Please sign in to comment.