-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.repailer.js
58 lines (54 loc) · 2.09 KB
/
role.repailer.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
var roleBuilder = require('role.builder');
/**
* This use case allows you make creeps work as harvesters.
**/
const roleRepailer = (creep) => {
// if creep is trying to repair something but has no energy left
if (creep.memory.repairing == true && creep.carry.energy == 0) {
// switch state
creep.memory.repairing = false;
}
// if creep is harvesting energy but is full
else if (!creep.memory.repairing && creep.carry.energy == creep.carryCapacity) {
// switch state
creep.memory.repairing = true;
creep.say('⚡ repair');
}
// if creep is supposed to repair something
if (creep.memory.repairing == true) {
// find closest structure with less than max hits
// Exclude walls because they have way too many max hits and would keep
// our repairers busy forever. We have to find a solution for that later.
var structure = creep.pos.findClosestByPath(FIND_STRUCTURES, {
// the second argument for findClosestByPath is an object which takes
// a property called filter which can be a function
// we use the arrow operator to define it
filter: (s) => s.hits < s.hitsMax && s.structureType != STRUCTURE_WALL
});
// if we find one
if (structure != undefined) {
// try to repair it, if it is out of range
if (creep.repair(structure) == ERR_NOT_IN_RANGE) {
// move towards it
creep.moveTo(structure);
}
}
// if we can't fine one
else {
// look for construction sites
creep.say('now builder');
roleBuilder(creep);
}
}
// if creep is supposed to harvest energy from source
else {
// find closest source
var source = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE);
// try to harvest energy, if the source is not in range
if (creep.harvest(source) == ERR_NOT_IN_RANGE) {
// move towards the source
creep.moveTo(source);
}
}
};
module.exports = roleRepailer;