Re-Write for multi-room support

This commit is contained in:
paradizelost 2016-09-23 00:12:42 -05:00
parent 9cd8f77f36
commit 2ab5943444
12 changed files with 372 additions and 312 deletions

7
NOTES.js Normal file
View File

@ -0,0 +1,7 @@
serializePos: function(pos) {
return pos.x + '_' + pos.y + '_' + pos.roomName;
},
deserializePos: function(str) {
var ar = str.split('_');
return new RoomPosition(ar[0], ar[1], ar[2]);
},

127
main.js
View File

@ -1,78 +1,79 @@
var roleHarvester = require('role.harvester');
var roleHarvester2 = require('role.harvester2');
var roleUpgrader = require('role.upgrader');
var roleBuilder=require('role.builder'); var roleBuilder=require('role.builder');
var roleMiner=require('role.miner'); var roleMiner=require('role.miner');
var roleMiner2=require('role.miner2'); var roleMiner2=require('role.miner2');
var roleHauler = require('role.hauler'); var roleHauler=require('role.hauler')
var roleHauler2 = require('role.hauler2'); var roleHauler2=require('role.hauler2')
var roleWarrior = require('role.warrior'); var roleUpgrader=require('role.upgrader')
var roleTowerrecharger=require('role.recharger')
var runSources=require('sourceprocessing')
var roleRepairbot=require('role.repairbot') var roleRepairbot=require('role.repairbot')
var runTower = require('tower'); var runTower = require('tower');
var roleTowerrecharger = require('role.towerrecharger');
var purgetype="NONE"
var clearflags=0
module.exports.loop = function () { module.exports.loop = function () {
if(purgetype != "NONE"){ _(Game.creeps).filter(c=>c.memory.role == purgetype).forEach(c=>c.suicide()).value() }
runTower.tick();
if(clearflags>0){
console.log("Clearing Flags");
_.forEach(Game.flags, function(flag) {flag.remove()});
}
console.log("--------")
for(var name in Game.rooms) {
if(Game.rooms[name].memory.maxbuilders == undefined){ Game.rooms[name].memory.maxbuilders =4 }
if(Game.rooms[name].memory.maxupgraders == undefined){ Game.rooms[name].memory.maxupgraders = 3 }
if(Game.rooms[name].memory.maxminers == undefined){ Game.rooms[name].memory.maxminers = 1 }
if(Game.rooms[name].memory.maxminer2s == undefined){ Game.rooms[name].memory.maxminer2s = 1 }
if(Game.rooms[name].memory.maxhaulers == undefined){ Game.rooms[name].memory.maxhaulers = 2 }
if(Game.rooms[name].memory.maxhauler2s == undefined){ Game.rooms[name].memory.maxhauler2s = 1 }
if(Game.rooms[name].memory.maxwarriors == undefined){ Game.rooms[name].memory.maxwarriors = 2 }
if(Game.rooms[name].memory.maxtowerrechargers == undefined){ Game.rooms[name].memory.maxtowerrechargers = 3 }
if(Game.rooms[name].memory.maxharvesters == undefined){ Game.rooms[name].memory.maxharvesters = 0 }
if(Game.rooms[name].memory.maxharvester2s == undefined){ Game.rooms[name].memory.maxharvester2s = 0 }
console.log('Room "'+name+'" has ' + Game.rooms[name].energyAvailable + ' energy');
}
for(var name in Memory.creeps) { for(var name in Memory.creeps) {
if(!Game.creeps[name]) { if(!Game.creeps[name]) {
delete Memory.creeps[name]; delete Memory.creeps[name];
console.log('Clearing non-existing creep memory:', name); console.log('Clearing non-existing creep memory:', name);
} }
} }
for(var name in Game.rooms) {
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++){
usedstorage+=_.sum(allstorage[i].store)
mycapacity+=allstorage[i].storeCapacity
}
Game.rooms[name].memory.storagepercent = usedstorage/mycapacity
roleMiner.spawn() var containerusedstorage = 0
roleMiner2.spawn() var containercapacity=0
roleHarvester.spawn() for(var i=0; i < allcontainers.length;i++){
roleHarvester2.spawn() containerusedstorage+=_.sum(allcontainers[i].store)
roleBuilder.spawn() containercapacity+=allcontainers[i].storeCapacity
roleUpgrader.spawn()
roleHauler.spawn()
roleHauler2.spawn()
roleTowerrecharger.spawn()
roleWarrior.spawn()
roleRepairbot.spawn()
for(var name in Game.creeps) {
var creep = Game.creeps[name];
if(creep.memory.role == 'harvester') {
roleHarvester.run(creep);
}
if(creep.memory.role == 'harvester2') {
roleHarvester2.run(creep);
}
if(creep.memory.role == 'upgrader') {
roleUpgrader.run(creep);
}
if(creep.memory.role == 'builder') {
roleBuilder.run(creep);
} }
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')
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 }
if(Game.rooms[name].memory.maxtowerrechargers == undefined){ Game.rooms[name].memory.maxtowerrechargers = 0 }
if(Game.rooms[name].memory.maxharvesters == undefined){ Game.rooms[name].memory.maxharvesters = 0 }
if(Game.rooms[name].memory.maxharvester2s == undefined){ Game.rooms[name].memory.maxharvester2s = 0 }
if(Game.rooms[name].memory.maxrepairbots == undefined){ Game.rooms[name].memory.maxrepairbots = 0 }
if(Game.rooms[name].memory.minworkenergypct == undefined){ Game.rooms[name].memory.minworkenergypct = 0.3 }
if(Game.rooms[name].memory.builderparkx==undefined){Game.rooms[name].memory.builderparkx=24}
if(Game.rooms[name].memory.builderparky==undefined){Game.rooms[name].memory.builderparky=24}
if(Game.rooms[name].memory.hauler2parkx==undefined){Game.rooms[name].memory.hauler2parkx=24}
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){
try{
var creep = mycreeps[mycreep]
if(creep.memory.role == 'miner'){ if(creep.memory.role == 'miner'){
roleMiner.run(creep); roleMiner.run(creep)
} }
if(creep.memory.role == 'miner2'){ if(creep.memory.role == 'miner2'){
roleMiner2.run(creep); roleMiner2.run(creep)
} }
if(creep.memory.role == 'hauler'){ if(creep.memory.role == 'hauler'){
roleHauler.run(creep) roleHauler.run(creep)
@ -80,8 +81,11 @@ module.exports.loop = function () {
if(creep.memory.role == 'hauler2'){ if(creep.memory.role == 'hauler2'){
roleHauler2.run(creep) roleHauler2.run(creep)
} }
if(creep.memory.role == 'warrior'){ if(creep.memory.role == 'builder'){
roleWarrior.run(creep) roleBuilder.run(creep)
}
if(creep.memory.role == 'upgrader'){
roleUpgrader.run(creep)
} }
if(creep.memory.role=='towerrecharger'){ if(creep.memory.role=='towerrecharger'){
roleTowerrecharger.run(creep) roleTowerrecharger.run(creep)
@ -89,5 +93,10 @@ module.exports.loop = function () {
if(creep.memory.role == 'repairbot'){ if(creep.memory.role == 'repairbot'){
roleRepairbot.run(creep) roleRepairbot.run(creep)
} }
} catch (e) {
//console.log(e.toString)
} }
} }
}
console.log('-------')
}

View File

@ -1,27 +1,5 @@
var roleBuilder = { var roleBuilder = {
run: function(creep) {
/** @param {Creep} creep **/
run: function(creep,mysource) {
var allcontainers = creep.room.find(FIND_STRUCTURES, {
filter: (s) => {
return ( s.structureType == STRUCTURE_CONTAINER)
}
});
var usedstorage=0
var mycapacity=0
for(var i=0; i < allcontainers.length;i++){
usedstorage+=_.sum(allcontainers[i].store)
mycapacity+=allcontainers[i].storeCapacity
}
var storagepercent = usedstorage/mycapacity
var ttl = creep.ticksToLive
if(ttl < 300) {
console.log(creep.name + ': ' + ttl + ' - ' + creep.memory.role )
}
if(ttl < 20) {
roleBuilder.spawn("true")
}
if(creep.memory.building && creep.carry.energy == 0) { if(creep.memory.building && creep.carry.energy == 0) {
creep.memory.building = false; creep.memory.building = false;
creep.say('Gathering'); creep.say('Gathering');
@ -33,59 +11,58 @@ var roleBuilder = {
if(creep.memory.building) { if(creep.memory.building) {
var target = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES); var target = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES);
//Check next room as well
if(target != undefined) { if(target != undefined) {
if(storagepercent > .4){
if(creep.build(target) == ERR_NOT_IN_RANGE) { if(creep.build(target) == ERR_NOT_IN_RANGE) {
creep.moveTo(target); creep.moveTo(target);
if(creep.fatigue<1){ if(creep.fatigue<1){
creep.say("MTCS"); creep.say("MTCS");
} else { creep.say("Tired")} } else { creep.say("Tired")}
} }
} else {
creep.say("MTF") } else {
creep.moveTo(Game.flags.Flag2); //console.log(creep.room.memory.builderpark)
} creep.moveTo(creep.room.memory.builderparkx,creep.room.memory.builderparky,creep.room.roomName)
} }
} }
else { else {
var containers = creep.room.find(FIND_STRUCTURES, { var containers = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => { filter: (structure) => {return ((structure.structureType == STRUCTURE_CONTAINER|| structure.structureType == STRUCTURE_STORAGE) && structure.store[RESOURCE_ENERGY] > 0) ;}});
return ((structure.structureType == STRUCTURE_CONTAINER|| structure.structureType == STRUCTURE_STORAGE) && structure.store[RESOURCE_ENERGY] > 0) ;
}});
var allcontainers = creep.room.find(FIND_STRUCTURES, { var allcontainers = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => { filter: (structure) => {
return (structure.structureType == STRUCTURE_CONTAINER ) ; return (structure.structureType == STRUCTURE_CONTAINER ) ;
}}); }});
var sources = creep.room.find(FIND_SOURCES); var sources = creep.room.find(FIND_SOURCES);
var droppedenergy = creep.room.find(FIND_DROPPED_ENERGY ); var droppedenergy = creep.pos.findClosestByRange(FIND_DROPPED_ENERGY );
if(containers==0){
if(creep.pickup(droppedenergy) == ERR_NOT_IN_RANGE) {
if(allcontainers.length<1){
if(creep.pickup(droppedenergy[0]) == ERR_NOT_IN_RANGE) {
creep.say("MTDE"); creep.say("MTDE");
creep.moveTo(droppedenergy[0]); creep.moveTo(droppedenergy);
} }
} else { } else {
if(creep.withdraw(containers[0],RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { if(creep.withdraw(containers[0],RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.say("MTSC"); creep.say("MTSC");
creep.moveTo(containers[0]); creep.moveTo(containers[0]);
} }
} }
} }
}, },
spawn: function(dyingcreep="false"){ 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 myrole='builder';
var nummyrole=4; var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole);
var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']);
if(myroles.length < nummyrole || dyingcreep=="true" ) { if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) {
if(dyingcreep=="true"){console.log(myrole + " dying. preventative spawn")} var newName = spawn.createCreep([WORK,WORK,CARRY,MOVE,MOVE], undefined, {role: myrole});
console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole);
var newName = Game.spawns['Spawn1'].createCreep([MOVE,MOVE,MOVE,WORK,CARRY,CARRY,CARRY], undefined, {role: myrole});
console.log('Spawning new ' + myrole + ': ' + newName); console.log('Spawning new ' + myrole + ': ' + newName);
} }
} }
}
}; };
module.exports = roleBuilder; module.exports = roleBuilder;

