-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprototype.sources.js
50 lines (47 loc) · 1.71 KB
/
prototype.sources.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
// This is called during global reset to set up source memory,
// because it doesn't need to be called often.
if(!Memory.sources) {
console.log('[Memory] Initializing source memory');
Memory.sources = {};
}
// Adds source memory to Source things.
// Easier to reason about garbage collection in this implementation.
Object.defineProperty(Source.prototype, "memory", {
get: function () {
if(!Memory.rooms[this.room.name].sources) {
Memory.rooms[this.room.name].sources = {}
}
if(!Memory.rooms[this.room.name].sources[this.id]) {
Memory.rooms[this.room.name].sources[this.id] = {};
}
return Memory.rooms[this.room.name].sources[this.id];
},
set: function(v) {
return _.set(Memory, 'sources.' + this.room.name + '.sources.' + this.id, v);
},
configurable: true,
enumerable: false
});
// Add an miner
Object.defineProperty(Source.prototype, "miner", {
get: function() {
if (!Memory.rooms[this.room.name].sources[this.id].miner) {
Memory.rooms[this.room.name].sources[this.id].miner = null;
}
return Memory.rooms[this.room.name].sources[this.id].miner;
},
set: function(v) {
return _.set(Memory, 'sources.' + this.room.name + '.sources.' + this.id + '.miner', v);
},
configurable: true,
enumerable: false
});
// Call this periodically to garbage collect source memory
// (I find once every 10k ticks is fine)
global.GCSourceMemory = function() {
for (var id in Memory.sources )
if(!Game.sources[this.room.name][id]) {
console.log("Garbage collecting source " + id + ', ' + JSON.stringify(Memory.sources[id]));
delete Memory.sources[id];
}
}