From d5956ede22e3b2dadb24e35ed79af778d64afaf9 Mon Sep 17 00:00:00 2001 From: ObeseTermite Date: Fri, 6 Jun 2025 18:55:25 -0700 Subject: [PATCH] Added progress checking, and automatic source delegation for creeps, when there is more than 1 source --- role_dispatcher.js | 2 +- role_harvester.js | 8 ++++-- util_energy.js | 68 +++++++++++++++++++++++++++++++++++----------- util_progress.js | 9 +++--- 4 files changed, 63 insertions(+), 24 deletions(-) diff --git a/role_dispatcher.js b/role_dispatcher.js index fc3dc31..b1209bb 100644 --- a/role_dispatcher.js +++ b/role_dispatcher.js @@ -15,7 +15,7 @@ const roleMap = { var creepCounts = { "harvester" : 3, - "builder" : 1, + "builder" : 2, "upgrader" : 2, "repairer" : 1, "streltsy" : 2 diff --git a/role_harvester.js b/role_harvester.js index 4393b2d..e3ba40b 100644 --- a/role_harvester.js +++ b/role_harvester.js @@ -4,9 +4,11 @@ var name = 'harvester'; function run(creep) { if(creep.store.getFreeCapacity() > 0) { - var sources = creep.room.find(FIND_SOURCES); - if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { - creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}}); + if(typeof creep.memory.assignedSource === 'undefined') energyUtils.sourceDelegate(creep); + var source = Game.getObjectById(creep.memory.assignedSource); + + if(creep.harvest(source) == ERR_NOT_IN_RANGE) { + creep.moveTo(source, {visualizePathStyle: {stroke: '#ffaa00'}}); } } else { diff --git a/util_energy.js b/util_energy.js index 32a89c8..2801f4d 100644 --- a/util_energy.js +++ b/util_energy.js @@ -1,25 +1,61 @@ var energyUtils = { gatherEnergy : function(creep) { - var storage = creep.room.find(FIND_STRUCTURES, { - filter: (structure) => { - return (structure.structureType == STRUCTURE_CONTAINER) && - structure.store.getUsedCapacity(RESOURCE_ENERGY) > 0; - } - }); - /** - if(storage.length == 0){ - storage = creep.room.find(FIND_STRUCTURES, { - filter: (structure) => { - return (structure.structureType == STRUCTURE_SPAWN) && - structure.store.getUsedCapacity(RESOURCE_ENERGY) > 0; - } - }); - } - **/ + let storage; + if(creep.room.memory.hasContainer){ + storage = creep.room.find(FIND_STRUCTURES, { + filter: (structure) => { + return (structure.structureType == STRUCTURE_CONTAINER) && + structure.store.getUsedCapacity(RESOURCE_ENERGY) > 0; + } + }); + } + else{ + if(typeof creep.memory.assignedSource === 'undefined') energyUtils.sourceDelegate(creep); + let source = Game.getObjectById(creep.memory.assignedSource); + if(creep.harvest(source) == ERR_NOT_IN_RANGE) { + creep.moveTo(source, {visualizePathStyle: {stroke: '#ffaa00'}}); + } + return; + } + + if(storage.length == 0) return; + if(creep.withdraw(storage[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { creep.moveTo(storage[0], {visualizePathStyle: {stroke: '#ffaa00'}}); } }, + sourceDelegate : function(creep){ + const room = creep.room; + + const sources = creep.room.find(FIND_SOURCES); + + let minSource = sources[0].id; + let minSourceCount = 0; + + if(typeof room.memory[sources[0].id] !== 'undefined') + minSourceCount = room.memory[sources[0].id].length; + + sources.forEach(function(source){ + if(typeof room.memory[source.id] === 'undefined') + room.memory[source.id] = []; + + room.memory[source.id].forEach(function(assignedCreep, index){ + if(!Game.creeps[assignedCreep]){ + room.memory[source.id].splice(index, 1); + } + }); + + if(room.memory[source.id].length < minSourceCount){ + minSource = source.id; + minSourceCount = room.memory[source.id].length; + } + }); + + room.memory[minSource].push(creep.name); + creep.memory.assignedSource = minSource; + + } + , depositEnergy : function(creep) { var targets = creep.room.find(FIND_STRUCTURES, { filter: (structure) => { diff --git a/util_progress.js b/util_progress.js index 85de3d4..3110960 100644 --- a/util_progress.js +++ b/util_progress.js @@ -1,18 +1,19 @@ var progressUtils = { checkProgress : function() { - for(var room in Game.rooms){ - const structures = Game.rooms[room].find(FIND_MY_STRUCTURES); + for(var roomName in Game.rooms){ + const room = Game.rooms[roomName]; + const structures = room.find(FIND_MY_STRUCTURES); room.memory.hasContainer = false; room.memory.hasSpawn = false; room.memory.extensions = 0; - for(var structure in structures){ + structures.forEach(function (structure){ if(structure.structureType == STRUCTURE_CONTAINER) room.memory.hasContainer = true; if(structure.structureType == STRUCTURE_SPAWN) room.memory.hasSpawn = true; if(structure.structureType == STRUCTURE_EXTENSION) room.memory.extensions++; - } + }); } } }