View File

@ -2,6 +2,7 @@ var roleHauler = {
run: function(creep) { run: function(creep) {
if(creep.memory.hauling == undefined){creep.memory.hauling=false} if(creep.memory.hauling == undefined){creep.memory.hauling=false}
if(creep.memory.hauling && creep.carry.energy == 0) { if(creep.memory.hauling && creep.carry.energy == 0) {
creep.memory.destsource=undefined
creep.memory.hauling = false; creep.memory.hauling = false;
creep.say('gathering'); creep.say('gathering');
} }
@ -9,51 +10,62 @@ var roleHauler = {
creep.memory.hauling = true; creep.memory.hauling = true;
creep.say('hauling'); creep.say('hauling');
} }
var sources = creep.room.find(FIND_DROPPED_ENERGY ); var sources = creep.pos.findClosestByRange(FIND_DROPPED_ENERGY );
if(creep.memory.hauling==false){ if(creep.memory.hauling==false){
if(creep.pickup(sources[0]) == ERR_NOT_IN_RANGE && creep.carryCapacity > creep.carry.energy) { if(Game.getObjectById(creep.memory.destsource.id)==undefined){creep.memory.destsource=undefined}
creep.moveTo(sources[0]); var 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 ) if(sources != undefined )
{ var spawntargets = creep.room.find(FIND_STRUCTURES, { { var spawntargets = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: (structure) => { filter: (structure) => {
return ((structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN) && structure.energy < structure.energyCapacity) return ((structure.structureType == STRUCTURE_EXTENSION || structure.structureType == STRUCTURE_SPAWN) && structure.energy < structure.energyCapacity)
} }
}); });
var containertargets = creep.room.find(FIND_STRUCTURES, { var containertargets = creep.pos.findClosestByRange(FIND_STRUCTURES, {
filter: (structure) => { filter: (structure) => {
return ((structure.structureType == STRUCTURE_CONTAINER || structure.structureType == STRUCTURE_STORAGE) && _.sum(structure.store) < structure.storeCapacity) ; return ((structure.structureType == STRUCTURE_CONTAINER ) && _.sum(structure.store) < structure.storeCapacity) ;
} }
}); });
if(spawntargets.length > 0) { var storagetargets = creep.pos.findClosestByRange(FIND_STRUCTURES, {
if(creep.transfer(spawntargets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { filter: (structure) => {
creep.moveTo(spawntargets[0]); return ((structure.structureType == STRUCTURE_STORAGE ) && _.sum(structure.store) < structure.storeCapacity) ;
} }
} else if (containertargets.length > 0) {
if(creep.transfer(containertargets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { });
creep.moveTo(containertargets[0]); if(spawntargets) {
if(creep.transfer(spawntargets, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(spawntargets);
}
} else if (containertargets) {
if(creep.transfer(containertargets, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(containertargets);
} }
} else { } else {
creep.say("") if(creep.transfer(storagetargets, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(Game.flags.Flag1); creep.moveTo(storagetargets);
}
} }
}else{
creep.say("NS,NT")
creep.moveTo(Game.flags.Flag1);
} }
} }
}, },
spawn: function(){ 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 myrole='hauler';
var nummyrole=2; var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole);
var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']);
if(myroles.length < nummyrole) { if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) {
console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole); var newName = spawn.createCreep([CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,MOVE,MOVE,MOVE,MOVE], undefined, {role: myrole});
var newName = Game.spawns['Spawn1'].createCreep([MOVE,MOVE,MOVE,MOVE,MOVE,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY], undefined, {role: myrole});
console.log('Spawning new ' + myrole + ': ' + newName); console.log('Spawning new ' + myrole + ': ' + newName);
} }
} }
}
}; };
module.exports = roleHauler; module.exports = roleHauler;

