simple-circuit-engine 0.0.12 → 0.0.14

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.
Files changed (99) hide show
  1. package/AGENTS.md +9 -6
  2. package/CLAUDE.md +2 -0
  3. package/README.md +8 -8
  4. package/dist/core/index.js +109 -74
  5. package/dist/core/setup.d.ts +18 -0
  6. package/dist/core/simulation/behaviors/arithmetic/AdderBehavior.d.ts +28 -0
  7. package/dist/core/simulation/behaviors/arithmetic/ArithmeticBehaviorMixin.d.ts +63 -0
  8. package/dist/core/simulation/behaviors/arithmetic/EightBitAdderBehavior.d.ts +51 -0
  9. package/dist/core/simulation/behaviors/arithmetic/EightBitOnesComplementBehavior.d.ts +29 -0
  10. package/dist/core/simulation/behaviors/arithmetic/HalfAdderBehavior.d.ts +22 -0
  11. package/dist/core/simulation/behaviors/arithmetic/index.d.ts +7 -0
  12. package/dist/core/simulation/behaviors/index.d.ts +14 -0
  13. package/dist/core/simulation/behaviors/interface/EightInputBehavior.d.ts +9 -0
  14. package/dist/core/simulation/behaviors/interface/EightLightBehavior.d.ts +9 -0
  15. package/dist/core/simulation/behaviors/interface/FourInputBehavior.d.ts +9 -0
  16. package/dist/core/simulation/behaviors/interface/FourLightBehavior.d.ts +9 -0
  17. package/dist/core/simulation/behaviors/interface/InputBehaviorMixin.d.ts +37 -0
  18. package/dist/core/simulation/behaviors/interface/LightBehaviorMixin.d.ts +60 -0
  19. package/dist/core/simulation/behaviors/interface/OneInputBehavior.d.ts +9 -0
  20. package/dist/core/simulation/behaviors/interface/OneLightBehavior.d.ts +9 -0
  21. package/dist/core/simulation/behaviors/interface/TwoInputBehavior.d.ts +9 -0
  22. package/dist/core/simulation/behaviors/interface/TwoLightBehavior.d.ts +9 -0
  23. package/dist/core/simulation/behaviors/interface/index.d.ts +0 -0
  24. package/dist/core/simulation/states/arithmetic/AdderState.d.ts +16 -0
  25. package/dist/core/simulation/states/arithmetic/ArithmeticState.d.ts +53 -0
  26. package/dist/core/simulation/states/arithmetic/EightBitAdderState.d.ts +25 -0
  27. package/dist/core/simulation/states/arithmetic/EightBitOnesComplementState.d.ts +18 -0
  28. package/dist/core/simulation/states/arithmetic/HalfAdderState.d.ts +16 -0
  29. package/dist/core/simulation/states/arithmetic/index.d.ts +7 -0
  30. package/dist/core/simulation/states/index.d.ts +15 -0
  31. package/dist/core/simulation/states/interface/EightInputState.d.ts +6 -0
  32. package/dist/core/simulation/states/interface/EightLightState.d.ts +6 -0
  33. package/dist/core/simulation/states/interface/FourInputState.d.ts +6 -0
  34. package/dist/core/simulation/states/interface/FourLightState.d.ts +6 -0
  35. package/dist/core/simulation/states/interface/InputState.d.ts +45 -0
  36. package/dist/core/simulation/states/interface/LightState.d.ts +54 -0
  37. package/dist/core/simulation/states/interface/OneInputState.d.ts +6 -0
  38. package/dist/core/simulation/states/interface/OneLightState.d.ts +6 -0
  39. package/dist/core/simulation/states/interface/TwoInputState.d.ts +6 -0
  40. package/dist/core/simulation/states/interface/TwoLightState.d.ts +6 -0
  41. package/dist/core/simulation/types.d.ts +10 -2
  42. package/dist/core/topology/Component.d.ts +6 -1
  43. package/dist/core/topology/ENode.d.ts +7 -2
  44. package/dist/core/topology/index.d.ts +2 -0
  45. package/dist/core/topology/networkTraversal.d.ts +50 -0
  46. package/dist/core/topology/types.d.ts +31 -6
  47. package/dist/core-HH6iRWtB.js +4671 -0
  48. package/dist/core-HH6iRWtB.js.map +1 -0
  49. package/dist/i18n/index.d.ts +1158 -0
  50. package/dist/i18n/locales/en.json.d.ts +366 -0
  51. package/dist/i18n/locales/fr.json.d.ts +366 -0
  52. package/dist/index.d.ts +1 -0
  53. package/dist/index.js +172 -118
  54. package/dist/scene/CircuitEngine.d.ts +49 -4
  55. package/dist/scene/index.d.ts +6 -2
  56. package/dist/scene/index.js +62 -45
  57. package/dist/scene/setup.d.ts +18 -0
  58. package/dist/scene/shared/AbstractCircuitController.d.ts +11 -3
  59. package/dist/scene/shared/HoverManager.d.ts +15 -0
  60. package/dist/scene/shared/components/arithmetic/AdderVisualFactory.d.ts +54 -0
  61. package/dist/scene/shared/components/arithmetic/EightBitAdderVisualFactory.d.ts +45 -0
  62. package/dist/scene/shared/components/arithmetic/EightBitOnesComplementVisualFactory.d.ts +63 -0
  63. package/dist/scene/shared/components/arithmetic/HalfAdderVisualFactory.d.ts +55 -0
  64. package/dist/scene/shared/components/arithmetic/index.d.ts +4 -0
  65. package/dist/scene/shared/components/index.d.ts +14 -0
  66. package/dist/scene/shared/components/interface/EightInputVisualFactory.d.ts +15 -0
  67. package/dist/scene/shared/components/interface/EightLightVisualFactory.d.ts +15 -0
  68. package/dist/scene/shared/components/interface/FourInputVisualFactory.d.ts +15 -0
  69. package/dist/scene/shared/components/interface/FourLightVisualFactory.d.ts +15 -0
  70. package/dist/scene/shared/components/interface/InputVisualFactoryBase.d.ts +50 -0
  71. package/dist/scene/shared/components/interface/LightVisualFactoryBase.d.ts +46 -0
  72. package/dist/scene/shared/components/interface/OneInputVisualFactory.d.ts +15 -0
  73. package/dist/scene/shared/components/interface/OneLightVisualFactory.d.ts +15 -0
  74. package/dist/scene/shared/components/interface/TwoInputVisualFactory.d.ts +15 -0
  75. package/dist/scene/shared/components/interface/TwoLightVisualFactory.d.ts +15 -0
  76. package/dist/scene/shared/types.d.ts +34 -4
  77. package/dist/scene/shared/utils/ControlsUtils.d.ts +1 -1
  78. package/dist/scene/shared/utils/GeometryUtils.d.ts +111 -0
  79. package/dist/scene/static/CircuitController.d.ts +13 -1
  80. package/dist/scene/static/tools/BuildTool.d.ts +81 -0
  81. package/dist/scene/static/tools/ComponentPickerWidget.d.ts +7 -0
  82. package/dist/scene/static/tools/ConfigPanelWidget.d.ts +14 -0
  83. package/dist/scene/static/tools/MultiWiringPlacement.d.ts +40 -0
  84. package/dist/scene/widgets/HelpWidget.d.ts +13 -0
  85. package/dist/scene/widgets/ModeWidget.d.ts +16 -0
  86. package/dist/scene/widgets/MultiWiringWidget.d.ts +21 -0
  87. package/dist/scene/{static → widgets}/PinTooltipWidget.d.ts +7 -1
  88. package/dist/scene/widgets/SimulationPlayerWidget.d.ts +62 -0
  89. package/dist/scene/widgets/ToolsWidget.d.ts +18 -0
  90. package/dist/scene/widgets/WidgetsManager.d.ts +65 -0
  91. package/dist/scene/widgets/assets/icons.d.ts +20 -0
  92. package/dist/scene/widgets/index.d.ts +9 -0
  93. package/dist/scene/widgets/styles.d.ts +33 -0
  94. package/dist/{scene-CVsDdySt.js → scene-C1xhdw_B.js} +3993 -772
  95. package/dist/scene-C1xhdw_B.js.map +1 -0
  96. package/package.json +14 -9
  97. package/dist/core-Bjta9Y7_.js +0 -2707
  98. package/dist/core-Bjta9Y7_.js.map +0 -1
  99. package/dist/scene-CVsDdySt.js.map +0 -1
