-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathextensions.js
91 lines (91 loc) · 3.36 KB
/
extensions.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
var mod = {
extend: function(){
Object.defineProperty(Structure.prototype, 'memory', {
configurable: true,
get: function() {
if(_.isUndefined(Memory.structures)) {
Memory.structures = {};
}
if(!_.isObject(Memory.structures)) {
return undefined;
}
return Memory.structures[this.id] = Memory.structures[this.id] || {};
},
set: function(value) {
if(_.isUndefined(Memory.structures)) {
Memory.structures = {};
}
if(!_.isObject(Memory.structures)) {
throw new Error('Could not set memory extension for structures');
}
Memory.structures[this.id] = value;
}
});
Object.defineProperty(Structure.prototype, 'creeps', {
// TODO: persist target assignments not only to creeps but also to targets. also not only as creepId but with activity
configurable: true,
get: function() {
if(_.isUndefined(this._creeps)) {
this._creeps = [];
}
return this._creeps;
},
set: function(value) {
this._creeps = value;
}
});
Object.defineProperty(Structure.prototype, 'towers', {
configurable: true,
get: function() {
if(_.isUndefined(this._towers)) {
this._towers = [];
}
return this._towers;
},
set: function(value) {
this._towers = value;
}
});
Object.defineProperty(Source.prototype, 'memory', {
configurable: true,
get: function() {
if(_.isUndefined(Memory.sources)) {
Memory.sources = {};
}
if(!_.isObject(Memory.sources)) {
return undefined;
}
return Memory.sources[this.id] = Memory.sources[this.id] || {};
},
set: function(value) {
if(_.isUndefined(Memory.sources)) {
Memory.sources = {};
}
if(!_.isObject(Memory.sources)) {
throw new Error('Could not set memory extension for sources');
}
Memory.sources[this.id] = value;
}
});
Object.defineProperty(Source.prototype, 'accessibleFields', {
configurable: true,
get: function() {
if( _.isUndefined(this.memory.accessibleFields) ) {
var fields = this.room.lookForAtArea(LOOK_TERRAIN, this.pos.y-1, this.pos.x-1, this.pos.y+1, this.pos.x+1, true);
this.memory.accessibleFields = 9-_.countBy( fields , "terrain" ).wall;
}
return this.memory.accessibleFields;
}
});
Object.defineProperty(StructureStorage.prototype, 'sum', {
configurable: true,
get: function() {
if( _.isUndefined(this._sum) ) {
this._sum = _.sum(this.store);
}
return this._sum;
}
});
}
}
module.exports = mod;