From fd695a82bc31f5e991ed9cdf4efae68aae0beb12 Mon Sep 17 00:00:00 2001 From: ObeseTermite Date: Tue, 10 Jun 2025 16:09:16 -0700 Subject: [PATCH] General balances, and made number of harvester dynamic to possible harvesting positions --- role_dispatcher.js | 31 +++++++++++++++++++++++-------- role_equalizer.js | 8 ++++---- util_energy.js | 9 ++++++--- util_etc.js | 16 ++++++++++++++++ util_progress.js | 4 ++-- 5 files changed, 51 insertions(+), 17 deletions(-) create mode 100644 util_etc.js diff --git a/role_dispatcher.js b/role_dispatcher.js index dc7435b..e25fd70 100644 --- a/role_dispatcher.js +++ b/role_dispatcher.js @@ -5,6 +5,7 @@ var roleRepairer = require('role_repairer'); var roleGuard = require('role_guard'); var roleBase = require('role_base'); var roleEqualizer = require('role_equalizer'); +var utils = require('util_etc'); const roleMap = { harvester: roleHarvester, @@ -15,6 +16,20 @@ const roleMap = { desatilnik: roleEqualizer }; +// calculates how much space there is for creeps next to +// energy sources, in order to have the perfect amount +function calculateTotalSourceSpace(room){ + var sources = room.find(FIND_SOURCES); + + var count = 0; + + for(const source of sources){ + count += utils.calculateSourceSpace(source); + } + + return count; +} + function calculateCreepCounts(room){ var creepCounts = { "harvester" : 3, @@ -26,7 +41,7 @@ function calculateCreepCounts(room){ }; // 2 harvesters per source by default - creepCounts.harvester = room.find(FIND_SOURCES).length * 2; + creepCounts.harvester = calculateTotalSourceSpace(room); if(room.memory.containers == 0){ @@ -45,6 +60,7 @@ function calculateCreepCounts(room){ if(room.memory.extensions >= 3){ creepCounts.builder += 1; + creepCounts.upgrader += 1; } if(room.memory.containers >= 2){ @@ -68,18 +84,17 @@ function calculateCreepBodies(room){ "desatilnik" : [CARRY, CARRY, CARRY, MOVE, MOVE, MOVE] }; + // 400 cost + if(room.memory.extensions >= 2){ + creepBodies.upgrader = [MOVE, MOVE, WORK, WORK, CARRY, CARRY]; + creepBodes.builder = [MOVE, MOVE, WORK, WORK, CARRY, CARRY]; + } + // 450 cost if(room.memory.extensions >= 3){ creepBodies.harvester = [MOVE, MOVE, WORK, WORK, WORK, CARRY]; } - // 400 cost - // but only want the upgrading to scale up when have a bunch - // of extra energy - if(room.memory.extensions >= 10){ - creepBodies.upgrader = [MOVE, MOVE, WORK, WORK, CARRY, CARRY]; - } - room.memory.creepBodies = creepBodies; } diff --git a/role_equalizer.js b/role_equalizer.js index 4533c74..35b2188 100644 --- a/role_equalizer.js +++ b/role_equalizer.js @@ -13,11 +13,11 @@ function run(creep) { var maxContainer = containers[0]; - containers.forEach((container, index) => { + for(const container of containers){ if(container.store[RESOURCE_ENERGY] > maxContainer.store[RESOURCE_ENERGY]){ maxContainer = container; } - }); + }; if(creep.withdraw(maxContainer, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { creep.moveTo(maxContainer); @@ -30,11 +30,11 @@ function run(creep) { var minContainer = containers[0]; - containers.forEach((container, index) => { + for(const container of containers){ if(container.store[RESOURCE_ENERGY] < minContainer.store[RESOURCE_ENERGY]){ minContainer = container; } - }); + }; if(creep.transfer(minContainer, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { creep.moveTo(minContainer); diff --git a/util_energy.js b/util_energy.js index 7e24085..89ea2d1 100644 --- a/util_energy.js +++ b/util_energy.js @@ -1,3 +1,4 @@ +var utils = require('util_etc'); var energyUtils = { // get energy from a container (if available) otherwise just mine for it at @@ -40,7 +41,7 @@ var energyUtils = { if(typeof room.memory[sources[0].id] !== 'undefined') minSourceCount = room.memory[sources[0].id].length; - sources.forEach(function(source){ + for(const source of sources){ if(typeof room.memory[source.id] === 'undefined') room.memory[source.id] = []; @@ -50,11 +51,13 @@ var energyUtils = { } }); - if(room.memory[source.id].length < minSourceCount){ + if(room.memory[source.id].length < minSourceCount + && room.memory[source.id].length < utils.calculateSourceSpace(source)){ + minSource = source.id; minSourceCount = room.memory[source.id].length; } - }); + } room.memory[minSource].push(creep.name); creep.memory.assignedSource = minSource; diff --git a/util_etc.js b/util_etc.js new file mode 100644 index 0000000..8543e84 --- /dev/null +++ b/util_etc.js @@ -0,0 +1,16 @@ +function calculateSourceSpace(source){ + var count = 0; + + for(let y = -1; y <= 1; y++){ + for(let x = -1; x <= 1; x++){ + if(x == 0 && y == 0) continue; + for(var object of source.room.lookAt(source.pos.x + x, source.pos.y + y)){ + if(object.type != 'terrain') continue; + if(object.terrain != 'wall') count++; + } + } + } + return count; +} + +module.exports = { calculateSourceSpace }; diff --git a/util_progress.js b/util_progress.js index 62ff7d9..5a8c858 100644 --- a/util_progress.js +++ b/util_progress.js @@ -8,12 +8,12 @@ var progressUtils = { room.memory.containers = 0; room.memory.extensions = 0; - structures.forEach(function (structure){ + for(const structure of structures){ if(structure.structureType == STRUCTURE_CONTAINER) room.memory.containers++; if(structure.structureType == STRUCTURE_SPAWN) room.memory.spawns++; if(structure.structureType == STRUCTURE_EXTENSION) room.memory.extensions++; - }); + }; } } }