aorecomp

Anarchy Online classic in the browser

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


commit dcab329b5240804bd768a04bf5183be7aac51833
parent e100edd15488b6b53aa14e94d4502805d5901a48
Author: anon <anon@example.com>
Date:   Sat, 17 Oct 2026 23:02:26 +0000

Compute terrain height and normals directly in C

Diffstat:
Msrc/Makefile.linux | 7+++++++
Msrc/game/llr.dll/lifted-0021.c | 47+++++++++++++++++++++++++++++++++++++++++++++++
Asrc/tests/terrain_normal.c | 134+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 188 insertions(+), 0 deletions(-)

diff --git a/src/Makefile.linux b/src/Makefile.linux @@ -231,6 +231,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric ./build/linux/terrain-line ./build/linux/grid-walk ./build/linux/terrain-tile + ./build/linux/terrain-normal ./build/linux/shadow-contact ./build/linux/network-queue ./build/linux/vertex-upload @@ -554,3 +555,9 @@ build/linux/animation-events: tests/animation_events.c game/kkr.dll/lifted-0025. $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ test: build/linux/animation-events + +build/linux/terrain-normal: tests/terrain_normal.c game/llr.dll/lifted-0021.c game/mr.dll/lifted-0000.c game/mr.dll/lifted-0001.c runtime/guest_memory.c runtime/recomp_state.c runtime/recomp_types.h runtime/guest_memory.h + mkdir -p $(@D) + $(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@ + +test: build/linux/terrain-normal diff --git a/src/game/llr.dll/lifted-0021.c b/src/game/llr.dll/lifted-0021.c @@ -8030,6 +8030,52 @@ L_3803877A: PUSH32(esp, esi); /* 0x38038792: push esi */ RECOMP_ICALL(0x38038370u); /* 0x38038793: call 0x38038370 */ /* not lifted */ RECOMP_FLAGS_IN(); + /* Select the terrain triangle and form its two float-rounded edges. */ + if (!recomp_yield_hook && MEM32(0x3804E4C8) == 0x3B001170u && + MEM32(0x3804E4F4) == 0x3B0010E0u && + MEM32(0x3804E4EC) == 0x3B003730u && + MEM32(0x3804E4D4) == 0x3B001110u) { + float *a = (float *)ADDR(0x3806EFA0); + const float *b = (const float *)ADDR(0x3806EF90); + const float *c = (const float *)ADDR(0x3806EF80); + const float *d = (const float *)ADDR(0x3806EF70); + const float *left, *right; + double x = (double)*(float *)ADDR(edi) - a[0]; + double z = (double)*(float *)ADDR(edi + 8) - a[2]; + if (LO8(eax)) { + left = x > z ? c : d; + right = x > z ? b : c; + } else if (x <= (double)*(float *)ADDR(ebp - 4) - z) { + left = d; right = b; + } else { + memcpy(a, b, 12); + left = d; right = c; + } + float u[3], v[3]; + for (unsigned i = 0; i < 3; ++i) { + u[i] = (float)((double)a[i] - left[i]); + v[i] = (float)((double)a[i] - right[i]); + } + + /* Vector3_t::operator%= stores each double product difference as float. */ + esi = MEM32(ebp + 12); + float *normal = (float *)ADDR(esi); + normal[0] = (float)((double)u[1] * v[2] - (double)v[1] * u[2]); + normal[1] = (float)((double)v[0] * u[2] - (double)u[0] * v[2]); + normal[2] = (float)((double)v[1] * u[0] - (double)u[1] * v[0]); + /* Evaluate the plane before normalizing; input and output may alias. */ + double dz = ((double)*(float *)ADDR(edi + 8) - a[2]) * normal[2]; + double dx = ((double)*(float *)ADDR(edi) - a[0]) * normal[0]; + float height = (float)(-((dz + dx) / normal[1]) + a[1]); + double length2 = (double)normal[0] * normal[0] + (double)normal[1] * normal[1]; + length2 += (double)normal[2] * normal[2]; + float inverse = (float)((double)*(float *)ADDR(0x3B006048) / sqrt(length2)); + for (unsigned i = 0; i < 3; ++i) + normal[i] = (float)((double)normal[i] * inverse); + fp_push(height); + ebx = POP32_VAL(esp); + goto normal_done; + } /* test LO8(eax), LO8(eax) */ /* 0x38038798: test al, al */ _flag_a = (uint32_t)(LO8(eax)) << 24; _flag_b = (uint32_t)(LO8(eax)) << 24; _cf = 0; @@ -8279,6 +8325,7 @@ L_38038A73: RECOMP_ICALL(MEM32(0x3804E4D4)); /* 0x38038AA7: call dword ptr [0x3804e4d4] */ RECOMP_FLAGS_IN(); fp_push(*(float*)&MEM32(ebp + 0xC)); /* 0x38038AAD: fld dword ptr [ebp + 0xc] */ +normal_done: edi = POP32_VAL(esp); /* 0x38038AB0: pop edi */ esi = POP32_VAL(esp); /* 0x38038AB1: pop esi */ esp = ebp; /* 0x38038AB2: mov esp, ebp */ diff --git a/src/tests/terrain_normal.c b/src/tests/terrain_normal.c @@ -0,0 +1,134 @@ +#include "recomp_types.h" +#include <assert.h> + +void sub_380385E0(void); +void sub_3B001170(void); +void sub_3B0010E0(void); +void sub_3B001110(void); +void sub_3B003730(void); +static unsigned diagonal, registrations, corner_calls; + +static void corner_points(void) +{ + const float corners[] = {0,0,0, 4,0,0, 4,4,4, 0,0,4}; + for (unsigned i = 0; i < 4; ++i) + memcpy((void *)ADDR(MEM32(g_esp + 12 + i * 4)), corners + i * 3, 12); + ++corner_calls; + g_eax = diagonal; + g_esp += 28; +} +static void size(void) { g_eax = 1; g_esp += 4; } +static void to_integer(void) { g_eax = (int32_t)fp_pop_impl(g_st, &g_fp_top); g_esp += 4; } +static void register_exit(void) { ++registrations; g_eax = 0; g_esp += 4; } +static void yield(void) {} +recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } +recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } +recomp_func_t recomp_lookup(uint32_t address) +{ + switch (address) { + case 0x38038370: return corner_points; + case 0x38039da0: case 0x38039db0: return size; + case 0x3804b688: return to_integer; + case 0x3804b54c: return register_exit; + case 0x3b001170: case 1: return sub_3B001170; + case 0x3b0010e0: return sub_3B0010E0; + case 0x3b001110: return sub_3B001110; + case 0x3b003730: return sub_3B003730; + default: abort(); + } +} + +static void fixture(unsigned parity, float x, float z) +{ + diagonal = parity; + registrations = corner_calls = 0; + recomp_yield_hook = NULL; + MEM32(0x3804e4c8) = 0x3b001170; + MEM32(0x3804e4d4) = 0x3b001110; + MEM32(0x3804e4ec) = 0x3b003730; + MEM32(0x3804e4f4) = 0x3b0010e0; + *(float *)ADDR(0x3b006048) = 1; + memset((void *)ADDR(0x3806ef70), 0, 0x40); + + g_ecx = 0x50000000; g_esp = 0x5001ff00; + g_ebx = 111; g_esi = 222; g_edi = 333; g_ebp = 444; + g_fp_top = 2; g_st[0] = 17.25; g_st[1] = -31.5; + g_st_i64[0] = INT64_MAX; g_st_i64[1] = INT64_MIN; + MEM32(0x5000000c) = 0x50001000; + *(float *)ADDR(0x50009264) = 4; + MEM32(g_esp + 4) = 0x5000a000; + MEM32(g_esp + 8) = 0x5000a010; + *(float *)ADDR(0x5000a000) = x; + *(float *)ADDR(0x5000a004) = 51; + *(float *)ADDR(0x5000a008) = z; + memset((void *)ADDR(0x5000a010), 0xa5, 12); +} + +static void check_abi(void) +{ + assert(g_esp == 0x5001ff0c); + assert(g_ebx == 111 && g_esi == 222 && g_edi == 333 && g_ebp == 444); + assert(g_fp_top == 2 && g_st[0] == 17.25 && g_st[1] == -31.5); + assert(g_st_i64[0] == INT64_MAX && g_st_i64[1] == INT64_MIN); +} + +static void triangle(unsigned parity, float x, float z, float height, float nx, float ny, float nz) +{ + fixture(parity, x, z); + + sub_380385E0(); + + assert(fp_pop_impl(g_st, &g_fp_top) == height); + assert(fabs(*(float *)ADDR(0x5000a010) - nx) < 1e-6); + assert(fabs(*(float *)ADDR(0x5000a014) - ny) < 1e-6); + assert(fabs(*(float *)ADDR(0x5000a018) - nz) < 1e-6); + assert(registrations == 4 && corner_calls == 1); + check_abi(); +} + +static void outside(float x, float z) +{ + fixture(0, x, z); + + sub_380385E0(); + + assert(fp_pop_impl(g_st, &g_fp_top) == 0); + for (unsigned i = 0; i < 3; ++i) assert(MEM32(0x5000a010 + i * 4) == 0xa5a5a5a5); + assert(registrations == 0 && corner_calls == 0); + check_abi(); +} + +static void alias_and_fallback(unsigned fallback) +{ + fixture(1, 3, .5f); + MEM32(g_esp + 8) = 0x5000a000; + if (fallback == 1) recomp_yield_hook = yield; + if (fallback == 2) MEM32(0x3804e4c8) = 1; + + sub_380385E0(); + + /* The plane uses the overwritten input coordinates before normalization. */ + assert(fp_pop_impl(g_st, &g_fp_top) == -16); + assert(*(float *)ADDR(0x5000a000) == 0); + assert(fabs(*(float *)ADDR(0x5000a004) - .70710678f) < 1e-6); + assert(fabs(*(float *)ADDR(0x5000a008) + .70710678f) < 1e-6); + check_abi(); +} + +int main(void) +{ + assert(guest_map("terrain normal fixture", 0x50000000, 0x20000)); + assert(guest_map("terrain imports", 0x38040000, 0x10000)); + assert(guest_map("terrain globals", 0x38060000, 0x10000)); + assert(guest_map("vector constants", 0x3b000000, 0x10000)); + triangle(1, 3, .5f, .5f, 0, .70710678f, -.70710678f); + triangle(1, .5f, 3, .5f, -.70710678f, .70710678f, 0); + triangle(1, 2, 2, 2, -.70710678f, .70710678f, 0); + triangle(0, .5f, .5f, 0, 0, 1, 0); + triangle(0, 3, 3, 2, -.57735027f, .57735027f, -.57735027f); + triangle(0, 2, 2, 0, 0, 1, 0); + outside(-1, 0); outside(0, -1); outside(4, 0); outside(0, 4); + outside(NAN, 0); outside(0, NAN); outside(INFINITY, 0); + for (unsigned fallback = 0; fallback < 3; ++fallback) alias_and_fallback(fallback); + puts("terrain normals: triangles, edges, bounds, aliasing and fallback passed"); +}