const { describe, it } = require('node:test'); const assert = require('node:assert/strict'); const fs = require('fs'); const path = require('path'); const FloorCast = require('../src/floorCast.js'); function makeChecker (tw, th, cell) { const rgba = new Uint8ClampedArray(tw * th * 4); const colors = [ [255, 0, 0], [0, 255, 0], [0, 0, 255], [255, 255, 0], [255, 0, 255], [0, 255, 255], [255, 128, 0], [128, 0, 255] ]; for (let y = 0; y < th; y++) { for (let x = 0; x < tw; x++) { const ci = ((x / cell) | 0) + ((y / cell) | 0) * 2; const c = colors[ci % colors.length]; const o = (y * tw + x) * 4; rgba[o] = c[0]; rgba[o + 1] = c[1]; rgba[o + 2] = c[2]; rgba[o + 3] = 255; } } return { rgba, w: tw, h: th }; } function openLevel (texId) { const w = 8, h = 8, n = w * h; const floors = []; const ceilings = []; for (let i = 0; i < n; i++) { floors.push(texId); ceilings.push(texId); } return { width: w, height: h, floors, ceilings }; } describe('FloorCast math', () => { it('floorHit below horizon returns positive path and isCeiling false', () => { const hit = FloorCast.floorHit(160, 300, 320, 240, 48, 48, 0, 0, null, 32); assert.ok(hit); assert.equal(hit.isCeiling, false); assert.ok(hit.rowDist > 0); assert.ok(hit.wx > 48); }); it('floorHit above horizon is ceiling', () => { const hit = FloorCast.floorHit(160, 40, 320, 240, 48, 48, 0, 0, null, 32); assert.ok(hit); assert.equal(hit.isCeiling, true); }); it('posZ matches wall projection: bottom row ≈ one cell away', () => { const screenH = 240, cell = 32; const hit = FloorCast.floorHit(160, screenH - 1, 320, screenH, 48, 48, 0, 0, null, cell); // p ≈ screenH/2 → rowDist ≈ cellSize assert.ok(hit.rowDist > cell * 0.7 && hit.rowDist < cell * 1.4, 'rowDist=' + hit.rowDist); }); it('perspective: farther rows (nearer horizon) have larger rowDist', () => { const near = FloorCast.floorHit(160, 220, 320, 240, 48, 48, 0, 0, null, 32); const far = FloorCast.floorHit(160, 130, 320, 240, 48, 48, 0, 0, null, 32); assert.ok(far.rowDist > near.rowDist * 1.5); }); it('sampleNearest wraps and picks texel', () => { const rgba = new Uint8ClampedArray([ 10, 0, 0, 255, 20, 0, 0, 255, 30, 0, 0, 255, 40, 0, 0, 255 ]); assert.deepEqual(FloorCast.sampleNearest(rgba, 2, 2, 0.1, 0.1), [10, 0, 0, 255]); assert.deepEqual(FloorCast.sampleNearest(rgba, 2, 2, 0.6, 0.1), [20, 0, 0, 255]); assert.deepEqual(FloorCast.sampleNearest(rgba, 2, 2, 1.1, 0.1), [10, 0, 0, 255]); assert.deepEqual(FloorCast.sampleNearest(rgba, 2, 2, -0.1, 0.6), [40, 0, 0, 255]); }); it('render of checkerboard floor yields many unique colours (not flat)', () => { const w = 160, h = 120; const img = { data: new Uint8ClampedArray(w * h * 4) }; const tex = makeChecker(64, 64, 8); const level = openLevel('check'); const player = { x: 48, y: 48, rotation: 0.2, pitch: 0.35 }; FloorCast.render(img, w, h, player, level, { check: tex }, 32); const floorUnique = FloorCast.countUniqueRgb(img, w, h, 40, Math.floor(h * 0.55), 120, h); assert.ok(floorUnique >= 6, 'floor unique colours=' + floorUnique + ' (need textured sampling)'); const ceilUnique = FloorCast.countUniqueRgb(img, w, h, 40, 0, 120, Math.floor(h * 0.35)); assert.ok(ceilUnique >= 4, 'ceiling unique colours=' + ceilUnique); }); it('render with real stone.png yields many source texel colours on the floor', () => { const meta = JSON.parse(fs.readFileSync(path.join(__dirname, 'fixtures/stone.meta.json'), 'utf8')); const buf = fs.readFileSync(path.join(__dirname, 'fixtures/stone.rgba')); const stone = { rgba: new Uint8ClampedArray(buf.buffer, buf.byteOffset, buf.byteLength), w: meta.w, h: meta.h }; assert.ok(stone.w >= 64 && stone.h >= 64); assert.equal(stone.rgba.length, stone.w * stone.h * 4); const w = 320, h = 240; const img = { data: new Uint8ClampedArray(w * h * 4) }; const level = openLevel('stone'); const player = { x: 48, y: 48, rotation: 0.15, pitch: 0.4 }; FloorCast.render(img, w, h, player, level, { stone: stone }, 32); const floorUnique = FloorCast.countUniqueRgb(img, w, h, 80, Math.floor(h * 0.55), 240, h - 4); // Stone has ~80 unique colours; with perspective + shade we still need clearly > flat gray. assert.ok(floorUnique >= 12, 'stone floor unique=' + floorUnique + ' (was ~2 when posZ was wrong)'); }); });