-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathinterpreter.ts
More file actions
112 lines (92 loc) · 3.11 KB
/
Copy pathinterpreter.ts
File metadata and controls
112 lines (92 loc) · 3.11 KB
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
import { Random } from "./random";
import { Grid } from "./grid";
import { vec3 } from "./helpers/helper";
import { SymmetryHelper } from "./helpers/symmetry";
import { Node, Branch, MarkovNode, WFCNode, EventNode } from "./mj-nodes";
export class Interpreter {
public root: Branch;
public current: Branch;
public listener: EventNode;
public blocking = false;
public grid: Grid;
public startgrid: Grid;
origin: boolean;
public rng: Random;
public time = 0;
public readonly changes: vec3[] = [];
public readonly first: number[] = [];
public counter = 0;
public static async load(
elem: Element,
MX: number,
MY: number,
MZ: number
) {
const ip = new Interpreter();
ip.origin = elem.getAttribute("origin") === "True";
ip.grid = Grid.build(elem, MX, MY, MZ);
if (!ip.grid) {
console.error("Failed to load grid");
return null;
}
ip.startgrid = ip.grid;
const symmetryString = elem.getAttribute("symmetry");
const dflt = new Uint8Array(ip.grid.MZ === 1 ? 8 : 48);
dflt.fill(1);
const symmetry = SymmetryHelper.getSymmetry(
ip.grid.MZ === 1,
symmetryString,
dflt
);
if (!symmetry) {
console.error(elem, `unknown symmetry ${symmetryString}`);
return null;
}
const topnode = await Node.factory(elem, symmetry, ip, ip.grid);
if (!topnode) return null;
ip.root =
topnode instanceof Branch ? topnode : new MarkovNode(topnode, ip);
return ip;
}
public *run(
seed: number,
steps: number
): Generator<[Uint8Array, string, number, number, number]> {
this.rng = new Random(seed);
this.grid = this.startgrid;
this.grid.clear();
if (this.origin) {
const center =
(this.grid.MX >>> 1) +
(this.grid.MY >>> 1) * this.grid.MX +
(this.grid.MZ >>> 1) * this.grid.MX * this.grid.MY;
this.grid.state[center] = 1;
}
this.changes.splice(0, this.changes.length);
this.first.splice(0, this.first.length);
this.first.push(0);
this.time = 0;
this.root.reset();
this.current = this.root;
this.counter = 0;
while (this.current && (steps <= 0 || this.counter < steps)) {
if (!this.blocking) yield this.state();
this.current.run();
this.increChanges();
}
yield this.state();
}
public increChanges() {
this.counter++;
this.first.push(this.changes.length);
}
public onRender() {
if (this.current instanceof WFCNode && this.current.n < 0) {
this.current.updateState();
}
}
public state(): [Uint8Array, string, number, number, number] {
const grid = this.grid;
return [grid.padded, grid.characters, grid.MX, grid.MY, grid.MZ];
}
}