commit 46c0100f179a34ef1d63274cd1d92501ddf3a312
parent 81c39de5ee15b11f9e72a5d9fb1a0c0bd3b56358
Author: anon <anon@example.com>
Date: Sat, 17 Oct 2026 06:25:58 +0000
Optimize resident terrain height reads with compatible guest state
Diffstat:
5 files changed, 210 insertions(+), 3 deletions(-)
diff --git a/README.md b/README.md
@@ -17,9 +17,9 @@ launch creates a character at Bronto Burger if no characters exist. Existing
characters and local data are preserved. [Server controls](http://127.0.0.1:8080/control)
provide character editing, teleporting and time controls.
-The browser build is an initial port: Bronto Burger has passed rendering,
-movement, inventory/map, audio and connection checks. Lush Fields currently has
-a known loading fault.
+Bronto Burger, Lush Fields and Newland have passed browser rendering, movement,
+inventory/map, audio and connection checks. Performance work and broader
+gameplay validation continue.
For native clients, keep the server running in a separate terminal and use the
commands below. Creating a character in server controls also works.
diff --git a/src/Makefile.linux b/src/Makefile.linux
@@ -222,6 +222,7 @@ test: build/linux/window build/linux/graphics build/linux/atoi build/linux/stric
./build/linux/fpu-stack
./build/linux/water-ring
./build/linux/skinning
+ ./build/linux/ground-height
./build/linux/tile-corners
./build/linux/terrain-line
./build/linux/network-queue
@@ -477,3 +478,9 @@ build/linux/skinning: tests/skinning.c game/bbr.dll/lifted-0061.c runtime/guest_
$(CC) $(CPPFLAGS) $(CFLAGS) -ffunction-sections -fdata-sections $(filter %.c,$^) -Wl,--gc-sections -pthread -lm -o $@
test: build/linux/skinning
+
+build/linux/ground-height: tests/ground_height.c game/bbr.dll/lifted-0013.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/ground-height
diff --git a/src/Makefile.wasm b/src/Makefile.wasm
@@ -74,3 +74,7 @@ build/wasm/terrain-line.js: tests/terrain_line.c game/llr.dll/lifted-0023.c runt
build/wasm/skinning.js: tests/skinning.c game/bbr.dll/lifted-0061.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 -lm -o $@
+
+build/wasm/ground-height.js: tests/ground_height.c game/bbr.dll/lifted-0013.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 -lm -o $@
diff --git a/src/game/bbr.dll/lifted-0013.c b/src/game/bbr.dll/lifted-0013.c
@@ -9421,6 +9421,59 @@ void sub_25040840(void) {
RECOMP_REGS_LOCALS
RECOMP_ENTER(0x25040840u);
+ /* AnarchyGroundData_t::Height: resident patches need no loader calls. */
+ {
+ uint32_t stack = esp, ground = ecx;
+ uint32_t x = MEM32(stack + 4), z = MEM32(stack + 8);
+ uint32_t shift = MEM32(ground + 0x3c) & 31;
+ uint32_t mask = MEM32(ground + 0x40);
+ uint32_t local_x = x & mask, local_z = z & mask;
+ uint32_t patch_index = (uint32_t)((int32_t)z >> shift) * MEM32(ground + 0x48)
+ + (uint32_t)((int32_t)x >> shift);
+ uint32_t patch = MEM32(ground + 0x50) + patch_index * 0x54;
+ if (MEM32(patch + 0x1c)) {
+ /* Preserve the original guest frame and overwritten arguments. */
+ MEM32(stack - 4) = ebp;
+ MEM32(stack - 28) = ebx;
+ MEM32(stack - 32) = esi;
+ MEM32(stack - 36) = edi;
+ unsigned full_height = MEM8(ground + 0x29);
+ if (full_height) {
+ MEM32(stack + 4) = local_x;
+ } else {
+ MEM32(stack - 16) = local_z;
+ MEM32(stack - 20) = local_x;
+ MEM32(stack - 24) = patch;
+ }
+ MEM32(stack + 8) = patch;
+
+ /* Access stamps participate in FlushHeightMapCache eviction. */
+ uint32_t stamp = MEM32(ground + 0x24);
+ MEM32(patch + 0x18) = stamp;
+ uint32_t row = (MEM32(ground + 0x38) + 1) * local_z;
+ uint32_t index = row + local_x;
+ uint32_t data = MEM32(patch + 0x1c);
+ uint32_t height = MEM16(data + index * 2);
+ ecx = data;
+ if (full_height) {
+ eax = (index & 0xffff0000u) | height;
+ edx = stamp;
+ _flag_a = row; _flag_b = local_x; _cf = index < row;
+ _flag_k = FK_ADD;
+ } else {
+ eax = height >> 8;
+ edx = index;
+ _cf = (height >> 7) & 1;
+ _flag_a = recomp_flags_pack(eax, _cf, 0, 0);
+ _flag_b = 0; _flag_k = FK_EFLAGS;
+ }
+ esp = stack + 12;
+ RECOMP_FLAGS_OUT();
+ RECOMP_REGS_OUT();
+ return;
+ }
+ }
+
L_25040840:
PUSH32(esp, ebp); /* 0x25040840: push ebp */
ebp = esp; /* 0x25040841: mov ebp, esp */
@@ -10293,6 +10346,35 @@ void sub_25040CB0(void) {
RECOMP_REGS_LOCALS
RECOMP_ENTER(0x25040CB0u);
+ /* AnarchyGroundData_t::RawHeight: preserve the resident-patch access. */
+ {
+ uint32_t stack = esp, ground = ecx;
+ uint32_t patch = MEM32(stack + 4);
+ if (MEM32(patch + 0x1c)) {
+ /* Preserve the original guest frame and caller-saved results. */
+ MEM32(stack - 4) = ebp;
+ MEM32(stack - 28) = ebx;
+ MEM32(stack - 12) = patch;
+ MEM32(patch + 0x18) = MEM32(ground + 0x24);
+ MEM32(stack - 32) = esi;
+ MEM32(stack - 36) = edi;
+ MEM32(stack - 24) = ground;
+
+ uint32_t x = MEM32(stack + 8), z = MEM32(stack + 12);
+ uint32_t row = (MEM32(ground + 0x38) + 1) * z;
+ uint32_t index = row + x;
+ uint32_t data = MEM32(patch + 0x1c);
+ eax = (patch & 0xffff0000u) | MEM16(data + index * 2);
+ ecx = data;
+ edx = index;
+ _flag_a = row; _flag_b = x; _cf = index < row; _flag_k = FK_ADD;
+ esp = stack + 16;
+ RECOMP_FLAGS_OUT();
+ RECOMP_REGS_OUT();
+ return;
+ }
+ }
+
L_25040CB0:
PUSH32(esp, ebp); /* 0x25040CB0: push ebp */
ebp = esp; /* 0x25040CB1: mov ebp, esp */
diff --git a/src/tests/ground_height.c b/src/tests/ground_height.c
@@ -0,0 +1,114 @@
+#include "recomp_types.h"
+#include <assert.h>
+
+void sub_25040840(void);
+void sub_25040CB0(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) { abort(); }
+
+static void setup(void)
+{
+ memset((void *)ADDR(0x50000000), 0xa5, 0x20000);
+ g_eax = 0x12345678; g_ebx = 0x23456789; g_edx = 0x34567890;
+ g_esi = 0x45678901; g_edi = 0x56789012; g_ebp = 0x67890123;
+ g_ecx = 0x50002000; g_esp = 0x5001f000;
+ g_flag_k = FK_CMP; g_flag_a = 12; g_flag_b = 34; g_flag_cf = 1;
+ g_fp_top = 3;
+ for (unsigned i = 0; i < 8; ++i) {
+ g_st[i] = i + .375;
+ g_st_i64[i] = INT64_MAX - i;
+ }
+
+ /* Two resident patches with a 65-sample row, including the border. */
+ MEM32(0x50002024) = 0x1234beef;
+ MEM8(0x50002029) = 1;
+ MEM32(0x50002038) = 64;
+ MEM32(0x5000203c) = 6;
+ MEM32(0x50002040) = 63;
+ MEM32(0x50002048) = 3;
+ MEM32(0x50002050) = 0x50003000;
+ MEM32(0x5000301c) = 0x50005000;
+ MEM32(0x50003070) = 0x50007400;
+}
+
+static void check_preserved_state(void)
+{
+ assert(g_ebx == 0x23456789 && g_esi == 0x45678901);
+ assert(g_edi == 0x56789012 && g_ebp == 0x67890123);
+ assert(MEM32(0x5001effc) == 0x67890123);
+ assert(MEM32(0x5001efe4) == 0x23456789);
+ assert(MEM32(0x5001efe0) == 0x45678901);
+ assert(MEM32(0x5001efdc) == 0x56789012);
+ assert(g_fp_top == 3);
+ for (unsigned i = 0; i < 8; ++i) {
+ assert(g_st[i] == i + .375);
+ assert(g_st_i64[i] == INT64_MAX - i);
+ }
+}
+
+static void height_crosses_patch_boundary(unsigned full)
+{
+ setup();
+ MEM8(0x50002029) = full;
+ MEM32(g_esp + 4) = 65;
+ MEM32(g_esp + 8) = 2;
+ MEM16(0x50007506) = 0xabcd;
+
+ sub_25040840();
+
+ assert(g_eax == (full ? 0xabcd : 0xab));
+ assert(g_ecx == 0x50007400);
+ assert(g_edx == (full ? 0x1234beef : 131));
+ assert(g_esp == 0x5001f00c);
+ assert(MEM32(0x5000306c) == 0x1234beef);
+ assert(MEM32(0x50003018) == 0xa5a5a5a5);
+ assert(MEM32(0x5001f004) == (full ? 1 : 65));
+ assert(MEM32(0x5001f008) == 0x50003054);
+ if (full) {
+ assert(g_flag_k == FK_ADD && g_flag_a == 130 && g_flag_b == 1);
+ assert(g_flag_cf == 0);
+ } else {
+ assert(g_flag_k == FK_EFLAGS && g_flag_a == 0x203 && g_flag_b == 0);
+ assert(g_flag_cf == 1);
+ assert(MEM32(0x5001eff0) == 2);
+ assert(MEM32(0x5001efec) == 1);
+ assert(MEM32(0x5001efe8) == 0x50003054);
+ }
+ check_preserved_state();
+}
+
+static void raw_height_reads_border(unsigned stamp_alias)
+{
+ setup();
+ MEM32(g_esp + 4) = 0x50003000;
+ MEM32(g_esp + 8) = 64;
+ MEM32(g_esp + 12) = 64;
+ MEM16(0x50007100) = 0xabcd;
+ if (stamp_alias)
+ MEM32(0x5000301c) = 0x50000f18;
+
+ sub_25040CB0();
+
+ assert(g_eax == (stamp_alias ? 0x5000beef : 0x5000abcd));
+ assert(g_ecx == (stamp_alias ? 0x50000f18 : 0x50005000));
+ assert(g_edx == 4224 && g_esp == 0x5001f010);
+ assert(MEM32(0x50003018) == 0x1234beef);
+ assert(MEM32(0x5001eff4) == 0x50003000);
+ assert(MEM32(0x5001efe8) == 0x50002000);
+ assert(g_flag_k == FK_ADD && g_flag_a == 4160 && g_flag_b == 64);
+ assert(g_flag_cf == 0);
+ check_preserved_state();
+}
+
+int main(void)
+{
+ assert(guest_map("ground height fixture", 0x50000000, 0x20000));
+ height_crosses_patch_boundary(0);
+ height_crosses_patch_boundary(1);
+ raw_height_reads_border(0);
+ raw_height_reads_border(1);
+ puts("ground height: patch crossing, normalization, border and stamp alias passed");
+ return 0;
+}