View File

@ -2,6 +2,7 @@
/** @param {Creep} creep **/ /** @param {Creep} creep **/
run: function(creep) { run: function(creep) {
if(creep.memory.hauling == undefined){creep.memory.hauling=false} if(creep.memory.hauling == undefined){creep.memory.hauling=false}
if(creep.memory.hauling && creep.carry.energy == 0) { if(creep.memory.hauling && creep.carry.energy == 0) {
creep.memory.hauling = false; creep.memory.hauling = false;
@ -21,6 +22,7 @@
creep.say("MTSC"); creep.say("MTSC");
creep.moveTo(container); creep.moveTo(container);
} }
}
} else { } else {
var spawntarget = creep.pos.findClosestByRange(FIND_STRUCTURES, { var spawntarget = creep.pos.findClosestByRange(FIND_STRUCTURES, {
filter: (structure) => { filter: (structure) => {
@ -33,21 +35,24 @@
creep.moveTo(spawntarget); creep.moveTo(spawntarget);
} }
} else { } else {
creep.say('MTF') creep.say('NTD');
creep.moveTo(Game.flags.Flag1); creep.moveTo(creep.room.memory.hauler2parkx,creep.room.memory.hauler2parky,creep.room.roomName)
}
} }
} }
}, },
spawn: function(){ 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 myrole='hauler2';
var nummyrole=1; var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole);
var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']);
if(myroles.length < nummyrole) { if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) {
console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole); var newName = spawn.createCreep([CARRY,CARRY,CARRY,MOVE,MOVE], undefined, {role: myrole});
var newName = Game.spawns['Spawn1'].createCreep([MOVE,CARRY], undefined, {role: myrole});
console.log('Spawning new ' + myrole + ': ' + newName); console.log('Spawning new ' + myrole + ': ' + newName);
} }
} }
}
}; };
module.exports = roleHauler2; module.exports = roleHauler2;

