Added progress checking, and automatic source delegation for creeps, when there is more than 1 source

This commit is contained in:
ObeseTermite 2025-06-06 18:55:25 -07:00
parent 97bf5fe399
commit d5956ede22
4 changed files with 63 additions and 24 deletions

View file

@ -15,7 +15,7 @@ const roleMap = {
var creepCounts = { var creepCounts = {
"harvester" : 3, "harvester" : 3,
"builder" : 1, "builder" : 2,
"upgrader" : 2, "upgrader" : 2,
"repairer" : 1, "repairer" : 1,
"streltsy" : 2 "streltsy" : 2

View file

@ -4,9 +4,11 @@ var name = 'harvester';
function run(creep) { function run(creep) {
if(creep.store.getFreeCapacity() > 0) { if(creep.store.getFreeCapacity() > 0) {
var sources = creep.room.find(FIND_SOURCES); if(typeof creep.memory.assignedSource === 'undefined') energyUtils.sourceDelegate(creep);
if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { var source = Game.getObjectById(creep.memory.assignedSource);
creep.moveTo(sources[0], {visualizePathStyle: {stroke: '#ffaa00'}});
if(creep.harvest(source) == ERR_NOT_IN_RANGE) {
creep.moveTo(source, {visualizePathStyle: {stroke: '#ffaa00'}});
} }
} }
else { else {

View file

@ -1,25 +1,61 @@
var energyUtils = { var energyUtils = {
gatherEnergy : function(creep) { gatherEnergy : function(creep) {
var storage = creep.room.find(FIND_STRUCTURES, { let storage;
filter: (structure) => { if(creep.room.memory.hasContainer){
return (structure.structureType == STRUCTURE_CONTAINER) && storage = creep.room.find(FIND_STRUCTURES, {
structure.store.getUsedCapacity(RESOURCE_ENERGY) > 0; 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) => { else{
return (structure.structureType == STRUCTURE_SPAWN) && if(typeof creep.memory.assignedSource === 'undefined') energyUtils.sourceDelegate(creep);
structure.store.getUsedCapacity(RESOURCE_ENERGY) > 0; 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) { if(creep.withdraw(storage[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(storage[0], {visualizePathStyle: {stroke: '#ffaa00'}}); 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) { depositEnergy : function(creep) {
var targets = creep.room.find(FIND_STRUCTURES, { var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => { filter: (structure) => {

View file

@ -1,18 +1,19 @@
var progressUtils = { var progressUtils = {
checkProgress : function() { checkProgress : function() {
for(var room in Game.rooms){ for(var roomName in Game.rooms){
const structures = Game.rooms[room].find(FIND_MY_STRUCTURES); const room = Game.rooms[roomName];
const structures = room.find(FIND_MY_STRUCTURES);
room.memory.hasContainer = false; room.memory.hasContainer = false;
room.memory.hasSpawn = false; room.memory.hasSpawn = false;
room.memory.extensions = 0; 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_CONTAINER) room.memory.hasContainer = true;
if(structure.structureType == STRUCTURE_SPAWN) room.memory.hasSpawn = true; if(structure.structureType == STRUCTURE_SPAWN) room.memory.hasSpawn = true;
if(structure.structureType == STRUCTURE_EXTENSION) room.memory.extensions++; if(structure.structureType == STRUCTURE_EXTENSION) room.memory.extensions++;
} });
} }
} }
} }