-
Notifications
You must be signed in to change notification settings - Fork 62
/
prototype.creep.js
52 lines (49 loc) · 1.86 KB
/
prototype.creep.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
var roles = {
harvester: require('role.harvester'),
upgrader: require('role.upgrader'),
builder: require('role.builder'),
repairer: require('role.repairer'),
wallRepairer: require('role.wallRepairer'),
longDistanceHarvester: require('role.longDistanceHarvester'),
claimer: require('role.claimer'),
miner: require('role.miner'),
lorry: require('role.lorry')
};
Creep.prototype.runRole =
function () {
roles[this.memory.role].run(this);
};
/** @function
@param {bool} useContainer
@param {bool} useSource */
Creep.prototype.getEnergy =
function (useContainer, useSource) {
/** @type {StructureContainer} */
let container;
// if the Creep should look for containers
if (useContainer) {
// find closest container
container = this.pos.findClosestByPath(FIND_STRUCTURES, {
filter: s => (s.structureType == STRUCTURE_CONTAINER || s.structureType == STRUCTURE_STORAGE) &&
s.store[RESOURCE_ENERGY] > 0
});
// if one was found
if (container != undefined) {
// try to withdraw energy, if the container is not in range
if (this.withdraw(container, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
// move towards it
this.moveTo(container);
}
}
}
// if no container was found and the Creep should look for Sources
if (container == undefined && useSource) {
// find closest source
var source = this.pos.findClosestByPath(FIND_SOURCES_ACTIVE);
// try to harvest energy, if the source is not in range
if (this.harvest(source) == ERR_NOT_IN_RANGE) {
// move towards it
this.moveTo(source);
}
}
};