View File

@ -1,24 +1,23 @@
var roleMiner = { var roleMiner = {
run: function(creep) {
/** @param {Creep} creep **/
run: function(creep,mysource) {
var sources = creep.room.find(FIND_SOURCES); var sources = creep.room.find(FIND_SOURCES);
if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) { if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[0]); creep.moveTo(sources[0]);
} }
}, },
spawn: function(){ 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 myrole='miner';
var nummyrole=1; var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole);
var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']);
if(myroles.length < nummyrole) { 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('Miners: ' + myroles.length + ' Needed: ' + nummyrole);
var newName = Game.spawns['Spawn1'].createCreep([WORK,WORK,WORK,WORK,WORK,MOVE], undefined, {role: myrole});
console.log('Spawning new ' + myrole + ': ' + newName); console.log('Spawning new ' + myrole + ': ' + newName);
} }
} }
}
}; };
module.exports = roleMiner; module.exports = roleMiner;

View File

@ -1,22 +1,23 @@
var roleMiner2 = { var roleMiner2 = {
/** @param {Creep} creep **/
run: function(creep) { run: function(creep) {
var sources = creep.room.find(FIND_SOURCES); var sources = creep.room.find(FIND_SOURCES);
if(creep.harvest(sources[1]) == ERR_NOT_IN_RANGE) { if(creep.harvest(sources[1]) == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[1]); creep.moveTo(sources[1]);
} }
}, },
spawn: function(){ 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 myrole='miner2';
var nummyrole=1; var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole);
var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']);
if(myroles.length < nummyrole) { if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) {
console.log('Miners: ' + myroles.length + ' Needed: ' + nummyrole); var newName = spawn.createCreep([WORK,WORK,WORK,WORK,WORK,MOVE], undefined, {role: myrole});
var newName = Game.spawns['Spawn1'].createCreep([WORK,WORK,WORK,WORK,WORK,MOVE], undefined, {role: myrole});
console.log('Spawning new ' + myrole + ': ' + newName); console.log('Spawning new ' + myrole + ': ' + newName);
} }
} }
}
}; };
module.exports = roleMiner2; module.exports = roleMiner2;

