-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroomaspect.minerals.js
125 lines (102 loc) · 3.93 KB
/
roomaspect.minerals.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
var spawnHelper = require("helper.spawning");
const logistic = require("helper.logistic");
const roads = require("construction.roads");
var carrier = require("role.carrier");
var miner = require("role.miner");
const mineralExcessThreshold = 50000;
module.exports = class MineralsAspect {
constructor(roomai) {
this.roomai = roomai;
this.room = roomai.room;
this.mineral = this.room.find(FIND_MINERALS)[0];
}
run() {
if(this.room.controller.level < 6) {
return;
}
if(this.hasExtractor()) {
if(this.needWorkers()) {
this.buildMiner();
this.buildCarrier();
}
if(this.storePoisoned()) {
this.buildStoreCleaner();
}
}
this.buildStructures();
}
buildMiner() {
var existingMiners = spawnHelper.localCreepsWithRole(this.roomai, miner.name);
if(_.any(existingMiners, (c) => c.memory.target == this.mineral.id)) {
return;
}
var parts = spawnHelper.bestAvailableParts(this.room, miner.mineralConfigs(this.mineral));
var memory = {
role: miner.name,
target: this.mineral.id,
resource: this.mineral.mineralType
};
this.roomai.spawn(parts, memory);
}
buildCarrier() {
let mineralStore = logistic.storeFor(this.mineral);
if(mineralStore === this.room.terminal || mineralStore === this.room.storage ||
_.any(spawnHelper.localCreepsWithRole(this.roomai, carrier.name), (creep) => creep.memory.source == this.mineral.id)) {
return;
}
if(!this.room.storage) return;
let parts = spawnHelper.bestAvailableParts(this.room, carrier.partConfigs);
let memory = {
role: carrier.name,
source: this.mineral.id,
destination: this.room.storage.id,
resource: this.mineral.mineralType
};
this.roomai.spawn(parts, memory);
}
buildStructures() {
if(this.roomai.intervals.buildStructure.isActive()) {
this.room.createConstructionSite(this.mineral.pos, STRUCTURE_EXTRACTOR);
}
if(this.roomai.intervals.buildStructure.isActive() && this.room.storage) {
let container = logistic.storeFor(this.mineral, true);
if(container) roads.buildRoadFromTo(this.room, this.room.storage.pos, container.pos);
}
}
masterRoom() {
return Game.rooms[this.room.memory.slaveOf];
}
hasExtractor() {
return this.room.find(FIND_MY_STRUCTURES, { filter: (s) => s.structureType == STRUCTURE_EXTRACTOR }).length > 0;
}
needWorkers() {
if(this.roomai.canSpawn() && this.mineral.mineralAmount > 0) {
if(this.roomai.trading.requiredExportFromRoom(this.mineral.mineralType, { showExcess: true }) < mineralExcessThreshold) {
return true;
}
}
return false;
}
storePoisoned() {
let store = logistic.storeFor(this.mineral);
return store && store.structureType === STRUCTURE_CONTAINER && store.store.energy > (store.store.getCapacity() / 2);
}
buildStoreCleaner() {
if(!this.room.storage) return;
let store = logistic.storeFor(this.mineral);
if(_.any(spawnHelper.localCreepsWithRole(this.roomai, carrier.name), (c) => c.memory.source === store.id)) {
return;
}
let parts = spawnHelper.bestAvailableParts(this.room, carrier.configsForCapacity(100));
let memory = {
role: carrier.name,
source: store.id,
destination: this.room.storage.id,
resource: "energy"
};
this.roomai.spawn(parts, memory);
console.log("Mineral store in " + this.room.name + " contains energy. Cleaning up!");
}
}
const profiler = require("screeps-profiler");
profiler.registerClass(module.exports, 'MineralsAspect');