Spaces:
Running
Running
Update main.js
Browse files
main.js
CHANGED
|
@@ -1,147 +1,59 @@
|
|
| 1 |
-
import {
|
| 2 |
|
| 3 |
-
|
| 4 |
-
const gl = canvas.getContext("webgl");
|
| 5 |
const log = document.getElementById("log");
|
| 6 |
const input = document.getElementById("input");
|
| 7 |
|
| 8 |
-
function print(
|
| 9 |
-
|
| 10 |
-
|
| 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 |
-
// -----
|
| 27 |
-
|
| 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 |
-
|
| 35 |
-
|
| 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;
|
| 59 |
-
agents.length=0; d.agents.forEach(a=>agents.push(Object.assign(new Agent(a.id),a)));
|
| 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);}`;
|
| 67 |
-
function compile(t,s){const sh=gl.createShader(t);gl.shaderSource(sh,s);gl.compileShader(sh);return sh;}
|
| 68 |
-
const prog=gl.createProgram();
|
| 69 |
-
gl.attachShader(prog,compile(gl.VERTEX_SHADER,vert));
|
| 70 |
-
gl.attachShader(prog,compile(gl.FRAGMENT_SHADER,frag));
|
| 71 |
-
gl.linkProgram(prog); gl.useProgram(prog);
|
| 72 |
-
|
| 73 |
-
const buf=gl.createBuffer();
|
| 74 |
-
gl.bindBuffer(gl.ARRAY_BUFFER,buf);
|
| 75 |
-
gl.bufferData(gl.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),gl.STATIC_DRAW);
|
| 76 |
-
const loc=gl.getAttribLocation(prog,"p");
|
| 77 |
-
gl.enableVertexAttribArray(loc);
|
| 78 |
-
gl.vertexAttribPointer(loc,2,gl.FLOAT,false,0,0);
|
| 79 |
-
|
| 80 |
-
const uRes=gl.getUniformLocation(prog,"u_res");
|
| 81 |
-
const uPow=gl.getUniformLocation(prog,"u_power");
|
| 82 |
-
const uEng=gl.getUniformLocation(prog,"u_energy");
|
| 83 |
-
const uCam=gl.getUniformLocation(prog,"u_camPos");
|
| 84 |
-
const uRot=gl.getUniformLocation(prog,"u_camRot");
|
| 85 |
-
|
| 86 |
-
function loop(){
|
| 87 |
-
if(keys["w"]) camPos[2]+=0.05;
|
| 88 |
-
if(keys["s"]) camPos[2]-=0.05;
|
| 89 |
-
if(keys["a"]) camPos[0]-=0.05;
|
| 90 |
-
if(keys["d"]) camPos[0]+=0.05;
|
| 91 |
-
|
| 92 |
-
agents.forEach(a=>a.step());
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
gl.uniform1f(uEng,energy);
|
| 97 |
-
gl.uniform3fv(uCam,camPos);
|
| 98 |
-
gl.uniformMatrix3fv(uRot,false,new Float32Array(camRot()));
|
| 99 |
-
gl.drawArrays(gl.TRIANGLE_STRIP,0,4);
|
| 100 |
-
requestAnimationFrame(loop);
|
| 101 |
}
|
| 102 |
-
loop();
|
| 103 |
-
})();
|
| 104 |
|
| 105 |
-
|
| 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 |
-
// -----
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
|
|
|
| 132 |
}
|
| 133 |
-
// Otherwise, ask cognition bridge
|
| 134 |
-
const plan = await reason(inputText);
|
| 135 |
-
execPlan(plan);
|
| 136 |
}
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
|
|
|
|
|
|
| 143 |
}
|
| 144 |
});
|
| 145 |
|
| 146 |
-
|
| 147 |
-
print("
|
|
|
|
|
|
|
|
|
| 1 |
+
import { loadURL } from "./web.js";
|
| 2 |
|
| 3 |
+
// ----- Console -----
|
|
|
|
| 4 |
const log = document.getElementById("log");
|
| 5 |
const input = document.getElementById("input");
|
| 6 |
|
| 7 |
+
function print(msg) {
|
| 8 |
+
log.innerHTML += msg + "<br/>";
|
| 9 |
+
log.scrollTop = log.scrollHeight;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
}
|
| 11 |
|
| 12 |
+
// ----- Intent Parsing (v8) -----
|
| 13 |
+
function parseIntent(text) {
|
| 14 |
+
const t = text.toLowerCase();
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
if (t.includes("open") || t.includes("browse")) {
|
| 17 |
+
return { type: "web", url: text.split(" ").pop() };
|
|
|
|
|
|
|
|
|
|
| 18 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
if (t.includes("brighter")) {
|
| 21 |
+
return { type: "inject", value: 2 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
}
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
return { type: "command", raw: text };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
}
|
| 26 |
|
| 27 |
+
// ----- Command Router -----
|
| 28 |
+
function route(intent) {
|
| 29 |
+
switch (intent.type) {
|
| 30 |
+
case "web":
|
| 31 |
+
print("Opening web layer → " + intent.url);
|
| 32 |
+
loadURL(intent.url);
|
| 33 |
+
break;
|
| 34 |
+
|
| 35 |
+
case "inject":
|
| 36 |
+
Psi = intent.value;
|
| 37 |
+
print("Ψ injected → " + Psi);
|
| 38 |
+
break;
|
| 39 |
+
|
| 40 |
+
case "command":
|
| 41 |
+
handle(intent.raw);
|
| 42 |
+
break;
|
| 43 |
}
|
|
|
|
|
|
|
|
|
|
| 44 |
}
|
| 45 |
|
| 46 |
+
// ----- Input Handling -----
|
| 47 |
+
input.addEventListener("keydown", e => {
|
| 48 |
+
if (e.key === "Enter") {
|
| 49 |
+
const text = input.value.trim();
|
| 50 |
+
print("> " + text);
|
| 51 |
+
route(parseIntent(text));
|
| 52 |
+
input.value = "";
|
| 53 |
}
|
| 54 |
});
|
| 55 |
|
| 56 |
+
// Boot
|
| 57 |
+
print("CodexReality3D v8 ONLINE");
|
| 58 |
+
print("World + Web Unified");
|
| 59 |
+
print("Try: open wikipedia.org");
|