-
Notifications
You must be signed in to change notification settings - Fork 0
/
fill.js
312 lines (269 loc) · 12 KB
/
fill.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
var marketMgr = require("mgr.market");
var roleUpgrader = require("role.upgrader");
var roleRepairer = require("role.repairer");
var roleFiller = require("role.filler")
var fill = {
withdrawAndDropTerm: function (creep) {
let resourceType = creep.memory.mineral;
let amount = creep.store.getFreeCapacity();
var terminal = creep.room.terminal;
//console.log("Terminal " + terminal)
while (terminal.store.getFreeCapacity() < 5000 && terminal.store.getUsedCapacity(RESOURCE_ENERGY) < 5000) {
if (terminal.store.getUsedCapacity(resourceType) >= amount) {
switch (creep.withdraw(terminal, resourceType, amount)) {
case 0:
case -8:
console.log(amount + ' ' + resourceType + ' withdrawn from terminal.');
var dropResult = creep.drop(resourceType, creep.store.getUsedCapacity());
switch (dropResult) {
case 0:
console.log(amount + ' ' + resourceType + ' dropped at terminal.');
break;
case -1:
break;
case -4:
break;
case -6:
console.log("extra drop call");
break;
case -10:
break;
default:
break;
}
break;
case -1:
console.log('Error withdrawing ' + resourceType + ': ' + result);
break;
case -2:
console.log('Error withdrawing ' + resourceType + ': ' + result);
break;
case -3:
console.log('Error withdrawing ' + resourceType + ': ' + result);
break;
case -4:
console.log('Error withdrawing ' + resourceType + ': ' + result);
break;
case -5:
console.log('Error withdrawing ' + resourceType + ': ' + result);
break;
case -6:
console.log('Error withdrawing ' + resourceType + ': ' + result);
break;
case -7:
console.log('Error withdrawing ' + resourceType + ': ' + result);
break;
case -9:
creep.moveTo(terminal, { visualizePathStyle: { stroke: '#FFC0CB' } });
break;
case -10:
console.log('Error withdrawing ' + resourceType + ': ' + result);
break;
default:
console.log("failed to withdraw from terminal in " + creep.room.name);
}
} else {
console.log('Not enough ' + resourceType + ' in terminal.');
break;
}
}
},
fillTerminals: function (creep) {
//Set Terminal
var terminal = creep.room.terminal;
// transfer all resources
for(const resourceType in creep.store) {
switch (creep.transfer(terminal, resourceType)) {
case 0: //successful
//console.log("successful")
break;
case -6:
//no mineral
console.log("logic error for " + creep.memory.role + " in transfering to terminal")
break;
case -7:
//console.log("No Terminal");
this.fillAnything(creep);
//creep.memory.role = "filler"
break;
case -8:
console.log("Terminal " + terminal.id + " is full of energy");
marketMgr.manageRoomRes(creep.room);
break;
case -9: //not in range
creep.moveTo(terminal, { visualizePathStyle: { stroke: '#FFC0CB' } });
break;
case -10:
creep.memory.mineral = RESOURCE_ENERGY;
console.log("Fix resource in fill.fillterminals()");
console.log(creep.memory.role + "-10")
break;
default:
console.log(creep.transfer(terminal, creep.memory.mineral));
console.log("unknown Error in terminal transfer, investigate")
break;
}
}
},
fillAnything: function (creep) {
var room = creep.room;
var roomName = room.name;
/*var targets = creep.room.find(FIND_STRUCTURES).filter(
structure => [STRUCTURE_CONTAINER, STRUCTURE_STORAGE].indexOf(
structure.structureType) !== -1).filter(
structure => structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0);*/
var targets = [];
for (var i in Memory[roomName].storage.containers) {
targets.push(Game.getObjectById(Memory[roomName].storage.containers[i].id));
}
for (var i in Memory[roomName].storage.storages) {
targets.push(Game.getObjectById(Memory[roomName].storage.storages[i].id));
}
//console.log(roomName + " targets " + targets);
if (targets.length == 0) {
console.log("Out of storage")
console.log(creep.room + " " + targets)
// console.log(" team 1 out")
targets = creep.room.find(FIND_STRUCTURES).filter(
structure => [STRUCTURE_CONTAINER, STRUCTURE_STORAGE].indexOf(
structure.structureType) !== -1).filter(
structure => structure.store.energy < structure.store.getCapacity());
} else {
//console.log(creep.room + " " + targets)
//current container is chosen manually from structures less than 2 spots away, need to find closest container.
//console.log(targets[0]);
if (!creep.memory.mineral) {
creep.memory.mineral = RESOURCE_ENERGY;
}
var status = creep.transfer(targets[0], creep.memory.mineral);
switch (status) {
case -9:
creep.moveTo(targets[targets.length - 1]);
break;
case 0:
break;
case -6:
//doesnt have res
break;
case -7:
//when the room level is too low for the storage
console.log("Room level too low for storage")
break;
case -8://full
console.log("Room Full");
break;
default:
console.log(status + " Error filling container for " + creep.name)
}
if (creep.memory.team === 0) {
} else if (creep.memory.team === 1) {
}
}
},
fillContainers: function (creep) {
var room = creep.room;
var roomName = room.name;
/*var targets = creep.room.find(FIND_STRUCTURES).filter(
structure => [STRUCTURE_CONTAINER, STRUCTURE_STORAGE].indexOf(
structure.structureType) !== -1).filter(
structure => structure.store.getFreeCapacity(RESOURCE_ENERGY) > 0);*/
var targets = [];
for (var i in Memory[roomName].storage.containers) {
targets.push(Game.getObjectById(Memory[roomName].storage.containers[i].id));
}
for (var i in Memory[roomName].storage.storages) {
targets.push(Game.getObjectById(Memory[roomName].storage.storages[i].id));
}
//console.log(roomName + " targets " + targets);
if (targets.length == 0) {
//console.log("Out of storage")
//console.log(creep.room + " " + targets)
if (!Memory[roomName].towers) {
roleFiller.run(creep);
} else {
console.log("decide what to do instead of be a guard")
//roleGuard.run(creep);
}
} else {
//find containers and storage with storage open
/*var targets = creep.room.find(FIND_STRUCTURES).filter(
structure => [STRUCTURE_CONTAINER, STRUCTURE_STORAGE].indexOf(
structure.structureType) !== -1).filter(
structure => structure.store.energy < structure.store.getCapacity()).filter(
structure => creep.pos.getRangeTo(structure) < 10
);*/
//console.log(creep.room + " " + targets)
if (targets.length == 0) {
// console.log(" team 1 out")
targets = creep.room.find(FIND_STRUCTURES).filter(
structure => [STRUCTURE_CONTAINER, STRUCTURE_STORAGE].indexOf(
structure.structureType) !== -1).filter(
structure => structure.store.energy < structure.store.getCapacity());
}
//current container is chosen manually from structures less than 2 spots away, need to find closest container.
//console.log(targets[0]);
var status = creep.transfer(targets[0], RESOURCE_ENERGY);
switch (status) {
case -9:
creep.moveTo(targets[targets.length - 1]);
break;
case 0:
break;
case -6:
//doesnt have res
break;
case -7:
//when the room level is too low for the storage
roleUpgrader.run(creep);
break;
case -8://full
roleRepairer.run(creep);
break;
default:
console.log(status + " Error filling container for " + creep.name)
this.fillAnything(creep);
}
if (creep.memory.team === 0) {
} else if (creep.memory.team === 1) {
}
}
},
fillLinks: function (creep) {
var room = creep.room;
var roomName = room.name;
var upgradeLink = Game.getObjectById(Memory[roomName].links.upgradeLink);
var storageLink = Game.getObjectById(Memory[roomName].links.storageLink);
//creep must already have a link defined in memory
if (creep.memory.link == null) {
console.log(creep.name + " needs a link");
//creep.memory.role = "builder"
} else {
var link = Game.getObjectById(creep.memory.link);
// console.log(Game.getObjectById(creep.memory.link));
var status = creep.transfer(link, RESOURCE_ENERGY)
switch (status) {
case 0:
//successful
break;
case -6:
//creep does not have the materials to do this
break;
case -9:
//not in range
creep.moveTo(link);
break;
case -7:
console.log("Invalid Link reference " + creep.memory.link);
case -8:
//full link
this.fillContainers(creep)
break;
default:
//console.log(status);
console.log("Unknown Error in Fill Links: " + status);
}
//console.log(links);
}
}
};
module.exports = fill;