Fixed palettes

This commit is contained in:
MaddoScientisto 2026-04-18 14:38:40 +02:00
commit 93bc6e7a07
6 changed files with 328 additions and 100 deletions

View file

@ -286,14 +286,34 @@ function isStructuredCandidate(words) {
return true;
}
function projectCtorPlacement(xWord, yWord, zWord) {
// Engine-accurate projection, matching `psx_project_object_main_visible`
// (0x80040d44) followed by the camera subtraction at 0x80040e3c/0x80040e5c.
//
// The projection step writes to obj+0x78/0x7c:
// proj_x = Y - X
// proj_y = 2*Z - (X+Y)/2
// The draw step then writes to obj+0x20/0x22:
// screen_x = (cam_X - proj_x) - origin_X
// screen_y = (cam_Y - proj_y) - origin_Y
// i.e. the engine renders the world with the sign flipped relative to the
// raw projection. For a camera-less full-map export we fold that flip into
// the projection so higher world Z appears higher on the canvas (Y-down).
const projX = yWord - xWord;
const projY = (2 * zWord) - Math.floor((xWord + yWord) / 2);
return {
screenX: -projX * PSX_SCREEN_SCALE,
screenY: -projY * PSX_SCREEN_SCALE,
};
}
function buildRecord(words, source, offset, rawWords = words) {
if (!isPlausibleRecord(words)) {
return null;
}
const [typeWord, xWord, yWord, zWord, selectorWord, laneWord] = words;
const screenX = (yWord - xWord) * PSX_SCREEN_SCALE;
const screenY = ((2 * zWord) - Math.floor((xWord + yWord) / 2)) * PSX_SCREEN_SCALE;
const { screenX, screenY } = projectCtorPlacement(xWord, yWord, zWord);
return {
index: -1,
@ -595,11 +615,17 @@ export function parseRegion01Records(region) {
// Loader-faithful 12-byte constructor-placement records straight out of the
// `ctorPlacements` subrange of the section pack. Layout per
// `psx_dispatch_section0_constructor_placements @ 0x800258cc`:
// [u32 count][count * { u16 typeWord; u16 X; u16 Y; u16 Z; u16 selector;
// u16 flags }]
// Each record is called as `descriptor_table[typeWord].slot0(record, 0)` and
// `psx_object_create_compound_record` then reads exactly those 6 u16 fields.
// `psx_object_create_compound_record @ 0x80025040`:
// +0x00 u16 typeWord -> descriptor_table index
// +0x02 u16 X -> obj world X (<<16 seed)
// +0x04 u16 Y -> obj world Y (<<16 seed)
// +0x06 u8 Z (low byte only!)-> obj world Z (<<16 seed). The high byte at
// +0x07 is not read as part of Z.
// +0x08 u8 selector -> state-script seed
// +0x09 u8 (padding/unknown)
// +0x0a u16 laneWord -> OR'd into obj+0x1c (flags/lane)
// The previous implementation decoded Z as a u16 from +0x06, which mixed the
// selector byte into Z and produced large bogus elevations.
export function parseCtorPlacementsBlock(block, variant = 'lset') {
if (!block || !block.buffer || block.size < 4) {
return { source: 'ctorPlacements', records: [], count: 0 };
@ -614,11 +640,17 @@ export function parseCtorPlacementsBlock(block, variant = 'lset') {
if (recordOffset + 12 > block.buffer.length) {
break;
}
const words = [];
for (let cursor = 0; cursor < 12; cursor += 2) {
words.push(readU16LE(block.buffer, recordOffset + cursor));
}
const record = buildRecord(words, 'ctorPlacements', recordOffset, words);
const typeWord = readU16LE(block.buffer, recordOffset + 0x00);
const xWord = readU16LE(block.buffer, recordOffset + 0x02);
const yWord = readU16LE(block.buffer, recordOffset + 0x04);
const zWord = block.buffer[recordOffset + 0x06];
const z2Byte = block.buffer[recordOffset + 0x07];
const selectorWord = block.buffer[recordOffset + 0x08];
const selectorHighByte = block.buffer[recordOffset + 0x09];
const laneWord = readU16LE(block.buffer, recordOffset + 0x0a);
const rawWords = [typeWord, xWord, yWord, (z2Byte << 8) | zWord, (selectorHighByte << 8) | selectorWord, laneWord];
const words = [typeWord, xWord, yWord, zWord, selectorWord, laneWord];
const record = buildRecord(words, 'ctorPlacements', recordOffset, rawWords);
if (!record) {
continue;
}
@ -646,17 +678,17 @@ export function parseCtorPlacementsBlock(block, variant = 'lset') {
// `psx_dispatch_section0_dispatch_roots @ 0x800256b0`:
// [u32 count][count * 24 bytes]
// Within each record the dispatcher reads:
// +4 u16 typeId (argument to descriptor_table[typeId])
// +8 u16 screenX (for +/-0x140 cull)
// +10 u16 screenY (for +/-0x140 cull)
// +16 u16 flags (bit 3 = skip this record)
// Z, selector and lane are not universally used by the dispatcher. The empirical
// best mapping that matches constructor-placement convention is:
// +6 u16 zeta (often 0)
// +12 u16 selector/rotation
// +14 u16 lane/flags-lo
// We keep them in rawWords so downstream consumers can probe further, but
// buildRecord uses the cull-verified X/Y/typeId for positioning.
// +0x04 u16 typeId (descriptor_table index)
// +0x08 u16 worldX (compared to cam+0x3e +/- 0x140 — cam+0x3e is the
// camera's world-X short, not the projected integer
// part; proof: root worldX values overlap constructor
// placement worldX values in the same map)
// +0x0a u16 worldY (compared to cam+0x42 +/- 0x140)
// +0x10 u16 flags (bit 3 = skip this record)
// We therefore project root worldX/worldY through the same projection as
// constructor placements (with Z=0 since dispatch roots do not carry a Z byte
// at a known offset) and render them on the same canvas. The trailing raw
// fields are preserved as `dispatchRootRawWords` for future probes.
export function parseDispatchRootsBlock(block, variant = 'lset') {
if (!block || !block.buffer || block.size < 4) {
return { source: 'dispatchRoots', records: [], count: 0 };
@ -675,17 +707,6 @@ export function parseDispatchRootsBlock(block, variant = 'lset') {
for (let cursor = 0; cursor < 24; cursor += 2) {
rawWords.push(readU16LE(block.buffer, recordOffset + cursor));
}
// Project into buildRecord's 6-word ctor-placement shape using the fields
// the live dispatcher reads. rawWords indices:
// [0..1] (+0..+3) prefix
// [2] (+4) typeId (dispatch)
// [3] (+6) zeta / z-ish
// [4] (+8) X (cull)
// [5] (+10) Y (cull)
// [6] (+12) selector-ish
// [7] (+14) lane-ish
// [8] (+16) flags (bit 3 skip)
// [9..11] trailing
const flags = rawWords[8];
if ((flags & 0x8) !== 0) {
continue;
@ -693,14 +714,12 @@ export function parseDispatchRootsBlock(block, variant = 'lset') {
const typeWord = rawWords[2];
const xWord = rawWords[4];
const yWord = rawWords[5];
const zWord = rawWords[3] & 0xff;
// Dispatch roots do not carry a Z byte at a well-known offset; they are
// treated as floor-plane (Z=0) until a Z field is recovered from the
// descriptor's slot0 callback.
const zWord = 0;
const selectorWord = rawWords[6];
const laneWord = rawWords[7];
// Relaxed plausibility: dispatch-root records can have lane==0 or large
// selectors (e.g. behavior opcodes) because the dispatcher only reads
// typeId, X, Y, and the +0x10 skip flag. We only require typeId in the
// scene-relevant range and X/Y not both zero. This is looser than
// `isPlausibleRecord` on purpose.
if (typeWord < 0x20 || typeWord > 0x1ff) {
continue;
}
@ -708,8 +727,7 @@ export function parseDispatchRootsBlock(block, variant = 'lset') {
continue;
}
const words = [typeWord, xWord, yWord, zWord, selectorWord, laneWord];
const screenX = (yWord - xWord) * PSX_SCREEN_SCALE;
const screenY = ((2 * zWord) - Math.floor((xWord + yWord) / 2)) * PSX_SCREEN_SCALE;
const { screenX, screenY } = projectCtorPlacement(xWord, yWord, zWord);
const record = {
index: -1,
source: 'dispatchRoots',