screeps/role.upgrader.js
2016-09-15 01:41:19 -05:00

67 lines
2.6 KiB
JavaScript

var roleUpgrader = {
/** @param {Creep} creep **/
run: function(creep) {
if(creep.memory.upgrading && creep.carry.energy == 0) {
creep.memory.upgrading = false;
creep.say('harvesting');
}
if(!creep.memory.upgrading && creep.carry.energy == creep.carryCapacity) {
creep.memory.upgrading = true;
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(storagepercent > .3){
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.controller);
}
}
}
else {
var containers = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_CONTAINER && structure.store[RESOURCE_ENERGY] > 0) ;
}});
var allcontainers = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_CONTAINER) ;
}});
var droppedenergy = creep.room.find(FIND_DROPPED_ENERGY );
if(allcontainers.length<0){
if(creep.pickup(droppedenergy[0]) == ERR_NOT_IN_RANGE) {
creep.moveTo(droppedenergy[0]);
}
} else{
if(creep.withdraw(containers[0],RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(containers[0]);
}
}
}
},
spawn: function(){
var myrole='upgrader';
var nummyrole=3;
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([MOVE,MOVE,MOVE,MOVE,WORK,WORK,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY,CARRY], undefined, {role: myrole});
console.log('Spawning new ' + myrole + ': ' + newName);
}
}
};
module.exports = roleUpgrader;