-
Notifications
You must be signed in to change notification settings - Fork 13
/
struct-base.js
103 lines (94 loc) · 3.86 KB
/
struct-base.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
const Constants = require('constants');
const Roads = require('roads');
const BuildOrders = require('build-orders');
const utils = require('utils');
class Struct {
constructor(structureType, opts={}) {
this.structureType = structureType;
this.howmanyAtEachPoi = opts.howmanyAtEachPoi || -1;
this.minFreeAdjSpaces = opts.minFreeAdjSpaces || 3,
this.minPlacementDistance = opts.minPlacementDistance || 7,
this.avoidList = opts.avoidList || [];
this.avoidIsCheckered = opts.avoidIsCheckered || false;
this.structureFilter = opts.structureFilter || { structureType };
this.gc();
}
getMyStructs(room) {
if (!this._structs[room.id]) {
this._structs[room.id] = room.find(FIND_STRUCTURES, {filter: this.structureFilter});
}
return this._structs[room.id];
}
getMySites (room) {
if (!this._sites[room.id]) {
this._sites[room.id] = room.find(FIND_CONSTRUCTION_SITES, {filter: this.structureFilter});
}
return this._sites[room.id];
}
getDesiredNumberOfStructs (room) {
let lvl = room.controller.level;
return CONTROLLER_STRUCTURES[this.structureType][lvl] || 0;
}
getDesiredRange (room) {
let lvl = room.controller.level,
info = Constants.RoomLevel[lvl],
num = info[this.structureType + 'Range'];
return num || 5;
}
* getBuildingPointsOfInterests (room) {
let s = room.find(FIND_SOURCES);
for (let i=0;i<s.length;i++) {
yield s[i];
}
}
buildInRoom (room) {
let existingExt = this.getMyStructs(room),
existingSites = this.getMySites(room),
plannedSitesCount = BuildOrders.getCount(room, this.structureFilter),
desired = this.getDesiredNumberOfStructs(room),
howmanyBuilt = existingExt.length + existingSites.length,
howmanyToBuild = desired - howmanyBuilt - plannedSitesCount,
range = this.getDesiredRange(room);
if (! room || !(room instanceof Room)) {
console.log('buildInRoom failed because it needs valid room');
return;
}
if(howmanyToBuild < 1) {
// console.log(`No ${this.structureType} to create in ${room}. Already enough: ${howmanyBuilt}/${desired}`);
return;
}
// console.log(`Bulding ${howmanyToBuild} ${this.structureType}`);
//round robin, build
let poiAreas = this.getBuildingPointsOfInterests(room);
for(let target of poiAreas) {
let freePOSs = utils.findFreePosNearby(target, range, this.minFreeAdjSpaces, this.minPlacementDistance, this.avoidList, this.avoidIsCheckered),
howmanyHere = 0,
roadList = [];
for(let pos of freePOSs) {
// console.log(`Building ${this.structureType} ${howmanyToBuild}:${howmanyHere} at ${pos}`);
if (BuildOrders.schedule(room, this.structureType, pos)) {
howmanyToBuild--;
howmanyHere++;
roadList.push(pos); // build a road to the structure so creeps can charge it.
}
if (howmanyToBuild === 0 || howmanyHere === this.howmanyAtEachPoi) {
break; // we have built enough or want to move onto the next POI
}
}
Roads.connect(target, roadList);
if (howmanyToBuild === 0) {
// don't need the rest of the POI locations.
break;
}
}
if (howmanyToBuild) {
console.log(`Unable to build ${howmanyToBuild} ${this.structureType}(s) in ${room}`);
}
return howmanyToBuild === 0;
}
gc() {
this._structs = {};
this._sites = {};
}
}
module.exports = Struct;