aorecomp

Anarchy Online classic in the browser

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


client/tests/weapon_rating.c (4783B)


      1 #include "recomp_types.h"
      2 #include <assert.h>
      3 
      4 void sub_3608F1F0(void);
      5 void sub_3608F2F0(void);
      6 void sub_3606DFB0(void);
      7 
      8 static int32_t effective[1200];
      9 static unsigned section, section_calls, lazy_probes, effective_calls;
     10 
     11 static void item_section(void)
     12 {
     13     assert(g_ecx == 0x50000100 && MEM32(g_esp + 4) == section);
     14     section_calls++;
     15     g_eax = 0x50000800;
     16     g_esp += 8;
     17 }
     18 
     19 static void has_section(void)
     20 {
     21     assert(g_ecx == 0x50000100);
     22     assert(MEM32(g_esp + 4) == ++lazy_probes);
     23     g_eax = 0;
     24     g_esp += 8;
     25 }
     26 
     27 /* Effective values isolate the native getter's mode-0 branch. */
     28 static void effective_stat(void)
     29 {
     30     assert(g_ecx == 0x50000700 && MEM32(g_esp + 4) < 1200);
     31     effective_calls++;
     32     g_eax = (uint32_t)effective[MEM32(g_esp + 4)];
     33     g_esp += 8;
     34 }
     35 
     36 recomp_func_t recomp_lookup(uint32_t address)
     37 {
     38     switch (address) {
     39     case 0x3608f810: return item_section;
     40     case 0x3608f8e0: return has_section;
     41     case 0x3606dfb0: return sub_3606DFB0;
     42     case 0x36075ca0: return effective_stat;
     43     default: fprintf(stderr, "unexpected rating call: %08x\n", address); abort();
     44     }
     45 }
     46 
     47 recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; }
     48 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); }
     49 
     50 static void weighted_ratings(void)
     51 {
     52     const struct {
     53         unsigned defense, npc;
     54         int32_t first, second, first_weight, second_weight, modifier, expected;
     55     } cases[] = {
     56         {0, 0, 125, 125, 50, 50, 7, 131},
     57         {1, 0, 125, 125, 50, 50, 7, 131},
     58         {0, 0, 3, 3, 33, 33, 99, 0},
     59         {1, 0, 3, 3, 33, 33, 99, 0},
     60         {0, 0, -3, 1, 50, 50, 99, -1},
     61         {1, 0, -3, 1, 50, 50, 99, -1},
     62         {0, 0, -3, 3, 50, 50, 99, 0},
     63         {1, 0, -3, 3, 50, 50, 99, 0},
     64         {0, 0, 3, 3, 50, 50, -7, -5},
     65         {1, 0, 3, 3, 50, 50, -7, -5},
     66         {0, 1, 5, 5, 50, 50, 7, 11},
     67         {1, 1, 5, 5, 50, 50, 7, 11},
     68         {0, 1, -3, 1, 50, 50, 99, -1},
     69         {1, 1, -3, 1, 50, 50, 99, -1}
     70     };
     71     for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) {
     72         unsigned first_stat = cases[i].defense ? 155 : 100;
     73         unsigned second_stat = cases[i].defense ? 154 : 102;
     74         unsigned modifier_stat = cases[i].defense ? 277 : 276;
     75         section = cases[i].defense ? 13 : 12;
     76         section_calls = lazy_probes = effective_calls = 0;
     77         MEM8(0x500003b4) = cases[i].npc;
     78         MEM32(0x50000804) = 0x50000900;
     79         MEM32(0x50000808) = 0x50000910;
     80         MEM32(0x50000900) = first_stat;
     81         MEM32(0x50000904) = (uint32_t)cases[i].first_weight;
     82         MEM32(0x50000908) = second_stat;
     83         MEM32(0x5000090c) = (uint32_t)cases[i].second_weight;
     84         MEM32(0x50001000 + first_stat * 4) = (uint32_t)cases[i].first;
     85         MEM32(0x50001000 + second_stat * 4) = (uint32_t)cases[i].second;
     86         MEM32(0x50001000 + modifier_stat * 4) = 999;
     87         effective[first_stat] = cases[i].first + (cases[i].npc ? 100 : 0);
     88         effective[second_stat] = cases[i].second + (cases[i].npc ? 100 : 0);
     89         effective[modifier_stat] = cases[i].modifier;
     90         g_esp = 0x5000ff00;
     91         g_ecx = 0x50000100;
     92         MEM32(g_esp + 4) = 0x50000200;
     93 
     94         if (cases[i].defense) sub_3608F2F0();
     95         else sub_3608F1F0();
     96 
     97         assert((int32_t)g_eax == cases[i].expected);
     98         assert(g_esp == 0x5000ff08 && lazy_probes == 0);
     99         assert(section_calls == 5);
    100         unsigned weighted_reads = cases[i].npc ? 0 : 2;
    101         unsigned modifier_reads = cases[i].expected > 0 || cases[i].modifier == -7;
    102         assert(effective_calls == weighted_reads + modifier_reads);
    103     }
    104 }
    105 
    106 static void missing_and_empty_ratings(void)
    107 {
    108     for (unsigned defense = 0; defense < 2; defense++) {
    109         for (unsigned missing = 0; missing < 2; missing++) {
    110             section = defense ? 13 : 12;
    111             section_calls = lazy_probes = effective_calls = 0;
    112             MEM32(0x50000804) = MEM32(0x50000808) = 0x50000900;
    113             g_esp = 0x5000ff00;
    114             g_ecx = 0x50000100;
    115             MEM32(g_esp + 4) = missing ? 0 : 0x50000200;
    116 
    117             if (defense) sub_3608F2F0();
    118             else sub_3608F1F0();
    119 
    120             assert(g_eax == 0 && g_esp == 0x5000ff08);
    121             assert(effective_calls == 0);
    122             assert(lazy_probes == (missing ? 0 : 35));
    123             assert(section_calls == (missing ? 0 : 3));
    124         }
    125     }
    126 }
    127 
    128 int main(void)
    129 {
    130     assert(guest_map("weapon rating fixture", 0x50000000, 0x10000));
    131     MEM32(0x500002e4) = 0x50000400;
    132     MEM32(0x50000404) = 0x3606dfb0;
    133     MEM32(0x500002e8) = 0x50000600;
    134     MEM32(0x50000604) = 0x50001000;
    135     MEM32(0x50000354) = 0x50000700;
    136     weighted_ratings();
    137     missing_and_empty_ratings();
    138     puts("native weapon ratings: 18 signed, section, modifier and getter-mode cases passed");
    139 }