Spaces:
Running
Running
Update main.js
Browse files
main.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
-
|
| 2 |
-
// CodexReality3D v7 — Living World
|
| 3 |
-
// ================================
|
| 4 |
|
| 5 |
const canvas = document.getElementById("gl");
|
| 6 |
const gl = canvas.getContext("webgl");
|
|
@@ -9,71 +7,52 @@ const input = document.getElementById("input");
|
|
| 9 |
|
| 10 |
function print(m){ log.innerHTML += m+"<br/>"; log.scrollTop=log.scrollHeight; }
|
| 11 |
|
| 12 |
-
canvas.width = window.innerWidth -
|
| 13 |
canvas.height = window.innerHeight;
|
| 14 |
|
| 15 |
// ---------- Camera ----------
|
| 16 |
-
let camPos
|
| 17 |
-
|
| 18 |
-
const keys = {};
|
| 19 |
window.addEventListener("keydown",e=>keys[e.key]=true);
|
| 20 |
window.addEventListener("keyup",e=>keys[e.key]=false);
|
| 21 |
canvas.addEventListener("mousemove",e=>{
|
| 22 |
-
if(e.buttons===1){
|
| 23 |
-
yaw += e.movementX*0.002;
|
| 24 |
-
pitch += e.movementY*0.002;
|
| 25 |
-
}
|
| 26 |
});
|
| 27 |
function camRot(){
|
| 28 |
-
const cy=Math.cos(yaw), sy=Math.sin(yaw);
|
| 29 |
-
|
| 30 |
-
return [
|
| 31 |
-
cy,0,-sy,
|
| 32 |
-
sy*sp,cp,cy*sp,
|
| 33 |
-
sy*cp,-sp,cy*cp
|
| 34 |
-
];
|
| 35 |
}
|
| 36 |
|
| 37 |
-
// ----------
|
| 38 |
async function loadShader(){ return await (await fetch("shader.glsl")).text(); }
|
| 39 |
|
| 40 |
-
let power=8.0, energy=1.0;
|
| 41 |
-
|
| 42 |
// ---------- World Systems ----------
|
| 43 |
-
|
| 44 |
-
const
|
| 45 |
-
const ledger
|
| 46 |
|
|
|
|
| 47 |
class Agent {
|
| 48 |
constructor(id){
|
| 49 |
-
this.id=id;
|
| 50 |
-
this.
|
| 51 |
-
this.vel=[0,0,0];
|
| 52 |
-
this.energy=1.0;
|
| 53 |
}
|
| 54 |
step(){
|
| 55 |
-
|
| 56 |
-
this.vel[
|
| 57 |
-
this.
|
| 58 |
-
this.
|
| 59 |
-
this.
|
| 60 |
-
this.energy -= 0.001;
|
| 61 |
-
if(this.energy<0.5 && economy.gold>1){
|
| 62 |
-
economy.gold-=1;
|
| 63 |
-
this.energy+=0.5;
|
| 64 |
-
}
|
| 65 |
}
|
| 66 |
}
|
| 67 |
|
| 68 |
// ---------- Persistence ----------
|
| 69 |
function saveWorld(){
|
| 70 |
-
localStorage.setItem("
|
| 71 |
-
power, energy, camPos, economy, agents
|
| 72 |
-
}));
|
| 73 |
print("Ω world saved");
|
| 74 |
}
|
| 75 |
function loadWorld(){
|
| 76 |
-
const d=JSON.parse(localStorage.getItem("
|
| 77 |
if(!d) return;
|
| 78 |
power=d.power; energy=d.energy; camPos=d.camPos;
|
| 79 |
economy.gold=d.economy.gold; economy.price=d.economy.price;
|
|
@@ -81,7 +60,7 @@ function loadWorld(){
|
|
| 81 |
print("Ω world restored");
|
| 82 |
}
|
| 83 |
|
| 84 |
-
// ----------
|
| 85 |
(async()=>{
|
| 86 |
const frag=await loadShader();
|
| 87 |
const vert=`attribute vec2 p;void main(){gl_Position=vec4(p,0,1);}`;
|
|
@@ -123,39 +102,46 @@ function loadWorld(){
|
|
| 123 |
loop();
|
| 124 |
})();
|
| 125 |
|
| 126 |
-
// ----------
|
| 127 |
-
function
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
print("
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
print("
|
| 147 |
-
|
| 148 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
}
|
|
|
|
|
|
|
|
|
|
| 150 |
}
|
| 151 |
|
| 152 |
-
input.addEventListener("keydown",e=>{
|
| 153 |
if(e.key==="Enter"){
|
| 154 |
print("> "+input.value);
|
| 155 |
-
handle(input.value
|
| 156 |
input.value="";
|
| 157 |
}
|
| 158 |
});
|
| 159 |
|
| 160 |
-
print("CodexReality3D
|
| 161 |
-
print("Type
|
|
|
|
| 1 |
+
import { reason } from "./llm_bridge.js";
|
|
|
|
|
|
|
| 2 |
|
| 3 |
const canvas = document.getElementById("gl");
|
| 4 |
const gl = canvas.getContext("webgl");
|
|
|
|
| 7 |
|
| 8 |
function print(m){ log.innerHTML += m+"<br/>"; log.scrollTop=log.scrollHeight; }
|
| 9 |
|
| 10 |
+
canvas.width = window.innerWidth - 460;
|
| 11 |
canvas.height = window.innerHeight;
|
| 12 |
|
| 13 |
// ---------- Camera ----------
|
| 14 |
+
let camPos=[0,0,-4], yaw=0, pitch=0;
|
| 15 |
+
const keys={};
|
|
|
|
| 16 |
window.addEventListener("keydown",e=>keys[e.key]=true);
|
| 17 |
window.addEventListener("keyup",e=>keys[e.key]=false);
|
| 18 |
canvas.addEventListener("mousemove",e=>{
|
| 19 |
+
if(e.buttons===1){ yaw+=e.movementX*0.002; pitch+=e.movementY*0.002; }
|
|
|
|
|
|
|
|
|
|
| 20 |
});
|
| 21 |
function camRot(){
|
| 22 |
+
const cy=Math.cos(yaw), sy=Math.sin(yaw), cp=Math.cos(pitch), sp=Math.sin(pitch);
|
| 23 |
+
return [ cy,0,-sy, sy*sp,cp,cy*sp, sy*cp,-sp,cy*cp ];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
+
// ---------- Load Shader ----------
|
| 27 |
async function loadShader(){ return await (await fetch("shader.glsl")).text(); }
|
| 28 |
|
|
|
|
|
|
|
| 29 |
// ---------- World Systems ----------
|
| 30 |
+
let power=8.0, energy=1.0;
|
| 31 |
+
const agents=[], economy={ gold:1000, price:1.0 };
|
| 32 |
+
const ledger=[];
|
| 33 |
|
| 34 |
+
// ---------- Agent ----------
|
| 35 |
class Agent {
|
| 36 |
constructor(id){
|
| 37 |
+
this.id=id; this.pos=[Math.random()*2-1,0,Math.random()*2-1];
|
| 38 |
+
this.vel=[0,0,0]; this.energy=1.0;
|
|
|
|
|
|
|
| 39 |
}
|
| 40 |
step(){
|
| 41 |
+
this.vel[0]+=(Math.random()-0.5)*0.01;
|
| 42 |
+
this.vel[2]+=(Math.random()-0.5)*0.01;
|
| 43 |
+
this.pos[0]+=this.vel[0]; this.pos[2]+=this.vel[2];
|
| 44 |
+
this.energy-=0.001;
|
| 45 |
+
if(this.energy<0.5 && economy.gold>1){ economy.gold-=1; this.energy+=0.5; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
}
|
| 47 |
}
|
| 48 |
|
| 49 |
// ---------- Persistence ----------
|
| 50 |
function saveWorld(){
|
| 51 |
+
localStorage.setItem("codex_v8", JSON.stringify({ power, energy, camPos, economy, agents }));
|
|
|
|
|
|
|
| 52 |
print("Ω world saved");
|
| 53 |
}
|
| 54 |
function loadWorld(){
|
| 55 |
+
const d=JSON.parse(localStorage.getItem("codex_v8"));
|
| 56 |
if(!d) return;
|
| 57 |
power=d.power; energy=d.energy; camPos=d.camPos;
|
| 58 |
economy.gold=d.economy.gold; economy.price=d.economy.price;
|
|
|
|
| 60 |
print("Ω world restored");
|
| 61 |
}
|
| 62 |
|
| 63 |
+
// ---------- GPU Init ----------
|
| 64 |
(async()=>{
|
| 65 |
const frag=await loadShader();
|
| 66 |
const vert=`attribute vec2 p;void main(){gl_Position=vec4(p,0,1);}`;
|
|
|
|
| 102 |
loop();
|
| 103 |
})();
|
| 104 |
|
| 105 |
+
// ---------- CCL Executor ----------
|
| 106 |
+
function execPlan(plan){
|
| 107 |
+
plan.forEach(step=>{
|
| 108 |
+
switch(step.op){
|
| 109 |
+
case "inject": energy = step.value || energy; print("Ψ="+energy); break;
|
| 110 |
+
case "superposition": power = step.value || power; print("σ="+power); break;
|
| 111 |
+
case "spawn_agent": agents.push(new Agent(agents.length)); print("Agent spawned"); break;
|
| 112 |
+
case "help": print("Type: inject, superposition, spawn_agent, save, load"); break;
|
| 113 |
+
}
|
| 114 |
+
});
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
// ---------- CLI + NL ----------
|
| 118 |
+
async function handle(inputText){
|
| 119 |
+
ledger.push(inputText);
|
| 120 |
+
// If looks like CCL, execute directly
|
| 121 |
+
const t=inputText.trim().split(" ");
|
| 122 |
+
if(["inject","superposition","spawn_agent","save","load","economy"].includes(t[0])){
|
| 123 |
+
switch(t[0]){
|
| 124 |
+
case "inject": energy=parseFloat(t[1])||energy; print("Ψ="+energy); break;
|
| 125 |
+
case "superposition": power=parseFloat(t[1])||power; print("σ="+power); break;
|
| 126 |
+
case "spawn_agent": agents.push(new Agent(agents.length)); print("Agent spawned"); break;
|
| 127 |
+
case "economy": print("Gold="+economy.gold); break;
|
| 128 |
+
case "save": saveWorld(); break;
|
| 129 |
+
case "load": loadWorld(); break;
|
| 130 |
+
}
|
| 131 |
+
return;
|
| 132 |
}
|
| 133 |
+
// Otherwise, ask cognition bridge
|
| 134 |
+
const plan = await reason(inputText);
|
| 135 |
+
execPlan(plan);
|
| 136 |
}
|
| 137 |
|
| 138 |
+
input.addEventListener("keydown", async e=>{
|
| 139 |
if(e.key==="Enter"){
|
| 140 |
print("> "+input.value);
|
| 141 |
+
await handle(input.value);
|
| 142 |
input.value="";
|
| 143 |
}
|
| 144 |
});
|
| 145 |
|
| 146 |
+
print("CodexReality3D v8 — Cognitive World Engine");
|
| 147 |
+
print("Type commands or natural language (e.g. 'make it brighter')");
|