-
Notifications
You must be signed in to change notification settings - Fork 0
/
digger.js
54 lines (47 loc) · 1.99 KB
/
digger.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
45
46
47
48
49
50
51
52
53
54
let digger = {
run: function (creep) {
if (creep.carry.energy < creep.carryCapacity) {
let source = Game.getObjectById(creep.memory.assignedSource);
if (source == null) {
for (var spawnName in Game.spawns) {
const spawn = Game.spawns[spawnName];
const energySources = spawn.room.find(FIND_SOURCES);
for (var energySource in energySources) {
const diggersAtSource = _.filter(Game.creeps, (creep) => creep.memory.assignedSource == energySources[energySource].id).length;
if (diggersAtSource < 1) {
creep.memory.assignedSource = energySources[energySource].id;
}
}
}
}
if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
creep.moveTo(source);
}
}
else {
const buildings = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType == STRUCTURE_CONTAINER);
}
});
if (buildings.length > 0) {
let storage = creep.pos.findClosestByPath(buildings);
if (storage) {
if (creep.transfer(storage, RESOURCE_ENERGY, creep.carryCapacity) == ERR_NOT_IN_RANGE || creep.transfer(storage, RESOURCE_ENERGY, creep.carryCapacity) == ERR_FULL) {
DropResources(creep);
}
}
else if (storage.store[RESOURCE_ENERGY] >= storage.storeCapacity - 50) {
DropResources(creep);
}
}
else {
DropResources(creep);
}
}
}
}
function DropResources(creep) {
creep.drop(RESOURCE_ENERGY, creep.carryCapacity);
}
module.exports = digger;