-
Notifications
You must be signed in to change notification settings - Fork 7
/
role.reloader.js
44 lines (38 loc) · 1.39 KB
/
role.reloader.js
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
const spawnHelper = require("helper.spawning");
module.exports = {
name: "reloader",
parts: spawnHelper.makeParts(12, CARRY, 6, MOVE),
run: function(creep) {
if(!creep.room.storage) {
return;
}
if(_.sum(creep.store) > 0) {
this.carryToTower(creep);
} else {
this.pickupResource(creep);
}
},
carryToTower: function(creep) {
// withdraw when passing by
if(creep.store.energy < creep.store.getCapacity() && creep.pos.isNearTo(creep.room.storage)) {
creep.withdraw(creep.room.storage, RESOURCE_ENERGY);
}
let towers = _.sortBy(_.sortBy(creep.room.find(FIND_MY_STRUCTURES, { filter: (s) => s.structureType === STRUCTURE_TOWER && s.store.getFreeCapacity(RESOURCE_ENERGY) > 0 }), (s) => s.pos.getRangeTo(creep)), (s) => s.energy);
let target = towers[0];
if(!target) return;
if(creep.pos.isNearTo(target)) {
creep.transfer(target, RESOURCE_ENERGY);
} else {
creep.goTo(target);
}
},
pickupResource: function(creep) {
if(creep.pos.isNearTo(creep.room.storage)) {
creep.withdraw(creep.room.storage, RESOURCE_ENERGY);
} else {
creep.goTo(creep.room.storage);
}
}
};
const profiler = require("screeps-profiler");
profiler.registerObject(module.exports, 'reloader');