-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprototype.structures.js
33 lines (31 loc) · 1.08 KB
/
prototype.structures.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
// 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];
}
}
}