diff --git a/NOTES.js b/NOTES.js index 0250635..27e323c 100644 --- a/NOTES.js +++ b/NOTES.js @@ -4,4 +4,57 @@ serializePos: function(pos) { deserializePos: function(str) { var ar = str.split('_'); return new RoomPosition(ar[0], ar[1], ar[2]); - }, \ No newline at end of file + }, + + + + Structure.prototype.getResourceCapacity = function(resourceType) { + switch (this.structureType) { + case STRUCTURE_CONTAINER: + case STRUCTURE_STORAGE: + case STRUCTURE_TERMINAL: + return this.storeCapacity; + case STRUCTURE_SPAWN: + case STRUCTURE_EXTENSION: + case STRUCTURE_LINK: + case STRUCTURE_TOWER: + if (RESOURCE_ENERGY == resourceType) { + return(this.energyCapacity); + } else { + return(-1); + } + case STRUCTURE_LAB: + if (RESOURCE_ENERGY == resourceType) { + return(this.energyCapacity); + } else { + return(this.mineralCapacity); + } + default: + return(-1); + } +} +Structure.prototype.getResource = function(resourceType) { + switch (this.structureType) { + case STRUCTURE_CONTAINER: + case STRUCTURE_STORAGE: + case STRUCTURE_TERMINAL: + return this.store[resourceType]; + case STRUCTURE_SPAWN: + case STRUCTURE_EXTENSION: + case STRUCTURE_LINK: + case STRUCTURE_TOWER: + if (RESOURCE_ENERGY == resourceType) { + return(this.energy); + } else { + return(-1); + } + case STRUCTURE_LAB: + if (RESOURCE_ENERGY == resourceType) { + return(this.energy); + } else { + return(this.mineralAmount); + } + default: + return(-1); + } +} \ No newline at end of file diff --git a/Spawnloop.js b/Spawnloop.js deleted file mode 100644 index 7174c2d..0000000 --- a/Spawnloop.js +++ /dev/null @@ -1,40 +0,0 @@ -Object.defineProperty(Room.prototype, "spawns", { - /** - * Holds array of spawns. If you do not own the room, returns ERR_NOT_OWNER - * - * @returns {number|ERR_NOT_OWNER|object} - */ - configurable: true, - get: function () { - if (!this.controller || !this.controller.my) { - return ERR_NOT_OWNER; - } - let spawnObjects = []; - if (!this.memory.spawns) { - let spawns = []; - spawnObjects = this.find(FIND_MY_STRUCTURES, { - filter: { - structureType: STRUCTURE_SPAWN - } - }); - spawnObjects.forEach( - function (spawn) { - spawns.push(spawn.id) - }); - this.memory.spawns = spawns; - } - if (spawnObjects.length == 0 && this.memory.spawns.length > 0) { - for (let key in this.memory.spawns) { - let spawnId = this.memory.spawns[key]; - let spawn = Game.getObjectById(spawnId); - if (!spawn) { - this.log('No spawn found for ID ' + spawnId + ' despite being in cache, cleaning.', WARNING, true); - delete this.memory.spawns; - } else { - spawnObjects.push(spawn); - } - } - } - return spawnObjects; - } -}); \ No newline at end of file diff --git a/bodypartbuilder.js b/bodypartbuilder.js index e3d7f02..3cff0d9 100644 --- a/bodypartbuilder.js +++ b/bodypartbuilder.js @@ -1,4 +1,36 @@ -bodyParts.forEach(function (bodyPart) { - var part = typeof bodyPart === 'string' ? bodyPart : bodyPart.type; - cost += BODYPART_COST[part]; - }); \ No newline at end of file +let buildparts = { + tick: function(roomname,role) { + console.log(roomname + ":" + role) + let energyready = Game.rooms[roomname].energyAvailable + let energymax = Game.rooms[roomname].energyCapacityAvailable + if(energyready==energymax){ + let tobuild=[] + switch(role){ + case 'builder': + let myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) + for(let spawn of myspawns){ + for(let totry of [ + [MOVE,MOVE,MOVE,MOVE,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,CARRY,CARRY,CARRY,WORK], + [MOVE,MOVE,MOVE,MOVE,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,CARRY,CARRY,CARRY,MOVE], + [MOVE,MOVE,CARRY] + ]){ + let ispossible = spawn.canCreateCreep(totry) + if(ispossible==OK){ + tobuild=totry + break; + } + } + console.log(tobuild) + } + break; + case 'hauler': + console.log('2') + break + default: + console.log('3') + } + } + + } +}; +module.exports = buildparts; \ No newline at end of file diff --git a/energyinfo.js b/energyinfo.js deleted file mode 100644 index d41af68..0000000 --- a/energyinfo.js +++ /dev/null @@ -1,69 +0,0 @@ -import {Util} from "../Util"; - -/** - * @Serializable - */ -export class EnergyInformation { - - energy: number; - energyCapacity: number; - availableForDistribution: boolean; - - static isFull(energyInformation: EnergyInformation): boolean { - return energyInformation.energy === energyInformation.energyCapacity; - } - - static isEmpty(energyInformation: EnergyInformation): boolean { - return energyInformation.energy === 0; - } - - /** - * @param structure - * @returns {EnergyInformation} if structure can hold energy - */ - static ofStructure(structure: Structure): EnergyInformation|undefined { - var storedEnergy = 0; - var energyCapacity = 0; - var energyAvailableForDistribution: boolean; - - switch (structure.structureType) { - case STRUCTURE_EXTENSION: - energyAvailableForDistribution = false; - storedEnergy = ( structure).energy; - energyCapacity = ( structure).energyCapacity; - break; - case STRUCTURE_CONTAINER: - case STRUCTURE_STORAGE: - // both, for StructureStorage and StructureContainer use *StructureStorage* - energyAvailableForDistribution = true; - var store = ( structure).store; - storedEnergy = store[RESOURCE_ENERGY] || 0; - var allStorageUnits: number = _.sum(store); - energyCapacity = ( structure).storeCapacity - (allStorageUnits - storedEnergy); - break; - case STRUCTURE_LINK: - energyAvailableForDistribution = true; - storedEnergy = ( structure).energy; - energyCapacity = ( structure).energyCapacity; - break; - case STRUCTURE_SPAWN: - energyAvailableForDistribution = false; - storedEnergy = ( structure).energy; - energyCapacity = ( structure).energyCapacity; - break; - case STRUCTURE_TOWER: - energyAvailableForDistribution = false; - storedEnergy = ( structure).energy; - energyCapacity = ( structure).energyCapacity; - break; - default: - return undefined; - } - - var energyInformation = new EnergyInformation(); - energyInformation.energy = storedEnergy; - energyInformation.energyCapacity = energyCapacity; - energyInformation.availableForDistribution = energyAvailableForDistribution; - return energyInformation; - } -} \ No newline at end of file diff --git a/main.js b/main.js index 10c40a4..2cbe69f 100644 --- a/main.js +++ b/main.js @@ -1,47 +1,73 @@ -var roleBuilder=require('role.builder'); -var roleMiner=require('role.miner'); -var roleMiner2=require('role.miner2'); -var roleHauler=require('role.hauler') -var roleHauler2=require('role.hauler2') -var roleUpgrader=require('role.upgrader') -var roleTowerrecharger=require('role.recharger') -var runSources=require('sourceprocessing') -var roleRepairbot=require('role.repairbot') -var runTower = require('tower'); +'use strict'; +let roleBuilder=require('role.builder'); +let roleMiner=require('role.miner'); +let roleHauler=require('role.hauler') +let roleHauler2=require('role.hauler2') +let roleUpgrader=require('role.upgrader') +let roleTowerrecharger=require('role.recharger') +let runSources=require('sourceprocessing') +let roleRepairbot=require('role.repairbot') +let runTower = require('tower'); +let assignMiners=require('sproc') +let buildparts=require('bodypartbuilder') module.exports.loop = function () { - - for(var name in Memory.creeps) { + console.log('---START---') + for(let name in Memory.creeps) { if(!Game.creeps[name]) { delete Memory.creeps[name]; console.log('Clearing non-existing creep memory:', name); } } - for(var name in Game.rooms) { + + for(let myroom of _.filter(Game.rooms, 'controller.my')) { + let name = myroom.name + console.log(name) + + buildparts.tick(name,'builder') + console.log('---'+name+'---') runTower.tick(name); - var roomenergy = Game.rooms[name].energyAvailable - var allstorage = Game.rooms[name].find(FIND_STRUCTURES, {filter: (s) => {return ( s.structureType == STRUCTURE_CONTAINER || s.structureType == STRUCTURE_STORAGE)}}) - var allcontainers = Game.rooms[name].find(FIND_STRUCTURES, {filter: (s) => {return ( s.structureType == STRUCTURE_CONTAINER)}}) - var usedstorage = 0 - var mycapacity=0 - for(var i=0; i < allstorage.length;i++){ + assignMiners.tick(name) + runSources.tick(name) + + try{ + let roomenergy = Game.rooms[name].energyAvailable + let allstorage = Game.rooms[name].find(FIND_STRUCTURES, {filter: (s) => {return ( s.structureType == STRUCTURE_CONTAINER || s.structureType == STRUCTURE_STORAGE)}}) + + + let usedstorage = 0 + let mycapacity=0 + for(let i=0; i < allstorage.length;i++){ usedstorage+=_.sum(allstorage[i].store) mycapacity+=allstorage[i].storeCapacity } - Game.rooms[name].memory.storagepercent = usedstorage/mycapacity - var containerusedstorage = 0 - var containercapacity=0 - for(var i=0; i < allcontainers.length;i++){ + + let allcontainers = Game.rooms[name].find(FIND_STRUCTURES, {filter: (s) => {return ( s.structureType == STRUCTURE_CONTAINER)}}) + let containerusedstorage = 0 + let containercapacity=0 + for(let i=0; i < allcontainers.length;i++){ containerusedstorage+=_.sum(allcontainers[i].store) containercapacity+=allcontainers[i].storeCapacity } + /** + var allstoragecontainers = Game.rooms[name].find(FIND_STRUCTURES, {filter: (s) => {return ( s.structureType == STRUCTURE_STORAGE)}}) + var storagecontainerusedstorage = 0 + var storagecontainercapacity=0 + for(var i=0; i < allcontainers.length;i++){ + storagecontainerusedstorage+=_.sum(allstoragecontainers[i].store) + storagecontainercapacity+=allstoragecontainers[i].storeCapacity + } + if() + console.log(storagecontainerusedstorage + ':' + storagecontainercapacity) + **/ + Game.rooms[name].memory.storagepercent = usedstorage/mycapacity Game.rooms[name].memory.containerstoragepercent = containerusedstorage/containercapacity console.log('-------') console.log('Room "'+name+'" has '+roomenergy+' energy : Using ' + usedstorage + ' of ' + mycapacity + ', ' + Game.rooms[name].memory.storagepercent * 100 + '% Storage, ' + containerusedstorage + ' of ' + containercapacity + ':' + Game.rooms[name].memory.containerstoragepercent *100+ '% in containers') + } catch(e){} if(Game.rooms[name].memory.maxbuilders == undefined){ Game.rooms[name].memory.maxbuilders =0 } if(Game.rooms[name].memory.maxupgraders == undefined){ Game.rooms[name].memory.maxupgraders = 0 } if(Game.rooms[name].memory.maxminers == undefined){ Game.rooms[name].memory.maxminers = 0 } - if(Game.rooms[name].memory.maxminer2s == undefined){ Game.rooms[name].memory.maxminer2s = 0 } if(Game.rooms[name].memory.maxhaulers == undefined){ Game.rooms[name].memory.maxhaulers = 0 } if(Game.rooms[name].memory.maxhauler2s == undefined){ Game.rooms[name].memory.maxhauler2s = 0 } if(Game.rooms[name].memory.maxwarriors == undefined){ Game.rooms[name].memory.maxwarriors = 0 } @@ -56,25 +82,21 @@ module.exports.loop = function () { if(Game.rooms[name].memory.hauler2parky==undefined){Game.rooms[name].memory.hauler2parky=24} if(Game.rooms[name].memory.upgraderparkx==undefined){Game.rooms[name].memory.upgraderparkx=24} if(Game.rooms[name].memory.upgraderparky==undefined){Game.rooms[name].memory.upgraderparky=24} - runSources.tick(name) + roleBuilder.spawn(name) roleMiner.spawn(name) roleHauler.spawn(name) roleHauler2.spawn(name) roleUpgrader.spawn(name) - roleMiner2.spawn(name) roleTowerrecharger.spawn(name) roleRepairbot.spawn(name) - var mycreeps = Game.rooms[name].find(FIND_MY_CREEPS) - for(var mycreep in mycreeps){ + let mycreeps = Game.rooms[name].find(FIND_MY_CREEPS) + for(let mycreep in mycreeps){ try{ - var creep = mycreeps[mycreep] + let creep = mycreeps[mycreep] if(creep.memory.role == 'miner'){ roleMiner.run(creep) } - if(creep.memory.role == 'miner2'){ - roleMiner2.run(creep) - } if(creep.memory.role == 'hauler'){ roleHauler.run(creep) } @@ -96,7 +118,9 @@ module.exports.loop = function () { } catch (e) { //console.log(e.toString) } + } + } - console.log('-------') + console.log('----END---') } \ No newline at end of file diff --git a/role.builder.js b/role.builder.js index 4ff46c8..532662c 100644 --- a/role.builder.js +++ b/role.builder.js @@ -1,5 +1,8 @@ -var roleBuilder = { +let roleBuilder = { run: function(creep) { + if(creep.memory.originroom === undefined){ + creep.memory.originroom = creep.room.name + } if(creep.memory.building && creep.carry.energy == 0) { creep.memory.building = false; creep.say('Gathering'); @@ -10,32 +13,34 @@ var roleBuilder = { } if(creep.memory.building) { - var target = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES); + if(creep.memory.buildsite==undefined|| Game.getObjectById(creep.memory.buildsite.id) == undefined){ + creep.memory.buildsite = Game.rooms[creep.memory.originroom].find(FIND_CONSTRUCTION_SITES)[0]; + } + let target = Game.getObjectById(creep.memory.buildsite.id) //Check next room as well - - if(target != undefined) { - - if(creep.build(target) == ERR_NOT_IN_RANGE) { - creep.moveTo(target); - if(creep.fatigue<1){ - creep.say("MTCS"); - } else { creep.say("Tired")} - } - - } else { - //console.log(creep.room.memory.builderpark) - creep.moveTo(creep.room.memory.builderparkx,creep.room.memory.builderparky,creep.room.roomName) + if(target) { + if(Game.rooms[creep.memory.originroom].memory.containerstoragepercent > -.5 ){ + if(creep.build(target) == ERR_NOT_IN_RANGE) { + creep.moveTo(target); + if(creep.fatigue<1){ + creep.say("MTCS"); + } else { creep.say("Tired")} + } + } + } else { + creep.say('parking') + creep.moveTo(Game.rooms[creep.memory.originroom].memory.builderparkx,Game.rooms[creep.memory.originroom].memory.builderparky,creep.memory.originroom) } } else { - var containers = creep.room.find(FIND_STRUCTURES, { + let containers =Game.rooms[creep.memory.originroom].find(FIND_STRUCTURES, { filter: (structure) => {return ((structure.structureType == STRUCTURE_CONTAINER|| structure.structureType == STRUCTURE_STORAGE) && structure.store[RESOURCE_ENERGY] > 0) ;}}); - var allcontainers = creep.room.find(FIND_STRUCTURES, { + let allcontainers = creep.room.find(FIND_STRUCTURES, { filter: (structure) => { return (structure.structureType == STRUCTURE_CONTAINER ) ; }}); - var sources = creep.room.find(FIND_SOURCES); - var droppedenergy = creep.pos.findClosestByRange(FIND_DROPPED_ENERGY ); + let sources = Game.rooms[creep.memory.originroom].find(FIND_SOURCES); + let droppedenergy = creep.pos.findClosestByRange(FIND_DROPPED_ENERGY ); if(containers==0){ if(creep.pickup(droppedenergy) == ERR_NOT_IN_RANGE) { creep.say("MTDE"); @@ -51,15 +56,14 @@ var roleBuilder = { } }, spawn: function(roomname){ - var myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) - var myroom = Game.rooms[roomname] - for(var thisspawn in myspawns){ - var spawn = myspawns[thisspawn] - var myrole='builder'; - var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole); + let myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) + let myroom = Game.rooms[roomname] + for(let spawn of myspawns){ + let myrole='builder'; + let myroles = _.filter(Game.creeps, (creep) => { return ((creep.memory.role == myrole) && (creep.memory.originroom == roomname))}); console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']); if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) { - var newName = spawn.createCreep([WORK,WORK,CARRY,MOVE,MOVE], undefined, {role: myrole}); + let newName = spawn.createCreep([MOVE,WORK,CARRY], undefined, {role: myrole}); console.log('Spawning new ' + myrole + ': ' + newName); } } diff --git a/role.harvester.js b/role.harvester.js deleted file mode 100644 index 435d0e2..0000000 --- a/role.harvester.js +++ /dev/null @@ -1,50 +0,0 @@ -var roleHarvester = { - - /** @param {Creep} creep **/ - run: function(creep) { - if(creep.carry.energy < creep.carryCapacity) { - var sources = creep.room.find(FIND_SOURCES); - if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { - creep.moveTo(sources[0]); - } - } - else { - var spawntargets = creep.room.find(FIND_STRUCTURES, { - filter: (structure) => { - return ((structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN) &&structure.energy < structure.energyCapacity) - /**|| (structure.structureType == STRUCTURE_CONTAINER && structure.store[RESOURCE_ENERGY] < structure.storeCapacity) **/ ; - } - - }); - var containertargets = creep.room.find(FIND_STRUCTURES, { - filter: (structure) => { - return (structure.structureType == STRUCTURE_CONTAINER && structure.store[RESOURCE_ENERGY] < structure.storeCapacity) ; - } - - }); - if(spawntargets.length > 0) { - if(creep.transfer(spawntargets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - creep.moveTo(spawntargets[0]); - } - } else if (containertargets.length > 0) { - if(creep.transfer(containertargets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - creep.moveTo(containertargets[0]); - } - }else { - creep.moveTo(Game.flags.Flag1); - } - } - }, - spawn: function(){ - var myrole='harvester'; - var nummyrole=0; - var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); - if(myroles.length < nummyrole) { - console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole); - var newName = Game.spawns['Spawn1'].createCreep([WORK,CARRY,MOVE], undefined, {role: myrole}); - console.log('Spawning new ' + myrole + ': ' + newName); - } - } -}; - -module.exports = roleHarvester; \ No newline at end of file diff --git a/role.harvester2.js b/role.harvester2.js deleted file mode 100644 index 812c390..0000000 --- a/role.harvester2.js +++ /dev/null @@ -1,49 +0,0 @@ -var roleHarvester2 = { - - /** @param {Creep} creep **/ - run: function(creep) { - if(creep.carry.energy < creep.carryCapacity) { - var sources = creep.room.find(FIND_SOURCES); - if(creep.harvest(sources[1]) == ERR_NOT_IN_RANGE) { - creep.moveTo(sources[1]); - } - } - else { - var spawntargets = creep.room.find(FIND_STRUCTURES, { - filter: (structure) => { - return ((structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN) &&structure.energy < structure.energyCapacity) - } - - }); - var containertargets = creep.room.find(FIND_STRUCTURES, { - filter: (structure) => { - return (structure.structureType == STRUCTURE_CONTAINER && structure.store[RESOURCE_ENERGY] < structure.storeCapacity) ; - } - - }); - if(spawntargets.length > 0) { - if(creep.transfer(spawntargets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - creep.moveTo(spawntargets[0]); - } - } else if (containertargets.length > 0) { - if(creep.transfer(containertargets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - creep.moveTo(containertargets[0]); - } - }else { - creep.moveTo(Game.flags.Flag1); - } - } - }, - spawn: function(){ - var myrole='harvester2'; - var nummyrole=0; - var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); - if(myroles.length < nummyrole) { - console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole); - var newName = Game.spawns['Spawn1'].createCreep([WORK,CARRY,MOVE], undefined, {role: myrole}); - console.log('Spawning new ' + myrole + ': ' + newName); - } - } -}; - -module.exports = roleHarvester2; \ No newline at end of file diff --git a/role.hauler.js b/role.hauler.js index 902e6ef..6d88347 100644 --- a/role.hauler.js +++ b/role.hauler.js @@ -1,5 +1,8 @@ -var roleHauler = { +let roleHauler = { run: function(creep) { + if(creep.memory.originroom === undefined){ + creep.memory.originroom = creep.room.name + } if(creep.memory.hauling == undefined){creep.memory.hauling=false} if(creep.memory.hauling && creep.carry.energy == 0) { creep.memory.destsource=undefined @@ -10,28 +13,28 @@ var roleHauler = { creep.memory.hauling = true; creep.say('hauling'); } - var sources = creep.pos.findClosestByRange(FIND_DROPPED_ENERGY ); + let sources = creep.pos.findClosestByRange(FIND_DROPPED_ENERGY ); if(creep.memory.hauling==false){ if(Game.getObjectById(creep.memory.destsource.id)==undefined){creep.memory.destsource=undefined} - var mysource=Game.getObjectById(creep.memory.destsource.id) + let mysource=Game.getObjectById(creep.memory.destsource.id) if(creep.pickup(mysource) == ERR_NOT_IN_RANGE && creep.carryCapacity > creep.carry.energy) { creep.moveTo(mysource); } - }else { + } else { if(sources != undefined ) - { var spawntargets = creep.pos.findClosestByPath(FIND_STRUCTURES, { + { let spawntargets = creep.pos.findClosestByPath(FIND_STRUCTURES, { filter: (structure) => { return ((structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN) && structure.energy < structure.energyCapacity) } }); - var containertargets = creep.pos.findClosestByRange(FIND_STRUCTURES, { + let containertargets = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) => { return ((structure.structureType == STRUCTURE_CONTAINER ) && _.sum(structure.store) < structure.storeCapacity) ; } }); - var storagetargets = creep.pos.findClosestByRange(FIND_STRUCTURES, { + let storagetargets = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) => { return ((structure.structureType == STRUCTURE_STORAGE ) && _.sum(structure.store) < structure.storeCapacity) ; } @@ -54,15 +57,14 @@ var roleHauler = { } }, spawn: function(roomname){ - var myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) - var myroom = Game.rooms[roomname] - for(var thisspawn in myspawns){ - var spawn = myspawns[thisspawn] - var myrole='hauler'; - var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole); + let myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) + let myroom = Game.rooms[roomname] + for(let spawn of myspawns){ + let myrole='hauler'; + let myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole && creep.memory.originroom == roomname); console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']); if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) { - var newName = spawn.createCreep([CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE], undefined, {role: myrole}); + let newName = spawn.createCreep([CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE], undefined, {role: myrole}); console.log('Spawning new ' + myrole + ': ' + newName); } } diff --git a/role.hauler2.js b/role.hauler2.js index 032c614..1a3a7d5 100644 --- a/role.hauler2.js +++ b/role.hauler2.js @@ -1,8 +1,10 @@ - var roleHauler2 = { + let roleHauler2 = { /** @param {Creep} creep **/ run: function(creep) { - + if(creep.memory.originroom === undefined){ + creep.memory.originroom = creep.room.name + } if(creep.memory.hauling == undefined){creep.memory.hauling=false} if(creep.memory.hauling && creep.carry.energy == 0) { creep.memory.hauling = false; @@ -14,7 +16,7 @@ } if(creep.memory.hauling==false){ if(creep.carryCapacity > creep.carry.energy){ - var container = creep.pos.findClosestByRange(FIND_STRUCTURES, { + let container = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) => { return ((structure.structureType == STRUCTURE_CONTAINER|| structure.structureType == STRUCTURE_STORAGE) && structure.store[RESOURCE_ENERGY] > 0) ; }}); @@ -24,7 +26,7 @@ } } } else { - var spawntarget = creep.pos.findClosestByRange(FIND_STRUCTURES, { + let spawntarget = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) => { return ((structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN) &&structure.energy < structure.energyCapacity) } @@ -41,15 +43,14 @@ } }, spawn: function(roomname){ - var myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) - var myroom = Game.rooms[roomname] - for(var thisspawn in myspawns){ - var spawn = myspawns[thisspawn] - var myrole='hauler2'; - var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole); + let myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) + let myroom = Game.rooms[roomname] + for(let spawn of myspawns){ + let myrole='hauler2'; + let myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole && creep.memory.originroom == roomname); console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']); if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) { - var newName = spawn.createCreep([CARRY,CARRY,CARRY,MOVE,MOVE], undefined, {role: myrole}); + let newName = spawn.createCreep([CARRY,CARRY,CARRY,MOVE,MOVE], undefined, {role: myrole}); console.log('Spawning new ' + myrole + ': ' + newName); } } diff --git a/role.miner.js b/role.miner.js index 579ec4e..d3f507d 100644 --- a/role.miner.js +++ b/role.miner.js @@ -1,20 +1,23 @@ -var roleMiner = { +let roleMiner = { run: function(creep) { - var sources = creep.room.find(FIND_SOURCES); - if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { - creep.moveTo(sources[0]); - } + if(creep.memory.originroom === undefined){ + creep.memory.originroom = creep.room.name + } + if(Game.getObjectById(creep.memory.destsource.id)==undefined){creep.memory.destsource=undefined} + let mysource=Game.getObjectById(creep.memory.destsource.id) + if(creep.harvest(mysource) == ERR_NOT_IN_RANGE) { + creep.moveTo(mysource); + } }, spawn: function(roomname){ - var myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) - var myroom = Game.rooms[roomname] - for(var thisspawn in myspawns){ - var spawn = myspawns[thisspawn] - var myrole='miner'; - var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole); + let myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) + let myroom = Game.rooms[roomname] + for(let spawn of myspawns){ + let myrole='miner'; + let myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole && creep.memory.originroom == roomname); console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']); if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) { - var newName = spawn.createCreep([WORK,WORK,WORK,WORK,WORK,MOVE], undefined, {role: myrole}); + let newName = spawn.createCreep([WORK,WORK,WORK,WORK,WORK,MOVE], undefined, {role: myrole}); console.log('Spawning new ' + myrole + ': ' + newName); } } diff --git a/role.miner2.js b/role.miner2.js deleted file mode 100644 index 654638e..0000000 --- a/role.miner2.js +++ /dev/null @@ -1,23 +0,0 @@ -var roleMiner2 = { - run: function(creep) { - var sources = creep.room.find(FIND_SOURCES); - if(creep.harvest(sources[1]) == ERR_NOT_IN_RANGE) { - creep.moveTo(sources[1]); - } - }, - spawn: function(roomname){ - var myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) - var myroom = Game.rooms[roomname] - for(var thisspawn in myspawns){ - var spawn = myspawns[thisspawn] - var myrole='miner2'; - var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole); - console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']); - if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) { - var newName = spawn.createCreep([WORK,WORK,WORK,WORK,WORK,MOVE], undefined, {role: myrole}); - console.log('Spawning new ' + myrole + ': ' + newName); - } - } - } -}; -module.exports = roleMiner2; \ No newline at end of file diff --git a/role.recharger.js b/role.recharger.js index 874ca32..e29f595 100644 --- a/role.recharger.js +++ b/role.recharger.js @@ -1,7 +1,10 @@ - var roleTowerrecharger = { + let roleTowerrecharger = { /** @param {Creep} creep **/ run: function(creep) { + if(creep.memory.originroom === undefined){ + creep.memory.originroom = creep.room.name + } if(creep.memory.recharging && creep.carry.energy == 0) { creep.memory.recharging = false; creep.say('refilling'); @@ -12,7 +15,7 @@ } if(!creep.memory.recharging) { - var container = creep.pos.findClosestByRange(FIND_STRUCTURES, { + let container = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) => { return ((structure.structureType == STRUCTURE_CONTAINER|| structure.structureType == STRUCTURE_STORAGE) && structure.store[RESOURCE_ENERGY] > 1000) ; }}); @@ -21,7 +24,7 @@ creep.moveTo(container); } } else { - var spawntarget = creep.pos.findClosestByRange(FIND_STRUCTURES, { + let spawntarget = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) => { return (structure.structureType == STRUCTURE_TOWER && structure.energy < structure.energyCapacity) } @@ -34,15 +37,14 @@ } }, spawn: function(roomname){ - var myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) - var myroom = Game.rooms[roomname] - for(var thisspawn in myspawns){ - var spawn = myspawns[thisspawn] - var myrole='towerrecharger'; - var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole); + let myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) + let myroom = Game.rooms[roomname] + for(let spawn of myspawns){ + let myrole='towerrecharger'; + let myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole && creep.memory.originroom == roomname); console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']); if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) { - var newName = spawn.createCreep([CARRY,CARRY,CARRY,MOVE,MOVE], undefined, {role: myrole}); + let newName = spawn.createCreep([CARRY,CARRY,CARRY,MOVE,MOVE], undefined, {role: myrole}); console.log('Spawning new ' + myrole + ': ' + newName); } } diff --git a/role.repairbot.js b/role.repairbot.js index 2ae3ee2..efe3e9e 100644 --- a/role.repairbot.js +++ b/role.repairbot.js @@ -1,7 +1,10 @@ -var roleRepairbot = { +let roleRepairbot = { /** @param {Creep} creep **/ run: function(creep) { + if(creep.memory.originroom === undefined){ + creep.memory.originroom = creep.room.name + } if(creep.memory.repairing == undefined){creep.memory.repairing=true} if(creep.memory.repairing && creep.carry.energy == 0) { creep.memory.repairing = false; @@ -12,7 +15,7 @@ var roleRepairbot = { creep.say('repairing'); } if(creep.memory.repairing==false){ - var container = creep.pos.findClosestByRange(FIND_STRUCTURES, { + let container = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) => { return ((structure.structureType == STRUCTURE_CONTAINER|| structure.structureType == STRUCTURE_STORAGE) && structure.store[RESOURCE_ENERGY] > 0) ; }}); @@ -21,7 +24,7 @@ var roleRepairbot = { creep.moveTo(container); } } else { - var importantstructures = creep.room.find(FIND_STRUCTURES, { + let importantstructures = creep.room.find(FIND_STRUCTURES, { filter: (structure) => { return (structure.structureType == STRUCTURE_CONTAINER && structure.hits < structure.hitsMax) ; }}); @@ -31,7 +34,7 @@ var roleRepairbot = { creep.moveTo(importantstructures[0]) } } else { - var damagedstructures = creep.room.find(FIND_STRUCTURES,{filter: (s) => s.hits < s.hitsMax}); + let damagedstructures = creep.room.find(FIND_STRUCTURES,{filter: (s) => s.hits < s.hitsMax}); damagedstructures = _.sortBy(damagedstructures, (s)=>s.hits / s.hitsMax) if(damagedstructures.length>0){ if(creep.repair(damagedstructures[0]) == ERR_NOT_IN_RANGE){ @@ -42,15 +45,14 @@ var roleRepairbot = { } }, spawn: function(roomname){ - var myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) - var myroom = Game.rooms[roomname] - for(var thisspawn in myspawns){ - var spawn = myspawns[thisspawn] - var myrole='repairbot'; - var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole); + let myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) + let myroom = Game.rooms[roomname] + for(let spawn of myspawns){ + let myrole='repairbot'; + let myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole && creep.memory.originroom == roomname); console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']); if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) { - var newName = spawn.createCreep([WORK,CARRY,MOVE], undefined, {role: myrole}); + let newName = spawn.createCreep([WORK,CARRY,MOVE], undefined, {role: myrole}); console.log('Spawning new ' + myrole + ': ' + newName); } } diff --git a/role.towerrecharger.js b/role.towerrecharger.js deleted file mode 100644 index efa9335..0000000 --- a/role.towerrecharger.js +++ /dev/null @@ -1,42 +0,0 @@ - var roleTowerrecharger = { - - /** @param {Creep} creep **/ - run: function(creep) { - if(creep.carryCapacity > creep.carry.energy){ - var container = creep.pos.findClosestByRange(FIND_STRUCTURES, { - filter: (structure) => { - return ((structure.structureType == STRUCTURE_CONTAINER|| structure.structureType == STRUCTURE_STORAGE) && structure.store[RESOURCE_ENERGY] > 1000) ; - }}); - if(creep.withdraw(container,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - creep.say("MTSC"); - creep.moveTo(container); - } - } else { - var spawntarget = creep.pos.findClosestByRange(FIND_STRUCTURES, { - filter: (structure) => { - return (structure.structureType == STRUCTURE_TOWER && structure.energy < structure.energyCapacity) - } - }); - if(spawntarget != undefined) { - if(creep.transfer(spawntarget, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { - creep.say('refilling') - creep.moveTo(spawntarget); - } - } else { - creep.say('MTF') - creep.moveTo(Game.flags.Flag1); - } - } - }, - spawn: function(){ - var myrole='towerrecharger'; - var nummyrole=2; - var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); - if(myroles.length < nummyrole) { - console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole); - var newName = Game.spawns['Spawn1'].createCreep([WORK,CARRY,CARRY,CARRY,MOVE,MOVE], undefined, {role: myrole}); - console.log('Spawning new ' + myrole + ': ' + newName); - } - } - }; - module.exports = roleTowerrecharger; \ No newline at end of file diff --git a/role.upgrader.js b/role.upgrader.js index 811008a..d5ceacf 100644 --- a/role.upgrader.js +++ b/role.upgrader.js @@ -1,6 +1,8 @@ -var roleUpgrader = { +let roleUpgrader = { run: function(creep) { - + if(creep.memory.originroom === undefined){ + creep.memory.originroom = creep.room.name + } if(creep.memory.upgrading==undefined){creep.memory.upgrading=true} if(creep.memory.upgrading && creep.carry.energy == 0) { creep.memory.upgrading = false; @@ -19,7 +21,7 @@ var roleUpgrader = { } else { creep.moveTo(creep.room.memory.upgraderparkx,creep.room.memory.upgraderparky,creep.room.roomName) } } else { - var container = creep.pos.findClosestByRange(FIND_STRUCTURES, { + let container = creep.pos.findClosestByRange(FIND_STRUCTURES, { filter: (structure) => { return ((structure.structureType == STRUCTURE_CONTAINER|| structure.structureType == STRUCTURE_STORAGE) && structure.store[RESOURCE_ENERGY] > 0) ; }}); @@ -30,7 +32,7 @@ var roleUpgrader = { creep.moveTo(container); } }else{ - var droppedenergy = creep.room.find(FIND_DROPPED_ENERGY ); + let droppedenergy = creep.room.find(FIND_DROPPED_ENERGY ); if(creep.pickup(droppedenergy[0]) == ERR_NOT_IN_RANGE) { creep.say("MTDE"); creep.moveTo(droppedenergy[0]); @@ -39,15 +41,14 @@ var roleUpgrader = { } }, spawn: function(roomname){ - var myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) - var myroom = Game.rooms[roomname] - for(var thisspawn in myspawns){ - var spawn = myspawns[thisspawn] - var myrole='upgrader'; - var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole); + let myspawns=Game.rooms[roomname].find(FIND_MY_SPAWNS) + let myroom = Game.rooms[roomname] + for(let spawn of myspawns){ + let myrole='upgrader'; + let myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole && creep.memory.originroom == roomname); console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']); if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) { - var newName = spawn.createCreep([MOVE,MOVE,WORK,WORK,WORK,CARRY,CARRY,CARRY], undefined, {role: myrole}); + let newName = spawn.createCreep([MOVE,MOVE,WORK,WORK,WORK,CARRY,CARRY,CARRY], undefined, {role: myrole}); console.log('Spawning new ' + myrole + ': ' + newName); } } diff --git a/role.warrior.js b/role.warrior.js deleted file mode 100644 index be1b54e..0000000 --- a/role.warrior.js +++ /dev/null @@ -1,29 +0,0 @@ -var roleWarrior = { - - /** @param {Creep} creep **/ - run: function(creep) { - var hostile = creep.pos.findClosestByRange(Game.HOSTILE_CREEPS); - - if(hostile!=undefined) { - var username = hostile.owner.username; - Game.notify(`User ${username} spotted in room ${roomName}`); - creep.moveTo(hostile); - creep.attack(hostile); - - } else{ - creep.moveTo(Game.flags.Flag3); - } - }, - spawn: function(){ - var myrole='warrior'; - var nummyrole=2; - var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); - if(myroles.length < nummyrole) { - console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole); - var newName = Game.spawns['Spawn1'].createCreep([ATTACK,RANGED_ATTACK,MOVE], undefined, {role: myrole}); - console.log('Spawning new ' + myrole + ': ' + newName); - } - } -} - -module.exports = roleWarrior; \ No newline at end of file diff --git a/sourceprocessing.js b/sourceprocessing.js index fa28f9c..9b0e5dc 100644 --- a/sourceprocessing.js +++ b/sourceprocessing.js @@ -1,18 +1,21 @@ -var runSources = { +let runSources = { tick: function(roomname) { - var sources = Game.rooms[roomname].find(FIND_DROPPED_ENERGY ); - for(var source of sources){ - var allhaulers = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role=='hauler' ); - var unassignedhaulers = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => (creep.memory.destsource == undefined && creep.memory.role=='hauler')); - var assignedhaulers = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => (creep.memory.destsource != undefined && creep.memory.role=='hauler')); - var myhaulers = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => (creep.memory.destsource != undefined && creep.memory.destsource.id==source.id && creep.memory.role=='hauler')); - var sourcecount = sources.length - var persrc = allhaulers.length / sourcecount - if(myhaulers.length >=persrc){break;} else{ + let sources = Game.rooms[roomname].find(FIND_DROPPED_ENERGY ); + for(let source of sources){ + try{ + let allhaulers = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role=='hauler' ); + let unassignedhaulers = _.filter(allhaulers, (creep) => (creep.memory.destsource == undefined && creep.memory.role=='hauler')); + let assignedhaulers = _.filter(allhaulers, (creep) => (creep.memory.destsource != undefined && creep.memory.role=='hauler')); + let myhaulers = _.filter(allhaulers, (creep) => (creep.memory.destsource != undefined && creep.memory.destsource.id==source.id && creep.memory.role=='hauler')); + let sourcecount = sources.length + let persrc = allhaulers.length / sourcecount + console.log('My Assigned Haulers: ' +myhaulers.length + ' Max per source:' + persrc + ' Total Haulers:' + allhaulers.length + ' Total Sources:' + sourcecount + ' Unassigned Haulers:' + unassignedhaulers.length) if(unassignedhaulers.length > 0){ - unassignedhaulers[0].memory.destsource = source + if(myhaulers.length < persrc){ + unassignedhaulers[0].memory.destsource = source + } else {} } - } + } catch(e){} } } }; diff --git a/sproc.js b/sproc.js new file mode 100644 index 0000000..8d8c522 --- /dev/null +++ b/sproc.js @@ -0,0 +1,22 @@ +let assignMiners = { + tick: function(roomname) { + let sources = Game.rooms[roomname].find(FIND_SOURCES ); + for(var source of sources){ + let allminers = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role=='miner' ); + let unassignedminers = _.filter(allminers, (creep) => (creep.memory.destsource == undefined && creep.memory.role=='miner')); + let assignedminers = _.filter(allminers, (creep) => (creep.memory.destsource != undefined && creep.memory.role=='miner')); + let myminers = _.filter(allminers, (creep) => (creep.memory.destsource != undefined && creep.memory.destsource.id==source.id && creep.memory.role=='miner')); + let sourcecount = sources.length + let persrc = allminers.length / sourcecount + console.log('My Assigned Miners: ' +myminers.length + ' Max per source:' + persrc + ' Total Miners:' + allminers.length + ' Total Sources:' + sourcecount + ' Unassigned Miners:' + unassignedminers.length) + if(unassignedminers.length > 0){ + if(myminers.length >= persrc){ + //break; + } else { + unassignedminers[0].memory.destsource = source + } + } + } + } +}; +module.exports = assignMiners; \ No newline at end of file diff --git a/totest.js b/totest.js deleted file mode 100644 index 80d6835..0000000 --- a/totest.js +++ /dev/null @@ -1,270 +0,0 @@ -let roleHarvester = require('role.harvester'); -let roleBuilder = require('role.builder'); -let roleRepairer = require('role.repairer'); -let roleDistributor = require('role.distributor'); -let roleUpgrader = require('role.upgrader'); -let roleClaimer = require('role.claimer'); -let roleMiner = require("role.miner"); - -let roleDefender = require('role.defender'); - -let structureTower = require('structure.tower'); -let structureLink = require('structure.link'); - -const FLAG_PRIORITIES = { 'Flag1': 100, 'Flag2': 70, 'Flag3': 80, 'Flag4': 75, 'Flag5': 100}; -const ROLE_PRIORITIES = { 'Claimers': 100, 'Harvesters': 95, 'Distributors': 94, 'Repairers': 85, 'Builders': 80, 'Upgraders': 75, 'Defenders': 90 , 'Miners' : 85 }; - -module.exports.loop = function () { - let spawnStack = new Array(); - - cleanUpCreepsFromMemory(); - - for(let room in Memory.myRooms){ - let roomBuf = Memory.myRooms[room]; - let spawnsInRoom = Game.rooms[room].find(FIND_MY_STRUCTURES, { filter: (s) => s.structureType == STRUCTURE_SPAWN }); - - structureLink.run();//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! - - for(let tower in Game.rooms[room].find(FIND_MY_STRUCTURES, {filter: s => s.structureType == STRUCTURE_TOWER})) - structureTower.run(Game.rooms[room].find(FIND_MY_STRUCTURES, {filter: s => s.structureType == STRUCTURE_TOWER})[tower]); - - for(let flag in roomBuf){ - let flagBuf = roomBuf[flag]; - - for(let role in flagBuf){ - let roleBuf = flagBuf[role]; - - while(roleBuf.creeps.length < roleBuf.amountToSpawn){ - roleBuf.creeps.push("undefined"); - } - - for(let i = roleBuf.creeps.length-1; i > 0; i--){ - if(i >= roleBuf.amountToSpawn && !Game.creeps[roleBuf.creeps[i]]){ - roleBuf.creeps.pop(); - }else { - break; - } - } - - for(let creep in roleBuf.creeps){ - let creepBuf = roleBuf.creeps[creep]; - - if(!Game.creeps[creepBuf]){ - addToSpawnStack(spawnStack, room, flag, role, creep); - }else { - var start = Game.cpu.getUsed(); - orderCreep(room, flag, role, creepBuf, creep); - var end = Game.cpu.getUsed(); - //console.log(end-start + " " + flag + " " + role); - - let hostileCreeps = Game.creeps[creepBuf].room.find(FIND_HOSTILE_CREEPS); - if(hostileCreeps.length > 0){ - Game.flags['DD'].setPosition(hostileCreeps[0].pos); - } - } - } - } - } - - spawnStack.sort(function(s1, s2){ return s1.priority > s2.priority }); - for(let spawn in spawnsInRoom){ - let spawnBuf = spawnsInRoom[spawn]; - - if(spawnBuf.isSpawning == null && spawnStack.length > 0) - spawnFromStack(spawnStack, spawnBuf); - } - } -} - -let cleanUpCreepsFromMemory = function () { - for (var name in Memory.creeps) { - if (!Game.creeps[name]) { - delete Memory.creeps[name]; - console.log('Clearing non-existing creep memory:', name); - } - } -} - -let addToSpawnStack = function(spawnStack, room, flag, role, creep){ - let priority = FLAG_PRIORITIES[flag] * ROLE_PRIORITIES[role]; - spawnStack.push({'priority': priority,'room': room, 'flag': flag, 'role': role, 'creep': creep}); - //console.log("Pushed: " + priority + " " + room + " " + flag + " " + role + " " + creep); -} - -let spawnFromStack = function(spawnStack, spawn){ - if(spawn.isSpawning != null) - return; - - let creepToSpawn = spawnStack.pop(); - - let room = creepToSpawn.room; - let flag = creepToSpawn.flag; - let role = creepToSpawn.role; - let creep = creepToSpawn.creep; - let newCreep = spawnCreep(role, spawn); - - if(Game.creeps[newCreep]){ - Memory.myRooms[room][flag][role].creeps[creep] = newCreep; - console.log("Spawned new Creep: " + newCreep + " " + role + " " + flag + " " + room); - }else { - spawnStack.push(creepToSpawn); - } -} - -let spawnCreep = function(role, spawn){ - let newCreep; - if(role == 'Harvesters'){ - if(spawn.name == "Spawn1") - newCreep = spawn.createCreep([WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE], undefined); - else - newCreep = spawn.createCreep([WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,WORK,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE], undefined); - }else if(role == 'Distributors'){ - if(spawn.name == "Spawn1") - newCreep = spawn.createCreep([CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE], undefined); - else - newCreep = spawn.createCreep([CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], undefined); - }else if(role == 'Upgraders'){ - if(spawn.name == "Spawn1") - newCreep = spawn.createCreep([WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE], undefined); - else - ; - }else if(role == 'Builders'){ - if(spawn.name == "Spawn1") - newCreep = spawn.createCreep([WORK, WORK, WORK, WORK, WORK, WORK, WORK, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE], undefined); - else - newCreep = spawn.createCreep([WORK,WORK,WORK,WORK,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], undefined); - }else if(role == 'Repairers'){ - if(spawn.name == "Spawn1") - newCreep = spawn.createCreep([WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, CARRY, CARRY, CARRY, CARRY, CARRY, CARRY, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE], undefined); - else - newCreep = spawn.createCreep([WORK,WORK,WORK,WORK,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE,MOVE,MOVE], undefined); - }else if(role == 'Miners'){ - if(spawn.name == "Spawn1") - newCreep = spawn.createCreep([WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, CARRY, CARRY, MOVE, MOVE], undefined); - else - newCreep = spawn.createCreep([WORK, WORK, WORK, WORK, WORK, WORK, WORK, WORK, CARRY, CARRY, MOVE, MOVE], undefined); - }else if(role == 'Claimers'){ - if(spawn.name == "Spawn1") - newCreep = spawn.createCreep([CLAIM, CLAIM, MOVE, MOVE], undefined); - else - newCreep = spawn.createCreep([CLAIM,CLAIM,MOVE,MOVE], undefined); - }else if(role == 'Defenders'){ - if(spawn.name == "Spawn1") - newCreep = spawn.createCreep([TOUGH, TOUGH, TOUGH, TOUGH, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE, MOVE ,MOVE, HEAL, HEAL, HEAL, HEAL, ATTACK, ATTACK, ATTACK, ATTACK, ATTACK, ATTACK, ATTACK, ATTACK, ATTACK, ATTACK], undefined); - else - ; - } - return newCreep; -} - -let orderCreep = function(room, flag, role, creepName, creepIndex){ - if(role == 'Harvesters'){ - - roleHarvester.run(room, flag, creepName, creepIndex); - - }else if(role == 'Distributors'){ - - roleDistributor.run(room, flag, creepName, creepIndex); - - }else if(role == 'Upgraders'){ - - roleUpgrader.run(flag, creepName); - - }else if(role == 'Builders'){ - - roleBuilder.run(room, flag, creepName); - - }else if(role == 'Repairers'){ - - roleRepairer.run(room, flag, creepName); - - }else if(role == 'Miners'){ - - roleMiner.run(room, flag, creepName); - - }else if(role == 'Claimers'){ - - roleClaimer.run(flag, creepName); - - }else if(role == 'Defenders'){ - - roleDefender.run('DD', creepName); - - } -} - -let init = function(){ - Memory.myRooms = - { - 'E22S57': - { - 'Flag1': - { - 'Harvesters': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Distributors': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Repairers': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Builders': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Upgraders': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Miners': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Claimers': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Healers': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Melees': {'amountToSpawn': 0, 'creeps': new Array()}, - }, - - 'Flag2': - { - 'Harvesters': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Distributors': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Repairers': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Builders': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Upgraders': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Miners': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Claimers': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Healers': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Melees': {'amountToSpawn': 0, 'creeps': new Array()}, - }, - - 'Flag3': - { - 'Harvesters': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Distributors': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Repairers': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Builders': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Upgraders': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Miners': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Claimers': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Healers': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Melees': {'amountToSpawn': 0, 'creeps': new Array()}, - }, - - 'Flag4': - { - 'Harvesters': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Distributors': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Repairers': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Builders': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Upgraders': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Miners': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Claimers': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Healers': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Melees': {'amountToSpawn': 0, 'creeps': new Array()}, - } - }, - - 'E24S57': - { - 'Flag5': - { - 'Harvesters': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Distributors': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Repairers': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Builders': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Upgraders': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Miners': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Claimers': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Healers': {'amountToSpawn': 0, 'creeps': new Array()}, - 'Melees': {'amountToSpawn': 0, 'creeps': new Array()}, - } - } - }; -} \ No newline at end of file diff --git a/tower.js b/tower.js index 6f97f49..d77e618 100644 --- a/tower.js +++ b/tower.js @@ -9,18 +9,18 @@ var towers = { _.forEach(towers, function(tower){ - var myclosestDamagedStructure = tower.room.find(FIND_STRUCTURES, { + let myclosestDamagedStructure = tower.room.find(FIND_STRUCTURES, { filter: (structure) => structure.structureType != STRUCTURE_WALL && structure.structureType != STRUCTURE_RAMPART && structure.hits < structure.hitsMax }); - var closestDamagedStructure = _.first(_.sortBy(myclosestDamagedStructure, (s)=>s.hits / s.hitsMax)); + let closestDamagedStructure = _.first(_.sortBy(myclosestDamagedStructure, (s)=>s.hits / s.hitsMax)); - var allcontainers = tower.room.find(FIND_STRUCTURES, { + let allcontainers = tower.room.find(FIND_STRUCTURES, { filter: (s) => { return ( s.structureType == STRUCTURE_CONTAINER) } }); if(tower.energy > tower.energyCapacity * .7 ){ - var importantstructures = tower.room.find(FIND_STRUCTURES, { + let importantstructures = tower.room.find(FIND_STRUCTURES, { filter: (structure) => { return (structure.structureType == STRUCTURE_CONTAINER && structure.hits < structure.hitsMax) ; }}); @@ -36,7 +36,7 @@ var towers = { } else { console.log("tower waiting for more storage. Currently at: " + Game.rooms[roomname].memory.containerstoragepercent)} } } - var closestHostile = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS); + let closestHostile = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS); if(closestHostile) { tower.attack(closestHostile); }