aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit f168b2aebd4fbf26e41ecb1a4e1430dffa3a4ac1
parent 52fca79d3e90d293d950e4c71acf6d9f0cc2ceb0
Author: anon <anon@example.com>
Date:   Sun, 18 Oct 2026 07:18:53 +0000

Normalize shadow light direction with scalar C

Diffstat:
Msrc/game/kkr.dll/lifted-0081.c | 20++++++++++++++++++++
Msrc/tests/shadow_contact.c | 44++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 64 insertions(+), 0 deletions(-)

diff --git a/src/game/kkr.dll/lifted-0081.c b/src/game/kkr.dll/lifted-0081.c @@ -4784,6 +4784,26 @@ L_36117477: MEM32(ebp + (-0xA4)) = eax; /* 0x361174A6: mov dword ptr [ebp - 0xa4], eax */ RECOMP_ICALL(0x360D3B40u); /* 0x361174AC: call 0x360d3b40 */ /* not lifted */ RECOMP_FLAGS_IN(); +#ifdef RECOMP_PORTABLE + /* Normalize the negative light vector, preserving float spills and sum order. + * Only the three output floats are live at the ray-endpoint calculation. */ + { + double x = (float)-(double)*(float *)ADDR(eax); + double y = (float)-(double)*(float *)ADDR(eax + 4); + double z = (float)-(double)*(float *)ADDR(eax + 8); + double squared = (x * x + z * z) + y * y; + float direction[3] = {0, -1, 0}; + if (squared > (double)*(float *)ADDR(0x36170ef0)) { + double inverse = (double)*(float *)ADDR(0x36170380) / sqrt(squared); + direction[0] = (float)(x * inverse); + direction[1] = (float)(y * inverse); + direction[2] = (float)(z * inverse); + } + memcpy((void *)ADDR(ebp - 0x40), direction, sizeof(direction)); + } + esp += 4; + goto L_3611754D; +#endif fp_push(*(float*)&MEM32(eax)); /* 0x361174B1: fld dword ptr [eax] */ _st[0] = -_st[0]; /* 0x361174B3: fchs */ _flag_a = (uint32_t)(esp); _flag_b = (uint32_t)(4); diff --git a/src/tests/shadow_contact.c b/src/tests/shadow_contact.c @@ -12,6 +12,7 @@ static unsigned timing_calls, geometry_calls, update_calls, segment_calls; static unsigned line_calls, sample_calls; static int line_hit; static float contact_normal_y; +static float first_endpoint[3], updated_direction[3]; /* Exercise the controller with stubbed engine and terrain boundaries. */ static void timing(void) { ++timing_calls; g_esp += 4; } @@ -35,6 +36,8 @@ static void geometry(void) static void line(void) { assert(g_ecx == surface); + if (!line_calls) + memcpy(first_endpoint, (void *)ADDR(MEM32(g_esp + 8)), sizeof(first_endpoint)); ++line_calls; uint32_t point = MEM32(g_esp + 12), normal = MEM32(g_esp + 16); memcpy((void *)ADDR(point), (void *)ADDR(MEM32(g_esp + 4)), 12); @@ -81,6 +84,7 @@ static void cross(void) static void update(void) { assert(g_ecx == shadow && geometry_calls >= 1); + memcpy(updated_direction, (void *)ADDR(MEM32(g_esp + 4)), sizeof(updated_direction)); ++update_calls; g_eax = 1; g_esp += 20; @@ -232,6 +236,44 @@ static void normal_threshold_and_unordered_cases(void) } } +static void light_direction_tracks_inputs(void) +{ + const float inputs[][3] = {{0,1,0}, {1,0,0}, {0,0,0}, {.01f,0,0}, + {.01f,0,0}, {.01f,0,0}, {NAN,1,0}}; + const float expected[][3] = {{0,-1,0}, {-1,0,0}, {0,-1,0}, {0,-1,0}, + {-1,0,0}, {-.5f,0,0}, {0,-1,0}}; + for (unsigned i = 0; i < 7; ++i) { + arrange_fixture(); + line_hit = 1; contact_normal_y = 1; + memcpy((void *)ADDR(0x5000d000), inputs[i], 12); + *(float *)ADDR(0x36170ef0) = i >= 4 ? .00001f : .001f; + *(float *)ADDR(0x36170380) = i == 5 ? .5f : 1; + + run_controller(); + + assert(update_calls == 1 && line_calls == 2); + for (unsigned axis = 0; axis < 3; ++axis) + assert(updated_direction[axis] == expected[i][axis]); + } +} + +static void repeated_light_keeps_current_position(void) +{ + arrange_fixture(); + line_hit = 1; contact_normal_y = 1; + run_controller(); + assert(first_endpoint[0] == 64 && first_endpoint[1] == -17); + *(float *)ADDR(position) = 96; + line_calls = 0; + + run_controller(); + + assert(first_endpoint[0] == 96 && first_endpoint[1] == -17); + assert(update_calls == 2 && geometry_calls == 2 && line_calls == 2); + assert(updated_direction[0] == 0 && updated_direction[1] == -1); + assert(updated_direction[2] == 0); +} + int main(void) { assert(guest_map("shadow fixture", 0x50000000, 0x10000)); @@ -241,5 +283,7 @@ int main(void) failed_contact_with_upward_normal_keeps_samples(); successful_contact_restores_conformance(); normal_threshold_and_unordered_cases(); + light_direction_tracks_inputs(); + repeated_light_keeps_current_position(); puts("shadow-contact: failed contacts, retained sampling, recovery and guest ABI passed"); }