-
Notifications
You must be signed in to change notification settings - Fork 0
/
role2.dropHarvester.js
346 lines (325 loc) · 12.5 KB
/
role2.dropHarvester.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
const util = require('util.creep');
const pathing = require('util.pathing');
const BodyBuilder = require('util.bodybuilder');
// Mostly-WORK harvester that drops on the ground (or in containers if
// available).
// Important to optimize source withdrawal, since the regen timer doesn't start
// until the first harvest each time.
//
// TODO(baptr): Is it actually better to time it perfectly, or to drain the
// the source as fast as possible? Slow and steady means more waiting...
// TOOD(baptr): Create a second container if the first one is filling up. (But
// remember 5 limit)
const ROLE = 'dropHarvester';
module.exports = {
spawn: function(spawn) {
var room = spawn.room;
const availEnergy = room.energyAvailable;
if(availEnergy < MIN_HARVESTER_COST || spawn.spawning) { return ERR_BUSY; }
var builder = new BodyBuilder([WORK, WORK, MOVE], availEnergy);
builder.extend([WORK, WORK, MOVE], limit=(HARVESTER_WORKS - builder.count(WORK))/2);
builder.extend([CARRY, MOVE], limit=1);
builder.sort();
// TODO(baptr): Base on available locatiosn near the spawn.
if(builder.count(WORK) < 4) return ERR_NOT_ENOUGH_RESOURCES;
const targetSource = pickSource(room);
if(!targetSource) { return ERR_NOT_FOUND; }
const name = `${ROLE}-${room.name}-${Game.time}`
var ret = spawn.spawnCreep(builder.body, name, {memory: {
role: ROLE,
src: targetSource.id,
cost: builder.cost,
life: {},
}});
if(ret != OK) {
console.log(`Spawning ${name} = ${ret}`);
}
return ret;
},
spawnRemote: function(spawn, srcRoom, srcID=null) {
if(!srcRoom) {
console.log('Need srcRoom and srcID if possible');
return ERR_INVALID_ARGS;
}
if(srcRoom && !srcID) {
var room = Game.rooms[srcRoom];
if(!room) {
console.log('Need srcID if srcRoom is not visible');
return ERR_INVALID_ARGS;
}
var src = pickSource(room);
if(!src) {
console.log(`Unable to find source in ${srcRoom}`);
return ERR_NOT_FOUND;
}
srcID = src.id;
}
var builder = new BodyBuilder([WORK, WORK, MOVE], spawn.room.energyAvailable);
builder.extend([CARRY, CARRY, MOVE], limit=1);
builder.extend([WORK, WORK, MOVE]);
if(builder.count(WORK) < 6) { // randomly chosen. TODO: math
console.log(`Not worth remote dropHarvesting for ${builder.count(WORK)} WORK at ${builder.cost} energy`);
return ERR_NOT_ENOUGH_ENERGY;
}
const name = `${ROLE}-${srcRoom}-${Game.time}`;
var ret = spawn.spawnCreep(builder.sort(), name, {memory: {
role: ROLE,
src: srcID,
cost: builder.cost,
remoteRoom: srcRoom,
}})
if(ret != OK) {
console.log(`Failed to spawn ${name}: ${ret}`);
}
return ret;
},
run: function(creep) {
util.track(creep, 'alive');
var src = Game.getObjectById(creep.memory.src);
if(!src) {
if(creep.memory.remoteRoom) {
return creep.moveTo(new RoomPosition(25, 25, creep.memory.remoteRoom));
}
src = pickSource(creep.room);
if(src) {
creep.memory.src = src.id;
} else{
console.log(`${creep.name} has invalid src ${creep.memory.src}`);
return false;
}
}
if(!creep.pos.isNearTo(src)) {
// XXX need to prevent repositioning for a different container from
// flapping against this.
var ret = creep.moveTo(src);
util.track(creep, 'move', ret);
return ret;
}
// In range...
const cont = planContainer(creep, src);
var ret = creep.harvest(src);
util.track(creep, 'harvest', ret);
switch(ret) {
case ERR_NOT_IN_RANGE:
// Shouldn't happen with the range check above..
var ret = creep.moveTo(src);
util.track(creep, 'late_move', ret);
break;
case OK:
// TODO(baptr): This is probably a little fuzzy if multiple are pulling
// from it when it runs out, but meh.
creep.memory.delivered += Math.min(creep.getActiveBodyparts(WORK)*HARVEST_POWER, src.energy);
if(!creep.carryCapacity) break;
if(!cont || !cont.store) break;
if(creep.carry.energy == creep.carryCapacity && cont.store.energy == cont.storeCapacity) {
var link = Game.getObjectById(creep.memory.link);
if(!link && !creep.memory.hasOwnProperty('link')) {
links = creep.pos.findInRange(FIND_STRUCTURES, 1, {filter: {structureType: STRUCTURE_LINK}});
if(links.length) {
link = links[0];
creep.memory.link = links[0].id;
} else {
creep.memory.link = null;
}
}
if(link) {
creep.transfer(link, RESOURCE_ENERGY);
if(creep.carry.energy < creep.carryCapacity) {
const ground = creep.pos.lookFor(LOOK_RESOURCES);
if(ground.length) {
console.log(`${creep.name} pickup: ${creep.pickup(ground[0])}`);
}
}
}
}
break;
case ERR_NOT_ENOUGH_RESOURCES:
if(creep.carryCapacity && cont) {
if(cont instanceof ConstructionSite) {
creep.build(cont);
} else {
if(cont.hits < cont.hitsMax) {
creep.repair(cont);
} else {
const structs = _.filter(creep.pos.lookFor(LOOK_STRUCTURES), {
structureType: STRUCTURE_RAMPART
});
if(structs.length) {
creep.repair(structs[0]);
}
}
}
const res = creep.pos.lookFor(LOOK_RESOURCES, {filter: {resourceType: RESOURCE_ENERGY}});
if(res.length) creep.pickup(res[0]);
}
break;
default:
console.log(`${creep.name} unhandled harvest ret: ${ret}`);
}
},
ROLE,
pickSource,
};
// XXX this feels expensive...
// TODO(baptr): memoize the time to pay attention again?
function pickSource(room) {
var harvesters = room.find(FIND_MY_CREEPS, {filter: c => c.memory.role == ROLE});
var workParts = {};
var numHarvesters = {};
_.forEach(harvesters, c => {
// TODO(baptr): Feels like there should be a better way to write this.
let src = c.memory.src;
if(workParts[src] == null) {
workParts[src] = 0;
numHarvesters[src] = 0;
}
if(c.ticksToLive > ELDER_EPSILON) {
workParts[src] += c.getActiveBodyparts(WORK);
numHarvesters[src]++;
}
});
const assigned = function(id) { return workParts[id] || 0 };
const existing = function(id) { return numHarvesters[id] || 0 };
var sources = room.find(FIND_SOURCES);
sources.sort((a,b) => assigned(a.id)-assigned(b.id));
for(var i = 0; i < sources.length; i++) {
var s = sources[i];
if(assigned(s.id) >= HARVESTER_WORKS) {
continue;
}
if(existing(s.id) >= pathing.spacesNear(s.pos).length) {
continue;
}
return s;
}
return null;
}
function planContainer(creep, src) {
var cont = Game.getObjectById(creep.memory.container);
if(!cont && creep.memory.remoteRoom) {
if(creep.pos.roomName != creep.memory.remoteRoom) {
// Wait until we're there to figure it out.
return null;
}
// Only think about containers in a controlled (owned or reserved) room.
let ctrl = creep.room.controller;
// TODO(baptr): Consider latching 'no' if appropriate.
if(!ctrl || !ctrl.reservation || !ctrl.reservation.username == 'baptr') return null;
}
if(!cont) {
var conts = src.pos.findInRange(FIND_STRUCTURES, 1, {
filter: {structureType: STRUCTURE_CONTAINER}
});
// Should avoid one if there's already a drop harvester on it...
// But maybe wait/bump any other type?
cont = creep.pos.findClosestByPath(conts, {filter: s => {
if(s.pos.lookFor(LOOK_CREEPS).length && !s.pos.isEqualTo(creep.pos)) {
return false;
}
if(_.sum(s.store) == s.storeCapacity) {
// TODO(baptr): better than nothing, though...
return false;
}
return true;
}})
if(!cont) {
var ret = creep.room.createConstructionSite(creep.pos, STRUCTURE_CONTAINER);
cont = _.filter(creep.pos.lookFor(LOOK_CONSTRUCTION_SITES), {structureType: STRUCTURE_CONTAINER})[0];
if(!cont && ret == ERR_RCL_NOT_ENOUGH) {
// Don't save it in case we don't make it.
// TODO(baptr): Could probably cause flapping...
var destCont = creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: {structureType: STRUCTURE_CONTAINER}});
if(destCont && creep.pos.inRangeTo(destCont, 1)) {
creep.moveTo(destCont);
}
}
}
if(!cont) return null;
creep.memory.container = cont.id;
}
if(_.sum(cont.store) > cont.storeCapacity*0.9) {
let swap = creep.pos.findInRange(FIND_STRUCTURES, 1, {filter: s =>
s.structureType == STRUCTURE_CONTAINER && s.pos.isNearTo(src) &&
_.sum(s.store) < s.storeCapacity * 0.5
})[0];
if(swap) {
console.log(`${creep.name} full, repositioning`);
cont = swap;
creep.memory.container = swap.id;
}
}
if(!creep.pos.isEqualTo(cont.pos)) {
creep.moveTo(cont);
}
return cont;
}
// XXX incomplete + unused
function pickPosition(creep, src) {
// TODO(baptr): Any risk of container not being near source?
var cont = Game.getObjectById(creep.memory.container);
if(cont && _.sum(cont.store) < cont.storeCapacity) {
return cont.pos;
}
var conts = src.pos.findInRange(FIND_MY_STRUCTURES, 1, {
filter: {structureType: STRUCTURE_CONTAINER}
});
var free = _.filter(conts, s => {
let occupant = s.pos.lookFor(LOOK_CREEPS);
return !occupant.length || occupant[0].id == creep.id;
});
var cap = _.filter(free, s => _.sum(s.store) < s.storeCapacity);
cont = creep.pos.findClosestByPath(cap);
if(cont) {
creep.memory.container = cont.id;
return cont.pos;
}
if(conts.length >= 2 || conts.length >= pathing.spacesNear(src)) {
// Don't really want to build more.
cont = creep.pos.findClosestByPath(free);
if(cont) {
creep.memory.container = cont.id;
return cont.pos;
} else {
return null;
}
}
// Build one.
// Maybe - get a path to the source and build at the last position?
/*
var ret = creep.room.createConstructionSite(pos, STRUCTURE_CONTAINER);
if(ret == OK) {
// memoize the container position?
return pos;
}
if(!cont) {
// may already exist, we'll check next
// TODO(baptr): Only 5 containers per room, should prioritize
// standing on one
var ret = creep.room.createConstructionSite(creep.pos, STRUCTURE_CONTAINER);
cont = _.filter(creep.pos.lookFor(LOOK_CONSTRUCTION_SITES), s => s.structureType == STRUCTURE_CONTAINER)[0];
if(!cont && ret == ERR_RCL_NOT_ENOUGH) {
// Don't save it in case we don't make it.
// TODO(baptr): Could probably cause flapping...
var destCont = creep.pos.findClosestByPath(FIND_STRUCTURES, {filter: s => s.structureType == STRUCTURE_CONTAINER});
if(destCont && creep.pos.inRangeTo(destCont, 1)) {
creep.moveTo(destCont);
}
}
}
if(cont) {
creep.memory.container = cont.id;
}
}
*/
}
// TODO(baptr): Tune. It should allow for spawn+travel time.
const ELDER_EPSILON = 75;
// how many WORK parts to empty a source as it refills?
// - assuming no time to repair containers/move/etc
// - can be divided between multiple workers
// TODO(baptr): Spawn small versions early and drop asap, or let bootstrappers
// deal with it?
const HARVESTER_WORKS = 1 + SOURCE_ENERGY_CAPACITY / (HARVEST_POWER * ENERGY_REGEN_TIME); // (5)
// Could be smaller, but bootstrappers can handle the initial setup, so why bother?
const MIN_HARVESTER_BODY = [WORK, WORK, MOVE];
const MIN_HARVESTER_COST = util.bodyCost(MIN_HARVESTER_BODY);