@wonderyard/vivarium 1.0.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -70,7 +70,7 @@ const canvas = document.createElement(canvas);
70
70
  document.body.appendChild(canvas);
71
71
 
72
72
  // Set the canvas size. This will be the automaton size as well.
73
- const size = 512;
73
+ const size = 256;
74
74
  canvas.width = size;
75
75
  canvas.height = size;
76
76
 
@@ -27,7 +27,8 @@ export declare class ElementBlueprint extends RefBlueprint {
27
27
  protected context: BlueprintContext;
28
28
  type: "element";
29
29
  id: string;
30
- constructor(context: BlueprintContext, automaton: Automaton, element: Element);
30
+ index: number;
31
+ constructor(context: BlueprintContext, automaton: Automaton, element: Element, index: number);
31
32
  }
32
33
  /**
33
34
  * Blueprint for a kind. Returned by {@link VivariumBlueprint.kind | vivarium().kind()}.
@@ -1,8 +1,8 @@
1
- import { BaseBlueprint as r } from "./base-blueprint.js";
2
- import { R as n } from "../rule-blueprint-DGODv9IV.js";
3
- import { toRefIdOrPoint as p } from "./utils.js";
1
+ import { BaseBlueprint as n } from "./base-blueprint.js";
2
+ import { R as p } from "../rule-blueprint-DGODv9IV.js";
3
+ import { toRefIdOrPoint as d } from "./utils.js";
4
4
  import { A as u } from "../constants-D4GX9YB2.js";
5
- class o extends r {
5
+ class o extends n {
6
6
  constructor(e, t) {
7
7
  super(), this.automaton = e, this.id = t.id, this.name = t.name, this.type = t.type;
8
8
  }
@@ -20,29 +20,30 @@ class o extends r {
20
20
  const t = {
21
21
  fromId: this.id,
22
22
  fromType: this.type,
23
- to: p(e),
23
+ to: d(e),
24
24
  when: [],
25
25
  accept: u.ALL
26
26
  };
27
- return this.automaton.rules.push(t), new n(this.context, this.automaton, t);
27
+ return this.automaton.rules.push(t), new p(this.context, this.automaton, t);
28
28
  }
29
29
  }
30
- class h extends o {
31
- constructor(e, t, s) {
32
- super(t, s), this.context = e, this.id = s.id;
30
+ class l extends o {
31
+ constructor(e, t, i, r) {
32
+ super(t, i), this.context = e, this.id = i.id, this.index = r;
33
33
  }
34
34
  type = "element";
35
35
  id;
36
+ index;
36
37
  }
37
- class l extends o {
38
- constructor(e, t, s) {
39
- super(t, s), this.context = e, this.id = s.id;
38
+ class x extends o {
39
+ constructor(e, t, i) {
40
+ super(t, i), this.context = e, this.id = i.id;
40
41
  }
41
42
  type = "kind";
42
43
  id;
43
44
  }
44
45
  export {
45
- h as ElementBlueprint,
46
- l as KindBlueprint,
46
+ l as ElementBlueprint,
47
+ x as KindBlueprint,
47
48
  o as RefBlueprint
48
49
  };
@@ -1,11 +1,11 @@
1
1
  import { n as o } from "../rule-blueprint-DGODv9IV.js";
2
- import { BaseBlueprint as n } from "./base-blueprint.js";
3
- import { Helpers as a } from "./helpers.js";
4
- import { ElementBlueprint as h, KindBlueprint as m } from "./ref-blueprint.js";
5
- import { C as u, S as p } from "../constants-D4GX9YB2.js";
6
- class C extends n {
2
+ import { BaseBlueprint as a } from "./base-blueprint.js";
3
+ import { Helpers as h } from "./helpers.js";
4
+ import { ElementBlueprint as m, KindBlueprint as u } from "./ref-blueprint.js";
5
+ import { C as d, S as p } from "../constants-D4GX9YB2.js";
6
+ class b extends a {
7
7
  constructor(t) {
8
- super(), this.neighborhoodName = t, this.helpers = new a(this.neighborhoodName), this.neighbor = this.neighborhoodName === "cross" ? u : p, this.automaton = {
8
+ super(), this.neighborhoodName = t, this.helpers = new h(this.neighborhoodName), this.neighbor = this.neighborhoodName === "cross" ? d : p, this.automaton = {
9
9
  neighborhood: t ?? "square",
10
10
  elements: [],
11
11
  kinds: [],
@@ -36,16 +36,16 @@ class C extends n {
36
36
  * @param extensions - An optional array of kinds this element extends.
37
37
  * @returns An {@link ElementBlueprint} that can be used to define rules.
38
38
  */
39
- element(t, e, r = []) {
39
+ element(t, e, i = []) {
40
40
  this.assertNotCreated(), this.assertUniqueName(t), this.assertUniqueColor(e);
41
41
  const s = {
42
42
  type: "element",
43
43
  id: o(),
44
44
  name: t,
45
45
  color: e,
46
- extensions: r.map((i) => i.id)
47
- };
48
- return this.automaton.elements.push(s), new h(this.context, this.automaton, s);
46
+ extensions: i.map((r) => r.id)
47
+ }, n = this.automaton.elements.push(s) - 1;
48
+ return new m(this.context, this.automaton, s, n);
49
49
  }
