-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrole.upgrader.js
69 lines (62 loc) · 2.49 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
module.exports = {
// a function to run the logic for this role
/** @param {Creep} creep */
run: function(creep) {
// if creep is bringing energy to the controller but has no energy left
if (creep.memory.working == true && creep.carry.energy == 0) {
// switch state
creep.memory.working = false;
}
// if creep is harvesting energy but is full
else if (creep.memory.working == false && creep.carry.energy == creep.carryCapacity) {
// switch state
creep.memory.working = true;
}
// if creep is supposed to transfer energy to the controller
if (creep.memory.working == true) {
// instead of upgraderController we could also use:
// if (creep.transfer(creep.room.controller, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
// try to upgrade the controller
if (creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
// if not in range, move towards the controller
creep.travelTo(creep.room.controller);
}
}
// if creep is supposed to get energy
else {
var controller = creep.room.controller;
// find links by the controller
let controllerLink = controller.pos.findInRange(FIND_STRUCTURES, 3, {
filter: s => s.structureType == STRUCTURE_LINK
})[0];
// if a link is found and it has enough energy...
if (controllerLink != undefined &&
controllerLink.energy >= creep.carryCapacity) {
if (creep.withdraw(controllerLink, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
// move towards it
creep.travelTo(controllerLink);
}
}
// if there is no link or if the link doesn't have enough energy,
// look for a container by controller
if (controllerLink == undefined || controllerLink.energy < creep.carryCapacity) {
// find a container by the controller
let controllerContainer = controller.pos.findInRange(FIND_STRUCTURES, 3, {
filter: s => s.structureType == STRUCTURE_CONTAINER
})[0];
// if one is found and it had enough energy...
if (controllerContainer != undefined &&
controllerContainer.store[RESOURCE_ENERGY] >= creep.carryCapacity) {
if (creep.withdraw(controllerContainer, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
// move towards it
creep.travelTo(controllerContainer);
}
}
// if no link or no container is found...
else {
creep.getEnergy(true, true);
}
}
}
}
};