Skip to content

Commit

Permalink
Merge pull request #231 from DizzyEggg/sub_805D8C8
Browse files Browse the repository at this point in the history
Match/Document sub_805D8C8 and fix fakematches with static inlines
  • Loading branch information
AnonymousRandomPerson authored Oct 20, 2024
2 parents 25436f0 + 4d16a26 commit 9e49584
Show file tree
Hide file tree
Showing 27 changed files with 1,010 additions and 2,540 deletions.
5 changes: 1 addition & 4 deletions data/data_8106A4C.s
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ gUnknown_8106AC8: @ 8106AC8

.global gUnknown_8106AE8
gUnknown_8106AE8: @ 8106AE8
.byte 0x00, 0x00, 0x01, 0x00

.global gUnknown_8106AEC
gUnknown_8106AEC: @ 8106AEC
.byte 0x00, 0x00, 0x01, 0x00
.byte 0x12, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x13, 0x02, 0x00, 0x00
.byte 0x00, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x14, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
.byte 0x01, 0x00, 0xff, 0xff, 0x13, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff
Expand Down
7 changes: 7 additions & 0 deletions include/global.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@
// Prevent cross-jump optimization.
#define BLOCK_CROSS_JUMP asm("");

// Sometimes incrementing and decrementing a variable changes how registers are allocated, which helps with matching functions. Functionality-wise this doesn't do anything.
#ifdef NONMATCHING
#define ASM_MATCH_TRICK(a) {;}
#else
#define ASM_MATCH_TRICK(a) {a++;a--;}
#endif // NONMATCHING

// to help in decompiling
#define asm_comment(x) asm volatile("@ -- " x " -- ")

Expand Down
13 changes: 5 additions & 8 deletions include/items.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,6 @@ s32 SaveTeamInventory(u8 *, u32 size);
s32 WriteHighDecimal(s32, u8 *strbuf, u8);
u32 sub_80913E0(Item *slot, u32, struct subStruct_203B240 **);

// This macro doesn't do anything, it creates unused variables for the purpose of tricking the compiler and matching the asm.
// It's possible there was some unknown macro, or the structure of TeamInventory/Item is not 100% how it was originally written.
#define DUMMY_TEAM_ITEMS_ASM_MATCH(id) \
{ \
UNUSED size_t offs = id * sizeof(Item); \
UNUSED Item *item = &gTeamInventoryRef->teamItems[id]; \
}

static inline void ZeroOutItem(Item *item)
{
item->id = 0;
Expand All @@ -108,6 +100,11 @@ static inline bool8 ItemExists(Item *item)
return (item->flags & ITEM_FLAG_EXISTS);
}

static inline bool8 ItemSet(Item *item)
{
return (item->flags & ITEM_FLAG_SET);
}

static inline bool8 ItemInShop(Item *item)
{
return (item->flags & ITEM_FLAG_IN_SHOP);
Expand Down
12 changes: 11 additions & 1 deletion include/moves.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ typedef struct MoveDataFile
} MoveDataFile;

bool8 CanBeSnatched(u16 moveID);
void CopyAndResetMoves(Move *destMoves, Move *srcMoves);
void CopyAndResetMoves(Moves *destMoves, Move *srcMoves);
void CopyBareMoveData(Move *destMoves, Move *srcMoves);
bool8 DoesMoveCharge(u16 move);
bool8 FailsWhileMuzzled(u16 moveID);
Expand Down Expand Up @@ -94,6 +94,16 @@ static inline bool8 MoveFlagExists(Move *move)
return (move->moveFlags & MOVE_FLAG_EXISTS);
}

static inline bool8 MoveFlagSet(Move *move)
{
return (move->moveFlags & MOVE_FLAG_SET);
}

static inline bool8 MoveFlagLastUsed(Move *move)
{
return (move->moveFlags & MOVE_FLAG_LAST_USED);
}

static inline bool8 MoveFlagLinkChain(Move *move)
{
return (move->moveFlags & MOVE_FLAG_SUBSEQUENT_IN_LINK_CHAIN);
Expand Down
7 changes: 7 additions & 0 deletions include/pokemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,11 @@ static inline bool8 IsMonPartner(PokemonStruct1 *mon)
return (mon->dungeonLocation.id == DUNGEON_JOIN_LOCATION_PARTNER);
}

// Needed to match a couple of functions which use species id.
// The theory is that there was some internal species conversion which got scrapped later on.
static inline s16 SpeciesId(s32 id)
{
return id;
}

#endif // GUARD_POKEMON_H
34 changes: 17 additions & 17 deletions include/sprite_oam.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,72 +22,72 @@ typedef struct SpriteOAM

#define SPRITEOAM_MAX_Y 0xFF
#define SPRITEOAM_SHIFT_Y 0
#define SPRITEOAM_MASK_Y (SPRITEOAM_MAX_Y << SPRITEOAM_SHIFT_Y)
#define SPRITEOAM_MASK_Y (SPRITEOAM_MAX_Y << SPRITEOAM_SHIFT_Y) // ~ 0xFF00

