aorecomp

Anarchy Online classic in the browser

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


client/tests/terrain_tile.c (8496B)


      1 #include "recomp_types.h"
      2 #include <assert.h>
      3 
      4 void sub_3803C220(void);
      5 void sub_3803C6B0(void);
      6 void sub_3B001000(void);
      7 void sub_3B001010(void);
      8 void sub_3B0010B0(void);
      9 void sub_3B0010E0(void);
     10 void sub_3B001110(void);
     11 void sub_3B001170(void);
     12 void sub_3B0011B0(void);
     13 void sub_3B0011F0(void);
     14 void sub_3B001240(void);
     15 void sub_3B001500(void);
     16 void sub_3B0015A0(void);
     17 void sub_3B001610(void);
     18 void sub_3B003730(void);
     19 static float corners[12];
     20 static unsigned parity, registrations, corner_calls;
     21 
     22 static void corner_points(void)
     23 {
     24     ++corner_calls;
     25     for (unsigned i = 0; i < 4; ++i)
     26         memcpy((void *)ADDR(MEM32(g_esp + 12 + i * 4)), corners + i * 3, 12);
     27     g_eax = parity;
     28     g_esp += 28;
     29 }
     30 static void register_exit(void) { ++registrations; g_eax = 0; g_esp += 4; }
     31 recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; }
     32 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); }
     33 recomp_func_t recomp_lookup(uint32_t address)
     34 {
     35     switch (address) {
     36     case 0x38038370: return corner_points;
     37     case 0x3804b54c: return register_exit;
     38     case 0x3803c6b0: return sub_3803C6B0;
     39     case 0x3b001000: return sub_3B001000;
     40     case 0x3b001010: return sub_3B001010;
     41     case 0x3b0010b0: return sub_3B0010B0;
     42     case 0x3b0010e0: return sub_3B0010E0;
     43     case 0x3b001110: return sub_3B001110;
     44     case 0x3b001170: return sub_3B001170;
     45     case 0x3b0011b0: return sub_3B0011B0;
     46     case 0x3b0011f0: return sub_3B0011F0;
     47     case 0x3b001240: return sub_3B001240;
     48     case 0x3b001500: return sub_3B001500;
     49     case 0x3b0015a0: return sub_3B0015A0;
     50     case 0x3b001610: return sub_3B001610;
     51     case 0x3b003730: return sub_3B003730;
     52     default: fprintf(stderr, "Unexpected terrain call %08x\n", address); abort();
     53     }
     54 }
     55 static void triangle_hit(unsigned diagonal, float x, float z, float slope)
     56 {
     57     memset((void *)ADDR(0x50000000), 0, 0x20000);
     58     memset((void *)ADDR(0x3806f000), 0, 0x300);
     59     float tile[] = {0,0,0, 4,4*slope,0, 4,4*slope,4, 0,0,4};
     60     memcpy(corners, tile, sizeof(tile));
     61     parity = diagonal; registrations = 0;
     62     g_ecx = 0x50000000; g_esp = 0x5001ff00;
     63     g_ebp = 123; g_ebx = 234; g_esi = 345; g_edi = 456;
     64     g_fp_top = 2;
     65     g_st[0] = 17.25; g_st[1] = -31.5;
     66     g_st_i64[0] = INT64_MAX; g_st_i64[1] = INT64_MIN;
     67     MEM32(g_esp + 4) = 0; MEM32(g_esp + 8) = 0;
     68     MEM32(g_esp + 12) = 0x50001000; MEM32(g_esp + 16) = 0x5000100c;
     69     float origin[] = {x,10,z}, direction[] = {0,-20,0}, end[] = {x,-10,z};
     70     memcpy((void *)ADDR(0x3806f028), origin, 12);
     71     memcpy((void *)ADDR(0x3806f018), direction, 12);
     72     memcpy((void *)ADDR(0x3806f008), end, 12);
     73 
     74     sub_3803C220();
     75 
     76     assert((g_eax & 255) == 1);
     77     assert(*(float *)ADDR(0x50001000) == x);
     78     assert(*(float *)ADDR(0x50001004) == x * slope);
     79     assert(*(float *)ADDR(0x50001008) == z);
     80     assert(fabs(*(float *)ADDR(0x5000100c) - (slope ? -.4472136f : 0)) < 1e-6);
     81     assert(fabs(*(float *)ADDR(0x50001010) - (slope ? .8944272f : 1)) < 1e-6);
     82     assert(*(float *)ADDR(0x50001014) == 0);
     83     assert(registrations == 2);
     84     assert(g_esp == 0x5001ff14 && g_ebp == 123 && g_ebx == 234 && g_esi == 345 && g_edi == 456);
     85     assert(g_fp_top == 2 && g_st[0] == 17.25 && g_st[1] == -31.5);
     86     assert(g_st_i64[0] == INT64_MAX && g_st_i64[1] == INT64_MIN);
     87 }
     88 static void outside_triangle(void)
     89 {
     90     triangle_hit(1, 3, .5f, 0);
     91     *(float *)ADDR(0x3806f028) = 5;
     92     *(float *)ADDR(0x3806f008) = 5;
     93     g_ecx = 0x50000000; g_esp = 0x5001ff00;
     94     MEM32(g_esp + 16) = 0x5000100c;
     95 
     96     sub_3803C220();
     97 
     98     assert((g_eax & 255) == 0);
     99     assert(*(float *)ADDR(0x50001000) == 5);
    100     assert(*(float *)ADDR(0x50001004) == 0);
    101     assert(*(float *)ADDR(0x50001008) == .5f);
    102     assert(*(float *)ADDR(0x5000100c) == 0);
    103     assert(*(float *)ADDR(0x50001010) == -16);
    104     assert(*(float *)ADDR(0x50001014) == 0);
    105 }
    106 static void above_tile(void)
    107 {
    108     triangle_hit(1, 3, .5f, 0);
    109     *(float *)ADDR(0x3806f00c) = 20;
    110     *(float *)ADDR(0x3806f01c) = 10;
    111     g_ecx = 0x50000000; g_esp = 0x5001ff00;
    112     MEM32(g_esp + 16) = 0x5000100c;
    113     memset((void *)ADDR(0x50001000), 0xa5, 24);
    114 
    115     sub_3803C220();
    116 
    117     assert((g_eax & 255) == 0);
    118     for (unsigned i = 0; i < 6; ++i) assert(MEM32(0x50001000 + i * 4) == 0xa5a5a5a5);
    119 }
    120 static void maximum_corner(unsigned corner, float height)
    121 {
    122     triangle_hit(1, 3, .5f, 0);
    123     corners[corner * 3 + 1] = height;
    124     *(float *)ADDR(0x3806f00c) = 20;
    125     *(float *)ADDR(0x3806f01c) = 10;
    126     g_ecx = 0x50000000; g_esp = 0x5001ff00;
    127     MEM32(g_esp + 16) = 0x5000100c;
    128     memset((void *)ADDR(0x50001000), 0xa5, 24);
    129 
    130     sub_3803C220();
    131 
    132     /* The height check must allow triangle testing; the upward ray then misses.
    133      * Failed triangle tests write a normal, unlike the height rejection. */
    134     assert((g_eax & 255) == 0);
    135     assert(*(float *)ADDR(0x50001010) == -16);
    136 }
    137 static void resident_height_rejection(unsigned full_height, unsigned mode)
    138 {
    139     memset((void *)ADDR(0x50000000), 0, 0x20000);
    140     MEM32(0x50000004) = 0x50000100;
    141     MEM32(0x5000010c) = 0x50002000;
    142     MEM32(0x50002018) = mode == 4;
    143     *(float *)ADDR(0x5000201c) = .5f;
    144     MEM32(0x5000a258) = 0x5000b000;
    145     MEM32(0x5000a25c) = MEM32(0x5000a260) = 32;
    146     MEM32(0x5000b024) = 1234;
    147     MEM8(0x5000b029) = full_height;
    148     MEM32(0x5000b038) = 16; MEM32(0x5000b03c) = 4;
    149     MEM32(0x5000b040) = 15; MEM32(0x5000b048) = 2;
    150     MEM32(0x5000b050) = 0x5000c000;
    151     MEM32(0x5000c01c) = mode == 1 ? 0 : 0x5000d000;
    152     MEM16(0x5000d000 + 88 * 2) = 256;
    153     MEM16(0x5000d000 + 89 * 2) = 512;
    154     MEM16(0x5000d000 + 106 * 2) = 768;
    155     MEM16(0x5000d000 + 105 * 2) = 1024;
    156     MEM32(0x3804e24c) = 0x25040c80;
    157     MEM32(0x3804e248) = 0x25040cb0;
    158     MEM32(0x3804e244) = mode == 3 ? 0x508 : 0x25040840;
    159     float maximum = full_height ? 512.f : 2.f;
    160     float tile[] = {0,0,0, 4,0,0, 4,maximum,4, 0,0,4};
    161     memcpy(corners, tile, sizeof(tile));
    162     *(float *)ADDR(0x3806f02c) = maximum + 1;
    163     *(float *)ADDR(0x3806f00c) = mode == 5 ? maximum : maximum + 2;
    164     g_ecx = 0x50000000; g_esp = 0x5001ff00;
    165     g_ebp = 123; g_ebx = 234; g_esi = 345; g_edi = 456;
    166     g_fp_top = 2; g_st[0] = 17.25; g_st[1] = -31.5;
    167     MEM32(g_esp + 4) = mode == 2 ? 15 : 3; MEM32(g_esp + 8) = 5;
    168     MEM32(g_esp + 12) = 0x50001000; MEM32(g_esp + 16) = 0x5000100c;
    169     memset((void *)ADDR(0x50001000), 0xa5, 24);
    170     corner_calls = 0;
    171 
    172     sub_3803C220();
    173 
    174     assert(corner_calls == (mode ? 1 : 0));
    175     assert(MEM32(0x5000c018) == (mode ? 0 : 1234));
    176     assert(g_esp == 0x5001ff14 && g_ebp == 123 && g_ebx == 234 && g_esi == 345 && g_edi == 456);
    177     assert(g_fp_top == 2 && g_st[0] == 17.25 && g_st[1] == -31.5);
    178     if (!mode) {
    179         assert((g_eax & 255) == 0);
    180         for (unsigned i = 0; i < 6; ++i) assert(MEM32(0x50001000 + i * 4) == 0xa5a5a5a5);
    181     }
    182 }
    183 int main(void)
    184 {
    185     assert(guest_map("terrain tile fixture", 0x50000000, 0x20000));
    186     assert(guest_map("terrain tile imports", 0x38040000, 0x10000));
    187     assert(guest_map("terrain tile globals", 0x38060000, 0x10000));
    188     assert(guest_map("vector constants", 0x3b000000, 0x10000));
    189     MEM32(0x3804e4c8) = 0x3b001170; MEM32(0x3804e4d0) = 0x3b001610;
    190     MEM32(0x3804e4d4) = 0x3b001110; MEM32(0x3804e4d8) = 0x3b001010;
    191     MEM32(0x3804e4e0) = 0x3b001240; MEM32(0x3804e4e8) = 0x3b0010b0;
    192     MEM32(0x3804e4ec) = 0x3b003730; MEM32(0x3804e4f4) = 0x3b0010e0;
    193     MEM32(0x3804e4fc) = 0x3b0015a0; MEM32(0x3804e504) = 0x3b0011f0;
    194     MEM32(0x3804e508) = 0x3b001500; MEM32(0x3804e51c) = 0x3b001000;
    195     MEM32(0x3804e528) = 0x3b0011b0;
    196     *(float *)ADDR(0x3b006048) = 1; *(float *)ADDR(0x3804e8cc) = 1;
    197     *(float *)ADDR(0x3804e8e4) = 0; *(float *)ADDR(0x3804f7a4) = -.0005f;
    198 
    199     /* Both diagonals, both triangles, their shared edge, and a sloped surface. */
    200     for (unsigned diagonal = 0; diagonal < 2; ++diagonal) {
    201         triangle_hit(diagonal, 3, .5f, 0);
    202         triangle_hit(diagonal, .5f, 3, 0);
    203         triangle_hit(diagonal, 2, 2, 0);
    204         triangle_hit(diagonal, 3, .5f, .5f);
    205         triangle_hit(diagonal, .5f, 3, .5f);
    206     }
    207     for (unsigned corner = 0; corner < 4; ++corner) maximum_corner(corner, 30);
    208     maximum_corner(2, 10);
    209     outside_triangle();
    210     above_tile();
    211     /* Resident, cold, patch boundary, alternate import, dungeon, and touching rays. */
    212     for (unsigned full_height = 0; full_height < 2; ++full_height)
    213         for (unsigned mode = 0; mode < 6; ++mode)
    214             resident_height_rejection(full_height, mode);
    215     puts("terrain tile intersections passed");
    216     return 0;
    217 }