51
role.recharger.js Normal file
View File

@ -0,0 +1,51 @@
var roleTowerrecharger = {
/** @param {Creep} creep **/
run: function(creep) {
if(creep.memory.recharging && creep.carry.energy == 0) {
creep.memory.recharging = false;
creep.say('refilling');
}
if(!creep.memory.recharging && creep.carry.energy == creep.carryCapacity) {
creep.memory.recharging = true;
creep.say('building');
}
if(!creep.memory.recharging) {
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.moveTo(spawntarget);
}
}
}
},
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);
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});
console.log('Spawning new ' + myrole + ': ' + newName);
}
}
}
};
module.exports = roleTowerrecharger;

View File

@ -21,42 +21,40 @@ var roleRepairbot = {
creep.moveTo(container); creep.moveTo(container);
} }
} else { } else {
var myclosestDamagedStructure = creep.room.find(FIND_STRUCTURES, { var importantstructures = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => structure.hits < structure.hitsMax filter: (structure) => {
}); return (structure.structureType == STRUCTURE_CONTAINER && structure.hits < structure.hitsMax) ;
var closestDamagedStructure = _.first(_.sortBy(myclosestDamagedStructure, (s)=>s.hits / s.hitsMax)); }});
var allcontainers = creep.room.find(FIND_STRUCTURES, { importantstructures = _.sortBy(importantstructures, (s)=>s.hits / s.hitsMax)
filter: (s) => { if(importantstructures.length > 0){
return ( s.structureType == STRUCTURE_CONTAINER) if(creep.repair(importantstructures[0]) == ERR_NOT_IN_RANGE){
creep.moveTo(importantstructures[0])
}
} else {
var 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){
creep.moveTo(damagedstructures[0])
} }
});
var usedstorage=0
var mycapacity=0
for(var i=0; i < allcontainers.length;i++){
usedstorage+=_.sum(allcontainers[i].store)
mycapacity+=allcontainers[i].storeCapacity
} }
var storagepercent = usedstorage/mycapacity
if(closestDamagedStructure) {
if(storagepercent > .1){
console.log(creep.name + " repairing. Currently at: " + storagepercent)
if(creep.repair(closestDamagedStructure) == ERR_NOT_IN_RANGE){ creep.moveTo(closestDamagedStructure)}
} else { console.log(creep.name + " waiting for more storage. Currently at: " + storagepercent)}
} }
} }
}, },
spawn: function(){ 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 myrole='repairbot';
var nummyrole=1; var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole);
var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']);
if(myroles.length < nummyrole) { if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) {
var newName = spawn.createCreep([WORK,CARRY,MOVE], undefined, {role: myrole});
console.log('Miners: ' + myroles.length + ' Needed: ' + nummyrole);
var newName = Game.spawns['Spawn1'].createCreep([WORK,CARRY,CARRY,MOVE], undefined, {role: myrole});
console.log('Spawning new ' + myrole + ': ' + newName); console.log('Spawning new ' + myrole + ': ' + newName);
} }
} }
}
}; };