#define SPRITEOAM_MAX_AFFINEMODE1 1
#define SPRITEOAM_SHIFT_AFFINEMODE1 8
#define SPRITEOAM_MASK_AFFINEMODE1 (SPRITEOAM_MAX_AFFINEMODE1 << SPRITEOAM_SHIFT_AFFINEMODE1)
#define SPRITEOAM_MASK_AFFINEMODE1 (SPRITEOAM_MAX_AFFINEMODE1 << SPRITEOAM_SHIFT_AFFINEMODE1) // ~ 0xFEFF

#define SPRITEOAM_MAX_AFFINEMODE2 1
#define SPRITEOAM_SHIFT_AFFINEMODE2 9
#define SPRITEOAM_MASK_AFFINEMODE2 (SPRITEOAM_MAX_AFFINEMODE2 << SPRITEOAM_SHIFT_AFFINEMODE2)
#define SPRITEOAM_MASK_AFFINEMODE2 (SPRITEOAM_MAX_AFFINEMODE2 << SPRITEOAM_SHIFT_AFFINEMODE2) // ~ 0xFDFF

#define SPRITEOAM_MAX_OBJMODE 3
#define SPRITEOAM_SHIFT_OBJMODE 10
#define SPRITEOAM_MASK_OBJMODE (SPRITEOAM_MAX_OBJMODE << SPRITEOAM_SHIFT_OBJMODE)
#define SPRITEOAM_MASK_OBJMODE (SPRITEOAM_MAX_OBJMODE << SPRITEOAM_SHIFT_OBJMODE) // ~ 0xF3FF

#define SPRITEOAM_MAX_MOSAIC 1
#define SPRITEOAM_SHIFT_MOSAIC 12
#define SPRITEOAM_MASK_MOSAIC (SPRITEOAM_MAX_MOSAIC << SPRITEOAM_SHIFT_MOSAIC)
#define SPRITEOAM_MASK_MOSAIC (SPRITEOAM_MAX_MOSAIC << SPRITEOAM_SHIFT_MOSAIC) // ~ 0xEFFF

#define SPRITEOAM_MAX_BPP 1
#define SPRITEOAM_SHIFT_BPP 13
#define SPRITEOAM_MASK_BPP (SPRITEOAM_MAX_BPP << SPRITEOAM_SHIFT_BPP)
#define SPRITEOAM_MASK_BPP (SPRITEOAM_MAX_BPP << SPRITEOAM_SHIFT_BPP) // ~ 0xDFFF

#define SPRITEOAM_MAX_SHAPE 3
#define SPRITEOAM_SHIFT_SHAPE 14
#define SPRITEOAM_MASK_SHAPE (SPRITEOAM_MAX_SHAPE << SPRITEOAM_SHIFT_SHAPE)
#define SPRITEOAM_MASK_SHAPE (SPRITEOAM_MAX_SHAPE << SPRITEOAM_SHIFT_SHAPE) // ~ 0x3FFF

// attrib2

#define SPRITEOAM_MAX_X 0x1FF
#define SPRITEOAM_SHIFT_X 0
#define SPRITEOAM_MASK_X (SPRITEOAM_MAX_X << SPRITEOAM_SHIFT_X)
#define SPRITEOAM_MASK_X (SPRITEOAM_MAX_X << SPRITEOAM_SHIFT_X) // ~ 0xFE00

#define SPRITEOAM_MAX_MATRIXNUM 31
#define SPRITEOAM_SHIFT_MATRIXNUM 9
#define SPRITEOAM_MASK_MATRIXNUM (SPRITEOAM_MAX_MATRIXNUM << SPRITEOAM_SHIFT_MATRIXNUM)
#define SPRITEOAM_MASK_MATRIXNUM (SPRITEOAM_MAX_MATRIXNUM << SPRITEOAM_SHIFT_MATRIXNUM) // ~ 0xC1FF

#define SPRITEOAM_MAX_SIZE 3
#define SPRITEOAM_SHIFT_SIZE 14
#define SPRITEOAM_MASK_SIZE (SPRITEOAM_MAX_SIZE << SPRITEOAM_SHIFT_SIZE)
#define SPRITEOAM_MASK_SIZE (SPRITEOAM_MAX_SIZE << SPRITEOAM_SHIFT_SIZE) // ~ 0x3FFF

// attrib3

#define SPRITEOAM_MAX_TILENUM 0x3FF
#define SPRITEOAM_SHIFT_TILENUM 0
#define SPRITEOAM_MASK_TILENUM (SPRITEOAM_MAX_TILENUM << SPRITEOAM_SHIFT_TILENUM)
#define SPRITEOAM_MASK_TILENUM (SPRITEOAM_MAX_TILENUM << SPRITEOAM_SHIFT_TILENUM) // ~ 0xFC00

#define SPRITEOAM_MAX_PRIORITY 3
#define SPRITEOAM_SHIFT_PRIORITY 10
#define SPRITEOAM_MASK_PRIORITY (SPRITEOAM_MAX_PRIORITY << SPRITEOAM_SHIFT_PRIORITY)
#define SPRITEOAM_MASK_PRIORITY (SPRITEOAM_MAX_PRIORITY << SPRITEOAM_SHIFT_PRIORITY) // ~ 0xF3FF