@@ -1,2707 +0,0 @@
1
- function it(e) {
2
- let t, n, i;
3
- return typeof e == "object" ? (t = e.hashFunction, n = e.expiring, i = e.tags) : t = e, (s, o, r) => {
4
- if (r.value != null) r.value = B(r.value, t, n, i);
5
- else if (r.get != null) r.get = B(r.get, t, n, i);
6
- else throw "Only put a Memoize() decorator on a method or get accessor.";
7
- };
8
- }
9
- function te(e, t) {
10
- return it({
11
- expiring: e,
12
- hashFunction: t
13
- });
14
- }
15
- var k = /* @__PURE__ */ new Map();
16
- function B(e, t, n = 0, i) {
17
- const s = /* @__PURE__ */ Symbol("__memoized_map__");
18
- return function(...o) {
19
- let r;
20
- this.hasOwnProperty(s) || Object.defineProperty(this, s, {
21
- configurable: !1,
22
- enumerable: !1,
23
- writable: !1,
24
- value: /* @__PURE__ */ new Map()
25
- });
26
- let a = this[s];
27
- if (Array.isArray(i)) for (const c of i) k.has(c) ? k.get(c).push(a) : k.set(c, [a]);
28
- if (t || o.length > 0 || n > 0) {
29
- let c;
30
- t === !0 ? c = o.map((p) => p.toString()).join("!") : t ? c = t.apply(this, o) : c = o[0];
31
- const d = `${c}__timestamp`;
32
- let h = !1;
33
- if (n > 0) if (!a.has(d)) h = !0;
34
- else {
35
- let p = a.get(d);
36
- h = Date.now() - p > n;
37
- }
38
- a.has(c) && !h ? r = a.get(c) : (r = e.apply(this, o), a.set(c, r), n > 0 && a.set(d, Date.now()));
39
- } else {
40
- const c = this;
41
- a.has(c) ? r = a.get(c) : (r = e.apply(this, o), a.set(c, r));
42
- }
43
- return r;
44
- };
45
- }
46
- var P = class W {
47
- constructor(t, n, i) {
48
- this.x = t, this.y = n, this.z = i;
49
- }
50
- equals(t) {
51
- return this.x === t.x && this.y === t.y && this.z === t.z;
52
- }
53
- toJSON() {
54
- return {
55
- x: this.x,
56
- y: this.y,
57
- z: this.z
58
- };
59
- }
60
- static fromJSON(t) {
61
- return new W(t.x, t.y, t.z);
62
- }
63
- toString() {
64
- return `Position(${this.x}, ${this.y}, ${this.z})`;
65
- }
66
- }, $ = class z {
67
- constructor(t = new P(0, 15, 15), n = new P(0, 0, 0), i = 75, s = 0.1, o = 1e3) {
68
- this.position = t, this.lookAtPosition = n, this.fov = i, this.near = s, this.far = o;
69
- }
70
- toJSON() {
71
- return {
72
- position: this.position.toJSON(),
73
- lookAtPosition: this.lookAtPosition.toJSON(),
74
- fov: this.fov,
75
- near: this.near,
76
- far: this.far
77
- };
78
- }
79
- static fromJSON(t) {
80
- return new z(P.fromJSON(t.position), P.fromJSON(t.lookAtPosition), t.fov, t.near, t.far);
81
- }
82
- toString() {
83
- return `CameraOptions(position: ${this.position.toString()}, lookAt: ${this.lookAtPosition.toString()}, fov: ${this.fov}, near: ${this.near}, far: ${this.far})`;
84
- }
85
- }, M = class F {
86
- constructor(t, n) {
87
- if (this.x = t, this.y = n, !Number.isInteger(t) || !Number.isInteger(n)) throw new TypeError(`Position coordinates must be integers (got x=${t}, y=${n})`);
88
- }
89
- equals(t) {
90
- return this.x === t.x && this.y === t.y;
91
- }
92
- toJSON() {
93
- return {
94
- x: this.x,
95
- y: this.y
96
- };
97
- }
98
- static fromJSON(t) {
99
- return new F(t.x, t.y);
100
- }
101
- toString() {
102
- return `Position(${this.x}, ${this.y})`;
103
- }
104
- };
105
- function nt(e, t, n = 1 / 0) {
106
- if (e.length === 0) return 0;
107
- if (e.length === 1) return 1;
108
- let i = 1;
109
- for (let s = 0; s < e.length - 1; s++) {
110
- const o = e[s], r = e[s + 1], a = st(t, o, r);
111
- a < n && (n = a, i = s + 1);
112
- }
113
- return i;
114
- }
115
- function st(e, t, n) {
116
- const i = n.x - t.x, s = n.y - t.y, o = i * i + s * s;
117
- if (o === 0) return Math.sqrt((e.x - t.x) ** 2 + (e.y - t.y) ** 2);
118
- const r = Math.max(0, Math.min(1, ((e.x - t.x) * i + (e.y - t.y) * s) / o)), a = t.x + r * i, c = t.y + r * s;
119
- return Math.sqrt((e.x - a) ** 2 + (e.y - c) ** 2);
120
- }
121
- function L(e, t = 5) {
122
- if (e.length <= 2) return [...e];
123
- const n = [e[0]];
124
- for (let i = 1; i < e.length - 1; i++) {
125
- const s = n[n.length - 1], o = e[i], r = e[i + 1];
126
- ot(s, o, r, t) || n.push(o);
127
- }
128
- return n.push(e[e.length - 1]), n;
129
- }
130
- function ot(e, t, n, i = 5) {
131
- const s = (t.x - e.x) * (n.y - e.y) - (t.y - e.y) * (n.x - e.x);
132
- return Math.abs(s) <= i;
133
- }
134
- var rt = class J {
135
- constructor(t) {
136
- if (this.angle = t, !Number.isInteger(t)) throw new TypeError(`Rotation angle must be an integer (got ${t})`);
137
- }
138
- toJSON() {
139
- return this.angle;
140
- }
141
- static fromJSON(t) {
142
- return new J(t);
143
- }
144
- equals(t) {
145
- return this.angle === t.angle;
146
- }
147
- toString() {
148
- return `Rotation(${this.angle}°)`;
149
- }
150
- };
151
- function G() {
152
- if (typeof crypto < "u" && crypto.randomUUID) return crypto.randomUUID();
153
- const e = "0123456789abcdef", t = [
154
- 8,
155
- 4,
156
- 4,
157
- 4,
158
- 12
159
- ], n = [];
160
- for (const s of t) {
161
- let o = "";
162
- for (let r = 0; r < s; r++) {
163
- const a = Math.floor(Math.random() * 16);
164
- o += e[a];
165
- }
166
- n.push(o);
167
- }
168
- const i = n.join("-").split("");
169
- return i[14] = "4", i[19] = e[parseInt(i[19] ?? "0", 16) & 3 | 8] ?? "0", i.join("");
170
- }
171
- var X = "0.0.11", g = /* @__PURE__ */ (function(e) {
172
- return e.Voltage = "Voltage", e.Current = "Current", e;
173
- })({}), C = /* @__PURE__ */ (function(e) {
174
- return e.Pin = "Pin", e.BranchingPoint = "BranchingPoint", e;
175
- })({}), ee = [
176
- "CMOS1",
177
- "TTL1",
178
- "Sandbox"
179
- ], U = "CMOS1", u = /* @__PURE__ */ (function(e) {
180
- return e.Cube = "cube", e.Label = "label", e.Battery = "battery", e.Switch = "switch", e.DoubleThrowSwitch = "doubleThrowSwitch", e.Lightbulb = "lightbulb", e.Relay = "relay", e.SmallLED = "smallLED", e.RectangleLED = "rectangleLED", e.Clock = "clock", e.Inverter = "inverter", e.NandGate = "nandGate", e.Nand4Gate = "nand4Gate", e.Nand8Gate = "nand8Gate", e.NorGate = "norGate", e.Nor4Gate = "nor4Gate", e.Nor8Gate = "nor8Gate", e.XorGate = "xorGate", e.Xor4Gate = "xor4Gate", e.Xor8Gate = "xor8Gate", e;
181
- })({}), N = {
182
- [u.Switch]: {
183
- id: u.Switch,
184
- name: "Switch",
185
- pins: /* @__PURE__ */ new Map([["input", {
186
- subtype: "free",
187
- sourceType: void 0
188
- }], ["output", {
189
- subtype: "free",
190
- sourceType: void 0
191
- }]]),
192
- config: /* @__PURE__ */ new Map([
193
- ["initialState", "open"],
194
- ["transitionSpan", "1"],
195
- ["size", "1"]
196
- ])
197
- },
198
- [u.DoubleThrowSwitch]: {
199
- id: u.DoubleThrowSwitch,
200
- name: "DoubleThrowSwitch",
201
- pins: /* @__PURE__ */ new Map([
202
- ["input1", {
203
- subtype: "free",
204
- sourceType: void 0
205
- }],
206
- ["input2", {
207
- subtype: "free",
208
- sourceType: void 0
209
- }],
210
- ["output", {
211
- subtype: "free",
212
- sourceType: void 0
213
- }]
214
- ]),
215
- config: /* @__PURE__ */ new Map([
216
- ["initialState", "input1"],
217
- ["transitionSpan", "1"],
218
- ["size", "1"]
219
- ])
220
- },
221
- [u.Battery]: {
222
- id: u.Battery,
223
- name: "Battery",
224
- pins: /* @__PURE__ */ new Map([["cathode", {
225
- subtype: "mainVcc",
226
- sourceType: g.Voltage
227
- }], ["anode", {
228
- subtype: "mainGnd",
229
- sourceType: g.Current
230
- }]]),
231
- config: /* @__PURE__ */ new Map([])
232
- },
233
- [u.Lightbulb]: {
234
- id: u.Lightbulb,
235
- name: "Lightbulb",
236
- pins: /* @__PURE__ */ new Map([["pin1", {
237
- subtype: "free",
238
- sourceType: void 0
239
- }], ["pin2", {
240
- subtype: "free",
241
- sourceType: void 0
242
- }]]),
243
- config: /* @__PURE__ */ new Map([["transitionSpan", "1"], ["size", "1"]])
244
- },
245
- [u.Relay]: {
246
- id: u.Relay,
247
- name: "Relay",
248
- pins: /* @__PURE__ */ new Map([
249
- ["cmd_in", {
250
- subtype: "free",
251
- sourceType: void 0
252
- }],
253
- ["cmd_out", {
254
- subtype: "free",
255
- sourceType: void 0
256
- }],
257
- ["power_in", {
258
- subtype: "free",
259
- sourceType: void 0
260
- }],
261
- ["power_out", {
262
- subtype: "free",
263
- sourceType: void 0
264
- }]
265
- ]),
266
- config: /* @__PURE__ */ new Map([
267
- ["activationLogic", "positive"],
268
- ["transitionSpan", "1"],
269
- ["initializationOrder", ""]
270
- ])
271
- },
272
- [u.SmallLED]: {
273
- id: u.SmallLED,
274
- name: "SmallLED",
275
- pins: /* @__PURE__ */ new Map([["pin1", {
276
- subtype: "free",
277
- sourceType: void 0
278
- }], ["pin2", {
279
- subtype: "free",
280
- sourceType: void 0
281
- }]]),
282
- config: /* @__PURE__ */ new Map([
283
- ["transitionSpan", "1"],
284
- ["idleColor", "white"],
285
- ["activeColor", "#ffff00"],
286
- ["size", "1"],
287
- ["ywRatio", "1"]
288
- ])
289
- },
290
- [u.RectangleLED]: {
291
- id: u.RectangleLED,
292
- name: "RectangleLED",
293
- pins: /* @__PURE__ */ new Map([["pin1", {
294
- subtype: "free",
295
- sourceType: void 0
296
- }], ["pin2", {
297
- subtype: "free",
298
- sourceType: void 0
299
- }]]),
300
- config: /* @__PURE__ */ new Map([
301
- ["transitionSpan", "1"],
302
- ["idleColor", "white"],
303
- ["activeColor", "#ffff00"],
304
- ["size", "1"],
305
- ["hwRatio", "1"],
306
- ["ywRatio", "1"]
307
- ])
308
- },
309
- [u.Cube]: {
310
- id: u.Cube,
311
- name: "Cube",
312
- pins: /* @__PURE__ */ new Map([]),
313
- config: /* @__PURE__ */ new Map([["color", "red"]])
314
- },
315
- [u.Label]: {
316
- id: u.Label,
317
- name: "Label",
318
- pins: /* @__PURE__ */ new Map([]),
319
- config: /* @__PURE__ */ new Map([["text", "Label"], ["size", "1"]])
320
- },
321
- [u.Clock]: {
322
- id: u.Clock,
323
- name: "Clock",
324
- pins: /* @__PURE__ */ new Map([
325
- ["vcc", {
326
- subtype: "vcc",
327
- sourceType: g.Voltage
328
- }],
329
- ["output", {
330
- subtype: "logicOutput",
331
- sourceType: void 0
332
- }],
333
- ["gnd", {
334
- subtype: "gnd",
335
- sourceType: g.Current
336
- }]
337
- ]),
338
- config: /* @__PURE__ */ new Map([["startHigh", "true"], ["halfPeriod", "2"]])
339
- },
340
- [u.Inverter]: {
341
- id: u.Inverter,
342
- name: "Inverter",
343
- pins: /* @__PURE__ */ new Map([
344
- ["vcc", {
345
- subtype: "vcc",
346
- sourceType: g.Voltage
347
- }],
348
- ["input", {
349
- subtype: "logicInput",
350
- sourceType: void 0
351
- }],
352
- ["output", {
353
- subtype: "logicOutput",
354
- sourceType: void 0
355
- }],
356
- ["gnd", {
357
- subtype: "gnd",
358
- sourceType: g.Current
359
- }]
360
- ]),
361
- config: /* @__PURE__ */ new Map([
362
- ["defaultLogicFamily", "CMOS1"],
363
- ["activationLogic", "negative"],
364
- ["transitionSpan", "1"],
365
- ["initializationOrder", ""]
366
- ])
367
- },
368
- [u.NandGate]: {
369
- id: u.NandGate,
370
- name: "NAND Gate",
371
- pins: /* @__PURE__ */ new Map([
372
- ["vcc", {
373
- subtype: "vcc",
374
- sourceType: g.Voltage
375
- }],
376
- ["input1", {
377
- subtype: "logicInput",
378
- sourceType: void 0
379
- }],
380
- ["input2", {
381
- subtype: "logicInput",
382
- sourceType: void 0
383
- }],
384
- ["output", {
385
- subtype: "logicOutput",
386
- sourceType: void 0
387
- }],
388
- ["gnd", {
389
- subtype: "gnd",
390
- sourceType: g.Current
391
- }]
392
- ]),
393
- config: /* @__PURE__ */ new Map([
394
- ["defaultLogicFamily", "CMOS1"],
395
- ["activationLogic", "negative"],
396
- ["transitionSpan", "1"],
397
- ["initializationOrder", ""]
398
- ])
399
- },
400
- [u.Nand4Gate]: {
401
- id: u.Nand4Gate,
402
- name: "NAND4 Gate",
403
- pins: /* @__PURE__ */ new Map([
404
- ["vcc", {
405
- subtype: "vcc",
406
- sourceType: g.Voltage
407
- }],
408
- ["input1", {
409
- subtype: "logicInput",
410
- sourceType: void 0
411
- }],
412
- ["input2", {
413
- subtype: "logicInput",
414
- sourceType: void 0
415
- }],
416
- ["input3", {
417
- subtype: "logicInput",
418
- sourceType: void 0
419
- }],
420
- ["input4", {
421
- subtype: "logicInput",
422
- sourceType: void 0
423
- }],
424
- ["output", {
425
- subtype: "logicOutput",
426
- sourceType: void 0
427
- }],
428
- ["gnd", {
429
- subtype: "gnd",
430
- sourceType: g.Current
431
- }]
432
- ]),
433
- config: /* @__PURE__ */ new Map([
434
- ["defaultLogicFamily", "CMOS1"],
435
- ["activationLogic", "negative"],
436
- ["transitionSpan", "2"],
437
- ["initializationOrder", ""]
438
- ])
439
- },
440
- [u.Nand8Gate]: {
441
- id: u.Nand8Gate,
442
- name: "NAND8 Gate",
443
- pins: /* @__PURE__ */ new Map([
444
- ["vcc", {
445
- subtype: "vcc",
446
- sourceType: g.Voltage
447
- }],
448
- ["input1", {
449
- subtype: "logicInput",
450
- sourceType: void 0
451
- }],
452
- ["input2", {
453
- subtype: "logicInput",
454
- sourceType: void 0
455
- }],
456
- ["input3", {
457
- subtype: "logicInput",
458
- sourceType: void 0
459
- }],
460
- ["input4", {
461
- subtype: "logicInput",
462
- sourceType: void 0
463
- }],
464
- ["input5", {
465
- subtype: "logicInput",
466
- sourceType: void 0
467
- }],
468
- ["input6", {
469
- subtype: "logicInput",
470
- sourceType: void 0
471
- }],
472
- ["input7", {
473
- subtype: "logicInput",
474
- sourceType: void 0
475
- }],
476
- ["input8", {
477
- subtype: "logicInput",
478
- sourceType: void 0
479
- }],
480
- ["output", {
481
- subtype: "logicOutput",
482
- sourceType: void 0
483
- }],
484
- ["gnd", {
485
- subtype: "gnd",
486
- sourceType: g.Current
487
- }]
488
- ]),
489
- config: /* @__PURE__ */ new Map([
490
- ["defaultLogicFamily", "CMOS1"],
491
- ["activationLogic", "negative"],
492
- ["transitionSpan", "3"],
493
- ["initializationOrder", ""]
494
- ])
495
- },
496
- [u.NorGate]: {
497
- id: u.NorGate,
498
- name: "NOR Gate",
499
- pins: /* @__PURE__ */ new Map([
500
- ["vcc", {
501
- subtype: "vcc",
502
- sourceType: g.Voltage
503
- }],
504
- ["input1", {
505
- subtype: "logicInput",
506
- sourceType: void 0
507
- }],
508
- ["input2", {
509
- subtype: "logicInput",
510
- sourceType: void 0
511
- }],
512
- ["output", {
513
- subtype: "logicOutput",
514
- sourceType: void 0
515
- }],
516
- ["gnd", {
517
- subtype: "gnd",
518
- sourceType: g.Current
519
- }]
520
- ]),
521
- config: /* @__PURE__ */ new Map([
522
- ["defaultLogicFamily", "CMOS1"],
523
- ["activationLogic", "negative"],
524
- ["transitionSpan", "1"],
525
- ["initializationOrder", ""]
526
- ])
527
- },
528
- [u.Nor4Gate]: {
529
- id: u.Nor4Gate,
530
- name: "NOR4 Gate",
531
- pins: /* @__PURE__ */ new Map([
532
- ["vcc", {
533
- subtype: "vcc",
534
- sourceType: g.Voltage
535
- }],
536
- ["input1", {
537
- subtype: "logicInput",
538
- sourceType: void 0
539
- }],
540
- ["input2", {
541
- subtype: "logicInput",
542
- sourceType: void 0
543
- }],
544
- ["input3", {
545
- subtype: "logicInput",
546
- sourceType: void 0
547
- }],
548
- ["input4", {
549
- subtype: "logicInput",
550
- sourceType: void 0
551
- }],
552
- ["output", {
553
- subtype: "logicOutput",
554
- sourceType: void 0
555
- }],
556
- ["gnd", {
557
- subtype: "gnd",
558
- sourceType: g.Current
559
- }]
560
- ]),
561
- config: /* @__PURE__ */ new Map([
562
- ["defaultLogicFamily", "CMOS1"],
563
- ["activationLogic", "negative"],
564
- ["transitionSpan", "2"],
565
- ["initializationOrder", ""]
566
- ])
567
- },
568
- [u.Nor8Gate]: {
569
- id: u.Nor8Gate,
570
- name: "NOR8 Gate",
571
- pins: /* @__PURE__ */ new Map([
572
- ["vcc", {
573
- subtype: "vcc",
574
- sourceType: g.Voltage
575
- }],
576
- ["input1", {
577
- subtype: "logicInput",
578
- sourceType: void 0
579
- }],
580
- ["input2", {
581
- subtype: "logicInput",
582
- sourceType: void 0
583
- }],
584
- ["input3", {
585
- subtype: "logicInput",
586
- sourceType: void 0
587
- }],
588
- ["input4", {
589
- subtype: "logicInput",
590
- sourceType: void 0
591
- }],
592
- ["input5", {
593
- subtype: "logicInput",
594
- sourceType: void 0
595
- }],
596
- ["input6", {
597
- subtype: "logicInput",
598
- sourceType: void 0
599
- }],
600
- ["input7", {
601
- subtype: "logicInput",
602
- sourceType: void 0
603
- }],
604
- ["input8", {
605
- subtype: "logicInput",
606
- sourceType: void 0
607
- }],
608
- ["output", {
609
- subtype: "logicOutput",
610
- sourceType: void 0
611
- }],
612
- ["gnd", {
613
- subtype: "gnd",
614
- sourceType: g.Current
615
- }]
616
- ]),
617
- config: /* @__PURE__ */ new Map([
618
- ["defaultLogicFamily", "CMOS1"],
619
- ["activationLogic", "negative"],
620
- ["transitionSpan", "3"],
621
- ["initializationOrder", ""]
622
- ])
623
- },
624
- [u.XorGate]: {
625
- id: u.XorGate,
626
- name: "XOR Gate",
627
- pins: /* @__PURE__ */ new Map([
628
- ["vcc", {
629
- subtype: "vcc",
630
- sourceType: g.Voltage
631
- }],
632
- ["input1", {
633
- subtype: "logicInput",
634
- sourceType: void 0
635
- }],
636
- ["input2", {
637
- subtype: "logicInput",
638
- sourceType: void 0
639
- }],
640
- ["output", {
641
- subtype: "logicOutput",
642
- sourceType: void 0
643
- }],
644
- ["gnd", {
645
- subtype: "gnd",
646
- sourceType: g.Current
647
- }]
648
- ]),
649
- config: /* @__PURE__ */ new Map([
650
- ["defaultLogicFamily", "CMOS1"],
651
- ["activationLogic", "positive"],
652
- ["transitionSpan", "2"],
653
- ["initializationOrder", ""]
654
- ])
655
- },
656
- [u.Xor4Gate]: {
657
- id: u.Xor4Gate,
658
- name: "XOR4 Gate",
659
- pins: /* @__PURE__ */ new Map([
660
- ["vcc", {
661
- subtype: "vcc",
662
- sourceType: g.Voltage
663
- }],
664
- ["input1", {
665
- subtype: "logicInput",
666
- sourceType: void 0
667
- }],
668
- ["input2", {
669
- subtype: "logicInput",
670
- sourceType: void 0
671
- }],
672
- ["input3", {
673
- subtype: "logicInput",
674
- sourceType: void 0
675
- }],
676
- ["input4", {
677
- subtype: "logicInput",
678
- sourceType: void 0
679
- }],
680
- ["output", {
681
- subtype: "logicOutput",
682
- sourceType: void 0
683
- }],
684
- ["gnd", {
685
- subtype: "gnd",
686
- sourceType: g.Current
687
- }]
688
- ]),
689
- config: /* @__PURE__ */ new Map([
690
- ["defaultLogicFamily", "CMOS1"],
691
- ["activationLogic", "positive"],
692
- ["transitionSpan", "4"],
693
- ["initializationOrder", ""]
694
- ])
695
- },
696
- [u.Xor8Gate]: {
697
- id: u.Xor8Gate,
698
- name: "XOR8 Gate",
699
- pins: /* @__PURE__ */ new Map([
700
- ["vcc", {
701
- subtype: "vcc",
702
- sourceType: g.Voltage
703
- }],
704
- ["input1", {
705
- subtype: "logicInput",
706
- sourceType: void 0
707
- }],
708
- ["input2", {
709
- subtype: "logicInput",
710
- sourceType: void 0
711
- }],
712
- ["input3", {
713
- subtype: "logicInput",
714
- sourceType: void 0
715
- }],
716
- ["input4", {
717
- subtype: "logicInput",
718
- sourceType: void 0
719
- }],
720
- ["input5", {
721
- subtype: "logicInput",
722
- sourceType: void 0
723
- }],
724
- ["input6", {
725
- subtype: "logicInput",
726
- sourceType: void 0
727
- }],
728
- ["input7", {
729
- subtype: "logicInput",
730
- sourceType: void 0
731
- }],
732
- ["input8", {
733
- subtype: "logicInput",
734
- sourceType: void 0
735
- }],
736
- ["output", {
737
- subtype: "logicOutput",
738
- sourceType: void 0
739
- }],
740
- ["gnd", {
741
- subtype: "gnd",
742
- sourceType: g.Current
743
- }]
744
- ]),
745
- config: /* @__PURE__ */ new Map([
746
- ["defaultLogicFamily", "CMOS1"],
747
- ["activationLogic", "positive"],
748
- ["transitionSpan", "6"],
749
- ["initializationOrder", ""]
750
- ])
751
- }
752
- }, x = class H {
753
- id;
754
- type;
755
- component;
756
- pinLabel;
757
- position;
758
- wires;
759
- source;
760
- subtype;
761
- constructor(t, n, i, s, o = void 0, r = "free") {
762
- this.id = G(), this.type = t, this.component = n, this.pinLabel = i, this.position = s, this.wires = /* @__PURE__ */ new Set(), this.source = o, this.subtype = r;
763
- }
764
- getPosition(t) {
765
- if (this.type === C.Pin) {
766
- if (!this.component) throw new Error("Pin node missing component reference");
767
- const n = t.getComponent(this.component);
768
- if (!n) throw new Error(`Component ${this.component} not found for pin node ${this.id}`);
769
- return n.position;
770
- }
771
- if (!this.position) throw new Error("Branching point node missing position");
772
- return this.position;
773
- }
774
- setPosition(t) {
775
- Object.defineProperty(this, "position", {
776
- value: t,
777
- writable: !1,
778
- enumerable: !0,
779
- configurable: !0
780
- });
781
- }
782
- setSourceType(t) {
783
- Object.defineProperty(this, "source", {
784
- value: t,
785
- writable: !0,
786
- enumerable: !0,
787
- configurable: !0
788
- });
789
- }
790
- toJSON() {
791
- const t = {
792
- id: this.id,
793
- type: this.type,
794
- source: this.source || null,
795
- subtype: this.subtype
796
- };
797
- return this.type === C.Pin ? (t.component = this.component || null, t.pinLabel = this.pinLabel || null) : t.position = this.position?.toJSON() || null, t;
798
- }
799
- static fromJSON(t) {
800
- const n = t.position ? M.fromJSON(t.position) : void 0, i = new H(t.type, t.component || void 0, t.pinLabel || void 0, n, t.source || void 0, t.subtype ?? "free");
801
- return Object.defineProperty(i, "id", {
802
- value: t.id,
803
- writable: !1,
804
- enumerable: !0,
805
- configurable: !1
806
- }), i;
807
- }
808
- }, E = class Q {
809
- id;
810
- node1;
811
- node2;
812
- intermediatePositions;
813
- constructor(t, n, i = []) {
814
- this.id = G(), this.node1 = t, this.node2 = n, this.intermediatePositions = i;
815
- }
816
- isStraightLine() {
817
- return this.intermediatePositions.length === 0;
818
- }
819
- toJSON() {
820
- return {
821
- id: this.id,
822
- node1: this.node1,
823
- node2: this.node2,
824
- intermediatePositions: this.intermediatePositions.map((t) => t.toJSON())
825
- };
826
- }
827
- static fromJSON(t) {
828
- const n = t.intermediatePositions.map((s) => M.fromJSON(s)), i = new Q(t.node1, t.node2, n);
829
- return Object.defineProperty(i, "id", {
830
- value: t.id,
831
- writable: !1,
832
- enumerable: !0,
833
- configurable: !1
834
- }), i;
835
- }
836
- }, _ = class q {
837
- id;
838
- type;
839
- position;
840
- rotation;
841
- pins;
842
- config;
843
- editable;
844
- constructor(t, n, i, s, o = !0) {
845
- if (this.id = G(), this.type = t, this.position = n, this.rotation = i, new Set(s).size !== s.length) {
846
- const r = s.filter((a, c) => s.indexOf(a) !== c);
847
- throw new Error(`Duplicate pin names are not allowed: ${[...new Set(r)].join(", ")}`);
848
- }
849
- this.pins = s, this.config = new Map(N[t].config), this.editable = o;
850
- }
851
- getPinLabel(t) {
852
- const n = this.pins.indexOf(t);
853
- if (n === -1) return;
854
- const i = N[this.type].pins.keys();
855
- return Array.from(i)[n] || void 0;
856
- }
857
- setAllParameters(t) {
858
- this.config = new Map(t);
859
- }
860
- setParameter(t, n) {
861
- this.config.set(t, n);
862
- }
863
- setPosition(t) {
864
- Object.defineProperty(this, "position", {
865
- value: t,
866
- writable: !1,
867
- enumerable: !0,
868
- configurable: !0
869
- });
870
- }
871
- setRotation(t) {
872
- Object.defineProperty(this, "rotation", {
873
- value: t,
874
- writable: !1,
875
- enumerable: !0,
876
- configurable: !0
877
- });
878
- }
879
- toJSON() {
880
- return {
881
- id: this.id,
882
- type: this.type,
883
- position: this.position.toJSON(),
884
- rotation: this.rotation.toJSON(),
885
- pins: [...this.pins],
886
- config: Object.fromEntries(this.config),
887
- editable: this.editable
888
- };
889
- }
890
- static fromJSON(t) {
891
- const n = new q(t.type, M.fromJSON(t.position), rt.fromJSON(t.rotation), t.pins, t.editable);
892
- return n.config = new Map(Object.entries(t.config)), Object.defineProperty(n, "id", {
893
- value: t.id,
894
- writable: !1,
895
- enumerable: !0,
896
- configurable: !1
897
- }), n;
898
- }
899
- }, R = class K {
900
- constructor(t = "Untitled Circuit", n = U) {
901
- this.name = t, this.defaultLogicFamily = n;
902
- }
903
- toJSON() {
904
- return {
905
- name: this.name,
906
- defaultLogicFamily: this.defaultLogicFamily
907
- };
908
- }
909
- static fromJSON(t) {
910
- return new K(t.name, t.defaultLogicFamily);
911
- }
912
- }, D = class Y {
913
- constructor(t, n, i, s, o) {
914
- if (this.version = t, this.options = n, this.size = i, this.divisions = s, this.cameraOptions = o, !Number.isInteger(i) || !Number.isInteger(s)) throw new TypeError(`Size and divisions must be integers (got size=${i}, divisions=${s})`);
915
- }
916
- toJSON() {
917
- return {
918
- version: this.version,
919
- options: this.options.toJSON(),
920
- size: this.size,
921
- divisions: this.divisions,
922
- cameraOptions: this.cameraOptions.toJSON()
923
- };
924
- }
925
- static fromJSON(t) {
926
- t.version !== "0.0.11" && console.warn(`This version of the engine supports v${X} circuit version files.
927
- Unexpected behavior may occurs loading v${t.version}.`);
928
- const n = t.options ? R.fromJSON(t.options) : new R("Untitled Circuit", U);
929
- return new Y(t.version, n, t.size, t.divisions, $.fromJSON(t.cameraOptions));
930
- }
931
- toString() {
932
- return `CircuitMetadata(${this.version}, ${this.options.name}, ${this.options.defaultLogicFamily}, ${this.size}, ${this.divisions}, ${this.cameraOptions.toString()})`;
933
- }
934
- }, at = {
935
- 2: 1,
936
- 4: 1,
937
- 8: 2,
938
- 16: 2
939
- }, ct = {
940
- 2: 2,
941
- 4: 2,
942
- 8: 3,
943
- 16: 3
944
- }, ut = {
945
- 2: 1,
946
- 4: 2,
947
- 8: 2,
948
- 16: 3
949
- }, ht = {
950
- 2: 2,
951
- 4: 3,
952
- 8: 3,
953
- 16: 4
954
- };
955
- function dt(e) {
956
- const t = e.config.get("defaultLogicFamily");
957
- if (!(!t || t === "Sandbox") && pt(e.type)) {
958
- const n = e.config.get("activationLogic") ?? "negative", i = lt(e.type, n);
959
- return i && gt(t, i.gateFamily, i.inputCount) || void 0;
960
- }
961
- }
962
- function pt(e) {
963
- return [
964
- u.Inverter,
965
- u.NandGate,
966
- u.Nand4Gate,
967
- u.Nand8Gate,
968
- u.NorGate,
969
- u.Nor4Gate,
970
- u.Nor8Gate,
971
- u.XorGate,
972
- u.Xor4Gate,
973
- u.Xor8Gate
974
- ].includes(e);
975
- }
976
- function lt(e, t) {
977
- switch (e) {
978
- case u.Inverter:
979
- return {
980
- gateFamily: t === "negative" ? "NOT" : "Buffer",
981
- inputCount: 1
982
- };
983
- case u.NandGate:
984
- return {
985
- gateFamily: t === "negative" ? "NAND" : "AND",
986
- inputCount: 2
987
- };
988
- case u.Nand4Gate:
989
- return {
990
- gateFamily: t === "negative" ? "NAND" : "AND",
991
- inputCount: 4
992
- };
993
- case u.Nand8Gate:
994
- return {
995
- gateFamily: t === "negative" ? "NAND" : "AND",
996
- inputCount: 8
997
- };
998
- case u.NorGate:
999
- return {
1000
- gateFamily: t === "negative" ? "NOR" : "OR",
1001
- inputCount: 2
1002
- };
1003
- case u.Nor4Gate:
1004
- return {
1005
- gateFamily: t === "negative" ? "NOR" : "OR",
1006
- inputCount: 4
1007
- };
1008
- case u.Nor8Gate:
1009
- return {
1010
- gateFamily: t === "negative" ? "NOR" : "OR",
1011
- inputCount: 8
1012
- };
1013
- case u.XorGate:
1014
- return {
1015
- gateFamily: t === "negative" ? "XNOR" : "XOR",
1016
- inputCount: 2
1017
- };
1018
- case u.Xor4Gate:
1019
- return {
1020
- gateFamily: t === "negative" ? "XNOR" : "XOR",
1021
- inputCount: 4
1022
- };
1023
- case u.Xor8Gate:
1024
- return {
1025
- gateFamily: t === "negative" ? "XNOR" : "XOR",
1026
- inputCount: 8
1027
- };
1028
- default:
1029
- return null;
1030
- }
1031
- }
1032
- function gt(e, t, n) {
1033
- if (e === "Sandbox") throw new Error("computeGateDelay must not be called for Sandbox family");
1034
- if (t === "NOT") return 1;
1035
- if (t === "Buffer") return 2;
1036
- if (t === "XOR") return Math.log2(n) * 2;
1037
- if (t === "XNOR") return Math.log2(n) * 2 + 1;
1038
- if (e === "CMOS1") {
1039
- const i = Math.log2(n);
1040
- switch (t) {
1041
- case "NAND":
1042
- return i;
1043
- case "NOR":
1044
- return i;
1045
- case "AND":
1046
- return i + 1;
1047
- case "OR":
1048
- return i + 1;
1049
- }
1050
- }
1051
- if (e === "TTL1") {
1052
- let i;
1053
- switch (t) {
1054
- case "NAND":
1055
- i = at;
1056
- break;
1057
- case "AND":
1058
- i = ct;
1059
- break;
1060
- case "NOR":
1061
- i = ut;
1062
- break;
1063
- case "OR":
1064
- i = ht;
1065
- break;
1066
- }
1067
- const s = i[n];
1068
- if (s === void 0) throw new Error(`Unsupported input count ${n} for TTL1 ${t}`);
1069
- return s;
1070
- }
1071
- throw new Error(`Unsupported logic family: ${e}`);
1072
- }
1073
- var ie = class Z {
1074
- metadata;
1075
- components;
1076
- enodes;
1077
- wires;
1078
- constructor(t) {
1079
- this.metadata = new D(X, t, 10, 10, new $()), this.components = /* @__PURE__ */ new Map(), this.enodes = /* @__PURE__ */ new Map(), this.wires = /* @__PURE__ */ new Map();
1080
- }
1081
- get name() {
1082
- return this.metadata.options.name;
1083
- }
1084
- set name(t) {
1085
- if (typeof t != "string" || t.trim() === "") throw new TypeError("Circuit name must be a non-empty string");
1086
- this.metadata.options.name = t;
1087
- }
1088
- addComponent(t, n, i, s) {
1089
- const o = N[t], r = new _(t, n, i, []);
1090
- if (s) for (const [c, d] of s) r.config.set(c, d);
1091
- r.config.has("defaultLogicFamily") && !r.config.get("defaultLogicFamily") && r.config.set("defaultLogicFamily", this.metadata.options.defaultLogicFamily), this.resolveTransitionSpan(r);
1092
- const a = [];
1093
- for (const [c, d] of o.pins) {
1094
- const h = new x(C.Pin, r.id, c, void 0, d.sourceType, d.subtype);
1095
- this.enodes.set(h.id, h), a.push(h.id);
1096
- }
1097
- return Object.defineProperty(r, "pins", {
1098
- value: a,
1099
- writable: !1,
1100
- enumerable: !0,
1101
- configurable: !1
1102
- }), this.components.set(r.id, r), r;
1103
- }
1104
- resolveTransitionSpan(t) {
1105
- if (!t.config.has("transitionSpan")) return;
1106
- const n = dt(t);
1107
- n && t.config.set("transitionSpan", String(n));
1108
- }
1109
- removeComponent(t) {
1110
- const n = this.components.get(t);
1111
- if (!n) throw new Error(`Component ${t} does not exist`);
1112
- const i = [], s = [];
1113
- for (const o of n.pins) {
1114
- const r = this.enodes.get(o);
1115
- if (r) {
1116
- const a = Array.from(r.wires);
1117
- for (const c of a)
1118
- this.removeWire(c), i.push(c);
1119
- }
1120
- this.enodes.delete(o), s.push(o);
1121
- }
1122
- return this.components.delete(t), {
1123
- deletedWires: i,
1124
- deletedENodes: s
1125
- };
1126
- }
1127
- hasComponent(t) {
1128
- return this.components.has(t);
1129
- }
1130
- getComponent(t) {
1131
- return this.components.get(t);
1132
- }
1133
- getAllComponents() {
1134
- return Array.from(this.components.values());
1135
- }
1136
- getAllComponentsByType(t) {
1137
- const n = [];
1138
- for (const i of this.components.values()) i.type === t && n.push(i);
1139
- return n;
1140
- }
1141
- getFirstComponentOfType(t) {
1142
- for (const n of this.components.values()) if (n.type === t) return n;
1143
- }
1144
- getENode(t) {
1145
- return this.enodes.get(t);
1146
- }
1147
- getAllENodes() {
1148
- return Array.from(this.enodes.values());
1149
- }
1150
- addBranchingPoint(t, n) {
1151
- const i = new x(C.BranchingPoint, void 0, void 0, t, n);
1152
- return this.enodes.set(i.id, i), i;
1153
- }
1154
- removeBranchingPoint(t) {
1155
- const n = this.enodes.get(t);
1156
- if (!n) throw new Error(`Enode ${t} does not exist`);
1157
- if (n.type !== C.BranchingPoint) throw new Error(`Enode ${t} is not a branching point, it must be removed with its component.`);
1158
- const i = {}, s = this.getWiresByNode(t);
1159
- if (s.length === 1 || s.length > 2) {
1160
- const o = [];
1161
- for (const r of s)
1162
- this.removeWire(r.id), o.push(r.id);
1163
- Object.assign(i, { deletedWires: o });
1164
- } else if (s.length === 2) {
1165
- const o = s[0], r = s[1], a = o.node1 === t ? o.node2 : o.node1, c = r.node1 === t ? r.node2 : r.node1, d = [];
1166
- a === o.node1 ? d.push(...o.intermediatePositions) : a === o.node2 && d.push(...[...o.intermediatePositions].reverse()), d.push(n.getPosition(this)), c === r.node1 ? d.push(...[...r.intermediatePositions].reverse()) : c === r.node2 && d.push(...r.intermediatePositions), this.removeWire(o.id), this.removeWire(r.id);
1167
- const h = this.addWire(a, c, d);
1168
- if (h instanceof Error) throw new Error(`Failed to merge wires at branching point ${t}: ${h.message}`);
1169
- Object.assign(i, { mergedWires: [o.id, r.id] }), Object.assign(i, { newWire: h });
1170
- }
1171
- return this.enodes.delete(t), i;
1172
- }
1173
- addWire(t, n, i) {
1174
- if (t === n) return /* @__PURE__ */ new Error("Cannot create wire connecting node to itself");
1175
- const s = this.enodes.get(t), o = this.enodes.get(n);
1176
- if (!s || !o) return /* @__PURE__ */ new Error("Wire requires at least one existing ENode");
1177
- if (this.hasWireBetween(t, n)) return /* @__PURE__ */ new Error("Duplicate wire between same nodes");
1178
- const r = new E(t, n, i || []);
1179
- return this.wires.set(r.id, r), s.wires.add(r.id), o.wires.add(r.id), r;
1180
- }
1181
- removeWire(t) {
1182
- const n = this.wires.get(t);
1183
- if (!n) throw new Error(`Wire ${t} does not exist`);
1184
- const i = this.enodes.get(n.node1), s = this.enodes.get(n.node2);
1185
- i && i.wires.delete(t), s && s.wires.delete(t), this.wires.delete(t);
1186
- }
1187
- splitWire(t, n, i = null) {
1188
- const s = this.wires.get(t);
1189
- if (!s) throw new Error(`Wire ${t} does not exist`);
1190
- const o = this.enodes.get(s.node1), r = this.enodes.get(s.node2);
1191
- if (!o || !r) throw new Error(`Wire ${t} is connected to non-existent ENodes`);
1192
- const a = [
1193
- o.getPosition(this),
1194
- ...s.intermediatePositions,
1195
- r.getPosition(this)
1196
- ], c = nt(a, n), d = a.slice(1, c), h = a.slice(c, a.length - 1);
1197
- this.wires.delete(t), o.wires.delete(t), r.wires.delete(t);
1198
- let p;
1199
- if (i) if (this.enodes.get(i)) p = this.enodes.get(i);
1200
- else throw new Error(`Target ENode ${i} does not exist`);
1201
- else p = this.addBranchingPoint(n);
1202
- const v = [];
1203
- if ((!p.component || o.component !== p.component) && !this.hasWireBetween(o.id, p.id)) {
1204
- const f = this.addWire(o.id, p.id, d);
1205
- f instanceof E ? (this.simplifyWireIntermediatePositions(f.id), v.push(f)) : console.warn(`Failure to create wire at split : ${f.message}`);
1206
- }
1207
- if ((!p.component || r.component !== p.component) && !this.hasWireBetween(r.id, p.id)) {
1208
- const f = this.addWire(p.id, r.id, h);
1209
- f instanceof E ? (this.simplifyWireIntermediatePositions(f.id), v.push(f)) : console.warn(`Failure to create wire at split : ${f.message}`);
1210
- }
1211
- return {
1212
- branchingPoint: p,
1213
- wires: v
1214
- };
1215
- }
1216
- getWireBetweenNodes(t, n) {
1217
- const i = this.enodes.get(t);
1218
- if (i)
1219
- for (const s of i.wires) {
1220
- const o = this.wires.get(s);
1221
- if (o && (o.node2 === n || o.node1 === n)) return o;
1222
- }
1223
- }
1224
- getWire(t) {
1225
- return this.wires.get(t);
1226
- }
1227
- getAllWires() {
1228
- return Array.from(this.wires.values());
1229
- }
1230
- getWiresByNode(t) {
1231
- const n = this.enodes.get(t);
1232
- if (!n) return [];
1233
- const i = [];
1234
- for (const s of n.wires) {
1235
- const o = this.wires.get(s);
1236
- o && i.push(o);
1237
- }
1238
- return i;
1239
- }
1240
- getWiresByComponent(t) {
1241
- const n = this.components.get(t);
1242
- if (!n) return [];
1243
- const i = [];
1244
- for (const s of n.pins) i.push(...this.getWiresByNode(s));
1245
- return i;
1246
- }
1247
- getNodesByWire(t) {
1248
- const n = this.wires.get(t);
1249
- if (!n) return;
1250
- const i = this.enodes.get(n.node1), s = this.enodes.get(n.node2);
1251
- if (!(!i || !s))
1252
- return [i, s];
1253
- }
1254
- hasWireBetween(t, n) {
1255
- const i = this.enodes.get(t);
1256
- if (!i) return !1;
1257
- for (const s of i.wires) {
1258
- const o = this.wires.get(s);
1259
- if (o && (o.node2 === n || o.node1 === n)) return !0;
1260
- }
1261
- return !1;
1262
- }
1263
- getComponentsOfPins(t) {
1264
- const n = /* @__PURE__ */ new Set();
1265
- for (const i of t) {
1266
- const s = this.enodes.get(i);
1267
- s?.component && n.add(s.component);
1268
- }
1269
- return n;
1270
- }
1271
- getComponentPinByLabel(t, n) {
1272
- let i = 0;
1273
- const s = N[t.type], o = Array.from(s.pins.keys());
1274
- for (const r of t.pins) {
1275
- const a = this.enodes.get(r), c = o[i];
1276
- if (c) {
1277
- if (a && c === n) return a;
1278
- i++;
1279
- }
1280
- }
1281
- }
1282
- updateWireIntermediatePositions(t, n, i = !1) {
1283
- const s = this.wires.get(t);
1284
- if (!s) throw new Error(`Wire ${t} does not exist`);
1285
- if (i) {
1286
- const o = L([
1287
- this.enodes.get(s.node1).getPosition(this),
1288
- ...n,
1289
- this.enodes.get(s.node2).getPosition(this)
1290
- ], 10);
1291
- s.intermediatePositions = o.slice(1, o.length - 1);
1292
- } else s.intermediatePositions = n;
1293
- return s;
1294
- }
1295
- simplifyWireIntermediatePositions(t) {
1296
- const n = this.wires.get(t);
1297
- if (!n) throw new Error(`Wire ${t} does not exist`);
1298
- const i = L([
1299
- this.enodes.get(n.node1).getPosition(this),
1300
- ...n.intermediatePositions,
1301
- this.enodes.get(n.node2).getPosition(this)
1302
- ], 5);
1303
- return n.intermediatePositions = i.slice(1, i.length - 1), n.intermediatePositions = L(n.intermediatePositions), n;
1304
- }
1305
- updateENodeSourceType(t, n) {
1306
- const i = this.enodes.get(t);
1307
- if (!i) throw new Error(`ENode ${t} does not exist`);
1308
- i.source = n || void 0;
1309
- }
1310
- getEnclosingSize(t = 0) {
1311
- let n = 0;
1312
- for (const i of this.components.values()) n = Math.max(n, Math.abs(i.position.x), Math.abs(i.position.y));
1313
- for (const i of this.enodes.values()) {
1314
- if (i.type === C.Pin) continue;
1315
- const s = i.position;
1316
- s && (n = Math.max(n, Math.abs(s.x), Math.abs(s.y)));
1317
- }
1318
- for (const i of this.wires.values()) for (const s of i.intermediatePositions) n = Math.max(n, Math.abs(s.x), Math.abs(s.y));
1319
- return Math.ceil(n * 2 + Math.max(t, 0));
1320
- }
1321
- toJSON() {
1322
- return {
1323
- metadata: this.metadata.toJSON(),
1324
- components: this.getAllComponents().map((t) => t.toJSON()),
1325
- enodes: this.getAllENodes().map((t) => t.toJSON()),
1326
- wires: this.getAllWires().map((t) => t.toJSON())
1327
- };
1328
- }
1329
- static fromJSON(t) {
1330
- const n = D.fromJSON(t.metadata), i = new Z(n.options);
1331
- i.metadata = n;
1332
- for (const s of t.components) {
1333
- const o = _.fromJSON(s);
1334
- i.components.set(o.id, o);
1335
- }
1336
- for (const s of t.enodes) {
1337
- const o = x.fromJSON(s);
1338
- i.enodes.set(o.id, o);
1339
- }
1340
- if (t.wires) for (const s of t.wires) {
1341
- const o = E.fromJSON(s);
1342
- i.wires.set(o.id, o);
1343
- const r = i.enodes.get(o.node1), a = i.enodes.get(o.node2);
1344
- r && r.wires.add(o.id), a && a.wires.add(o.id);
1345
- }
1346
- return i;
1347
- }
1348
- }, ne = {
1349
- MIN_TPS: 1,
1350
- MAX_TPS: 50,
1351
- DEFAULT_TPS: 3,
1352
- DEFAULT_INTERVAL_MS: 500
1353
- }, j = {
1354
- TRANSITION_SPAN_TICKS: 1,
1355
- TRANSITION_USER_SPAN_MS: 200
1356
- };
1357
- function I(...e) {
1358
- return {
1359
- hasVoltage: e.some((t) => t.hasVoltage),
1360
- hasCurrent: e.some((t) => t.hasCurrent),
1361
- locked: !1
1362
- };
1363
- }
1364
- var V = class tt {
1365
- tick;
1366
- nodeStates;
1367
- wireStates;
1368
- componentStates;
1369
- constructor(t) {
1370
- if (t < 0 || !Number.isInteger(t)) throw new RangeError(`Tick must be a non-negative integer (got ${t})`);
1371
- this.tick = t, this.nodeStates = /* @__PURE__ */ new Map(), this.wireStates = /* @__PURE__ */ new Map(), this.componentStates = /* @__PURE__ */ new Map();
1372
- }
1373
- setTick(t) {
1374
- this.tick = t;
1375
- }
1376
- clone() {
1377
- const t = new tt(this.tick), n = /* @__PURE__ */ new Map();
1378
- for (const [o, r] of this.nodeStates.entries()) n.set(o, { ...r });
1379
- const i = /* @__PURE__ */ new Map();
1380
- for (const [o, r] of this.wireStates.entries()) i.set(o, { ...r });
1381
- const s = /* @__PURE__ */ new Map();
1382
- for (const [o, r] of this.componentStates.entries()) s.set(o, Object.assign(Object.create(Object.getPrototypeOf(r)), r));
1383
- return Object.defineProperty(t, "nodeStates", {
1384
- value: n,
1385
- writable: !1,
1386
- configurable: !1,
1387
- enumerable: !0
1388
- }), Object.defineProperty(t, "wireStates", {
1389
- value: i,
1390
- writable: !1,
1391
- configurable: !1,
1392
- enumerable: !0
1393
- }), Object.defineProperty(t, "componentStates", {
1394
- value: s,
1395
- writable: !1,
1396
- configurable: !1,
1397
- enumerable: !0
1398
- }), t;
1399
- }
1400
- }, b = class {
1401
- componentId;
1402
- _state;
1403
- _startTick;
1404
- _expirationTick;
1405
- _nextState;
1406
- parameters = /* @__PURE__ */ new Map();
1407
- pinStates = /* @__PURE__ */ new Map();
1408
- constructor(e, t) {
1409
- this.componentId = e, this._state = t, this._startTick = 0, this._expirationTick = -1, this._nextState = null;
1410
- }
1411
- get state() {
1412
- return this._state;
1413
- }
1414
- setState(e, t) {
1415
- this._state = e, this._startTick = t, this._expirationTick = -1, this._nextState = null;
1416
- }
1417
- get startTick() {
1418
- return this._startTick;
1419
- }
1420
- get expirationTick() {
1421
- return this._expirationTick;
1422
- }
1423
- get nextState() {
1424
- return this._nextState;
1425
- }
1426
- get hasExpiration() {
1427
- return this._expirationTick >= 0 && !!this._nextState && this._nextState !== this.state;
1428
- }
1429
- setNextState(e, t) {
1430
- this._nextState = e, this._expirationTick = t;
1431
- }
1432
- }, ft = class extends b {
1433
- constructor(e) {
1434
- super(e, "on");
1435
- }
1436
- }, yt = class extends b {
1437
- constructor(e, t = "off") {
1438
- super(e, t);
1439
- }
1440
- get isLit() {
1441
- return this.state === "on" || this.state === "goingOn";
1442
- }
1443
- }, et = class extends b {
1444
- constructor(e, t = "off") {
1445
- super(e, t);
1446
- }
1447
- get isLit() {
1448
- return this.state === "on" || this.state === "goingOn";
1449
- }
1450
- }, vt = class extends et {
1451
- }, wt = class extends b {
1452
- constructor(e, t = "open") {
1453
- super(e, t);
1454
- }
1455
- get isInTransition() {
1456
- return this.state === "closing" || this.state === "opening";
1457
- }
1458
- get isClosed() {
1459
- return this.state === "closed" || this.state === "closing";
1460
- }
1461
- }, St = class extends b {
1462
- constructor(e, t = "open") {
1463
- super(e, t);
1464
- }
1465
- get isInTransition() {
1466
- return this.state === "closing" || this.state === "opening";
1467
- }
1468
- get isClosed() {
1469
- return this.state === "closed" || this.state === "closing";
1470
- }
1471
- }, mt = class extends b {
1472
- constructor(e, t = "input1") {
1473
- super(e, t);
1474
- }
1475
- get isInTransition() {
1476
- return this.state === "1to2" || this.state === "2to1";
1477
- }
1478
- }, bt = class extends b {
1479
- constructor(e) {
1480
- super(e, "high");
1481
- }
1482
- }, S = class extends b {
1483
- constructor(e, t = "low") {
1484
- super(e, t);
1485
- }
1486
- get isInTransition() {
1487
- return this.state === "rising" || this.state === "falling";
1488
- }
1489
- get isHigh() {
1490
- return this.state === "high";
1491
- }
1492
- }, Ct = class extends S {
1493
- constructor(e, t = "low") {
1494
- super(e, t);
1495
- }
1496
- }, Tt = class extends S {
1497
- constructor(e, t = "low") {
1498
- super(e, t);
1499
- }
1500
- }, Nt = class extends S {
1501
- constructor(e, t = "low") {
1502
- super(e, t);
1503
- }
1504
- }, It = class extends S {
1505
- constructor(e, t = "low") {
1506
- super(e, t);
1507
- }
1508
- }, Pt = class extends S {
1509
- constructor(e, t = "low") {
1510
- super(e, t);
1511
- }
1512
- }, Et = class extends S {
1513
- constructor(e, t = "low") {
1514
- super(e, t);
1515
- }
1516
- }, Ot = class extends S {
1517
- constructor(e, t = "low") {
1518
- super(e, t);
1519
- }
1520
- }, kt = class extends S {
1521
- constructor(e, t = "low") {
1522
- super(e, t);
1523
- }
1524
- }, Lt = class extends S {
1525
- constructor(e, t = "low") {
1526
- super(e, t);
1527
- }
1528
- }, xt = class extends S {
1529
- constructor(e, t = "low") {
1530
- super(e, t);
1531
- }
1532
- }, se = class {
1533
- behaviors;
1534
- constructor() {
1535
- this.behaviors = /* @__PURE__ */ new Map();
1536
- }
1537
- register(e) {
1538
- if (!e) throw new TypeError("Behavior cannot be null or undefined");
1539
- if (!e.componentType || e.componentType.trim() === "") throw new TypeError("Behavior componentType cannot be empty");
1540
- return this.behaviors.set(e.componentType, e), this;
1541
- }
1542
- registerAll(e) {
1543
- e.forEach((t) => this.register(t));
1544
- }
1545
- get(e) {
1546
- return this.behaviors.get(e);
1547
- }
1548
- has(e) {
1549
- return this.behaviors.has(e);
1550
- }
1551
- unregister(e) {
1552
- return this.behaviors.delete(e);
1553
- }
1554
- clear() {
1555
- this.behaviors.clear();
1556
- }
1557
- getRegisteredTypes() {
1558
- return Array.from(this.behaviors.keys());
1559
- }
1560
- size() {
1561
- return this.behaviors.size;
1562
- }
1563
- }, T = class {
1564
- _componentType;
1565
- constructor(e) {
1566
- this._componentType = e;
1567
- }
1568
- get componentType() {
1569
- return this._componentType;
1570
- }
1571
- get typeMetadata() {
1572
- const e = N[this._componentType];
1573
- if (!e) throw new Error(`Unknown metadata for Component type ${this._componentType}`);
1574
- return e;
1575
- }
1576
- getPinStates(e, t) {
1577
- const n = /* @__PURE__ */ new Map();
1578
- for (const i of e.pins) n.set(e.getPinLabel(i), t.get(i));
1579
- return n;
1580
- }
1581
- getChangedPins(e, t) {
1582
- const n = /* @__PURE__ */ new Set();
1583
- for (const [i, s] of e) {
1584
- if (!t.has(i)) continue;
1585
- const o = t.get(i);
1586
- (s.hasVoltage !== o?.hasVoltage || s.hasCurrent !== o?.hasCurrent) && n.add(i);
1587
- }
1588
- return n;
1589
- }
1590
- onStart(e, t) {
1591
- return null;
1592
- }
1593
- onPinsChange(e, t, n, i) {
1594
- return {
1595
- componentState: t,
1596
- hasChanged: !1,
1597
- shouldCancelPending: !1,
1598
- scheduledEvents: []
1599
- };
1600
- }
1601
- allowConductivity(e, t, n, i, s) {
1602
- return !1;
1603
- }
1604
- onUserCommand(e, t, n) {
1605
- return {
1606
- componentState: t,
1607
- hasChanged: !1,
1608
- shouldCancelPending: !1,
1609
- scheduledEvents: []
1610
- };
1611
- }
1612
- onEventFiring(e, t, n) {
1613
- return {
1614
- componentState: t,
1615
- hasChanged: !1,
1616
- shouldCancelPending: !1,
1617
- scheduledEvents: []
1618
- };
1619
- }
1620
- };
1621
- function O(e) {
1622
- const t = parseInt(e.get("transitionSpan") || "", 10);
1623
- return isNaN(t) || t < 1 ? j.TRANSITION_SPAN_TICKS : t;
1624
- }
1625
- var Mt = class extends T {
1626
- constructor() {
1627
- super(u.Battery);
1628
- }
1629
- createInitialState(e) {
1630
- if (e.type !== this._componentType) throw new Error(`Invalid component type for BatteryBehavior: ${e.type}`);
1631
- return new ft(e.id);
1632
- }
1633
- }, A = class extends T {
1634
- getBehavior(e, t, n, i) {
1635
- let s = !1;
1636
- const o = [], r = O(e.config), a = t.expirationTick < 1 ? r : Math.max(i - t.startTick, 1);
1637
- return n ? (t.state === "off" || t.state === "goingOff") && (s = !0, t.setState("goingOn", i), t.setNextState("on", i + a), o.push({
1638
- targetId: e.id,
1639
- scheduledAtTick: t.startTick,
1640
- readyAtTick: t.expirationTick,
1641
- type: "GoingOnEnd",
1642
- parameters: void 0
1643
- })) : (t.state === "on" || t.state === "goingOn") && (s = !0, t.setState("goingOff", i), t.setNextState("off", i + a), o.push({
1644
- targetId: e.id,
1645
- scheduledAtTick: t.startTick,
1646
- readyAtTick: t.expirationTick,
1647
- type: "GoingOffEnd",
1648
- parameters: void 0
1649
- })), {
1650
- componentState: t,
1651
- hasChanged: s,
1652
- shouldCancelPending: !0,
1653
- scheduledEvents: o
1654
- };
1655
- }
1656
- allowConductivity(e, t, n, i, s) {
1657
- return !0;
1658
- }
1659
- onEventFiring(e, t, n) {
1660
- let i = !1;
1661
- return n.type === "GoingOffEnd" ? t.state !== "off" && (i = !0, t.setState("off", n.readyAtTick)) : n.type === "GoingOnEnd" && t.state !== "on" && (i = !0, t.setState("on", n.readyAtTick)), {
1662
- componentState: t,
1663
- hasChanged: i,
1664
- shouldCancelPending: !1,
1665
- scheduledEvents: []
1666
- };
1667
- }
1668
- }, Gt = class extends A {
1669
- constructor() {
1670
- super(u.Lightbulb);
1671
- }
1672
- createInitialState(e) {
1673
- if (e.type !== this._componentType) throw new Error(`Invalid component type for lightbulbBehavior: ${e.type}`);
1674
- return new yt(e.id);
1675
- }
1676
- onPinsChange(e, t, n, i) {
1677
- const s = this.getPinStates(e, n);
1678
- t.pinStates = s;
1679
- const o = I(s.get("pin1"), s.get("pin2")), r = o.hasVoltage && o.hasCurrent;
1680
- return this.getBehavior(e, t, r, i);
1681
- }
1682
- }, At = class extends A {
1683
- constructor() {
1684
- super(u.RectangleLED);
1685
- }
1686
- createInitialState(e) {
1687
- if (e.type !== this._componentType) throw new Error(`Invalid component type for RectangleLEDBehavior: ${e.type}`);
1688
- return new vt(e.id);
1689
- }
1690
- onPinsChange(e, t, n, i) {
1691
- const s = this.getPinStates(e, n);
1692
- t.pinStates = s;
1693
- const o = I(s.get("pin1"), s.get("pin2")), r = o.hasVoltage && o.hasCurrent;
1694
- return this.getBehavior(e, t, r, i);
1695
- }
1696
- };
1697
- function Bt(e) {
1698
- const t = parseInt(e.get("transitionSpan") || "", 10);
1699
- return isNaN(t) || t < 1 ? j.TRANSITION_SPAN_TICKS : t;
1700
- }
1701
- var _t = class extends T {
1702
- constructor() {
1703
- super(u.Relay);
1704
- }
1705
- createInitialState(e) {
1706
- if (e.type !== this._componentType) throw new Error(`Invalid component type for RelayBehavior: ${e.type}`);
1707
- const t = e.config.get("activationLogic") === "negative" ? "closed" : "open";
1708
- return new wt(e.id, t);
1709
- }
1710
- allowConductivity(e, t, n, i, s) {
1711
- if (i === s) return !0;
1712
- const o = e.getPinLabel(i), r = e.getPinLabel(s);
1713
- if (!o || !r) return !1;
1714
- const a = [o, r];
1715
- return a.includes("cmd_in") && a.includes("cmd_out") ? !0 : a.includes("power_in") && a.includes("power_out") ? t.state === "closed" || t.state === "opening" : !1;
1716
- }
1717
- onPinsChange(e, t, n, i) {
1718
- const s = this.getPinStates(e, n);
1719
- s.set("cmd_in*cmd_out", I(s.get("cmd_in"), s.get("cmd_out"))), s.set("power_in*power_out", I(s.get("power_in"), s.get("power_out")));
1720
- const o = t.pinStates;
1721
- t.pinStates = s;
1722
- const r = this.getChangedPins(s, o), a = s.get("cmd_in*cmd_out"), c = a.hasVoltage && a.hasCurrent, d = e.config.get("activationLogic") === "negative" ? !c : c;
1723
- let h = r.size > 0;
1724
- const p = [], v = Bt(e.config);
1725
- if (d) {
1726
- if (t.state === "open" || t.state === "opening") {
1727
- let f = t.state === "open" ? v : Math.max(i - t.startTick, 1);
1728
- h = !0, t.setState("closing", i), t.setNextState("closed", i + f), p.push({
1729
- targetId: e.id,
1730
- scheduledAtTick: t.startTick,
1731
- readyAtTick: t.expirationTick,
1732
- type: "ClosingEnd",
1733
- parameters: /* @__PURE__ */ new Map([["exclusive", "true"]])
1734
- });
1735
- }
1736
- } else if (t.state === "closed" || t.state === "closing") {
1737
- let f = t.state === "closed" ? v : Math.max(i - t.startTick, 1);
1738
- h = !0, t.setState("opening", i), t.setNextState("open", i + f), p.push({
1739
- targetId: e.id,
1740
- scheduledAtTick: t.startTick,
1741
- readyAtTick: t.expirationTick,
1742
- type: "OpeningEnd",
1743
- parameters: /* @__PURE__ */ new Map([["exclusive", "true"]])
1744
- });
1745
- }
1746
- return {
1747
- componentState: t,
1748
- hasChanged: h,
1749
- shouldCancelPending: h,
1750
- scheduledEvents: p
1751
- };
1752
- }
1753
- onEventFiring(e, t, n) {
1754
- let i = !1;
1755
- return n.type === "ClosingEnd" ? t.state !== "closed" && (t.setState("closed", n.readyAtTick), i = !0) : n.type === "OpeningEnd" && t.state !== "open" && (t.setState("open", n.readyAtTick), i = !0), {
1756
- componentState: t,
1757
- hasChanged: i,
1758
- shouldCancelPending: !1,
1759
- scheduledEvents: []
1760
- };
1761
- }
1762
- }, Rt = class extends A {
1763
- constructor() {
1764
- super(u.SmallLED);
1765
- }
1766
- createInitialState(e) {
1767
- if (e.type !== this._componentType) throw new Error(`Invalid component type for SmallLEDBehavior: ${e.type}`);
1768
- return new et(e.id);
1769
- }
1770
- onPinsChange(e, t, n, i) {
1771
- const s = this.getPinStates(e, n);
1772
- t.pinStates = s;
1773
- const o = I(s.get("pin1"), s.get("pin2")), r = o.hasVoltage && o.hasCurrent;
1774
- return this.getBehavior(e, t, r, i);
1775
- }
1776
- }, Dt = class extends T {
1777
- constructor() {
1778
- super(u.Switch);
1779
- }
1780
- createInitialState(e) {
1781
- if (e.type !== this._componentType) throw new Error(`Invalid component type for SwitchBehavior: ${e.type}`);
1782
- const t = e.config.get("initialState") || "open";
1783
- return new St(e.id, t);
1784
- }
1785
- allowConductivity(e, t, n, i, s) {
1786
- return t.state === "closed" || t.state === "opening";
1787
- }
1788
- onPinsChange(e, t, n, i) {
1789
- const s = this.getPinStates(e, n), o = t.pinStates;
1790
- t.pinStates = s;
1791
- const r = this.getChangedPins(s, o);
1792
- return r.size < 1 || !r.has("output") ? {
1793
- componentState: t,
1794
- hasChanged: !1,
1795
- shouldCancelPending: !1,
1796
- scheduledEvents: []
1797
- } : {
1798
- componentState: t,
1799
- hasChanged: !0,
1800
- shouldCancelPending: !1,
1801
- scheduledEvents: []
1802
- };
1803
- }
1804
- onUserCommand(e, t, n) {
1805
- let i = !1;
1806
- const s = [], o = O(e.config);
1807
- return n.type === "toggle_switch" && ["open", "closed"].includes(t.state) && (t.setState(t.state === "open" ? "closing" : "opening", n.scheduledAtTick), t.setNextState(t.state === "closing" ? "closed" : "open", n.scheduledAtTick + o), i = !0, s.push({
1808
- targetId: e.id,
1809
- scheduledAtTick: t.startTick,
1810
- readyAtTick: t.expirationTick,
1811
- type: t.state === "closing" ? "ClosingEnd" : "OpeningEnd",
1812
- parameters: /* @__PURE__ */ new Map([["exclusive", "true"]])
1813
- })), {
1814
- componentState: t,
1815
- hasChanged: i,
1816
- shouldCancelPending: !0,
1817
- scheduledEvents: s
1818
- };
1819
- }
1820
- onEventFiring(e, t, n) {
1821
- let i = !1;
1822
- return n.type === "ClosingEnd" ? t.state !== "closed" && (t.setState("closed", n.readyAtTick), i = !0) : n.type === "OpeningEnd" && t.state !== "open" && (t.setState("open", n.readyAtTick), i = !0), {
1823
- componentState: t,
1824
- hasChanged: i,
1825
- shouldCancelPending: !1,
1826
- scheduledEvents: []
1827
- };
1828
- }
1829
- }, Vt = class extends T {
1830
- constructor() {
1831
- super(u.DoubleThrowSwitch);
1832
- }
1833
- createInitialState(e) {
1834
- if (e.type !== this._componentType) throw new Error(`Invalid component type for DoubleThrowSwitchBehavior: ${e.type}`);
1835
- const t = e.config.get("initialState") || "input1";
1836
- return new mt(e.id, t);
1837
- }
1838
- allowConductivity(e, t, n, i, s) {
1839
- if (i === s) return !0;
1840
- const o = e.getPinLabel(i), r = e.getPinLabel(s);
1841
- if (!o || !r) return !1;
1842
- const a = [o, r];
1843
- return a.includes("output") && a.includes("input1") ? t.state === "input1" || t.state === "1to2" : a.includes("output") && a.includes("input2") ? t.state === "input2" || t.state === "2to1" : !1;
1844
- }
1845
- onPinsChange(e, t, n, i) {
1846
- const s = this.getPinStates(e, n), o = t.pinStates;
1847
- t.pinStates = s;
1848
- const r = this.getChangedPins(s, o);
1849
- return r.size < 1 || !r.has("output") ? {
1850
- componentState: t,
1851
- hasChanged: !1,
1852
- shouldCancelPending: !1,
1853
- scheduledEvents: []
1854
- } : {
1855
- componentState: t,
1856
- hasChanged: !0,
1857
- shouldCancelPending: !1,
1858
- scheduledEvents: []
1859
- };
1860
- }
1861
- onUserCommand(e, t, n) {
1862
- let i = !1;
1863
- const s = [], o = O(e.config);
1864
- return n.type === "toggle_switch" && ["input1", "input2"].includes(t.state) && (t.setState(t.state === "input1" ? "1to2" : "2to1", n.scheduledAtTick), t.setNextState(t.state === "1to2" ? "input2" : "input1", n.scheduledAtTick + o), i = !0, s.push({
1865
- targetId: e.id,
1866
- scheduledAtTick: t.startTick,
1867
- readyAtTick: t.expirationTick,
1868
- type: t.state === "1to2" ? "ContactedInput2" : "ContactedInput1",
1869
- parameters: /* @__PURE__ */ new Map([["exclusive", "true"]])
1870
- })), {
1871
- componentState: t,
1872
- hasChanged: i,
1873
- shouldCancelPending: !0,
1874
- scheduledEvents: s
1875
- };
1876
- }
1877
- onEventFiring(e, t, n) {
1878
- let i = !1;
1879
- return n.type === "ContactedInput2" ? t.state !== "input2" && (t.setState("input2", n.readyAtTick), i = !0) : n.type === "ContactedInput1" && t.state !== "input1" && (t.setState("input1", n.readyAtTick), i = !0), {
1880
- componentState: t,
1881
- hasChanged: i,
1882
- shouldCancelPending: !1,
1883
- scheduledEvents: []
1884
- };
1885
- }
1886
- }, Wt = class extends T {
1887
- constructor() {
1888
- super(u.Clock);
1889
- }
1890
- createInitialState(e) {
1891
- if (e.type !== this._componentType) throw new Error(`Invalid component type for ClockBehavior: ${e.type}`);
1892
- const t = new bt(e.id), n = e.config.get("startHigh") == "true";
1893
- t.setState(n ? "high" : "low", 0);
1894
- const i = Number(e.config.get("halfPeriod"));
1895
- return t.setNextState(n ? "low" : "high", i), t;
1896
- }
1897
- allowConductivity(e, t, n, i, s) {
1898
- if (i === s) return !0;
1899
- const o = e.getPinLabel(i), r = e.getPinLabel(s);
1900
- if (!o || !r) return !1;
1901
- const a = [o, r];
1902
- return a.includes("gnd") && a.includes("vcc") ? !1 : a.includes("gnd") && a.includes("output") ? t.state === "low" : a.includes("vcc") && a.includes("output") ? t.state === "high" : !1;
1903
- }
1904
- onStart(e, t) {
1905
- const i = Number(e.config.get("halfPeriod"));
1906
- return t.setNextState(t.state === "high" ? "low" : "high", 0 + i), {
1907
- componentState: t,
1908
- hasChanged: !0,
1909
- shouldCancelPending: !0,
1910
- scheduledEvents: [{
1911
- targetId: e.id,
1912
- scheduledAtTick: t.startTick,
1913
- readyAtTick: t.expirationTick,
1914
- type: "tick",
1915
- parameters: /* @__PURE__ */ new Map([["exclusive", "true"]])
1916
- }]
1917
- };
1918
- }
1919
- onEventFiring(e, t, n) {
1920
- if (n.type !== "tick") return {
1921
- componentState: t,
1922
- hasChanged: !1,
1923
- shouldCancelPending: !1,
1924
- scheduledEvents: []
1925
- };
1926
- const i = Number(e.config.get("halfPeriod"));
1927
- return t.setState(t.state === "high" ? "low" : "high", n.readyAtTick), t.setNextState(t.state === "high" ? "low" : "high", n.readyAtTick + i), {
1928
- componentState: t,
1929
- hasChanged: !0,
1930
- shouldCancelPending: !0,
1931
- scheduledEvents: [{
1932
- targetId: e.id,
1933
- scheduledAtTick: t.startTick,
1934
- readyAtTick: t.expirationTick,
1935
- type: "tick",
1936
- parameters: /* @__PURE__ */ new Map([["exclusive", "true"]])
1937
- }]
1938
- };
1939
- }
1940
- }, m = class extends T {
1941
- vccGuardBehavior(e, t, n) {
1942
- if (t.get("vcc").hasVoltage) return null;
1943
- let i = !1, s = !1;
1944
- return e.state !== "low" && (e.setState("low", n), i = !0, s = !0), {
1945
- componentState: e,
1946
- hasChanged: i,
1947
- shouldCancelPending: s,
1948
- scheduledEvents: []
1949
- };
1950
- }
1951
- nonLogicInputGuardBehavior(e, t, n) {
1952
- let i = !1;
1953
- const s = this.typeMetadata.pins;
1954
- for (const [a, c] of t) {
1955
- if (!s.has(a)) continue;
1956
- const d = s.get(a);
1957
- if (d && d?.subtype === "logicInput") {
1958
- if (c.hasVoltage && c.hasCurrent) {
1959
- i = !0;
1960
- break;
1961
- }
1962
- if (!c.hasVoltage && !c.hasCurrent) {
1963
- i = !0;
1964
- break;
1965
- }
1966
- }
1967
- }
1968
- if (!i) return null;
1969
- let o = !1, r = !1;
1970
- return e.state !== "indeterminate" && (e.setState("indeterminate", n), o = !0, r = !0), {
1971
- componentState: e,
1972
- hasChanged: o,
1973
- shouldCancelPending: r,
1974
- scheduledEvents: []
1975
- };
1976
- }
1977
- getBehavior(e, t, n, i) {
1978
- if (![
1979
- "low",
1980
- "rising",
1981
- "high",
1982
- "falling",
1983
- "indeterminate"
1984
- ].includes(t.state))
1985
- return t.setState("low", i), {
1986
- componentState: t,
1987
- hasChanged: !0,
1988
- shouldCancelPending: !0,
1989
- scheduledEvents: []
1990
- };
1991
- const s = O(e.config), o = t.expirationTick < 1 ? s : Math.max(i - t.startTick, 1);
1992
- if (n) {
1993
- if (t.state === "low" || t.state === "falling" || t.state === "indeterminate")
1994
- return t.setState("rising", i), t.setNextState("high", i + o), {
1995
- componentState: t,
1996
- hasChanged: !0,
1997
- shouldCancelPending: !0,
1998
- scheduledEvents: [{
1999
- targetId: e.id,
2000
- scheduledAtTick: t.startTick,
2001
- readyAtTick: t.expirationTick,
2002
- type: "RisingComplete",
2003
- parameters: void 0
2004
- }]
2005
- };
2006
- if (t.state === "rising" || t.state === "high") return {
2007
- componentState: t,
2008
- hasChanged: !1,
2009
- shouldCancelPending: !1,
2010
- scheduledEvents: []
2011
- };
2012
- }
2013
- return t.state === "falling" || t.state === "low" ? {
2014
- componentState: t,
2015
- hasChanged: !1,
2016
- shouldCancelPending: !1,
2017
- scheduledEvents: []
2018
- } : t.state === "high" || t.state === "rising" || t.state === "indeterminate" ? (t.setState("falling", i), t.setNextState("low", i + o), {
2019
- componentState: t,
2020
- hasChanged: !0,
2021
- shouldCancelPending: !0,
2022
- scheduledEvents: [{
2023
- targetId: e.id,
2024
- scheduledAtTick: t.startTick,
2025
- readyAtTick: t.expirationTick,
2026
- type: "FallingComplete",
2027
- parameters: void 0
2028
- }]
2029
- }) : (t.setState("low", i), {
2030
- componentState: t,
2031
- hasChanged: !0,
2032
- shouldCancelPending: !0,
2033
- scheduledEvents: []
2034
- });
2035
- }
2036
- allowConductivity(e, t, n, i, s) {
2037
- if (i === s) return !0;
2038
- const o = e.getPinLabel(i), r = e.getPinLabel(s);
2039
- if (!o || !r) return !1;
2040
- const a = [o, r];
2041
- return a.includes("vcc") && a.includes("output") ? t.state === "high" || t.state === "falling" : a.includes("gnd") && a.includes("output") ? t.state === "low" || t.state === "rising" : !1;
2042
- }
2043
- onEventFiring(e, t, n) {
2044
- let i = !1;
2045
- return n.type === "RisingComplete" ? t.state !== "high" && (i = !0, t.setState("high", n.readyAtTick)) : n.type === "FallingComplete" && t.state !== "low" && (i = !0, t.setState("low", n.readyAtTick)), {
2046
- componentState: t,
2047
- hasChanged: i,
2048
- shouldCancelPending: !1,
2049
- scheduledEvents: []
2050
- };
2051
- }
2052
- }, $t = class extends m {
2053
- constructor() {
2054
- super(u.Inverter);
2055
- }
2056
- createInitialState(e) {
2057
- if (e.type !== this._componentType) throw new Error(`Invalid component type for InverterBehavior: ${e.type}`);
2058
- const t = e.config.get("activationLogic") === "negative" ? "high" : "low";
2059
- return new Ct(e.id, t);
2060
- }
2061
- onPinsChange(e, t, n, i) {
2062
- const s = this.getPinStates(e, n);
2063
- t.pinStates = s;
2064
- const o = this.vccGuardBehavior(t, s, i);
2065
- if (o) return o;
2066
- const r = this.nonLogicInputGuardBehavior(t, s, i);
2067
- if (r) return r;
2068
- const a = s.get("input").hasVoltage, c = e.config.get("activationLogic") === "negative" ? !a : a;
2069
- return this.getBehavior(e, t, c, i);
2070
- }
2071
- }, zt = class extends m {
2072
- constructor() {
2073
- super(u.NandGate);
2074
- }
2075
- createInitialState(e) {
2076
- if (e.type !== this._componentType) throw new Error(`Invalid component type for NandGateBehavior: ${e.type}`);
2077
- const t = e.config.get("activationLogic") === "negative" ? "high" : "low";
2078
- return new Tt(e.id, t);
2079
- }
2080
- onPinsChange(e, t, n, i) {
2081
- const s = this.getPinStates(e, n);
2082
- t.pinStates = s;
2083
- const o = this.vccGuardBehavior(t, s, i);
2084
- if (o) return o;
2085
- const r = this.nonLogicInputGuardBehavior(t, s, i);
2086
- if (r) return r;
2087
- const a = s.get("input1").hasVoltage && s.get("input2").hasVoltage, c = e.config.get("activationLogic") === "negative" ? !a : a;
2088
- return this.getBehavior(e, t, c, i);
2089
- }
2090
- }, Ft = class extends m {
2091
- constructor() {
2092
- super(u.Nand4Gate);
2093
- }
2094
- createInitialState(e) {
2095
- if (e.type !== this._componentType) throw new Error(`Invalid component type for Nand4GateBehavior: ${e.type}`);
2096
- const t = e.config.get("activationLogic") === "negative" ? "high" : "low";
2097
- return new Nt(e.id, t);
2098
- }
2099
- onPinsChange(e, t, n, i) {
2100
- const s = this.getPinStates(e, n);
2101
- t.pinStates = s;
2102
- const o = this.vccGuardBehavior(t, s, i);
2103
- if (o) return o;
2104
- const r = this.nonLogicInputGuardBehavior(t, s, i);
2105
- if (r) return r;
2106
- const a = s.get("input1").hasVoltage && s.get("input2").hasVoltage && s.get("input3").hasVoltage && s.get("input4").hasVoltage, c = e.config.get("activationLogic") === "negative" ? !a : a;
2107
- return this.getBehavior(e, t, c, i);
2108
- }
2109
- }, Jt = class extends m {
2110
- constructor() {
2111
- super(u.Nand8Gate);
2112
- }
2113
- createInitialState(e) {
2114
- if (e.type !== this._componentType) throw new Error(`Invalid component type for Nand8GateBehavior: ${e.type}`);
2115
- const t = e.config.get("activationLogic") === "negative" ? "high" : "low";
2116
- return new It(e.id, t);
2117
- }
2118
- onPinsChange(e, t, n, i) {
2119
- const s = this.getPinStates(e, n);
2120
- t.pinStates = s;
2121
- const o = this.vccGuardBehavior(t, s, i);
2122
- if (o) return o;
2123
- const r = this.nonLogicInputGuardBehavior(t, s, i);
2124
- if (r) return r;
2125
- const a = s.get("input1").hasVoltage && s.get("input2").hasVoltage && s.get("input3").hasVoltage && s.get("input4").hasVoltage && s.get("input5").hasVoltage && s.get("input6").hasVoltage && s.get("input7").hasVoltage && s.get("input8").hasVoltage, c = e.config.get("activationLogic") === "negative" ? !a : a;
2126
- return this.getBehavior(e, t, c, i);
2127
- }
2128
- }, Xt = class extends m {
2129
- constructor() {
2130
- super(u.NorGate);
2131
- }
2132
- createInitialState(e) {
2133
- if (e.type !== this._componentType) throw new Error(`Invalid component type for NorGateBehavior: ${e.type}`);
2134
- const t = e.config.get("activationLogic") === "negative" ? "high" : "low";
2135
- return new Pt(e.id, t);
2136
- }
2137
- onPinsChange(e, t, n, i) {
2138
- const s = this.getPinStates(e, n);
2139
- t.pinStates = s;
2140
- const o = this.vccGuardBehavior(t, s, i);
2141
- if (o) return o;
2142
- const r = this.nonLogicInputGuardBehavior(t, s, i);
2143
- if (r) return r;
2144
- const a = s.get("input1").hasVoltage || s.get("input2").hasVoltage, c = e.config.get("activationLogic") === "negative" ? !a : a;
2145
- return this.getBehavior(e, t, c, i);
2146
- }
2147
- }, Ut = class extends m {
2148
- constructor() {
2149
- super(u.Nor4Gate);
2150
- }
2151
- createInitialState(e) {
2152
- if (e.type !== this._componentType) throw new Error(`Invalid component type for Nor4GateBehavior: ${e.type}`);
2153
- const t = e.config.get("activationLogic") === "negative" ? "high" : "low";
2154
- return new Et(e.id, t);
2155
- }
2156
- onPinsChange(e, t, n, i) {
2157
- const s = this.getPinStates(e, n);
2158
- t.pinStates = s;
2159
- const o = this.vccGuardBehavior(t, s, i);
2160
- if (o) return o;
2161
- const r = this.nonLogicInputGuardBehavior(t, s, i);
2162
- if (r) return r;
2163
- const a = s.get("input1").hasVoltage || s.get("input2").hasVoltage || s.get("input3").hasVoltage || s.get("input4").hasVoltage, c = e.config.get("activationLogic") === "negative" ? !a : a;
2164
- return this.getBehavior(e, t, c, i);
2165
- }
2166
- }, Ht = class extends m {
2167
- constructor() {
2168
- super(u.Nor8Gate);
2169
- }
2170
- createInitialState(e) {
2171
- if (e.type !== this._componentType) throw new Error(`Invalid component type for Nor8GateBehavior: ${e.type}`);
2172
- const t = e.config.get("activationLogic") === "negative" ? "high" : "low";
2173
- return new Ot(e.id, t);
2174
- }
2175
- onPinsChange(e, t, n, i) {
2176
- const s = this.getPinStates(e, n);
2177
- t.pinStates = s;
2178
- const o = this.vccGuardBehavior(t, s, i);
2179
- if (o) return o;
2180
- const r = this.nonLogicInputGuardBehavior(t, s, i);
2181
- if (r) return r;
2182
- const a = s.get("input1").hasVoltage || s.get("input2").hasVoltage || s.get("input3").hasVoltage || s.get("input4").hasVoltage || s.get("input5").hasVoltage || s.get("input6").hasVoltage || s.get("input7").hasVoltage || s.get("input8").hasVoltage, c = e.config.get("activationLogic") === "negative" ? !a : a;
2183
- return this.getBehavior(e, t, c, i);
2184
- }
2185
- }, Qt = class extends m {
2186
- constructor() {
2187
- super(u.XorGate);
2188
- }
2189
- createInitialState(e) {
2190
- if (e.type !== this._componentType) throw new Error(`Invalid component type for XorGateBehavior: ${e.type}`);
2191
- const t = e.config.get("activationLogic") === "negative" ? "high" : "low";
2192
- return new kt(e.id, t);
2193
- }
2194
- onPinsChange(e, t, n, i) {
2195
- const s = this.getPinStates(e, n);
2196
- t.pinStates = s;
2197
- const o = this.vccGuardBehavior(t, s, i);
2198
- if (o) return o;
2199
- const r = this.nonLogicInputGuardBehavior(t, s, i);
2200
- if (r) return r;
2201
- const a = s.get("input1").hasVoltage !== s.get("input2").hasVoltage, c = e.config.get("activationLogic") === "negative" ? !a : a;
2202
- return this.getBehavior(e, t, c, i);
2203
- }
2204
- }, qt = class extends m {
2205
- constructor() {
2206
- super(u.Xor4Gate);
2207
- }
2208
- createInitialState(e) {
2209
- if (e.type !== this._componentType) throw new Error(`Invalid component type for Xor4GateBehavior: ${e.type}`);
2210
- const t = e.config.get("activationLogic") === "negative" ? "high" : "low";
2211
- return new Lt(e.id, t);
2212
- }
2213
- onPinsChange(e, t, n, i) {
2214
- const s = this.getPinStates(e, n);
2215
- t.pinStates = s;
2216
- const o = this.vccGuardBehavior(t, s, i);
2217
- if (o) return o;
2218
- const r = this.nonLogicInputGuardBehavior(t, s, i);
2219
- if (r) return r;
2220
- const a = ((s.get("input1").hasVoltage ? 1 : 0) + (s.get("input2").hasVoltage ? 1 : 0) + (s.get("input3").hasVoltage ? 1 : 0) + (s.get("input4").hasVoltage ? 1 : 0)) % 2 === 1, c = e.config.get("activationLogic") === "negative" ? !a : a;
2221
- return this.getBehavior(e, t, c, i);
2222
- }
2223
- }, Kt = class extends m {
2224
- constructor() {
2225
- super(u.Xor8Gate);
2226
- }
2227
- createInitialState(e) {
2228
- if (e.type !== this._componentType) throw new Error(`Invalid component type for Xor8GateBehavior: ${e.type}`);
2229
- const t = e.config.get("activationLogic") === "negative" ? "high" : "low";
2230
- return new xt(e.id, t);
2231
- }
2232
- onPinsChange(e, t, n, i) {
2233
- const s = this.getPinStates(e, n);
2234
- t.pinStates = s;
2235
- const o = this.vccGuardBehavior(t, s, i);
2236
- if (o) return o;
2237
- const r = this.nonLogicInputGuardBehavior(t, s, i);
2238
- if (r) return r;
2239
- const a = ((s.get("input1").hasVoltage ? 1 : 0) + (s.get("input2").hasVoltage ? 1 : 0) + (s.get("input3").hasVoltage ? 1 : 0) + (s.get("input4").hasVoltage ? 1 : 0) + (s.get("input5").hasVoltage ? 1 : 0) + (s.get("input6").hasVoltage ? 1 : 0) + (s.get("input7").hasVoltage ? 1 : 0) + (s.get("input8").hasVoltage ? 1 : 0)) % 2 === 1, c = e.config.get("activationLogic") === "negative" ? !a : a;
2240
- return this.getBehavior(e, t, c, i);
2241
- }
2242
- }, Yt = class {
2243
- heap;
2244
- constructor() {
2245
- this.heap = [];
2246
- }
2247
- schedule(e) {
2248
- if (e.readyAtTick < e.scheduledAtTick) throw new RangeError(`readyAtTick (${e.readyAtTick}) cannot be before scheduledAtTick (${e.scheduledAtTick})`);
2249
- this.heap.push(e), this.bubbleUp(this.heap.length - 1);
2250
- }
2251
- getReadyEvents(e) {
2252
- const t = [];
2253
- for (; this.heap.length > 0 && this.heap[0].readyAtTick <= e; ) {
2254
- const n = this.extractMin();
2255
- n && t.push(n);
2256
- }
2257
- return t.sort((n, i) => n.readyAtTick === i.readyAtTick ? n.scheduledAtTick - i.scheduledAtTick : n.readyAtTick - i.readyAtTick), t;
2258
- }
2259
- hasEvents() {
2260
- return this.heap.length > 0;
2261
- }
2262
- clear() {
2263
- this.heap = [];
2264
- }
2265
- scheduleMany(e, t) {
2266
- for (const n of e) if (n.readyAtTick < n.scheduledAtTick) throw new RangeError(`readyAtTick (${n.readyAtTick}) cannot be before scheduledAtTick (${n.scheduledAtTick})`);
2267
- t && t.size > 0 && (this.heap = this.heap.filter((n) => !t.has(n.targetId)));
2268
- for (const n of e) this.heap.push(n);
2269
- (e.length > 0 || t && t.size > 0) && this.rebuildHeap();
2270
- }
2271
- removeEventsForTarget(e) {
2272
- const t = this.heap.length;
2273
- return this.heap = this.heap.filter((n) => n.targetId !== e), this.heap.length !== t && this.rebuildHeap(), t - this.heap.length;
2274
- }
2275
- size() {
2276
- return this.heap.length;
2277
- }
2278
- rebuildHeap() {
2279
- for (let e = Math.floor(this.heap.length / 2) - 1; e >= 0; e--) this.bubbleDown(e);
2280
- }
2281
- bubbleUp(e) {
2282
- for (; e > 0; ) {
2283
- const t = Math.floor((e - 1) / 2);
2284
- if (this.heap[e].readyAtTick < this.heap[t].readyAtTick)
2285
- [this.heap[e], this.heap[t]] = [this.heap[t], this.heap[e]], e = t;
2286
- else break;
2287
- }
2288
- }
2289
- bubbleDown(e) {
2290
- const t = this.heap.length;
2291
- for (; ; ) {
2292
- const n = 2 * e + 1, i = 2 * e + 2;
2293
- let s = e;
2294
- if (n < t && this.heap[n].readyAtTick < this.heap[s].readyAtTick && (s = n), i < t && this.heap[i].readyAtTick < this.heap[s].readyAtTick && (s = i), s !== e)
2295
- [this.heap[e], this.heap[s]] = [this.heap[s], this.heap[e]], e = s;
2296
- else break;
2297
- }
2298
- }
2299
- extractMin() {
2300
- if (this.heap.length === 0) return;
2301
- const e = this.heap[0], t = this.heap.pop();
2302
- return this.heap.length > 0 && t && (this.heap[0] = t, this.bubbleDown(0)), e;
2303
- }
2304
- }, Zt = class {
2305
- dirtyComponents;
2306
- dirtyWires;
2307
- dirtyEnodes;
2308
- constructor() {
2309
- this.dirtyComponents = /* @__PURE__ */ new Set(), this.dirtyWires = /* @__PURE__ */ new Set(), this.dirtyEnodes = /* @__PURE__ */ new Set();
2310
- }
2311
- markComponentDirty(e) {
2312
- this.dirtyComponents.add(e);
2313
- }
2314
- markWireDirty(e) {
2315
- this.dirtyWires.add(e);
2316
- }
2317
- markEnodeDirty(e) {
2318
- this.dirtyEnodes.add(e);
2319
- }
2320
- setDirtyComponents(e) {
2321
- this.dirtyComponents = new Set(e);
2322
- }
2323
- setDirtyEnodes(e) {
2324
- this.dirtyEnodes = new Set(e);
2325
- }
2326
- setDirtyWires(e) {
2327
- this.dirtyWires = new Set(e);
2328
- }
2329
- getDirtyElements() {
2330
- const e = {
2331
- components: new Set(this.dirtyComponents),
2332
- wires: new Set(this.dirtyWires),
2333
- enodes: new Set(this.dirtyEnodes)
2334
- };
2335
- return this.clear(), e;
2336
- }
2337
- hasDirtyElements() {
2338
- return this.dirtyComponents.size > 0 || this.dirtyWires.size > 0 || this.dirtyEnodes.size > 0;
2339
- }
2340
- clear() {
2341
- this.dirtyComponents.clear(), this.dirtyWires.clear(), this.dirtyEnodes.clear();
2342
- }
2343
- getDirtyComponentCount() {
2344
- return this.dirtyComponents.size;
2345
- }
2346
- getDirtyWireCount() {
2347
- return this.dirtyWires.size;
2348
- }
2349
- getDirtyEnodeCount() {
2350
- return this.dirtyEnodes.size;
2351
- }
2352
- }, jt = class {
2353
- currentState;
2354
- history;
2355
- historyEnabled;
2356
- historyLimit;
2357
- historyWriteIndex;
2358
- constructor(e = !1, t = 1e3) {
2359
- if (t < 1) throw new RangeError(`historyLimit must be at least 1 (got ${t})`);
2360
- this.historyEnabled = e, this.historyLimit = t, this.currentState = new V(0), this.history = [], this.historyWriteIndex = 0;
2361
- }
2362
- getCurrentState() {
2363
- return this.currentState;
2364
- }
2365
- getCurrentTick() {
2366
- return this.currentState.tick;
2367
- }
2368
- advanceToNextTick() {
2369
- const e = this.currentState.tick + 1;
2370
- return this.historyEnabled && this.saveToHistory(this.currentState.clone()), this.currentState.tick = e, this.currentState;
2371
- }
2372
- getStateAtTick(e) {
2373
- if (this.historyEnabled)
2374
- return this.history.find((t) => t.tick === e);
2375
- }
2376
- getHistory() {
2377
- return this.historyEnabled ? [...this.history].sort((e, t) => e.tick - t.tick) : [];
2378
- }
2379
- getOldestTick() {
2380
- if (!(!this.historyEnabled || this.history.length === 0))
2381
- return Math.min(...this.history.map((e) => e.tick));
2382
- }
2383
- getNewestHistoricalTick() {
2384
- if (!(!this.historyEnabled || this.history.length === 0))
2385
- return Math.max(...this.history.map((e) => e.tick));
2386
- }
2387
- clearHistory() {
2388
- this.history = [], this.historyWriteIndex = 0;
2389
- }
2390
- reset() {
2391
- this.currentState = new V(0), this.clearHistory();
2392
- }
2393
- isHistoryEnabled() {
2394
- return this.historyEnabled;
2395
- }
2396
- getHistoryLimit() {
2397
- return this.historyLimit;
2398
- }
2399
- getHistorySize() {
2400
- return this.history.length;
2401
- }
2402
- saveToHistory(e) {
2403
- this.history.length < this.historyLimit ? this.history.push(e) : (this.history[this.historyWriteIndex] = e, this.historyWriteIndex = (this.historyWriteIndex + 1) % this.historyLimit);
2404
- }
2405
- }, oe = class {
2406
- circuit;
2407
- stateManager;
2408
- eventQueue;
2409
- commands;
2410
- dirtyTracker;
2411
- behaviorRegistry;
2412
- constructor(e, t, n = {}) {
2413
- this.circuit = e, this.behaviorRegistry = t, this.stateManager = new jt(n.enableHistory ?? !1, n.historyLimit ?? 1e3), this.eventQueue = new Yt(), this.commands = /* @__PURE__ */ new Map(), this.dirtyTracker = new Zt();
2414
- try {
2415
- this.initializeState();
2416
- } catch (i) {
2417
- throw console.error("Error during CircuitRunner initialization:", i), i;
2418
- }
2419
- }
2420
- tick() {
2421
- const e = this.eventQueue.size(), t = this.stateManager.getCurrentTick(), n = this.applyReadyEvents(t + 1), i = this.updateState(t + 1);
2422
- i.firedEventCount = n.length, i.processedCommandCount = this.processCommands().length, i.scheduledEventCount = this.eventQueue.size() + i.firedEventCount - e;
2423
- for (const s of n) s.hasChanged && this.dirtyTracker.markComponentDirty(s.componentState.componentId);
2424
- return i.componentUpdateCount = this.dirtyTracker.getDirtyComponentCount(), this.stateManager.advanceToNextTick(), i.endTick = this.stateManager.getCurrentTick(), i;
2425
- }
2426
- tickN(e) {
2427
- if (e < 1) throw new RangeError(`Tick count must be at least 1 (got ${e})`);
2428
- const t = [];
2429
- for (let n = 0; n < e; n++) t.push(this.tick());
2430
- return t;
2431
- }
2432
- reset() {
2433
- this.stateManager.reset(), this.eventQueue.clear(), this.dirtyTracker.clear(), this.initializeState();
2434
- }
2435
- getCurrentTick() {
2436
- return this.stateManager.getCurrentTick();
2437
- }
2438
- getCurrentState() {
2439
- return this.stateManager.getCurrentState();
2440
- }
2441
- getEnodeState(e) {
2442
- return this.stateManager.getCurrentState().nodeStates.get(e);
2443
- }
2444
- getWireState(e) {
2445
- return this.stateManager.getCurrentState().wireStates.get(e);
2446
- }
2447
- getComponentState(e) {
2448
- return this.stateManager.getCurrentState().componentStates.get(e);
2449
- }
2450
- getStateAtTick(e) {
2451
- return this.stateManager.getStateAtTick(e);
2452
- }
2453
- hasBehavior(e) {
2454
- return this.behaviorRegistry.has(e);
2455
- }
2456
- submitCommand(e) {
2457
- if (!this.circuit.hasComponent(e.targetId)) throw Error(`Cannot submit command for unknown component ID '${e.targetId}'`);
2458
- return this.commands.has(e.targetId) ? !1 : (e.scheduledAtTick = this.getCurrentTick(), this.commands.set(e.targetId, e), !0);
2459
- }
2460
- processCommands() {
2461
- const e = this.stateManager.getCurrentState(), t = [], n = /* @__PURE__ */ new Set(), i = [];
2462
- for (const s of this.commands.values()) {
2463
- const o = this.circuit.getComponent(s.targetId), r = this.behaviorRegistry.get(o.type).onUserCommand(o, e.componentStates.get(o.id), s);
2464
- r.shouldCancelPending && n.add(o.id);
2465
- for (const a of r.scheduledEvents) i.push(a);
2466
- t.push(r), r.hasChanged && this.dirtyTracker.markComponentDirty(o.id);
2467
- }
2468
- return this.eventQueue.scheduleMany(i, n), this.commands.clear(), t;
2469
- }
2470
- getInitializationOrder(e) {
2471
- const t = e.get("initializationOrder");
2472
- if (!t || t === "") return 0;
2473
- const n = parseInt(t, 10);
2474
- return isNaN(n) ? 0 : n;
2475
- }
2476
- initializeState() {
2477
- const e = this.stateManager.getCurrentState();
2478
- for (const c of this.circuit.getAllComponents()) {
2479
- if (c.pins.length < 1) continue;
2480
- const d = this.behaviorRegistry.get(c.type);
2481
- if (!d) continue;
2482
- const h = d.createInitialState(c);
2483
- e.componentStates.set(c.id, h), this.dirtyTracker.markComponentDirty(c.id);
2484
- }
2485
- for (const c of this.circuit.getAllENodes()) e.nodeStates.set(c.id, {
2486
- hasVoltage: c.source === g.Voltage,
2487
- hasCurrent: c.source === g.Current,
2488
- locked: !!c.source
2489
- });
2490
- for (const c of this.circuit.getAllWires()) e.wireStates.set(c.id, {
2491
- hasVoltage: !1,
2492
- hasCurrent: !1,
2493
- locked: !1
2494
- });
2495
- const t = this.circuit.getAllComponents(), n = /* @__PURE__ */ new Map();
2496
- for (const c of t) {
2497
- const d = this.getInitializationOrder(c.config), h = n.get(d) ?? [];
2498
- h.push(c), n.set(d, h);
2499
- }
2500
- const i = Array.from(n.keys()).sort((c, d) => c - d);
2501
- for (const c of i) n.get(c).sort((d, h) => d.id.localeCompare(h.id));
2502
- let s = !0, o = 0;
2503
- const r = 100;
2504
- for (; s && o < r; ) {
2505
- s = !1, o++;
2506
- for (const c of i) {
2507
- const d = n.get(c);
2508
- for (const h of d) {
2509
- const p = this.behaviorRegistry.get(h.type);
2510
- if (!p) continue;
2511
- const v = e.componentStates.get(h.id);
2512
- if (!v) continue;
2513
- this.propagateConductivity();
2514
- const f = p.onStart(h, v);
2515
- if (f) {
2516
- e.componentStates.set(h.id, f.componentState), f.shouldCancelPending && this.eventQueue.removeEventsForTarget(h.id);
2517
- for (const l of f.scheduledEvents) this.eventQueue.schedule(l);
2518
- continue;
2519
- }
2520
- const w = p.onPinsChange(h, v, e.nodeStates, 0);
2521
- w.hasChanged && (s = !0, e.componentStates.set(h.id, w.componentState));
2522
- for (const l of w.scheduledEvents) {
2523
- const y = p.onEventFiring(h, v, l);
2524
- y.hasChanged && (s = !0, e.componentStates.set(h.id, y.componentState));
2525
- }
2526
- }
2527
- }
2528
- }
2529
- const a = this.updateState(0);
2530
- return this.dirtyTracker.setDirtyComponents(/* @__PURE__ */ new Set([...this.circuit.getAllComponents().map((c) => c.id)])), this.dirtyTracker.setDirtyEnodes(/* @__PURE__ */ new Set([...this.circuit.getAllENodes().map((c) => c.id)])), this.dirtyTracker.setDirtyWires(/* @__PURE__ */ new Set([...this.circuit.getAllWires().map((c) => c.id)])), a;
2531
- }
2532
- updateState(e) {
2533
- const t = this.stateManager.getCurrentState(), { updatedNodes: n, updatedWires: i } = this.propagateConductivity(), s = this.circuit.getComponentsOfPins(n), o = [], r = /* @__PURE__ */ new Set();
2534
- let a = Array.from(s);
2535
- e === 0 && (a = a.sort((h, p) => {
2536
- const v = this.circuit.getComponent(h), f = this.circuit.getComponent(p), w = this.getInitializationOrder(v.config), l = this.getInitializationOrder(f.config);
2537
- return w !== l ? w - l : h.localeCompare(p);
2538
- }));
2539
- const c = /* @__PURE__ */ new Set(), d = [];
2540
- for (const h of a) {
2541
- const p = this.circuit.getComponent(h), v = this.behaviorRegistry.get(p.type);
2542
- if (!v) continue;
2543
- const f = v.onPinsChange(p, t.componentStates.get(h), t.nodeStates, e);
2544
- f.shouldCancelPending && c.add(h), f.hasChanged && (r.add(h), o.push(f));
2545
- for (const w of f.scheduledEvents) d.push(w);
2546
- }
2547
- return this.eventQueue.scheduleMany(d, c), this.dirtyTracker.setDirtyComponents(r), this.dirtyTracker.setDirtyEnodes(n), this.dirtyTracker.setDirtyWires(i), {
2548
- startTick: this.getCurrentTick(),
2549
- endTick: this.getCurrentTick(),
2550
- componentUpdateCount: r.size,
2551
- nodeUpdateCount: n.size,
2552
- wireUpdateCount: i.size,
2553
- processedCommandCount: 0,
2554
- scheduledEventCount: d.length,
2555
- firedEventCount: 0
2556
- };
2557
- }
2558
- propagateConductivity() {
2559
- const e = this.stateManager.getCurrentState(), t = (r) => {
2560
- const a = /* @__PURE__ */ new Set(), c = /* @__PURE__ */ new Set(), d = this.circuit.getAllENodes().filter((l) => l.source == r).map((l) => l.id), h = /* @__PURE__ */ new Set([...this.circuit.getAllENodes().filter((l) => !l.source).map((l) => l.id)]), p = /* @__PURE__ */ new Set([...this.circuit.getAllWires().map((l) => l.id)]), { nodes: v, wires: f } = this.computeReachability(r, d, e.componentStates), w = r == g.Voltage ? "hasVoltage" : "hasCurrent";
2561
- for (const l of v) {
2562
- const y = e.nodeStates.get(l);
2563
- y && !y.locked && (y[w] || (y[w] = !0, a.add(l)), h.delete(l));
2564
- }
2565
- for (const l of h) {
2566
- const y = e.nodeStates.get(l);
2567
- y && !y.locked && y[w] && (y[w] = !1, a.add(l));
2568
- }
2569
- for (const l of f) {
2570
- const y = e.wireStates.get(l);
2571
- y && (y[w] || (y[w] = !0, c.add(l)), p.delete(l));
2572
- }
2573
- for (const l of p) {
2574
- const y = e.wireStates.get(l);
2575
- y && y[w] && (y[w] = !1, c.add(l));
2576
- }
2577
- return {
2578
- updatedNodes: a,
2579
- updatedWires: c
2580
- };
2581
- }, { updatedNodes: n, updatedWires: i } = t(g.Voltage), { updatedNodes: s, updatedWires: o } = t(g.Current);
2582
- return {
2583
- updatedNodes: /* @__PURE__ */ new Set([...n, ...s]),
2584
- updatedWires: /* @__PURE__ */ new Set([...i, ...o])
2585
- };
2586
- }
2587
- computeReachability(e, t, n) {
2588
- const i = /* @__PURE__ */ new Set(), s = /* @__PURE__ */ new Set(), o = [];
2589
- for (const r of t)
2590
- o.push(r), i.add(r);
2591
- for (; o.length > 0; ) {
2592
- const r = o.shift();
2593
- for (const c of this.circuit.getWiresByNode(r)) {
2594
- const d = c.node1 === r ? c.node2 : c.node1;
2595
- i.has(d) || (i.add(d), o.push(d)), s.has(c.id) || s.add(c.id);
2596
- }
2597
- const a = this.circuit.getENode(r);
2598
- if (a.type === C.Pin) {
2599
- const c = this.circuit.getComponent(a.component), d = this.behaviorRegistry.get(c.type);
2600
- if (!d) continue;
2601
- const h = n.get(c.id);
2602
- for (const p of c.pins)
2603
- p !== r && (i.has(p) || d.allowConductivity(c, h, e, r, p) && (i.add(p), o.push(p)));
2604
- }
2605
- }
2606
- return {
2607
- nodes: i,
2608
- wires: s
2609
- };
2610
- }
2611
- applyReadyEvents(e) {
2612
- const t = this.stateManager.getCurrentState(), n = this.eventQueue.getReadyEvents(e), i = [], s = /* @__PURE__ */ new Set(), o = [];
2613
- for (const r of n) {
2614
- const a = this.circuit.getComponent(r.targetId), c = this.behaviorRegistry.get(a.type);
2615
- if (!c) continue;
2616
- const d = t.componentStates.get(a.id), h = c.onEventFiring(a, d, r);
2617
- h.shouldCancelPending && s.add(a.id);
2618
- for (const p of h.scheduledEvents) o.push(p);
2619
- i.push(h);
2620
- }
2621
- return this.eventQueue.scheduleMany(o, s), i;
2622
- }
2623
- };
2624
- function re(e) {
2625
- return e.register(new Mt()).register(new Gt()).register(new At()).register(new _t()).register(new Rt()).register(new Dt()).register(new Vt()).register(new Wt()), e;
2626
- }
2627
- function ae(e) {
2628
- return e.register(new $t()).register(new zt()).register(new Ft()).register(new Jt()).register(new Xt()).register(new Ut()).register(new Ht()).register(new Qt()).register(new qt()).register(new Kt()), e;
2629
- }
2630
- export {
2631
- dt as $,
2632
- Ot as A,
2633
- wt as B,
2634
- Gt as C,
2635
- xt as D,
2636
- se as E,
2637
- Tt as F,
2638
- b as G,
2639
- et as H,
2640
- Ct as I,
2641
- ne as J,
2642
- V as K,
2643
- bt as L,
2644
- Pt as M,
2645
- It as N,
2646
- Lt as O,
2647
- Nt as P,
2648
- gt as Q,
2649
- mt as R,
2650
- At as S,
2651
- O as T,
2652
- yt as U,
2653
- vt as V,
2654
- ft as W,
2655
- ie as X,
2656
- j as Y,
2657
- lt as Z,
2658
- Wt as _,
2659
- $ as _t,
2660
- Zt as a,
2661
- ee as at,
2662
- Rt as b,
2663
- te as bt,
2664
- qt as c,
2665
- u as ct,
2666
- Ut as d,
2667
- C as dt,
2668
- D as et,
2669
- Xt as f,
2670
- G as ft,
2671
- $t as g,
2672
- L as gt,
2673
- zt as h,
2674
- nt as ht,
2675
- jt as i,
2676
- x as it,
2677
- Et as j,
2678
- kt as k,
2679
- Qt as l,
2680
- U as lt,
2681
- Ft as m,
2682
- M as mt,
2683
- ae as n,
2684
- _ as nt,
2685
- Yt as o,
2686
- X as ot,
2687
- Jt as p,
2688
- rt as pt,
2689
- I as q,
2690
- oe as r,
2691
- E as rt,
2692
- Kt as s,
2693
- N as st,
2694
- re as t,
2695
- R as tt,
2696
- Ht as u,
2697
- g as ut,
2698
- Vt as v,
2699
- P as vt,
2700
- Mt as w,
2701
- _t as x,
2702
- Dt as y,
2703
- it as yt,
2704
- St as z
2705
- };
2706
-
2707
- //# sourceMappingURL=core-Bjta9Y7_.js.map