-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.upgrader.js
79 lines (69 loc) · 2.7 KB
/
role.upgrader.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
var utilsRoom = require('utils.room');
var roleUpgrader = {
/** @param {Creep} creep **/
run: function(creep) {
if(creep.memory.source == '' || creep.memory.source == null) {
console.log("Performing upgrader load-balance.")
roleUpgrader.loadBalance(creep.room);
}
if(creep.memory.upgrading && creep.carry.energy == 0) {
creep.memory.upgrading = false;
}
if(!creep.memory.upgrading && creep.carry.energy == creep.carryCapacity) {
creep.memory.upgrading = true;
}
if(creep.memory.upgrading) {
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.controller);
}
}
else {
var source = Game.getObjectById(creep.memory.source);
if(source != null && source.store[RESOURCE_ENERGY] > (0)) {
if(source.transfer(creep, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(source);
}
}
else {
//Move to staging flag
//FIND_FLAGS
var staging = creep.room.find(FIND_FLAGS, {
filter: (f) => {
return (f.color == COLOR_GREEN)
}
});
if(staging.length) {
creep.moveTo(staging[0]);
}
}
}
},
loadBalance: function(room) {
var upgraders = _.filter(Game.creeps, function (creep) { return creep.memory.role == "upgrader" && creep.room == room; });
// var sources = utilsRoom.getAllSources(room);
// var sinks = [];
var also = room.controller.pos.findClosestByRange(FIND_STRUCTURES, {
filter: (o) => {
return (o instanceof StructureContainer ||
o instanceof StructureStorage);
}
});
for(var i = 0; i < upgraders.length; i++) {
upgraders[i].memory.source = also.id;
}
//Generate sinks mapping
// for (var src in sources) {
// sinks[sources[src]] = sources[src].pos.findClosestByRange(FIND_STRUCTURES, {
// filter: (o) => {
// return (o instanceof StructureContainer ||
// o instanceof StructureStorage);
// }
// });
// }
// for(var i = 0; i < upgraders.length; i++) {
// var source = sources[i % sources.length];
// upgraders[i].memory.source = sinks[source].id;
// }
}
};
module.exports = roleUpgrader;