View File

@ -1,8 +1,7 @@
var roleUpgrader = { var roleUpgrader = {
/** @param {Creep} creep **/
run: function(creep) { run: function(creep) {
if(creep.memory.upgrading==undefined){creep.memory.upgrading=true}
if(creep.memory.upgrading && creep.carry.energy == 0) { if(creep.memory.upgrading && creep.carry.energy == 0) {
creep.memory.upgrading = false; creep.memory.upgrading = false;
creep.say('harvesting'); creep.say('harvesting');
@ -11,57 +10,47 @@ var roleUpgrader = {
creep.memory.upgrading = true; creep.memory.upgrading = true;
creep.say('upgrading'); creep.say('upgrading');
} }
var allcontainers = creep.room.find(FIND_STRUCTURES, {
filter: (s) => {
return ( s.structureType == STRUCTURE_CONTAINER)
}
});
var usedstorage=0
var mycapacity=0
for(var i=0; i < allcontainers.length;i++){
usedstorage+=_.sum(allcontainers[i].store)
mycapacity+=allcontainers[i].storeCapacity
}
var storagepercent = usedstorage/mycapacity
if(creep.memory.upgrading) { if(creep.memory.upgrading) {
if(storagepercent > .3){ if(creep.room.memory.containerstoragepercent>.6){
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) { if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
creep.say('MTRC')
creep.moveTo(creep.room.controller); creep.moveTo(creep.room.controller);
} }
} } else { creep.moveTo(creep.room.memory.upgraderparkx,creep.room.memory.upgraderparky,creep.room.roomName) }
} } else {
else {
var containers = creep.room.find(FIND_STRUCTURES, { var container = creep.pos.findClosestByRange(FIND_STRUCTURES, {
filter: (structure) => { filter: (structure) => {
return ((structure.structureType == STRUCTURE_CONTAINER|| structure.structureType == STRUCTURE_STORAGE) && structure.store[RESOURCE_ENERGY] > 0) ; return ((structure.structureType == STRUCTURE_CONTAINER|| structure.structureType == STRUCTURE_STORAGE) && structure.store[RESOURCE_ENERGY] > 0) ;
}}); }});
var allcontainers = creep.room.find(FIND_STRUCTURES, { if(container){
filter: (structure) => {
return (structure.structureType == STRUCTURE_CONTAINER) ; if(creep.withdraw(container,RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
}}); creep.say("MTSC");
var droppedenergy = creep.room.find(FIND_DROPPED_ENERGY ); creep.moveTo(container);
if(allcontainers.length<0){
if(creep.pickup(droppedenergy[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(droppedenergy[0]);
} }
}else{ }else{
if(creep.withdraw(containers[0],RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) { var droppedenergy = creep.room.find(FIND_DROPPED_ENERGY );
creep.moveTo(containers[0]); if(creep.pickup(droppedenergy[0]) == ERR_NOT_IN_RANGE) {
creep.say("MTDE");
creep.moveTo(droppedenergy[0]);
} }
} }
} }
}, },
spawn: function(){ 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 myrole='upgrader';
var nummyrole=3; var myroles = _.filter(Game.rooms[roomname].find(FIND_MY_CREEPS), (creep) => creep.memory.role == myrole);
var myroles = _.filter(Game.creeps, (creep) => creep.memory.role == myrole); console.log(myrole + 's: ' + myroles.length + ' Needed: ' + Game.rooms[roomname].memory['max'+myrole+'s']);
if(myroles.length < nummyrole) { if(myroles.length < Game.rooms[roomname].memory['max'+myrole+'s']) {
console.log(myrole + 's: ' + myroles.length + ' Needed: ' + nummyrole); var newName = spawn.createCreep([MOVE,MOVE,WORK,WORK,WORK,CARRY,CARRY,CARRY], undefined, {role: myrole});
var newName = Game.spawns['Spawn1'].createCreep([MOVE,MOVE,MOVE,MOVE,WORK,WORK,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY], undefined, {role: myrole});
console.log('Spawning new ' + myrole + ': ' + newName); console.log('Spawning new ' + myrole + ': ' + newName);
} }
} }
}
}; };
module.exports = roleUpgrader; module.exports = roleUpgrader;

19
sourceprocessing.js Normal file
View File

@ -0,0 +1,19 @@
var 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{
if(unassignedhaulers.length > 0){
unassignedhaulers[0].memory.destsource = source
}
}
}
}
};
module.exports = runSources;

