Allow Repairs and overhaul blueprints and creeps

This commit is contained in:
uwap 2022-12-26 11:07:08 +01:00
parent f7f39bc90a
commit 365fd17b01
7 changed files with 39 additions and 8 deletions

View file

@ -0,0 +1,23 @@
import { createAction, Fail, InProgress, Success } from "./Action";
import { moveTo } from "./moveTo";
export const repairStructure = () => createAction('repairStructure', (creep: Creep) => {
const cs = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (str) => str.hits < str.hitsMax * 0.8 });
if (!cs) {
return Fail;
}
switch (creep.repair(cs)) {
case OK: {
return InProgress;
}
case ERR_NOT_ENOUGH_RESOURCES: {
return Success;
}
case ERR_NOT_IN_RANGE: {
return moveTo(cs);
}
default: {
return Fail;
}
}
})

View file

@ -6,13 +6,18 @@ export const buildExtentions = (room: Room) => {
const spawns = room.find(FIND_MY_SPAWNS);
if (spawns.length < 1) return;
const spawn = spawns[0];
const exts = extentionsAvailable(room.controller?.level ?? 0)
const exts = extentionsAvailable(room.controller?.level ?? 0);
const terrain = room.getTerrain();
for (let x = -Math.floor(Math.sqrt(exts) / 2); x < Math.sqrt(exts) / 2; x++) {
for (let y = -Math.floor(Math.sqrt(exts) / 2); y < Math.sqrt(exts) / 2; y++) {
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 (terrain.get(spawn.pos.x + x * 2 - 1, spawn.pos.y + y * 2 - 1) !== TERRAIN_MASK_WALL) {
room.createConstructionSite(spawn.pos.x + x * 2 - 1, spawn.pos.y + y * 2 - 1, STRUCTURE_ROAD);
}
if (terrain.get(spawn.pos.x + x * 2, spawn.pos.y + y * 2 - 1) !== TERRAIN_MASK_WALL) {
room.createConstructionSite(spawn.pos.x + x * 2, spawn.pos.y + y * 2 - 1, STRUCTURE_ROAD);
}
if (terrain.get(spawn.pos.x + x * 2 - 1, spawn.pos.y + y * 2) !== TERRAIN_MASK_WALL) {
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);

View file

@ -4,9 +4,10 @@ export const buildRoads = (room: Room) => {
}
const sources: _HasRoomPosition[] = room.find(FIND_SOURCES);
const sourcesAndSpawns = sources.concat(room.find(FIND_MY_SPAWNS));
const sourcesAndMinerals = sources.concat(room.find(FIND_MINERALS));
room.visual.clear();
for (const source of sourcesAndSpawns) {
for (const source2 of sources) {
for (const source2 of (room.controller?.level ?? 0) > 4 ? sourcesAndMinerals : sources) {
const path = source.pos.findPathTo(source2, {
ignoreCreeps: true,
ignoreRoads: true

View file

@ -1,17 +1,19 @@
import { Fail, runAction } from "../Actions/Action";
import { buildConstructionSite } from "../Actions/buildConstructionSite";
import { harvestFromClosestActiveSource } from "../Actions/harvest";
import { repairStructure } from "../Actions/repairStructure";
import { upgradeController } from "../Actions/upgradeController";
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: { structureType: STRUCTURE_CONTAINER }}) as StructureContainer | null))
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))
.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 / 300)).fill([WORK, MOVE, MOVE, CARRY, CARRY]).reduce((x, y) => x.concat(y), [])
bodyDefinition: (energy: number) => new Array(Math.floor(energy / 250)).fill([WORK, MOVE, CARRY, CARRY]).reduce((x, y) => x.concat(y), [])
}

View file

@ -5,7 +5,7 @@ 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 }}) as StructureContainer | null))
.andThen(transferEnergy(creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (str: Structure) => str.structureType === STRUCTURE_CONTAINER && (str as StructureContainer).store.getFreeCapacity(RESOURCE_ENERGY) > 0}) as StructureContainer | null))
.repeat(),
name: 'miner',
requiredCreeps: (room: Room) => room.find(FIND_STRUCTURES, { filter: { structureType: STRUCTURE_CONTAINER }}).length > 0 ? 4 : 0,

View file

@ -6,7 +6,7 @@ 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: { structureType: STRUCTURE_CONTAINER }}) as StructureContainer | null))
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))

View file

@ -17,7 +17,7 @@ export function loop() {
if (!controller) {
return;
}
const workerTypes = [Constructor, Upgrader, Miner];
const workerTypes = [Upgrader, Miner, Constructor];
spawnWorkers(spawn, workerTypes);
runWorkers(spawn, workerTypes);
if (Game.time % 100 === 0) {