-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructure.energy.js
48 lines (46 loc) · 1.53 KB
/
structure.energy.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
Structure.prototype.needsEnergy = function() {
return (this.structureType == STRUCTURE_EXTENSION ||
this.structureType == STRUCTURE_SPAWN ||
this.structureType == STRUCTURE_TOWER)
&& this.energy < this.energyCapacity;
}
//XXX magic numbers
StructureLink.prototype.pullEnergy = function() {
if(this.energy > 400 || this.cooldown > 0) {
return;
}
let links = this.room.find(FIND_STRUCTURES, {filter: (structure) => structure.structureType == STRUCTURE_LINK});
for(let i = 0; i < links.length; i++) {
let link = links[i];
if(link.energy > 400) {
let result = link.transferEnergy(this, 400);
if(result == OK) {
return;
}
}
}
}
StructureLink.prototype.pushEnergy = function() {
if(this.cooldown > 0) {
return;
}
// console.log('>> pushing energy from', this, this.pos);
let targetId = this.room.memory.upgradeLink;
if(!targetId) {
console.log('>> Did not find upgrade link');
let links = this.room.find(FIND_STRUCTURES, {filter: (structure) => structure.structureType == STRUCTURE_LINK});
for(let i = 0; i < links.length; i++) {
let link = links[i];
if(link.energy < 600) {
let result = this.transferEnergy(link);
if(result == OK) {
return;
}
}
}
}
else {
let target = Game.getObjectById(targetId);
this.transferEnergy(target);
}
}