-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbaseControl.js
304 lines (258 loc) · 7.82 KB
/
baseControl.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
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
/**
*
*/
var util = require('common');
function attackHostiles(support) {
var towers = support.room.memory.cache.towers;
if (!util.def(towers.onAlert)) {
towers.onAlert = false;
}
if (!util.def(towers.timeouts.attack)) {
towers.timeouts.attack = Game.time;
}
var targets = [];
if (towers.onAlert || (towers.timeouts.attack < Game.time)) {
targets = support.room.find(FIND_HOSTILE_CREEPS);
towers.timeouts.attack = Game.time + 11 + util.getRand(1, Object.keys(Game.rooms).length);
}
if (targets.length == 0) {
towers.onAlert = false;
return false;
} else {
towers.onAlert = true;
}
var hitList = targets.sort(function(a, b) {
if (a.hits / a.hitsMax > b.hits / b.hitsMax) {
return 1;
}
if (a.hits / a.hitsMax < b.hits / b.hitsMax) {
return -1;
}
return 0;
}); // Get most sensible
var res = support.attack(hitList[0]);
switch (res) {
case OK:
return true;
break;
case ERR_NOT_ENOUGH_ENERGY:
return true; // this gun is out, but others might not be
break;
case ERR_RCL_NOT_ENOUGH:
return false;
break;
default:
dlog('Attack Error - ' + util.getError(res));
return false;
break;
}
}
function healTroops(support) {
var towers = support.room.memory.cache.towers;
if (!util.def(towers.injured)) {
towers.injured = false;
}
if (!util.def(towers.timeouts.heal)) {
towers.timeouts.heal = Game.time;
}
var targets = [];
if (towers.injured || (towers.timeouts.heal < Game.time)) {
towers.timeouts.heal = Game.time + 11 + util.getRand(1, Object.keys(Game.rooms).length);
var targets = support.room.find(FIND_MY_CREEPS, {
filter: (i) => i.hits < i.hitsMax
});
}
if (targets.length == 0) {
towers.injured = false;
return false
} else {
towers.injured = true;
}
var hitList = targets.sort(function(a, b) {
if (a.hits / a.hitsMax > b.hits / b.hitsMax) {
return 1;
}
if (a.hits / a.hitsMax < b.hits / b.hitsMax) {
return -1;
}
return 0;
}); // Get most sensible
var res = support.heal(hitList[0]);
switch (res) {
case OK:
return true;
break;
case ERR_NOT_ENOUGH_ENERGY:
return true; // this gun is out, but others might not be
break;
dlog('Heal Error - ' + util.getError(res));
return false;
break;
}
}
function repairBase(support) {
var towers = support.room.memory.cache.towers;
if (!util.def(towers.damaged)) {
towers.damaged = false;
}
if (!util.def(towers.timeouts.repair)) {
towers.timeouts.repair = Game.time;
}
var targets = [];
if (towers.damaged || (towers.timeouts.repair + 61 + util.getRand(1, Object.keys(Game.rooms).length) < Game.time)) {
var targets = support.room.find(FIND_MY_STRUCTURES, {
filter: (i) => i.hits < i.hitsMax
});
}
if (targets.length == 0) {
towers.damaged = false;
return false
} else {
towers.damaged = true;
}
//dlog(support.id + ' trying to repar');
// dlog('found ' + targets.length)
var hitList = targets.sort(function(a, b) {
if (a.hits / a.hitsMax > b.hits / b.hitsMax) {
return 1;
}
if (a.hits / a.hitsMax < b.hits / b.hitsMax) {
return -1;
}
return 0;
}); // Get most sensible
var res = support.repair(hitList[0]);
switch (res) {
case OK:
return true;
break;
case ERR_NOT_ENOUGH_ENERGY:
return true; // this gun is out, but others might not be
break;
case ERR_RCL_NOT_ENOUGH:
return false;
break;
default:
dlog('Repair Error - ' + util.getError(res));
return false;
break;
}
}
function repairRoads(support) {
var towers = support.room.memory.cache.towers;
if (!util.def(towers.potholes)) {
towers.potholes = false;
}
if (!util.def(towers.timeouts.fixroads)) {
towers.timeouts.fixroads = Game.time;
}
var targets = [];
if (towers.potholes || (towers.timeouts.fixroads + 21 + util.getRand(1, Object.keys(Game.rooms).length) < Game.time)) {
var targets = support.room.find(FIND_STRUCTURES, {
filter: (i) => i.hits < i.hitsMax && i.structureType == 'road'
});
}
if (targets.length == 0) {
towers.potholes = false;
return false
} else {
towers.potholes = true;
}
// dlog(support.id + ' trying to repar');
var hitList = targets.sort(function(a, b) {
if (a.hits / a.hitsMax > b.hits / b.hitsMax) {
return 1;
}
if (a.hits / a.hitsMax < b.hits / b.hitsMax) {
return -1;
}
return 0;
}); // Get most sensible
var spin = 0;
while (spin < targets.length) {
var roadCrack = hitList[spin];
if (roadCrack.room.memory.trafficMap && roadCrack.room.memory.trafficMap[roadCrack.pos.x] && roadCrack.room.memory.trafficMap[roadCrack.pos.x][roadCrack.pos.y]) {
var road = roadCrack.room.memory.trafficMap[roadCrack.pos.x][roadCrack.pos.y];
var decrement = road.heat - (Game.time - road.refereshed);
road.heat = (decrement > road.heat) ? 0 : road.heat - decrement;
road.refereshed = Game.time;
if (road.heat < 75) {
spin++;
continue;
}
}
var res = support.repair(roadCrack);
switch (res) {
case OK:
return true;
break;
case ERR_NOT_ENOUGH_ENERGY:
return true; // this gun is out, but others might not be
break;
case ERR_RCL_NOT_ENOUGH:
return false;
break;
default:
dlog('Road Repair Error - ' + util.getError(res));
return false;
break;
}
}
}
function towerControl(room) {
if (!Game.rooms[room.name] || !Game.rooms[room.name].controller) {
return false;
}
// sanity checks
if (!util.def(room.memory.cache)) {
room.memory.cache = {};
}
var towers = room.memory.cache.towers;
if (!util.def(towers) || !util.def(towers.refreshed)) {
room.memory.cache.towers = {
refreshed: 0
}
return;
}
if (!util.def(towers.timeouts)) {
towers.timeouts = {
attack: Game.time,
repair: Game.time,
heal: Game.time,
roads: Game.time
};
return;
}
if (towers.refreshed + 317 + util.getRand(1, 100) < Game.time) {
// cache expired. redo.
towers.refreshed = Game.time;
var towerIds = room.find(FIND_MY_STRUCTURES, {
filter: {
structureType: STRUCTURE_TOWER
}
});
var temp = [];
for (var ind in towerIds) {
temp.push(towerIds[ind].id);
}
towers.assets = temp;
}
for (var gun in towers.assets) {
var thisTower = Game.getObjectById(towers.assets[gun]);
if (!thisTower || thisTower.energy == 0) {
continue
}
if (attackHostiles(thisTower) ||
repairBase(thisTower) ||
repairRoads(thisTower) ||
healTroops(thisTower)) {
continue;
} else {
return false
};
}
}
module.exports.towerControl = towerControl;
function dlog(msg) {
util.dlog('BASE CONTROL: ', msg);
}