Builder Creeps and Extension Blueprints

This commit is contained in:
uwap 2022-12-21 15:19:45 +01:00
parent 48350d6f33
commit 5e6618edca
5 changed files with 62 additions and 10 deletions

View file

@ -51,7 +51,7 @@ export const createAction = (name: string, action: (creep: Creep) => Action): Ac
if ((creep.memory.state ?? 0) > state) { if ((creep.memory.state ?? 0) > state) {
return Success(creep, state); return Success(creep, state);
} }
console.log(`[${creep.name}] Running action ${name}`) //console.log(`[${creep.name}] Running action ${name}`)
return action(creep)(creep, state); return action(creep)(creep, state);
} }
} }

View file

@ -0,0 +1,23 @@
import { createAction, Fail, InProgress, Success } from "./Action";
import { moveTo } from "./moveTo";
export const buildConstructionSite = () => createAction('buildConstructionSite', (creep: Creep) => {
const cs = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES);
if (!cs) {
return Fail;
}
switch (creep.build(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

@ -0,0 +1,16 @@
const extentionsAvailable = (roomlevel: number) => {
return roomlevel > 2 ? roomlevel * 10 - 20 : (roomlevel - 1) * 5;
}
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)
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);
room.createConstructionSite(spawn.pos.x + x * 2, spawn.pos.y + y * 2, STRUCTURE_EXTENSION);
}
}
}

View file

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

View file

@ -1,7 +1,9 @@
import { runAction } from "./Actions/Action"; import { runAction } from "./Actions/Action";
import { buildConstructionSite } from "./Actions/buildConstructionSite";
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 { buildExtentions } from "./RoomPlanner/Blueprints/Extensions";
import { buildRoads } from "./RoomPlanner/Blueprints/Roads"; import { buildRoads } from "./RoomPlanner/Blueprints/Roads";
export function loop() { export function loop() {
@ -10,21 +12,31 @@ export function loop() {
if (!controller) { if (!controller) {
return; return;
} }
spawn.spawnCreep([WORK, CARRY, CARRY, MOVE], 'w6'); spawn.spawnCreep([WORK, CARRY, CARRY, MOVE], 'builder6');
spawn.spawnCreep([WORK, CARRY, CARRY, MOVE], 'w5'); spawn.spawnCreep([WORK, CARRY, CARRY, MOVE], 'uprader5');
spawn.spawnCreep([WORK, CARRY, CARRY, MOVE], 'w4'); spawn.spawnCreep([WORK, CARRY, CARRY, MOVE], 'builder4');
spawn.spawnCreep([WORK, CARRY, CARRY, MOVE], 'w3'); spawn.spawnCreep([WORK, CARRY, CARRY, MOVE], 'builder3');
spawn.spawnCreep([WORK, CARRY, CARRY, MOVE], 'w2'); spawn.spawnCreep([WORK, CARRY, CARRY, MOVE], 'upgrader2');
spawn.spawnCreep([WORK, CARRY, CARRY, MOVE], 'w1'); spawn.spawnCreep([WORK, CARRY, CARRY, MOVE], 'upgrader1');
for (const creep of Object.values(Game.creeps)) { for (const creep of Object.values(Game.creeps)) {
if (creep.name.startsWith('u') || creep.name.startsWith('w')) {
runAction(creep, harvestFromClosestActiveSource()) runAction(creep, harvestFromClosestActiveSource())
.andThen(transferEnergy(spawn)) .andThen(transferEnergy(spawn))
.or(upgradeController(controller)) .or(upgradeController(controller))
.repeat() .repeat()
} else if (creep.name.startsWith('b')) {
runAction(creep, harvestFromClosestActiveSource())
.andThen(buildConstructionSite())
.or(upgradeController(controller))
.repeat()
}
} }
if (Game.time % 100 === 0) { if (Game.time % 100 === 0) {
buildRoads(spawn.room); buildRoads(spawn.room);
} }
if (Game.time % 100 === 50) {
buildExtentions(spawn.room)
}
if (Game.cpu.bucket === 10000) { if (Game.cpu.bucket === 10000) {
Game.cpu.generatePixel(); Game.cpu.generatePixel();
} }