#define SPRITEOAM_MAX_PALETTENUM 15
#define SPRITEOAM_SHIFT_PALETTENUM 12
#define SPRITEOAM_MASK_PALETTENUM (SPRITEOAM_MAX_PALETTENUM << SPRITEOAM_SHIFT_PALETTENUM)
#define SPRITEOAM_MASK_PALETTENUM (SPRITEOAM_MAX_PALETTENUM << SPRITEOAM_SHIFT_PALETTENUM) // ~ 0xFFF

// unk6

#define SPRITEOAM_MAX_UNK6_0 1
#define SPRITEOAM_SHIFT_UNK6_0 0
#define SPRITEOAM_MASK_UNK6_0 (SPRITEOAM_MAX_UNK6_0 << SPRITEOAM_SHIFT_UNK6_0)
#define SPRITEOAM_MASK_UNK6_0 (SPRITEOAM_MAX_UNK6_0 << SPRITEOAM_SHIFT_UNK6_0) // ~ 0xFFFE

#define SPRITEOAM_MAX_UNK6_1 1
#define SPRITEOAM_SHIFT_UNK6_1 1
#define SPRITEOAM_MASK_UNK6_1 (SPRITEOAM_MAX_UNK6_1 << SPRITEOAM_SHIFT_UNK6_1)
#define SPRITEOAM_MASK_UNK6_1 (SPRITEOAM_MAX_UNK6_1 << SPRITEOAM_SHIFT_UNK6_1) // ~ 0xFFFD

#define SPRITEOAM_MAX_UNK6_4 0xFFF
#define SPRITEOAM_SHIFT_UNK6_4 4
#define SPRITEOAM_MASK_UNK6_4 (SPRITEOAM_MAX_UNK6_4 << SPRITEOAM_SHIFT_UNK6_4)
#define SPRITEOAM_MASK_UNK6_4 (SPRITEOAM_MAX_UNK6_4 << SPRITEOAM_SHIFT_UNK6_4) // ~ 0xF

#endif // GUARD_SPRITE_OAM_H
#endif // GUARD_SPRITE_OAM_H
27 changes: 26 additions & 1 deletion include/structs/dungeon_entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ typedef struct EntityInfo
/* 0xF8 */ bool8 speedStageChanged; // Toggled when pokemon is movement speed is sped up
/* 0xF9 */ u8 unkF9;
/* 0xFA */ u8 terrifiedTurns; // Doubles as a bool for whether the Pokémon is terrified.
u8 unkFB;
u8 expMultiplier;
// Set to true if the player makes a teammate use their held item.
// This is done by going to the teammate's held item in the toolbox and selecting "Use".
/* 0xFC */ bool8 useHeldItem;
Expand Down Expand Up @@ -418,6 +418,31 @@ enum VisualFlag
VISUAL_FLAG_RUN_AWAY = 2
};

enum ExpMultiplier
{
EXP_HALVED, // 0.5x when fainting a mon with only the regular attack and no moves
EXP_REGULAR, // 1x
EXP_BOOSTED, // 1.5x so far use not found, but most likely linked moves ; to be confirmed once more code is decompiled
};

static inline bool8 ExpMultiplierChanged(EntityInfo *info)
{
return (info->expMultiplier != EXP_HALVED);
}

static inline void SetRegularExpMultiplier(EntityInfo *info)
{
info->expMultiplier = EXP_REGULAR;
}

// After using a move, pokemon will get more experience than
// This inline is needed to match one function - sub_805AD54
static inline void SetExpMultplier(EntityInfo *info)
{
if (!ExpMultiplierChanged(info))
SetRegularExpMultiplier(info);
}

static inline EntityInfo *GetEntInfo(Entity *ent)
{
return ent->info;
Expand Down
6 changes: 0 additions & 6 deletions src/code_803D110.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,6 @@ void sub_803D8F0(void)
}
}

// Hmm...
static inline s16 SpeciesId(s32 id)
{
return id;
}

bool8 sub_803D930(s16 speciesToFind)
{
s32 i;
Expand Down
4 changes: 1 addition & 3 deletions src/code_803E724.c
Original file line number Diff line number Diff line change
Expand Up @@ -1399,9 +1399,7 @@ void sub_803FE30(s32 a0, u16 *a1, bool8 a2, bool8 a3)
s32 arrId = (!a2) ? 1 : 0;

for (i = 0; i < 9; i++) {
#ifndef NONMATCHING
a1++;a1--; // Good old matching trick.
#endif // NONMATCHING
ASM_MATCH_TRICK(a1);
if (gUnknown_80F64B4[i] <= a0) {
*a1 = gUnknown_80F64D8[arrId][i];
*ptr = (((a0 - gUnknown_80F64B4[i]) + varAdd) + 0x258) | 0xF000;
Expand Down
Loading

0 comments on commit 9e49584

Please sign in to comment.