aorecomp

Anarchy Online classic in the browser

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


client/runtime/browser_resolution.c (33442B)


      1 #include "recomp_types.h"
      2 #include "linux_graphics.h"
      3 #include "guest_heap.h"
      4 #include <assert.h>
      5 #include <stdatomic.h>
      6 #ifdef __EMSCRIPTEN__
      7 #include <emscripten.h>
      8 #else
      9 #define EMSCRIPTEN_KEEPALIVE
     10 #endif
     11 
     12 static _Atomic uint32_t requested_size;
     13 static _Atomic unsigned chat_viewport_bottom;
     14 static _Atomic int orbit_x, orbit_y, orbit_active;
     15 static _Atomic int wheel_x, wheel_y;
     16 static _Atomic int orbit_zoom;
     17 static _Atomic int cursor_hidden;
     18 static _Atomic uint32_t hud_insets;
     19 static _Atomic int chat_scroll_pixels;
     20 static _Atomic unsigned touch_frame_request;
     21 static _Atomic(char *) requested_creation_name;
     22 static int chat_scroll_remainder;
     23 static uint32_t chat_width_owner;
     24 static unsigned chat_max_width;
     25 static int hud_adjusted;
     26 static int orbit_running;
     27 static struct {
     28     uint32_t chat, hidden_toolbar;
     29     int input_x, input_y, output_x, output_y, output_height;
     30     unsigned output_lines;
     31 } mobile_chat;
     32 void ao_chat_bounds(int x, int y, int width, int height, int screen_width, int screen_height,
     33                     int output_x, int output_y, int output_width, int output_height);
     34 void ao_touch_regions(const int *regions, unsigned count, int screen_width, int screen_height, int item_menu);
     35 void ao_creation_state(const int *bounds, const char *name);
     36 void ao_touch_frame(unsigned request);
     37 
     38 EMSCRIPTEN_KEEPALIVE void browser_touch_frame(unsigned request)
     39 {
     40     atomic_store(&touch_frame_request, request);
     41 }
     42 
     43 void browser_touch_frame_complete(void)
     44 {
     45     /* A request can arrive after this frame's input poll. Acknowledge it only
     46        after the following complete frame has consumed the queued pointer motion. */
     47     static unsigned pending;
     48     if (pending) ao_touch_frame(pending);
     49     pending = atomic_exchange(&touch_frame_request, 0);
     50 }
     51 
     52 static void publish_creation_state(void)
     53 {
     54     uint32_t scene = MEM32(0x2b0ee7d0);
     55     /* CharCreateModule_t::FrameProcess clears the scene for the outro, while
     56      * its renderer remains alive until ShutdownMessage (sub_2B02DE30). */
     57     int bounds[] = {scene || MEM32(0x2b0ee7e4), 0, 0, 0, 0,
     58                     MEM32(0x27048be8), MEM32(0x27048bec)};
     59     const char *name = "";
     60     /* NameScene_t::sub_2B0338B0 reads the InputWindow_t and its native string. */
     61     if (scene && scene == MEM32(0x2b0ee7e0)) {
     62         uint32_t input = MEM32(scene + 0x88);
     63         if (input && MEM8(MEM32(input + 4) + 8)) {
     64             uint32_t window = MEM32(input);
     65             for (unsigned i = 0; i < 4; ++i) bounds[i + 1] = MEM32(window + 0x2c + i * 4);
     66             uint32_t text = MEM32(MEM32(MEM32(input + 8) + 0x50) + 4);
     67             if (text) name = (const char *)ADDR(text);
     68         }
     69     }
     70 
     71     /* Include native edits and random-name replies even when layout is unchanged. */
     72     static void *previous;
     73     static size_t previous_size;
     74     size_t size = sizeof(bounds) + strlen(name) + 1;
     75     if (previous_size == size && !memcmp(previous, bounds, sizeof(bounds)) &&
     76         !strcmp((const char *)previous + sizeof(bounds), name)) return;
     77     previous = realloc(previous, size);
     78     if (!previous) abort();
     79     previous_size = size;
     80     memcpy(previous, bounds, sizeof(bounds));
     81     memcpy((char *)previous + sizeof(bounds), name, size - sizeof(bounds));
     82     ao_creation_state(previous, (const char *)previous + sizeof(bounds));
     83 }
     84 
     85 static void publish_touch_regions(void)
     86 {
     87     /* Native hit order and rectangles: client/game/ccr.dll/lifted-0005.c:sub_27010890. */
     88     uint32_t module = MEM32(0x27048b7c);
     89     uint32_t head = module && MEM8(module + 4) ? MEM32(MEM32(module) + 4) : 0;
     90     unsigned count = 0;
     91     for (uint32_t node = head ? MEM32(head) : 0; node && node != head; node = MEM32(node))
     92         if (MEM8(MEM32(node + 8) + 8)) ++count;
     93 
     94     int regions[3 + count * 5];
     95     regions[0] = MEM32(0x27048be8);
     96     regions[1] = MEM32(0x27048bec);
     97     regions[2] = MEM32(0x27048b98) != 0;
     98     uint32_t drag = MEM32(0x27048b6c);
     99     int dragging = drag && MEM32(drag + 0x20);
    100     uint32_t action = MEM32(0x2b0ee640);
    101     uint32_t world = action ? MEM32(action + 0x10) : 0;
    102     uint32_t chat = MEM32(0x2b0ee6a4);
    103     uint32_t history = chat ? MEM32(MEM32(chat + 0xc) + 0x18) : 0;
    104     uint32_t inventory = MEM32(0x2b0eea14);
    105     unsigned offset = 3;
    106     for (uint32_t node = head ? MEM32(head) : 0; node && node != head; node = MEM32(node)) {
    107         uint32_t hotspot = MEM32(node + 8);
    108         if (!MEM8(hotspot + 8)) continue;
    109         for (unsigned i = 0; i < 4; ++i) regions[offset++] = MEM32(hotspot + 0x2c + i * 4);
    110         /* CCR DragObject_t's class, identity and dummy flag distinguish carried
    111            inventory items from buttons, empty cells and placement destinations. */
    112         int item = !dragging && MEM32(hotspot) == 0x27034278 && !MEM8(hotspot + 0x70) &&
    113             MEM32(hotspot + 0x54) == 104 && MEM32(hotspot + 0x58) >= 48 && MEM32(hotspot + 0x58) < 78;
    114         /* InventoryView_t::sub_2B03E040 binds its pass-through mouse detector at +0x1c. */
    115         regions[offset++] = hotspot == world ? 1 : hotspot == history ? 2 : item ? 3 :
    116             inventory && hotspot == MEM32(inventory + 0x1c) ? 4 : 0;
    117     }
    118 
    119     /* Publish changed hit regions and menu state; copy synchronously to keep the snapshot stable. */
    120     static int *previous;
    121     static size_t previous_size;
    122     if (previous_size == sizeof(regions) && !memcmp(previous, regions, sizeof(regions))) return;
    123     previous = realloc(previous, sizeof(regions));
    124     if (!previous) abort();
    125     previous_size = sizeof(regions);
    126     memcpy(previous, regions, sizeof(regions));
    127     ao_touch_regions(previous + 3, count, regions[0], regions[1], regions[2]);
    128 }
    129 
    130 static void publish_chat_bounds(void)
    131 {
    132     uint32_t chat = MEM32(0x2b0ee6a4);
    133     if (!chat) return;
    134     /* Input window layout: client/game/ccr.dll/lifted-0007.c:sub_270156A0. */
    135     uint32_t window = MEM32(MEM32(chat + 8));
    136     uint32_t output = MEM32(MEM32(chat + 0xc) + 0x14);
    137     int bounds[] = {MEM32(window + 0x2c) + 1, MEM32(window + 0x30) + 1,
    138                     MEM32(window + 0x34) - 2, MEM32(window + 0x38) - 2,
    139                     MEM32(0x27048be8), MEM32(0x27048bec),
    140                     MEM32(output + 0x2c), MEM32(output + 0x30),
    141                     MEM32(output + 0x34), MEM32(output + 0x38)};
    142     static int previous[10];
    143     if (!memcmp(previous, bounds, sizeof(bounds))) return;
    144     memcpy(previous, bounds, sizeof(bounds));
    145     ao_chat_bounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5],
    146                    bounds[6], bounds[7], bounds[8], bounds[9]);
    147 }
    148 
    149 /* The browser main thread only publishes a request; the game owns all objects. */
    150 EMSCRIPTEN_KEEPALIVE void browser_request_resize(unsigned width, unsigned height)
    151 {
    152     if (!width || !height || width > 65535 || height > 65535) return;
    153     atomic_store(&requested_size, width | (height << 16));
    154 }
    155 
    156 EMSCRIPTEN_KEEPALIVE void browser_creation_finish(const char *name)
    157 {
    158     char *copy = strdup(name);
    159     if (!copy) abort();
    160     free(atomic_exchange(&requested_creation_name, copy));
    161 }
    162 
    163 EMSCRIPTEN_KEEPALIVE void browser_chat_viewport(unsigned bottom)
    164 {
    165     atomic_store(&chat_viewport_bottom, bottom);
    166 }
    167 
    168 EMSCRIPTEN_KEEPALIVE void browser_hide_cursor(int hidden)
    169 {
    170     atomic_store(&cursor_hidden, hidden);
    171 }
    172 
    173 int browser_cursor_hidden(void)
    174 {
    175     return atomic_load(&cursor_hidden);
    176 }
    177 
    178 EMSCRIPTEN_KEEPALIVE void browser_hud_insets(unsigned right, unsigned top)
    179 {
    180     atomic_store(&hud_insets, right | (top << 16));
    181 }
    182 
    183 EMSCRIPTEN_KEEPALIVE void browser_chat_scroll(int pixels)
    184 {
    185     if (atomic_load(&hud_insets)) atomic_fetch_add(&chat_scroll_pixels, pixels);
    186 }
    187 
    188 EMSCRIPTEN_KEEPALIVE void browser_orbit(int x, int y, int active)
    189 {
    190     if (active) {
    191         atomic_fetch_add(&orbit_x, x);
    192         atomic_fetch_add(&orbit_y, y);
    193     } else {
    194         atomic_store(&orbit_x, 0);
    195         atomic_store(&orbit_y, 0);
    196         atomic_store(&orbit_zoom, 0);
    197         atomic_store(&wheel_x, 0);
    198         atomic_store(&wheel_y, 0);
    199     }
    200     atomic_store(&orbit_active, active);
    201 }
    202 
    203 EMSCRIPTEN_KEEPALIVE void browser_camera_wheel(int x, int y)
    204 {
    205     atomic_fetch_add(&wheel_x, x);
    206     atomic_fetch_add(&wheel_y, y);
    207 }
    208 
    209 EMSCRIPTEN_KEEPALIVE void browser_zoom(int amount)
    210 {
    211     atomic_fetch_add(&orbit_zoom, amount);
    212 }
    213 
    214 static void call_method(uint32_t address, uint32_t object, unsigned count,
    215                         const uint32_t *arguments)
    216 {
    217     uint32_t stack = g_esp;
    218     for (unsigned i = count; i; --i) PUSH32(g_esp, arguments[i - 1]);
    219     PUSH32(g_esp, RECOMP_RETADDR);
    220     g_ecx = object;
    221     recomp_lookup(address)();
    222     assert(g_esp == stack);
    223 }
    224 
    225 static void finish_creation(void)
    226 {
    227     if (!atomic_load(&requested_creation_name)) return;
    228     if (!MEM32(0x2b0ee7e0)) {
    229         free(atomic_exchange(&requested_creation_name, NULL));
    230         return;
    231     }
    232     uint32_t scene = MEM32(0x2b0ee7d0);
    233     if (scene != MEM32(0x2b0ee7e0) || MEM32(0x2b0e42a8) || MEM32(scene + 0xa4)) return;
    234     uint32_t input = MEM32(scene + 0x88);
    235     if (!input || !MEM8(MEM32(input + 4) + 8)) return;
    236 
    237     /* Consume once before the original Finish path, including validation errors.
    238      * SetString keeps the same editor limits and callbacks as random-name replies. */
    239     char *name = atomic_exchange(&requested_creation_name, NULL);
    240     unsigned size = (strlen(name) + 4) & ~3u;
    241     g_esp -= size;
    242     strcpy((char *)ADDR(g_esp), name);
    243     call_method(0x2701f310, MEM32(input + 8), 1, (uint32_t[]){g_esp});
    244     g_esp += size;
    245     call_method(0x2b033cb0, scene, 0, NULL);
    246     free(name);
    247 }
    248 
    249 static void orbit_frame(void)
    250 {
    251     /* Consume each wheel once, even when several events arrive between frames. */
    252     int x = atomic_exchange(&wheel_x, 0), y = atomic_exchange(&wheel_y, 0);
    253     int zoom = atomic_exchange(&orbit_zoom, 0);
    254     int active = atomic_load(&orbit_active) || x || y || zoom;
    255     if (!active && !orbit_running) return;
    256     x += atomic_exchange(&orbit_x, 0);
    257     y += atomic_exchange(&orbit_y, 0);
    258     uint32_t engine = MEM32(0x2d01752c);
    259     if (!engine) { orbit_running = 0; return; }
    260 
    261     /* CCR sub_270041F0 uses half a screen fraction for native camera movement. */
    262     if (active) {
    263         float movement[] = {x / 131072.0f, y / 131072.0f};
    264         uint32_t arguments[2];
    265         memcpy(arguments, movement, sizeof(arguments));
    266         call_method(0x36020440, engine, 2, arguments);
    267 
    268         /* Scale the current camera offset without rebuilding its orbit direction.
    269          * client/game/llr.dll/lifted-0000.c:sub_38002720 limits zoom to 0.7..150. */
    270         uint32_t camera = MEM32(engine + 0x80);
    271         if (zoom && camera && MEM32(camera + 0x19c)) {
    272             uint32_t vehicle = MEM32(camera + 0x60);
    273             g_esp -= 12;
    274             call_method(0x38001f50, vehicle, 1, (uint32_t[]){g_esp});
    275             float offset[3], squared_distance = 0;
    276             for (unsigned i = 0; i < 3; ++i) {
    277                 offset[i] = *(float *)ADDR(vehicle + 0x54 + i * 4) - *(float *)ADDR(g_esp + i * 4);
    278                 squared_distance += offset[i] * offset[i];
    279             }
    280             float distance = sqrtf(squared_distance);
    281             if (distance > 0) {
    282                 float scale = fminf(150, fmaxf(0.7f, distance * expf(zoom / 65536.0f))) / distance;
    283                 for (unsigned i = 0; i < 3; ++i)
    284                     *(float *)ADDR(g_esp + i * 4) += offset[i] * scale;
    285                 call_method(MEM32(0x3804e6f4), vehicle, 1, (uint32_t[]){g_esp});
    286                 call_method(MEM32(MEM32(vehicle) + 0x88), vehicle, 1, (uint32_t[]){1});
    287             }
    288             g_esp += 12;
    289         }
    290     } else {
    291         call_method(0x36020470, engine, 0, NULL);
    292     }
    293     orbit_running = active;
    294 }
    295 
    296 static void move_point(uint32_t method, uint32_t object, int x, int y)
    297 {
    298     g_esp -= 8;
    299     MEM32(g_esp) = x;
    300     MEM32(g_esp + 4) = y;
    301     call_method(method, object, 1, (uint32_t[]){g_esp});
    302     g_esp += 8;
    303 }
    304 
    305 static void scroll_mobile_chat(void)
    306 {
    307     int pixels = atomic_exchange(&chat_scroll_pixels, 0);
    308     uint32_t chat = MEM32(0x2b0ee6a4);
    309     if (!pixels || !chat || !atomic_load(&hud_insets)) return;
    310     uint32_t output = MEM32(chat + 0xc);
    311     int line_height = MEM32(output + 0x38);
    312     chat_scroll_remainder += pixels;
    313     int lines = chat_scroll_remainder / line_height;
    314     chat_scroll_remainder %= line_height;
    315     /* Native history scrolling also updates its scrollbar and follow-newest state. */
    316     if (lines) call_method(0x2b005220, output, 1, (uint32_t[]){-lines});
    317 }
    318 
    319 static void resize_chat_background(uint32_t output)
    320 {
    321     /* The history background is a scaled 16x16 sprite: ddr.dll:sub_2B003ED0. */
    322     g_esp -= 8;
    323     *(float *)ADDR(g_esp) = MEM32(output + 0x48) / 16.0f;
    324     *(float *)ADDR(g_esp + 4) = MEM32(output + 0x4c) / 16.0f;
    325     call_method(MEM32(0x2b0b5658), MEM32(output + 0x20), 2, (uint32_t[]){g_esp, g_esp + 4});
    326     g_esp += 8;
    327 }
    328 
    329 static void update_chat_toolbar(uint32_t output_window, unsigned bottom)
    330 {
    331     /* ToolbarSystem_t: client/game/ddr.dll/lifted-0031.c:sub_2B0775B0. */
    332     uint32_t toolbar = MEM32(0x2b0ef034);
    333     if (!toolbar) { mobile_chat.hidden_toolbar = 0; return; }
    334     int hide = 0;
    335     if (bottom) {
    336         /* The strip's hotspot covers its full area; its render window is one tile. */
    337         uint32_t hotspot = MEM32(toolbar + 0x5c);
    338         int x = MEM32(hotspot + 0x2c), y = MEM32(hotspot + 0x30);
    339         int width = (int)MEM32(hotspot + 0x34) - x;
    340         int height = (int)MEM32(hotspot + 0x38) - y;
    341         int chat_x = MEM32(output_window + 0x2c), chat_y = MEM32(output_window + 0x30);
    342         int overlap_width = fminf(x + width, chat_x + MEM32(output_window + 0x34)) - fmaxf(x, chat_x);
    343         int overlap_height = fminf(y + height, chat_y + MEM32(output_window + 0x38)) - fmaxf(y, chat_y);
    344         hide = overlap_width > 0 && overlap_height > 0 &&
    345                (int64_t)overlap_width * overlap_height * 5 > (int64_t)width * height;
    346     }
    347 
    348     /* Restore only a bar that this chat session hid. */
    349     if (hide && !MEM8(toolbar + 4)) {
    350         call_method(0x2b0775b0, toolbar, 1, (uint32_t[]){1});
    351         mobile_chat.hidden_toolbar = toolbar;
    352     } else if (!hide && mobile_chat.hidden_toolbar == toolbar) {
    353         call_method(0x2b0775b0, toolbar, 1, (uint32_t[]){0});
    354         mobile_chat.hidden_toolbar = 0;
    355     }
    356 }
    357 
    358 static void position_mobile_chat(void)
    359 {
    360     uint32_t chat = MEM32(0x2b0ee6a4);
    361     if (!chat) {
    362         update_chat_toolbar(0, 0);
    363         mobile_chat.chat = 0;
    364         return;
    365     }
    366     unsigned bottom = atomic_load(&chat_viewport_bottom);
    367     uint32_t input = MEM32(chat + 8), output = MEM32(chat + 0xc);
    368     uint32_t input_window = MEM32(input), output_window = MEM32(output + 0x14);
    369     if (bottom && mobile_chat.chat != chat) {
    370         mobile_chat.chat = chat;
    371         mobile_chat.input_x = MEM32(input_window + 0x2c);
    372         mobile_chat.input_y = MEM32(input_window + 0x30);
    373         mobile_chat.output_x = MEM32(output_window + 0x2c);
    374         mobile_chat.output_y = MEM32(output_window + 0x30);
    375         mobile_chat.output_height = MEM32(output_window + 0x38);
    376         mobile_chat.output_lines = MEM32(output + 0x3c);
    377     }
    378     if (mobile_chat.chat != chat) return;
    379 
    380     /* Keep chat clear of the left health/nano bars while the editor is open. */
    381     int margin = (atomic_load(&hud_insets) & 65535) / 2;
    382     if (margin < 20) margin = 20;
    383     int input_x = bottom && mobile_chat.input_x < margin ? margin : mobile_chat.input_x;
    384     int output_x = bottom && mobile_chat.output_x < margin ? margin : mobile_chat.output_x;
    385 
    386     /* Reserve the input line first, then fit whole history lines above it. */
    387     int input_y = bottom ? (int)bottom - (int)MEM32(input_window + 0x38) : mobile_chat.input_y;
    388     unsigned lines = mobile_chat.output_lines;
    389     if (bottom && input_y < mobile_chat.output_height) {
    390         lines = input_y > 0 ? input_y / MEM32(output + 0x38) : 0;
    391         if (!lines) lines = 1;
    392     }
    393     int output_height = lines == mobile_chat.output_lines ? mobile_chat.output_height :
    394                         lines * MEM32(output + 0x38);
    395     int changed = 0;
    396     if ((int)MEM32(output_window + 0x38) != output_height) {
    397         /* DDR history layout: lifted-0000.c:sub_2B003ED0 and sub_2B004F50. */
    398         MEM32(output + 0x3c) = lines;
    399         MEM32(output + 0x4c) = output_height;
    400         if (MEM8(output + 0x60))
    401             MEM32(output + 0x58) = MEM32(output + 0x2c) > lines ? MEM32(output + 0x2c) - lines : 0;
    402         MEM32(output + 0x54) = MEM32(output + 0x58) + lines;
    403         MEM8(output + 9) = 1;
    404 
    405         /* Shrink the existing tile grids so restoring them needs no allocation. */
    406         move_point(0x27018720, output_window, MEM32(output_window + 0x34), output_height);
    407         uint32_t scrollbar = MEM32(output + 0x6c);
    408         move_point(0x27018720, scrollbar, MEM32(scrollbar + 0x34), output_height);
    409         uint32_t detector = MEM32(output + 0x18);
    410         move_point(MEM32(MEM32(detector) + 0x18), detector,
    411                    MEM32(output_window + 0x34), output_height);
    412         resize_chat_background(output);
    413         changed = 1;
    414     }
    415     int output_y = bottom ? input_y - (int)MEM32(output_window + 0x38) : mobile_chat.output_y;
    416     if ((int)MEM32(input_window + 0x2c) != input_x || (int)MEM32(input_window + 0x30) != input_y) {
    417         move_point(0x27015910, input, input_x, input_y);
    418         changed = 1;
    419     }
    420     if ((int)MEM32(output_window + 0x2c) != output_x || (int)MEM32(output_window + 0x30) != output_y) {
    421         move_point(0x2b004560, output, output_x, output_y);
    422         changed = 1;
    423     }
    424     update_chat_toolbar(output_window, bottom);
    425     /* WidgetModule_t::InvalidateAll redraws UI uncovered by relocated chat. */
    426     if (changed && MEM32(0x27048be4)) call_method(0x2701d270, 0, 0, NULL);
    427     if (!bottom) mobile_chat.chat = 0;
    428 }
    429 
    430 /* Layout from client/game/ddr.dll/lifted-0033.c:sub_2B081C40 (status bar). */
    431 struct status_bar {
    432     uint32_t detector, options, help, inventory, skills, map, chat, menu;
    433     int height;
    434     uint32_t uplink, uplink_text, uplink_value, transfer, transfer_text, transfer_detector;
    435     uint32_t money_text, money, money_detector, load, load_fill;
    436     uint32_t load_state[3], camera[4], camera_detector, camera_mode;
    437     uint32_t distance, distance_fill, distance_detector, distance_value, background;
    438 };
    439 
    440 static void resize_status_bar(unsigned width, unsigned height)
    441 {
    442     uint32_t address = MEM32(0x2b0ef09c);
    443     if (!address) return;
    444     struct status_bar *bar = (void *)ADDR(address);
    445     int x = (int)width - (int)(atomic_load(&hud_insets) & 65535);
    446     int y = (int)height - bar->height;
    447     uint32_t buttons[] = {bar->help, bar->options};
    448     for (unsigned i = 0; i < 2; ++i) {
    449         x -= MEM32(buttons[i] + 0x44);
    450         call_method(0x2700b2d0, buttons[i], 2, (uint32_t[]){x, y});
    451     }
    452     x -= MEM32(MEM32(bar->uplink + 0x14) + 0xc);
    453     move_point(0x2701cab0, bar->uplink, x, y);
    454     call_method(0x27020dd0, bar->uplink_text, 2, (uint32_t[]){x + 24, y + 10});
    455     x -= MEM32(MEM32(bar->transfer + 0x14) + 0xc);
    456     move_point(0x2701cab0, bar->transfer, x, y);
    457     move_point(MEM32(MEM32(bar->transfer_detector) + 0x14), bar->transfer_detector, x, y);
    458     call_method(0x27020dd0, bar->transfer_text, 2, (uint32_t[]){x + 130, y + 6});
    459 
    460     uint32_t menu_buttons[] = {bar->menu, bar->chat, bar->inventory, bar->skills, bar->map};
    461     for (unsigned i = 0; i < 5; ++i) {
    462         x -= MEM32(menu_buttons[i] + 0x44);
    463         call_method(0x2700b2d0, menu_buttons[i], 2, (uint32_t[]){x, y});
    464     }
    465     x -= MEM32(MEM32(bar->money + 0x14) + 0xc);
    466     move_point(0x2701cab0, bar->money, x, y);
    467     move_point(MEM32(MEM32(bar->money_detector) + 0x14), bar->money_detector, x, y);
    468     move_point(0x2b076030, bar->money_text, x + 3, y + 3);
    469     x -= 8 + MEM32(MEM32(bar->load + 0x14) + 0xc);
    470     move_point(0x2701cab0, bar->load, x, y);
    471     move_point(0x250ae720, bar->load_fill, x + 4, y + 3);
    472     x -= MEM32(MEM32(bar->camera[0] + 0x14) + 0xc);
    473     for (unsigned i = 0; i < 4; ++i) move_point(0x2701cab0, bar->camera[i], x, y);
    474     move_point(MEM32(MEM32(bar->camera_detector) + 0x14), bar->camera_detector, x, y);
    475     x -= MEM32(MEM32(bar->distance + 0x14) + 0xc);
    476     move_point(0x2701cab0, bar->distance, x, y);
    477     move_point(0x250ae720, bar->distance_fill, x + 4, y + 3);
    478     move_point(MEM32(MEM32(bar->distance_detector) + 0x14), bar->distance_detector, x, y);
    479 
    480     /* Rebuild the tiled strip so growth allocates tiles and shrink releases them. */
    481     move_point(MEM32(MEM32(bar->detector) + 0x14), bar->detector, 0, y);
    482     move_point(MEM32(MEM32(bar->detector) + 0x18), bar->detector, width, bar->height);
    483     unsigned minimum = MEM32(MEM32(MEM32(bar->background) + 0x14) + 0xc) +
    484                        MEM32(MEM32(MEM32(bar->background + 4) + 0x14) + 0xc) + 1;
    485     call_method(0x27026160, bar->background, 0, NULL);
    486     call_method(0x27025ec0, bar->background, 8,
    487                 (uint32_t[]){0, y, width > minimum ? width : minimum, 1, 0x62, 0x62, 0x62, 10000});
    488 }
    489 
    490 static void position_mobile_hud(void)
    491 {
    492     uint32_t insets = atomic_load(&hud_insets);
    493     if (!insets && !hud_adjusted) return;
    494     unsigned right = insets & 65535, top = insets >> 16;
    495     int changed = 0;
    496 
    497     /* Keep chat text clear of rounded left corners, including after typing. */
    498     unsigned chat_margin = right / 2;
    499     uint32_t chat = MEM32(0x2b0ee6a4);
    500     if (chat_margin && chat) {
    501         uint32_t input = MEM32(chat + 8), output = MEM32(chat + 0xc);
    502         uint32_t input_window = MEM32(input), output_window = MEM32(output + 0x14);
    503         if ((int)MEM32(input_window + 0x2c) < (int)chat_margin) {
    504             move_point(0x27015910, input, chat_margin, MEM32(input_window + 0x30));
    505             changed = 1;
    506         }
    507         if ((int)MEM32(output_window + 0x2c) < (int)chat_margin) {
    508             move_point(0x2b004560, output, chat_margin, MEM32(output_window + 0x30));
    509             changed = 1;
    510         }
    511 
    512         /* Retain the native maximum and reserve space for the scrollbar and right edge. */
    513         if (chat_width_owner != chat) {
    514             chat_width_owner = chat;
    515             chat_max_width = MEM32(output_window + 0x34);
    516         }
    517         uint32_t scrollbar = MEM32(output + 0x6c);
    518         int available = (int)MEM32(0x27048be8) - (int)MEM32(output_window + 0x2c) -
    519                         (int)chat_margin - (int)MEM32(scrollbar + 0x34);
    520         unsigned width = available > 0 ? available : 1;
    521         if (width > chat_max_width) width = chat_max_width;
    522         if (MEM32(output_window + 0x34) != width) {
    523             MEM32(output + 0x48) = MEM32(output + 0x50) = width;
    524             MEM32(output + 0x54) = MEM32(output + 0x58) + MEM32(output + 0x3c);
    525             MEM8(output + 9) = 1;
    526             move_point(0x27018720, output_window, width, MEM32(output_window + 0x38));
    527             uint32_t detector = MEM32(output + 0x18);
    528             move_point(MEM32(MEM32(detector) + 0x18), detector,
    529                        width, MEM32(output_window + 0x38));
    530             resize_chat_background(output);
    531             move_point(0x2b004560, output, MEM32(output_window + 0x2c), MEM32(output_window + 0x30));
    532             changed = 1;
    533         }
    534     }
    535 
    536     /* Reposition native buttons with their hotspots, retaining the full strip background. */
    537     uint32_t address = MEM32(0x2b0ef09c);
    538     if (address) {
    539         struct status_bar *bar = (void *)ADDR(address);
    540         unsigned width = MEM32(0x27048be8), height = MEM32(0x27048bec);
    541         int x = (int)width - (int)right - (int)MEM32(bar->help + 0x44);
    542         if ((int)MEM32(bar->help + 0x3c) != x ||
    543             (int)MEM32(bar->help + 0x40) != (int)height - bar->height) {
    544             resize_status_bar(width, height);
    545             changed = 1;
    546         }
    547     }
    548 
    549     /* StatView_t's health/nano sprites: ddr.dll/lifted-0016.c:sub_2B0403D0. */
    550     uint32_t stats = MEM32(0x2b0eea5c);
    551     if (stats) {
    552         for (unsigned i = 0; i < 2; ++i) {
    553             uint32_t sprite = MEM32(stats + 0xdc + i * 4);
    554             uint32_t hotspot = MEM32(stats + (i ? 0xe4 : 0xe8));
    555             int y = top + i * 144;
    556             if (*(float *)ADDR(sprite + 0x1c) == y) continue;
    557             move_point(0x250ae720, sprite, *(float *)ADDR(sprite + 0x18), y);
    558             move_point(MEM32(MEM32(hotspot) + 0x14), hotspot, MEM32(hotspot + 0x2c), y);
    559             changed = 1;
    560         }
    561     }
    562     if (changed && MEM32(0x27048be4)) call_method(0x2701d270, 0, 0, NULL);
    563     hud_adjusted = !!insets;
    564 }
    565 
    566 static void resize_side_panel(unsigned width, unsigned height)
    567 {
    568     uint32_t panel = MEM32(0x2b0ee9b8);
    569     if (!panel) return;
    570     unsigned available = height > 20 ? height - 20 : 1;
    571     uint32_t window = MEM32(0x2b0ee9dc);
    572     int active = MEM8(window + 0x24);
    573     MEM32(0x2b0ee9d4) = available;
    574 
    575     /* CCR RenderWindow_t::Resize cannot grow its tile grid; rebuild its area. */
    576     if (MEM32(window + 0xc) != (available + 63) / 64) {
    577         /* TextOutput_t retains this SpriteInfo_t, so keep its address stable. */
    578         uint32_t image = MEM32(window + 0x28);
    579         MEM32(window + 0x28) = 0;
    580         g_esp -= 8;
    581         MEM32(g_esp) = 192;
    582         MEM32(g_esp + 4) = available;
    583         call_method(0x270181e0, window, 3, (uint32_t[]){6, 6, g_esp});
    584         g_esp += 8;
    585         uint32_t replacement = MEM32(window + 0x28);
    586         guest_free(MEM32(image));
    587         memcpy((void *)ADDR(image), (void *)ADDR(replacement), 0x34);
    588         guest_free(replacement);
    589         MEM32(window + 0x28) = image;
    590         call_method(0x250af340, image, 3, (uint32_t[]){0, 0, 1});
    591     }
    592     move_point(0x27018620, window, (int)width - 192, 0);
    593     call_method(0x2b03b110, panel, 0, NULL);
    594     if (!active) call_method(0x27018890, window, 0, NULL);
    595     move_point(0x27018620, MEM32(MEM32(panel + 0x70)), (int)width - 192, 0);
    596 
    597     uint32_t border = MEM32(panel + 0x54);
    598     unsigned minimum = MEM32(MEM32(MEM32(border) + 0x14) + 8) +
    599                        MEM32(MEM32(MEM32(border + 4) + 0x14) + 8) + 1;
    600     call_method(0x27026160, border, 0, NULL);
    601     call_method(0x27025ec0, border, 8,
    602                 (uint32_t[]){width - 197, 0, available > minimum ? available : minimum,
    603                              0, 0x16, 0x16, 0x16, 4000});
    604     if (MEM8(panel + 0x6c)) call_method(0x270262c0, border, 0, NULL);
    605 }
    606 
    607 static void resize_windows(unsigned width, int height_change)
    608 {
    609     /* DDR inventory, floating views, shortcut bar, planet map, and chat bounds. */
    610     const uint32_t guards[] = {0x2b0eea0c, 0x2b0eea04, 0x2b0eea6c, 0x2b0eee7c,
    611                                0x2b0ef028, 0x2b0ef054, 0x2b0ef048, 0x2b0ef0c8};
    612     for (unsigned i = 0; i < sizeof(guards) / sizeof(guards[0]); ++i) MEM32(guards[i]) = 0;
    613 
    614     /* Preserve user positions, clamping through each window's own layout method. */
    615     uint32_t inventory = MEM32(0x2b0eea14);
    616     if (inventory) {
    617         uint32_t image = MEM32(MEM32(inventory + 0x14) + 0x14);
    618         int right = (int)width - (int)MEM32(image + 0xc) - 192;
    619         if (right < 0) right = 0;
    620         if (MEM32(inventory + 0x20) == MEM32(inventory + 0x48)) MEM32(inventory + 0x20) = right;
    621         MEM32(inventory + 0x48) = right;
    622         call_method(0x2b03e740, inventory, 1, (uint32_t[]){inventory + 0x20});
    623     }
    624     uint32_t map = MEM32(0x2b0ef094);
    625     if (map) call_method(0x2b07d180, map, 2, (uint32_t[]){MEM32(map), MEM32(map + 4)});
    626 
    627     /* Chat output and its input line retain their distance from the bottom edge. */
    628     uint32_t chat = MEM32(0x2b0ee6a4);
    629     if (chat) {
    630         if (mobile_chat.chat == chat) {
    631             mobile_chat.input_y += height_change;
    632             mobile_chat.output_y += height_change;
    633         }
    634         uint32_t output = MEM32(chat + 0xc), window = MEM32(output + 0x14);
    635         int y = (int)MEM32(window + 0x30) + height_change;
    636         move_point(0x2b004560, output, MEM32(window + 0x2c), y > 0 ? y : 0);
    637         uint32_t input = MEM32(chat + 8);
    638         window = MEM32(input);
    639         y = (int)MEM32(window + 0x30) + height_change;
    640         move_point(0x27015910, input, MEM32(window + 0x2c), y > 0 ? y : 0);
    641     }
    642 }
    643 
    644 static void resize_frame(void)
    645 {
    646     uint32_t size = atomic_load(&requested_size);
    647     if (!size) return;
    648     uint32_t display = MEM32(0x25238568), action = MEM32(0x2b0ee640);
    649     if (!display || !MEM32(display + 0x38) || !MEM32(display + 0x34)) return;
    650     unsigned width = size & 65535, height = size >> 16;
    651     uint32_t info = MEM32(display + 8);
    652     int height_change = (int)height - (int)MEM32(info + 4);
    653     if (MEM32(info) == width && MEM32(info + 4) == height) {
    654         atomic_compare_exchange_strong(&requested_size, &size, 0);
    655         return;
    656     }
    657     if (!linux_graphics_resize(width, height, MEM32(MEM32(display + 0xd4)))) return;
    658     atomic_compare_exchange_strong(&requested_size, &size, 0);
    659 
    660     /* BBR DisplayInfo and Randy_t::ResizeBuffers own the full back/depth buffers. */
    661     MEM32(info) = width;
    662     MEM32(info + 4) = height;
    663     call_method(0x250d6010, MEM32(display + 0x34), 2, (uint32_t[]){width, height});
    664     uint32_t drawing_area = MEM32(display + 0x20);
    665     MEM32(drawing_area + 0xc) = height;
    666     MEM32(drawing_area + 0x10) = width;
    667     /* Discard old dirty rectangles before any blit can reach the smaller surfaces. */
    668     for (unsigned i = 0; i < MEM32(drawing_area + 4); ++i) {
    669         uint32_t rectangles = MEM32(MEM32(drawing_area + 0x24) + i * 24);
    670         MEM32(rectangles + 8) = MEM32(rectangles + 4);
    671     }
    672 
    673     /* CCR caches the screen dimensions for sprite clipping and relative layout. */
    674     MEM32(0x27048be8) = width;
    675     MEM32(0x27048bec) = height;
    676     MEM32(0x27048bc4) = 0;
    677     MEM32(0x27048bd0) = 0;
    678     if (action) {
    679         /* DDR ActionView_t owns the side-panel and bottom-strip camera insets. */
    680         MEM32(action + 0x18) = width;
    681         MEM32(action + 0x1c) = height;
    682         unsigned view_width = MEM8(0x2b0ee645) ? width : (width > 197 ? width - 197 : 1);
    683         unsigned view_height = height > 20 ? height - 20 : 1;
    684         MEM32(action) = MEM32(action + 4) = 0;
    685         MEM32(action + 8) = view_width;
    686         MEM32(action + 12) = view_height;
    687         call_method(0x250a5f40, display, 2, (uint32_t[]){view_width, view_height});
    688         call_method(0x250a5ff0, display, 2, (uint32_t[]){0, 0});
    689 
    690         /* Move the world mouse detector with the viewport, then notify its listeners. */
    691         uint32_t detector = MEM32(action + 0x10), methods = MEM32(detector);
    692         call_method(MEM32(methods + 0x14), detector, 1, (uint32_t[]){action});
    693         call_method(MEM32(methods + 0x18), detector, 1, (uint32_t[]){action + 8});
    694         call_method(0x20003630, MEM32(0x20015cd4), 2, (uint32_t[]){0x12, 0x6e});
    695     } else {
    696         call_method(0x250a5f40, display, 2, (uint32_t[]){width, height});
    697     }
    698     /* Keep the loading logo centered and undistorted across viewport changes. */
    699     if (MEM32(0x2b0ee968)) {
    700         g_esp -= 4;
    701         *(float *)ADDR(g_esp) = fminf(width / 1024.0f, height / 768.0f);
    702         call_method(0x27018ac0, MEM32(0x2b0ee968), 2, (uint32_t[]){g_esp, g_esp});
    703         g_esp += 4;
    704         call_method(0x27018b20, MEM32(0x2b0ee968), 2, (uint32_t[]){0x3f000000, 0x3f000000});
    705 
    706         /* Stretch the black 16x16 backdrop and reposition the loading caption. */
    707         g_esp -= 8;
    708         *(float *)ADDR(g_esp) = width / 16.0f;
    709         *(float *)ADDR(g_esp + 4) = height / 16.0f;
    710         call_method(MEM32(0x27029218), MEM32(0x2b0ee958), 2, (uint32_t[]){g_esp, g_esp + 4});
    711         g_esp += 8;
    712         move_point(0x250ae720, MEM32(0x2b0ee958), 0, 0);
    713         if (MEM32(0x2b0ee960))
    714             move_point(0x27018620, MEM32(0x2b0ee960),
    715                        ((int)width - (int)MEM32(MEM32(0x2b0ee960) + 0x34)) / 2, (int)height - 50);
    716     }
    717     resize_status_bar(width, height);
    718     resize_side_panel(width, height);
    719     resize_windows(width, height_change);
    720     if (MEM32(0x27048be4)) call_method(0x2701d270, 0, 0, NULL);
    721 
    722     fprintf(stderr, "[browser-resolution] %ux%u\n", width, height);
    723 }
    724 
    725 /* Called by Clientr.exe::sub_28002D20 before AFCM starts the next frame. */
    726 void browser_resize_frame(void)
    727 {
    728     if (!atomic_load(&requested_size) && !atomic_load(&chat_viewport_bottom) && !mobile_chat.chat &&
    729         !atomic_load(&orbit_active) && !orbit_running && !atomic_load(&wheel_x) &&
    730         !atomic_load(&wheel_y) && !atomic_load(&orbit_zoom) && !atomic_load(&hud_insets) && !hud_adjusted &&
    731         !atomic_load(&requested_creation_name)) {
    732         publish_chat_bounds();
    733         publish_touch_regions();
    734         publish_creation_state();
    735         return;
    736     }
    737 
    738     /* Preserve the interrupted guest CPU, including x87 values and flags. */
    739     uint32_t registers[] = {g_eax, g_ecx, g_edx, g_ebx, g_esi, g_edi, g_ebp,
    740                             g_cur_func, g_flag_k, g_flag_a, g_flag_b, g_flag_cf};
    741     double fpu[8];
    742     int64_t integers[8];
    743     memcpy(fpu, g_st, sizeof(fpu));
    744     memcpy(integers, g_st_i64, sizeof(integers));
    745     int top = g_fp_top;
    746     uint16_t control = g_fpu_cw;
    747 
    748     resize_frame();
    749     finish_creation();
    750     orbit_frame();
    751     position_mobile_chat();
    752     position_mobile_hud();
    753     scroll_mobile_chat();
    754     publish_chat_bounds();
    755     publish_touch_regions();
    756     publish_creation_state();
    757 
    758     g_eax = registers[0]; g_ecx = registers[1]; g_edx = registers[2];
    759     g_ebx = registers[3]; g_esi = registers[4]; g_edi = registers[5];
    760     g_ebp = registers[6]; g_cur_func = registers[7]; g_flag_k = registers[8];
    761     g_flag_a = registers[9]; g_flag_b = registers[10]; g_flag_cf = registers[11];
    762     memcpy(g_st, fpu, sizeof(fpu));
    763     memcpy(g_st_i64, integers, sizeof(integers));
    764     g_fp_top = top;
    765     g_fpu_cw = control;
    766 }