commit 662fa7c166d12d9a0821ac4fcdc43ac3ecf5151a
parent 5e0228db1e328a617d5dccbd3d6a6cc3dfb2a1c8
Author: anon <anon@example.com>
Date: Sat, 17 Oct 2026 10:11:42 +0000
Reject rays above resident terrain tiles before corner lookup
Diffstat:
2 files changed, 93 insertions(+), 1 deletion(-)
diff --git a/src/game/llr.dll/lifted-0023.c b/src/game/llr.dll/lifted-0023.c
@@ -5924,6 +5924,47 @@ L_3803C220:
PUSH32(esp, ebx); /* 0x3803C22C: push ebx */
PUSH32(esp, esi); /* 0x3803C22D: push esi */
PUSH32(esp, edi); /* 0x3803C22E: push edi */
+ /* Reject rays above a resident outdoor tile before constructing its corners.
+ * bbr.dll!AnarchyGroundData_t::{PatchHandle,RawHeight,Height} supplies
+ * the same four samples. Boundary and cold patches use the general path. */
+ if (MEM32(0x3804E24C) == 0x25040C80u &&
+ MEM32(0x3804E248) == 0x25040CB0u && MEM32(0x3804E244) == 0x25040840u) {
+ uint32_t data = MEM32(ecx + 12);
+ uint32_t x = MEM32(ebp + 8), z = MEM32(ebp + 12);
+ if (!MEM32(data + 0x18) && (int32_t)x >= 0 && (int32_t)z >= 0 &&
+ (int32_t)x < (int32_t)MEM32(data + 0x825c) - 2 &&
+ (int32_t)z < (int32_t)MEM32(data + 0x8260) - 2) {
+ uint32_t ground = MEM32(data + 0x8258);
+ unsigned shift = MEM32(ground + 0x3c) & 31;
+ if ((x >> shift) == ((x + 1) >> shift) &&
+ (z >> shift) == ((z + 1) >> shift)) {
+ uint32_t patch_index = (z >> shift) * MEM32(ground + 0x48) + (x >> shift);
+ uint32_t patch = MEM32(ground + 0x50) + patch_index * 0x54;
+ uint32_t samples = MEM32(patch + 0x1c);
+ if (samples) {
+ uint32_t mask = MEM32(ground + 0x40), stride = MEM32(ground + 0x38) + 1;
+ uint32_t offsets[4] = {
+ stride * (z & mask) + (x & mask),
+ stride * (z & mask) + ((x + 1) & mask),
+ stride * ((z + 1) & mask) + ((x + 1) & mask),
+ stride * ((z + 1) & mask) + (x & mask)};
+ unsigned height_shift = MEM8(ground + 0x29) ? 0 : 8;
+ double scale = *(float *)ADDR(data + 0x1c);
+ float heights[4];
+ for (unsigned i = 0; i < 4; ++i)
+ heights[i] = (float)((double)(MEM16(samples + offsets[i] * 2) >> height_shift) * scale);
+ float first = heights[0] > heights[1] ? heights[0] : heights[1];
+ float second = heights[2] > heights[3] ? heights[2] : heights[3];
+ float maximum = first > second ? first : second;
+ if (*(float *)ADDR(0x3806f02c) > maximum &&
+ *(float *)ADDR(0x3806f00c) > maximum) {
+ MEM32(patch + 0x18) = MEM32(ground + 0x24);
+ goto L_3803C2F7;
+ }
+ }
+ }
+ }
+ }
eax = ebp + (-0x30); /* 0x3803C22F: lea eax, [ebp - 0x30] */
PUSH32(esp, eax); /* 0x3803C232: push eax */
edx = ebp + (-0x18); /* 0x3803C233: lea edx, [ebp - 0x18] */
diff --git a/src/tests/terrain_tile.c b/src/tests/terrain_tile.c
@@ -17,10 +17,11 @@ void sub_3B0015A0(void);
void sub_3B001610(void);
void sub_3B003730(void);
static float corners[12];
-static unsigned parity, registrations;
+static unsigned parity, registrations, corner_calls;
static void corner_points(void)
{
+ ++corner_calls;
for (unsigned i = 0; i < 4; ++i)
memcpy((void *)ADDR(MEM32(g_esp + 12 + i * 4)), corners + i * 3, 12);
g_eax = parity;
@@ -133,6 +134,52 @@ static void maximum_corner(unsigned corner, float height)
assert((g_eax & 255) == 0);
assert(*(float *)ADDR(0x50001010) == -16);
}
+static void resident_height_rejection(unsigned full_height, unsigned mode)
+{
+ memset((void *)ADDR(0x50000000), 0, 0x20000);
+ MEM32(0x50000004) = 0x50000100;
+ MEM32(0x5000010c) = 0x50002000;
+ MEM32(0x50002018) = mode == 4;
+ *(float *)ADDR(0x5000201c) = .5f;
+ MEM32(0x5000a258) = 0x5000b000;
+ MEM32(0x5000a25c) = MEM32(0x5000a260) = 32;
+ MEM32(0x5000b024) = 1234;
+ MEM8(0x5000b029) = full_height;
+ MEM32(0x5000b038) = 16; MEM32(0x5000b03c) = 4;
+ MEM32(0x5000b040) = 15; MEM32(0x5000b048) = 2;
+ MEM32(0x5000b050) = 0x5000c000;
+ MEM32(0x5000c01c) = mode == 1 ? 0 : 0x5000d000;
+ MEM16(0x5000d000 + 88 * 2) = 256;
+ MEM16(0x5000d000 + 89 * 2) = 512;
+ MEM16(0x5000d000 + 106 * 2) = 768;
+ MEM16(0x5000d000 + 105 * 2) = 1024;
+ MEM32(0x3804e24c) = 0x25040c80;
+ MEM32(0x3804e248) = 0x25040cb0;
+ MEM32(0x3804e244) = mode == 3 ? 0x508 : 0x25040840;
+ float maximum = full_height ? 512.f : 2.f;
+ float tile[] = {0,0,0, 4,0,0, 4,maximum,4, 0,0,4};
+ memcpy(corners, tile, sizeof(tile));
+ *(float *)ADDR(0x3806f02c) = maximum + 1;
+ *(float *)ADDR(0x3806f00c) = mode == 5 ? maximum : maximum + 2;
+ g_ecx = 0x50000000; g_esp = 0x5001ff00;
+ g_ebp = 123; g_ebx = 234; g_esi = 345; g_edi = 456;
+ g_fp_top = 2; g_st[0] = 17.25; g_st[1] = -31.5;
+ MEM32(g_esp + 4) = mode == 2 ? 15 : 3; MEM32(g_esp + 8) = 5;
+ MEM32(g_esp + 12) = 0x50001000; MEM32(g_esp + 16) = 0x5000100c;
+ memset((void *)ADDR(0x50001000), 0xa5, 24);
+ corner_calls = 0;
+
+ sub_3803C220();
+
+ assert(corner_calls == (mode ? 1 : 0));
+ assert(MEM32(0x5000c018) == (mode ? 0 : 1234));
+ assert(g_esp == 0x5001ff14 && g_ebp == 123 && g_ebx == 234 && g_esi == 345 && g_edi == 456);
+ assert(g_fp_top == 2 && g_st[0] == 17.25 && g_st[1] == -31.5);
+ if (!mode) {
+ assert((g_eax & 255) == 0);
+ for (unsigned i = 0; i < 6; ++i) assert(MEM32(0x50001000 + i * 4) == 0xa5a5a5a5);
+ }
+}
int main(void)
{
assert(guest_map("terrain tile fixture", 0x50000000, 0x20000));
@@ -161,6 +208,10 @@ int main(void)
maximum_corner(2, 10);
outside_triangle();
above_tile();
+ /* Resident, cold, patch boundary, alternate import, dungeon, and touching rays. */
+ for (unsigned full_height = 0; full_height < 2; ++full_height)
+ for (unsigned mode = 0; mode < 6; ++mode)
+ resident_height_rejection(full_height, mode);
puts("terrain tile intersections passed");
return 0;
}