Add Logistics and Extension support

This commit is contained in:
uwap 2022-12-23 09:02:22 +01:00
parent e2e93947b5
commit 18ff62a97b
9 changed files with 66 additions and 9 deletions

7
.swcrc
View file

@ -7,5 +7,12 @@
}, },
"module": { "module": {
"type": "commonjs" "type": "commonjs"
},
"env": {
"targets": {
"node": "9"
},
"mode": "entry",
"coreJs": "3.22"
} }
} }

View file

@ -1,7 +1,10 @@
import { createAction, Fail, InProgress, Success } from "./Action"; import { createAction, Fail, InProgress, Success } from "./Action";
import { moveTo } from "./moveTo"; 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) { if (target.store.getFreeCapacity(RESOURCE_ENERGY) === 0) {
return Fail; return Fail;
} }

View file

@ -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;
}
}
});

View file

@ -12,6 +12,8 @@ export const buildExtentions = (room: Room) => {
room.visual.circle(spawn.pos.x + x * 2, spawn.pos.y + y * 2); 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'})) { 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 - 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); room.createConstructionSite(spawn.pos.x + x * 2, spawn.pos.y + y * 2, STRUCTURE_EXTENSION);
} }

View file

@ -2,14 +2,16 @@ import { Fail, runAction } from "../Actions/Action";
import { buildConstructionSite } from "../Actions/buildConstructionSite"; import { buildConstructionSite } from "../Actions/buildConstructionSite";
import { harvestFromClosestActiveSource } from "../Actions/harvest"; import { harvestFromClosestActiveSource } from "../Actions/harvest";
import { upgradeController } from "../Actions/upgradeController"; import { upgradeController } from "../Actions/upgradeController";
import { withdrawEnergy } from "../Actions/withdrawEnergy";
import { WorkerDefinition } from "./worker"; import { WorkerDefinition } from "./worker";
export const Constructor: WorkerDefinition = { export const Constructor: WorkerDefinition = {
runAction: (creep: Creep) => runAction(creep, harvestFromClosestActiveSource()) runAction: (creep: Creep) => runAction(creep, withdrawEnergy(<StructureContainer | null> creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: { structureType: STRUCTURE_CONTAINER }})))
.or(harvestFromClosestActiveSource())
.andThen(buildConstructionSite()) .andThen(buildConstructionSite())
.or(creep.room.controller ? upgradeController(creep.room.controller) : Fail) .or(creep.room.controller ? upgradeController(creep.room.controller) : Fail)
.repeat(), .repeat(),
name: 'constructor', name: 'constructor',
requiredCreeps: (room: Room) => room.find(FIND_MY_CONSTRUCTION_SITES).length / 5 + 1, 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), [])
} }

13
src/Workers/Miner.ts Normal file
View file

@ -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(<StructureContainer | null> 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))
}

View file

@ -2,14 +2,17 @@ import { Fail, runAction } from "../Actions/Action";
import { harvestFromClosestActiveSource } from "../Actions/harvest"; import { harvestFromClosestActiveSource } from "../Actions/harvest";
import { transferEnergy } from "../Actions/transferEnergy"; import { transferEnergy } from "../Actions/transferEnergy";
import { upgradeController } from "../Actions/upgradeController"; import { upgradeController } from "../Actions/upgradeController";
import { withdrawEnergy } from "../Actions/withdrawEnergy";
import { WorkerDefinition } from "./worker"; import { WorkerDefinition } from "./worker";
export const Upgrader: WorkerDefinition = { export const Upgrader: WorkerDefinition = {
runAction: (creep: Creep, spawn: StructureSpawn) => runAction(creep, harvestFromClosestActiveSource()) runAction: (creep: Creep, spawn: StructureSpawn) => runAction(creep, withdrawEnergy(<StructureContainer | null> creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: { structureType: STRUCTURE_CONTAINER }})))
.or(harvestFromClosestActiveSource())
.andThen(transferEnergy(spawn)) .andThen(transferEnergy(spawn))
.or(transferEnergy(<StructureExtension | null> 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) .or(creep.room.controller ? upgradeController(creep.room.controller) : Fail)
.repeat(), .repeat(),
name: 'upgrader', name: 'upgrader',
requiredCreeps: (room: Room) => 2, requiredCreeps: (room: Room) => 4,
bodyDefinition: (energy: number) => [WORK, WORK, MOVE, CARRY] bodyDefinition: (energy: number) => new Array(Math.floor(energy / 300)).fill([WORK, WORK, MOVE, CARRY]).reduce((x, y) => x.concat(y), [])
} }

View file

@ -8,7 +8,7 @@ export interface WorkerDefinition {
export const spawnWorkers = (spawn: StructureSpawn, workers: WorkerDefinition[]): void => { export const spawnWorkers = (spawn: StructureSpawn, workers: WorkerDefinition[]): void => {
for (const worker of workers) { for (const worker of workers) {
for (let i = 0; i < worker.requiredCreeps(spawn.room); i++) { 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());
} }
} }
}; };

View file

@ -7,6 +7,7 @@ import { buildContainers } from "./RoomPlanner/Blueprints/Containers";
import { buildExtentions } from "./RoomPlanner/Blueprints/Extensions"; import { buildExtentions } from "./RoomPlanner/Blueprints/Extensions";
import { buildRoads } from "./RoomPlanner/Blueprints/Roads"; import { buildRoads } from "./RoomPlanner/Blueprints/Roads";
import { Constructor } from "./Workers/Constructor"; import { Constructor } from "./Workers/Constructor";
import { Miner } from "./Workers/Miner";
import { Upgrader } from "./Workers/Upgrader"; import { Upgrader } from "./Workers/Upgrader";
import { runWorkers, spawnWorkers } from "./Workers/worker"; import { runWorkers, spawnWorkers } from "./Workers/worker";
@ -16,8 +17,9 @@ export function loop() {
if (!controller) { if (!controller) {
return; return;
} }
spawnWorkers(spawn, [Constructor, Upgrader]); const workerTypes = [Constructor, Upgrader, Miner];
runWorkers(spawn, [Constructor, Upgrader]); spawnWorkers(spawn, workerTypes);
runWorkers(spawn, workerTypes);
if (Game.time % 100 === 0) { if (Game.time % 100 === 0) {
buildRoads(spawn.room); buildRoads(spawn.room);
} }