50
50
  /**
51
51
  * Defines a new kind in the vivarium. A kind groups elements that share common behavior.
@@ -60,7 +60,7 @@ class C extends n {
60
60
  id: o(),
61
61
  name: t
62
62
  };
63
- return this.automaton.kinds.push(e), new m(this.context, this.automaton, e);
63
+ return this.automaton.kinds.push(e), new u(this.context, this.automaton, e);
64
64
  }
65
65
  /**
66
66
  * Finalizes the vivarium and produces an {@link Automaton} object. After calling this method,
@@ -75,5 +75,5 @@ class C extends n {
75
75
  }
76
76
  }
77
77
  export {
78
- C as VivariumBlueprint
78
+ b as VivariumBlueprint
79
79
  };
package/dist/main.d.ts CHANGED
@@ -1,6 +1,6 @@
1
+ export { Helpers } from './blueprint/helpers';
2
+ export { ElementBlueprint, KindBlueprint, RefBlueprint, } from './blueprint/ref-blueprint';
3
+ export { RuleBlueprint, RuleBlueprintWithAccept, } from './blueprint/rule-blueprint';
4
+ export { VivariumBlueprint } from './blueprint/vivarium-blueprint';
1
5
  export { vivarium } from './vivarium/vivarium';
2
6
  export { setup } from './webgpu/setup';
3
- export { VivariumBlueprint } from './blueprint/vivarium-blueprint';
4
- export { RefBlueprint, ElementBlueprint, KindBlueprint, } from './blueprint/ref-blueprint';
5
- export { RuleBlueprint, RuleBlueprintWithAccept, } from './blueprint/rule-blueprint';
6
- export { Helpers } from './blueprint/helpers';
package/dist/main.js CHANGED
@@ -1,17 +1,17 @@
1
- import { vivarium as t } from "./vivarium/vivarium.js";
2
- import { setup as i } from "./webgpu/setup.js";
3
- import { VivariumBlueprint as u } from "./blueprint/vivarium-blueprint.js";
4
- import { ElementBlueprint as m, KindBlueprint as n, RefBlueprint as f } from "./blueprint/ref-blueprint.js";
5
- import { R as B, a } from "./rule-blueprint-DGODv9IV.js";
6
- import { Helpers as R } from "./blueprint/helpers.js";
1
+ import { Helpers as t } from "./blueprint/helpers.js";
2
+ import { ElementBlueprint as i, KindBlueprint as o, RefBlueprint as u } from "./blueprint/ref-blueprint.js";
3
+ import { R as m, a as n } from "./rule-blueprint-DGODv9IV.js";
4
+ import { VivariumBlueprint as x } from "./blueprint/vivarium-blueprint.js";
5
+ import { vivarium as a } from "./vivarium/vivarium.js";
6
+ import { setup as R } from "./webgpu/setup.js";
7
7
  export {
8
- m as ElementBlueprint,
9
- R as Helpers,
10
- n as KindBlueprint,
11
- f as RefBlueprint,
12
- B as RuleBlueprint,
13
- a as RuleBlueprintWithAccept,
14
- u as VivariumBlueprint,
15
- i as setup,
16
- t as vivarium
8
+ i as ElementBlueprint,
9
+ t as Helpers,
10
+ o as KindBlueprint,
11
+ u as RefBlueprint,
12
+ m as RuleBlueprint,
13
+ n as RuleBlueprintWithAccept,
14
+ x as VivariumBlueprint,
15
+ R as setup,
16
+ a as vivarium
17
17
  };
@@ -63,16 +63,20 @@ export declare const mainCompute: import('typegpu').TgpuComputeFn<{
63
63
  }>;
64
64
  /**
65
65
  * Initializes the WebGPU simulation for a given canvas and automaton. The grid is randomly initialized
66
- * with the defined elements.
66
+ * with the defined elements unless an `initialGrid` is provided.
67
+ *
68
+ * Use `index` found in `ElementBlueprint` instances to reference elements when building the initialGrid.
67
69
  *
68
70
  * @param options - An object containing the `canvas` element and the compiled `automaton`.
69
71
  * @param options.canvas - The HTML canvas element. Its `width` and `height` define the grid dimensions.
70
72
  * @param options.automaton - The compiled automaton produced by {@link VivariumBlueprint.create | vivarium().create()}.
73
+ * @param options.initialGrid - An optional 2d array of element indices to use instead of random initialization. Alternatively, you can pass a flat (1d) array with one index per cell, row-major order.
71
74
  * @returns An object with an `evolve` function that advances the simulation by one step, a `setAutomaton` function to update the automaton, and the underlying `tgpuRoot`.
72
75
  */
73
- export declare const setup: ({ canvas, automaton, }: {
76
+ export declare const setup: ({ canvas, automaton, initialGrid, }: {
74
77
  canvas: HTMLCanvasElement;
75
78
  automaton: Automaton;
79
+ initialGrid?: number[] | number[][];
76
80
  }) => {
77
81
  evolve: () => Promise<void>;
78
82
  setAutomaton: ({ automaton }: {