LordXido commited on
Commit
1f0a6e9
·
verified ·
1 Parent(s): 88f2712

Update main.js

Browse files
Files changed (1) hide show
  1. main.js +50 -34
main.js CHANGED
@@ -1,54 +1,70 @@
1
  import { initGPU } from "./gpu.js";
2
  import { World } from "./world.js";
 
3
  import { Agents } from "./agents.js";
4
- import { parseCommand } from "./llm.js";
5
 
6
- const canvas = document.getElementById("world");
7
- const consoleEl = document.getElementById("console");
 
8
 
9
- function log(m) {
10
- consoleEl.textContent += "\n" + m;
11
- consoleEl.scrollTop = consoleEl.scrollHeight;
12
  }
13
 
14
- let world, agents;
15
- let running = false;
16
 
17
- window.codexCommand = (input) => {
18
- log("> " + input);
19
- const cmd = parseCommand(input);
20
- if (cmd) world.apply(cmd);
21
- };
22
-
23
- async function boot() {
24
- log("Booting runtime...");
25
- const gpu = await initGPU();
26
- log("Compute mode: " + gpu.mode.toUpperCase());
27
-
28
- world = new World(canvas, gpu);
29
- agents = new Agents(world);
30
-
31
- waitForWorld();
32
- }
33
-
34
- function waitForWorld() {
35
- if (!world.ready) return requestAnimationFrame(waitForWorld);
36
- log("World initialized");
37
- running = true;
38
- requestAnimationFrame(loop);
39
- }
40
 
41
  let last = performance.now();
 
42
  function loop(t) {
43
- if (!running) return;
44
  const dt = (t - last) / 1000;
45
  last = t;
46
 
47
- world.update(dt);
48
  agents.update(dt);
 
49
  world.render();
50
 
51
  requestAnimationFrame(loop);
52
  }
53
 
54
- boot();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import { initGPU } from "./gpu.js";
2
  import { World } from "./world.js";
3
+ import { record, replay } from "./plme.js";
4
  import { Agents } from "./agents.js";
 
5
 
6
+ const canvas = document.getElementById("viewport");
7
+ const log = document.getElementById("log");
8
+ const input = document.getElementById("input");
9
 
10
+ function println(msg) {
11
+ log.textContent += "\n" + msg;
12
+ log.scrollTop = log.scrollHeight;
13
  }
14
 
15
+ const gpu = await initGPU();
16
+ println("GPU mode: " + gpu.mode);
17
 
18
+ const world = new World(canvas, gpu);
19
+ const agents = new Agents(world);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  let last = performance.now();
22
+
23
  function loop(t) {
 
24
  const dt = (t - last) / 1000;
25
  last = t;
26
 
 
27
  agents.update(dt);
28
+ world.update(dt);
29
  world.render();
30
 
31
  requestAnimationFrame(loop);
32
  }
33
 
34
+ requestAnimationFrame(loop);
35
+
36
+ input.addEventListener("keydown", e => {
37
+ if (e.key === "Enter") {
38
+ const text = input.value.trim();
39
+ input.value = "";
40
+
41
+ const cmd = parseCommand(text);
42
+ record(cmd);
43
+ execute(cmd);
44
+ }
45
+ });
46
+
47
+ function execute(cmd) {
48
+ println("> " + cmd.raw);
49
+
50
+ if (cmd.lane === "world") world.apply(cmd);
51
+ if (cmd.lane === "agent") agents.apply(cmd);
52
+ if (cmd.lane === "system" && cmd.action === "replay") {
53
+ println("Replaying PLME stream...");
54
+ replay(execute);
55
+ }
56
+ }
57
+
58
+ function parseCommand(text) {
59
+ const timestamp = performance.now();
60
+ if (text.startsWith("power")) {
61
+ return { lane: "world", payload: { power: parseFloat(text.split(" ")[1]) }, raw: text, timestamp };
62
+ }
63
+ if (text === "spawn") {
64
+ return { lane: "agent", action: "spawn", raw: text, timestamp };
65
+ }
66
+ if (text === "replay") {
67
+ return { lane: "system", action: "replay", raw: text, timestamp };
68
+ }
69
+ return { lane: "text", raw: text, timestamp };
70
+ }