diff --git a/.swcrc b/.swcrc index 096bb29..c2f739c 100644 --- a/.swcrc +++ b/.swcrc @@ -7,5 +7,12 @@ }, "module": { "type": "commonjs" + }, + "env": { + "targets": { + "node": "9" + }, + "mode": "entry", + "coreJs": "3.22" } } \ No newline at end of file diff --git a/src/Actions/transferEnergy.ts b/src/Actions/transferEnergy.ts index 7e50646..8528040 100644 --- a/src/Actions/transferEnergy.ts +++ b/src/Actions/transferEnergy.ts @@ -1,7 +1,10 @@ import { createAction, Fail, InProgress, Success } from "./Action"; import { moveTo } from "./moveTo"; -export const transferEnergy = (target: Creep | StructureSpawn) => createAction('transferEnergy', (creep: Creep) => { +export const transferEnergy = (target: Creep | StructureSpawn | StructureContainer | StructureExtension | null) => createAction('transferEnergy', (creep: Creep) => { + if (target == null) { + return Fail; + } if (target.store.getFreeCapacity(RESOURCE_ENERGY) === 0) { return Fail; } diff --git a/src/Actions/withdrawEnergy.ts b/src/Actions/withdrawEnergy.ts new file mode 100644 index 0000000..7e2a3a7 --- /dev/null +++ b/src/Actions/withdrawEnergy.ts @@ -0,0 +1,25 @@ +import { createAction, Fail, InProgress, Success } from "./Action"; +import { moveTo } from "./moveTo"; + +export const withdrawEnergy = (target: StructureContainer | null) => createAction('transferEnergy', (creep: Creep) => { + if (target == null) { + return Fail; + } + if (target.store.getUsedCapacity(RESOURCE_ENERGY) === 0) { + return Fail; + } + switch(creep.withdraw(target, RESOURCE_ENERGY)) { + case OK: { + return InProgress; + } + case ERR_FULL: { + return Success; + } + case ERR_NOT_IN_RANGE: { + return moveTo(target); + } + default: { + return Fail; + } + } +}); \ No newline at end of file diff --git a/src/RoomPlanner/Blueprints/Extensions.ts b/src/RoomPlanner/Blueprints/Extensions.ts index 4ce72ee..b7b0bd6 100644 --- a/src/RoomPlanner/Blueprints/Extensions.ts +++ b/src/RoomPlanner/Blueprints/Extensions.ts @@ -12,6 +12,8 @@ export const buildExtentions = (room: Room) => { room.visual.circle(spawn.pos.x + x * 2, spawn.pos.y + y * 2); if (!room.lookAt(spawn.pos.x + x * 2 - 1, spawn.pos.y + y * 2 - 1).includes({ type: 'terrain', terrain: 'wall'})) { room.createConstructionSite(spawn.pos.x + x * 2 - 1, spawn.pos.y + y * 2 - 1, STRUCTURE_ROAD); + room.createConstructionSite(spawn.pos.x + x * 2, spawn.pos.y + y * 2 - 1, STRUCTURE_ROAD); + room.createConstructionSite(spawn.pos.x + x * 2 - 1, spawn.pos.y + y * 2, STRUCTURE_ROAD); } room.createConstructionSite(spawn.pos.x + x * 2, spawn.pos.y + y * 2, STRUCTURE_EXTENSION); } diff --git a/src/Workers/Constructor.ts b/src/Workers/Constructor.ts index 7bf50dd..548283b 100644 --- a/src/Workers/Constructor.ts +++ b/src/Workers/Constructor.ts @@ -2,14 +2,16 @@ import { Fail, runAction } from "../Actions/Action"; import { buildConstructionSite } from "../Actions/buildConstructionSite"; import { harvestFromClosestActiveSource } from "../Actions/harvest"; import { upgradeController } from "../Actions/upgradeController"; +import { withdrawEnergy } from "../Actions/withdrawEnergy"; import { WorkerDefinition } from "./worker"; export const Constructor: WorkerDefinition = { - runAction: (creep: Creep) => runAction(creep, harvestFromClosestActiveSource()) + runAction: (creep: Creep) => runAction(creep, withdrawEnergy( creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: { structureType: STRUCTURE_CONTAINER }}))) + .or(harvestFromClosestActiveSource()) .andThen(buildConstructionSite()) .or(creep.room.controller ? upgradeController(creep.room.controller) : Fail) .repeat(), name: 'constructor', requiredCreeps: (room: Room) => room.find(FIND_MY_CONSTRUCTION_SITES).length / 5 + 1, - bodyDefinition: (energy: number) => [WORK, MOVE, MOVE, CARRY, CARRY] + bodyDefinition: (energy: number) => new Array(Math.floor(energy / 300)).fill([WORK, MOVE, MOVE, CARRY, CARRY]).reduce((x, y) => x.concat(y), []) } \ No newline at end of file diff --git a/src/Workers/Miner.ts b/src/Workers/Miner.ts new file mode 100644 index 0000000..b8298d2 --- /dev/null +++ b/src/Workers/Miner.ts @@ -0,0 +1,13 @@ +import { Fail, runAction } from "../Actions/Action"; +import { harvestFromClosestActiveSource } from "../Actions/harvest"; +import { transferEnergy } from "../Actions/transferEnergy"; +import { WorkerDefinition } from "./worker"; + +export const Miner: WorkerDefinition = { + runAction: (creep: Creep) => runAction(creep, harvestFromClosestActiveSource()) + .andThen(transferEnergy( creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: { structureType: STRUCTURE_CONTAINER }}))) + .repeat(), + name: 'miner', + requiredCreeps: (room: Room) => room.find(FIND_STRUCTURES, { filter: { structureType: STRUCTURE_CONTAINER }}).length > 0 ? 4 : 0, + bodyDefinition: (energy: number) => [WORK, WORK, MOVE, CARRY].concat(new Array(Math.floor((energy - 300) / 100)).fill(WORK)) +} \ No newline at end of file diff --git a/src/Workers/Upgrader.ts b/src/Workers/Upgrader.ts index 3a2686e..b721318 100644 --- a/src/Workers/Upgrader.ts +++ b/src/Workers/Upgrader.ts @@ -2,14 +2,17 @@ import { Fail, runAction } from "../Actions/Action"; import { harvestFromClosestActiveSource } from "../Actions/harvest"; import { transferEnergy } from "../Actions/transferEnergy"; import { upgradeController } from "../Actions/upgradeController"; +import { withdrawEnergy } from "../Actions/withdrawEnergy"; import { WorkerDefinition } from "./worker"; export const Upgrader: WorkerDefinition = { - runAction: (creep: Creep, spawn: StructureSpawn) => runAction(creep, harvestFromClosestActiveSource()) + runAction: (creep: Creep, spawn: StructureSpawn) => runAction(creep, withdrawEnergy( creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: { structureType: STRUCTURE_CONTAINER }}))) + .or(harvestFromClosestActiveSource()) .andThen(transferEnergy(spawn)) + .or(transferEnergy( creep.pos.findClosestByRange(FIND_MY_STRUCTURES, { filter: (structure: StructureExtension) => structure.structureType === STRUCTURE_EXTENSION && structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0}))) .or(creep.room.controller ? upgradeController(creep.room.controller) : Fail) .repeat(), name: 'upgrader', - requiredCreeps: (room: Room) => 2, - bodyDefinition: (energy: number) => [WORK, WORK, MOVE, CARRY] + requiredCreeps: (room: Room) => 4, + bodyDefinition: (energy: number) => new Array(Math.floor(energy / 300)).fill([WORK, WORK, MOVE, CARRY]).reduce((x, y) => x.concat(y), []) } \ No newline at end of file diff --git a/src/Workers/worker.ts b/src/Workers/worker.ts index 858a13f..4f0515e 100644 --- a/src/Workers/worker.ts +++ b/src/Workers/worker.ts @@ -8,7 +8,7 @@ export interface WorkerDefinition { export const spawnWorkers = (spawn: StructureSpawn, workers: WorkerDefinition[]): void => { for (const worker of workers) { for (let i = 0; i < worker.requiredCreeps(spawn.room); i++) { - spawn.spawnCreep(worker.bodyDefinition(spawn.store.getCapacity(RESOURCE_ENERGY), spawn), worker.name + i.toString()); + const ret = spawn.spawnCreep(worker.bodyDefinition(spawn.store.getCapacity(RESOURCE_ENERGY) + spawn.room.find(FIND_MY_STRUCTURES, { filter: { structureType: STRUCTURE_EXTENSION }}).length * 50, spawn), worker.name + i.toString()); } } }; diff --git a/src/index.ts b/src/index.ts index 5891d98..ebf5daf 100644 --- a/src/index.ts +++ b/src/index.ts @@ -7,6 +7,7 @@ import { buildContainers } from "./RoomPlanner/Blueprints/Containers"; import { buildExtentions } from "./RoomPlanner/Blueprints/Extensions"; import { buildRoads } from "./RoomPlanner/Blueprints/Roads"; import { Constructor } from "./Workers/Constructor"; +import { Miner } from "./Workers/Miner"; import { Upgrader } from "./Workers/Upgrader"; import { runWorkers, spawnWorkers } from "./Workers/worker"; @@ -16,8 +17,9 @@ export function loop() { if (!controller) { return; } - spawnWorkers(spawn, [Constructor, Upgrader]); - runWorkers(spawn, [Constructor, Upgrader]); + const workerTypes = [Constructor, Upgrader, Miner]; + spawnWorkers(spawn, workerTypes); + runWorkers(spawn, workerTypes); if (Game.time % 100 === 0) { buildRoads(spawn.room); }