My personal screeps.com scripts, various branches/tags for various states.

role.harvester.js 857B

1234567891011121314151617181920212223242526272829
  1. var roleHarvester = {
  2. /** @param {Creep} creep **/
  3. run: function(creep) {
  4. if(creep.carry.energy < creep.carryCapacity) {
  5. var sources = creep.room.find(FIND_SOURCES);
  6. if(creep.harvest(sources[0]) == ERR_NOT_IN_RANGE) {
  7. creep.moveTo(sources[0]);
  8. }
  9. }
  10. else {
  11. var targets = creep.room.find(FIND_STRUCTURES, {
  12. filter: (structure) => {
  13. return (structure.structureType == STRUCTURE_EXTENSION ||
  14. structure.structureType == STRUCTURE_SPAWN ||
  15. structure.structureType == STRUCTURE_TOWER) && structure.energy < structure.energyCapacity;
  16. }
  17. });
  18. if(targets.length > 0) {
  19. if(creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
  20. creep.moveTo(targets[0]);
  21. }
  22. }
  23. }
  24. }
  25. };
  26. module.exports = roleHarvester;