-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.rampartRepairer.js
70 lines (62 loc) · 2.04 KB
/
role.rampartRepairer.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
65
66
67
68
69
70
var roleBuilder = require('role.builder');
module.exports = {
// a function to run the logic for this role
/** @param {Creep} creep */
run: function(creep) {
// if creep is trying to repair something but has no energy left
if (creep.memory.working == true && creep.carry.energy == 0) {
// switch state
creep.memory.working = false;
}
// if creep is harvesting energy but is full
else if (creep.memory.working == false && creep.carry.energy == creep.carryCapacity) {
// switch state
creep.memory.working = true;
}
// if creep is supposed to repair something
if (creep.memory.working == true) {
// find any ramparts that need to be constructed first
var constructionSite = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES, {
filter: (s) => s.structureType == STRUCTURE_RAMPART
});
// if there are ramparts that need to be built
if (constructionSite) {
if (creep.build(constructionSite) == ERR_NOT_IN_RANGE) {
// move towards the constructionSite
creep.travelTo(constructionSite);
}
}
// if there are no ramparts that need to be built
else {
// find all ramparts in the room
var ramparts = creep.room.find(FIND_STRUCTURES, {
filter: (s) => s.structureType == STRUCTURE_RAMPART
});
var target = undefined;
for (let rampart of ramparts) {
if (rampart.hits < 350000) {
target = rampart;
break;
}
}
// if we find a rampart that has to be repaired
if (target != undefined) {
// try to repair it, if not in range
if (creep.repair(target) == ERR_NOT_IN_RANGE) {
// move towards it
creep.travelTo(target);
}
}
// if we can't fine one
else {
// look for construction sites
roleBuilder.run(creep);
}
}
}
// if creep is supposed to get energy
else {
creep.getEnergy(true, true);
}
}
};