-
Notifications
You must be signed in to change notification settings - Fork 6
/
triggers.js
503 lines (459 loc) · 11.9 KB
/
triggers.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
const conditionIds = Object.freeze({
NONE: 0,
COUNTDOWN_TIMER: 1,
COMMAND: 2,
BRING: 3,
ACCUMULATE: 4,
KILL: 5,
COMMAND_MOST: 6,
COMMAND_MOST_AT: 7,
MOST_KILLS: 8,
HIGHEST_SCORE: 9,
MOST_RESOURCES: 10,
SWITCH: 11,
ELAPSED_TIME: 12,
OPPONENTS: 14,
DEATHS: 15,
COMMAND_LEAST: 16,
COMMAND_LEAST_AT: 17,
LEAST_KILLS: 18,
LOWEST_SCORE: 19,
LEAST_RESOURCES: 20,
SCORE: 21,
ALWAYS: 22,
NEVER: 23,
})
const actionIds = Object.freeze({
NONE: 0,
VICTORY: 1,
DEFEAT: 2,
PRESERVE_TRIGGER: 3,
WAIT: 4,
PAUSE: 5,
UNPAUSE: 6,
TRANSMISSION: 7,
PLAY_WAV: 8,
DISPLAY_TEXT: 9,
CENTER_VIEW: 10,
CREATE_UNIT_WITH_PROPERTIES: 11,
SET_MISSION_OBJECTIVES: 12,
SET_SWITCH: 13,
SET_COUNTDOWN_TIMER: 14,
RUN_AI_SCRIPT: 15,
RUN_AI_SCRIPT_AT_LOCATION: 16,
LEADERBOARD_CONTROL: 17,
LEADERBOARD_CONTROL_AT_LOCATION: 18,
LEADERBOARD_RESOURCES: 19,
LEADERBOARD_KILLS: 20,
LEADERBOARD_POINTS: 21,
KILL_UNIT: 22,
KILL_UNIT_AT_LOCATION: 23,
REMOVE_UNIT: 24,
REMOVE_UNIT_AT_LOCATION: 25,
SET_RESOURCES: 26,
SET_SCORE: 27,
MINIMAP_PING: 28,
TALKING_PORTRAIT: 29,
MUTE_UNIT_SPEECH: 30,
UNMUTE_UNIT_SPEECH: 31,
LEADERBOARD_COMPUTERS: 32,
LEADERBOARD_GOAL_CONTROL: 33,
LEADERBOARD_GOAL_CONTROL_AT_LOCATION: 34,
LEADERBOARD_GOAL_RESOURCES: 35,
LEADERBOARD_GOAL_KILLS: 36,
LEADERBOARD_GOAL_POINTS: 37,
MOVE_LOCATION: 38,
MOVE_UNIT: 39,
LEADERBOARD_GREED: 40,
SET_NEXT_SCENARIO: 41,
SET_DOODAD_STATE: 42,
SET_INVINCIBILITY: 43,
CREATE_UNIT: 44,
SET_DEATHS: 45,
ISSUE_ORDER: 46,
COMMENT: 47,
GIVE_UNIT: 48,
SET_UNIT_HP: 49,
SET_UNIT_ENERGY: 50,
SET_UNIT_SHIELDS: 51,
SET_UNIT_RESOURCE: 52,
SET_UNIT_HANGAR: 53,
PAUSE_COUNTDOWN_TIMER: 54,
UNPAUSE_COUNTDOWN_TIMER: 55,
DRAW: 56,
SET_ALLIANCE: 57,
DISABLE_DEBUG: 58,
ENABLE_DEBUG: 59,
})
const CONDITION_PARAM_BITS = [
// Location 0x1, player 0x2, amount/comparision 0x4, unit 0x8,
// switch 0x10, resource 0x20, score 0x40
0x4, // COUNTDOWN_TIMER
0xe, // COMMAND
0xf, // BRING
0x26, // ACCUMULATE
0xe, // KILL
0x8, // COMMAND_MOST (Most / least conditions always are "Current player", no player can be set)
0x9, // COMMAND_MOST_AT
0x8, // MOST_KILLS
0x40, // HIGHEST_SCORE
0x20, // MOST_RESOURCES
0x10, // SWITCH
0x4, // ELAPSED_TIME
0x0, // MISSION_BRIEFING_ALWAYS
0x6, // OPPONENTS
0xe, // DEATHS
0x8, // COMMAND_LEAST
0x9, // COMMAND_LEAST_AT
0x8, // LEAST_KILLS
0x40, // LOWEST_SCORE
0x20, // LEAST_RESOURCES
0x46, // SCORE
]
const ACTION_PARAM_BITS = [
// Location 0x1, dest location 0x2, player 0x4, dest player 0x8,
// time 0x10, modifier 0x20, unit 0x40, order 0x80,
// alliance 0x100, switch 0x200, resource 0x400, score 0x800,
// amount (countdown time) 0x1000, amount (general) 0x2000, unit amount 0x4000,
// ai script 0x8000, text 0x10000, sound file 0x20000, state 0x40000,
// moved location 0x80000, always display 0x100000
0x0, // VICTORY
0x0, // DEFEAT
0x0, // PRESERVE_TRIGGER
0x10, // WAIT
0x0, // PAUSE
0x0, // UNPAUSE
0x132071, // TRANSMISSION
0x20010, // PLAY_WAV
0x110000, // DISPLAY_TEXT
0x1, // CENTER_VIEW
0x4045, // CREATE_UNIT_WITH_PROPERTIES
0x10000, // SET_MISSION_OBJECTIVES
0x200, // SET_SWITCH
0x1020, // SET_COUNTDOWN_TIMER
0x8000, // RUN_AI_SCRIPT
0x8001, // RUN_AI_SCRIPT_AT_LOCATION
0x10040, // LEADERBOARD_CONTROL
0x10041, // LEADERBOARD_CONTROL_AT_LOCATION
0x10400, // LEADERBOARD_RESOURCES
0x10040, // LEADERBOARD_KILLS
0x10800, // LEADERBOARD_POINTS
0x44, // KILL_UNIT
0x4045, // KILL_UNIT_AT_LOCATION
0x44, // REMOVE_UNIT
0x4045, // REMOVE_UNIT_AT_LOCATION
0x2424, // SET_RESOURCES
0x2824, // SET_SCORE
0x1, // MINIMAP_PING
0x50, // TALKING_PORTRAIT
0x0, // MUTE_UNIT_SPEECH
0x0, // UNMUTE_UNIT_SPEECH
0x40000, // LEADERBOARD_COMPUTERS
0x12040, // LEADERBOARD_GOAL_CONTROL
0x12041, // LEADERBOARD_GOAL_CONTROL_AT_LOCATION
0x12400, // LEADERBOARD_GOAL_RESOURCES
0x12040, // LEADERBOARD_GOAL_KILLS
0x12800, // LEADERBOARD_GOAL_POINTS
0x80045, // MOVE_LOCATION
0x4047, // MOVE_UNIT
0x2000, // LEADERBOARD_GREED
0x10000, // SET_NEXT_SCENARIO
0x40045, // SET_DOODAD_STATE
0x40045, // SET_INVINCIBILITY
0x4045, // CREATE_UNIT
0x2064, // SET_DEATHS
0xc7, // ISSUE_ORDER
0x10000, // COMMENT
0x404d, // GIVE_UNIT
0x6045, // SET_UNIT_HP
0x6045, // SET_UNIT_ENERGY
0x6045, // SET_UNIT_SHIELDS
0x6005, // SET_UNIT_RESOURCE
0x6045, // SET_UNIT_HANGAR
0x0, // PAUSE_COUNTDOWN_TIMER
0x0, // UNPAUSE_COUNTDOWN_TIMER
0x0, // DRAW
0x104, // SET_ALLIANCE
]
const COMPARISION_TYPES = [
'AtLeast',
'AtMost',
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
'Exactly',
]
const MODIFIER_TYPES = [
'Set',
'Add',
'Subtract',
]
const UNIT_ORDERS = [
'Move',
'Patrol',
'Attack',
]
const ALLIANCE_STATES = [
'Enemy',
'Ally',
'AlliedVictory',
]
const CONDITION_SWITCH_STATES = [
'Set',
'Clear',
]
const ACTION_SWITCH_STATES = [
'Set',
'Clear',
'Toggle',
undefined,
undefined,
undefined,
undefined,
'Randomize',
]
const ACTION_STATES = [
'Set',
'Clear',
'Toggle',
]
const RESOURCE_TYPES = [
'Ore',
'Gas',
'OreAndGas',
]
const SCORE_TYPES = [
'Total',
'Units',
'Buildings',
'UnitsAndBuildings',
'Kills',
'Razings',
'KillsAndRazings',
'Custom',
]
class Triggers {
constructor(strings, buffer) {
this._buffer = buffer
this._strings = strings
}
[Symbol.iterator]() {
return this.iterateFrom(0)
}
iterateFrom(pos) {
return new TriggerIterator(this._strings, this._buffer, pos * 2400)
}
get size() {
return this._buffer.length / 2400
}
}
class TriggerIterator {
constructor(strings, buffer, pos) {
this._buffer = buffer
this._pos = pos
this._strings = strings
}
[Symbol.iterator]() {
return this
}
next() {
if (this._pos + 2400 > this._buffer.length) {
return { done: true }
}
const trigger = new Trigger(this._strings, this._buffer, this._pos)
this._pos += 2400
return {
value: trigger,
}
}
}
class Trigger {
constructor(strings, buffer, pos) {
this._buffer = buffer
this._pos = pos
this._strings = strings
}
players() {
let pos = this._pos + 2372
let result = []
for (let i = 0; i < 27; i++) {
if (this._buffer[pos + i] !== 0) {
result.push(i)
}
}
return result
}
conditions() {
return new ConditionIterator(this._buffer, this._pos, 0, false)
}
allConditions() {
return new ConditionIterator(this._buffer, this._pos, 0, true)
}
actions() {
return new ActionIterator(this._strings, this._buffer, this._pos + 320, 0, false)
}
allActions() {
return new ActionIterator(this._strings, this._buffer, this._pos + 320, 0, true)
}
rawByteView() {
return this._buffer.slice(this._pos, this._pos + 2400)
}
}
class ConditionIterator {
constructor(buffer, pos, index, alsoDisabled) {
this._buffer = buffer
this._pos = pos
this._index = index
this._alsoDisabled = alsoDisabled
}
[Symbol.iterator]() {
return this
}
next() {
for (;;) {
const pos = this._pos
if (this._index === 16 || this._buffer[pos + 15] === 0) {
return { done: true }
}
this._pos += 20
this._index += 1
if (!this._alsoDisabled && this._buffer[pos + 17] & 0x2 !== 0) {
continue
}
const condition = new TriggerCondition(this._buffer, pos)
return {
value: condition,
}
}
}
}
class TriggerCondition {
constructor(buffer, pos) {
this._buffer = buffer
this._pos = pos
}
id() {
return this._buffer[this._pos + 15]
}
isDisabled() {
return this._buffer[this._pos + 17] & 0x2 !== 0
}
params() {
const id = this.id()
const bits = id > 0 && id <= CONDITION_PARAM_BITS.length ? CONDITION_PARAM_BITS[id - 1] : 0
const buf = this._buffer
const pos = this._pos
return {
location: bits & 0x1 ? buf[pos + 0] - 1 : undefined,
player: bits & 0x2 ? buf.readUInt32LE(pos + 4) : undefined,
amount: bits & 0x4 ? buf.readUInt32LE(pos + 8) : undefined,
comparisionType: bits & 0x4 ? nameOrUnknown(COMPARISION_TYPES, buf[pos + 14]) : undefined,
unitId: bits & 0x8 ? buf.readUInt16LE(pos + 12) : undefined,
switchId: bits & 0x10 ? buf[pos + 16] : undefined,
switchState:
bits & 0x10 ? nameOrUnknown(CONDITION_SWITCH_STATES, buf[pos + 14] - 2) : undefined,
resourceType: bits & 0x20 ? nameOrUnknown(RESOURCE_TYPES, buf[pos + 16]) : undefined,
scoreType: bits & 0x40 ? nameOrUnknown(SCORE_TYPES, buf[pos + 16]) : undefined,
}
}
rawByteView() {
return this._buffer.slice(this._pos, this._pos + 20)
}
}
class ActionIterator {
constructor(strings, buffer, pos, index, alsoDisabled) {
this._buffer = buffer
this._pos = pos
this._index = index
this._alsoDisabled = alsoDisabled
this._strings = strings
}
[Symbol.iterator]() {
return this
}
next() {
for (;;) {
const pos = this._pos
if (this._index === 64 || this._buffer[pos + 26] === 0) {
return { done: true }
}
this._pos += 32
this._index += 1
if (!this._alsoDisabled && this._buffer[pos + 28] & 0x2 !== 0) {
continue
}
const action = new TriggerAction(this._buffer, pos, this._strings)
return {
value: action,
}
}
}
}
class TriggerAction {
constructor(buffer, pos, strings) {
this._buffer = buffer
this._pos = pos
this._strings = strings
}
id() {
return this._buffer[this._pos + 26]
}
isDisabled() {
return this._buffer[this._pos + 28] & 0x2 !== 0
}
params() {
const id = this.id()
const bits = id > 0 && id <= ACTION_PARAM_BITS.length ? ACTION_PARAM_BITS[id - 1] : 0
const buf = this._buffer
const pos = this._pos
let amount = undefined
if (bits & 0x1000) {
amount = buf.readUInt32LE(pos + 12)
} else if (bits & 0x2000) {
amount = buf.readUInt32LE(pos + 20)
}
return {
location: bits & 0x1 ? buf[pos + 0] - 1 : undefined,
destLocation: bits & 0x2 ? buf[pos + 20] - 1 : undefined,
movedLocation: bits & 0x80000 ? buf[pos + 20] - 1 : undefined,
player: bits & 0x4 ? buf.readUInt32LE(pos + 16) : undefined,
destPlayer: bits & 0x8 ? buf.readUInt32LE(pos + 20) : undefined,
time: bits & 0x10 ? buf.readUInt32LE(pos + 12) : undefined,
modifierType: bits & 0x20 ? nameOrUnknown(MODIFIER_TYPES, buf[pos + 27] - 7) : undefined,
unitId: bits & 0x40 ? buf.readUInt16LE(pos + 24) : undefined,
unitOrder: bits & 0x80 ? nameOrUnknown(UNIT_ORDERS, buf[pos + 27]) : undefined,
unitAmount: bits & 0x4000 ? buf[pos + 27] : undefined,
allianceStatus:
bits & 0x100 ? nameOrUnknown(ALLIANCE_STATES, buf.readUInt16LE(pos + 24)) : undefined,
switchId: bits & 0x200 ? buf.readUInt32LE(pos + 20) : undefined,
switchState: bits & 0x200 ? nameOrUnknown(ACTION_SWITCH_STATES, buf[pos + 27] - 4) : undefined,
resourceType:
bits & 0x400 ? nameOrUnknown(RESOURCE_TYPES, buf.readUInt16LE(pos + 24)) : undefined,
scoreType:
bits & 0x800 ? nameOrUnknown(SCORE_TYPES, buf.readUInt16LE(pos + 24)) : undefined,
aiScript: bits & 0x8000 ? buf.readUInt32BE(pos + 20) : undefined,
alwaysDisplay: bits & 0x100000 ? (buf[pos + 28] & 0x4) !== 0 : undefined,
text: bits & 0x10000 ? this._strings.get(buf.readUInt32LE(pos + 4)) : undefined,
soundFile: bits & 0x20000 ? this._strings.get(buf.readUInt32LE(pos + 8)) : undefined,
state: bits & 0x40000 ? nameOrUnknown(ACTION_STATES, buf[pos + 27] - 4) : undefined,
amount,
}
}
rawByteView() {
return this._buffer.slice(this._pos, this._pos + 20)
}
}
function nameOrUnknown(names, index) {
const name = names[index]
return name === undefined ? 'Unknown' : name
}
module.exports = {
Triggers,
actionIds,
conditionIds,
}