-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrole.harvester.js
327 lines (309 loc) · 11.9 KB
/
role.harvester.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
// Harvester Drone
module.exports.role = 'harvester';
// Core Type
module.exports.sType = 'normal';
// Spawn count roster
module.exports.roster = {
1: 2,
2: 2,
3: 2,
4: 2,
5: 2,
6: 2,
7: 2,
8: 2,
};
// Human Readable costs (likely to be pruned)
module.exports.cost = {
1 : 300,
2 : 400,
3 : 500,
4 : 1300,
5 : 1800,
6 : 1800,
7 : 1800,
8 : 1800,
};
// Body Part array for each RCL
module.exports.body = {
1 : [
WORK,
CARRY,CARRY,
MOVE,MOVE
],
2 : [
WORK,
CARRY,CARRY,CARRY,
MOVE,MOVE,MOVE,MOVE
],
3 : [
CARRY,CARRY,CARRY,CARRY,CARRY,
MOVE,MOVE,MOVE,MOVE,MOVE
],
4 : [
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
],
5 : [
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,
],
6 : [
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,
],
7 : [
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,
],
8 : [
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,
],
};
// Multiplier for spawn use
module.exports.multiplier = 2;
// Enabled method
module.exports.enabled = function (room, debug = false) {
const _room = Game.rooms[room];
if (_room.controller && _room.memory.minersNeeded && _room.memory.minersNeeded > 0) {
var list = _.filter(Game.creeps, (creep) => creep.memory.role === this.role && creep.memory.roomName === room && !creep.memory.dying);
if (list.length < _room.memory.minersNeeded*this.multiplier) {
return true;
}
}
return false;
}
// Main run method
module.exports.run = function(creep) {
if (creep.isTired()) { return; }
if (!creep.memory.dying && creep.ticksToLive < 100) { creep.memory.dying = true; }
// If it's dying force it into delivery mode
if (creep.memory.dying) {
creep.say(creep.ticksToLive);
if (_.sum(creep.carry) > (creep.carryCapacity/2) || creep.ticksToLive < 50) {
creep.memory.delivering = true;
} else {
creep.memory.delivering = false;
}
}
// Are we not in our home room for some stupid reason
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) {
// Any minerals to pickup?
// Always pickup none
if (creep.getNearbyEnergy() === 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;
var fillSpawns = false;
if (creep.room.energyAvailable < creep.room.energyCapacityAvailable*0.75) {
fillSpawns = true;
}
// only refill spawns and other things if room level below 4 after 4 we just fill storage
// after 5 we fill storage and terminal
// unless emergency, then we fill spawns too
if (fillSpawns || creep.room.controller.level < 4 || creep.room.memory.emergency || !creep.room.storage) {
var target = false;
// Do we have energy?
if (creep.carry.energy > 0) {
// We do, try to find a spawn or extension to fill
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(let 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 tower = false;
// First find towers with less than 400 energy
tower = creep.pos.findClosestByRange(FIND_MY_STRUCTURES, {
filter : (i) => i.structureType === STRUCTURE_TOWER && i.energy < 400
});
// If we didn't find any get them with less than 800
if (!tower) {
tower = creep.pos.findClosestByRange(FIND_MY_STRUCTURES, {
filter : (i) => i.structureType === STRUCTURE_TOWER && i.energy < 800
});
}
// Okay all above 800, get any now
if (!tower) {
tower = creep.pos.findClosestByRange(FIND_MY_STRUCTURES, {
filter : (i) => i.structureType === STRUCTURE_TOWER && i.energy < i.energyCapacity
});
}
// If towers are full, can we dump it into a lab?
if (!tower) {
tower = creep.pos.findClosestByRange(FIND_MY_STRUCTURES, {
filter : (i) => i.structureType === STRUCTURE_LAB && i.energy < i.energyCapacity
});
}
// So did we find one?
if (tower) {
// Attempt transfer, unless out of range
if(creep.transfer(tower, RESOURCE_ENERGY) === ERR_NOT_IN_RANGE) {
// Let's go to the tower
creep.travelTo(tower);
// Say because move
creep.say('>>');
} else {
// Succesful drop off
creep.say('V');
}
return;
}
}
}
// 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 >= 10) {
// 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);
}
//}
}
}
}
}