-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathroomaspect.sources.js
69 lines (55 loc) · 2.34 KB
/
roomaspect.sources.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
const logistic = require('helper.logistic');
const miner = require("role.miner");
const roads = require("construction.roads");
const spawnHelper = require("helper.spawning");
const energyExcessThreshold = 50000;
module.exports = class SourcesAspect {
constructor(roomai) {
this.roomai = roomai;
this.room = roomai.room;
this.sources = this.room.find(FIND_SOURCES);
// order sources by distance to primary spawn, to ensure that aspects
// work on that source first
this.sources = _.sortBy(this.sources, (s) => s.pos.getRangeTo(roomai.spawns.primary));
}
run() {
this.buildRoads();
this.buildMiners();
}
buildRoads() {
let storagePos = this.room.storagePos();
if(!this.roomai.intervals.buildStructure.isActive() || !storagePos) {
return;
}
for(let source of this.sources) {
let store = logistic.storeFor(source);
if(!store) continue;
roads.buildRoadFromTo(this.room, storagePos, store.pos);
}
}
buildMiners() {
if(!this.roomai.canSpawn()) {
return;
}
let hasExcessEnergy = this.roomai.trading.requiredExportFromRoom(RESOURCE_ENERGY, { showExcess: true }) >= energyExcessThreshold;
if(hasExcessEnergy) return;
let idealParts = spawnHelper.bestAvailableParts(this.room, miner.energyConfigs);
let minimalParts = spawnHelper.bestAffordableParts(this.room, miner.energyConfigs, true);
let spawnDuration = spawnHelper.spawnDuration(idealParts);
let existingMiners = spawnHelper.localCreepsWithRole(this.roomai, miner.name);
let longLivingMiners = _.filter(existingMiners, (c) => !c.ticksToLive || c.ticksToLive > spawnDuration);
for(let source of this.sources) {
if(!_.any(longLivingMiners, (m) => m.memory.target == source.id)) {
let parts = _.any(existingMiners, (m) => m.memory.target == source.id) ? idealParts : minimalParts;
let memory = {
role: miner.name,
target: source.id,
resource: RESOURCE_ENERGY
};
this.roomai.spawn(parts, memory);
}
}
}
}
const profiler = require("screeps-profiler");
profiler.registerClass(module.exports, 'SourcesAspect');