client/tests/ip_training.c (6943B)
1 #include "recomp_types.h" 2 #include <assert.h> 3 4 void sub_360751E0(void); 5 void sub_360755C0(void); 6 7 static int32_t profession, level, title, factor, category; 8 static int32_t title_cap, previous_cap, per_level, title_start; 9 static int32_t breed_base, breed_cap, tested_stat; 10 static double trickle; 11 12 static void actor_stat(void) 13 { 14 assert(g_ecx == 0x500002e4 && MEM32(g_esp + 8) == 2); 15 switch (MEM32(g_esp + 4)) { 16 case 4: g_eax = 1; break; 17 case 37: g_eax = title; break; 18 case 54: g_eax = level; break; 19 case 60: g_eax = profession; break; 20 default: abort(); 21 } 22 g_esp += 12; 23 } 24 25 static void ability_factor(void) 26 { 27 assert(MEM32(g_esp + 4) == 1 && MEM32(g_esp + 8) == tested_stat); 28 g_eax = factor; 29 g_esp += 4; 30 } 31 32 static void skill_factor(void) 33 { 34 assert(MEM32(g_esp + 4) == (uint32_t)profession); 35 assert(MEM32(g_esp + 8) == (uint32_t)tested_stat); 36 g_eax = factor; 37 g_esp += 4; 38 } 39 40 static void category_cap(void) 41 { 42 assert(MEM32(g_esp + 4) == (uint32_t)category); 43 int32_t requested_title = MEM32(g_esp + 8); 44 assert(requested_title == title || requested_title == title - 1); 45 g_eax = requested_title == title ? title_cap : previous_cap; 46 g_esp += 4; 47 } 48 49 static void category_growth(void) 50 { 51 assert(MEM32(g_esp + 4) == (uint32_t)category); 52 g_eax = per_level; 53 g_esp += 4; 54 } 55 56 static void profession_title(void) 57 { 58 assert(MEM32(g_esp + 4) == (uint32_t)profession); 59 assert(MEM32(g_esp + 8) == (uint32_t)title); 60 MEM32(0x5000040c) = title_start; 61 g_eax = 0x50000400; 62 g_esp += 4; 63 } 64 65 static void ability_trickle(void) 66 { 67 assert(g_ecx == 0x50000000); 68 assert(MEM32(g_esp + 4) == (uint32_t)tested_stat && MEM32(g_esp + 8) == 0); 69 fp_push_impl(g_st, &g_fp_top, trickle); 70 g_esp += 12; 71 } 72 73 static void breed_start(void) 74 { 75 assert(MEM32(g_esp + 4) == 1 && MEM32(g_esp + 8) == tested_stat); 76 g_eax = breed_base; 77 g_esp += 4; 78 } 79 80 static void breed_maximum(void) 81 { 82 assert(MEM32(g_esp + 4) == 1 && MEM32(g_esp + 8) == tested_stat); 83 g_eax = breed_cap; 84 g_esp += 4; 85 } 86 87 static void integer(void) 88 { 89 g_eax = (uint32_t)(int32_t)g_st[0]; 90 fp_pop_impl(g_st, &g_fp_top); 91 g_esp += 4; 92 } 93 94 recomp_func_t recomp_lookup(uint32_t address) 95 { 96 switch (address) { 97 case 0x50000300: return actor_stat; 98 case 0x360dd900: return ability_factor; 99 case 0x360e95d0: return skill_factor; 100 case 0x360ee030: return category_cap; 101 case 0x360ee060: return category_growth; 102 case 0x360e6050: return profession_title; 103 case 0x36073fa0: return ability_trickle; 104 case 0x360ee870: return breed_start; 105 case 0x360ee8c0: return breed_maximum; 106 case 0x3615fd8c: return integer; 107 default: fprintf(stderr, "unexpected IP training call: %08x\n", address); abort(); 108 } 109 } 110 recomp_func_t recomp_lookup_manual(uint32_t address) { return NULL; } 111 recomp_func_t recomp_lookup_import(uint32_t address) { abort(); } 112 113 static void setup(void) 114 { 115 g_esp = 0x5000ff00; 116 g_ecx = 0x50000000; 117 g_fp_top = 0; 118 profession = 6; 119 level = 3; 120 title = 1; 121 factor = 10; 122 category = 1; 123 title_cap = 100; 124 previous_cap = 40; 125 per_level = 5; 126 title_start = 15; 127 trickle = 100; 128 breed_base = 6; 129 breed_cap = 512; 130 } 131 132 static void point_costs(void) 133 { 134 const struct { int32_t stat, raw, factor, profession; double expected; } cases[] = { 135 {100, 5, 10, 6, 5.000000074505806}, 136 {100, 5, 15, 6, 7.500000111758709}, 137 {100, 5, 16, 6, 8.00000011920929}, 138 {16, 5, 2, 6, 10}, {21, 5, 2, 6, 10}, 139 {100, 0, 10, 6, 1.0000000149011612}, 140 {100, -3, 10, 6, 1.0000000149011612}, 141 {100, 5, 10, -1, 1}, 142 {15, 5, 10, 6, 2.5}, {22, 5, 10, 6, 2.5}, 143 {99, 5, 10, 6, 2.5}, {168, 5, 10, 6, 5.000000074505806}, 144 {169, 5, 10, 6, 2.5} 145 }; 146 for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { 147 setup(); 148 tested_stat = cases[i].stat; 149 factor = cases[i].factor; 150 profession = cases[i].profession; 151 MEM32(g_esp + 4) = tested_stat; 152 MEM32(g_esp + 8) = cases[i].raw; 153 154 sub_360751E0(); 155 156 assert(g_fp_top == 1 && g_st[0] == cases[i].expected); 157 assert(g_esp == 0x5000ff0c); 158 } 159 } 160 161 static void skill_caps(void) 162 { 163 const struct { 164 int32_t factor, category, title, level, per_level, title_cap; 165 double trickle; 166 int32_t expected; 167 } cases[] = { 168 {10, 1, 1, 3, 5, 100, 100, 20}, 169 {14, 1, 1, 3, 5, 100, 100, 20}, 170 {15, 2, 1, 3, 5, 100, 100, 20}, 171 {16, 2, 1, 3, 5, 100, 100, 20}, 172 {24, 2, 1, 3, 5, 100, 100, 20}, 173 {25, 3, 1, 3, 5, 100, 100, 20}, 174 {10, 1, 1, 3, 0, 100, 100, 23}, 175 {10, 1, 1, 3, 11, 100, 100, 23}, 176 {10, 1, 1, 3, 5, 100, 1.99, 15}, 177 {10, 1, 1, 3, 5, 100, 0, 20}, 178 {10, 1, 2, 20, 5, 100, 100, 70}, 179 {10, 1, 2, 20, 5, 60, 100, 60}, 180 {10, 1, 2, 20, 10, 100, 100, 76}, 181 {10, 1, 2, 20, 5, 100, 3.125, 25} 182 }; 183 for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { 184 setup(); 185 tested_stat = 100; 186 factor = cases[i].factor; 187 category = cases[i].category; 188 title = cases[i].title; 189 level = cases[i].level; 190 per_level = cases[i].per_level; 191 title_cap = cases[i].title_cap; 192 trickle = cases[i].trickle; 193 MEM32(g_esp + 4) = tested_stat; 194 195 sub_360755C0(); 196 197 assert((int32_t)g_eax == cases[i].expected); 198 assert(g_esp == 0x5000ff08 && g_fp_top == 0); 199 } 200 } 201 202 static void ability_caps(void) 203 { 204 const struct { int32_t stat, level, base, maximum, expected; } cases[] = { 205 {16, 1, 6, 512, 9}, {21, 3, 6, 512, 15}, 206 {16, 200, 6, 480, 480}, {21, 200, 6, 512, 512} 207 }; 208 for (unsigned i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { 209 setup(); 210 tested_stat = cases[i].stat; 211 level = cases[i].level; 212 breed_base = cases[i].base; 213 breed_cap = cases[i].maximum; 214 MEM32(g_esp + 4) = tested_stat; 215 216 sub_360755C0(); 217 218 assert((int32_t)g_eax == cases[i].expected); 219 assert(g_esp == 0x5000ff08 && g_fp_top == 0); 220 } 221 } 222 223 int main(void) 224 { 225 assert(guest_map("IP training fixture", 0x50000000, 0x10000)); 226 assert(guest_map("native IP constants", 0x36170000, 0x10000)); 227 MEM32(0x50000014) = 0x50000200; 228 MEM32(0x500002e4) = 0x50000100; 229 MEM32(0x50000104) = 0x50000300; 230 231 // Original kkr.dll constant bytes, rebased from 1017xxxx to 3617xxxx. 232 MEM32(0x36170380) = 0x3f800000; 233 MEM32(0x36170464) = 0x3f000000; 234 MEM32(0x361709dc) = 0x447a0000; 235 MEM32(0x36170ba8) = 0x41000000; 236 MEM32(0x361710c4) = 0x40a00000; 237 MEM32(0x361714f0) = 0; 238 MEM32(0x361714f4) = 0x3fe00000; 239 MEM32(0x361714f8) = 0x3dcccccd; 240 241 point_costs(); 242 skill_caps(); 243 ability_caps(); 244 puts("native IP training: point costs, category boundaries and skill/ability caps passed"); 245 }