-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrole.repairer.js
76 lines (75 loc) · 2.69 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
64
65
66
67
68
69
70
71
72
73
74
75
76
var roleBuilder = require('role.builder');
module.exports = {
run: function(creep) {
if (creep.spawning === false) {
var numEnergySources = 2;
if (creep.memory.sourcenum == undefined || creep.memory.sourcenum > 1) {
// var numEnergySources = creep.room.lookForAtArea(LOOK_SOURCES, 0, 0, 49, 49, true).length;
creep.memory.sourcenum = Math.floor(Math.random() * (numEnergySources + 1));
}
if (creep.memory.working === true) {
if (creep.carry.energy === 0) {
creep.memory.working = false;
} else {
var structures = creep.pos.findInRange(FIND_STRUCTURES, 1, {
filter: (s) => {
return (
(s.hits < 25000 && s.structureType === STRUCTURE_RAMPART) ||
(
s.structureType !== STRUCTURE_RAMPART &&
s.structureType !== STRUCTURE_WALL && (s.hits < s.hitsMax)
)
);
}
});
if (structures.length !== 0) {
if (creep.repair(structures) === ERR_NOT_IN_RANGE) {
creep.moveTo(structures);
}
} else {
var structures = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: (s) => {
return (
(s.hits < 25000 && s.structureType === STRUCTURE_RAMPART) ||
(
s.structureType !== STRUCTURE_RAMPART &&
s.structureType !== STRUCTURE_WALL && ((s.hits < s.hitsMax) < 0.9)
)
);
}
});
if (structures) {
if (creep.repair(structures) === ERR_NOT_IN_RANGE) {
creep.moveTo(structures);
}
} else {
roleBuilder.run(creep);
}
}
}
} else {
if (creep.carry.energy === creep.carryCapacity) {
creep.memory.working = true;
}
var targets = creep.room.find(FIND_STRUCTURES, {
filter: (structure) => {
return (structure.structureType === STRUCTURE_CONTAINER ||
structure.structureType === STRUCTURE_STORAGE) &&
structure.store.energy > creep.carryCapacity;
}
});
if (targets.length > 0) {
var source = targets[0];
if (creep.withdraw(source, RESOURCE_ENERGY, creep.carryCapacity - creep.carry) === ERR_NOT_IN_RANGE) {
creep.moveTo(source);
}
} else {
var source = creep.room.find(FIND_SOURCES)[creep.memory.sourcenum];
if (creep.harvest(source) === ERR_NOT_IN_RANGE) {
creep.moveTo(source);
}
}
}
}
}
};