This repository was archived by the owner on May 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.harvester.js
64 lines (60 loc) · 2.73 KB
/
role.harvester.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
55
56
57
58
59
60
61
62
63
64
var roleBuilder = require('role.builder');
var roleUpgrader = require('role.upgrader');
var ml = require('methodlib');
var roleHarvester = {
/** @param {Creep} creep **/
run: function (creep) {
if (creep.memory.harvesting && creep.carry.energy == creep.carryCapacity) {
if (!creep.moveTo(Game.flags.Flag1))
creep.memory.harvesting = false;
return;
}
if (!creep.memory.harvesting && creep.carry.energy == 0) {
creep.memory.harvesting = true;
}
if (creep.memory.harvesting) {
var sources = creep.room.find(FIND_SOURCES, {
filter: (source) => (source.energy > 0)
});
var ret = creep.harvest(sources[((creep.memory.id*2/3)>>0 + 1) % sources.length]);
if (ret == ERR_NOT_IN_RANGE) {
creep.moveTo(sources[((creep.memory.id*2/3)>>0 + 1) % sources.length]);
}
} else {
var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => (structure.structureType == STRUCTURE_SPAWN || structure.structureType == STRUCTURE_EXTENSION) && (structure.energy < structure.energyCapacity)
});
if (targets.length > 0) {
if (creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0]);
}
} else {
targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => (structure.structureType == STRUCTURE_TOWER) && (structure.energy < structure.energyCapacity)
});
if (targets.length > 0) {
if (creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0]);
}
} else {
targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => (structure.structureType == STRUCTURE_CONTAINER || structure.structureType == STRUCTURE_STORAGE) && (structure.energy < structure.energyCapacity)
});
if (targets.length > 0) {
if (creep.transfer(targets[0], RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(targets[0]);
}
} else {
if ((creep.memory.id >> 1) % 2) {
roleUpgrader.run(creep);
//console.log('nope');
} else {
roleBuilder.run(creep);
}
}
}
}
}
}
};
module.exports = roleHarvester;