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

Make it compile with the newer version of typed-screeps. #19

Open
wants to merge 1 commit into
base: testing
Choose a base branch
from
Open
Changes from all 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
45 changes: 22 additions & 23 deletions src/Traveler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,6 @@ export class Traveler {
if (route) { allowedRooms = route; }
}

let roomsSearched = 0;

let callback = (roomName: string): CostMatrix | boolean => {

if (allowedRooms) {
Expand All @@ -309,8 +307,6 @@ export class Traveler {
return false;
}

roomsSearched++;

let matrix;
let room = Game.rooms[roomName];
if (room) {
Expand All @@ -326,7 +322,7 @@ export class Traveler {
}

if (options.obstacles) {
matrix = matrix.clone();
matrix = matrix!.clone();
for (let obstacle of options.obstacles) {
if (obstacle.pos.roomName !== roomName) { continue; }
matrix.set(obstacle.pos.x, obstacle.pos.y, 0xff);
Expand Down Expand Up @@ -502,13 +498,13 @@ export class Traveler {
* @returns {any}
*/

public static getStructureMatrix(roomName: string, freshMatrix?: boolean): CostMatrix {
public static getStructureMatrix(roomName: string, freshMatrix?: boolean): CostMatrix | undefined {
let room = Game.rooms[roomName];
if (!room) {
if (this.structureMatrixCache[roomName]) {
return this.structureMatrixCache[roomName];
} else {
return;
return undefined;
}
}

Expand All @@ -526,11 +522,13 @@ export class Traveler {
* @returns {any}
*/

public static getCreepMatrix(room: Room) {
public static getCreepMatrix(room: Room): CostMatrix | undefined {
if (!this.creepMatrixCache[room.name] || Game.time !== this.creepMatrixTick) {
this.creepMatrixTick = Game.time;
this.creepMatrixCache[room.name] = Traveler.addCreepsToMatrix(room,
this.getStructureMatrix(room.name, true).clone());
const matrix = this.getStructureMatrix(room.name, true);
if (matrix !== undefined) {
this.creepMatrixCache[room.name] = Traveler.addCreepsToMatrix(room, matrix.clone());
}
}
return this.creepMatrixCache[room.name];
}
Expand Down Expand Up @@ -560,7 +558,7 @@ export class Traveler {
}
}

for (let site of room.find<ConstructionSite>(FIND_MY_CONSTRUCTION_SITES)) {
for (let site of room.find(FIND_MY_CONSTRUCTION_SITES)) {
if (site.structureType === STRUCTURE_CONTAINER || site.structureType === STRUCTURE_ROAD
|| site.structureType === STRUCTURE_RAMPART) { continue; }
matrix.set(site.pos.x, site.pos.y, 0xff);
Expand All @@ -581,7 +579,7 @@ export class Traveler {
*/

public static addCreepsToMatrix(room: Room, matrix: CostMatrix): CostMatrix {
room.find<Creep>(FIND_CREEPS).forEach((creep: Creep) => matrix.set(creep.pos.x, creep.pos.y, 0xff) );
room.find(FIND_CREEPS).forEach((creep: Creep) => matrix.set(creep.pos.x, creep.pos.y, 0xff) );
return matrix;
}

Expand Down Expand Up @@ -615,34 +613,34 @@ export class Traveler {
* @returns {RoomPosition}
*/

public static positionAtDirection(origin: RoomPosition, direction: number): RoomPosition {
public static positionAtDirection(origin: RoomPosition, direction: number): RoomPosition | undefined {
let offsetX = [0, 0, 1, 1, 1, 0, -1, -1, -1];
let offsetY = [0, -1, -1, 0, 1, 1, 1, 0, -1];
let x = origin.x + offsetX[direction];
let y = origin.y + offsetY[direction];
let position = new RoomPosition(x, y, origin.roomName);
if (!this.isValid(position)) { return; }
if (!this.isValid(position)) { return undefined; }
return position;
}

public static nextDirectionInPath(creep: Creep): number {
public static nextDirectionInPath(creep: Creep): number | undefined {
let travelData = creep.memory._trav as TravelData;
if (!travelData || !travelData.path || travelData.path.length === 0) { return; }
if (!travelData || !travelData.path || travelData.path.length === 0) { return undefined; }
return Number.parseInt(travelData.path[0]);
}

public static nextPositionInPath(creep: Creep): RoomPosition {
public static nextPositionInPath(creep: Creep): RoomPosition | undefined {
let nextDir = this.nextDirectionInPath(creep);
if (!nextDir) { return; }
if (!nextDir) { return undefined; }
return this.positionAtDirection(creep.pos, nextDir);
}

private static pushCreep(creep: Creep, insist: boolean): boolean {
let nextDir = this.nextDirectionInPath(creep);
if (!nextDir) { return false; }
let nextPos = this.positionAtDirection(creep.pos, nextDir);
if (!nextPos) { return; }
let otherCreep = nextPos.lookFor<Creep>(LOOK_CREEPS)[0];
if (!nextPos) { return false; }
let otherCreep = nextPos.lookFor(LOOK_CREEPS)[0];
if (!otherCreep) { return false; }

let otherData = otherCreep.memory._trav as TravelData;
Expand Down Expand Up @@ -730,11 +728,11 @@ export class Traveler {
* @returns {{x: (string|any), y: (string|any), x_dir: (string|any), y_dir: (string|any)}}
*/

public static getRoomCoordinates(roomName: string): RoomCoord {
public static getRoomCoordinates(roomName: string): RoomCoord | undefined {

let coordinateRegex = /(E|W)(\d+)(N|S)(\d+)/g;
let match = coordinateRegex.exec(roomName);
if (!match) { return; }
if (!match) { return undefined; }

let xDir = match[1];
let x = match[2];
Expand All @@ -749,10 +747,11 @@ export class Traveler {
};
}

public static roomType(roomName: string): number {
public static roomType(roomName: string): number | undefined {
if (!this.roomTypeCache[roomName]) {
let type: number;
let coords = this.getRoomCoordinates(roomName);
if (coords === undefined) { return undefined; }
if (coords.x % 10 === 0 || coords.y % 10 === 0) {
type = ROOMTYPE_HIGHWAY;
} else if (coords.x % 5 === 0 && coords.y % 5 === 0) {
Expand Down