screeps/role.miner.js

53 lines
2.2 KiB
JavaScript
Raw Normal View History

2016-09-28 08:37:28 -05:00
let roleMiner = {
2016-09-23 00:12:42 -05:00
run: function(creep) {
2020-11-14 01:18:05 -06:00
//if(Game.getObjectById(creep.memory.destsource.id)==undefined){creep.memory.destsource=undefined}
let ignorecreeps=true
let filllevel = _.sum(creep.carry)
if(creep.memory.working && filllevel == 0) {
creep.memory.working = false;
creep.say('Gathering');
}
if(!creep.memory.working && _.sum(creep.carry) == creep.carryCapacity) {
creep.memory.working = true;
creep.say('working');
}
if(!creep.memory.working){
let mysource=Game.rooms[creep.room.name].find(FIND_MINERALS)[0]
if(creep.harvest(mysource) == ERR_NOT_IN_RANGE) {
creep.travelTo(mysource);
}
2020-11-17 10:30:13 -06:00
} else if ((creep.ticksToLive < 300 || creep.ticksToLive <= creep.memory.renewto) && (Game.rooms[creep.room.name].find(FIND_MY_SPAWNS, {filter: (r) =>{return ( r.store[RESOURCE_ENERGY]>1)}})) ) {
if(creep.memory.renewto == undefined){
creep.memory.renewto = 1200
} else {
if(creep.ticksToLive >= creep.memory.renewto){
delete creep.memory.renewto
}
}
//console.log(creep.name + ": " + creep.ticksToLive + " " + creep.memory.renewto)
creep.say('renewing')
let spawn = creep.pos.findClosestByRange(FIND_MY_SPAWNS)
if(spawn.renewCreep(creep) == ERR_NOT_IN_RANGE)
{
creep.moveTo(spawn);
}
} else {
let storagetargets = creep.pos.findClosestByRange(FIND_STRUCTURES, {filter: (s) => {return ((s.structureType == STRUCTURE_STORAGE || s.structureType == STRUCTURE_CONTAINER || s.structureType == STRUCTURE_TERMINAL || s.structureType == STRUCTURE_FACTORY ) && _.sum(s.store) < s.storeCapacity) ;}});
2020-11-14 01:18:05 -06:00
if(storagetargets){
if(this.transferAll(creep,storagetargets) == ERR_NOT_IN_RANGE) {
creep.moveTo(storagetargets,{ignoreCreeps:ignorecreeps})
}
}
}
},
transferAll: function(creep,targetStorage){
let result = 0;
for(mat in creep.store)
{
let tempResult = creep.transfer(targetStorage,mat);
if(tempResult !== ERR_INVALID_ARGS) {result = tempResult;}
}
return result;
}
2016-09-15 00:30:24 -05:00
};
module.exports = roleMiner;