summaryrefslogtreecommitdiff
path: root/src/role.harvester.js
blob: 5c6f85c391d8239b64430614b9881568a474303a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// vim: tw=80 ts=2 sw=2 et

const util = require('creep');

module.exports = {
  run: (creep) => {
    if (creep.carry.energy < creep.carryCapacity) {
      const source = creep.pos.findClosestByPath(FIND_SOURCES);
      if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
        creep.moveTo(source);
      }
    } else {
      const spawn = creep.pos.findClosestByPath(FIND_MY_STRUCTURES, {
        filter: (structure) => {
          return (structure.structureType == STRUCTURE_EXTENSION
              ||  structure.structureType == STRUCTURE_SPAWN)
              && structure.energy < structure.energyCapacity;
        }
      });
      if (creep.transfer(spawn, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
        creep.moveTo(spawn);
      }
    }
  }
};