-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathCreep intent tracker.ts
241 lines (213 loc) · 5.55 KB
/
Creep intent tracker.ts
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// unfleshedone 3 June 2017 at 20:56
// ===================================================
// Usage:
// For telling right side of the creep what his left side is doing.
if (creep.intentTracker.canMove())
creep.move(...)
if (creep.intentTracker.canAttack())
creep.attack(...)
// ===================================================
// intent.tracker.d.ts
interface IIntentTracker
{
canMove(): boolean;
canHarvest(): boolean;
canAttack(): boolean;
canBuild(): boolean;
canRepair(): boolean;
canDismantle(): boolean;
canAttackController(): boolean;
canRangedHeal(): boolean;
canHeal(): boolean;
canRangedAttack(): boolean;
canRangedMassAttack(): boolean;
canUpgradeController(): boolean;
canClaimController(): boolean;
canTransfer(): boolean;
canWithdraw(): boolean;
canDrop(): boolean;
canPickup(): boolean;
}
interface Creep
{
readonly intentTracker: IIntentTracker;
}
// ===================================================
// intent.tracker.ts
enum Buckets
{
eMeleePipeline,
eRangedPipeline,
}
type HostType = Creep;
interface IIntent
{
args: any[];
}
export class IntentTracker implements IIntentTracker
{
public static WrapIntents(host: HostType)
{
if ((host as any)._intentsWrapped)
return;
(host as any)._intentsWrapped = true;
IntentTracker.WrapIntent(host, "harvest");
IntentTracker.WrapIntent(host, "attack");
IntentTracker.WrapIntent(host, "build");
IntentTracker.WrapIntent(host, "repair");
IntentTracker.WrapIntent(host, "dismantle");
IntentTracker.WrapIntent(host, "attackController");
IntentTracker.WrapIntent(host, "rangedHeal");
IntentTracker.WrapIntent(host, "heal");
IntentTracker.WrapIntent(host, "rangedAttack");
IntentTracker.WrapIntent(host, "rangedMassAttack");
IntentTracker.WrapIntent(host, "upgradeController");
IntentTracker.WrapIntent(host, "claimController");
IntentTracker.WrapIntent(host, "move");
IntentTracker.WrapIntent(host, "transfer");
IntentTracker.WrapIntent(host, "withdraw");
IntentTracker.WrapIntent(host, "drop");
IntentTracker.WrapIntent(host, "pickup");
}
private static WrapIntent(host: HostType, functionName: string)
{
const descriptor = Object.getOwnPropertyDescriptor(host, functionName);
if (!descriptor)
return;
const hasAccessor = descriptor.get || descriptor.set;
if (hasAccessor)
return;
const isFunction = typeof descriptor.value === "function";
if (!isFunction)
return;
const originalFunction = (host as any)[functionName];
let buckets = IntentTracker.Pipelines[functionName];
if (buckets === undefined)
buckets = [];
(host as any)[functionName] = function(this: any, ...args: any[])
{
const res = originalFunction.apply(this, args);
if (res === OK)
this.intentTracker.onIntent(functionName, buckets, args);
return res;
};
}
private static Pipelines: { [name: string]: Buckets[] } =
{
attack: [Buckets.eMeleePipeline],
harvest: [Buckets.eMeleePipeline],
build: [Buckets.eMeleePipeline, Buckets.eRangedPipeline],
repair: [Buckets.eMeleePipeline, Buckets.eRangedPipeline],
dismantle: [Buckets.eMeleePipeline],
attackController: [Buckets.eMeleePipeline],
rangedHeal: [Buckets.eMeleePipeline, Buckets.eRangedPipeline],
heal: [Buckets.eMeleePipeline],
rangedAttack: [Buckets.eRangedPipeline],
rangedMassAttack: [Buckets.eRangedPipeline],
};
private pipelines: Set<Buckets> = new Set();
private intents: Map<string, IIntent> = new Map();
public onIntent(name: string, buckets: Buckets[], args: any[])
{
this.intents.set(name, { args });
_.each(buckets, (b) => this.pipelines.add(b));
}
private checkIntent(name: string): boolean
{
const buckets = IntentTracker.Pipelines[name];
if (buckets === undefined)
return !this.intents.has(name);
else
return _.all(buckets, (b) => !this.pipelines.has(b));
}
public canMove(): boolean
{
return this.checkIntent("move");
}
public canHarvest(): boolean
{
return this.checkIntent("harvest");
}
public canTransfer(): boolean
{
return this.checkIntent("transfer");
}
public canAttack(): boolean
{
return this.checkIntent("attack");
}
public canBuild(): boolean
{
return this.checkIntent("build");
}
public canRepair(): boolean
{
return this.checkIntent("repair");
}
public canDismantle(): boolean
{
return this.checkIntent("dismantle");
}
public canAttackController(): boolean
{
return this.checkIntent("attackController");
}
public canRangedHeal(): boolean
{
return this.checkIntent("rangedHeal");
}
public canHeal(): boolean
{
return this.checkIntent("heal");
}
public canRangedAttack(): boolean
{
return this.checkIntent("rangedAttack");
}
public canRangedMassAttack(): boolean
{
return this.checkIntent("rangedMassAttack");
}
public canUpgradeController(): boolean
{
return this.checkIntent("upgradeController");
}
public canClaimController(): boolean
{
return this.checkIntent("claimController");
}
public canWithdraw(): boolean
{
return this.checkIntent("withdraw");
}
public canDrop(): boolean
{
return this.checkIntent("drop");
}
public canPickup(): boolean
{
return this.checkIntent("pickup");
}
}
// ===================================================
// main.ts
if (!Creep.prototype.hasOwnProperty("intentTracker"))
{
Object.defineProperty(Creep.prototype, "intentTracker",
{
get(this: any)
{
if (this.__intentTracker === undefined)
this.__intentTracker = new IntentTracker();
return this.__intentTracker;
},
configurable: false,
enumerable: false,
});
}
export function loop()
{
// run this before screeps-profiler wrap
IntentTracker.WrapIntents(Creep.prototype);
// main loop
}