-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrole.upgrader.js
More file actions
121 lines (112 loc) · 3.37 KB
/
role.upgrader.js
File metadata and controls
121 lines (112 loc) · 3.37 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var utilities = require('utilities');
/**
* Make creep upgrade Controller.
*/
Creep.prototype.performUpgrade = function() {
// Upgrade controller.
if (this.pos.getRangeTo(this.room.controller) > 3) {
this.moveTo(this.room.controller);
}
else {
this.upgradeController(this.room.controller);
}
// Keep getting energy from link or container to ideally never stop upgrading
// Only real upgrader do this. Otherwise other primary roles would never stop upgrading
if (this.memory.role == 'upgrader' && _.sum(this.carry) < this.carryCapacity) {
var withdrawn = false;
if (this.room.memory.controllerLink) {
var controllerLink = Game.getObjectById(this.room.memory.controllerLink);
if (controllerLink.energy > 50 && this.pos.getRangeTo(controllerLink) <= 1) {
if (this.withdraw(controllerLink, RESOURCE_ENERGY) == OK) {
withdrawn = true;
}
}
}
if (!withdrawn && this.room.memory.controllerContainer) {
var controllerContainer = Game.getObjectById(this.room.memory.controllerContainer);
if (controllerContainer.store[RESOURCE_ENERGY] > 50 && this.pos.getRangeTo(controllerContainer) <= 1) {
if (this.withdraw(controllerContainer, RESOURCE_ENERGY) == OK) {
withdrawn = true;
}
}
}
}
return true;
};
/**
* Makes the creep gather energy as an upgrader.
*/
Creep.prototype.performGetUpgraderEnergy = function () {
var creep = this;
// Ideally, get energy from a link or container close to the controller
if (creep.room.memory.controllerLink) {
var target = Game.getObjectById(creep.room.memory.controllerLink);
if (target && target.energy > 50) {
if (creep.pos.getRangeTo(target) > 1) {
creep.moveTo(target);
}
else {
creep.withdraw(target, RESOURCE_ENERGY);
}
return true;
}
}
if (creep.room.memory.controllerContainer) {
var target = Game.getObjectById(creep.room.memory.controllerContainer);
if (target && target.store[RESOURCE_ENERGY] > 50) {
if (creep.pos.getRangeTo(target) > 1) {
creep.moveTo(target);
}
else {
creep.withdraw(target, RESOURCE_ENERGY);
}
return true;
}
}
// Could also try to get energy from another nearby container.
var otherContainers = creep.pos.findInRange(FIND_STRUCTURES, 3, {
filter: (structure) => structure.structureType == STRUCTURE_CONTAINER && structure.store.energy > 0 && structure.id != creep.room.memory.controllerContainer
});
if (otherContainers && otherContainers.length > 0) {
if (creep.pos.getRangeTo(otherContainers[0]) > 1) {
creep.moveTo(otherContainers[0]);
}
else {
creep.withdraw(otherContainers[0], RESOURCE_ENERGY);
}
return true;
}
// Otherwise get energy from anywhere.
if (creep.performGetEnergy()) {
return true;
}
else if (creep.carry.energy > 0) {
creep.setUpgraderState(true);
}
return false;
}
/**
* Set creep into or out of upgrading state.
*/
Creep.prototype.setUpgraderState = function(upgrading) {
this.memory.upgrading = upgrading;
delete this.memory.sourceTarget;
delete this.memory.tempRole;
}
/**
* Makes creep behave like an upgrader.
*/
Creep.prototype.runUpgraderLogic = function() {
if (this.memory.upgrading && this.carry.energy == 0) {
this.setUpgraderState(false);
}
if (!this.memory.upgrading && this.carry.energy == this.carryCapacity) {
this.setUpgraderState(true);
}
if (this.memory.upgrading) {
return this.performUpgrade();
}
else {
return this.performGetUpgraderEnergy();
}
}