-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrole.repairer.js
63 lines (63 loc) · 2.35 KB
/
role.repairer.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
/**
* @author Nicolas Durant
* @email [email protected]
* @create date 2019-11-29 14:18:41
* @modify date 2019-12-03 11:13:11
* @desc Repairer Role for a Creep. It will harvest Energy and put it into decaying structures.
*/
var _BUILDER = require('role.builder');
module.exports = {
/**
* The function that is defining the creeps behavior.
*
* @param {any} creep - Creep Object
*
* @memberOf Upgrader
*/
run: function (creep) {
// the creep is fully packed
if (creep.memory.idle && creep.store.getUsedCapacity() === creep.store.getCapacity()){
creep.say('Harvested👍')
creep.memory.idle = false;
}
// if the creep is empty or has not the idle memory yet
else if (!creep.memory.idle && creep.store.getUsedCapacity() === 0){
creep.say('Deposited👍')
creep.memory.idle = true;
}
// if the creep is idle, we sent it to the next source that is still harvestable (ACTIVE)
if (creep.memory.idle) {
const target = creep.pos.findClosestByRange(FIND_SOURCES_ACTIVE);
if (creep.harvest(target) === ERR_NOT_IN_RANGE){
if (creep.memory.status != 'to_work'){
creep.say('To work 🤮')
creep.memory.status = `to_work`
}
creep.moveTo(target, {reusePath: 5});
}
}
// else we sent it to repair decaying structures
else {
const structures = creep.room.find(FIND_STRUCTURES, {
filter: (s) => {
return s.hits < s.hitsMax && s.structureType != STRUCTURE_WALL
}
});
if (structures) {
structures.sort((a,b) => {
return a.hits - b.hits
})
if (creep.repair(structures[0]) === ERR_NOT_IN_RANGE){
if (creep.memory.status != 'to_repair'){
creep.say('To repair 🔄')
creep.memory.status = `to_repair`
}
creep.moveTo(structures[0], {reusePath: 5});
}
}// if there are no more constructions to be build atm, we make the creep an upgrader
else{
_BUILDER.run(creep);
}
}
}
};