Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add RoomTerrain.getRawBuffer() #247

Merged
merged 4 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2019,8 +2019,21 @@ 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;

type TypedArray =
| Int8Array
| Uint8Array
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Uint8ClampedArray
| Float32Array
| Float64Array;
/**
* `InterShardMemory` object provides an interface for communicating between shards.
* Your script is executed separatedly on each shard, and their `Memory` objects are isolated from each other.
Expand Down Expand Up @@ -4044,6 +4057,14 @@ 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 TypedArray>(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
13 changes: 13 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,5 +433,18 @@ 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;

type TypedArray =
| Int8Array
| Uint8Array
| Int16Array
| Uint16Array
| Int32Array
| Uint32Array
| Uint8ClampedArray
| Float32Array
| Float64Array;
8 changes: 8 additions & 0 deletions src/room-terrain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ 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 TypedArray>(destinationArray: T): T;
getRawBuffer(): Uint8Array;
}

interface RoomTerrainConstructor extends _Constructor<RoomTerrain> {
Expand Down