Add Logistics and Extension support
This commit is contained in:
parent
e2e93947b5
commit
18ff62a97b
9 changed files with 66 additions and 9 deletions
7
.swcrc
7
.swcrc
|
|
@ -7,5 +7,12 @@
|
|||
},
|
||||
"module": {
|
||||
"type": "commonjs"
|
||||
},
|
||||
"env": {
|
||||
"targets": {
|
||||
"node": "9"
|
||||
},
|
||||
"mode": "entry",
|
||||
"coreJs": "3.22"
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
25
src/Actions/withdrawEnergy.ts
Normal file
25
src/Actions/withdrawEnergy.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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(<StructureContainer | null> 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), [])
|
||||
}
|
||||
13
src/Workers/Miner.ts
Normal file
13
src/Workers/Miner.ts
Normal 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))
|
||||
}
|
||||
|
|
@ -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(<StructureContainer | null> creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: { structureType: STRUCTURE_CONTAINER }})))
|
||||
.or(harvestFromClosestActiveSource())
|
||||
.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)
|
||||
.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), [])
|
||||
}
|
||||
|
|
@ -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());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue