-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrooms.js
362 lines (337 loc) · 11.5 KB
/
rooms.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
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
/**
* Functions on Game.rooms
*/
let objects = require('./objects');
let Path = require('./path');
module.exports =
{
/**
* @returns number
*/
get length()
{
if (this.length_ === undefined) {
this.length_ = Object.keys(Game.rooms).length;
}
return this.length_;
}
};
/**
* Changes an object to something we can store in memory : only its id and position
*
* @var object RoomObject
* @return object { creep, id, x, y }
*/
let toMemoryObject = function(object)
{
let result = { x: object.pos.x, y: object.pos.y };
if (object.creep) {
result.creep = object.name;
}
else if (object.id) {
result.id = object.id;
}
return result;
};
module.exports.DEBUG = false;
module.exports.MEMORY = 'MEMORY';
/**
* Rooms cache (filled-in from memory only when needed)
*/
module.exports.rooms = {};
/**
* Copy creep name into all room_roles having the same role if at the same position
*
* @example source_roole_role = source_harvester : source_harvester.creep -> controller_harvester.creep
* @param room Room|string
* @param source_room_role string
*/
module.exports.copyRoleCreep = function(room, source_room_role)
{
let room_name = (room instanceof Room) ? room.name : room;
let source_role = Memory.rooms[room_name][source_room_role].role;
let source_pos = this.getPos(room, source_room_role);
if (source_role) {
for (let room_role in Memory.rooms[room_name]) if (Memory.rooms[room_name].hasOwnProperty(room_role)) {
let object = Memory.rooms[room_name][room_role];
let pos = this.getPos(room, room_role);
if ((object.role === source_role) && !(pos.x - source_pos.x) && !(pos.y - source_pos.y)) {
Memory.rooms[room_name][room_role].creep = Memory.rooms[room_name][source_room_role].creep;
}
}
}
};
/**
* @param callback callable
* @returns Creep[]
*/
module.exports.filter = function(callback)
{
return _.filter(Game.rooms, callback);
};
/**
* @param callback
* @param [thisArg] object
*/
module.exports.forEach = function(callback, thisArg)
{
if (thisArg === undefined) thisArg = this;
for (let room_name in Game.rooms) if (Game.rooms.hasOwnProperty(room_name)) {
if (callback.call(thisArg, Game.rooms[room_name], room_name, Game.rooms)) break;
}
};
/**
* Gets an object from memory (use cache)
*
* @param room Room|string
* @param object_name string eg controller, controller_source, spawn, spawn_source
* @param [property] string if set, the property to read from memory
* @return RoomObject|RoomPosition
*/
module.exports.get = function(room, object_name, property)
{
let room_name = (room instanceof Room) ? room.name : room;
if (!Memory.rooms[room_name]) Memory.rooms[room_name] = {};
if (property === this.MEMORY) {
return Memory.rooms[room_name][object_name];
}
if (property) {
return Memory.rooms[room_name][object_name] ? Memory.rooms[room_name][object_name][property] : null;
}
if (this.rooms[room_name] === undefined) {
this.rooms[room_name] = {};
}
if (this.rooms[room_name][object_name] === undefined) {
if (Memory.rooms[room_name][object_name] && Memory.rooms[room_name][object_name].creep) {
this.rooms[room_name][object_name] = Game.creeps[Memory.rooms[room_name][object_name].creep];
}
else if (Memory.rooms[room_name][object_name] && Memory.rooms[room_name][object_name].id) {
this.rooms[room_name][object_name] = Game.getObjectById(Memory.rooms[room_name][object_name].id);
}
else if (Memory.rooms[room_name][object_name]) {
this.rooms[room_name][object_name] = Game.rooms[room_name].getPositionAt(
Memory.rooms[room_name][object_name].x,
Memory.rooms[room_name][object_name].y
);
}
else {
this.rooms[room_name][object_name] = null;
}
}
return this.rooms[room_name][object_name];
};
/**
* Gets the position (x, y only) where the object is assigned to
*
* @param room Room|string
* @param object_name string
* @returns {{ x: number, y: number }}
*/
module.exports.getPos = function(room, object_name)
{
let room_name = (room instanceof Room) ? room.name : room;
let pos = Memory.rooms[room_name][object_name];
if (pos.path && ((pos.x === undefined) || (pos.y === undefined))) pos = Path.start(pos.path);
return pos;
};
/**
* Gets the room position where the object is assigned to
*
* @param room Room|string
* @param object_name string
* @return RoomPosition
*/
module.exports.getRoomPosition = function(room, object_name)
{
if (!(room instanceof Room)) room = Game.rooms[room];
let pos = this.getPos(room, object_name);
return room.getPositionAt(pos.x, pos.y);
};
/**
* Returns true if the room has an existing creep affected to the object name
*
* @param room Room|string
* @param object_name string @example spawn_harvester
* @returns boolean
*/
module.exports.has = function(room, object_name)
{
let room_name = (room instanceof Room) ? room.name : room;
return !!(Memory.rooms[room_name][object_name] && Memory.rooms[room_name][object_name].creep);
};
/**
* Refresh rooms memory :
* - get new rooms main information
* - remove lost rooms
*
* @param reset boolean if true, resets all rooms data (use this when you build a new structure in a room)
*/
module.exports.memorize = function(reset)
{
if (reset ||!Memory.rooms) {
Memory.rooms = {};
}
// memorize new rooms
this.forEach(function(room) {
Path.room = room;
if (!Memory.rooms[room.name]) {
let save_cost = Path.cost(1, 1, 1);
let cache = {};
let links = [];
let memory = {};
room.find(FIND_MY_STRUCTURES).forEach(function(structure) {
switch (structure.structureType) {
case STRUCTURE_CONTROLLER: memory.controller = toMemoryObject(cache.controller = structure); break;
case STRUCTURE_LINK: links.push(structure); break;
case STRUCTURE_SPAWN: memory.spawn = toMemoryObject(cache.spawn = structure); break;
}
});
// spawn information
if (cache.spawn) {
cache.spawn_source = cache.spawn.pos.findClosestByRange(FIND_SOURCES_ACTIVE);
memory.spawn_source = toMemoryObject(cache.spawn_source);
memory.spawn_carrier = { path: Path.calculate(cache.spawn_source, cache.spawn, { caption: 'spawn_carrier', range: 1 }) };
memory.spawn_harvester = Path.start(memory.spawn_carrier.path);
memory.spawn_harvester.role = 'harvester';
memory.spawn_harvester.source = memory.spawn_source.id;
cache.spawn_harvester = Path.toRoomPosition(memory.spawn_harvester);
}
// controller information
if (cache.controller) {
cache.controller_source = cache.controller.pos.findClosestByRange(FIND_SOURCES_ACTIVE);
memory.controller_source = toMemoryObject(cache.controller_source);
// controller source is the same than spawn source
if (cache.controller_source.id === cache.spawn_source.id) {
cache.controller_harvester = cache.spawn_harvester;
memory.controller_harvester = memory.spawn_harvester;
memory.controller_carrier = {
path: Path.shift(
Path.calculate(cache.spawn_harvester, cache.controller, { caption: 'controller_carrier.same', range: 1 }), memory.controller_harvester
)
};
}
// controller source is another one than the spawn source
else {
memory.controller_carrier = {
path: Path.calculate(cache.controller_source, cache.controller, { caption: 'controller_carrier.another', range: 1 })
};
memory.controller_harvester = Path.start(memory.controller_carrier.path);
cache.controller_harvester = Path.toRoomPosition(memory.controller_harvester);
}
memory.controller_harvester.role = 'harvester';
memory.controller_harvester.source = memory.controller_source.id;
memory.controller_upgrader = Path.last(memory.controller_carrier.path);
cache.controller_upgrader = Path.toRoomPosition(memory.controller_upgrader);
memory.controller_upgrader.role = 'upgrader';
memory.controller_upgrader.target = 'controller';
}
// finalise carriers : remove creeps position from carriers paths
memory.spawn_carrier = {
path: Path.calculateTwoWay(cache.spawn_harvester, cache.spawn, { caption: 'spawn_carrier', range : 1 }),
role: 'carrier',
source: FIND_DROPPED_ENERGY,
target: STRUCTURE_SPAWN
};
memory.controller_carrier = {
path: Path.calculateTwoWay(cache.controller_harvester, cache.controller_upgrader, { caption: 'controller_carrier', range: 1 }),
role: 'carrier',
source: FIND_DROPPED_ENERGY,
target: 'controller_upgrader'
};
// links
for (let link of links) {
if (objects.range(link, room.storage) <= 2) memory.storage_link = link.id;
else memory.source_link = link.id;
}
this.rooms[room.name] = cache;
Memory.rooms[room.name] = memory;
Path.cost(save_cost);
}
});
// re-affect existing creeps
if (reset) {
for (let creep_name in Game.creeps) if (Game.creeps.hasOwnProperty(creep_name)) {
let creep = Memory.creeps[creep_name];
if (creep.room && creep.room_role) {
Memory.rooms[creep.room][creep.room_role].creep = creep_name;
}
}
}
// remove lost rooms
for (let room_name in Memory.rooms) if (Memory.rooms.hasOwnProperty(room_name)) {
if (!Game.rooms[room_name]) {
delete Memory.rooms[room_name];
}
}
};
/**
* Returns true if rooms is memorized
*
* @returns boolean
*/
module.exports.memorized = function()
{
return Memory.rooms !== undefined;
};
/**
* Returns the name of the room where the RoomObject / RoomPosition / object id is
*
* @param object RoomObject|RoomPosition|string an object or object id
* @return string|null
*/
module.exports.nameOf = function(object)
{
if (typeof object === 'string') object = Game.getObjectById(object);
if (object instanceof RoomObject) return object.room.name;
if (object instanceof RoomPosition) return object.roomName;
return null;
};
/**
* Returns the room where the RoomObject / RoomPosition / object id is
*
* @param object RoomObject|RoomPosition|string an object or object id
* @return Room
*/
module.exports.of = function(object)
{
if (typeof object === 'string') object = Game.getObjectById(object);
if (object instanceof RoomObject) return object.room;
if (object instanceof RoomPosition) return Game.rooms[object.roomName];
return null;
};
/**
* Remove creep name from all room_roles having the same role if at the same position
*
* @example source_roole_role = source_harvester : source_harvester.creep -> controller_harvester.creep
* @param room Room|string
* @param source_room_role string
*/
module.exports.removeRoleCreep = function(room, source_room_role)
{
let room_name = (room instanceof Room) ? room.name : room;
let source_role = Memory.rooms[room_name][source_room_role].role;
let source_pos = this.getPos(room, source_room_role);
if (source_role) {
for (let room_role in Memory.rooms[room_name]) if (Memory.rooms[room_name].hasOwnProperty(room_role)) {
let object = Memory.rooms[room_name][room_role];
let pos = this.getPos(room, room_role);
if ((object.role === source_role) && !(pos.x - source_pos.x) && !(pos.y - source_pos.y)) {
delete Memory.rooms[room_name][room_role].creep;
}
}
}
};
/**
* Sets a creep object
*
* @param room Room|string
* @param object_name string @values controller_harvester, controller_upgrader, spawn_harvester
* @param creep Creep
*/
module.exports.setCreep = function(room, object_name, creep)
{
let room_name = (room instanceof Room) ? room.name : room;
Memory.rooms[room_name][object_name].creep = creep.name;
this.copyRoleCreep(room, object_name, Memory.rooms[room_name][object_name].role);
this.rooms[room_name][object_name] = creep;
};