-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.janitor.js
91 lines (84 loc) · 4.17 KB
/
role.janitor.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
const jobWorker = require('job.worker');
const roleJanitor = {
/** @param {Creep} creep **/
run: function (creep) {
if (creep.memory.reparing && creep.carry.energy === 0) {
creep.memory.reparing = false;
creep.say('🔄 harvest');
}
if (!creep.memory.reparing && creep.carry.energy === creep.carryCapacity) {
creep.memory.reparing = true;
creep.say('🛠 repairing');
}
if (creep.memory.reparing) {
creep.memory.sourceId = null;
let target = Game.getObjectById(creep.memory.targetId);
if (!target) {
let assignedTargets = [];
//console.log(builders);
for (let name in Game.creeps) {
if (creep.memory.role === 'janitor' && Game.creeps[name].memory.targetId) {
assignedTargets.push(Game.creeps[name].memory.targetId);
}
}
let closestDamagedStructure = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: (structure) => structure.hits < Math.min(structure.hitsMax, 50000) //(structure.hits / structure.hitsMax) <= 0.6
&& [STRUCTURE_ROAD, STRUCTURE_WALL].indexOf(structure.structureType) === -1
&& assignedTargets.indexOf(structure.id) === -1
});
if (!closestDamagedStructure) {
closestDamagedStructure = creep.pos.findClosestByPath(FIND_STRUCTURES, {
filter: (structure) => (structure.hits < Math.min(structure.hitsMax, 50000) && structure.structureType !== STRUCTURE_CONTAINER)
|| (structure.structureType === STRUCTURE_CONTAINER && structure.hits < structure.hitsMax) //(structure.hits / structure.hitsMax) <= 0.8
&& assignedTargets.indexOf(structure.id) === -1
});
}
//console.log ('assigned targets', assignedTargets);
/*target = creep.pos.findClosestByRange(FIND_MY_STRUCTURES, {
filter: (object) => {
let health = object.ticksToDecay !== undefined ? (object.hits / object.hitsMax) : 1;
//console.log('hits', object.hits, object.hitsMax, health);
console.log(object.structureType, health <= 0.6);
return assignedTargets.indexOf(object.id) === -1
&& health <= 0.6;
}
});*/
//console.log('closestDamagedStructure', closestDamagedStructure);
creep.memory.targetId = (closestDamagedStructure && closestDamagedStructure.id)
? closestDamagedStructure.id
: null;
//console.log('add new target with id', target.id);
}
if (target) {
if (creep.repair(target) === ERR_NOT_IN_RANGE) {
creep.moveTo(target, {visualizePathStyle: {stroke: '#ff2144'}});
} else {
creep.memory.targetId = null;
}
} else {
//creep.say('Nothing to fix, run 🚧 build');
//roleUpgrader.run(creep);
}
}
else {
//creep.memory.sourceId = null;
creep.memory.targetId = null;
jobWorker.jobCollectEnergy(creep);
if (creep.memory.sourceId === null) {
jobWorker.jobLootEnergy(creep);
}
/*let source = Game.getObjectById(creep.memory.sourceId);
if (!source) {
source = creep.pos.findClosestByPath(FIND_SOURCES);
if (!source || source.id) {
source = creep.pos.findClosestByRange(FIND_SOURCES);
}
creep.memory.sourceId = source.id;
}
if (creep.harvest(source) === ERR_NOT_IN_RANGE) {
creep.moveTo(source, {visualizePathStyle: {stroke: '#ffaa00'}});
}*/
}
}
};
module.exports = roleJanitor;