forked from VladThePaler/screeps.behaviour-action-pattern
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrategy.js
170 lines (152 loc) · 5.68 KB
/
strategy.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
"use strict";
const mod = {};
module.exports = mod;
mod.decorateAgent = function(prototype, ...definitions) {
if (!prototype.customStrategy) prototype.customStrategy = function(ids) {};
if (!prototype.staticCustomStrategy) prototype.staticCustomStrategy = function(ids) {};
prototype.getStrategyHandler = function(ids, method, ...args) {
const currentStrategy = this.currentStrategy || this.strategy(ids);
const returnValOrMethod = currentStrategy[method];
const strategyKey = currentStrategy.key;
const strategyName = currentStrategy.name;
if (DEBUG && TRACE) trace('Strategy', {agent:this.name, strategyKey, strategyName, method});
if (returnValOrMethod === undefined) {
logError('strategy handler returned undefined', {agent: this.name || this.id, strategyKey, strategyName, method, stack: new Error().stack});
return;
}
if (args.length === 0) {
return returnValOrMethod;
}
const returnVal = returnValOrMethod.apply(this.currentStrategy, args);
if (returnVal !== undefined) {
return returnVal;
}
logError('handler returned undefined for args', {agent: this.name || this.id, strategyKey, strategyName, method, args:args.toString(), stack: new Error().stack});
};
prototype._strategyCache = {};
prototype.strategyKey = function(ids) {
const key = [];
for (let i = definitions.length - 1; i >= 0; i--) {
if( ids[i]) key[i] = ids[i];
else key[i] = definitions[i].default(this);
}
return key;
};
prototype.selectClient = function(ids, index) {
return ids[index] && definitions[index].select(ids[index]);
};
prototype.strategy = function(ids) {
const key = this.strategyKey(ids);
let strategy = mod.getCachedStrategy(this, key);
if (strategy) {
return mod.customizeStrategy(this, key, strategy);
}
strategy = mod.buildStrategy(key,
mod.strategyChainUtils,
definitions,
this.staticCustomStrategy.apply(this, key)
);
if (!strategy) {
logError('no strategy', {agent: this.name || this.id, key});
return {};
}
mod.putCachedStrategy(this, key, strategy);
return mod.customizeStrategy(this, key, strategy);
};
// Explain current activity
prototype.explain = function() {
const strategyKey = this.strategyKey([]);
let explained = this.toString() + ': ';
if (this.explainAgent) {
explained += this.explainAgent() + ' ';
}
explained += `assigned:[${strategyKey}]`;
for (let i = 0; i < strategyKey.length; i++) {
const client = this.selectClient(i);
if (client && client.explain) {
explained += `\n\t${strategyKey[i]}: ${client.explain(this)}`;
}
}
return explained;
};
};
// agent will prefer this strategy until it is free'd
mod.allocateStrategy = function(agent, ...definitions) {
agent.currentStrategy = agent.strategy.apply(agent, definitions);
};
mod.freeStrategy = function(agent) {
mod.freeStrategyChain(agent.currentStrategy);
delete agent.currentStrategy;
};
mod.buildStrategy = function(key, utils, definitions, custom) {
const currentStrategy = {key,name:[]};
mod.appendStrategies(currentStrategy, undefined, [utils]);
let head;
for (let i = 0; i < definitions.length; i++) {
const id = key[i];
const selector = id && definitions[i].selector(id);
const strategies = selector && selector.selectStrategies && selector.selectStrategies.apply(selector, key);
head = mod.appendStrategies(currentStrategy, head, strategies);
}
if (custom) {
head = mod.appendStrategies(currentStrategy, head, [custom]);
}
if (head) {
return currentStrategy;
}
};
mod.appendStrategies = function(currentStrategy, head, strategies) {
if (!strategies) return head;
for (let i = 0; i < strategies.length; i++) {
const strategy = strategies[i];
if (strategy) {
head = strategy;
_.assign(currentStrategy, strategy, function(objectValue, sourceValue, key) {
if (key === 'name') {
objectValue.push(sourceValue);
return objectValue;
} else {
return sourceValue;
}
});
}
}
return head;
};
mod.freeStrategyChain = function(chain) {
// used to clean prototype changes (feature in performance testing)
};
mod.customizeStrategy = function(agent, key, cachedStrategy) {
const customStrategy = agent.customStrategy.apply(agent, key);
if (!customStrategy) return cachedStrategy;
return _.assign({}, cachedStrategy, customStrategy, function(objectValue, sourceValue, key) {
if (key === 'name') {
if (Array.isArray(sourceValue)) {
return sourceValue.slice(0);
} else {
objectValue.push(sourceValue);
return objectValue;
}
} else {
return sourceValue;
}
});
};
mod.strategyChainUtils = {
toString: function() {
const returnVal = this.name.toString();
mod.freeStrategy(this);
return returnVal;
},
[Symbol.toPrimitive]: function() {
return this.toString();
},
};
// TODO NEED cache invalidation
mod.getCachedStrategy = function(agent, key) {
return _.get(agent._strategyCache, key);
};
mod.putCachedStrategy = function(agent, key, strategy) {
Object.freeze(strategy);
_.set(agent._strategyCache, key, strategy);
};