diff --git a/src/Actions/transferEnergy.ts b/src/Actions/transferEnergy.ts index 8528040..43c21ca 100644 --- a/src/Actions/transferEnergy.ts +++ b/src/Actions/transferEnergy.ts @@ -1,7 +1,7 @@ import { createAction, Fail, InProgress, Success } from "./Action"; import { moveTo } from "./moveTo"; -export const transferEnergy = (target: Creep | StructureSpawn | StructureContainer | StructureExtension | null) => createAction('transferEnergy', (creep: Creep) => { +export const transferEnergy = (target: Creep | StructureSpawn | StructureContainer | StructureExtension | StructureStorage | null) => createAction('transferEnergy', (creep: Creep) => { if (target == null) { return Fail; } diff --git a/src/Actions/withdrawEnergy.ts b/src/Actions/withdrawEnergy.ts index 7e2a3a7..92cb007 100644 --- a/src/Actions/withdrawEnergy.ts +++ b/src/Actions/withdrawEnergy.ts @@ -1,7 +1,7 @@ import { createAction, Fail, InProgress, Success } from "./Action"; import { moveTo } from "./moveTo"; -export const withdrawEnergy = (target: StructureContainer | null) => createAction('transferEnergy', (creep: Creep) => { +export const withdrawEnergy = (target: StructureContainer | StructureStorage | null) => createAction('transferEnergy', (creep: Creep) => { if (target == null) { return Fail; } diff --git a/src/RoomPlanner/Blueprints/Containers.ts b/src/RoomPlanner/Blueprints/Containers.ts index 3814ecd..650a02d 100644 --- a/src/RoomPlanner/Blueprints/Containers.ts +++ b/src/RoomPlanner/Blueprints/Containers.ts @@ -1,5 +1,9 @@ export const buildContainers = (room: Room) => { - if ((room.controller?.level ?? 0) < 2) { + const controller = room.controller; + if (controller == null) { + return; + } + if (controller.level < 2) { return; } const sources = room.find(FIND_SOURCES); @@ -9,4 +13,17 @@ export const buildContainers = (room: Room) => { room.createConstructionSite(source.pos.x, source.pos.y - 2, STRUCTURE_CONTAINER); room.createConstructionSite(source.pos.x, source.pos.y + 2, STRUCTURE_CONTAINER); } + if (controller.level < 4) { + return; + } + const terrain = room.getTerrain(); + if (terrain.get(controller.pos.x, controller.pos.y - 3) !== TERRAIN_MASK_WALL) { + room.createConstructionSite(controller.pos.x, controller.pos.y - 3, STRUCTURE_STORAGE); + } else if (terrain.get(controller.pos.x, controller.pos.y + 3) !== TERRAIN_MASK_WALL) { + room.createConstructionSite(controller.pos.x, controller.pos.y + 3, STRUCTURE_STORAGE); + } else if (terrain.get(controller.pos.x - 3, controller.pos.y) !== TERRAIN_MASK_WALL) { + room.createConstructionSite(controller.pos.x - 3, controller.pos.y, STRUCTURE_STORAGE); + } else if (terrain.get(controller.pos.x + 3, controller.pos.y) !== TERRAIN_MASK_WALL) { + room.createConstructionSite(controller.pos.x + 3, controller.pos.y, STRUCTURE_STORAGE); + } } \ No newline at end of file diff --git a/src/Workers/Clerk.ts b/src/Workers/Clerk.ts new file mode 100644 index 0000000..9008f0d --- /dev/null +++ b/src/Workers/Clerk.ts @@ -0,0 +1,23 @@ +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 Clerk: WorkerDefinition = { + runAction: (creep: Creep, spawn: StructureSpawn) => runAction(creep, withdrawEnergy(creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (str: Structure) => str.structureType === STRUCTURE_CONTAINER && (str as StructureContainer).store.getUsedCapacity(RESOURCE_ENERGY) > 0 }) as StructureContainer | null)) + .or(harvestFromClosestActiveSource()) + .andThen(transferEnergy(spawn)) + .or(transferEnergy(creep.pos.findClosestByRange(FIND_MY_STRUCTURES, { filter: (structure: AnyOwnedStructure) => structure.structureType === STRUCTURE_EXTENSION && structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0}) as StructureExtension | null)) + .or(transferEnergy(creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (str: Structure) => str.structureType === STRUCTURE_STORAGE && (str as StructureStorage).store.getFreeCapacity(RESOURCE_ENERGY) > 0 }) as StructureStorage | null)) + .or(creep.room.controller ? upgradeController(creep.room.controller) : Fail) + .repeat(), + name: 'clerk', + requiredCreeps: (room: Room) => 2, + bodyDefinition: (energy: number) => [WORK].concat(new Array(Math.floor(energy / 150)).fill([MOVE, CARRY, CARRY]).reduce((x, y) => x.concat(y), [])), + motivationalThougts: [ + "Carrying 🎒", + "💗 working" + ] +} \ No newline at end of file diff --git a/src/Workers/Constructor.ts b/src/Workers/Constructor.ts index c45ec02..91a345c 100644 --- a/src/Workers/Constructor.ts +++ b/src/Workers/Constructor.ts @@ -7,13 +7,18 @@ import { withdrawEnergy } from "../Actions/withdrawEnergy"; import { WorkerDefinition } from "./worker"; export const Constructor: WorkerDefinition = { - runAction: (creep: Creep) => runAction(creep, withdrawEnergy(creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (str: Structure) => str.structureType === STRUCTURE_CONTAINER && (str as StructureContainer).store.getUsedCapacity(RESOURCE_ENERGY) > 0 }) as StructureContainer | null)) + runAction: (creep: Creep) => runAction(creep, withdrawEnergy(creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (str: Structure) => str.structureType === STRUCTURE_STORAGE && (str as StructureStorage).store.getUsedCapacity(RESOURCE_ENERGY) > 0 }) as StructureStorage | null)) + .or(withdrawEnergy(creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (str: Structure) => str.structureType === STRUCTURE_CONTAINER && (str as StructureContainer).store.getUsedCapacity(RESOURCE_ENERGY) > 0 }) as StructureContainer | null)) .or(harvestFromClosestActiveSource()) .andThen(buildConstructionSite()) .or(repairStructure()) .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) => new Array(Math.floor(energy / 250)).fill([WORK, MOVE, CARRY, CARRY]).reduce((x, y) => x.concat(y), []) + requiredCreeps: (room: Room) => 1, + bodyDefinition: (energy: number) => new Array(Math.floor(energy / 250)).fill([WORK, MOVE, CARRY, CARRY]).reduce((x, y) => x.concat(y), []), + motivationalThougts: [ + "I 💗 making", + "Fixin' 🔧" + ] } \ No newline at end of file diff --git a/src/Workers/Miner.ts b/src/Workers/Miner.ts index 2be4422..8332e33 100644 --- a/src/Workers/Miner.ts +++ b/src/Workers/Miner.ts @@ -9,5 +9,8 @@ export const Miner: WorkerDefinition = { .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)) + bodyDefinition: (energy: number) => [WORK, WORK, MOVE, CARRY].concat(new Array(Math.floor((energy - 300) / 100)).fill(WORK)), + motivationalThougts: [ + "RocknStone" + ] } \ No newline at end of file diff --git a/src/Workers/Upgrader.ts b/src/Workers/Upgrader.ts index f192c77..a1f13aa 100644 --- a/src/Workers/Upgrader.ts +++ b/src/Workers/Upgrader.ts @@ -6,13 +6,15 @@ import { withdrawEnergy } from "../Actions/withdrawEnergy"; import { WorkerDefinition } from "./worker"; export const Upgrader: WorkerDefinition = { - runAction: (creep: Creep, spawn: StructureSpawn) => runAction(creep, withdrawEnergy(creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (str: Structure) => str.structureType === STRUCTURE_CONTAINER && (str as StructureContainer).store.getUsedCapacity(RESOURCE_ENERGY) > 0 }) as StructureContainer | null)) + runAction: (creep: Creep, spawn: StructureSpawn) => runAction(creep, withdrawEnergy(creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (str: Structure) => str.structureType === STRUCTURE_STORAGE && (str as StructureStorage).store.getUsedCapacity(RESOURCE_ENERGY) > 0 }) as StructureStorage | null)) + .or(withdrawEnergy(creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (str: Structure) => str.structureType === STRUCTURE_CONTAINER && (str as StructureContainer).store.getUsedCapacity(RESOURCE_ENERGY) > 0 }) as StructureContainer | null)) .or(harvestFromClosestActiveSource()) - .andThen(transferEnergy(spawn)) - .or(transferEnergy(creep.pos.findClosestByRange(FIND_MY_STRUCTURES, { filter: (structure: AnyOwnedStructure) => structure.structureType === STRUCTURE_EXTENSION && structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0}) as StructureExtension | null)) - .or(creep.room.controller ? upgradeController(creep.room.controller) : Fail) + .andThen(creep.room.controller ? upgradeController(creep.room.controller) : Fail) .repeat(), name: 'upgrader', - requiredCreeps: (room: Room) => 4, - bodyDefinition: (energy: number) => new Array(Math.floor(energy / 300)).fill([WORK, WORK, MOVE, CARRY]).reduce((x, y) => x.concat(y), []) + requiredCreeps: (room: Room) => 1, + bodyDefinition: (energy: number) => new Array(Math.floor(energy / 300)).fill([WORK, WORK, MOVE, CARRY]).reduce((x, y) => x.concat(y), []), + motivationalThougts: [ + '🏳️‍⚧️' + ] } \ No newline at end of file diff --git a/src/Workers/worker.ts b/src/Workers/worker.ts index 4f0515e..6ce713b 100644 --- a/src/Workers/worker.ts +++ b/src/Workers/worker.ts @@ -2,13 +2,17 @@ export interface WorkerDefinition { runAction: (creep: Creep, spawn: StructureSpawn) => void, name: string, requiredCreeps: (room: Room) => number, - bodyDefinition: (energy: number, spawn: StructureSpawn) => BodyPartConstant[] + bodyDefinition: (energy: number, spawn: StructureSpawn) => BodyPartConstant[], + motivationalThougts?: Array } export const spawnWorkers = (spawn: StructureSpawn, workers: WorkerDefinition[]): void => { for (const worker of workers) { for (let i = 0; i < worker.requiredCreeps(spawn.room); i++) { 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()); + if (ret === OK || ret === ERR_NOT_ENOUGH_ENERGY) { + return; + } } } }; @@ -16,8 +20,14 @@ export const spawnWorkers = (spawn: StructureSpawn, workers: WorkerDefinition[]) export const runWorkers = (spawn: StructureSpawn, workers: WorkerDefinition[]): void => { for (const worker of workers) { for (const creep of Object.values(Game.creeps)) { + if (creep.spawning) { + continue; + } if (creep.name.startsWith(worker.name)) { worker.runAction(creep, spawn); + if (worker.motivationalThougts != null && Math.random() < 0.1) { + creep.say(worker.motivationalThougts[Math.floor(worker.motivationalThougts.length * Math.random())], true); + } } } } diff --git a/src/index.ts b/src/index.ts index 7f15aee..bbab223 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,6 +6,7 @@ import { upgradeController } from "./Actions/upgradeController"; import { buildContainers } from "./RoomPlanner/Blueprints/Containers"; import { buildExtentions } from "./RoomPlanner/Blueprints/Extensions"; import { buildRoads } from "./RoomPlanner/Blueprints/Roads"; +import { Clerk } from "./Workers/Clerk"; import { Constructor } from "./Workers/Constructor"; import { Miner } from "./Workers/Miner"; import { Upgrader } from "./Workers/Upgrader"; @@ -17,7 +18,7 @@ export function loop() { if (!controller) { return; } - const workerTypes = [Upgrader, Miner, Constructor]; + const workerTypes = [Clerk, Upgrader, Miner, Constructor]; spawnWorkers(spawn, workerTypes); runWorkers(spawn, workerTypes); if (Game.time % 100 === 0) {