-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.basePlanner.js
276 lines (236 loc) · 7.69 KB
/
base.basePlanner.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
const overlay = require("./debug.overlay");
const matrixUtility = require("./base.matrixUtility");
const stamps = require("./base.stamps");
const PlanBuilder = require("./base.planBuilder");
const RCLPlanner = require("./base.RCLPlanner");
const {
serializeBasePlan,
deserializeBasePlan,
runTests,
} = require("./base.serializeBasePlan");
const {
MAX_VALUE,
EXCLUSION_ZONE,
structureToNumber,
numberToStructure,
MAX_RCL,
TITLE_SIZE,
HEADER_SIZE,
} = require("./base.planningConstants");
const {
getPlan,
savePlan,
savePlanData,
keys,
} = require("./base.planningUtility");
const WEIGHT_CONTROLLER = 1.2;
const WEIGHT_SOURCES = 0.85;
const WEIGHT_SOURCE_NEIGHBOURS = 100;
const WEIGHT_EXIT_DIST = -0.7;
const WEIGHT_TERRAIN_DIST = -0.9;
const STAMP_COUNT_SPAWN = 2;
const STAMP_COUNT_EXTENSION = 3;
const STAMP_COUNT_LAB = 1;
const BUCKET_MINIMUM = 300;
class BasePlanner {
generateNewRoomPlan(colony) {
if (Game.cpu.bucket <= BUCKET_MINIMUM) {
return;
}
const totalCpu = Game.cpu.getUsed();
let cpu = Game.cpu.getUsed();
this.printDebugMessage(
"<" +
"-".repeat(TITLE_SIZE) +
" Tyrant's Base Planner V1.0.2 " +
"-".repeat(TITLE_SIZE) +
">"
);
//#region Initialization
// Generate our necessary matrices for planning
const terrainMatrix = matrixUtility.generateTerrainMatrix(
colony.room.name
);
const distanceTransform = matrixUtility.generateDistanceTransform(
colony.room.name
);
const weightMatrix = this.generateWeightMatrix(
colony,
terrainMatrix,
distanceTransform
);
//#endregion
//#region Room Plan
const planBuilder = new PlanBuilder(
terrainMatrix,
distanceTransform,
weightMatrix,
stamps.core,
colony
);
planBuilder.planUpgraderContainer();
planBuilder.planExtractor();
planBuilder.planMiningSpots();
// Resort our spaces by distance to the core
planBuilder.resortSpaces(
(a, b) =>
planBuilder.floodfillFromCore.get(a.x, a.y) -
planBuilder.floodfillFromCore.get(b.x, b.y)
);
// Plan out our future routes to the exits for remotes
planBuilder.planRemoteRoads();
// Spawn stamps
planBuilder.placeStamps(
stamps.extensionStampXWithSpawn,
STAMP_COUNT_SPAWN
);
// Lab stamps
planBuilder.placeStamps(stamps.labs, STAMP_COUNT_LAB);
// Ordinary extension stamps
planBuilder.placeStamps(stamps.extensionStampX, STAMP_COUNT_EXTENSION);
// Plan our artery roads
planBuilder.planRoads();
// Connect up straggling roads
planBuilder.connectStragglingRoads();
// Place all of our dynamic structures
planBuilder.filterBadSpaces();
planBuilder.placeDynamicStructures();
// Finally, let's rampart our entire base
planBuilder.planRamparts();
// Cleanup any roads placed over terrain
planBuilder.cleanup();
const { structures, ramparts } = planBuilder.getProduct();
//#endregion
this.printDebugMessage(
`🟢 Completed plan creation in ${(Game.cpu.getUsed() - cpu).toFixed(
DEBUG.cpuPrintoutFigures
)} CPU`
);
cpu = Game.cpu.getUsed();
//#region RCL planning
const rclPlanner = new RCLPlanner(
structures,
planBuilder.corePos,
colony
);
rclPlanner.planGenericStructures();
rclPlanner.planContainers(
planBuilder.upgraderContainer,
planBuilder.mineralContainer,
planBuilder.sourceContainers
);
rclPlanner.planTowers();
rclPlanner.planRamparts(ramparts);
rclPlanner.planRoads(stamps.core);
const { rclStructures, rclRamparts } = rclPlanner.getProduct();
//#endregion
this.printDebugMessage(
`🟢 Completed RCL planning in ${(Game.cpu.getUsed() - cpu).toFixed(
DEBUG.cpuPrintoutFigures
)} CPU`
);
cpu = Game.cpu.getUsed();
// Save the serialized plans to memory
savePlan(
colony.room.name,
serializeBasePlan(rclStructures, rclRamparts)
);
savePlanData(
colony.room.name,
keys.upgraderContainerPos,
planBuilder.upgraderContainer
);
savePlanData(
colony.room.name,
keys.mineralContainerPos,
planBuilder.mineralContainer
);
savePlanData(
colony.room.name,
keys.sourceContainerPositions,
planBuilder.sourceContainers
);
savePlanData(colony.room.name, "core", planBuilder.corePos);
this.printDebugMessage(
`🟢 Completed plan serialization in ${(
Game.cpu.getUsed() - cpu
).toFixed(DEBUG.cpuPrintoutFigures)} CPU`
);
//#region Debug
if (DEBUG.testBasePlanSerialization) {
runTests(rclStructures, rclRamparts);
}
this.printDebugMessage(
"-".repeat(HEADER_SIZE) +
" Completed base planning in " +
(Game.cpu.getUsed() - totalCpu).toFixed(
DEBUG.cpuPrintoutFigures
) +
" CPU " +
"-".repeat(HEADER_SIZE)
);
//#endregion
}
visualizePlan(roomName, rcl = (Game.time % MAX_RCL) + 1) {
const plan = getPlan(roomName);
if (!plan) {
return;
}
const { structures, ramparts } = deserializeBasePlan(plan, rcl);
const mapping = _.omit(numberToStructure, [
structureToNumber[EXCLUSION_ZONE],
]);
overlay.visualizeBasePlan(roomName, structures, ramparts, mapping);
}
generateWeightMatrix(colony, terrainMatrix, distanceTransform) {
const controllerMatrix = {
matrix: matrixUtility.floodfill(
colony.room.controller.pos,
terrainMatrix.clone()
),
weight: WEIGHT_CONTROLLER,
};
const sourceMatrices = [];
for (const source of colony.sources) {
sourceMatrices.push({
matrix: matrixUtility.floodfill(
source.pos,
terrainMatrix.clone()
),
weight: WEIGHT_SOURCES,
});
// Heavily discourage structures directly next to sources,
// which block miners
sourceMatrices.push({
matrix: matrixUtility.generateNeighbourMatrix(source.pos, 1),
weight: WEIGHT_SOURCE_NEIGHBOURS,
});
}
const exitDistMatrix = {
matrix: matrixUtility.floodfill(
colony.room.find(FIND_EXIT),
terrainMatrix.clone()
),
weight: WEIGHT_EXIT_DIST,
};
const distMatrix = {
matrix: distanceTransform,
weight: WEIGHT_TERRAIN_DIST,
};
return matrixUtility.normalizeMatrix(
matrixUtility.addScoreMatrices(
controllerMatrix,
...sourceMatrices,
exitDistMatrix,
distMatrix
),
MAX_VALUE - 1
);
}
printDebugMessage(message) {
if (DEBUG.validateBasePlans) {
console.log(message);
}
}
}
module.exports = BasePlanner;