View File

@ -1,16 +1,16 @@
var towers = { var towers = {
/** @param {Game} game **/ /** @param {Game} game **/
tick: function() { tick: function(roomname) {
towers = Game.spawns.Spawn1.room.find(FIND_MY_STRUCTURES, { towers = Game.rooms[roomname].find(FIND_MY_STRUCTURES, {
filter: { structureType: STRUCTURE_TOWER } filter: { structureType: STRUCTURE_TOWER }
}) })
_.forEach(towers, function(tower){ _.forEach(towers, function(tower){
var myclosestDamagedStructure = tower.room.find(FIND_STRUCTURES, { var myclosestDamagedStructure = tower.room.find(FIND_STRUCTURES, {
filter: (structure) => structure.hits < structure.hitsMax filter: (structure) => structure.structureType != STRUCTURE_WALL && structure.structureType != STRUCTURE_RAMPART && structure.hits < structure.hitsMax
}); });
var closestDamagedStructure = _.first(_.sortBy(myclosestDamagedStructure, (s)=>s.hits / s.hitsMax)); var closestDamagedStructure = _.first(_.sortBy(myclosestDamagedStructure, (s)=>s.hits / s.hitsMax));
@ -19,28 +19,21 @@ var towers = {
return ( s.structureType == STRUCTURE_CONTAINER) return ( s.structureType == STRUCTURE_CONTAINER)
} }
}); });
var usedstorage=0 if(tower.energy > tower.energyCapacity * .7 ){
var mycapacity=0
for(var i=0; i < allcontainers.length;i++){
usedstorage+=_.sum(allcontainers[i].store)
mycapacity+=allcontainers[i].storeCapacity
}
var storagepercent = usedstorage/mycapacity
var importantstructures = tower.room.find(FIND_STRUCTURES, { var importantstructures = tower.room.find(FIND_STRUCTURES, {
filter: (structure) => { filter: (structure) => {
return (structure.structureType == STRUCTURE_CONTAINER && structure.hits < structure.hitsMax) ; return (structure.structureType == STRUCTURE_CONTAINER && structure.hits < structure.hitsMax) ;
}}); }});
importantstructures = _.sortBy(importantstructures, (s)=>s.hits / s.hitsMax) importantstructures = _.sortBy(importantstructures, (s)=>s.hits / s.hitsMax)
if(tower.energy > tower.energyCapacity * .7 ){
if(importantstructures.length > 0){ if(importantstructures.length > 0){
tower.repair(importantstructures[0]); tower.repair(importantstructures[0]);
} else } else
{ {
if(closestDamagedStructure) { if(closestDamagedStructure) {
if(storagepercent > .8){ if(Game.rooms[roomname].memory.containerstoragepercent > .8){
console.log("tower repairing. Currently at: " + storagepercent) console.log("tower repairing. Currently at: " + Game.rooms[roomname].memory.containerstoragepercent)
tower.repair(closestDamagedStructure); tower.repair(closestDamagedStructure);
} else { console.log("tower waiting for more storage. Currently at: " + storagepercent)} } else { console.log("tower waiting for more storage. Currently at: " + Game.rooms[roomname].memory.containerstoragepercent)}
} }
} }
var closestHostile = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS); var closestHostile = tower.pos.findClosestByRange(FIND_HOSTILE_CREEPS);