Skip to content

Commit

Permalink
Game.map.describeExits returns null for non-existent rooms (#250)
Browse files Browse the repository at this point in the history
* Game.map.describeExits returns null for non-existent rooms

* chore: add example for type `ExitsInformation`
  • Loading branch information
tiennou authored Feb 3, 2024
1 parent 8957b7b commit 1911145
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
13 changes: 11 additions & 2 deletions dist/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1746,7 +1746,7 @@ type BodyPartDefinition<T extends BodyPartConstant = BodyPartConstant> = T exten
*
* If the body part is boosted, this property specifies the mineral type which is used for boosting.
*/
boost?: keyof typeof BOOSTS[T];
boost?: keyof (typeof BOOSTS)[T];
/**
* One of the body part types constants.
*/
Expand Down Expand Up @@ -1790,6 +1790,15 @@ type StoreDefinitionUnlimited = Store<ResourceConstant, true>;
// energy: number;
// }

/**
* @example
* {
* "1": "W8N4", // TOP
* "3": "W7N3", // RIGHT
* // "5": "W8N2", // BOTTOM
* "7": "W9N3" // LEFT
* }
*/
type ExitsInformation = Partial<Record<ExitKey, string>>;

interface AllLookAtTypes {
Expand Down Expand Up @@ -2796,7 +2805,7 @@ interface GameMap {
* @param roomName The room name.
* @returns The exits information or null if the room not found.
*/
describeExits(roomName: string): ExitsInformation;
describeExits(roomName: string): ExitsInformation | null;
/**
* Find the exit direction from the given room en route to another room.
* @param fromRoom Start room name or room object.
Expand Down
16 changes: 9 additions & 7 deletions dist/screeps-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,15 @@ function resources(o: GenericStore): ResourceConstant[] {

{
const exits = Game.map.describeExits("W8N3");
// tslint:disable-next-line:newline-per-chained-call
keys(exits).map((exitKey) => {
const nextRoom = exits[exitKey];
const exitDir = +exitKey as ExitConstant;
const exitPos = creep.pos.findClosestByRange(exitDir);
return { nextRoom, exitPos };
});
if (exits) {
// tslint:disable-next-line:newline-per-chained-call
keys(exits).map((exitKey) => {
const nextRoom = exits[exitKey];
const exitDir = +exitKey as ExitConstant;
const exitPos = creep.pos.findClosestByRange(exitDir);
return { nextRoom, exitPos };
});
}
}

// Game.map.findExit()
Expand Down
11 changes: 10 additions & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ type BodyPartDefinition<T extends BodyPartConstant = BodyPartConstant> = T exten
*
* If the body part is boosted, this property specifies the mineral type which is used for boosting.
*/
boost?: keyof typeof BOOSTS[T];
boost?: keyof (typeof BOOSTS)[T];
/**
* One of the body part types constants.
*/
Expand Down Expand Up @@ -191,6 +191,15 @@ type StoreDefinitionUnlimited = Store<ResourceConstant, true>;
// energy: number;
// }

/**
* @example
* {
* "1": "W8N4", // TOP
* "3": "W7N3", // RIGHT
* // "5": "W8N2", // BOTTOM
* "7": "W9N3" // LEFT
* }
*/
type ExitsInformation = Partial<Record<ExitKey, string>>;

interface AllLookAtTypes {
Expand Down
2 changes: 1 addition & 1 deletion src/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface GameMap {
* @param roomName The room name.
* @returns The exits information or null if the room not found.
*/
describeExits(roomName: string): ExitsInformation;
describeExits(roomName: string): ExitsInformation | null;
/**
* Find the exit direction from the given room en route to another room.
* @param fromRoom Start room name or room object.
Expand Down

0 comments on commit 1911145

Please sign in to comment.