-
Notifications
You must be signed in to change notification settings - Fork 0
/
role.gatherer.js
74 lines (66 loc) · 3.12 KB
/
role.gatherer.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
var libs = require('lib');
var roleGatherer = {
run: function(creep, targetClaims, energyClaims) {
if (creep.store[RESOURCE_ENERGY] == 0) {
energyClaims[creep.name] = libs.getEnergySource(creep, energyClaims);
if (targetClaims !== undefined) {
delete targetClaims[creep.name];
}
if (creep.memory.upgrading) {
creep.memory.upgrading = false;
}
var rc = creep.harvest(energyClaims[creep.name]);
if (rc == ERR_NOT_IN_RANGE || rc == ERR_NOT_ENOUGH_RESOURCES) {
creep.moveTo(energyClaims[creep.name], {visualizePathStyle: {stroke: '#ffaa00'}});
}
} else if (creep.store.getFreeCapacity() == 0) {
if (energyClaims !== undefined) {
delete energyClaims[creep.name];
}
var targets = libs.getFreeTargets(creep, targetClaims, 2);
if(targets.length > 0 && creep.memory.role != 'upgrader') {
var target = creep.pos.findClosestByPath(targets);
targetClaims[creep.name] = target['pos'];
if(creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(target, {visualizePathStyle: {stroke: '#ffffff'}});
} else {
creep.memory.upgrading = true;
}
} else {
if (targetClaims !== undefined) {
delete targetClaims[creep.name];
}
if(creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.controller, {visualizePathStyle: {stroke: '#ffffff'}});
} else {
creep.memory.upgrading = true;
}
}
} else {
if (creep.memory.upgrading) {
delete energyClaims[creep.name];
var targets = libs.getFreeTargets(creep, targetClaims, 2);
if(targets.length > 0 && creep.memory.role != 'upgrader') {
var target = creep.pos.findClosestByPath(targets);
targetClaims[creep.name] = target['pos'];
if(creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
creep.moveTo(target, {visualizePathStyle: {stroke: '#ffffff'}});
}
} else {
if (creep.upgradeController(creep.room.controller) == ERR_NOT_IN_RANGE) {
creep.moveTo(creep.room.controller);
}
}
} else {
energyClaims[creep.name] = libs.getEnergySource(creep, energyClaims);
if (energyClaims[creep.name]['energy'] == 0) {
creep.memory.upgrading = true;
delete energyClaims[creep.name];
} else if (creep.harvest(energyClaims[creep.name]) == ERR_NOT_IN_RANGE) {
creep.moveTo(energyClaims[creep.name], {visualizePathStyle: {stroke: '#ffaa00'}});
}
}
}
}
};
module.exports = roleGatherer;