From 069e6587a9f45dac1328b304539364c94d2df5d4 Mon Sep 17 00:00:00 2001 From: Arne Schauf Date: Fri, 26 Jan 2024 15:44:02 +0100 Subject: [PATCH] autobuilding --- default/main.js | 43 +++++++++++++++++++++++++----- default/role.builder.js | 4 ++- default/role.filler.js | 1 + default/role.hauler.js | 14 +++++++--- default/role.remoteHarvester.js | 1 + default/role.reserver.js | 1 + default/role.scout.js | 1 + default/role.transporter.js | 46 +++++++++++++++++++++++++++++++-- default/role.upgrader.js | 14 +++++++--- default/spawn.js | 34 +++++++++++++++++++++--- 10 files changed, 140 insertions(+), 19 deletions(-) diff --git a/default/main.js b/default/main.js index 2a3fb40..322bae5 100644 --- a/default/main.js +++ b/default/main.js @@ -41,9 +41,32 @@ module.exports.loop = function () { } } + for (let [roomName, roomMemory] of Object.entries(Memory.rooms)) { + let room = Game.rooms[roomName] + if (!roomMemory.fatigues) { + roomMemory.fatigues = {} + } + for (let [k, v] of Object.entries(roomMemory.fatigues)) { + let [x, y] = k.split('_') + x = parseInt(x) + y = parseInt(y) + if (room && v > 20) { + room.createConstructionSite(x, y, STRUCTURE_ROAD) + roomMemory.fatigues[k] = 0 + } + if (v > 0) { + if (Game.time % 20 === 0) { + roomMemory.fatigues[k]-- + } + room.visual.text(roomMemory.fatigues[k], x, y, {size: 0.4}) + } + } + } + let spawn = Game.spawns['Spawn1'] + global.activeRemotes = spawn.room.memory.activeRemotes || [] - // Run modules +// Run modules for (let link of spawn.room.find(FIND_STRUCTURES, {filter: s => s.structureType === STRUCTURE_LINK})) { libLink.tick(link) } @@ -62,6 +85,14 @@ module.exports.loop = function () { if (ROLES[creep.memory.role].tick(creep) === 'idle') { ROLES[creep.memory.role].idling++ } + + if (creep.fatigue > 0) { + if (creep.pos.findInRange(FIND_STRUCTURES, 0, {filter: s => s.structureType === STRUCTURE_ROAD}).length === 0 + && creep.pos.findInRange(FIND_MY_CONSTRUCTION_SITES, 1, {filter: s => s.structureType === STRUCTURE_ROAD}).length === 0) { + creep.room.memory.fatigues[creep.pos.x + '_' + creep.pos.y] = (creep.room.memory.fatigues[creep.pos.x + '_' + creep.pos.y] || 0) + creep.fatigue + + } + } } libSpawn.tick(spawn) @@ -81,11 +112,11 @@ module.exports.loop = function () { } } - // for (let [role, values] of Object.entries(ROLES)) { - // if (values.idling > 0) { - // console.log(`${values.idling} ${role}s idling`) - // } - // } +// for (let [role, values] of Object.entries(ROLES)) { +// if (values.idling > 0) { +// console.log(`${values.idling} ${role}s idling`) +// } +// } spawn.room.visual.text( `${Game.cpu.getUsed().toFixed(1)} / ${Game.cpu.tickLimit} (${Game.cpu.bucket})`, diff --git a/default/role.builder.js b/default/role.builder.js index 11b4f5c..f311f86 100644 --- a/default/role.builder.js +++ b/default/role.builder.js @@ -5,7 +5,9 @@ let maxWallHits = 0 module.exports = { name: 'builder', prio: 5, - count: 1, + getCount: colonyRoom => { + return Math.min(5, Math.max(1, colonyRoom.find(FIND_MY_CONSTRUCTION_SITES).length)) + }, maxExpands: 4, baseBody: [WORK, CARRY, MOVE, MOVE], expandBody: [WORK, CARRY, MOVE, MOVE], diff --git a/default/role.filler.js b/default/role.filler.js index b275b35..f13a6c6 100644 --- a/default/role.filler.js +++ b/default/role.filler.js @@ -4,6 +4,7 @@ module.exports = { name: 'filler', prio: 4, count: 2, + minRcl: 2, bodies: [ [CARRY, CARRY, MOVE], [CARRY, CARRY, MOVE, CARRY, CARRY, MOVE], diff --git a/default/role.hauler.js b/default/role.hauler.js index 73dce7c..8af76d9 100644 --- a/default/role.hauler.js +++ b/default/role.hauler.js @@ -87,9 +87,17 @@ module.exports = { } } } else if (creep.store[RESOURCE_ENERGY] > 0) { - let storage = Game.rooms[creep.memory.colony].storage - if (creep.transfer(storage, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) { - creep.moveTo(storage) + let target = Game.rooms[creep.memory.colony].storage + if (!target) { + for (let scId of Game.rooms[creep.memory.colony].memory.spawnContainers) { + let container = Game.getObjectById(scId) + if (container && container.store.getFreeCapacity(RESOURCE_ENERGY) > 0) { + target = container + } + } + } + if (creep.transfer(target, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) { + creep.moveTo(target) } } else if (moveToRoom(creep,creep.memory.colony) === MOVING) { return diff --git a/default/role.remoteHarvester.js b/default/role.remoteHarvester.js index 428c351..c5940e7 100644 --- a/default/role.remoteHarvester.js +++ b/default/role.remoteHarvester.js @@ -3,6 +3,7 @@ let common = require('common') module.exports = { name: 'remoteHarvester', prio: 10, + minRcl: 3, bodies: [ [WORK, WORK, WORK, CARRY, MOVE, MOVE, MOVE, MOVE], [WORK, WORK, WORK, WORK, WORK, WORK, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE], diff --git a/default/role.reserver.js b/default/role.reserver.js index a8f7ee4..8e45f1f 100644 --- a/default/role.reserver.js +++ b/default/role.reserver.js @@ -1,6 +1,7 @@ module.exports = { name: 'reserver', prio: 20, + minRcl: 4, nextSpawn (spawn, roleCreeps) { for (let roomName of activeRemotes) { let room = Game.rooms[roomName] diff --git a/default/role.scout.js b/default/role.scout.js index 7a883ce..ed5042b 100644 --- a/default/role.scout.js +++ b/default/role.scout.js @@ -2,6 +2,7 @@ module.exports = { name: 'scout', count: 1, prio: 15, + minRcl: 3, bodies: [[MOVE]], nextSpawn (spawn, roleCreeps) { for (let roomName of activeRemotes) { diff --git a/default/role.transporter.js b/default/role.transporter.js index 0559a92..6d6af50 100644 --- a/default/role.transporter.js +++ b/default/role.transporter.js @@ -3,13 +3,49 @@ const common = require('common') module.exports = { name: 'transporter', prio: 4, - count: 1, + getCount: colonyRoom => Object.keys(colonyRoom.memory.sourcesData).length + + Math.floor((colonyRoom.find(FIND_DROPPED_RESOURCES).reduce((total, current) => total + current.amount, 0)) / 500) + + Math.floor((colonyRoom.find(FIND_STRUCTURES, {filter: s => s.structureType === STRUCTURE_CONTAINER && colonyRoom.memory.sourceContainers.indexOf(s.id) !== -1}).reduce((total, current) => total + current.store[RESOURCE_ENERGY], 0)) / 500) + , bodies: [ + [CARRY, MOVE], [CARRY, CARRY, MOVE], [CARRY, CARRY, MOVE, CARRY, CARRY, MOVE], [CARRY, CARRY, MOVE, CARRY, CARRY, MOVE, CARRY, CARRY, MOVE], [CARRY, CARRY, MOVE, CARRY, CARRY, MOVE, CARRY, CARRY, MOVE, CARRY, CARRY, MOVE], ], + tickInit () { + for (let room of Object.values(Game.rooms)) { + if (!room.memory.spawnContainers) { + room.memory.spawnContainers = [] + } + if (!room.memory.sourceContainers) { + room.memory.sourceContainers = [] + } + if (Game.time % 100 === 0 && room.controller.my) { + let containers = room.controller.pos.findInRange(FIND_STRUCTURES, 4, {filter: s => s.structureType === STRUCTURE_CONTAINER}) + if (containers.length > 0) { + room.memory.controllerContainer = containers[0].id + } + + room.memory.spawnContainers = [] + for (let spawn of room.find(FIND_STRUCTURES, {filter: s => s.structureType === STRUCTURE_SPAWN})) { + containers = spawn.pos.findInRange(FIND_STRUCTURES, 4, {filter: s => s.structureType === STRUCTURE_CONTAINER}) + if (containers.length > 0) { + containers.forEach(el => room.memory.spawnContainers.push(el.id)) + } + } + + room.memory.sourceContainers = [] + for (let spawn of room.find(FIND_SOURCES)) { + containers = spawn.pos.findInRange(FIND_STRUCTURES, 2, {filter: s => s.structureType === STRUCTURE_CONTAINER}) + if (containers.length > 0) { + containers.forEach(el => room.memory.sourceContainers.push(el.id)) + } + } + } + } + }, tick (creep) { if (creep.store.getFreeCapacity(RESOURCE_ENERGY) === 0) { creep.memory.loading = false @@ -45,6 +81,8 @@ module.exports = { let quickloadTargets = creep.pos.findInRange(FIND_STRUCTURES, 2, { filter: s => { return s.structureType === STRUCTURE_CONTAINER + && s.id !== creep.room.memory.controllerContainer + && creep.room.memory.spawnContainers.indexOf(s.id) === -1 && s.store[RESOURCE_ENERGY] > creep.store.getFreeCapacity(RESOURCE_ENERGY) } }) @@ -71,6 +109,8 @@ module.exports = { target = creep.pos.findClosestByPath(FIND_STRUCTURES, { filter: s => { return s.structureType === STRUCTURE_CONTAINER + && s.id !== creep.room.memory.controllerContainer + && creep.room.memory.spawnContainers.indexOf(s.id) === -1 && s.store[RESOURCE_ENERGY] > 100 } }) @@ -131,7 +171,9 @@ module.exports = { if (!target) { target = creep.pos.findClosestByPath(FIND_STRUCTURES, { filter: s => { - return s.structureType === STRUCTURE_STORAGE + return (s.structureType === STRUCTURE_STORAGE + || s.id === creep.room.memory.controllerContainer + || creep.room.memory.spawnContainers.indexOf(s.id) !== -1) && s.store.getFreeCapacity(RESOURCE_ENERGY) > 0 } }) diff --git a/default/role.upgrader.js b/default/role.upgrader.js index aefd17d..f4905c4 100644 --- a/default/role.upgrader.js +++ b/default/role.upgrader.js @@ -5,8 +5,16 @@ module.exports = { idling, name: 'upgrader', prio: 6, - count: 1, - getCount: colonyRoom => colonyRoom.storage ? Math.ceil(colonyRoom.storage.store[RESOURCE_ENERGY] / 100000) : 1, + getCount: colonyRoom => { + let cc = Game.getObjectById(colonyRoom.memory.controllerContainer) + if(colonyRoom.storage) { + return Math.ceil(colonyRoom.storage.store[RESOURCE_ENERGY] / 100000) + } + else if (colonyRoom.controller.level + cc.store) { + return Math.ceil(cc.store[RESOURCE_ENERGY] / 500) + } + return colonyRoom.controller.level + }, maxExpands: 4, baseBody: [WORK, CARRY, MOVE], expandBody: [WORK, WORK, MOVE], @@ -32,7 +40,7 @@ module.exports = { target = targets[0] } } - if (!target) { + if (!target && creep.store[RESOURCE_ENERGY] === 0) { target = creep.room.controller.pos.findClosestByRange(FIND_STRUCTURES, { filter: s => s.store && s.store.getCapacity(RESOURCE_ENERGY) > 0 }) diff --git a/default/spawn.js b/default/spawn.js index cb0ad75..381ad5e 100644 --- a/default/spawn.js +++ b/default/spawn.js @@ -27,6 +27,8 @@ function getBodyCost(body) { module.exports = { tickInit () { for (let room of Object.values(Game.rooms)) { + let spawns = room.find(FIND_MY_SPAWNS) + room.memory.spawingRoom = spawns.length > 0 if (!room.memory.harvestingSpots) { room.memory.harvestingSpots = {} } @@ -64,6 +66,27 @@ module.exports = { } } } + if (spawns.length > 0 && (Game.time + 5) % 100 === 0 && room.find(FIND_MY_CONSTRUCTION_SITES).length === 0) { + let spawn = spawns[0] + let extensions = room.find(FIND_MY_STRUCTURES, {filter: s => s.structureType === STRUCTURE_EXTENSION}) + if (CONTROLLER_STRUCTURES.extension[room.controller.level] > extensions.length) { + let ring = 0 + let x = spawn.pos.x + let y = spawn.pos.y + while (ring < 10 && room.createConstructionSite(x, y, STRUCTURE_EXTENSION) === ERR_INVALID_TARGET) { + x += 2 + if (x > spawn.pos.x + ring) { + x = spawn.pos.x - ring + y += 2 + } + if (y > spawn.pos.y + ring) { + ring++ + x = spawn.pos.x - ring + y = spawn.pos.y - ring + } + } + } + } } }, tick (spawn) { @@ -81,6 +104,9 @@ module.exports = { else { let newCreep, prio for (let [roleName, role] of Object.entries(ROLES)) { + if (role.minRcl > spawn.room.controller.level) { + continue + } let roleCreeps = _.filter(Game.creeps, {memory: {role: roleName, colony: spawn.room.name}}); if (minRoleCount === undefined || minRoleCount > roleCreeps.length) { minRoleCount = roleCreeps.length @@ -101,13 +127,13 @@ module.exports = { for (let source of spawn.room.find(FIND_SOURCES)) { let spots = spawn.room.memory.harvestingSpots[source.id] let harvestingCapacity = 0 - let sourceHarvesters = _.filter(Game.creeps, {memory: {role: 'harvester', source: source.id}}) + let sourceHarvesters = _.filter(roleCreeps, {memory: {source: source.id}}) if (sourceHarvesters.length >= spots.length) { continue } for (let harvester of sourceHarvesters) { - if (harvester.ticksToLive > harvester.body.length * 3 + source.room.memory.sourcesData[source.id].spawnDistance) { - harvestingCapacity += harvester.getActiveBodyparts(WORK) * 2 * 300 + if (!harvester.ticksToLive || harvester.ticksToLive > harvester.body.length * 3 + source.room.memory.sourcesData[source.id].spawnDistance) { + harvestingCapacity += _.filter(harvester.body, {type: WORK}).length * 2 * 300 } } if (harvestingCapacity < source.energyCapacity) { @@ -134,7 +160,7 @@ module.exports = { continue } for (let harvester of sourceHarvesters) { - if (harvester.ticksToLive > harvester.body.length * 3 + sourceData.spawnDistance) { + if (!harvester.ticksToLive || harvester.ticksToLive > harvester.body.length * 3 + sourceData.spawnDistance) { harvestingCapacity += harvester.getActiveBodyparts(WORK) * 2 * 300 } }