-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrole.collector.js
137 lines (135 loc) · 7.88 KB
/
role.collector.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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
Creep.prototype.roleCollector = function() {
// check for picked up minerals
if (this.memory.statusHarvesting == undefined || this.memory.statusHarvesting == false) {
var container;
if (this.memory.role == "harvester" || this.memory.role == "energyTransporter" || this.memory.role == "distributor" || this.memory.role == "scientist") {
// find closest container with energy
if (this.room.energyCapacityAvailable > this.room.energyAvailable) {
if (this.room.memory.terminalTransfer == undefined && this.checkTerminalLimits(RESOURCE_ENERGY).amount > 0) {
//spawn not full, terminal transfer ongoing -> find source, container or storage if available
if (this.memory.role == "harvester") {
container = this.findResource(RESOURCE_ENERGY, FIND_SOURCES, STRUCTURE_LINK, STRUCTURE_CONTAINER, STRUCTURE_STORAGE, STRUCTURE_TERMINAL);
}
if (this.memory.role == "energyTransporter" || this.memory.role == "distributor" || this.memory.role == "scientist") {
container = this.findResource(RESOURCE_ENERGY, STRUCTURE_LINK, STRUCTURE_CONTAINER, STRUCTURE_STORAGE, STRUCTURE_TERMINAL);
}
}
else {
//spawn not full, no terminal transfer ongoing -> find source, container, terminal or storage if available
if (this.memory.role == "harvester") {
container = this.findResource(RESOURCE_ENERGY, FIND_SOURCES, STRUCTURE_LINK, STRUCTURE_CONTAINER, STRUCTURE_STORAGE);
}
if (this.memory.role == "energyTransporter" || this.memory.role == "distributor" || this.memory.role == "scientist") {
container = this.findResource(RESOURCE_ENERGY, STRUCTURE_LINK, STRUCTURE_CONTAINER, STRUCTURE_STORAGE);
}
}
}
else if (this.room.storage != undefined && this.room.storage.owner.username == playerUsername && this.room.storage.storeCapacity - _.sum(this.room.storage.store > 0)) {
if (this.room.memory.terminalTransfer == undefined && this.checkTerminalLimits(RESOURCE_ENERGY).amount > 0) {
//spawn full and storage with space exists or towers need energy
if (this.memory.role == "harvester") {
container = this.findResource(RESOURCE_ENERGY, FIND_SOURCES, STRUCTURE_LINK, STRUCTURE_CONTAINER, STRUCTURE_TERMINAL);
}
if (this.memory.role == "energyTransporter" || this.memory.role == "distributor" || this.memory.role == "scientist") {
container = this.findResource(RESOURCE_ENERGY, STRUCTURE_LINK, STRUCTURE_CONTAINER, STRUCTURE_TERMINAL);
}
}
else {
//spawn full and storage with space exists or towers need energy
if (this.memory.role == "harvester") {
container = this.findResource(RESOURCE_ENERGY, FIND_SOURCES, STRUCTURE_LINK, STRUCTURE_CONTAINER);
}
if (this.memory.role == "energyTransporter" || this.memory.role == "distributor" || this.memory.role == "scientist") {
container = this.findResource(RESOURCE_ENERGY, STRUCTURE_LINK, STRUCTURE_CONTAINER);
}
}
}
else {
if (this.room.memory.terminalTransfer == undefined && this.checkTerminalLimits(RESOURCE_ENERGY).amount > 0) {
if (this.memory.role == "harvester") {
container = this.findResource(RESOURCE_ENERGY, FIND_SOURCES, STRUCTURE_LINK, STRUCTURE_TERMINAL);
}
if (this.memory.role == "energyTransporter" || this.memory.role == "distributor" || this.memory.role == "scientist") {
container = this.findResource(RESOURCE_ENERGY, STRUCTURE_LINK, STRUCTURE_TERMINAL);
}
}
else {
if (this.memory.role == "harvester") {
container = this.findResource(RESOURCE_ENERGY, FIND_SOURCES, STRUCTURE_LINK);
}
if (this.memory.role == "energyTransporter" || this.memory.role == "distributor" || this.memory.role == "scientist") {
container = this.findResource(RESOURCE_ENERGY, STRUCTURE_LINK);
}
}
}
if (container == undefined) {
//Nothing to do
let mineralsContainers = this.room.find(FIND_STRUCTURES, {filter: (s) => s.structureType == STRUCTURE_CONTAINER && (_.sum(s.store) > s.store[RESOURCE_ENERGY] || (_.sum(s.store) > 0 && s.store[RESOURCE_ENERGY == 0]))});
if (mineralsContainers.length == 0) {
this.memory.sleep = 5;
return (-1);
}
else {
//get minerals from container
if (this.memory.tidyFull == undefined && _.sum(this.carry) < this.carryCapacity && this.room.storage != undefined && this.room.storage.owner.username == playerUsername) {
//creep not full
for (let e in mineralsContainers[0].store){
if (e != "energy" && this.withdraw(mineralsContainers[0],e) == ERR_NOT_IN_RANGE) {
this.moveTo(mineralsContainers[0],{reusePath: moveReusePath()});
}
}
}
else if (this.room.storage != undefined && this.room.storage.owner.username == playerUsername){
//creep full
this.memory.tidyFull = true;
this.storeAllBut();
if (_.sum(this.carry) == 0) {
delete this.memory.tidyFull;
}
}
}
}
else if (container.ticksToRegeneration == undefined && (container.energy == undefined || container.energy < 3000)) {
//container
if (this.withdraw(container, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
this.moveTo(container, {reusePath: moveReusePath()});
}
}
else {
//Source
if (this.harvest(container) == ERR_NOT_IN_RANGE) {
this.moveTo(container, {reusePath: moveReusePath()});
}
}
}
else {
//no room harvester role
// find closest source
if (this.room.terminal != undefined && this.room.memory.terminalTransfer == undefined && this.checkTerminalLimits(RESOURCE_ENERGY).amount > 0) {
container = this.findResource(RESOURCE_ENERGY, FIND_SOURCES, STRUCTURE_LINK, STRUCTURE_CONTAINER, STRUCTURE_STORAGE, STRUCTURE_TERMINAL);
}
else {
container = this.findResource(RESOURCE_ENERGY, FIND_SOURCES, STRUCTURE_LINK, STRUCTURE_CONTAINER, STRUCTURE_STORAGE);
}
if (container != undefined) {
let res = this.withdraw(container, RESOURCE_ENERGY);
if (res != OK && res != ERR_NOT_IN_RANGE) {
res = this.harvest(container)
}
if (res == ERR_NOT_IN_RANGE) {
this.moveTo(container, {reusePath: moveReusePath()});
}
}
else {
return -1;
}
}
}
else {
// Creep is harvesting, try to keep harvesting
if (this.harvest(Game.getObjectById(this.memory.statusHarvesting)) != OK) {
this.memory.statusHarvesting = false;
}
}
return OK;
};