Set typescript eslint settings to strict
This commit is contained in:
parent
2375765503
commit
36b21e0260
19 changed files with 320 additions and 291 deletions
12
.eslintrc.js
12
.eslintrc.js
|
|
@ -1,12 +0,0 @@
|
|||
module.exports = {
|
||||
extends: ['eslint:recommended', 'plugin:@typescript-eslint/strict'],
|
||||
parser: '@typescript-eslint/parser',
|
||||
plugins: ['@typescript-eslint'],
|
||||
root: true,
|
||||
parserOptions: {
|
||||
"project": ["tsconfig.json"]
|
||||
},
|
||||
rules: {
|
||||
'no-unused-vars': "off"
|
||||
}
|
||||
};
|
||||
|
|
@ -21,7 +21,7 @@ export default defineConfig([
|
|||
"@stylistic/max-len": ["error", { code: 80, tabWidth: 2 }],
|
||||
},
|
||||
},
|
||||
tseslint.configs.recommended,
|
||||
tseslint.configs.strictTypeChecked,
|
||||
tseslint.configs.stylisticTypeChecked,
|
||||
stylistic.configs.customize({
|
||||
quotes: "double",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
declare global {
|
||||
interface RoomMemory {
|
||||
sources: Record<Id<Source>, SourceMemory>;
|
||||
sources?: Record<Id<Source>, SourceMemory>;
|
||||
spawn: Id<StructureSpawn> | null;
|
||||
mineral: Id<Mineral> | null;
|
||||
_spawnCacheTimeout?: number;
|
||||
|
|
@ -23,8 +23,8 @@ declare global {
|
|||
}
|
||||
|
||||
Object.defineProperty(Room.prototype, "sources", {
|
||||
get: function (this: Room) {
|
||||
if (this == Room.prototype || this == undefined) return undefined;
|
||||
get: function (this: Room | undefined): Source[] {
|
||||
if (this == Room.prototype || this == undefined) return [];
|
||||
if (!this.memory.sources) {
|
||||
this.memory.sources = {};
|
||||
const sources = this.find(FIND_SOURCES);
|
||||
|
|
@ -34,14 +34,15 @@ Object.defineProperty(Room.prototype, "sources", {
|
|||
};
|
||||
}
|
||||
}
|
||||
return Object.keys(this.memory.sources).map(Game.getObjectById);
|
||||
return Object.keys(this.memory.sources)
|
||||
.map(s => Game.getObjectById(s as Id<Source>)) as Source[];
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
});
|
||||
|
||||
Object.defineProperty(Room.prototype, "spawn", {
|
||||
get: function (this: Room) {
|
||||
get: function (this: Room | undefined) {
|
||||
if (this == Room.prototype || this == undefined) return undefined;
|
||||
if (!this.memory.spawn) {
|
||||
if (this.memory._spawnCacheTimeout == null
|
||||
|
|
@ -64,7 +65,7 @@ Object.defineProperty(Room.prototype, "spawn", {
|
|||
});
|
||||
|
||||
Object.defineProperty(Room.prototype, "mineral", {
|
||||
get: function (this: Room) {
|
||||
get: function (this: Room | undefined) {
|
||||
if (this == Room.prototype || this == undefined) return undefined;
|
||||
if (!this.memory.mineral) {
|
||||
const minerals = this.find(FIND_MINERALS);
|
||||
|
|
@ -82,9 +83,27 @@ Object.defineProperty(Room.prototype, "mineral", {
|
|||
|
||||
Object.defineProperty(Source.prototype, "memory", {
|
||||
get: function (this: Source) {
|
||||
if (!this.room.memory.sources) {
|
||||
this.room.memory.sources = {};
|
||||
const sources = this.room.find(FIND_SOURCES);
|
||||
for (const source of sources) {
|
||||
this.room.memory.sources[source.id] = {
|
||||
container: null,
|
||||
};
|
||||
}
|
||||
}
|
||||
return this.room.memory.sources[this.id];
|
||||
},
|
||||
set: function (this: Source, mem: SourceMemory) {
|
||||
if (!this.room.memory.sources) {
|
||||
this.room.memory.sources = {};
|
||||
const sources = this.room.find(FIND_SOURCES);
|
||||
for (const source of sources) {
|
||||
this.room.memory.sources[source.id] = {
|
||||
container: null,
|
||||
};
|
||||
}
|
||||
}
|
||||
this.room.memory.sources[this.id] = mem;
|
||||
},
|
||||
enumerable: false,
|
||||
|
|
@ -99,7 +118,7 @@ Object.defineProperty(Source.prototype, "container", {
|
|||
filter: (s: Structure) => s.structureType === STRUCTURE_CONTAINER,
|
||||
});
|
||||
if (containers.length > 0) {
|
||||
this.memory.container = containers[0].id;
|
||||
this.memory.container = containers[0].id as Id<StructureContainer>;
|
||||
}
|
||||
}
|
||||
return this.memory.container == null
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ const UNSET = 999;
|
|||
|
||||
declare global {
|
||||
interface RoomMemory {
|
||||
_planner: string;
|
||||
_planner?: string;
|
||||
}
|
||||
}
|
||||
const structureCoding: (BuildableStructureConstant | null)[]
|
||||
|
|
@ -21,7 +21,7 @@ const getCoord = (x: number, y: number): number => {
|
|||
};
|
||||
|
||||
const distanceTransform = (mask: (0 | 999 | 1000)[]): number[] => {
|
||||
const arr: number[] = new Array(...mask);
|
||||
const arr = new Array(...mask) as number[];
|
||||
for (let i = 0; arr.find(x => x === UNSET) != null; i++) {
|
||||
for (let x = 0; x < roomSize; x++) {
|
||||
for (let y = 0; y < roomSize; y++) {
|
||||
|
|
@ -61,7 +61,10 @@ const distanceTransform = (mask: (0 | 999 | 1000)[]): number[] => {
|
|||
} */
|
||||
|
||||
const createBuildSites = (room: Room) => {
|
||||
const structures = [];
|
||||
if (room.memory._planner == null) {
|
||||
return;
|
||||
}
|
||||
const structures = [] as (BuildableStructureConstant | null)[];
|
||||
for (let i = 0; i < room.memory._planner.length; i++) {
|
||||
structures.push(
|
||||
structureCoding[(room.memory._planner.charCodeAt(i) - 32) & 0b11111],
|
||||
|
|
@ -72,56 +75,57 @@ const createBuildSites = (room: Room) => {
|
|||
}
|
||||
for (let x = 0; x < roomSize; x++) {
|
||||
for (let y = 0; y < roomSize; y++) {
|
||||
const struct = structures[getCoord(x, y)];
|
||||
if ((room.controller?.level ?? 0) < 2
|
||||
&& structures[getCoord(x, y)] != STRUCTURE_CONTAINER) {
|
||||
&& struct != STRUCTURE_CONTAINER) {
|
||||
continue;
|
||||
}
|
||||
if ((room.controller?.level ?? 0) < 3
|
||||
&& !(structures[getCoord(x, y)] == STRUCTURE_CONTAINER
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_ROAD
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_EXTENSION)) {
|
||||
&& !(struct == STRUCTURE_CONTAINER
|
||||
|| struct == STRUCTURE_ROAD
|
||||
|| struct == STRUCTURE_EXTENSION)) {
|
||||
continue;
|
||||
}
|
||||
if ((room.controller?.level ?? 0) < 4
|
||||
&& !(structures[getCoord(x, y)] == STRUCTURE_CONTAINER
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_ROAD
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_EXTENSION
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_RAMPART
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_TOWER)) {
|
||||
&& !(struct == STRUCTURE_CONTAINER
|
||||
|| struct == STRUCTURE_ROAD
|
||||
|| struct == STRUCTURE_EXTENSION
|
||||
|| struct == STRUCTURE_RAMPART
|
||||
|| struct == STRUCTURE_TOWER)) {
|
||||
continue;
|
||||
}
|
||||
if ((room.controller?.level ?? 0) < 5
|
||||
&& !(structures[getCoord(x, y)] == STRUCTURE_CONTAINER
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_ROAD
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_EXTENSION
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_RAMPART
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_TOWER
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_WALL)) {
|
||||
&& !(struct == STRUCTURE_CONTAINER
|
||||
|| struct == STRUCTURE_ROAD
|
||||
|| struct == STRUCTURE_EXTENSION
|
||||
|| struct == STRUCTURE_RAMPART
|
||||
|| struct == STRUCTURE_TOWER
|
||||
|| struct == STRUCTURE_WALL)) {
|
||||
continue;
|
||||
}
|
||||
if ((room.controller?.level ?? 0) < 6
|
||||
&& !(structures[getCoord(x, y)] == STRUCTURE_CONTAINER
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_ROAD
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_EXTENSION
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_RAMPART
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_TOWER
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_WALL
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_STORAGE)) {
|
||||
&& !(struct == STRUCTURE_CONTAINER
|
||||
|| struct == STRUCTURE_ROAD
|
||||
|| struct == STRUCTURE_EXTENSION
|
||||
|| struct == STRUCTURE_RAMPART
|
||||
|| struct == STRUCTURE_TOWER
|
||||
|| struct == STRUCTURE_WALL
|
||||
|| struct == STRUCTURE_STORAGE)) {
|
||||
continue;
|
||||
}
|
||||
if ((room.controller?.level ?? 0) < 8
|
||||
&& !(structures[getCoord(x, y)] == STRUCTURE_CONTAINER
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_ROAD
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_EXTENSION
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_RAMPART
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_TOWER
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_WALL
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_STORAGE
|
||||
|| structures[getCoord(x, y)] == STRUCTURE_EXTRACTOR)) {
|
||||
&& !(struct == STRUCTURE_CONTAINER
|
||||
|| struct == STRUCTURE_ROAD
|
||||
|| struct == STRUCTURE_EXTENSION
|
||||
|| struct == STRUCTURE_RAMPART
|
||||
|| struct == STRUCTURE_TOWER
|
||||
|| struct == STRUCTURE_WALL
|
||||
|| struct == STRUCTURE_STORAGE
|
||||
|| struct == STRUCTURE_EXTRACTOR)) {
|
||||
continue;
|
||||
}
|
||||
if (structures[getCoord(x, y)] != null) {
|
||||
if (room.createConstructionSite(x, y, structures[getCoord(x, y)])
|
||||
if (struct != null) {
|
||||
if (room.createConstructionSite(x, y, struct)
|
||||
=== ERR_FULL) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -145,7 +149,7 @@ export default profiler.registerFN(function RoomPlanner(room: Room) {
|
|||
return;
|
||||
}
|
||||
const terrain = room.getTerrain();
|
||||
const mask: number[] = new Array(roomSize * roomSize).fill(0);
|
||||
const mask = new Array(roomSize * roomSize).fill(0) as number[];
|
||||
for (let x = 0; x < roomSize; x++) {
|
||||
for (let y = 0; y < roomSize; y++) {
|
||||
mask[getCoord(x, y)] = terrain.get(x, y);
|
||||
|
|
@ -154,7 +158,7 @@ export default profiler.registerFN(function RoomPlanner(room: Room) {
|
|||
const wallDistance = distanceTransform(
|
||||
mask.map(t => t === TERRAIN_MASK_WALL ? 0 : UNSET));
|
||||
const controllerCoord
|
||||
= (room.controller?.pos.x ?? 0) + (room.controller?.pos.y ?? -1) * roomSize;
|
||||
= room.controller.pos.x + room.controller.pos.y * roomSize;
|
||||
const controllerDistance = distanceTransform(
|
||||
mask.map((t, i) =>
|
||||
i === controllerCoord
|
||||
|
|
@ -168,7 +172,10 @@ export default profiler.registerFN(function RoomPlanner(room: Room) {
|
|||
(a, b) => b[0] - a[0])[0][1];
|
||||
const buildCenterX = buildCenter % roomSize;
|
||||
const buildCenterY = Math.floor(buildCenter / roomSize);
|
||||
const buildCenterPos = room.getPositionAt(buildCenterX, buildCenterY)!;
|
||||
const buildCenterPos = room.getPositionAt(buildCenterX, buildCenterY);
|
||||
if (buildCenterPos == null) {
|
||||
return;
|
||||
}
|
||||
/* room.visual.rect(buildCenterX - 0.5, buildCenterY - 0.5, 1, 1, {
|
||||
stroke: "#FF0000",
|
||||
}); */
|
||||
|
|
@ -178,22 +185,22 @@ export default profiler.registerFN(function RoomPlanner(room: Room) {
|
|||
//
|
||||
|
||||
const structures: (BuildableStructureConstant | null)[]
|
||||
= new Array(roomSize * roomSize);
|
||||
= new Array(roomSize * roomSize) as (BuildableStructureConstant | null)[];
|
||||
|
||||
// Build Roads + Containers
|
||||
const roadTargets = (room.sources as (_HasRoomPosition | null)[])
|
||||
.concat([room.controller, room.mineral])
|
||||
.concat([room.find(FIND_EXIT_TOP)?.[0],
|
||||
room.find(FIND_EXIT_LEFT)?.[0],
|
||||
room.find(FIND_EXIT_RIGHT)?.[0],
|
||||
room.find(FIND_EXIT_BOTTOM)?.[0]]
|
||||
.map(pos => pos == null ? null : ({ pos, highRange: true })));
|
||||
.concat([room.find(FIND_EXIT_TOP).at(0),
|
||||
room.find(FIND_EXIT_LEFT).at(0),
|
||||
room.find(FIND_EXIT_RIGHT).at(0),
|
||||
room.find(FIND_EXIT_BOTTOM).at(0)]
|
||||
.map(pos => pos != null ? ({ pos }) : null));
|
||||
for (const target of roadTargets) {
|
||||
if (target == null) {
|
||||
continue;
|
||||
}
|
||||
const { path } = PathFinder.search(buildCenterPos,
|
||||
{ pos: target.pos, range: "highRange" in target ? 3 : 1 }, {
|
||||
{ pos: target.pos, range: 1 }, {
|
||||
swampCost: 10,
|
||||
plainCost: 2,
|
||||
roomCallback: function (roomName) {
|
||||
|
|
@ -358,10 +365,10 @@ export default profiler.registerFN(function RoomPlanner(room: Room) {
|
|||
largestY = Math.min(48, largestY + 3);
|
||||
|
||||
const centerLine = Math.floor(largestY / 2 + smallestY / 2);
|
||||
|
||||
const { path } = PathFinder.search(
|
||||
room.getPositionAt(smallestX - 1, centerLine + 1)!,
|
||||
room.getPositionAt(smallestX - 1, centerLine - 1)!,
|
||||
const wallStart = room.getPositionAt(smallestX - 1, centerLine + 1);
|
||||
const wallEnd = room.getPositionAt(smallestX - 1, centerLine - 1);
|
||||
if (wallStart != null && wallEnd != null) {
|
||||
const { path } = PathFinder.search(wallStart, wallEnd,
|
||||
{
|
||||
plainCost: 100,
|
||||
swampCost: 100,
|
||||
|
|
@ -400,7 +407,10 @@ export default profiler.registerFN(function RoomPlanner(room: Room) {
|
|||
},
|
||||
});
|
||||
|
||||
path.push(room.getPositionAt(smallestX - 1, centerLine)!);
|
||||
const centerWallPiece = room.getPositionAt(smallestX - 1, centerLine);
|
||||
if (centerWallPiece != null) {
|
||||
path.push(centerWallPiece);
|
||||
}
|
||||
for (const pos of path) {
|
||||
const s = structures[getCoord(pos.x, pos.y)] === STRUCTURE_ROAD
|
||||
|| structures[getCoord(pos.x + 1, pos.y)] === STRUCTURE_ROAD
|
||||
|
|
@ -436,6 +446,7 @@ export default profiler.registerFN(function RoomPlanner(room: Room) {
|
|||
structures[getCoord(pos.x, pos.y - 1)] = s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Render
|
||||
// renderHeatmap(controllerDistance, room.visual);
|
||||
|
|
@ -457,4 +468,4 @@ export default profiler.registerFN(function RoomPlanner(room: Room) {
|
|||
}
|
||||
|
||||
room.memory._planner = str;
|
||||
});
|
||||
}) as (room: Room) => void;
|
||||
|
|
|
|||
|
|
@ -10,7 +10,8 @@ export const Build
|
|||
data: {},
|
||||
});
|
||||
|
||||
export const runBuild = profiler.registerFN((creep: Creep): TaskStatus => {
|
||||
export const runBuild = profiler.registerFN(
|
||||
function runBuild(creep: Creep): TaskStatus {
|
||||
const task = creep.task;
|
||||
if (task == null) {
|
||||
return TaskStatus.DONE;
|
||||
|
|
@ -20,7 +21,7 @@ export const runBuild = profiler.registerFN((creep: Creep): TaskStatus => {
|
|||
return TaskStatus.DONE;
|
||||
}
|
||||
|
||||
const target = task.target as ConstructionSite;
|
||||
const target = task.target as ConstructionSite | null;
|
||||
if (target == null && task.targetPos.roomName === creep.room.name) {
|
||||
return TaskStatus.DONE;
|
||||
}
|
||||
|
|
@ -30,4 +31,4 @@ export const runBuild = profiler.registerFN((creep: Creep): TaskStatus => {
|
|||
creep.travelTo(task.targetPos);
|
||||
}
|
||||
return TaskStatus.IN_PROGRESS;
|
||||
}, "runBuild");
|
||||
}) as (creep: Creep) => TaskStatus;
|
||||
|
|
|
|||
|
|
@ -27,7 +27,8 @@ export const Harvest
|
|||
},
|
||||
});
|
||||
|
||||
export const runHarvest = profiler.registerFN((creep: Creep): TaskStatus => {
|
||||
export const runHarvest = profiler.registerFN(
|
||||
function runHarvest(creep: Creep): TaskStatus {
|
||||
const task = creep.task;
|
||||
if (task == null) {
|
||||
return TaskStatus.DONE;
|
||||
|
|
@ -46,4 +47,4 @@ export const runHarvest = profiler.registerFN((creep: Creep): TaskStatus => {
|
|||
creep.travelTo(task.targetPos);
|
||||
}
|
||||
return TaskStatus.IN_PROGRESS;
|
||||
}, "runHarvest");
|
||||
}) as (creep: Creep) => TaskStatus;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import profiler from "screeps-profiler";
|
||||
import { TaskData, TaskStatus, TaskType } from "./Task";
|
||||
|
||||
export const Pickup
|
||||
|
|
@ -9,7 +10,8 @@ export const Pickup
|
|||
data: { resource: target.resourceType },
|
||||
});
|
||||
|
||||
export const runPickup = (creep: Creep): TaskStatus => {
|
||||
export const runPickup = profiler.registerFN(
|
||||
function runPickup(creep: Creep): TaskStatus {
|
||||
const task = creep.task;
|
||||
if (task == null) {
|
||||
return TaskStatus.DONE;
|
||||
|
|
@ -18,7 +20,7 @@ export const runPickup = (creep: Creep): TaskStatus => {
|
|||
return TaskStatus.DONE;
|
||||
}
|
||||
|
||||
const target = task.target as Resource;
|
||||
const target = task.target as Resource | null;
|
||||
const resource: ResourceConstant
|
||||
= (task.data as { resource: ResourceConstant }).resource;
|
||||
|
||||
|
|
@ -32,4 +34,4 @@ export const runPickup = (creep: Creep): TaskStatus => {
|
|||
return TaskStatus.IN_PROGRESS;
|
||||
}
|
||||
return TaskStatus.DONE;
|
||||
};
|
||||
}) as (creep: Creep) => TaskStatus;
|
||||
|
|
|
|||
|
|
@ -10,7 +10,8 @@ export const Repair
|
|||
data: {},
|
||||
});
|
||||
|
||||
export const runRepair = profiler.registerFN((creep: Creep): TaskStatus => {
|
||||
export const runRepair = profiler.registerFN(
|
||||
function runRepair(creep: Creep): TaskStatus {
|
||||
const task = creep.task;
|
||||
if (task == null) {
|
||||
return TaskStatus.DONE;
|
||||
|
|
@ -20,7 +21,7 @@ export const runRepair = profiler.registerFN((creep: Creep): TaskStatus => {
|
|||
return TaskStatus.DONE;
|
||||
}
|
||||
|
||||
const target = task.target as Structure;
|
||||
const target = task.target as Structure | null;
|
||||
if (target == null && task.targetPos.roomName === creep.room.name) {
|
||||
return TaskStatus.DONE;
|
||||
}
|
||||
|
|
@ -31,4 +32,4 @@ export const runRepair = profiler.registerFN((creep: Creep): TaskStatus => {
|
|||
return TaskStatus.IN_PROGRESS;
|
||||
}
|
||||
return TaskStatus.DONE;
|
||||
}, "runRepair");
|
||||
}) as (creep: Creep) => TaskStatus;
|
||||
|
|
|
|||
|
|
@ -45,8 +45,10 @@ const packTaskData = (td: TaskData): string =>
|
|||
+ JSON.stringify({ o: td.options, d: td.data, t: td.target?.id });
|
||||
|
||||
const unpackTaskData = (s: string): TaskData => {
|
||||
const { o: options, d: data, t: targetId } = JSON.parse(s.substring(3));
|
||||
const target = Game.getObjectById(targetId as Id<RoomObject & _HasId>);
|
||||
const { o: options, d: data, t: targetId }
|
||||
= JSON.parse(s.substring(3)) as {
|
||||
o: object; d: object; t: Id<RoomObject & _HasId>; };
|
||||
const target = Game.getObjectById(targetId);
|
||||
return {
|
||||
type: s.charCodeAt(0) - 65,
|
||||
targetPos: unpackPos(s.substring(1, 3)),
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import profiler from "screeps-profiler";
|
||||
import { TaskData, TaskStatus, TaskType } from "./Task";
|
||||
|
||||
interface TransferOptions {
|
||||
|
|
@ -22,13 +23,14 @@ export const Transfer
|
|||
data: {},
|
||||
});
|
||||
|
||||
export const runTransfer = (creep: Creep): TaskStatus => {
|
||||
export const runTransfer = profiler.registerFN(
|
||||
function runTransfer(creep: Creep): TaskStatus {
|
||||
const task = creep.task;
|
||||
if (task == null) {
|
||||
return TaskStatus.DONE;
|
||||
}
|
||||
|
||||
const target = task.target as Structure | Creep | PowerCreep;
|
||||
const target = task.target as Structure | Creep | PowerCreep | null;
|
||||
const opts = task.options as TransferOptions;
|
||||
|
||||
if (target == null
|
||||
|
|
@ -38,4 +40,4 @@ export const runTransfer = (creep: Creep): TaskStatus => {
|
|||
return TaskStatus.IN_PROGRESS;
|
||||
}
|
||||
return TaskStatus.DONE;
|
||||
};
|
||||
}) as (creep: Creep) => TaskStatus;
|
||||
|
|
|
|||
|
|
@ -10,7 +10,8 @@ export const Upgrade
|
|||
data: {},
|
||||
});
|
||||
|
||||
export const runUpgrade = profiler.registerFN((creep: Creep): TaskStatus => {
|
||||
export const runUpgrade = profiler.registerFN(
|
||||
function runUpgrade(creep: Creep): TaskStatus {
|
||||
const task = creep.task;
|
||||
if (task == null) {
|
||||
return TaskStatus.DONE;
|
||||
|
|
@ -27,4 +28,4 @@ export const runUpgrade = profiler.registerFN((creep: Creep): TaskStatus => {
|
|||
creep.travelTo(task.targetPos);
|
||||
}
|
||||
return TaskStatus.IN_PROGRESS;
|
||||
}, "runUpgrade");
|
||||
}) as (creep: Creep) => TaskStatus;
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import profiler from "screeps-profiler";
|
||||
import { TaskData, TaskStatus, TaskType } from "./Task";
|
||||
|
||||
interface WithdrawOptions {
|
||||
|
|
@ -25,7 +26,8 @@ export const Withdraw
|
|||
data: {},
|
||||
});
|
||||
|
||||
export const runWithdraw = (creep: Creep): TaskStatus => {
|
||||
export const runWithdraw = profiler.registerFN(
|
||||
function runWithdraw(creep: Creep): TaskStatus {
|
||||
const task = creep.task;
|
||||
if (task == null) {
|
||||
return TaskStatus.DONE;
|
||||
|
|
@ -34,7 +36,7 @@ export const runWithdraw = (creep: Creep): TaskStatus => {
|
|||
return TaskStatus.DONE;
|
||||
}
|
||||
|
||||
const target = task.target as Structure | Tombstone | Ruin;
|
||||
const target = task.target as Structure | Tombstone | Ruin | null;
|
||||
const opts = task.options as WithdrawOptions;
|
||||
|
||||
if (opts.limit != null
|
||||
|
|
@ -55,4 +57,4 @@ export const runWithdraw = (creep: Creep): TaskStatus => {
|
|||
return TaskStatus.IN_PROGRESS;
|
||||
}
|
||||
return TaskStatus.DONE;
|
||||
};
|
||||
}) as (creep: Creep) => TaskStatus;
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ const runTask = profiler.registerFN((creep: Creep): TaskStatus => {
|
|||
default:
|
||||
return TaskStatus.DONE;
|
||||
}
|
||||
}, "runTask");
|
||||
}, "runTask") as (creep: Creep) => TaskStatus;
|
||||
|
||||
Creep.prototype.run = function (generator?: (creep: Creep) => TaskData | null) {
|
||||
const status = runTask(this);
|
||||
|
|
|
|||
|
|
@ -12,8 +12,10 @@ const assignTask = (creep: Creep) => {
|
|||
if (ext != null) {
|
||||
return Tasks.Transfer(ext);
|
||||
}
|
||||
if (creep.room.spawn?.store.getFreeCapacity(RESOURCE_ENERGY) ?? 0 > 0) {
|
||||
return Tasks.Transfer(creep.room.spawn!);
|
||||
if ((creep.room.spawn?.store.getFreeCapacity(RESOURCE_ENERGY) ?? 0) > 0) {
|
||||
if (creep.room.spawn != null) {
|
||||
return Tasks.Transfer(creep.room.spawn);
|
||||
}
|
||||
}
|
||||
const tower = closestTowerToFill(creep.pos);
|
||||
if (tower != null) {
|
||||
|
|
@ -49,10 +51,11 @@ const assignTask = (creep: Creep) => {
|
|||
return null;
|
||||
};
|
||||
|
||||
const body = (energy: number) => (
|
||||
const body = (energy: number): BodyPartConstant[] => (
|
||||
energy < 100
|
||||
? []
|
||||
: [WORK].concat(new Array(Math.floor((energy - 100) / 150))
|
||||
: ([WORK] as BodyPartConstant[]).concat(
|
||||
new Array<BodyPartConstant[]>(Math.floor((energy - 100) / 150))
|
||||
.fill([MOVE, CARRY, CARRY]).reduce((x, y) => x.concat(y), []))
|
||||
);
|
||||
|
||||
|
|
|
|||
|
|
@ -49,7 +49,7 @@ const assignTask = (creep: Creep) => {
|
|||
};
|
||||
|
||||
const body = (energy: number) =>
|
||||
new Array(Math.floor(energy / 250))
|
||||
new Array<BodyPartConstant[]>(Math.floor(energy / 250))
|
||||
.fill([WORK, MOVE, CARRY, CARRY]).reduce((x, y) => x.concat(y), []);
|
||||
|
||||
export const Constructor: WorkerDefinition = {
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ const assignTask = (creep: Creep) => {
|
|||
};
|
||||
|
||||
const body = (energy: number) =>
|
||||
new Array(Math.floor(energy / 300))
|
||||
new Array<BodyPartConstant[]>(Math.floor(energy / 300))
|
||||
.fill([WORK, WORK, MOVE, CARRY]).reduce((x, y) => x.concat(y), []);
|
||||
|
||||
export const Upgrader: WorkerDefinition = {
|
||||
|
|
|
|||
|
|
@ -1,43 +1,38 @@
|
|||
export const closestTowerToFill = (pos: RoomPosition): StructureTower | null =>
|
||||
pos.findClosestByRange(FIND_MY_STRUCTURES, {
|
||||
filter: structure => structure.structureType === STRUCTURE_TOWER
|
||||
&& (structure as StructureTower).store
|
||||
.getFreeCapacity(RESOURCE_ENERGY) > 0,
|
||||
&& structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0,
|
||||
});
|
||||
|
||||
export const closestExtensionToFill
|
||||
= (pos: RoomPosition): StructureExtension | null =>
|
||||
pos.findClosestByRange(FIND_MY_STRUCTURES, {
|
||||
filter: structure => structure.structureType === STRUCTURE_EXTENSION
|
||||
&& (structure as StructureExtension).store
|
||||
.getFreeCapacity(RESOURCE_ENERGY) > 0,
|
||||
&& structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0,
|
||||
});
|
||||
|
||||
export const closestContainerWithEnergy
|
||||
= (pos: RoomPosition): StructureContainer | null =>
|
||||
pos.findClosestByRange(FIND_STRUCTURES, {
|
||||
filter: structure => structure.structureType === STRUCTURE_CONTAINER
|
||||
&& (structure as StructureContainer).store
|
||||
.getUsedCapacity(RESOURCE_ENERGY) > 0,
|
||||
&& structure.store.getUsedCapacity(RESOURCE_ENERGY) > 0,
|
||||
});
|
||||
export const closestContainerToFill
|
||||
= (pos: RoomPosition): StructureContainer | null =>
|
||||
pos.findClosestByRange(FIND_STRUCTURES, {
|
||||
filter: structure => structure.structureType === STRUCTURE_CONTAINER
|
||||
&& (structure as StructureContainer).store
|
||||
.getFreeCapacity(RESOURCE_ENERGY) > 0,
|
||||
&& structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0,
|
||||
});
|
||||
|
||||
export const closestStorageWithResource
|
||||
= (pos: RoomPosition, t: ResourceConstant): StructureStorage | null =>
|
||||
pos.findClosestByRange(FIND_MY_STRUCTURES, {
|
||||
filter: structure => structure.structureType === STRUCTURE_STORAGE
|
||||
&& (structure as StructureStorage).store
|
||||
.getUsedCapacity(t) > 0,
|
||||
&& structure.store.getUsedCapacity(t) > 0,
|
||||
});
|
||||
export const closestStorageToFill
|
||||
= (pos: RoomPosition, t: ResourceConstant): StructureStorage | null =>
|
||||
pos.findClosestByRange(FIND_MY_STRUCTURES, {
|
||||
filter: structure => structure.structureType === STRUCTURE_STORAGE
|
||||
&& (structure as StructureStorage).store.getFreeCapacity(t) > 0,
|
||||
&& structure.store.getFreeCapacity(t) > 0,
|
||||
});
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@ export interface WorkerDefinition {
|
|||
}
|
||||
|
||||
export const spawnWorkers = profiler.registerFN(
|
||||
(spawn: StructureSpawn, workers: WorkerDefinition[]): void => {
|
||||
function spawnWorkers(spawn: StructureSpawn, workers: WorkerDefinition[]) {
|
||||
for (const worker of workers) {
|
||||
for (let i = 0; i < worker.requiredCreeps(spawn.room); i++) {
|
||||
const ret = spawn.spawnCreep(
|
||||
|
|
@ -21,7 +21,7 @@ export const spawnWorkers = profiler.registerFN(
|
|||
}
|
||||
}
|
||||
}
|
||||
}, "spawnWorkers");
|
||||
}) as (spawn: StructureSpawn, workers: WorkerDefinition[]) => void;
|
||||
|
||||
export const runWorkers = profiler.registerFN(
|
||||
function runWorkers(workers: WorkerDefinition[]) {
|
||||
|
|
@ -41,4 +41,4 @@ export const runWorkers = profiler.registerFN(
|
|||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}) as (workers: WorkerDefinition[]) => void;
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ const runTowers = profiler.registerFN((spawn: StructureSpawn) => {
|
|||
console.log(tower.repair(str));
|
||||
}
|
||||
};
|
||||
}, "runTowers");
|
||||
}, "runTowers") as (spawn: StructureSpawn) => void;
|
||||
|
||||
profiler.enable();
|
||||
export const loop = profiler.wrap(() => {
|
||||
|
|
@ -52,6 +52,7 @@ export const loop = profiler.wrap(() => {
|
|||
|
||||
for (const creep in Memory.creeps) {
|
||||
if (!(creep in Game.creeps)) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
|
||||
delete Memory.creeps[creep];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue