-
Notifications
You must be signed in to change notification settings - Fork 0
/
mgr.memory.js
317 lines (292 loc) · 11.6 KB
/
mgr.memory.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
305
306
307
308
309
310
311
312
313
314
315
316
317
//cannot reference mgr here
var linksmgr = require("mgr.links")
//const { clearMemory } = require("./mgr");
var memMgr = {
createRoomsMem: function () {
//For creeps without a home room
Memory.outside = {};
Memory.outside.creeps = {}
//All my rooms will be in Game.rooms
for (let roomName in Game.rooms) {
let room = Game.rooms[roomName];
if (!Memory[roomName]) {
Memory[roomName] = {};
Memory[roomName].creeps = {};
//Memory[roomName].links = {};
//Memory[roomName].links.upgradeLink = -1;
//Memory[roomName].links.storageLink = -1;
Memory[roomName].repairs = {};
Memory[roomName].storage = {};
Memory[roomName].constructionSites = {};
Memory[roomName].terminal = {};
Memory[roomName].spawns = {};
for (let spawnName in Game.spawns) {
//console.log(spawnName)
//check if spawn is in current room
if (Game.spawns[spawnName].room == room) {
if (!Memory[roomName].spawns[0]) {
Memory[roomName].spawns[0] = Game.spawns[spawnName];
} else {
//Memory[roomName].spawns[Object.keys(Memory[myroomName.name].spawns).length] = Game.spawns[i];
}
}
}
Memory[roomName].sources = room.find(FIND_SOURCES);
console.log("Created Memory for Room " + room)
}
// if()
//declare memory variables
if (Memory[roomName].links == null) {
Memory[roomName].links = {};
Memory[roomName].links.upgradeLink = -1;
Memory[roomName].links.storageLink = -1;
console.log("Created Memory for Links in " + roomName)
}
if (Memory[roomName].creeps == null) {
Memory[roomName].creeps = {};
console.log("Created Memory for creeps in " + roomName)
}
if (Memory[roomName].repairs == null) {
Memory[roomName].repairs = {}
//memory[roomName].repairs
this.updateRepairMem();
console.log("Created Memory for repairs in " + roomName)
}
if (Memory[roomName].storage == null) {
Memory[roomName].storage = {}
console.log("Created Memory for storage in " + roomName)
}
if (Memory[roomName].constructionSites == null) {
Memory[roomName].constructionSites = {}
console.log("Created Memory for constructionSites in " + roomName)
}
// console.log(Memory[roomName].constructionSites.all)
if (Memory[roomName].constructionSites.all == null) {
//console.log("no Construction sites")
}
if (Memory[roomName].terminal == null) {
Memory[roomName].terminal = {}
console.log("Created Memory for terminal in " + roomName)
}
//console.log("Num of spawns " + Memory[roomName].spawns)
//Memory[roomName].spawns = null
if (Memory[roomName].spawns == null) {
Memory[roomName].spawns = {};
var spawns = room.find(FIND_STRUCTURES).filter(structure => [STRUCTURE_SPAWN].indexOf(structure.structureType) !== -1);
Memory[roomName].spawns = spawns;
console.log("Created Memory for spawns in " + roomName)
}
if (Memory[roomName].sources == null) {
console.log(roomName + " Sources empty")
var sources = room.find(FIND_SOURCES)
Memory[roomName].sources = sources;
Memory[roomName].sources.total = sources.length;
console.log("Created Memory for sources in " + room)
}
}
}
,
createCreepMem: function () {
//outside
Memory.outside.creeps = {};
Memory.outside.creeps.total = 0;
//rooms
for (let roomName in Game.rooms) {
let room = Game.rooms[roomName];
Memory[roomName].creeps = {};
Memory[roomName].creeps.total = 0;
}
}
,
updateCreepMem: function () {
this.createCreepMem();
for (let creepName in Game.creeps) {
var creep = Game.creeps[creepName];
let role = creep.memory.role;
let team = creep.memory.team;
let type = role + "_" + team;
//console.log(creep.room.controller)
if (!creep.room.controller || !creep.room.controller.my) {
//console.log("outside");
if (!Memory.outside.creeps[type]) {
Memory.outside.creeps[type] = 1;
} else {
Memory.outside.creeps[type]++;
}
} else {
var room = creep.room.name;
//console.log(creep.room)
Memory[room].creeps.total++
if (!Memory[room].creeps[type]) {
Memory[room].creeps[type] = 1;
} else {
Memory[room].creeps[type]++;
}
}
}
//outside
//rooms
}
,
updateRepairMem: function (room) {
var roomName = room.name;
//find things to be repaired
var targets = room.find(FIND_STRUCTURES, {
filter: object => object.hits < object.hitsMax
|| (object.structureType == "constructedWall" && object.hits < 50000)
&& object.hits < 50000
});
//console.log(roomName + " repairs: " + targets)
if (targets.length < 1) {
//console.log("Nothing to repair in " + roomName);
} else {
//console.log("repairs" + targets);
//sorted least hits/hitsmax to most
targets.sort((a, b) => a.hitsMax - b.hitsMax);
//find just containers that need to be repaired
//var containers =
Memory[room.name].repairs.toBeRepaired = targets.filter(structure => structure.structureType != STRUCTURE_CONTAINER);
Memory[room.name].repairs.toBeRepairedContainers = targets.filter(structure => structure.structureType == STRUCTURE_CONTAINER);
}
}
,
updateConstructionMem: function (room) {
var roomName = room.name;
var targets = room.find(FIND_CONSTRUCTION_SITES);
Memory[roomName].constructionSites.all = targets;
/*for (var site in Memory[roomName].constructionSites.all) {
//console.log(site + " " + Memory[roomName].constructionSites.all[site])
if(Memory[roomName].constructionSites.all[site]){
var thisSite = Memory[roomName].constructionSites.all[site]
}
//console.log(!Game.getObjectById(thisSite.id));
//console.log(thisSite.progress == null)
if (!thisSite ||!Game.getObjectById(thisSite.id)|| thisSite.progress == null ) {
delete Memory[roomName].constructionSites.all;
console.log("deleting a construction site from memory")
}
}*/
}
,
getConstructionSites: function (room) {
var roomName = room.name;
var constructionSites = [];
for (var i in Memory[roomName].constructionSites.all) {
constructionSites.push(Game.getObjectById(Memory[roomName].constructionSites.all[i].id));
}
//console.log(roomName + " " + towers)
return constructionSites;
}
,
updateContainersMem: function (room) {
var storage = room.find(FIND_STRUCTURES, {
filter: (i) => (i.structureType == STRUCTURE_CONTAINER || i.structureType == STRUCTURE_STORAGE)
});
// console.log("Containers " + room.name + " " + storage)
var justContainers = [];
var justStorages = [];
for (var i = 0; i < storage.length; i++) {
if (storage[i].structureType === STRUCTURE_CONTAINER) {
justContainers.push(storage[i]);
} else if (storage[i].structureType === STRUCTURE_STORAGE) {
justStorages.push(storage[i]);
}
//console.log(justContainers);
//console.log(justStorage);
}
Memory[room.name].storage.containers = justContainers;
Memory[room.name].storage.storages = justStorages;
Memory[room.name].storage.total = storage.length;
}
,
updateTowerMem: function (room) {
var roomName = room.name;
var towers = room.find(FIND_STRUCTURES,
{
filter: (structure) => structure.structureType == STRUCTURE_TOWER
&& structure.store[RESOURCE_ENERGY] < structure.store.getCapacity(RESOURCE_ENERGY)
});
Memory[roomName].towers = towers;
}
,
updateLinkMem(room) {
var roomName = room.name
var links = room.find(FIND_STRUCTURES, { filter: (i) => i.structureType == STRUCTURE_LINK })
Memory[roomName].links.total = links.length
Memory[roomName].links.all = links;
}
,
clearMemory: function () {
//Consider using this method instead of resetting memory every mem update
for (var name in Memory.creeps) {
//clear dead creeps from memory
if (!Game.creeps[name]) {
delete Memory.creeps[name];
console.log('Clearing non-existing creep memory:', name);
}
}
//delete Memory.constructionSites;
}
,
updateSpawnMem(room) {
var roomName = room.name;
Memory[roomName].spawns = {};
var spawns = []
for (let spawnName in Game.spawns) {
//console.log(spawnName)
//check if spawn is in current room
if (Game.spawns[spawnName].room == room) {
spawns.push(Game.spawns[spawnName]);
}
}
Memory[roomName].spawns = spawns;
}
,
getNumCreeps: function (room, role) {
if (!room) {
if (Memory.outside.creeps[role]) {
//console.log("outside "+ role+" creeps " +Memory.outside.creeps[role])
return Memory.outside.creeps[role];
}
} else if (Memory[room.name].creeps[role]) {
//console.log(room.name)
return Memory[room.name].creeps[role];
}
return 0;
}
,
getTowers: function (room) {
var roomName = room.name;
var towers = [];
for (var i in Memory[roomName].towers) {
towers.push(Game.getObjectById(Memory[roomName].towers[i].id));
}
//console.log(roomName + " " + towers)
return towers;
}
,
setRoomRes: function (room) {
roomName = room.name
if (!Memory[roomName].terminal.roomRes) {
var minerals = room.find(FIND_MINERALS);
//console.log(minerals);
Memory[roomName].terminal.roomRes = minerals;
}
}
,
updateExtensions: function (room) {
let roomName = room.name;
var extensions = room.find(FIND_STRUCTURES).filter(i => i.structureType == STRUCTURE_EXTENSION).filter(i => i.store.getFreeCapacity(RESOURCE_ENERGY) > 0);
Memory[roomName].extensions = extensions;
}
,
getExtensions: function(room){
let roomName = room.name;
var extensions = [];
for(var i in Memory[roomName].extensions){
extensions.push(Game.getObjectById(Memory[roomName].extensions[i].id));
}
return extensions
}
};
module.exports = memMgr;