-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.harvester.js
63 lines (58 loc) · 1.97 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
/**
*
* @type {Object}
*/
module.exports = {
/**
*
* @param {Creep} creep
*/
standardAction: function (creep) {
if (creep.carry[RESOURCE_ENERGY] === 0) {
creep.say("work");
creep.memory.status = "work"
}
else if (_.sum(creep.carry) === creep.carryCapacity) {
creep.say("return");
creep.memory.status = "return";
}
if (creep.memory.status === "work") {
let source = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE);
if (source && creep.harvest(source) === ERR_NOT_IN_RANGE) {
creep.moveTo(source);
}
}
else if (creep.memory.status === "return") {
let spawn = creep.pos.findClosestByPath(FIND_MY_STRUCTURES, {
filter: function (structure) {
return structure.structureType === STRUCTURE_SPAWN;
}
});
let repair = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: function (structure) {
return structure.hits < structure.hitsMax;
}
});
let controller = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: function (structure) {
return structure.structureType === STRUCTURE_CONTROLLER;
}
});
if (spawn && spawn.energy !== spawn.energyCapacity) {
if (creep.transfer(spawn, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.moveTo(spawn);
}
}
else if (controller) {
if (creep.upgradeController(controller) === ERR_NOT_IN_RANGE) {
creep.moveTo(controller);
}
}
else if (repair) {
if (creep.repair(repair) === ERR_NOT_IN_RANGE) {
creep.moveTo(repair.pos);
}
}
}
}
};