-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrole.extractor.js
284 lines (257 loc) · 9.78 KB
/
role.extractor.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
// Specialist Extractor Drone
module.exports.role = 'extractor';
// The core type
module.exports.sType = 'specialist';
// Possibly unused variable
module.exports.roomRequirement = 'extractorsNeeded';
// Spawn roster per RCL
module.exports.roster = {
1: 0,
2: 0,
3: 0,
4: 0,
5: 0,
6: 1,
7: 1,
8: 1,
};
// Human Readable costs (soon to be removed)
module.exports.cost = {
1 : 0,
2 : 0,
3 : 0,
4 : 0,
5 : 0,
6 : 2300,
7 : 4750,
8 : 4750,
};
// Room RCL based body parts
module.exports.body = {
1 : [], 2 : [], 3 : [], 4 : [], 5 : [],
6 : [
WORK,WORK,WORK,WORK,WORK,
WORK,WORK,WORK,WORK,WORK,
WORK,WORK,WORK,WORK,WORK,
WORK,WORK,WORK,WORK,WORK,
MOVE,MOVE,MOVE,MOVE,MOVE,MOVE,
],
7 : [
WORK,WORK,WORK,WORK,WORK,
WORK,WORK,WORK,WORK,WORK,
WORK,WORK,WORK,WORK,WORK,
WORK,WORK,WORK,WORK,WORK,
WORK,WORK,WORK,WORK,WORK,
WORK,WORK,WORK,WORK,WORK,
WORK,WORK,WORK,WORK,WORK,
WORK,WORK,WORK,WORK,WORK,
WORK,WORK,WORK,WORK,WORK,
MOVE,MOVE,MOVE,MOVE,MOVE,
],
8 : [
WORK,WORK,WORK,WORK,WORK,
WORK,WORK,WORK,WORK,WORK,
WORK,WORK,WORK,WORK,WORK,
WORK,WORK,WORK,WORK,WORK,
WORK,WORK,WORK,WORK,WORK,
WORK,WORK,WORK,WORK,WORK,
WORK,WORK,WORK,WORK,WORK,
WORK,WORK,WORK,WORK,WORK,
WORK,WORK,WORK,WORK,WORK,
MOVE,MOVE,MOVE,MOVE,MOVE,
],
};
// Set a time for this creep to 'expire' at
module.exports.expiry = 200;
// Enabled logic goes in here
module.exports.enabled = function (room, debug = false) {
// define the room
var theRoom = Game.rooms[room];
// Find the mineral site in the room
var mineral = theRoom.find(FIND_MINERALS);
// does it have minerals?
if (theRoom.controller && theRoom.controller.level >= 6 && mineral[0].mineralAmount > 0) {
let extractors = theRoom.find(FIND_STRUCTURES, {
filter: (s) => {
return (s.structureType === STRUCTURE_EXTRACTOR)
}
});
if (extractors.length > 0) { return true; }
}
// This should be disabled
return false;
}
// Main run method for this role
module.exports.run = function (creep, debug = false) {
if (creep.isTired()) { return; }
// Okay, health check
if (!creep.memory.dying && creep.ticksToLive <= 200) {
if (debug) { console.log('Creep[' + creep.name + '] Extractor Dying Making sure we spawn a new one'); }
// set dying to true and set the sourceId to null in room memory
creep.memory.dying = true;
var extractorId = creep.memory.assignedSExtractor;
creep.room.memory.assignedExtractors[extractorId] = null;
}
// Alright if it's dying, output the timer
if (creep.memory.dying) {
if (debug) { console.log('Creep[' + creep.name + '] Extractor Dying, ticking down'); }
creep.say(creep.ticksToLive);
// If it's less than 10 ticks, drop what we have
if (creep.ticksToLive < 10) {
if (debug) { console.log('Creep[' + creep.name + '] Extractor about to die'); }
creep.say('!!' + creep.ticksToLive + '!!');
}
}
// Only do this if we don't have an assigned Source
if (!creep.memory.assignedExtractor) {
this.setup();
if (debug) { console.log('Creep[' + creep.name + '] Extractor without assigned Source, assigning'); }
// Okay lets get the room memory for assigned sources
var sourceId = false;
var extractors = creep.room.find(FIND_MINERALS);
var assigned = creep.room.memory.assignedExtractors;
// Can't loop through extractors to just to an i = loop to get them
for (var i=0;i<=extractors.length-1;i++) {
var extractor = extractors[i];
if (assigned[extractor.id] == null) {
extractorId = extractor.id;
creep.room.memory.assignedExtractors[extractorId] = creep.id;
creep.memory.assignedExtractor = extractorId;
// Make sure we break out so we don't break the next extractor too
break;
}
}
// Do we have a extractorId?
if (extractorId === false) {
if (debug) { console.log('Creep[' + creep.name + '] Extractor cannot find extractor!!'); }
if (!creep.room.memory.extractorReset) {
creep.room.memory.extractorReset = true;
}
Game.notify('Extractor Creep unable to assign a extractor');
}
}
// Are we full?
if (_.sum(creep.carry) === creep.carryCapacity) {
if (debug) { console.log('Creep[' + creep.name + '] Extractor full, dropping!'); }
creep.memory.dropping = true;
} else {
creep.memory.dropping = false;
}
// Are we dropping?
if (creep.memory.dropping) {
// // This may need to change, depends if the drop costs fatigue or if dropping goes into a container
// console.log(creep.drop(RESOURCE_CATALYST));
creep.memory.dropping = false;
// creep.say('V');
// DANGER we just drop resources here... This could leave a pile of resources if our transfer dudes aren't keeping up
// // This is just here incase it's needed fast (saves me writing it in a panic)
// var container = creep.pos.findClosestByRange(FIND_STRUCTURES, {
// filter: (i) => {
// return (i.structureType == STRUCTURE_CONTAINER) && (_.sum(i.store) < i.storeCapacity)
// }
// });
// if (container) {
// try {
// for (var resource in creep.carry) {
// creep.transfer(container, resource);
// }
// } catch (ERR_NOT_IN_RANGE) {
// if (debug) { console.log('Creep[' + creep.name + '] Miner cannot transfer resources'); }
// }
// } else {
// creep.say('No Drop');
// if (debug) { console.log('Creep[' + creep.name + '] Miner unable to transfer resources is full!'); }
// }
}
if (!creep.memory.dropping) {
// Alright if we're not dropping, we're harvesting lets try harvesting our assigned source
var extractor = Game.getObjectById(creep.memory.assignedExtractor);
if (extractor) {
let result = creep.harvest(extractor);
switch (result) {
case ERR_TIRED:
creep.say('q(-_-)p');
return;
break;
case ERR_NOT_IN_RANGE:
if (debug) { console.log('Creep[' + creep.name + '] Extractor not in range, moving into range'); }
// We're not at the thing! Lets go there!
creep.travelTo(extractor);
// Moving make a say
creep.say('>>');
return;
break;
case OK:
creep.say('d(^-^)b');
break;
}
} else {
if (debug) { console.log('Creep[' + creep.name + '] Extractor cannot find extractor!!'); }
creep.say('WTF?');
Game.notify('Extractor Creep unable to assign a extractor');
}
}
}
/**
* Run this script to setup rooms ready for assigned extractors
*/
module.exports.setup = function (debug = false) {
// Loop through the game rooms we have
for (var name in Game.rooms) {
if (debug) { console.log('Setting up room ' + name); }
var theRoom = Game.rooms[name];
delete theRoom.memory.assignedExtractors;
if (!theRoom.memory.assignedExtractors) {
var extractors = theRoom.find(FIND_MINERALS, {
filter: (i) => i.ticksToRegeneration == 0
});
var array = {};
for (var i=0;i<=extractors.length-1;i++) {
if (debug) { console.log(extractors[i].id); }
array[extractors[i].id] = null;
}
theRoom.memory.assignedExtractors = array;
// Check for the extractorsNeeded flag
if (!theRoom.memory.extractorsNeeded) {
if (debug) { console.log('Setting extractors Needed to ' + extractors.length); }
theRoom.memory.extractorsNeeded = extractors.length;
} else {
if (debug) { console.log('Currently set to ' + theRoom.memory.extractorsNeeded); }
}
} else {
if (debug) { console.log('Assigned Extractors already exists. leaving alone!'); }
}
// First get the extractors
var extractors = theRoom.find(FIND_MINERALS, {
filter: (i) => i.ticksToRegeneration === 0
});
// Loop through the extractors
for (var i=0;i<=extractors.length-1;i++) {
// Get the extractors
var extractor = extractors[i];
// Make found false by default
var found = false;
var creepId = null;
var extractorId = extractor.id;
theRoom.memory.assignedExtractors[extractorId] = null;
// Loop through the miners
for (var creepName in Game.creeps) {
// Define the creep
var creep = Game.creeps[creepName];
if (!creep.memory.role == 'extractor' || creep.memory.dying) {
continue;
}
// If this creep has the assigned Source, we found it
if (creep.memory.assignedExtractor === extractorId) {
found = true;
creepId = creep.id;
break;
}
}
if (found) {
theRoom.memory.assignedExtractors[extractorId] = creepId;
}
}
}
return '++Extractor Setup Complete++';
}