diff options
author | Louis Hampton <roastveg@hotmail.co.uk> | 2020-06-20 19:01:00 +0100 |
---|---|---|
committer | Louis Hampton <roastveg@hotmail.co.uk> | 2020-06-20 19:01:00 +0100 |
commit | 3cf5ef0a661c62a94766e2c08025bb7ecb785b2a (patch) | |
tree | 7fa9fc230a0517f65876613318f043f3c14b8f63 /arm9/src | |
parent | 59f6bdfa9a7645d3e2e9d1ac0bde5af3ad77e8b2 (diff) | |
parent | 6298426d9ad4c59a875b0a90a0facfa1a3a513cc (diff) |
Merge branch 'master' of https://github.com/roastveg/pokediamond
Diffstat (limited to 'arm9/src')
-rw-r--r-- | arm9/src/main.c | 8 | ||||
-rw-r--r-- | arm9/src/math_util.c | 411 | ||||
-rw-r--r-- | arm9/src/msgdata.c | 400 | ||||
-rw-r--r-- | arm9/src/nutdata.c | 78 | ||||
-rw-r--r-- | arm9/src/pokemon.c | 61 | ||||
-rw-r--r-- | arm9/src/string_util.c | 4 | ||||
-rw-r--r-- | arm9/src/unk_0201B8B8.c | 33 | ||||
-rw-r--r-- | arm9/src/waza.c | 48 |
8 files changed, 984 insertions, 59 deletions
diff --git a/arm9/src/main.c b/arm9/src/main.c index 82b8b734..fea5a53b 100644 --- a/arm9/src/main.c +++ b/arm9/src/main.c @@ -267,8 +267,8 @@ THUMB_FUNC void FUN_02000F4C(u32 arg0, u32 arg1) } extern void FUN_0201265C(struct Unk21C4818 *, struct Unk21C4828 *); -extern void seedr_MT(u32); -extern void seedr_LC(u32); +extern void SetMTRNGSeed(u32); +extern void SetLCRNGSeed(u32); THUMB_FUNC void InitializeMainRNG(void) { @@ -278,8 +278,8 @@ THUMB_FUNC void InitializeMainRNG(void) { u32 r4 = gUnknown21C48B8.unk2C; u32 r5 = ((sp0.unk4 + sp0.unk8) << 24) + (spC.unk0 + ((256 * spC.unk4 * spC.unk8) << 16) + (sp0.unk0 << 16)); - seedr_MT(r4 + r5); - seedr_LC(r4 + r5); + SetMTRNGSeed(r4 + r5); + SetLCRNGSeed(r4 + r5); } } diff --git a/arm9/src/math_util.c b/arm9/src/math_util.c new file mode 100644 index 00000000..5aeb4f8e --- /dev/null +++ b/arm9/src/math_util.c @@ -0,0 +1,411 @@ +#include "global.h" +#include "math_util.h" + +extern const s16 UNK_020FFA38[]; // temporary until further notice + +/* + * Constant tables + */ +const s32 gSineTable[] = +{ + 0x00000000, 0x00000047, 0x0000008F, 0x000000D6, + 0x0000011E, 0x00000165, 0x000001AC, 0x000001F3, + 0x0000023A, 0x00000281, 0x000002C7, 0x0000030E, + 0x00000354, 0x00000399, 0x000003DF, 0x00000424, + 0x00000469, 0x000004AE, 0x000004F2, 0x00000536, + 0x00000579, 0x000005BC, 0x000005FE, 0x00000640, + 0x00000682, 0x000006C3, 0x00000704, 0x00000744, + 0x00000783, 0x000007C2, 0x00000800, 0x0000083E, + 0x0000087B, 0x000008B7, 0x000008F2, 0x0000092D, + 0x00000968, 0x000009A1, 0x000009DA, 0x00000A12, + 0x00000A49, 0x00000A7F, 0x00000AB5, 0x00000AE9, + 0x00000B1D, 0x00000B50, 0x00000B82, 0x00000BB4, + 0x00000BE4, 0x00000C13, 0x00000C42, 0x00000C6F, + 0x00000C9C, 0x00000CC7, 0x00000CF2, 0x00000D1B, + 0x00000D44, 0x00000D6B, 0x00000D92, 0x00000DB7, + 0x00000DDB, 0x00000DFE, 0x00000E21, 0x00000E42, + 0x00000E61, 0x00000E80, 0x00000E9E, 0x00000EBA, + 0x00000ED6, 0x00000EF0, 0x00000F09, 0x00000F21, + 0x00000F38, 0x00000F4D, 0x00000F61, 0x00000F74, + 0x00000F86, 0x00000F97, 0x00000FA6, 0x00000FB5, + 0x00000FC2, 0x00000FCE, 0x00000FD8, 0x00000FE1, + 0x00000FEA, 0x00000FF0, 0x00000FF6, 0x00000FFA, + 0x00000FFE, 0x00000FFF, 0x00001000, 0x00000FFF, + 0x00000FFE, 0x00000FFA, 0x00000FF6, 0x00000FF0, + 0x00000FEA, 0x00000FE1, 0x00000FD8, 0x00000FCE, + 0x00000FC2, 0x00000FB5, 0x00000FA6, 0x00000F97, + 0x00000F86, 0x00000F74, 0x00000F61, 0x00000F4D, + 0x00000F38, 0x00000F21, 0x00000F09, 0x00000EF0, + 0x00000ED6, 0x00000EBA, 0x00000E9E, 0x00000E80, + 0x00000E61, 0x00000E42, 0x00000E21, 0x00000DFE, + 0x00000DDB, 0x00000DB7, 0x00000D92, 0x00000D6B, + 0x00000D44, 0x00000D1B, 0x00000CF2, 0x00000CC7, + 0x00000C9C, 0x00000C6F, 0x00000C42, 0x00000C13, + 0x00000BE4, 0x00000BB4, 0x00000B82, 0x00000B50, + 0x00000B1D, 0x00000AE9, 0x00000AB5, 0x00000A7F, + 0x00000A49, 0x00000A12, 0x000009DA, 0x000009A1, + 0x00000968, 0x0000092D, 0x000008F2, 0x000008B7, + 0x0000087B, 0x0000083E, 0x00000800, 0x000007C2, + 0x00000783, 0x00000744, 0x00000704, 0x000006C3, + 0x00000682, 0x00000640, 0x000005FE, 0x000005BC, + 0x00000579, 0x00000536, 0x000004F2, 0x000004AE, + 0x00000469, 0x00000424, 0x000003DF, 0x00000399, + 0x00000354, 0x0000030E, 0x000002C7, 0x00000281, + 0x0000023A, 0x000001F3, 0x000001AC, 0x00000165, + 0x0000011E, 0x000000D6, 0x0000008F, 0x00000047, + 0x00000000, 0xFFFFFFB9, 0xFFFFFF71, 0xFFFFFF2A, + 0xFFFFFEE2, 0xFFFFFE9B, 0xFFFFFE54, 0xFFFFFE0D, + 0xFFFFFDC6, 0xFFFFFD7F, 0xFFFFFD39, 0xFFFFFCF2, + 0xFFFFFCAC, 0xFFFFFC67, 0xFFFFFC21, 0xFFFFFBDC, + 0xFFFFFB97, 0xFFFFFB52, 0xFFFFFB0E, 0xFFFFFACA, + 0xFFFFFA87, 0xFFFFFA44, 0xFFFFFA02, 0xFFFFF9C0, + 0xFFFFF97E, 0xFFFFF93D, 0xFFFFF8FC, 0xFFFFF8BC, + 0xFFFFF87D, 0xFFFFF83E, 0xFFFFF800, 0xFFFFF7C2, + 0xFFFFF785, 0xFFFFF749, 0xFFFFF70E, 0xFFFFF6D3, + 0xFFFFF698, 0xFFFFF65F, 0xFFFFF626, 0xFFFFF5EE, + 0xFFFFF5B7, 0xFFFFF581, 0xFFFFF54B, 0xFFFFF517, + 0xFFFFF4E3, 0xFFFFF4B0, 0xFFFFF47E, 0xFFFFF44C, + 0xFFFFF41C, 0xFFFFF3ED, 0xFFFFF3BE, 0xFFFFF391, + 0xFFFFF364, 0xFFFFF339, 0xFFFFF30E, 0xFFFFF2E5, + 0xFFFFF2BC, 0xFFFFF295, 0xFFFFF26E, 0xFFFFF249, + 0xFFFFF225, 0xFFFFF202, 0xFFFFF1DF, 0xFFFFF1BE, + 0xFFFFF19F, 0xFFFFF180, 0xFFFFF162, 0xFFFFF146, + 0xFFFFF12A, 0xFFFFF110, 0xFFFFF0F7, 0xFFFFF0DF, + 0xFFFFF0C8, 0xFFFFF0B3, 0xFFFFF09F, 0xFFFFF08C, + 0xFFFFF07A, 0xFFFFF069, 0xFFFFF05A, 0xFFFFF04B, + 0xFFFFF03E, 0xFFFFF032, 0xFFFFF028, 0xFFFFF01F, + 0xFFFFF016, 0xFFFFF010, 0xFFFFF00A, 0xFFFFF006, + 0xFFFFF002, 0xFFFFF001, 0xFFFFF000, 0xFFFFF001, + 0xFFFFF002, 0xFFFFF006, 0xFFFFF00A, 0xFFFFF010, + 0xFFFFF016, 0xFFFFF01F, 0xFFFFF028, 0xFFFFF032, + 0xFFFFF03E, 0xFFFFF04B, 0xFFFFF05A, 0xFFFFF069, + 0xFFFFF07A, 0xFFFFF08C, 0xFFFFF09F, 0xFFFFF0B3, + 0xFFFFF0C8, 0xFFFFF0DF, 0xFFFFF0F7, 0xFFFFF110, + 0xFFFFF12A, 0xFFFFF146, 0xFFFFF162, 0xFFFFF180, + 0xFFFFF19F, 0xFFFFF1BE, 0xFFFFF1DF, 0xFFFFF202, + 0xFFFFF225, 0xFFFFF249, 0xFFFFF26E, 0xFFFFF295, + 0xFFFFF2BC, 0xFFFFF2E5, 0xFFFFF30E, 0xFFFFF339, + 0xFFFFF364, 0xFFFFF391, 0xFFFFF3BE, 0xFFFFF3ED, + 0xFFFFF41C, 0xFFFFF44C, 0xFFFFF47E, 0xFFFFF4B0, + 0xFFFFF4E3, 0xFFFFF517, 0xFFFFF54B, 0xFFFFF581, + 0xFFFFF5B7, 0xFFFFF5EE, 0xFFFFF626, 0xFFFFF65F, + 0xFFFFF698, 0xFFFFF6D3, 0xFFFFF70E, 0xFFFFF749, + 0xFFFFF785, 0xFFFFF7C2, 0xFFFFF800, 0xFFFFF83E, + 0xFFFFF87D, 0xFFFFF8BC, 0xFFFFF8FC, 0xFFFFF93D, + 0xFFFFF97E, 0xFFFFF9C0, 0xFFFFFA02, 0xFFFFFA44, + 0xFFFFFA87, 0xFFFFFACA, 0xFFFFFB0E, 0xFFFFFB52, + 0xFFFFFB97, 0xFFFFFBDC, 0xFFFFFC21, 0xFFFFFC67, + 0xFFFFFCAC, 0xFFFFFCF2, 0xFFFFFD39, 0xFFFFFD7F, + 0xFFFFFDC6, 0xFFFFFE0D, 0xFFFFFE54, 0xFFFFFE9B, + 0xFFFFFEE2, 0xFFFFFF2A, 0xFFFFFF71, 0xFFFFFFB9, + 0x00000000, 0x00000047, 0x0000008F, 0x000000D6, + 0x0000011E, 0x00000165, 0x000001AC, 0x000001F3, + 0x0000023A, 0x00000281, 0x000002C7, 0x0000030E, + 0x00000354, 0x00000399, 0x000003DF, 0x00000424, + 0x00000469, 0x000004AE, 0x000004F2, 0x00000536, + 0x00000579, 0x000005BC, 0x000005FE, 0x00000640, + 0x00000682, 0x000006C3, 0x00000704, 0x00000744, + 0x00000783, 0x000007C2, 0x00000800, 0x0000083E, + 0x0000087B, 0x000008B7, 0x000008F2, 0x0000092D, + 0x00000968, 0x000009A1, 0x000009DA, 0x00000A12, + 0x00000A49, 0x00000A7F, 0x00000AB5, 0x00000AE9, + 0x00000B1D, 0x00000B50, 0x00000B82, 0x00000BB4, + 0x00000BE4, 0x00000C13, 0x00000C42, 0x00000C6F, + 0x00000C9C, 0x00000CC7, 0x00000CF2, 0x00000D1B, + 0x00000D44, 0x00000D6B, 0x00000D92, 0x00000DB7, + 0x00000DDB, 0x00000DFE, 0x00000E21, 0x00000E42, + 0x00000E61, 0x00000E80, 0x00000E9E, 0x00000EBA, + 0x00000ED6, 0x00000EF0, 0x00000F09, 0x00000F21, + 0x00000F38, 0x00000F4D, 0x00000F61, 0x00000F74, + 0x00000F86, 0x00000F97, 0x00000FA6, 0x00000FB5, + 0x00000FC2, 0x00000FCE, 0x00000FD8, 0x00000FE1, + 0x00000FEA, 0x00000FF0, 0x00000FF6, 0x00000FFA, + 0x00000FFE, 0x00000FFF +}; + +const u16 UNK_020EDC7E[] = // rotations? +{ + 0x0000, 0x00B7, 0x016D, 0x0223, 0x02D9, 0x038F, 0x0445, 0x04FB, 0x05B1, 0x0667, + 0x071D, 0x07D3, 0x0889, 0x093F, 0x09F5, 0x0AAB, 0x0B61, 0x0C17, 0x0CCD, 0x0D83, + 0x0E39, 0x0EEF, 0x0FA5, 0x105C, 0x1112, 0x11C8, 0x127E, 0x1334, 0x13EA, 0x14A0, + 0x1556, 0x160C, 0x16C2, 0x1778, 0x182E, 0x18E4, 0x199A, 0x1A50, 0x1B06, 0x1BBC, + 0x1C72, 0x1D28, 0x1DDE, 0x1E94, 0x1F4A, 0x2000, 0x20B7, 0x216D, 0x2223, 0x22D9, + 0x238F, 0x2445, 0x24FB, 0x25B1, 0x2667, 0x271D, 0x27D3, 0x2889, 0x293F, 0x29F5, + 0x2AAB, 0x2B61, 0x2C17, 0x2CCD, 0x2D83, 0x2E39, 0x2EEF, 0x2FA5, 0x305C, 0x3112, + 0x31C8, 0x327E, 0x3334, 0x33EA, 0x34A0, 0x3556, 0x360C, 0x36C2, 0x3778, 0x382E, + 0x38E4, 0x399A, 0x3A50, 0x3B06, 0x3BBC, 0x3C72, 0x3D28, 0x3DDE, 0x3E94, 0x3F4A, + 0x4000, 0x40B7, 0x416D, 0x4223, 0x42D9, 0x438F, 0x4445, 0x44FB, 0x45B1, 0x4667, + 0x471D, 0x47D3, 0x4889, 0x493F, 0x49F5, 0x4AAB, 0x4B61, 0x4C17, 0x4CCD, 0x4D83, + 0x4E39, 0x4EEF, 0x4FA5, 0x505C, 0x5112, 0x51C8, 0x527E, 0x5334, 0x53EA, 0x54A0, + 0x5556, 0x560C, 0x56C2, 0x5778, 0x582E, 0x58E4, 0x599A, 0x5A50, 0x5B06, 0x5BBC, + 0x5C72, 0x5D28, 0x5DDE, 0x5E94, 0x5F4A, 0x6000, 0x60B7, 0x616D, 0x6223, 0x62D9, + 0x638F, 0x6445, 0x64FB, 0x65B1, 0x6667, 0x671D, 0x67D3, 0x6889, 0x693F, 0x69F5, + 0x6AAB, 0x6B61, 0x6C17, 0x6CCD, 0x6D83, 0x6E39, 0x6EEF, 0x6FA5, 0x705C, 0x7112, + 0x71C8, 0x727E, 0x7334, 0x73EA, 0x74A0, 0x7556, 0x760C, 0x76C2, 0x7778, 0x782E, + 0x78E4, 0x799A, 0x7A50, 0x7B06, 0x7BBC, 0x7C72, 0x7D28, 0x7DDE, 0x7E94, 0x7F4A, + 0x8000, 0x80B7, 0x816D, 0x8223, 0x82D9, 0x838F, 0x8445, 0x84FB, 0x85B1, 0x8667, + 0x871D, 0x87D3, 0x8889, 0x893F, 0x89F5, 0x8AAB, 0x8B61, 0x8C17, 0x8CCD, 0x8D83, + 0x8E39, 0x8EEF, 0x8FA5, 0x905C, 0x9112, 0x91C8, 0x927E, 0x9334, 0x93EA, 0x94A0, + 0x9556, 0x960C, 0x96C2, 0x9778, 0x982E, 0x98E4, 0x999A, 0x9A50, 0x9B06, 0x9BBC, + 0x9C72, 0x9D28, 0x9DDE, 0x9E94, 0x9F4A, 0xA000, 0xA0B7, 0xA16D, 0xA223, 0xA2D9, + 0xA38F, 0xA445, 0xA4FB, 0xA5B1, 0xA667, 0xA71D, 0xA7D3, 0xA889, 0xA93F, 0xA9F5, + 0xAAAB, 0xAB61, 0xAC17, 0xACCD, 0xAD83, 0xAE39, 0xAEEF, 0xAFA5, 0xB05C, 0xB112, + 0xB1C8, 0xB27E, 0xB334, 0xB3EA, 0xB4A0, 0xB556, 0xB60C, 0xB6C2, 0xB778, 0xB82E, + 0xB8E4, 0xB99A, 0xBA50, 0xBB06, 0xBBBC, 0xBC72, 0xBD28, 0xBDDE, 0xBE94, 0xBF4A, + 0xC000, 0xC0B7, 0xC16D, 0xC223, 0xC2D9, 0xC38F, 0xC445, 0xC4FB, 0xC5B1, 0xC667, + 0xC71D, 0xC7D3, 0xC889, 0xC93F, 0xC9F5, 0xCAAB, 0xCB61, 0xCC17, 0xCCCD, 0xCD83, + 0xCE39, 0xCEEF, 0xCFA5, 0xD05C, 0xD112, 0xD1C8, 0xD27E, 0xD334, 0xD3EA, 0xD4A0, + 0xD556, 0xD60C, 0xD6C2, 0xD778, 0xD82E, 0xD8E4, 0xD99A, 0xDA50, 0xDB06, 0xDBBC, + 0xDC72, 0xDD28, 0xDDDE, 0xDE94, 0xDF4A, 0xE000, 0xE0B7, 0xE16D, 0xE223, 0xE2D9, + 0xE38F, 0xE445, 0xE4FB, 0xE5B1, 0xE667, 0xE71D, 0xE7D3, 0xE889, 0xE93F, 0xE9F5, + 0xEAAB, 0xEB61, 0xEC17, 0xECCD, 0xED83, 0xEE39, 0xEEEF, 0xEFA5, 0xF05C, 0xF112, + 0xF1C8, 0xF27E, 0xF334, 0xF3EA, 0xF4A0, 0xF556, 0xF60C, 0xF6C2, 0xF778, 0xF82E, + 0xF8E4, 0xF99A, 0xFA50, 0xFB06, 0xFBBC, 0xFC72, 0xFD28, 0xFDDE, 0xFE94, 0xFF4A +}; + +const u16 UNK_020EDB80[] = +{ + 0x169F, 0x0F14, 0x0B4F, 0x090C, 0x078A, 0x0676, 0x05A7, 0x0506, + 0x0486, 0x041C, 0x03C5, 0x037A, 0x033B, 0x0304, 0x02D3, 0x02A9, + 0x0283, 0x0261, 0x0243, 0x0227, 0x020E, 0x01F7, 0x01E2, 0x01CF, + 0x01BD, 0x01AC, 0x019D, 0x018F, 0x0182, 0x0175, 0x0169, 0x015E, + 0x0154, 0x014A, 0x0141, 0x0139, 0x0130, 0x0128, 0x0121, 0x011A, + 0x0113, 0x010D, 0x0107, 0x0101, 0x00FB, 0x00F6, 0x00F1, 0x00EC, + 0x00E7, 0x00E3, 0x00DE, 0x00DA, 0x00D6, 0x00D2, 0x00CE, 0x00CB, + 0x00C7, 0x00C4, 0x00C1, 0x00BD, 0x00BA, 0x00B7, 0x00B4, 0x00B2, + 0x00AF, 0x00AC, 0x00AA, 0x00A7, 0x00A5, 0x00A3, 0x00A0, 0x009E, + 0x009C, 0x009A, 0x0098, 0x0096, 0x0094, 0x0092, 0x0090, 0x008E, + 0x008D, 0x008B, 0x0089, 0x0088, 0x0086, 0x0085, 0x0083, 0x0082, + 0x0080, 0x007F, 0x007D, 0x007C, 0x007B, 0x0079, 0x0078, 0x0077, + 0x0076, 0x0074, 0x0073, 0x0072, 0x0071, 0x0070, 0x006F, 0x006E, + 0x006D, 0x006C, 0x006B, 0x006A, 0x0069, 0x0068, 0x0067, 0x0066, + 0x0065, 0x0064, 0x0063, 0x0062, 0x0062, 0x0061, 0x0060, 0x005F, + 0x005E, 0x005E, 0x005D, 0x005C, 0x005B, 0x005B, 0x005A +}; + +/* + * Temporary prototypes; keep until all files using math_util are decompiled. + */ +THUMB_FUNC s32 Sin(u16 degrees); +THUMB_FUNC s32 Cos(u16 degrees); +THUMB_FUNC s32 Sin_Wrap(u16 degrees); +THUMB_FUNC s32 Cos_Wrap(u16 degrees); +THUMB_FUNC u16 MathUtil_0201B9A0(u16 x); +THUMB_FUNC s32 Sin32(s32 degrees); + +THUMB_FUNC u32 GetLCRNGSeed(); +THUMB_FUNC void SetLCRNGSeed(u32 seed); +THUMB_FUNC u16 LCRandom(void); + +THUMB_FUNC u32 PRandom(u32 seed); + +THUMB_FUNC void SetMTRNGSeed(u32 seed); +THUMB_FUNC u32 MTRandom(void); + +THUMB_FUNC void MTX22_2DAffine(struct Mtx22 * mtx, u16 radians, fx32 x, fx32 y, u8 type); + +THUMB_FUNC s32 CircularDistance(s32 x1, s32 y1, s32 x2, s32 y2); +THUMB_FUNC s32 MathUtil_0201BC84(u16 arg0, s32 arg1); + +/* + * Trigonometric functions + */ +// Returns the sine of a 16-bit unsigned degree value. +THUMB_FUNC s32 Sin(u16 degrees) +{ + if (degrees >= 360) + return 0; + else + return gSineTable[degrees]; +} +// Returns the cosine of a 16-bit unsigned degree value. +THUMB_FUNC s32 Cos(u16 degrees) +{ + if (degrees >= 360) + return 0; + else + return gSineTable[degrees + 90]; +} + +// Returns the wrapped sine of a 16-bit unsigned degree value. +THUMB_FUNC s32 Sin_Wrap(u16 degrees) +{ + return gSineTable[degrees % 360]; +} + +// Returns the wrapped cosine of a 16-bit unsigned degree value. +THUMB_FUNC s32 Cos_Wrap(u16 degrees) +{ + return gSineTable[(degrees % 360) + 90]; +} + +// Purpose unknown. +THUMB_FUNC u16 MathUtil_0201B9A0(u16 x) +{ + return UNK_020EDC7E[x % 360]; +} + +// Returns the sine of a 32-bit signed degree value. +THUMB_FUNC s32 Sin32(s32 degrees) +{ + return gSineTable[(u16)(degrees >> 12) % 360]; +} + +/* + * Random number generators + */ +static u32 sMTRNG_State[624]; // Mersenne Twister seed storage/buffer +static union +{ + u32 LC_State; // Linear-congruential seed storage/buffer + u32 MTRNG_State[]; // Don't bother asking why Game Freak did this. Just don't. +} sRNGHack; + +// Returns the Linear-congruential buffer in full. +THUMB_FUNC u32 GetLCRNGSeed() +{ + return sRNGHack.LC_State; +} + +// Initializes the Linear-congruential buffer with a 32-bit seed. +THUMB_FUNC void SetLCRNGSeed(u32 seed) +{ + sRNGHack.LC_State = seed; +} + +// Calculates an unsigned 16-bit random integer using the Linear-congruential algorithm. +THUMB_FUNC u16 LCRandom(void) +{ + // cycle the RNG + sRNGHack.LC_State *= 0x41C64E6D; + sRNGHack.LC_State += 0x6073; + return (u16)(sRNGHack.LC_State / 65536); // shut up the compiler +} + +// Returns a cheap, psuedo-random unsigned 32-bit random integer from a seed. +THUMB_FUNC u32 PRandom(u32 seed) +{ + return seed * 1812433253 + 1; // seed from Mersenne Twister algorithm +} + +static s32 sMTRNG_Cycles = 625; // Mersenne Twister cycle counter, 625 default value +static u32 sMTRNG_XOR[2] = {0, 0x9908b0df}; // Mersenne Twister XOR mask table + +// Initializes the Mersenne Twister buffer with a 32-bit seed. +THUMB_FUNC void SetMTRNGSeed(u32 seed) +{ + sRNGHack.MTRNG_State[0+1] = seed; + + for (sMTRNG_Cycles = 1; sMTRNG_Cycles < 624; sMTRNG_Cycles++) + sMTRNG_State[sMTRNG_Cycles] = 1812433253 * (sMTRNG_State[sMTRNG_Cycles - 1] ^ (sMTRNG_State[sMTRNG_Cycles - 1] >> 30)) + sMTRNG_Cycles; +} + +// Calculates an unsigned 32-bit random integer using the Mersenne Twister algorithm. +THUMB_FUNC u32 MTRandom(void) +{ + u32 val; + s32 i; + + if (sMTRNG_Cycles >= 624) + { + if (sMTRNG_Cycles == 625) + SetMTRNGSeed(5489); + + for (i = 0; i < 227; i++) + { + val = (sMTRNG_State[i] & 0x80000000) | (sMTRNG_State[i + 1] & 0x7fffffff); + sMTRNG_State[i] = sMTRNG_State[i + 397] ^ (val >> 1) ^ sMTRNG_XOR[val & 0x1]; + } + for (; i < 623; i++) + { + val = (sMTRNG_State[i] & 0x80000000) | (sMTRNG_State[i + 1] & 0x7fffffff); + sMTRNG_State[i] = sMTRNG_State[i + -227] ^ (val >> 1) ^ sMTRNG_XOR[val & 0x1]; + } + + val = (sRNGHack.MTRNG_State[623+1] & 0x80000000) | (sRNGHack.MTRNG_State[0+1] & 0x7fffffff); + sRNGHack.MTRNG_State[623+1] = sRNGHack.MTRNG_State[396+1] ^ (val >> 1) ^ sMTRNG_XOR[val & 0x1]; + + sMTRNG_Cycles = 0; + } + + val = sMTRNG_State[sMTRNG_Cycles++]; // has to be this way in order to match + + val ^= val >> 11; + val ^= (val << 7) & 0x9d2c5680; + val ^= (val << 15) & 0xefc60000; + val ^= val >> 18; + + return val; +} + +/* + * Nitro FX specific functions + */ +// Rotates and scales a 2D plane by a number of degrees. +THUMB_FUNC void MTX22_2DAffine(struct Mtx22 * mtx, u16 radians, fx32 x, fx32 y, u8 type) +{ + if (type == 1) + radians = (u16)((u32)(radians * 65535) >> 8); // shut up the compiler + else if (type == 2) + radians = (u16)((u32)(radians * 65535) / 360); // shut up the compiler + + MTX_Rot22_(mtx, + UNK_020FFA38[((radians >> 4) * 2)], // TODO: macros + UNK_020FFA38[((radians >> 4) * 2) + 1]); // TODO: macros + MTX_ScaleApply22(mtx, mtx, x, y); +} + +/* + * Vector functions + */ +// Calculates the circular distance between two vector coordinates. +THUMB_FUNC s32 CircularDistance(s32 x1, s32 y1, s32 x2, s32 y2) +{ + struct Vecx32 v1, v2, v3, v4; + fx32 f1, f2; + s32 ret; + + // TODO: Code looks like it could have been macroized. + v1.x = x1 << FX32_INT_SHIFT; + v1.y = y1 << FX32_INT_SHIFT; + v1.z = 0; + + v2.x = x2 << FX32_INT_SHIFT; + v2.y = y2 << FX32_INT_SHIFT; + v2.z = 0; + + v4.x = 0; + v4.y = 0; + v4.z = FX32_MUL(v1.x, v2.y) - FX32_MUL(v2.x, v1.y); + + f1 = v4.x + v4.y + v4.z; + + v1.x = y1 << FX32_INT_SHIFT; + v1.y = x1 << FX32_INT_SHIFT; + v1.z = 0; + + VEC_Normalize(&v1, &v3); + + v1.x = x1 << FX32_INT_SHIFT; + v1.y = y1 << FX32_INT_SHIFT; + v1.z = 0; + + v2.x = x2 << FX32_INT_SHIFT; + v2.y = y2 << FX32_INT_SHIFT; + v2.z = 0; + + VEC_Subtract(&v2, &v1, &v4); + f2 = VEC_DotProduct(&v3, &v4); + ret = f2 >> FX32_INT_SHIFT; + ret = (ret < 0) ? -ret : ret; + + if (f1 <= 0) + ret *= -1; + return ret; +} + +THUMB_FUNC s32 MathUtil_0201BC84(u16 arg0, s32 arg1) +{ + return (arg1 * 65535) / + (FX32_MUL((arg0 * 2) << FX32_INT_SHIFT, FX32_CONST(3.140f)) >> FX32_INT_SHIFT); +} diff --git a/arm9/src/msgdata.c b/arm9/src/msgdata.c new file mode 100644 index 00000000..ae33e0f5 --- /dev/null +++ b/arm9/src/msgdata.c @@ -0,0 +1,400 @@ +#include "global.h" +#include "filesystem.h" +#include "msgdata.h" +#include "heap.h" +#include "MI_memory.h" +#include "string16.h" +#include "proto.h" + +#pragma thumb on + +static void * LoadSingleElementFromNarc(NarcId narc_id, s32 file_id, u32 heap_id); +static void FreeMsgDataRawData(void * data); +static void ReadMsgData_ExistingTable_ExistingArray(struct MsgDataTable * table, u32 num, u16 * dest); +static void ReadMsgData_NewNarc_ExistingArray(NarcId narc_id, u32 group, u32 num, u32 heap_id, u16 * dest); +static void CopyEncryptedMessage16(u16 * dest, const u16 * src, struct MsgDataAlloc * param); +static void ReadMsgData_ExistingTable_ExistingString(struct MsgDataTable * table, u32 num, struct String * dest); +static struct String * ReadMsgData_ExistingTable_NewString(struct MsgDataTable * table, u32 num, u32 heap_id); +static void ReadMsgData_ExistingNarc_ExistingString(NARC * narc, u32 group, u32 num, u32 heap_id, struct String * dest); +static struct String * ReadMsgData_ExistingNarc_NewString(NARC * narc, u32 group, u32 num, u32 heap_id); +static u16 GetMsgCount_ExistingTable(struct MsgDataTable * tbl); +static u16 GetMsgCount_TableFromNarc(NarcId narc_id, s32 file_id); + +static void * LoadSingleElementFromNarc(NarcId narc_id, s32 file_id, u32 heap_id) +{ + return AllocAndReadWholeNarcMemberByIdPair(narc_id, file_id, heap_id); +} + +static void FreeMsgDataRawData(void * data) +{ + FreeToHeap(data); +} + +inline static void Decrypt1(struct MsgDataAlloc * arg0, u32 arg1, u32 seed) +{ + seed = seed * 765 * (arg1 + 1) & 0xffff; + seed |= seed << 16; + arg0->offset ^= seed; + arg0->length ^= seed; +} + +inline static void Decrypt2(u16 * arg0, u32 count, u32 arg2) +{ + u16 seed = (u16)((arg2 + 1) * 596947); + + while (count-- > 0) + { + *arg0 ^= seed; + arg0++; + seed += 18749; + } +} + +static void ReadMsgData_ExistingTable_ExistingArray(struct MsgDataTable * table, u32 num, u16 * dest) +{ + struct MsgDataAlloc sp0; + + if (num < table->count) + { + sp0 = table->alloc[num]; + Decrypt1(&sp0, num, table->key); + + CopyEncryptedMessage16(dest, (const u16 *)((u8 *)table + sp0.offset), &sp0); + Decrypt2(dest, sp0.length, num); + } + else + { + GF_ASSERT(0); + } +} + +static void ReadMsgData_NewNarc_ExistingArray(NarcId narc_id, u32 group, u32 num, u32 heap_id, u16 * dest) +{ + NARC * narc = NARC_ctor(narc_id, heap_id); + u16 header[2]; + struct MsgDataAlloc alloc; + if (narc != NULL) + { + NARC_ReadFromMember(narc, group, 0, 4, header); + NARC_ReadFromMember(narc, group, 8 * num + 4, 8, &alloc); + Decrypt1(&alloc, num, header[1]); + NARC_ReadFromMember(narc, group, alloc.offset, 2 * alloc.length, dest); + Decrypt2(dest, alloc.length, num); + NARC_dtor(narc); + } +} + +static void CopyEncryptedMessage16(u16 * dest, const u16 * src, struct MsgDataAlloc * param) +{ + MI_CpuCopy16(src, dest, 2 * param->length); +} + +static void ReadMsgData_ExistingTable_ExistingString(struct MsgDataTable * table, u32 num, struct String * dest) +{ + struct MsgDataAlloc alloc; + u16 * buf; + if (num < table->count) + { + alloc = table->alloc[num]; + Decrypt1(&alloc, num, table->key); + buf = AllocFromHeapAtEnd(0, 2 * alloc.length); + if (buf != NULL) + { + MI_CpuCopy16((char *)table + alloc.offset, buf, 2 * alloc.length); + Decrypt2(buf, alloc.length, num); + FUN_02021E8C(dest, buf, alloc.length); + FreeToHeap(buf); + } + } + else + { + GF_ASSERT(0); + StringSetEmpty(dest); + } +} + +static struct String * ReadMsgData_ExistingTable_NewString(struct MsgDataTable * table, u32 num, u32 heap_id) +{ + struct MsgDataAlloc alloc; + u16 * buf; + struct String * dest; + if (num < table->count) + { + alloc = table->alloc[num]; + Decrypt1(&alloc, num, table->key); + buf = AllocFromHeapAtEnd(heap_id, 2 * alloc.length); + if (buf != NULL) + { + MI_CpuCopy16((char *)table + alloc.offset, buf, 2 * alloc.length); + Decrypt2(buf, alloc.length, num); + dest = String_ctor(alloc.length, heap_id); + if (dest != NULL) + FUN_02021E8C(dest, buf, alloc.length); + FreeToHeap(buf); + return dest; + } + else + { + return NULL; + } + } + else + { + GF_ASSERT(0); + return String_ctor(4, heap_id); + } +} + +void ReadMsgData_NewNarc_ExistingString(NarcId narc_id, u32 group, u32 num, u32 heap_id, struct String * dest) +{ + NARC * narc = NARC_ctor(narc_id, heap_id); + if (narc != NULL) + { + ReadMsgData_ExistingNarc_ExistingString(narc, group, num, heap_id, dest); + NARC_dtor(narc); + } +} + +static void ReadMsgData_ExistingNarc_ExistingString(NARC * narc, u32 group, u32 num, u32 heap_id, struct String * dest) +{ + u16 * buf; + u32 size; + u16 sp10[2]; + struct MsgDataAlloc alloc; + + NARC_ReadFromMember(narc, group, 0, 4, sp10); + if (num < sp10[0]) + { + NARC_ReadFromMember(narc, group, 8 * num + 4, 8, &alloc); + Decrypt1(&alloc, num, sp10[1]); + size = alloc.length * 2; + buf = AllocFromHeapAtEnd(heap_id, size); + if (buf != NULL) + { + NARC_ReadFromMember(narc, group, alloc.offset, size, buf); + Decrypt2(buf, alloc.length, num); + FUN_02021E8C(dest, buf, alloc.length); + FreeToHeap(buf); + return; + } + } + else + { + GF_ASSERT(0); + StringSetEmpty(dest); + } +} + +struct String * ReadMsgData_NewNarc_NewString(NarcId narc_id, u32 group, u32 num, u32 heap_id) +{ + NARC * narc = NARC_ctor(narc_id, heap_id); + struct String * string; + if (narc != NULL) + { + string = ReadMsgData_ExistingNarc_NewString(narc, group, num, heap_id); + NARC_dtor(narc); + } + else + { + string = String_ctor(4, heap_id); + } + return string; +} + +static struct String * ReadMsgData_ExistingNarc_NewString(NARC * narc, u32 group, u32 num, u32 heap_id) +{ + struct String * dest; + u16 * buf; + u32 size; + u16 sp10[2]; + struct MsgDataAlloc alloc; + + NARC_ReadFromMember(narc, group, 0, 4, sp10); + if (num < sp10[0]) + { + NARC_ReadFromMember(narc, group, 8 * num + 4, 8, &alloc); + Decrypt1(&alloc, num, sp10[1]); + dest = String_ctor(alloc.length, heap_id); + if (dest != NULL) + { + size = alloc.length * 2; + buf = AllocFromHeapAtEnd(heap_id, size); + if (buf != NULL) + { + NARC_ReadFromMember(narc, group, alloc.offset, size, buf); + Decrypt2(buf, alloc.length, num); + FUN_02021E8C(dest, buf, alloc.length); + FreeToHeap(buf); + } + } + return dest; + } + else + { + GF_ASSERT(0); + return String_ctor(4, heap_id); + } +} + +static u16 GetMsgCount_ExistingTable(struct MsgDataTable * tbl) +{ + return tbl->count; +} + +static u16 GetMsgCount_TableFromNarc(NarcId narc_id, s32 file_id) +{ + u16 n[2]; + ReadFromNarcMemberByIdPair(&n, narc_id, file_id, 0, 4); + return n[0]; +} + +struct MsgData * NewMsgDataFromNarc(u32 type, NarcId narc_id, s32 file_id, u32 heap_id) +{ + struct MsgData * msgData = AllocFromHeapAtEnd(heap_id, sizeof(struct MsgData)); + if (msgData != NULL) + { + if (type == 0) + { + msgData->data.raw = LoadSingleElementFromNarc(narc_id, file_id, heap_id); + if (msgData->data.raw == NULL) + { + FreeToHeap(msgData); + return NULL; + } + } + else + { + msgData->data.narc = NARC_ctor(narc_id, heap_id); + } + msgData->type = (u16)type; + msgData->narc_id = (u16)narc_id; + msgData->file_id = (u16)file_id; + msgData->heap_id = (u16)heap_id; + } + return msgData; +} + +void DestroyMsgData(struct MsgData * msgData) +{ + if (msgData != NULL) + { + switch (msgData->type) + { + case 0: + FreeMsgDataRawData(msgData->data.raw); + break; + case 1: + NARC_dtor(msgData->data.narc); + break; + } + FreeToHeap(msgData); + } +} + +void ReadMsgDataIntoString(struct MsgData * msgData, u32 msg_no, struct String * dest) +{ + switch (msgData->type) + { + case 0: + ReadMsgData_ExistingTable_ExistingString(msgData->data.raw, msg_no, dest); + break; + case 1: + ReadMsgData_ExistingNarc_ExistingString(msgData->data.narc, msgData->file_id, msg_no, msgData->heap_id, dest); + break; + } +} + +struct String * NewString_ReadMsgData(struct MsgData * msgData, u32 msg_no) +{ + switch (msgData->type) + { + case 0: + return ReadMsgData_ExistingTable_NewString(msgData->data.raw, msg_no, msgData->heap_id); + case 1: + return ReadMsgData_ExistingNarc_NewString(msgData->data.narc, msgData->file_id, msg_no, msgData->heap_id); + default: + return NULL; + } +} + +u16 MsgDataGetCount(struct MsgData * msgData) +{ + switch (msgData->type) + { + case 0: + return GetMsgCount_ExistingTable(msgData->data.raw); + case 1: + return GetMsgCount_TableFromNarc((NarcId)msgData->narc_id, msgData->file_id); + default: + return 0; + } +} + +void ReadMsgDataIntoU16Array(struct MsgData * msgData, u32 msg_no, u16 * dest) +{ + switch (msgData->type) + { + case 0: + ReadMsgData_ExistingTable_ExistingArray(msgData->data.raw, msg_no, dest); + break; + case 1: + ReadMsgData_NewNarc_ExistingArray((NarcId)msgData->narc_id, msgData->file_id, msg_no, msgData->heap_id, dest); + break; + } +} + +void GetSpeciesNameIntoArray(u16 species, u32 heap_id, u16 * dest) +{ + struct MsgData * msgData = NewMsgDataFromNarc(1, NARC_MSGDATA_MSG, 362, heap_id); + ReadMsgDataIntoU16Array(msgData, species, dest); + DestroyMsgData(msgData); +} + +struct String * ReadMsgData_ExpandPlaceholders(u32 * a0, struct MsgData * msgData, u32 msgno, u32 a3) +{ + struct String * ret = NULL; + struct String * r4 = String_ctor(1024, 0); + struct String * r5; + if (r4 != NULL) + { + r5 = NewString_ReadMsgData(msgData, msgno); + if (r5 != NULL) + { + StringExpandPlaceholders(a0, r4, r5); + ret = StringDup(r4, a3); + String_dtor(r5); + } + String_dtor(r4); + } + return ret; +} + +struct String * GetMoveName(u32 move, u32 heapno) +{ + struct MsgData * msgData = NewMsgDataFromNarc(1, NARC_MSGDATA_MSG, 588, heapno); + struct String * ret; + if (msgData != NULL) + { + ret = String_ctor(16, heapno); + if (ret != NULL) + { + ReadMsgDataIntoString(msgData, move, ret); + } + DestroyMsgData(msgData); + return ret; + } + return NULL; +} + +struct String * GetSpeciesName(u16 species, u32 heap_id) +{ + struct String * ret; + struct MsgData * msgData = NewMsgDataFromNarc(1, NARC_MSGDATA_MSG, 362, heap_id); + if (msgData != NULL) + { + ret = NewString_ReadMsgData(msgData, species); + DestroyMsgData(msgData); + return ret; + } + return NULL; +} diff --git a/arm9/src/nutdata.c b/arm9/src/nutdata.c new file mode 100644 index 00000000..a8f916e2 --- /dev/null +++ b/arm9/src/nutdata.c @@ -0,0 +1,78 @@ +#include "global.h" +#include "filesystem.h" +#include "itemtool.h" +#include "msgdata.h" + +#pragma thumb on + +NARC * OpenNutsDataNarc(u32 heap_id) +{ + return NARC_ctor(NARC_ITEMTOOL_ITEMDATA_NUTS_DATA, heap_id); +} + +struct NutData * ReadNutDataFromNarc(NARC * narc, u32 berry_idx, u32 heap_id) +{ + return NARC_AllocAndReadWholeMember(narc, berry_idx, heap_id); +} + +void CloseNutsDataNarc(NARC * narc) +{ + NARC_dtor(narc); +} + +struct NutData * LoadNutDataSingle(s32 berry_idx, u32 heap_id) +{ + return AllocAndReadWholeNarcMemberByIdPair(NARC_ITEMTOOL_ITEMDATA_NUTS_DATA, berry_idx, heap_id); +} + +struct NutData * LoadNutDataSingleByItemId(s32 item_id, u32 heap_id) +{ + return LoadNutDataSingle(item_id - FIRST_BERRY_IDX, heap_id); +} + +u32 GetNutAttr(struct NutData * nut, u32 attr) +{ + switch (attr) + { + case 0: + return nut->unk0; + case 1: + return nut->unk2; + case 2: + return nut->unk3; + case 3: + return nut->unk4; + case 4: + return nut->unk5; + case 5: + return nut->unk6; + case 6: + return nut->unk7; + case 7: + return nut->unk8; + case 8: + return nut->unk9; + case 9: + return nut->unkA; + case 10: + return nut->unkB; + default: + return 0; + } +} + +struct String * GetNutName(u32 berry_idx, u32 heap_id) +{ + struct MsgData * msgData = NewMsgDataFromNarc(1, NARC_MSGDATA_MSG, 373, heap_id); + struct String * ret = NewString_ReadMsgData(msgData, berry_idx); + DestroyMsgData(msgData); + return ret; +} + +struct String * GetNutDesc(u32 berry_idx, u32 heap_id) +{ + struct MsgData * msgData = NewMsgDataFromNarc(1, NARC_MSGDATA_MSG, 372, heap_id); + struct String * ret = NewString_ReadMsgData(msgData, berry_idx); + DestroyMsgData(msgData); + return ret; +} diff --git a/arm9/src/pokemon.c b/arm9/src/pokemon.c index e342bd0f..a15d20c1 100644 --- a/arm9/src/pokemon.c +++ b/arm9/src/pokemon.c @@ -10,6 +10,7 @@ #include "move_data.h" #include "string_util.h" #include "text.h" +#include "msgdata.h" #include "constants/abilities.h" #include "constants/items.h" #include "constants/moves.h" @@ -281,14 +282,14 @@ void CreateBoxMon(struct BoxPokemon * boxPokemon, int species, int level, int fi decry = AcquireBoxMonLock(boxPokemon); if (hasFixedPersonality == 0) { - fixedPersonality = (rand_LC() | (rand_LC() << 16)); + fixedPersonality = (LCRandom() | (LCRandom() << 16)); } SetBoxMonData(boxPokemon, MON_DATA_PERSONALITY, &fixedPersonality); if (otIdType == 2) { do { - fixedOtId = (rand_LC() | (rand_LC() << 16)); + fixedOtId = (LCRandom() | (LCRandom() << 16)); } while (SHINY_CHECK(fixedOtId, fixedPersonality)); } else if (otIdType != 1) @@ -316,14 +317,14 @@ void CreateBoxMon(struct BoxPokemon * boxPokemon, int species, int level, int fi } else { - exp = rand_LC(); + exp = LCRandom(); iv = exp & 0x1F; SetBoxMonData(boxPokemon, MON_DATA_HP_IV, &iv); iv = (exp & 0x3E0) >> 5; SetBoxMonData(boxPokemon, MON_DATA_ATK_IV, &iv); iv = (exp & 0x7C00) >> 10; SetBoxMonData(boxPokemon, MON_DATA_DEF_IV, &iv); - exp = rand_LC(); + exp = LCRandom(); iv = exp & 0x1F; SetBoxMonData(boxPokemon, MON_DATA_SPEED_IV, &iv); iv = (exp & 0x3E0) >> 5; @@ -353,7 +354,7 @@ void CreateMonWithNature(struct Pokemon * pokemon, u16 species, u8 level, u8 fix u32 personality; do { - personality = (u32)(rand_LC() | (rand_LC() << 16)); + personality = (u32)(LCRandom() | (LCRandom() << 16)); } while (nature != GetNatureFromPersonality(personality)); CreateMon(pokemon, (int)species, (int)level, (int)fixedIv, 1, (int)personality, (int)0, (int)0); } @@ -366,7 +367,7 @@ void CreateMonWithGenderNatureLetter(struct Pokemon * pokemon, u16 species, u8 l if (letter != 0 && letter < 29) { do { - pid = (u32)(rand_LC() | (rand_LC() << 16)); + pid = (u32)(LCRandom() | (LCRandom() << 16)); test = (u16)CALC_UNOWN_LETTER(pid); } while (nature != GetNatureFromPersonality(pid) || gender != GetGenderBySpeciesAndPersonality(species, pid) || test != letter - 1); } @@ -843,7 +844,7 @@ u32 GetBoxMonDataInternal(struct BoxPokemon * boxmon, int attr, void * dest) break; case MON_DATA_NICKNAME: if (boxmon->checksum_fail) - GetSpeciesName(SPECIES_MANAPHY_EGG, 0, dest); + GetSpeciesNameIntoArray(SPECIES_MANAPHY_EGG, 0, dest); else { u16 * dest16 = (u16 *)dest; @@ -860,9 +861,9 @@ u32 GetBoxMonDataInternal(struct BoxPokemon * boxmon, int attr, void * dest) case MON_DATA_NICKNAME_3: if (boxmon->checksum_fail) { - u16 * buffer = FUN_0200AA50(SPECIES_MANAPHY_EGG, 0); - FUN_02021A74(dest, buffer); - FUN_02021A20(buffer); + struct String * buffer = GetSpeciesName(SPECIES_MANAPHY_EGG, 0); + StringCopy(dest, buffer); + String_dtor(buffer); } else { @@ -978,7 +979,7 @@ u32 GetBoxMonDataInternal(struct BoxPokemon * boxmon, int attr, void * dest) } break; case MON_DATA_SPECIES_NAME: - GetSpeciesName(blockA->species, 0, dest); + GetSpeciesNameIntoArray(blockA->species, 0, dest); break; } return ret; @@ -1090,7 +1091,7 @@ void SetBoxMonDataInternal(struct BoxPokemon * boxmon, int attr, void * value) u16 namebuf[POKEMON_NAME_LENGTH + 1]; u16 namebuf2[POKEMON_NAME_LENGTH + 1]; u16 namebuf3[POKEMON_NAME_LENGTH + 1]; - u16 * speciesName; + struct String * speciesName; PokemonDataBlockA *blockA = &GetSubstruct(boxmon, boxmon->pid, 0)->blockA; PokemonDataBlockB *blockB = &GetSubstruct(boxmon, boxmon->pid, 1)->blockB; @@ -1309,7 +1310,7 @@ void SetBoxMonDataInternal(struct BoxPokemon * boxmon, int attr, void * value) blockB->Unused = VALUE(u16); break; case MON_DATA_NICKNAME_2: - GetSpeciesName(blockA->species, 0, namebuf); + GetSpeciesNameIntoArray(blockA->species, 0, namebuf); blockB->isNicknamed = StringNotEqual(namebuf, value); // fallthrough case MON_DATA_NICKNAME: @@ -1319,7 +1320,7 @@ void SetBoxMonDataInternal(struct BoxPokemon * boxmon, int attr, void * value) } break; case MON_DATA_NICKNAME_4: - GetSpeciesName(blockA->species, 0, namebuf2); + GetSpeciesNameIntoArray(blockA->species, 0, namebuf2); FUN_02021EF0(value, namebuf3, POKEMON_NAME_LENGTH + 1); blockB->isNicknamed = StringNotEqual(namebuf2, namebuf3); // fallthrough @@ -1420,9 +1421,9 @@ void SetBoxMonDataInternal(struct BoxPokemon * boxmon, int attr, void * value) blockB->spdefIV = (VALUE(u32) >> 25) & 0x1F; break; case MON_DATA_SPECIES_NAME: - speciesName = FUN_0200AA50(blockA->species, 0); + speciesName = GetSpeciesName(blockA->species, 0); FUN_02021EF0(speciesName, blockC->nickname, POKEMON_NAME_LENGTH + 1); - FUN_02021A20(speciesName); + String_dtor(speciesName); break; } #undef VALUE @@ -2052,7 +2053,7 @@ void MonApplyFriendshipMod(struct Pokemon * pokemon, u32 kind, u32 location) s16 friendship; s8 mod; - if (kind == 5 && (rand_LC() & 1)) + if (kind == 5 && (LCRandom() & 1)) return; species = (u16)GetMonData(pokemon, MON_DATA_SPECIES2, NULL); @@ -2146,18 +2147,18 @@ u32 GenerateShinyPersonality(u32 otid) u16 r6; u16 r5; otid = (u32)((((otid & 0xFFFF0000) >> 16) ^ (otid & 0xFFFF)) >> 3u); - r6 = (u16)(rand_LC() & 7); - r5 = (u16)(rand_LC() & 7); + r6 = (u16)(LCRandom() & 7); + r5 = (u16)(LCRandom() & 7); for (r4 = 0; r4 < 13; r4++) { if (MaskOfFlagNo(r4) & otid) { - if (rand_LC() & 1) + if (LCRandom() & 1) r6 |= MaskOfFlagNo(r4 + 3); else r5 |= MaskOfFlagNo(r4 + 3); } - else if (rand_LC() & 1) + else if (LCRandom() & 1) { r6 |= MaskOfFlagNo(r4 + 3); r5 |= MaskOfFlagNo(r4 + 3); @@ -2859,7 +2860,7 @@ void FUN_02069718(struct BoxPokemon * boxmon, u16 move) } moves[3] = move; - pp[3] = (u8)GetWazaAttr(move, 5); + pp[3] = (u8)GetWazaAttr(move, MOVEATTR_PP); ppUp[3] = 0; for (i = 0; i < 4; i++) @@ -3089,21 +3090,21 @@ void FUN_02069C4C(struct PlayerParty * party) int idx; struct Pokemon * pokemon; u8 sp0; - switch (rand_LC()) + switch (LCRandom()) { case 0x4000: case 0x8000: case 0xC000: do { - idx = rand_LC() % count; + idx = LCRandom() % count; pokemon = GetPartyMonByIndex(party, idx); } while (GetMonData(pokemon, MON_DATA_SPECIES, NULL) == SPECIES_NONE || GetMonData(pokemon, MON_DATA_IS_EGG, NULL)); if (!FUN_02069CF4(party, (u8)MaskOfFlagNo(idx))) { do { - sp0 = (u8)rand_LC(); + sp0 = (u8)LCRandom(); } while (!(sp0 & 7)); if (sp0 & 0xF0) sp0 &= 7; @@ -3178,7 +3179,7 @@ void FUN_02069DC8(struct PlayerParty * party) int i; struct Pokemon * pokemon; u8 pokerus; - if ((rand_LC() % 3) == 0) + if ((LCRandom() % 3) == 0) { for (i = 0; i < count; i++) { @@ -3349,7 +3350,7 @@ void FUN_0206A094(struct Pokemon * pokemon, u32 a1, u32 a2) u16 item1; u16 item2; if (!(a1 & 0x81)) { - chance = (u32)(rand_LC() % 100); + chance = (u32)(LCRandom() % 100); species = (u16)GetMonData(pokemon, MON_DATA_SPECIES, 0); forme = (u16)GetMonData(pokemon, MON_DATA_FORME, 0); item1 = (u16)GetMonBaseStat_HandleFormeConversion(species, forme, BASE_ITEM_1); @@ -3682,13 +3683,13 @@ BOOL FUN_0206A9AC(struct BoxPokemon * boxmon, struct SaveBlock2 * sb2, u32 heap_ u32 myGender = FUN_020239CC(sb2); u32 otGender = GetBoxMonData(boxmon, MON_DATA_MET_GENDER, NULL); struct String * r7 = FUN_020239A0(sb2, heap_id); - struct String * r6 = FUN_020219F4(OT_NAME_LENGTH + 1, heap_id); + struct String * r6 = String_ctor(OT_NAME_LENGTH + 1, heap_id); BOOL ret = FALSE; GetBoxMonData(boxmon, MON_DATA_OT_NAME_2, r6); if (myId == otId && myGender == otGender && FUN_02021CE0(r7, r6) == 0) ret = TRUE; - FUN_02021A20(r6); - FUN_02021A20(r7); + String_dtor(r6); + String_dtor(r7); return ret; } diff --git a/arm9/src/string_util.c b/arm9/src/string_util.c index 54ad61fc..19ed5fa3 100644 --- a/arm9/src/string_util.c +++ b/arm9/src/string_util.c @@ -32,7 +32,7 @@ const s32 gPowersOfTen[] = { 1000000000,
};
-THUMB_FUNC void StringCopy(u16 *dest, const u16 *src)
+THUMB_FUNC void CopyU16StringArray(u16 *dest, const u16 *src)
{
u16 c = *src;
while (c != EOS) {
@@ -44,7 +44,7 @@ THUMB_FUNC void StringCopy(u16 *dest, const u16 *src) *dest = EOS;
}
-THUMB_FUNC u16 *StringCopyN(u16 *dest, const u16 *src, u32 num)
+THUMB_FUNC u16 *CopyU16StringArrayN(u16 *dest, const u16 *src, u32 num)
{
u32 copied = 0;
if (num > copied) {
diff --git a/arm9/src/unk_0201B8B8.c b/arm9/src/unk_0201B8B8.c new file mode 100644 index 00000000..abb92067 --- /dev/null +++ b/arm9/src/unk_0201B8B8.c @@ -0,0 +1,33 @@ +#include "global.h" +#include "unk_0201B8B88.h" + +#pragma thumb on + +const u16 * FUN_0201B8B8(const u16 * r4) +{ + GF_ASSERT(*r4 == 0xFFFE); + if (*r4 == 0xFFFE) { + u16 r0 = r4[2]; + r4 += 3; + r4 += r0; + } + return r4; +} + +u16 FUN_0201B8E0(const u16 * r4) +{ + GF_ASSERT(*r4 == 0xFFFE); + return r4[1]; +} + +BOOL FUN_0201B8F8(const u16 * r4) +{ + return (FUN_0201B8E0(r4) & 0xFF00) == 0x100; +} + +u16 FUN_0201B914(const u16 * r5, u32 r4) +{ + GF_ASSERT(*r5 == 0xFFFE); + GF_ASSERT(r4 < r5[2]); + return r5[3 + r4]; +} diff --git a/arm9/src/waza.c b/arm9/src/waza.c index 1ed8574e..f394fedd 100644 --- a/arm9/src/waza.c +++ b/arm9/src/waza.c @@ -15,7 +15,7 @@ void LoadAllWazaTbl(struct WazaTbl * dest) ReadFromNarcMemberByIdPair(dest, NARC_POKETOOL_WAZA_WAZA_TBL, 0, 0, (NUM_MOVES + 1) * sizeof(struct WazaTbl)); } -u32 GetWazaAttr(u16 waza, u32 attr) +u32 GetWazaAttr(u16 waza, MoveAttr attr) { struct WazaTbl wazaTbl; LoadWazaEntry(waza, &wazaTbl); @@ -27,38 +27,40 @@ u8 WazaGetMaxPp(u16 waza, u8 ppUp) u8 pp; if (ppUp > 3) ppUp = 3; - pp = GetWazaAttr(waza, 5); - return pp + (pp * 20 * ppUp) / 100; + pp = (u8)GetWazaAttr(waza, MOVEATTR_PP); + return (u8)(pp + (pp * 20 * ppUp) / 100); } -u32 GetAttrFromWazaTbl(struct WazaTbl * wazaTbl, u32 attr) +u32 GetAttrFromWazaTbl(struct WazaTbl * wazaTbl, MoveAttr attr) { switch (attr) { - case 0: - return wazaTbl->unk0; - case 1: + case MOVEATTR_EFFECT: + return wazaTbl->effect; + case MOVEATTR_UNK1: return wazaTbl->unk2; - case 2: - return wazaTbl->unk3; - case 3: - return wazaTbl->unk4; - case 4: - return wazaTbl->unk5; - case 5: + case MOVEATTR_POWER: + return wazaTbl->power; + case MOVEATTR_TYPE: + return wazaTbl->type; + case MOVEATTR_ACCURACY: + return wazaTbl->accuracy; + case MOVEATTR_PP: return wazaTbl->pp; - case 6: - return wazaTbl->unk7; - case 7: + case MOVEATTR_EFFECT_CHANCE: + return wazaTbl->effectChance; + case MOVEATTR_UNK7: return wazaTbl->unk8; - case 8: - return wazaTbl->unkA; - case 9: + case MOVEATTR_PRIORTY: + return wazaTbl->priority; + case MOVEATTR_UNK9: return wazaTbl->unkB; - case 10: + case MOVEATTR_UNK10: return wazaTbl->unkC; - case 11: - return wazaTbl->unkD; + case MOVEATTR_CONTEST_TYPE: + return wazaTbl->contestType; + default: + return (u32)wazaTbl; } } |