From 18b293c86bbd4cba20ab4239849f1300d9e40136 Mon Sep 17 00:00:00 2001 From: ObeseTermite Date: Sat, 7 Jun 2025 13:48:30 -0700 Subject: [PATCH] Added progress and shit idk --- role_base.js | 10 +++--- role_builder.js | 2 +- role_dispatcher.js | 76 ++++++++++++++++++++++++++++++++++++++++------ role_equalizer.js | 27 ++++++++++++++++ role_guard.js | 6 ++-- role_harvester.js | 2 +- role_upgrader.js | 6 ++-- util_energy.js | 32 +++++++++++-------- util_progress.js | 10 +++--- 9 files changed, 129 insertions(+), 42 deletions(-) create mode 100644 role_equalizer.js diff --git a/role_base.js b/role_base.js index 7245c3c..d9e1994 100644 --- a/role_base.js +++ b/role_base.js @@ -1,14 +1,14 @@ -var body = [ MOVE, WORK, CARRY ]; var name = 'Creep'; function run(creep) { } -function spawn(spawnPoint, newBody, newName){ - if(typeof newBody === 'undefined') newBody = body; +function spawn(spawnPoint, newName){ + var newBody = spawnPoint.room.memory.creepBodies[newName]; if(typeof newName === 'undefined') newName = name; - spawnPoint.spawnCreep(newBody, newName + Game.time, + + spawnPoint.spawnCreep(newBody, newName + spawnPoint.room.name + "-" + Game.time, {memory : {role : newName} }); } -module.exports = {body,name,run,spawn}; +module.exports = {name,run,spawn}; diff --git a/role_builder.js b/role_builder.js index 30f48c0..53a3be7 100644 --- a/role_builder.js +++ b/role_builder.js @@ -14,7 +14,7 @@ function run(creep) { var targets = creep.room.find(FIND_CONSTRUCTION_SITES); if(targets.length) { if(creep.build(targets[0]) == ERR_NOT_IN_RANGE) { - creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}}); + creep.moveTo(targets[0]); } } } diff --git a/role_dispatcher.js b/role_dispatcher.js index b1209bb..8a4d7d9 100644 --- a/role_dispatcher.js +++ b/role_dispatcher.js @@ -13,13 +13,67 @@ const roleMap = { streltsy: roleGuard }; -var creepCounts = { - "harvester" : 3, - "builder" : 2, - "upgrader" : 2, - "repairer" : 1, - "streltsy" : 2 -}; +function calculateCreepCounts(room){ + var creepCounts = { + "harvester" : 3, + "builder" : 1, + "upgrader" : 1, + "repairer" : 1, + "streltsy" : 0 + }; + + // 2 harvesters per source by default + creepCounts.harvester = room.find(FIND_SOURCES).length * 2; + + if(room.memory.containers == 0){ + + // -1 harvester until at least a container built in order + // to reduce congestion + creepCounts.harvester -= 1; + } + + // streltsy cost 400 energy, and thus cant be built + // you have at least 2 extensions (300+50+50) + // i guess there wouldnt be a big difference since + // they wouldnt spawn anyways, but whatever + if(room.memory.extensions >= 2){ + creepCounts.streltsy += 2; + } + + if(room.memory.extensions >= 3){ + creepCounts.builder += 1; + } + + + room.memory.creepCounts = creepCounts; +} + +function calculateCreepBodies(room){ + var creepBodies = { + // 200 base cost + "harvester" : [WORK, CARRY, MOVE], + "builder" : [WORK, CARRY, MOVE], + "upgrader" : [WORK, CARRY, MOVE], + "repairer" : [WORK, CARRY, MOVE], + + // 400 base cost + "streltsy" : [MOVE, MOVE, RANGED_ATTACK, RANGED_ATTACK] + }; + + // 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; +} function runRole(creep){ const role = roleMap[creep.memory.role]; @@ -32,17 +86,19 @@ function runRole(creep){ } function spawnCreeps(spawn){ - for(const [role, count] of Object.entries(creepCounts)){ + calculateCreepCounts(spawn.room); + for(const [role, count] of Object.entries(spawn.room.memory.creepCounts)){ var roleCreeps = _.filter(Game.creeps, (creep) => creep.memory.role == role); if(roleCreeps.length < count && !spawn.spawning) { + calculateCreepBodies(spawn.room); if(typeof roleMap[role].spawn === 'function') roleMap[role].spawn(spawn); else - roleBase.spawn(spawn, roleMap[role].body, roleMap[role].name); + roleBase.spawn(spawn, roleMap[role].name); break; } } } -module.exports = { runRole, spawnCreeps, creepCounts}; +module.exports = { runRole, spawnCreeps}; diff --git a/role_equalizer.js b/role_equalizer.js new file mode 100644 index 0000000..70a2410 --- /dev/null +++ b/role_equalizer.js @@ -0,0 +1,27 @@ +var energyUtils = require('util_energy'); +var _ = require('lodash'); + +var name = 'desatilnik'; + +function run(creep) { + if(creep.room.memory.containers == 0) return; + + if(creep.store.getFreeCapacity() > 0 ){ + var containers = creep.room.find(FIND_STRUCTURES, { + filter : { structureType: STRUCTURE_CONTAINER} + }); + + var maxContainer = containers[0]; + + containers.forEach((container, index) => { + if(container.store[RESOURCE_ENERGY] > maxContainer.store[RESOURCE_ENERGY]){ + maxContainer = container; + } + }); + } + else{ + } +} + +module.exports = {name,run}; + diff --git a/role_guard.js b/role_guard.js index 044ec17..a06db40 100644 --- a/role_guard.js +++ b/role_guard.js @@ -1,4 +1,3 @@ -var body = [ MOVE, MOVE, RANGED_ATTACK, RANGED_ATTACK ]; var name = 'streltsy' function run(creep) { @@ -23,12 +22,13 @@ function run(creep) { } function spawn(spawnPoint, newWaypoints){ + var newBody = spawnPoint.room.memory.creepBodies[name]; if(typeof newWaypoints === 'undefined') newWaypoints = ['A','B']; - spawnPoint.spawnCreep(this.body, this.name + Game.time, + spawnPoint.spawnCreep(newBody, this.name + spawnPoint.room.name + "-" + Game.time, {memory : {role : this.name, waypoints : newWaypoints, currentWaypoint : 0} }); } -module.exports = {body,name,run,spawn}; +module.exports = {name,run,spawn}; diff --git a/role_harvester.js b/role_harvester.js index e3ba40b..8ae520b 100644 --- a/role_harvester.js +++ b/role_harvester.js @@ -8,7 +8,7 @@ function run(creep) { var source = Game.getObjectById(creep.memory.assignedSource); if(creep.harvest(source) == ERR_NOT_IN_RANGE) { - creep.moveTo(source, {visualizePathStyle: {stroke: '#ffaa00'}}); + creep.moveTo(source); } } else { diff --git a/role_upgrader.js b/role_upgrader.js index 84e4038..e08b479 100644 --- a/role_upgrader.js +++ b/role_upgrader.js @@ -1,3 +1,4 @@ +var energyUtils = require('util_energy'); var name = 'upgrader'; /** @param {Creep} creep **/ @@ -16,10 +17,7 @@ function run(creep) { } } else { - var sources = creep.room.find(FIND_SOURCES); - if(creep.harvest(sources[1]) == ERR_NOT_IN_RANGE) { - creep.moveTo(sources[1], {visualizePathStyle: {stroke: '#ffaa00'}}); - } + energyUtils.gatherEnergy(creep); } } diff --git a/util_energy.js b/util_energy.js index 2801f4d..a05a07d 100644 --- a/util_energy.js +++ b/util_energy.js @@ -1,7 +1,10 @@ var energyUtils = { + + // get energy from a container (if available) otherwise just mine for it at + // an energy source gatherEnergy : function(creep) { let storage; - if(creep.room.memory.hasContainer){ + if(creep.room.memory.containers > 0){ storage = creep.room.find(FIND_STRUCTURES, { filter: (structure) => { return (structure.structureType == STRUCTURE_CONTAINER) && @@ -13,7 +16,7 @@ var energyUtils = { 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'}}); + creep.moveTo(source); } return; } @@ -21,9 +24,11 @@ var energyUtils = { if(storage.length == 0) return; if(creep.withdraw(storage[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - creep.moveTo(storage[0], {visualizePathStyle: {stroke: '#ffaa00'}}); + creep.moveTo(storage[0]); } }, + + //assign a creep to source in the room the creep is in sourceDelegate : function(creep){ const room = creep.room; @@ -54,28 +59,29 @@ var energyUtils = { room.memory[minSource].push(creep.name); creep.memory.assignedSource = minSource; - } - , + }, + + // put energy into storage, first if possible into a spawner / extension + // otherwise put it into a container depositEnergy : function(creep) { - var targets = creep.room.find(FIND_STRUCTURES, { + var target = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) => { return (structure.structureType == STRUCTURE_SPAWN || structure.structureType == STRUCTURE_EXTENSION) && structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0; } }); - if(targets.length == 0){ - targets = creep.room.find(FIND_STRUCTURES, { + if(target == null){ + target = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) => { - return (structure.structureType == STRUCTURE_CONTAINER || - structure.structureType == STRUCTURE_TOWER) && + return (structure.structureType == STRUCTURE_CONTAINER) && structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0; } }); } - if(targets.length > 0) { - if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - creep.moveTo(targets[0], {visualizePathStyle: {stroke: '#ffffff'}}); + if(target != null) { + if(creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { + creep.moveTo(target, {visualizePathStyle: {stroke: '#ffffff'}}); } return true; } diff --git a/util_progress.js b/util_progress.js index 3110960..62ff7d9 100644 --- a/util_progress.js +++ b/util_progress.js @@ -3,14 +3,14 @@ var progressUtils = { 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; + const structures = room.find(FIND_STRUCTURES); + room.memory.spawns = 0; + room.memory.containers = 0; room.memory.extensions = 0; 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_CONTAINER) room.memory.containers++; + if(structure.structureType == STRUCTURE_SPAWN) room.memory.spawns++; if(structure.structureType == STRUCTURE_EXTENSION) room.memory.extensions++; });