-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrole.refill.js
349 lines (330 loc) · 12.1 KB
/
role.refill.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/* Refill drone */
module.exports.role = 'refill';
/* sType */
module.exports.sType = 'normal';
/* Spawn Roster */
module.exports.roster = {
1: 0,
2: 0,
3: 0,
4: 1,
5: 1,
6: 1,
7: 1,
8: 1,
}
/* Costs */
module.exports.cost = {
1 : 0,
2 : 0,
3 : 0,
4 : 500,
5 : 1000,
6 : 1000,
7 : 1000,
8 : 1000,
}
/* Body parts */
module.exports.body = {
1 : [],
2 : [],
3 : [],
4 : [
CARRY,CARRY,CARRY,CARRY,CARRY,
MOVE,MOVE,MOVE,MOVE,MOVE
],
5 : [
CARRY,CARRY,CARRY,CARRY,CARRY,
CARRY,CARRY,CARRY,CARRY,CARRY,
MOVE,MOVE,MOVE,MOVE,MOVE,
MOVE,MOVE,MOVE,MOVE,MOVE
],
6 : [
CARRY,CARRY,CARRY,CARRY,CARRY,
CARRY,CARRY,CARRY,CARRY,CARRY,
MOVE,MOVE,MOVE,MOVE,MOVE,
MOVE,MOVE,MOVE,MOVE,MOVE
],
7 : [
CARRY,CARRY,CARRY,CARRY,CARRY,
CARRY,CARRY,CARRY,CARRY,CARRY,
CARRY,CARRY,CARRY,CARRY,CARRY,
CARRY,CARRY,CARRY,CARRY,CARRY,
MOVE,MOVE,MOVE,MOVE,MOVE,
MOVE,MOVE,MOVE,MOVE,MOVE,
MOVE,MOVE,MOVE,MOVE,MOVE,
MOVE,MOVE,MOVE,MOVE,MOVE
],
8 : [
CARRY,CARRY,CARRY,CARRY,CARRY,
CARRY,CARRY,CARRY,CARRY,CARRY,
CARRY,CARRY,CARRY,CARRY,CARRY,
CARRY,CARRY,CARRY,CARRY,CARRY,
MOVE,MOVE,MOVE,MOVE,MOVE,
MOVE,MOVE,MOVE,MOVE,MOVE,
MOVE,MOVE,MOVE,MOVE,MOVE,
MOVE,MOVE,MOVE,MOVE,MOVE
],
}
module.exports.enabled = function (room, debug = false) {
const _room = Game.rooms[room];
if (_room.controller) {
if(_room.controller.level >= 4 && _room.storage) {
return true;
}
}
return false;
}
/**
* Harvester Role
*/
module.exports.run = function(creep) {
if (creep.isTired()) { return; }
var ticks = creep.ticksToLive;
if (ticks < 150 && !creep.memory.dying) {
creep.QueueReplacement();
creep.memory.dying = true;
}
// If it's dying force it into delivery mode
if (creep.memory.dying) {
creep.say(ticks);
if (_.sum(creep.carry) > (creep.carryCapacity/2) || ticks < 50) {
creep.memory.delivering = true;
} else {
creep.memory.delivering = false;
}
}
if (creep.room.name != creep.memory.roomName) {
delete creep.memory.energyPickup;
let pos = new RoomPosition(25,25,creep.memory.roomName);
creep.travelTo(pos);
creep.say('SEEK');
return;
}
// Is the creep dropping off and empty?
if (creep.memory.delivering && _.sum(creep.carry) === 0) {
creep.memory.delivering = false;
creep.say('GET');
}
// Is the creep not delivering and full?
if (!creep.memory.delivering && _.sum(creep.carry) === creep.carryCapacity) {
creep.memory.delivering = true;
creep.say('PUT');
}
// If we're not delivering, check if we can harvest, if not and we have half energy, go and deliver
if (!creep.memory.delivering) {
if (creep.getNearbyEnergy(true) === ERR_FULL) {
delete creep.memory.energyPickup;
creep.memory.delivering = true;
}
}
// Alright at this point if we're delivering it's time to move the Creep to a drop off
if (creep.memory.delivering) {
delete creep.memory.energyPickup;
// Let's try putting energy in the link
if (creep.carry.energy > 0 && creep.room.controller.level >= 5) {
var target = false;
// Find the link near the storage
if (creep.room.storage) {
// Check the storage cache
if (!creep.room.storage.memory.linkId) {
// we have storage, lets find the link nearby
var links = creep.room.find(FIND_MY_STRUCTURES, {
filter:(i) => i.structureType === STRUCTURE_LINK && i.pos.inRangeTo(creep.room.storage, 3)
});
// If we found a lin
if (links.length > 0) {
creep.room.storage.memory.linkId = links[0].id;
target = links[0].id;
if (target) {
Game.getObjectById(links[0].id).memory.linkType = 'storage';
}
}
}
if (creep.room.storage.memory.linkId) {
target = Game.getObjectById(creep.room.storage.memory.linkId);
// in case this gets switched
if (!target) {
delete creep.room.storage.memory.linkId;
}
}
// If we found the link, lets fill it
if (target && target.energy < target.energyCapacity) {
// Attempt transfer, unless out of range
if(creep.transfer(target, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
// Let's go to the target
creep.travelTo(target);
// Say because move
creep.say('>>');
} else {
// Succesful drop off
creep.say('V');
}
return;
}
}
}
// Do we have energy?
if (creep.carry.energy > 0) {
// We do, try to find a spawn or extension to fill
var target = creep.pos.findClosestByRange(FIND_STRUCTURES, {
filter: (structure) => {
return (
structure.structureType === STRUCTURE_EXTENSION ||
structure.structureType === STRUCTURE_SPAWN
) && structure.energy < structure.energyCapacity;
}
});
}
// Did we find a spawn or extension?
if (target) {
// Yep, so reset idle
creep.memory.idle = 0;
// Loop through our carry
for(var resourceType in creep.carry) {
// Only try to delivery energy to spawn and exention
if (resourceType === RESOURCE_ENERGY) {
// If we're not in range
if (creep.transfer(target, resourceType) === ERR_NOT_IN_RANGE) {
// Move to it
creep.travelTo(target);
// Say because move
creep.say('>>');
} else {
// Successful drop off
creep.say('V');
}
}
}
// We're done, next
return;
}
// We didn't find a target yet, do we still have energy to use?
if (creep.carry.energy > 0) {
var target = false;
// First find towers
targets = creep.room.find(FIND_MY_STRUCTURES, {
filter : (i) => i.structureType === STRUCTURE_TOWER && i.energy < i.energyCapacity
});
// No targets? try labs
if (targets.length == 0) {
targets = creep.room.find(FIND_MY_STRUCTURES, {
filter : (i) => i.structureType === STRUCTURE_LAB && i.energy < i.energyCapacity
});
}
if (targets.length > 0) {
target = _.min(targets, (t) => { return t.energy; });
}
// So did we find one?
if (target) {
// Attempt transfer, unless out of range
if(creep.transfer(target, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
// Let's go to the target
creep.travelTo(target);
// Say because move
creep.say('>>');
} else {
// Succesful drop off
creep.say('V');
}
return;
}
}
// Fallback for no targets to fill
if (!target) {
// Okay time for some fancy maths
var terminal = creep.room.terminal;
var storage = creep.room.storage;
// If we have both storage and terminal
if (storage && terminal) {
if (creep.room.memory.prioritise) {
if (creep.room.memory.prioritise === 'terminal') {
if (_.sum(terminal.store) < terminal.storeCapacity) {
var target = terminal;
} else {
var target = storage;
}
} else if (creep.room.memory.prioritise === 'storage') {
if (_.sum(storage.store) < storage.storeCapacity) {
var target = storage;
} else {
var target = terminal;
}
} else {
if (creep.carry.energy > 0) {
var target = storage;
} else {
var target = terminal;
}
}
} else {
// Do we have energy?
if (creep.carry.energy > 0) {
// Lets just assume these exist and get the percentage filled
// We need to know the relative filled of each of these, so [filled / (capacity/100)] should give us the percentage?
var terminalP = (_.sum(terminal.store) / (terminal.storeCapacity / 100));
var storageP = (_.sum(storage.store) / (storage.storeCapacity / 100));
// If the fill percentage is less or equal
if (terminalP <= storageP) {
var target = terminal;
}
// if it's the other way around use storage
if (storageP < terminalP) {
var target = storage;
}
} else {
// Prioritise the terminal for non-energy
var target = terminal;
// If we don't have one
if (!target || _.sum(terminal.store) === terminal.storeCapacity ) {
// try storage
var target = storage;
}
}
}
} else if (storage) { // Room storage?
var target = storage;
} else {
// We've no targets... now what?
}
}
// Did we find a target?
if (target) {
// reset idle
creep.memory.idle = 0;
// Loop through our resources
for(var resourceType in creep.carry) {
// Attempt to transfer them
if (creep.carry[resourceType] > 0) {
if (creep.transfer(target, resourceType) === ERR_NOT_IN_RANGE) {
creep.travelTo(target);
// Say because move
creep.say('>>');
// if we failed, we don't need to keep trying
break;
} else {
creep.say('V');
}
}
}
return;
} else {
creep.memory.idle++;
creep.say('idle: ' + creep.memory.idle);
if (creep.memory.idle >= 100) {
// Are we in our home room?
if (creep.room.name !== creep.memory.roomName) {
// lets go home
var spawns = Game.rooms[creep.memory.roomName].find(FIND_STRUCTURES, {
filter: (i) => i.structureType === STRUCTURE_SPAWN
});
var spawn = spawns[0];
if (spawn) {
creep.travelTo(spawn);
creep.say(global.sayMove);
}
}
}
}
}
}