Try to fix AI playerss getting bankrupt all the time

Fix z-fighting
This commit is contained in:
tyuXX 2026-11-15 16:09:08 +03:00
parent 2e3cea5a30
commit 5878827d6c
14 changed files with 511 additions and 105 deletions

127
analyze_zfight.mjs Normal file
View file

@ -0,0 +1,127 @@
// minimal DOM stubs for headless run
globalThis.document = {
createElement: () => ({
width: 0, height: 0,
getContext: () => ({
createRadialGradient: () => ({ addColorStop: () => {} }),
fillRect: () => {},
}),
}),
};
globalThis.window = globalThis;
globalThis.self = globalThis;
globalThis.ProgressEvent = class ProgressEvent { constructor(type, init){ this.type=type; Object.assign(this, init);} };
const RealRequest = globalThis.Request;
globalThis.Request = class extends RealRequest {
constructor(url, ...a) {
if (typeof url === "string" && url.startsWith("/")) url = "http://localhost" + url;
super(url, ...a);
}
};
import fs from "node:fs";
import path from "node:path";
globalThis.fetch = async (url) => {
let u = typeof url === "string" ? url : url.url;
let p = u.split("?")[0];
try { p = decodeURIComponent(new URL(p).pathname); } catch {}
if (p.startsWith("/models/")) p = "models/" + p.slice(8);
else if (p.startsWith("/vendor/")) p = "public/vendor/" + p.slice(8);
else if (p.startsWith("/data/")) p = "data/" + p.slice(6);
else return new Response("not found", { status: 404 });
try {
const buf = fs.readFileSync(path.resolve(p));
return new Response(buf, { status: 200 });
} catch { return new Response("nf", { status: 404 }); }
};
import * as THREE from "three";
import { buildRocket } from "./dist/shared/physics/buildRocket.js";
import { createStarterDesign } from "./dist/shared/physics/starters.js";
import { buildRocketMesh } from "./dist/client/three/rocketMesh/assembly.js";
const parts = JSON.parse(fs.readFileSync("data/parts.json", "utf8"));
const materials = JSON.parse(fs.readFileSync("data/materials.json", "utf8"));
const payloadId = process.env.PAYLOAD || "payload-cubesat-1u";
const design = createStarterDesign(null, "sandbox");
design.payloadPartId = payloadId;
design.stages.push({
engineId: "eng-merlin-vac", engineCount: 1, nozzleRatio: 1.4, gimbalDeg: 3,
tankId: "tank-med-kero", diameter: 3.7, length: 14, fillRatio: 0.9,
structureId: "struct-frame-std", materialId: "al-7075",
wallThicknessFactor: 1.0, hasFins: false, hasHeatshield: false,
});
design.stages[0].interstageId = "interstage-std";
const asm = buildRocket(design, parts, materials, {});
console.log("height", asm.height.toFixed(2), "stages", asm.stages.length, "payload", payloadId);
const handle = await buildRocketMesh(asm, { realModels: true, fairingCutaway: false });
handle.group.updateMatrixWorld(true);
const faces = [];
let meshId = 0;
const meshNames = new Map();
handle.group.traverse((obj) => {
if (!obj.isMesh) return;
const id = meshId++;
const chain = [];
let n = obj;
while (n && chain.length < 5) { chain.push(n.name || n.type); n = n.parent; }
meshNames.set(id, chain.reverse().join("<"));
const geo = obj.geometry;
const pos = geo.attributes.position;
if (!pos) return;
const index = geo.index;
const triCount = index ? index.count / 3 : pos.count / 3;
const p = [new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3()];
for (let t = 0; t < triCount; t++) {
const i0 = index ? index.getX(t * 3) : t * 3;
const i1 = index ? index.getX(t * 3 + 1) : t * 3 + 1;
const i2 = index ? index.getX(t * 3 + 2) : t * 3 + 2;
p[0].fromBufferAttribute(pos, i0).applyMatrix4(obj.matrixWorld);
p[1].fromBufferAttribute(pos, i1).applyMatrix4(obj.matrixWorld);
p[2].fromBufferAttribute(pos, i2).applyMatrix4(obj.matrixWorld);
const e1 = p[1].clone().sub(p[0]);
const e2 = p[2].clone().sub(p[0]);
const nrm = e1.cross(e2);
const area = nrm.length();
if (area < 1e-8) continue;
nrm.normalize();
if (Math.abs(nrm.y) < 0.999) continue;
const y = (p[0].y + p[1].y + p[2].y) / 3;
const cx = (p[0].x + p[1].x + p[2].x) / 3;
const cz = (p[0].z + p[1].z + p[2].z) / 3;
const rr = Math.max(
Math.hypot(p[0].x - cx, p[0].z - cz),
Math.hypot(p[1].x - cx, p[1].z - cz),
Math.hypot(p[2].x - cx, p[2].z - cz),
);
faces.push({ y, cx, cz, r: rr, ny: Math.sign(nrm.y), id, area });
}
});
console.log("meshes:", meshId, "horizontal faces:", faces.length);
faces.sort((a, b) => a.y - b.y);
const EPS = 0.03;
const groups = [];
for (const f of faces) {
const g = groups[groups.length - 1];
if (g && Math.abs(f.y - g.y) < EPS) g.items.push(f);
else groups.push({ y: f.y, items: [f] });
}
const seen = new Set();
for (const g of groups) {
const items = g.items;
for (let i = 0; i < items.length; i++) {
for (let j = i + 1; j < items.length; j++) {
const a = items[i], b = items[j];
if (a.id === b.id) continue;
const d = Math.hypot(a.cx - b.cx, a.cz - b.cz);
if (d < a.r + b.r - 0.05) {
const key = `${g.y.toFixed(2)}|${Math.min(a.id,b.id)}|${Math.max(a.id,b.id)}`;
if (seen.has(key)) continue;
seen.add(key);
console.log(`ZF y=${g.y.toFixed(3)}\n A m${a.id} ${meshNames.get(a.id)} ny=${a.ny} r=${a.r.toFixed(2)} @(${a.cx.toFixed(1)},${a.cz.toFixed(1)})\n B m${b.id} ${meshNames.get(b.id)} ny=${b.ny} r=${b.r.toFixed(2)} @(${b.cx.toFixed(1)},${b.cz.toFixed(1)})`);
}
}
}
}