-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.builder.js
78 lines (69 loc) · 3.31 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
70
71
72
73
74
75
76
77
78
var roleBuilder = {
run: function(creep) {
if (creep.memory.working && creep.store[RESOURCE_ENERGY] === 0) {
creep.memory.working = false;
creep.say('🔄 harvest');
}
if (!creep.memory.working && creep.store.getFreeCapacity() === 0) {
creep.memory.working = true;
creep.say('🚧 build');
}
if (creep.memory.working) {
// get Extensions requiring energy
const needEnergyExtensions = creep.room.find(FIND_MY_STRUCTURES, {
filter: (structure) => {
if (structure.structureType === STRUCTURE_EXTENSION || structure.structureType === STRUCTURE_TOWER) {
return structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0;
}
return false; // Exclude other structure types
}
});
//Provide energy to extensions
if (needEnergyExtensions.length > 0) {
const targetExtension = needEnergyExtensions[0];
if (creep.transfer(targetExtension, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
creep.moveTo(targetExtension, { visualizePathStyle: { stroke: '#ffffff' } });
}
}
else {
//get constrution sites
const constructionSites = creep.room.find(FIND_CONSTRUCTION_SITES, {
filter: (site) => {
return site.structureType === STRUCTURE_EXTENSION;
}
});
// Upgrade Controller if no construction sites available
if (constructionSites.length === 0) {
// Extensions are full and no construction sites, upgrade the controller
if (creep.upgradeController(creep.room.controller) === ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.controller, { visualizePathStyle: { stroke: '#ffffff' } });
}
}
//Provide energy to build construction sites
else {
const targetConstructionSite = constructionSites[0];
if (creep.build(targetConstructionSite) === ERR_NOT_IN_RANGE) {
creep.moveTo(targetConstructionSite, { visualizePathStyle: { stroke: '#ffffff' } });
}
}
}
}
// Collect energy
else {
const source = creep.pos.findClosestByPath(FIND_SOURCES_ACTIVE, {
filter: source => {
// Check if there are any hostile creeps or Source Keepers near the source
const nearbyHostiles = source.pos.findInRange(FIND_HOSTILE_CREEPS, 5);
const nearbySourceKeepers = source.pos.findInRange(FIND_HOSTILE_STRUCTURES, 5, {
filter: structure => structure.structureType === STRUCTURE_KEEPER_LAIR
});
return nearbyHostiles.length === 0 && nearbySourceKeepers.length === 0;
}
});
if(creep.harvest(source) === ERR_NOT_IN_RANGE){
creep.moveTo(source);
}
}
}
};
module.exports = roleBuilder;