-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.builder.js
69 lines (61 loc) · 2.79 KB
/
role.builder.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
var libs = require('lib');
var roleBuilder = {
run: function(creep, energyClaims) {
var targets = creep.room.find(FIND_CONSTRUCTION_SITES);
if (targets.length == 0) {
creep.memory.role = 'gatherer';
}
if(creep.memory.building && creep.store[RESOURCE_ENERGY] == 0) {
energyClaims[creep.name] = libs.getEnergySource(creep, energyClaims);
creep.memory.building = false;
creep.say('🔄 harvest');
}
if(!creep.memory.building && creep.store.getFreeCapacity() == 0) {
delete energyClaims[creep.name];
creep.memory.building = true;
creep.say('🚧 build');
}
if(creep.memory.building) {
let wallTargets = creep.room.find(FIND_STRUCTURES, {
filter: object => object.hits < 50000 && object.structureType == STRUCTURE_WALL
});
let targets = creep.room.find(FIND_CONSTRUCTION_SITES);
let bestTargets = [];
if (wallTargets.length > 0) {
//console.log(JSON.stringify(wallTargets, null, 2));
let bestClosest = creep.pos.findClosestByPath(wallTargets);
if(creep.repair(bestClosest) == ERR_NOT_IN_RANGE) {
creep.moveTo(bestClosest);
}
} else if (targets.length) {
let cheapestTarget;
for (let site of targets) {
if (cheapestTarget === undefined || (site.progressTotal - site.progress) < (cheapestTarget.progressTotal - cheapestTarget.progress)) {
cheapestTarget = site;
}
}
for (let site of targets) {
if (site.progressTotal - site.progress <= cheapestTarget.progressTotal - cheapestTarget.progress) {
bestTargets.push(site);
}
}
let bestClosest = creep.pos.findClosestByPath(bestTargets);
if(creep.build(bestClosest) == ERR_NOT_IN_RANGE) {
creep.moveTo(bestClosest, {visualizePathStyle: {stroke: '#ffffff'}});
}
}
} else {
energyClaims[creep.name] = libs.getEnergySource(creep, energyClaims);
if (energyClaims[creep.name]['energy'] == 0 && creep.store[RESOURCE_ENERGY] != 0) {
creep.memory.building = true;
delete energyClaims[creep.name];
} else {
let rc = creep.harvest(energyClaims[creep.name]);
if (rc== ERR_NOT_IN_RANGE || rc == ERR_NOT_ENOUGH_RESOURCES) {
creep.moveTo(energyClaims[creep.name], {visualizePathStyle: {stroke: '#ffaa00'}});
}
}
}
}
};
module.exports = roleBuilder;