forked from screepers/screeps-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOwnedStructure Memory.js
37 lines (34 loc) · 1.09 KB
/
OwnedStructure 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
// warinternal 27 December 2016 at 18:51
// This is called during global reset to set up structure memory,
// because it doesn't need to be called often.
if (!Memory.structures) {
console.log("[Memory] Initializing structure memory");
Memory.structures = {};
}
// Adds structure memory to OwnedStructure things.
// Easier to reason about garbage collection in this implementation.
Object.defineProperty(OwnedStructure.prototype, "memory", {
get: function() {
if (!Memory.structures[this.id]) Memory.structures[this.id] = {};
return Memory.structures[this.id];
},
set: function(v) {
return _.set(Memory, "structures." + this.id, v);
},
configurable: true,
enumerable: false
});
// Call this periodically to garbage collect structure memory
// (I find once every 10k ticks is fine)
global.GCStructureMemory = function() {
for (var id in Memory.structures)
if (!Game.structures[id]) {
console.log(
"Garbage collecting structure " +
id +
", " +
JSON.stringify(Memory.structures[id])
);
delete Memory.structures[id];
}
};