/
flowTypes.ts
552 lines (489 loc) · 13.5 KB
/
flowTypes.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
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
import {
FlowObjectProps,
TemplateProps,
Id,
ConnectionProps,
PinProps,
PinnedObjectProps,
ScriptNodeProps,
JumpProps,
DialogueFragmentProps,
FlowFragmentProps,
DialogueProps,
} from './json';
import { Database, ArticyType } from './database';
import { ArticyCreatorArguments } from './object';
import { runScript } from './script';
import { ArticyObject, Entity } from './types';
import { VariableStore } from './variables';
import { VisitSet } from './iterator';
/**
* Passed to methods like [[BaseFlowNode.next]], [[BaseFlowNode.numBranches]], and [[BaseFlowNode.execute]] to provide necessary context for execution calculations.
*/
export type ExecuteContext = {
/** Current variable store */
variables: VariableStore;
/** Current node visit count/index information */
visits: VisitSet;
};
/**
* Base class for all flow nodes
* @typeparam PropertiesType Property block interface
* @typeparam TemplateType Template block interface
* @category Flow Types
*/
export class BaseFlowNode<
PropertiesType extends FlowObjectProps = FlowObjectProps,
TemplateType extends TemplateProps = TemplateProps
> extends ArticyObject<PropertiesType, TemplateType> {
/**
* Returns the next node along the given branch index
* @param branchIndex Branch index to follow (-1 means default)
* @param last Id of the previous node (or null if none)
* @returns The next node along the branch (or undefined) and any Incoming/Outgoing Connection that was used to get there.
*/
next(
_context: ExecuteContext,
_branchIndex: number,
_last: Id | null,
_shadowing: boolean
): [BaseFlowNode | undefined, ConnectionProps | undefined] {
return [undefined, undefined];
}
/**
* Returns the number of valid branches at this node
* @param last Id of the node we were at least (or null if none)
* @returns Number of valid branches (so, not branches with failing conditions)
*/
numBranches(
_context: ExecuteContext,
_last: Id | null,
_shadowing: boolean
): number {
return 0;
}
/**
* Return true if this node might modify the variable state during a call to next()
*/
needsShadow(): boolean {
return false;
}
/**
* Executes scripts on the node (should do the same as next without the iteration advancement)
*/
execute(_context: ExecuteContext): void {
// do nothing
}
/**
* If this returns something other than undefined, then all node Ids returned will be counted as visited at the same time as this node
* @param _context Execution context
* @returns Id(s) to visit
*/
visits(_context: ExecuteContext): Id | Id[] | undefined {
return undefined;
}
}
function nextFromConnections(
db: Database,
branchIndex: number,
connections: ConnectionProps[]
): [BaseFlowNode | undefined, ConnectionProps | undefined] {
// If we have no connections, that's the end
if (connections.length === 0) {
return [undefined, undefined];
}
// No simple movement
if (branchIndex === -1 && connections.length > 1) {
return [undefined, undefined];
}
// Make sure it's in bounds
if (branchIndex < 0) {
branchIndex = 0;
}
// Bounds check
if (branchIndex >= connections.length) {
return [undefined, undefined];
}
// Go to target pin
return [
db.getObject(connections[branchIndex].TargetPin, BaseFlowNode),
connections[branchIndex],
];
}
/**
* Base class for all Flow Pins (see [[InputPin]] and [[OutputPin]]).
*/
export class BasePin extends BaseFlowNode<PinProps> {
private _connections: ConnectionProps[] | undefined;
/**
* Returns all Incoming or Outgoing connections to/from this pin.
* @returns List of connections
*/
public connections(): ConnectionProps[] {
if (this._connections) {
return this._connections;
}
// Create and sort connection list
this._connections = [...(this.properties.Connections ?? [])];
this._connections.sort((a, b) => {
const lnode = this.db.getModel<FlowObjectProps>(a.Target)?.Properties;
const rnode = this.db.getModel<FlowObjectProps>(b.Target)?.Properties;
return (lnode?.Position.y ?? 0) - (rnode?.Position.y ?? 0);
});
return this._connections;
}
}
/**
* An output pin with an optional script
*/
@ArticyType('OutputPin')
export class OutputPin extends BasePin {
next(
context: ExecuteContext,
branchIndex: number,
_last: Id | null,
shadowing: boolean
): [BaseFlowNode | undefined, ConnectionProps | undefined] {
// Evaluate instructions
runScript(
this.properties.Text,
context.variables,
context.visits,
this.id,
this.db,
false,
shadowing
);
// Return the appropriate connection
return nextFromConnections(this.db, branchIndex, this.connections());
}
numBranches(): number {
return this.connections().length;
}
needsShadow(): boolean {
return this.properties.Text.length > 0;
}
execute(context: ExecuteContext): void {
runScript(
this.properties.Text,
context.variables,
context.visits,
this.id,
this.db,
false,
false
);
}
}
/**
* An input pin with an optional condition
*/
@ArticyType('InputPin')
export class InputPin extends BasePin {
next(): [BaseFlowNode | undefined, ConnectionProps | undefined] {
return [this.db.getObject(this.properties.Owner, BaseFlowNode), undefined];
}
numBranches(
context: ExecuteContext,
_last: Id | null,
shadowing: boolean
): number {
// No branches if the script fails. Dead end.
if (
!runScript(
this.properties.Text,
context.variables,
context.visits,
this.id,
this.db,
true,
shadowing
)
) {
return 0;
}
// Otherwise always 1: returning to our owner
return 1;
}
execute(context: ExecuteContext): void {
runScript(
this.properties.Text,
context.variables,
context.visits,
this.id,
this.db,
true,
false
);
}
}
/**
* Base class for all flow nodes that have input and output pins
* @typeparam PropertiesType Property block interface
* @typeparam TemplateType Template block interface
*/
export class BasePinnedObject<
PropertiesType extends PinnedObjectProps = PinnedObjectProps,
TemplateType extends TemplateProps = TemplateProps
> extends BaseFlowNode<PropertiesType, TemplateType> {
/**
* Output pins leading out of this node
*/
public readonly OutputPins: OutputPin[];
/**
* Input pins leading into this node
*/
public readonly InputPins: InputPin[];
constructor(args: ArticyCreatorArguments<PropertiesType, TemplateType>) {
super(args);
this.InputPins =
args.props.InputPins?.map(
def => new InputPin({ props: def, type: 'InputPin', db: args.db })
) ?? [];
this.OutputPins =
args.props.OutputPins?.map(
def => new OutputPin({ props: def, type: 'OutputPin', db: args.db })
) ?? [];
}
}
/**
* Base class for all Hubs
* @typeparam TemplateType Template block interface
*/
@ArticyType('Hub')
export class Hub<
TemplateType extends TemplateProps = TemplateProps
> extends BasePinnedObject<PinnedObjectProps, TemplateType> {
next(): [BaseFlowNode | undefined, ConnectionProps | undefined] {
if (this.OutputPins.length === 0) {
return [undefined, undefined];
}
return [this.OutputPins[0], undefined];
}
numBranches(): number {
if (this.OutputPins.length === 0) {
return 0;
}
// Just going to our output pin
return 1;
}
}
/**
* Conditions that choose either their first or second output pin depending on the result of a condition script
* @typeparam TemplateType Template block interface
*/
@ArticyType('Condition')
export class Condition<
TemplateType extends TemplateProps = TemplateProps
> extends BasePinnedObject<ScriptNodeProps, TemplateType> {
next(
context: ExecuteContext,
_branchIndex: number,
_last: Id | null,
shadowing: boolean
): [BaseFlowNode | undefined, ConnectionProps | undefined] {
// Return 0 or 1 based on a script
const result = runScript(
this.properties.Expression,
context.variables,
context.visits,
this.id,
this.db,
true,
shadowing
);
return [this.OutputPins[result ? 0 : 1], undefined];
}
// Always one branch. True or False.
numBranches(): number {
return 1;
}
// Execute in case of side effects
execute(context: ExecuteContext): void {
runScript(
this.properties.Expression,
context.variables,
context.visits,
this.id,
this.db,
true,
false
);
}
}
/**
* Instruction that runs a script before moving onto the next node
* @typeparam TemplateType Template block interface
*/
@ArticyType('Instruction')
export class Instruction<
TemplateType extends TemplateProps = TemplateProps
> extends BasePinnedObject<ScriptNodeProps, TemplateType> {
next(
context: ExecuteContext,
_branchIndex: number,
_last: Id | null,
shadowing: boolean
): [BaseFlowNode | undefined, ConnectionProps | undefined] {
// Run script
runScript(
this.properties.Expression,
context.variables,
context.visits,
this.id,
this.db,
false,
shadowing
);
// Go to first pin
return [this.OutputPins[0], undefined];
}
// Always one branch.
numBranches(): number {
return 1;
}
needsShadow(): boolean {
return this.properties.Expression.length > 0;
}
execute(context: ExecuteContext): void {
runScript(
this.properties.Expression,
context.variables,
context.visits,
this.id,
this.db,
false,
false
);
}
}
/**
* Jumps to a destination node by reference
* @typeparam TemplateType Template block interface
*/
@ArticyType('Jump')
export class Jump<
TemplateType extends TemplateProps = TemplateProps
> extends BasePinnedObject<JumpProps, TemplateType> {
constructor(args: ArticyCreatorArguments<JumpProps, TemplateType>) {
super(args);
this.TargetPin = args.db.getObject(this.properties.TargetPin, InputPin);
}
next(): [BaseFlowNode | undefined, ConnectionProps | undefined] {
// Go to the target pin
return [this.TargetPin, undefined];
}
// One branch as long as we have a target
numBranches(): number {
return this.TargetPin ? 1 : 0;
}
public TargetPin: InputPin | undefined;
}
/**
* Fragment of dialogue spoken by an entity
* @typeparam TemplateType Template block interface
*/
@ArticyType('DialogueFragment')
export class DialogueFragment<
TemplateType extends TemplateProps = TemplateProps
> extends BasePinnedObject<DialogueFragmentProps, TemplateType> {
constructor(
args: ArticyCreatorArguments<DialogueFragmentProps, TemplateType>
) {
super(args);
this.Speaker = args.db.getObject(this.properties.Speaker, Entity);
}
next(): [BaseFlowNode | undefined, ConnectionProps | undefined] {
if (this.OutputPins.length === 0) {
return [undefined, undefined];
}
return [this.OutputPins[0], undefined];
}
numBranches(): number {
if (this.OutputPins.length === 0) {
return 0;
}
return 1;
}
public readonly Speaker: Entity | undefined;
}
/**
* Base class for all fragments with children
* @typeparam PropertiesType Property block interface
* @typeparam TemplateType Template block interface
*/
export class BaseFragment<
PropertiesType extends PinnedObjectProps,
TemplateType extends TemplateProps
> extends BasePinnedObject<PropertiesType, TemplateType> {
next(
_context: ExecuteContext,
branchIndex: number,
last: Id
): [BaseFlowNode | undefined, ConnectionProps | undefined] {
if (this.InputPins.length === 0) {
return [undefined, undefined];
}
// Find the pin we came in from
const inputPinToFollow =
last === null
? this.InputPins[0]
: this.InputPins.find(x => x.properties.Id === last);
// If we can't... die?
if (!inputPinToFollow) {
return [undefined, undefined];
}
// SPECIAL: Input pin goes nowhere, go to output pin
if (inputPinToFollow.connections().length === 0) {
if (this.OutputPins.length === 0) {
return [undefined, undefined];
}
return [this.OutputPins[0], undefined];
}
// Next connection from pin
return nextFromConnections(
this.db,
branchIndex,
inputPinToFollow.connections()
);
}
numBranches(_context: ExecuteContext, last: Id | null): number {
if (this.InputPins.length === 0) {
return 0;
}
// Find the pin we came in from
const inputPinToFollow =
last === null
? this.InputPins[0]
: this.InputPins.find(x => x.properties.Id === last);
// If we can't... die?
if (!inputPinToFollow) {
return 0;
}
// SPECIAL: Input pin goes nowhere, go to output pin
if (inputPinToFollow.connections().length === 0) {
if (this.OutputPins.length === 0) {
return 0;
}
return 1;
}
// Number of connections from our input pin
return inputPinToFollow.connections().length;
}
}
/**
* Flow fragment with attachments, text, and children.
* @typeparam TemplateType Template block interface
*/
@ArticyType('FlowFragment')
export class FlowFragment<
TemplateType extends TemplateProps = TemplateProps
> extends BaseFragment<FlowFragmentProps, TemplateType> {}
/**
* Dialogue node with attachments, text, and children
* @typeparam TemplateType Template block interface
*/
@ArticyType('Dialogue')
export class Dialogue<
TemplateType extends TemplateProps = TemplateProps
> extends BaseFragment<DialogueProps, TemplateType> {}