Merge pull request #58 from TheBozos/claude/native-ricochet-forward

Replace the two ricochet forward transforms with native math
This commit is contained in:
boubou666 authored and GitHub committed 2026-11-08 17:49:45 +01:00
commit 3e3740ff7c
5 files changed
+140 -24

No files matched your search

+4
View File
@@ -15,6 +15,10 @@ The earlier v0.3.2 release remains in Git history and on GitHub for comparison;
## Architecture and build
[`NATIVE_RICOCHET_FORWARD.md`](NATIVE_RICOCHET_FORWARD.md) adds the two ricochet movement
forwards, before its height test and after its reflection. Coverage is 139/269 known forward
returns (130 remain), with 134/134 known inverse returns native.
[`NATIVE_WEAPON_LOCK_FORWARD.md`](NATIVE_WEAPON_LOCK_FORWARD.md) adds six weapon lock-on,
look-ahead, skill-satellite and rock forwards, with actual aim, position and height-test consumers.
Coverage is 137/269 known forward returns (132 remain), with 134/134 known
+85
View File
@@ -0,0 +1,85 @@
# Native ricochet forward transforms
Two `Weapon::UpdateRicoTrans` (`1b2ee0`) forward returns use native XYZ
arithmetic after the [weapon lock-on, satellite and rock forwards](NATIVE_WEAPON_LOCK_FORWARD.md).
Known forward coverage grows from 137 to 139 of 269 returns (130 remain). Known
inverse coverage stays 134 of 134; BozoEngine stays at `4a8f70a`.
| Return PC | Producer and consumer |
| --- | --- |
| `1b2fb0` | Per-frame velocity through the weapon basis, in place, added to the ricochet position before its height test; the stack vector then receives the height-test normal and is read by `Weapons::ComputeReflection` |
| `1b3118` | Speed vector through the basis the owner rebuilt from the reflected direction, added to the ricochet position |
Both results are the owner's stack vector at frame `+0`, read only as XYZ:
`mathfAddVector` (`1daef0`), `hdHatTest` (which writes only XYZ into it), and
`ComputeReflection` (`1b7200`), whose subtract, normalize and dot-product helpers
are scalar. The owner never reads frame `+0c`, and reads no VU registers,
accumulator or flags. The existing native helper computes separately rounded XYZ,
preserves destination W and leaves VU state untouched. Unknown and historical
callers keep the original ABI. No engine or gameplay formula changes.
`Weapon::RockHD` (`1b6cd8`, `1b6d3c`) was examined and left on the original path:
its transformed point is handed to `hdPntSphereTest`, which publishes the pointer
to the `hdHier` hierarchy traversal. That consumer needs its own audit.
## Generated comparisons
Evidence is under `D:/BozoScratch/ricochet-forward`, against parent `4d863cd`
(PR #57). Parent headless/window binaries are preserved there and match PR #57's
manifest. Generated bodies, retail data and runtime recordings stay outside Git.
- Added coverage is 1,024 generated sequences / 64 whole-memory comparisons
(caller kinds 242 and 243): helper-boundary continuations and full-owner
entries, across four rounding modes, FTZ/DAZ, RAM aliases, identity/quarter-turn
bases and arbitrary W payloads.
- Full entries execute the real height test and, when the resulting height is
low, the real reflection branch: `ComputeReflection`, `mathfRotationFromVector`
and `mathfRotMatrixPH` rebuild the basis before the second transform. The
frame-count-zero scenario skips that branch. Producer reach is asserted for
both sites in every case.
- Transform inputs and outputs are captured at the real helper and
`mathfAddVector` boundaries. Where the fixture knows the basis (the first site,
and second-site helper-boundary cases), the output is checked against an
independent identity or quarter-turn result; the owner-rebuilt basis is covered
by the native/original comparison. The published point is asserted exactly
against the original FPU addition: X/Y at `hdHatTest` (Z is lifted for the
test) and the final position after the second transform. The first site
publishes in 768 cases, the second in 960.
- The VU rule from the previous increment applies: exact outside the original
leaf's write set; inside it, the original's later write or the native leaf's
entry value. Only forward scratch W at frame `+0c` is normalized for test
comparison, after every real consumer.
- The particle effect paths of the ricochet's launch state stay on the original
path and are not reached by these fixtures.
- The fixture fix also restores a satellite setup line from PR #57 that a
misplaced comment had disabled (`vehicle+0x2370` now gets its fixed point
instead of fill data). All earlier counts and assertions are unchanged.
- Aggregate caller coverage is 124,928 generated sequences / 5,792 whole-memory
comparisons. Historical forward ABI coverage is 8,320 / 520; inverse remains
8,576 / 134.
## Replay and window qualification
Player, headless runner, historical checker and all eleven native test targets
build. All eleven native suites and twelve Python tooling tests pass, with every
previous publication assertion enabled.
Default and strict routes complete 9,500 one-player and 8,500 actual four-player
frames. Every recording field matches parent `4d863cd`: raw RAM, scratchpad,
calls, draws, sound, thread state and EE timing/counts. Sampled 32 MiB RAM images
are identical. Strict/default recordings and snapshots are byte exact; strict
comparisons report no mismatch. The first ricochet return `1b2fb0` is live: it runs
natively 319 times on the one-player route with no recorded difference (the rock's
`1b3ef8` still runs 1,569 times, also exact). The reflection-branch return `1b3118`
is cold on these routes and is qualified by the generated owner suite above.
Fresh isolated window runs complete 6,500 one-player, 6,500 actual four-player
and 4,200 movie frames; `1b2fb0` runs natively 67 times in the one-player window.
Reviewed captures show the intro and credits, menus, vehicle and level selection,
driving with the HUD and four simultaneous views. Each route has five distinct
captures. Stereo 48 kHz, 16-bit WAV recordings contain nonzero audio.
Audio device underruns remain: 55.9 ms silence in one-player, 40.7 ms in
four-player and 18.0 ms in the movie run. These are functional checks, not a
performance or flawless-playback claim. A visible ricochet bounce off a wall, full
campaign and physical controller/hardware acceptance remain open.
+2
View File
@@ -1,5 +1,7 @@
# Native weapon lock-on, satellite and rock forward transforms
[Ricochet forwards](NATIVE_RICOCHET_FORWARD.md) follow with two more weapon sites.
Six weapon forward returns use native XYZ arithmetic after the
[Minion shield forwards](NATIVE_MINION_FORWARD.md). Known forward coverage grows
from 131 to 137 of 269 returns (132 remain). Known inverse coverage stays 134 of
+3 -2
View File
@@ -10,7 +10,7 @@
namespace tmb::math
{
// Audited XYZ-only consumers: collision queries, vehicle spatial records and hierarchy lighting/attachments.
inline constexpr std::array<uint32_t, 137> directionReturns{
inline constexpr std::array<uint32_t, 139> directionReturns{
0x168b30, 0x1cd618, 0x1cda54, 0x1cdabc, 0x11708c, 0x1880e0, 0x1d4284, 0x1d42e8, 0x1cb19c,
0x1cfd18, 0x1cfde8, 0x1d0224, 0x1d0250, // height-test collision/BSP results
0x1d25ec, 0x1d261c, 0x1d2784, 0x1d2804, // line-of-sight BSP/volume results
@@ -48,7 +48,8 @@ namespace tmb::math
0x129e58, 0x137cac, // helicopter movement and cable attachment position
0x1b2d1c, 0x1b3d30, 0x1b3e00, // gascan, saw-blade and taxi-spike movement
0x1ada84, 0x1adb8c, 0x1adba0, 0x1adca8, 0x1adcbc, 0x1addc4, 0x1addd8, // Minion shield hit-point distances
0x1b1700, 0x1b1bbc, 0x1b339c, 0x1b3634, 0x1b371c, 0x1b3ef8}; // weapon lock-on/look-ahead targets, satellite and rock movement
0x1b1700, 0x1b1bbc, 0x1b339c, 0x1b3634, 0x1b371c, 0x1b3ef8, // weapon lock-on/look-ahead targets, satellite and rock movement
0x1b2fb0, 0x1b3118}; // ricochet movement before its height test and after its reflection
struct alignas(16) Vector { std::array<uint32_t, 4> bits{}; };
struct Matrix { std::array<Vector, 4> column{}; };
+46 -22
View File
@@ -225,7 +225,7 @@ namespace
countMinionDirection(ctx); const auto before=*ctx; nativeDirectionImplementation(ram,ctx,rt);
require(vuUntouched(before,*ctx),"Minion forward leaf changed VU state");
}
constexpr uint32_t weaponDirectionReturns[]={0x1b1700,0x1b1bbc,0x1b339c,0x1b3634,0x1b371c,0x1b3ef8};
constexpr uint32_t weaponDirectionReturns[]={0x1b1700,0x1b1bbc,0x1b339c,0x1b3634,0x1b371c,0x1b3ef8,0x1b2fb0,0x1b3118};
std::array<unsigned,std::size(weaponDirectionReturns)> weaponDirectionCalls{};
void countWeaponDirection(const R5900Context *ctx) {
for(unsigned i=0;i<std::size(weaponDirectionReturns);++i) if(getRegU32(ctx,31)==weaponDirectionReturns[i]) ++weaponDirectionCalls[i];
@@ -345,7 +345,7 @@ void testHierarchyHelperCallers()
0x11ad1c,0x11ada0,0x11b26c,0x11bba8,0x11bbe8,0x11c368,
0x129e58,0x137cac,0x1b2d1c,0x1b3d30,0x1b3e00,
0x1ada84,0x1adb8c,0x1adba0,0x1adca8,0x1adcbc,0x1addc4,0x1addd8,
0x1b1700,0x1b1bbc,0x1b339c,0x1b3634,0x1b371c,0x1b3ef8};
0x1b1700,0x1b1bbc,0x1b339c,0x1b3634,0x1b371c,0x1b3ef8,0x1b2fb0,0x1b3118};
const auto isInverse = [](unsigned kind) { return kind < 4 || kind == 12 || kind == 13 || (kind >= 24 && kind <= 30) || (kind >= 37 && kind <= 45) || (kind >= 50 && kind <= 57) || (kind >= 65 && kind <= 67) || (kind >= 76 && kind <= 96) || (kind >= 111 && kind <= 118) || (kind >= 123 && kind <= 125) || (kind >= 135 && kind <= 138) || (kind >= 155 && kind <= 187) || (kind >= 189 && kind <= 200); };
const unsigned saved = _mm_getcsr(); unsigned cases = 0, memoryCases = 0; std::array<unsigned,2> aiShotPublications{};
unsigned weaponJumpCases=0, lightingPublicationCases=0, rolloverPublicationCases=0;
@@ -354,7 +354,7 @@ void testHierarchyHelperCallers()
std::array<unsigned,4> collisionPublications{}; unsigned collisionErrnoCases=0;
std::array<unsigned,5> attachmentPublications{}; unsigned gridRemovals=0;
std::array<unsigned,4> minionPublications{}, minionDestructions{}; unsigned minionBoundaries=0;
std::array<unsigned,6> weaponPublications{};
std::array<unsigned,8> weaponPublications{};
for (unsigned mode = 0; mode < 4; ++mode) for (unsigned kind = 0; kind < std::size(callers); ++kind)
for (unsigned n = 0; n < 128; ++n)
{
@@ -1959,12 +1959,12 @@ void testHierarchyHelperCallers()
reg(17,object); reg(16,actor); reg(4,parent+0x20); reg(5,sp);
std::memcpy(ram+sp,ram+0x4e56c0,16);
} else if(kind>=236) {
// Weapon lock-on, look-ahead, skill-satellite and rock transforms through their real owners.
// Weapon lock-on, look-ahead, skill-satellite, rock and ricochet transforms through their real owners.
const unsigned site=kind-236;
const auto fp=[&](unsigned r,float v) { std::memcpy(&initial.fu[r],&v,4); };
constexpr uint32_t owners[]={0x1b16a8,0x1b1ae0,0x1b3350,0x1b3350,0x1b3350,0x1b3e28};
constexpr RecompiledFunction resumes[]={resume_0x1b16a8,resume_0x1b1ae0,resume_0x1b3350,resume_0x1b3350,resume_0x1b3350,resume_0x1b3e28};
continuation=resumes[site]; entry=owners[site]; returnAt(site<2 ? 0x70 : 0x30);
constexpr uint32_t owners[]={0x1b16a8,0x1b1ae0,0x1b3350,0x1b3350,0x1b3350,0x1b3e28,0x1b2ee0,0x1b2ee0};
constexpr RecompiledFunction resumes[]={resume_0x1b16a8,resume_0x1b1ae0,resume_0x1b3350,resume_0x1b3350,resume_0x1b3350,resume_0x1b3e28,resume_0x1b2ee0,resume_0x1b2ee0};
continuation=resumes[site]; entry=owners[site]; returnAt(site<2 ? 0x70 : site>=6 ? 0xb0 : 0x30);
word(object+0x130,address(cs)); word(object+0x110,address(vehicle)); word(object+0x114,scenario==2 && n>=64 ? 0 : address(vehicle)); // no target only gates full entries
word(object+4,scenario==1 ? 4 : 9); word(object+0x34,scenario==3 ? 0 : 1); word(object+0x3c,n&1); word(object+0x40,0);
f(object+0x54,0.f); word(object+0x14,0); f(object+0x70,2.f); f(object+0x94,-5.f); f(object+0x98,scenario==4 ? .5f : 6.f);
@@ -1972,12 +1972,21 @@ void testHierarchyHelperCallers()
word(object+0x20,site==2 ? 0 : site==3 ? 1+(scenario&1) : 3);
basis(vehicle+0xd0,n%4==0); basis(object+0xd0,n%4==1); xyz(vehicle+0x1d0,100.f,200.f,300.f);
f(vehicle+0x2c4,2.f); word(vehicle+0x2018,address(child)); xyz(child+0x10,40.f,50.f,60.f); // look-ahead scales the basis column at +0xe0
f(vehicle+0x2378,scenario==5 || site==3 || site==4 ? 1000.f : -1000.f); // satellites keep zero angles: exact identity rebuild xyz(vehicle+0x2370,5.f,6.f,7.f);
f(vehicle+0x2378,scenario==5 || site==3 || site==4 ? 1000.f : -1000.f); xyz(vehicle+0x2370,5.f,6.f,7.f); // satellites keep zero angles: exact identity rebuild
f(gp-0x49cc,-1000.f); // rock impact effect/kill boundary stays on the original path, not reached
reg(16,object); reg(17,site==5 ? scene+0x190c20 : object+0x90); fp(20,10.f);
if(site<2) { reg(4,vehicle+0xd0); reg(5,object+0x120); reg(6,sp); }
else if(site==2) { reg(4,object+0xd0); reg(5,object+0x90); reg(6,sp); }
else { reg(4,site==5 ? object+0xd0 : cs+0x20); reg(5,sp); reg(6,sp); xyz(sp,3.f,-4.f,8.f); } // actual in-place transforms
else if(site<6) { reg(4,site==5 ? object+0xd0 : cs+0x20); reg(5,sp); reg(6,sp); xyz(sp,3.f,-4.f,8.f); } // actual in-place transforms
else {
// Ricochet: s2 is the weapon. A low resulting height takes the real reflection branch to the second
// transform, whose basis the owner rebuilds from the reflected direction; frame count 0 skips it.
reg(18,object); reg(19,object+0xd0); reg(16,scene+0x190c20);
f(object+0x98,-25.f); word(object+0x20,1+(n&1)); word(object+0x24,0); f(gp-0x4a8c,-1e6f); f(gp-0x4a80,1e6f);
xyz(object+0xc0,.25f,.5f,0.f); f(gp-0x4a7c,.5f); f(gp-0x4a78,10.f); // bounded heading/pitch smoothing
reg(4,object+0xd0); reg(6,sp);
if(site==6) { reg(5,sp); xyz(sp,3.f,-4.f,-25.f); } else reg(5,object+0x90);
}
} else {
continuation=kind==226 ? resume_0x1b2c40 : kind==227 ? resume_0x1b3c88 : resume_0x1b3d58;
entry=kind==226 ? 0x1b2c40 : kind==227 ? 0x1b3c88 : 0x1b3d58;
@@ -2405,19 +2414,34 @@ void testHierarchyHelperCallers()
const unsigned site=kind-236, scenario=(n%64)/8;
const auto read=[&](uint32_t at) { uint32_t v; std::memcpy(&v,ram+at,4); return v; };
const bool active=!fromEntry || site>=2 || (scenario!=1 && scenario!=2 && scenario!=3);
std::array<unsigned,6> reached{}; reached[site]=active;
std::array<unsigned,8> reached{};
std::vector<unsigned> order; // reached sites in execution order
if(site<6) { reached[site]=active; if(active) order.push_back(site); }
else {
// Ricochet: the first transform always runs from the entry; the reflection branch then reaches the second.
if(site==6 || fromEntry) { reached[6]=1; order.push_back(6); }
if(!fromEntry || scenario!=4) { reached[7]=1; order.push_back(7); }
}
require(refWeaponDirections==reached,"weapon forward producer reach differs");
const uint32_t frame=fromEntry ? sp-(site<2 ? 0x90u : site==5 ? 0x60u : 0x50u) : sp;
require(referenceW.size()==unsigned(active) && (!active || referenceW[0].offset==frame+0xc),"unexpected weapon forward-W exclusion");
require(refWeaponInputs.size()==unsigned(active) && refWeaponOutputs.size()==unsigned(active),"weapon transform consumer not executed");
if(active) {
// Independent quarter-turn/identity result, then the actual XYZ publication through mathfAddVector.
const bool rotated=site==2 || site==5 ? n%4==1 : (site==3 || site==4) && fromEntry ? false : n%4==0;
float in[3], out[3]; std::memcpy(in,refWeaponInputs[0].data(),12); std::memcpy(out,refWeaponOutputs[0].data(),12);
require(out[0]==(rotated ? -in[1] : in[0]) && out[1]==(rotated ? in[0] : in[1]) && out[2]==in[2],"weapon independent transform differs");
const uint32_t frame=fromEntry ? sp-(site<2 ? 0x90u : site==5 ? 0x60u : site>=6 ? 0xd0u : 0x50u) : sp;
require(referenceW.size()==unsigned(!order.empty()) && (order.empty() || referenceW[0].offset==frame+0xc),"unexpected weapon forward-W exclusion");
require(refWeaponInputs.size()==order.size() && refWeaponOutputs.size()==order.size(),"weapon transform consumer not executed");
for(size_t k=0;k<order.size();++k) {
// Independent quarter-turn/identity result where the basis is fixture-known, then the actual XYZ
// publication through mathfAddVector. Ricochet's second basis is rebuilt by the owner after its first.
const unsigned at=order[k];
const bool known=at!=7 || (site==7 && !fromEntry);
const bool rotated=at==2 || at>=5 ? n%4==1 : (at==3 || at==4) && fromEntry ? false : n%4==0;
float in[3], out[3]; std::memcpy(in,refWeaponInputs[k].data(),12); std::memcpy(out,refWeaponOutputs[k].data(),12);
if(known) require(out[0]==(rotated ? -in[1] : in[0]) && out[1]==(rotated ? in[0] : in[1]) && out[2]==in[2],"weapon independent transform differs");
std::array<uint32_t,3> sum;
for(unsigned i=0;i<3;++i) sum[i]=bozo::fadd(nullptr,refWeaponBases[0][i],refWeaponOutputs[0][i]);
if(site<2) {
for(unsigned i=0;i<3;++i) sum[i]=bozo::fadd(nullptr,refWeaponBases[k][i],refWeaponOutputs[k][i]);
if(at==6) {
require(refAttachmentActions[6]==1 && refWeaponTargets.size()==1 && refWeaponTargets[0][0]==sum[0] && refWeaponTargets[0][1]==sum[1],
"ricochet height-test position differs");
} else if(at==7) {
require(read(0x22010)==sum[0] && read(0x22014)==sum[1] && read(0x22018)==sum[2],"ricochet published position differs");
} else if(at<2) {
require(!refWeaponTargets.empty() && refWeaponTargets[0]==sum,"weapon lock target point differs");
if(site==0) require(refWeaponBases[0][0]==0x42c80000 && refWeaponBases[0][1]==0x43480000 && refWeaponBases[0][2]==0x43960000,"lock-on target position differs");
} else if(site<5) {
@@ -2426,7 +2450,7 @@ void testHierarchyHelperCallers()
require(refAttachmentActions[6]==1 && refWeaponTargets.size()==1 && refWeaponTargets[0][0]==sum[0] && refWeaponTargets[0][1]==sum[1],
"rock height-test position differs");
}
++weaponPublications[site];
++weaponPublications[at];
}
}
if (kind == 138 && fromEntry) require(refRandomCalls == 2, "random-position producer must make two real rand calls");
@@ -2499,7 +2523,7 @@ void testHierarchyHelperCallers()
std::all_of(minionDestructions.begin(),minionDestructions.end(),[](unsigned n) { return n!=0; }) && minionBoundaries,"Minion fixtures missed damage/destruction/distance boundaries");
std::printf("Minion forward publication cases: damage %u/%u/%u/%u destruction %u/%u/%u/%u exact distance boundaries %u\n",minionPublications[0],minionPublications[1],minionPublications[2],minionPublications[3],minionDestructions[0],minionDestructions[1],minionDestructions[2],minionDestructions[3],minionBoundaries);
require(std::all_of(weaponPublications.begin(),weaponPublications.end(),[](unsigned n) { return n!=0; }),"weapon fixtures missed transform publication");
std::printf("Weapon forward publication cases: lock-on %u look-ahead %u skill-satellite %u/%u/%u rock %u\n",weaponPublications[0],weaponPublications[1],weaponPublications[2],weaponPublications[3],weaponPublications[4],weaponPublications[5]);
std::printf("Weapon forward publication cases: lock-on %u look-ahead %u skill-satellite %u/%u/%u rock %u ricochet %u/%u\n",weaponPublications[0],weaponPublications[1],weaponPublications[2],weaponPublications[3],weaponPublications[4],weaponPublications[5],weaponPublications[6],weaponPublications[7]);
host.enabled = false; unsigned historical = 0;
for (unsigned kind = 4; kind < std::size(callers); ++kind) for (unsigned n = 0; n < 64; ++n)
{