-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprototype.room.js
More file actions
458 lines (436 loc) · 14.1 KB
/
prototype.room.js
File metadata and controls
458 lines (436 loc) · 14.1 KB
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
const util=require('utilities');
//Misc.
/**
* Summary. Returns a list of available spawns from the given list
*
* @param spawns List of spawns to select from
* @returns {StructureSpawn[]}
*/
Room.prototype.chooseSpawns=function() {
let spawns=this.getSpawns();
return spawns.filter(function(spawn){
return spawn.isActive() && spawn.spawning===null;
});
};
//Building Placement
/**
* Summary. Places buildings based on the appropriate RCL
*/
Room.prototype.controllerChange=function(){
const currLevel=this.memory.level;
const controllerLevel=this.controller.level;
const centerX=this.memory.centerX;
const centerY=this.memory.centerY;
if(centerX && centerY && currLevel<controllerLevel){
switch(currLevel){
case 1:
this.place(STRUCTURE_EXTENSION,5);
break;
case 2:
this.place(STRUCTURE_EXTENSION,5);
this.place(STRUCTURE_TOWER);
this.place(STRUCTURE_ROAD,999999);
//TODO: place walls and ramparts
break;
case 3:
this.place(STRUCTURE_STORAGE);
this.place(STRUCTURE_EXTENSION,10);
break;
case 4:
this.place(STRUCTURE_EXTENSION,10);
this.place(STRUCTURE_TOWER);
//TODO: add 2 links
break;
case 5:
const min=this.find(FIND_MINERALS)[0];
if(min) {
min.pos.createConstructionSite(STRUCTURE_EXTRACTOR);
this.memory.mineralType=min.mineralType;
}
this.place(STRUCTURE_EXTENSION,10);
this.place(STRUCTURE_LAB,3);
this.place(STRUCTURE_TERMINAL);
//TODO: add 1 link
break;
case 6:
this.place(STRUCTURE_SPAWN);
this.place(STRUCTURE_EXTENSION,10);
this.place(STRUCTURE_TOWER);
this.place(STRUCTURE_LAB,3);
//TODO: add 1 link
break;
case 7:
this.place(STRUCTURE_SPAWN);
this.place(STRUCTURE_EXTENSION,10);
this.place(STRUCTURE_TOWER,3);
this.place(STRUCTURE_LAB,4);
this.place(STRUCTURE_OBSERVER);
this.place(STRUCTURE_NUKER);
this.place(STRUCTURE_POWER_SPAWN);
//TODO: add 2 links
break;
}
this.memory.level++;
}
};
/**
* Summary. Places buildings of the indicated number based on the predefined base layout
*
* @param {String} type The type of structure to place
* @param number Number of Structures to place, default 1
*/
Room.prototype.place=function(type,number=1){
const centerX=this.memory.centerX;
const centerY=this.memory.centerY;
const orientation=this.memory.orientation;
const name=this.name;
if(centerX && centerY && orientation) {
let xVals=[];
let yVals=[];
switch (type) {
case STRUCTURE_SPAWN:
xVals=[-2,2,-3];
yVals=[0,0,-1];
break;
case STRUCTURE_EXTENSION:
//This took a very long time
xVals=[
-1, 1,-1, 1, 0,
0,-1,-2,-2,-3,
-3,-1,-2,-2,-3,
-4,-4,-4, 2, 2,
1, 1, 2, 3, 3,
4, 4, 4, 3, 2,
0, 0, 1, 2, 3,
3, 1, 2, 2, 3,
4, 4, 4,-1,-2,
-3,-3,-1,-2,-2,
-3,-4,-4,-4,-4,
-5,-5,-5,-5,-4
];
yVals=[
-2,-2,-3,-3,-4,
-5,-5,-3,-4,-4,
-5,-6,-6,-7,-7,
-7,-6,-5,-3,-4,
-5,-6,-6,-4,-5,
-5,-6,-7,-7,-7,
4, 5, 5, 4, 4,
5, 6, 6, 7, 7,
7, 6, 5, 5, 4,
4, 5, 6, 6, 7,
7, 7, 6, 5, 1,
1, 0,-1,-2,-2
];
break;
case STRUCTURE_POWER_SPAWN:
xVals=[3];
yVals=[1];
break;
case STRUCTURE_LAB:
xVals=[4,5,5,6,7,7,7,6,6,5];
yVals=[1,1,2,2,2,1,0,0,-1,-1];
break;
case STRUCTURE_STORAGE:
xVals=[0];
yVals=[0];
break;
case STRUCTURE_LINK:
xVals=[-3,3];
yVals=[1,-1];
break;
case STRUCTURE_OBSERVER:
xVals=[-4];
yVals=[2];
break;
case STRUCTURE_TOWER:
xVals=[-1,1,-2,-1,1,2];
yVals=[2,2,3,3,3,3];
break;
case STRUCTURE_NUKER:
xVals=[4];
yVals=[-2];
break;
case STRUCTURE_TERMINAL:
xVals=[4];
yVals=[2];
break;
case STRUCTURE_ROAD:
xVals=[
0, 1, 1, 1, 0,
-1,-1,-1, 0, 0,
-1, 1,-2, 2,-3,
3, 0, 0,-1, 1,
-2, 2,-3, 3,-3,
-4,-4, 3, 4, 4,
5, 6,-2,-2,-3,
-3,-4,-4,-5,-5,
2, 2, 3, 3, 4,
4, 5, 5,-2,-2,
-3,-3,-4,-4,-5,
-5, 2, 2, 3, 3,
4, 4, 5, 5
];
yVals=[
-1,-1, 0, 1, 1,
1, 0,-1,-2,-3,
-4,-4,-5,-5,-6,
-6, 2, 3, 4, 4,
5, 5, 6, 6, 0,
0,-1, 0, 0,-1,
0, 1,-1,-2,-2,
-3,-3,-4,-4,-5,
-1,-2,-2,-3,-3,
-4,-4,-5, 1, 2,
2, 3, 3, 4, 4,
5, 1, 2, 2, 3,
3, 4, 4, 5
];
break;
}
let validLocations=[];
for(let i=0;i<xVals.length;i++) {
if (orientation === 'vertical')
validLocations.push(new RoomPosition(centerX + xVals[i], centerY + yVals[i], name));
else
validLocations.push(new RoomPosition(centerX + yVals[i], centerY + xVals[i], name));
}
let placed=0;
for(let pos of validLocations){
let ret=this.createConstructionSite(pos,type);
if(ret===OK){
placed++;
if(placed>=number)
return;
}
else if(ret===ERR_RCL_NOT_ENOUGH){
return;
}
}
}
};
/**
* Summary. Calculates the best possible center for the base given the layout of the room and the predefined base
* formation and updates the room's memory
*/
Room.prototype.findCenter=function(){
this.memory.centerX=-1;
this.memory.centerY=-1;
this.memory.orientation='vertical';
//Initialize the array of all potential valid centers
let possibleCentersV=[];
let possibleCentersH=[];
for(let i=0;i<50;i++){
possibleCentersV.push([]);
possibleCentersH.push([]);
for(let j=0;j<50;j++){
const valid=i>7&&i<42&&j>8&&j<41;
possibleCentersV[i].push(valid); //Whether the initial location is valid
possibleCentersH[i].push(valid);
}
}
//Test each terrain in the room
for(let i=0;i<50;i++){
for(let j=0;j<50;j++){
let loc=this.lookForAt(LOOK_TERRAIN,i,j);
let found=false;
if(loc){
//account for multiple 'terrain' in one location
for(let terrain of loc){
if(terrain==='wall'){
found=true;
}
}
if(found){
//Iterate through all centers that a wall conflicts with
for(let k=0;k<13;k++){
for(let m=0;m<15;m++){
const centerX=i+k-6;
const centerY=j+m-7;
if(centerX>0&¢erX<50&¢erY>0&¢erY<50) {
possibleCentersV[centerX][centerY] = false;
}
}
}
//The same but for horizontal centers
for(let k=0;k<15;k++){
for(let m=0;m<13;m++){
const centerX=i+k-7;
const centerY=j+m-6;
if(centerX>0&¢erX<50&¢erY>0&¢erY<50) {
possibleCentersH[centerX][centerY] = false;
}
}
}
}
}
}
}
let centers=[];
for(let i=0;i<50;i++){
for(let j=0;j<50;j++){
//push x,y coords to another array and choose the best one
if(possibleCentersV[i][j]===true){
centers.push({x:i,y:j,o:'vertical'});
}
if(possibleCentersH[i][j]===true){
centers.push({x:i,y:j,o:'horizontal'});
}
}
}
const sources=this.getSources();
for(let i in centers){
const curr=centers[i];
let heur=0;
for(let j in sources){
heur+=util.distance(sources[j].pos,new RoomPosition(curr.x,curr.y,this.name));
}
heur+=util.distance(this.controller.pos,new RoomPosition(curr.x,curr.y,this.name));
centers[i].heur=heur;
}
centers.sort(function(a,b){
return a.heur-b.heur;
});
this.memory.centerX=centers[0].x;
this.memory.centerY=centers[0].y;
};
Room.prototype.findMinerals=function(){
let minerals = this.find(FIND_MINERALS)[0];
this.memory.mineralId = minerals.id;
};
//Memory Management
/**
* Summary. Returns all sources in the room. Parses from memory or calls Room.find and sets memory
*
* @returns {Source[]} All sources in the room
*/
Room.prototype.getSources=function(){
if(this.memory.sources!==undefined){
const objs=util.idsToObjs(JSON.parse(this.memory.sources));
let valid=true;
for(let i in objs){
if(objs[i]===null){
valid=null;
break;
}
}
if(valid)
return objs;
}
let sources=this.find(FIND_SOURCES);
this.memory.sources=JSON.stringify(util.objsToIds(sources));
return sources;
};
/**
* Summary. Returns all structures in the room. Parses from memory or calls Room.find and sets memory
*
* @returns {Structure[]} All Structure objects in the room
*/
Room.prototype.getBuildings=function(){
if(this.memory.buildings!==undefined){
const objs=util.idsToObjs(JSON.parse(this.memory.buildings));
let valid=true;
for(let i in objs){
if(objs[i]===null){
valid=null;
break;
}
}
if(valid)
return objs;
}
const buildings=this.find(FIND_STRUCTURES);
this.memory.buildings=JSON.stringify(util.objsToIds(buildings));
return buildings;
};
/**
* Summary. Returns all friendly creeps in the room. Parses from memory or calls Room.find and sets memory
*
* @returns {Creep[]} All friendly Creep objects in the room
*/
Room.prototype.getMyCreeps=function(){
if(this.memory.myCreeps!==undefined){
const objs=util.idsToObjs(JSON.parse(this.memory.myCreeps));
let valid=true;
for(let i in objs){
if(objs[i]===null){
valid=null;
break;
}
}
if(valid)
return objs;
}
const myCreeps=this.find(FIND_MY_CREEPS);
this.memory.myCreeps=JSON.stringify(util.objsToIds(myCreeps));
return myCreeps;
};
/**
* Summary. Sets the Room.memory.myCreeps to represent the passed myCreeps
*
* @param {Creep[]} myCreeps All friendly creeps in the room
*/
Room.prototype.setMyCreeps=function(myCreeps) {
this.memory.myCreeps = JSON.stringify(util.objsToIds(myCreeps));
};
/**
* Summary. Returns all enemy creeps in the room. Parses from memory or calls Room.find and sets memory
*
* @returns {Creep[]} All enemy Creep objects in the room
*/
Room.prototype.getEnemyCreeps=function(){
if(this.memory.enemyCreeps!==undefined){
const objs=util.idsToObjs(JSON.parse(this.memory.enemyCreeps));
let valid=true;
for(let i in objs){
if(objs[i]===null){
valid=null;
break;
}
}
if(valid)
return objs;
}
const enemyCreeps=this.find(FIND_HOSTILE_CREEPS);
this.memory.enemyCreeps=JSON.stringify(util.objsToIds(enemyCreeps));
return enemyCreeps;
};
/**
* Summary. Returns all spawns in the room. Parses from memory or calls Room.find and sets memory
*
* @returns {StructureSpawn[]} All spawns in the room
*/
Room.prototype.getSpawns=function(){
if(this.memory.spawns!==undefined){
const objs=util.idsToObjs(JSON.parse(this.memory.spawns));
let valid=true;
for(let i in objs){
if(objs[i]===null){
valid=null;
break;
}
}
if(valid)
return objs;
}
let spawns=_.filter(this.getBuildings(),(building)=>building.structureType===STRUCTURE_SPAWN);
this.memory.spawns=JSON.stringify(util.objsToIds(spawns));
return spawns;
};
/**
* Summary. Clears temporary memory for the room
*/
Room.prototype.resetTempMemory=function(){
this.memory.buildings=undefined;
this.memory.myCreeps=undefined;
this.memory.enemyCreeps=undefined;
this.memory.spawns=undefined;
};
/**
* Summary. Clears temporary memory for the room
*/
Room.prototype.resetAllMemory=function() {
for (let i in this.memory) {
this.memory[i] = undefined;
}
};