Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clean up move_relearner.c #545

Merged
merged 4 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion arm9/arm9.lsf
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ Static arm9
Object unk_0208898C.o
Object unk_02088AAC.o
Object unk_02088D1C.o
Object unk_02088DD8.o
Object move_relearner.o
Object unk_02088F0C.o
Object unk_020893E0.o
Object unk_02089498.o
Expand Down
10 changes: 5 additions & 5 deletions arm9/global.inc
Original file line number Diff line number Diff line change
Expand Up @@ -4815,7 +4815,7 @@
.extern Pokemon_UpdateArceusForm
.extern BoxMon_UpdateArceusForm
.extern GetArceusTypeByHeldItemEffect
.extern LoadWotbl_HandleAlternateForm
.extern LoadLevelUpLearnset_HandleAlternateForm
.extern sub_02069FB0
.extern sub_0206A014
.extern sub_0206A094
Expand Down Expand Up @@ -5448,10 +5448,10 @@
.extern sub_02088D84
.extern sub_02088DA0
.extern sub_02088DBC
.extern sub_02088DD8
.extern sub_02088DF0
.extern GetEligibleLevelUpMoves
.extern sub_02088EF8
.extern MoveRelearner_New
.extern MoveRelearner_Delete
.extern MoveRelearner_GetEligibleLevelUpMoves
.extern MoveRelearner_IsValidMove
.extern sub_02088F0C
.extern sub_02088F4C
.extern sub_020892C4
Expand Down
62 changes: 62 additions & 0 deletions arm9/src/move_relearner.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "global.h"
#include "move_relearner.h"
#include "pokemon.h"

MoveRelearner *MoveRelearner_New(HeapID heapId) {
MoveRelearner *ret = AllocFromHeap(heapId, sizeof(MoveRelearner));
memset(ret, 0, sizeof(MoveRelearner));
return ret;
}

void MoveRelearner_Delete(MoveRelearner *moveRelearner) {
FreeToHeap(moveRelearner);
}

u16 *MoveRelearner_GetEligibleLevelUpMoves(Pokemon* mon, HeapID heapId) {
u16 species = GetMonData(mon, MON_DATA_SPECIES, NULL);
u8 form = GetMonData(mon, MON_DATA_FORM, NULL);
u8 level = GetMonData(mon, MON_DATA_LEVEL, NULL);
u16 moves[MAX_MON_MOVES];

for (u8 i = 0; i < MAX_MON_MOVES; ++i) {
moves[i] = GetMonData(mon, MON_DATA_MOVE1 + i, NULL);
}

u16 *tableFromFile = AllocFromHeap(heapId, LEVEL_UP_LEARNSET_MAX * 2);
u16 *returnTable = AllocFromHeap(heapId, LEVEL_UP_LEARNSET_MAX * 2);

LoadLevelUpLearnset_HandleAlternateForm(species, form, tableFromFile);

for (u8 i = 0, j, k = 0; i < LEVEL_UP_LEARNSET_MAX; i++) {
if (tableFromFile[i] == LEVEL_UP_LEARNSET_END) {
returnTable[k] = LEVEL_UP_LEARNSET_END;
break;
} else if (LEVEL_UP_LEARNSET_LVL(tableFromFile[i]) > level) {
continue;
} else {
tableFromFile[i] = LEVEL_UP_LEARNSET_MOVE(tableFromFile[i]);
for (j = 0; j < MAX_MON_MOVES; j++) {
if (tableFromFile[i] == moves[j]) {
break;
}
}
if (j == MAX_MON_MOVES) {
for (j = 0; j < k; j++) {
if (returnTable[j] == tableFromFile[i]) {
break;
}
}
if (j == k) {
returnTable[k] = tableFromFile[i];
k++;
}
}
}
}
FreeToHeap(tableFromFile);
return returnTable;
}

BOOL MoveRelearner_IsValidMove(const u16 *ptr) {
return *ptr != LEVEL_UP_LEARNSET_END;
}
58 changes: 25 additions & 33 deletions arm9/src/pokemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ u8 Party_MaskMonsWithPokerus(struct Party * party_p, u8 mask);
BOOL BoxMon_HasPokerus(struct BoxPokemon * boxmon);
BOOL BoxMon_IsImmuneToPokerus(struct BoxPokemon * boxmon);
void BoxMon_UpdateArceusForm(struct BoxPokemon * boxmon);
void LoadWotbl_HandleAlternateForm(int species, int form, u16 * wotbl);
void LoadLevelUpLearnset_HandleAlternateForm(int species, int form, u16 *levelUpLearnset);
void sub_0206A054(struct BoxPokemon * boxmon, PlayerProfile * a1, u32 pokeball, u32 a3, u32 encounterType, HeapID heapId);
BOOL MonHasMove(struct Pokemon * pokemon, u16 move);
BOOL sub_0206A144(struct BoxPokemon * boxmon, u32 a1);
Expand Down Expand Up @@ -2778,38 +2778,30 @@ u16 GetEggSpecies(u16 species)
}
}

#define WOTBL_END 0xFFFF
#define WOTBL_MOVE_MASK 0x01FF
#define WOTBL_MOVE_SHIFT 0
#define WOTBL_LVL_MASK 0xFE00
#define WOTBL_LVL_SHIFT 9
#define WOTBL_MOVE(x) ((u16)(((x) & WOTBL_MOVE_MASK) >> WOTBL_MOVE_SHIFT))
#define WOTBL_LVL(x) ((u8)(((x) & WOTBL_LVL_MASK) >> WOTBL_LVL_SHIFT))

void InitBoxMonMoveset(struct BoxPokemon * boxmon)
{
BOOL decry;
u16 * wotbl;
u16 *levelUpLearnset;
int i;
u16 species;
u32 form;
u8 level;
u16 move;
wotbl = AllocFromHeap(HEAP_ID_DEFAULT, 22 * sizeof(u16));
levelUpLearnset = AllocFromHeap(HEAP_ID_DEFAULT, MAX_LEARNED_MOVES * sizeof(u16));
decry = AcquireBoxMonLock(boxmon);
species = (u16)GetBoxMonData(boxmon, MON_DATA_SPECIES, NULL);
form = GetBoxMonData(boxmon, MON_DATA_FORM, NULL);
level = (u8)CalcBoxMonLevel(boxmon);
LoadWotbl_HandleAlternateForm(species, (int)form, wotbl);
for (i = 0; wotbl[i] != WOTBL_END; i++)
LoadLevelUpLearnset_HandleAlternateForm(species, (int)form, levelUpLearnset);
for (i = 0; levelUpLearnset[i] != LEVEL_UP_LEARNSET_END; i++)
{
if ((wotbl[i] & WOTBL_LVL_MASK) > (level << WOTBL_LVL_SHIFT))
if ((levelUpLearnset[i] & LEVEL_UP_LEARNSET_LEVEL_MASK) > (level << LEVEL_UP_LEARNSET_LEVEL_SHIFT))
break;
move = WOTBL_MOVE(wotbl[i]);
move = LEVEL_UP_LEARNSET_MOVE(levelUpLearnset[i]);
if (sub_020696A8(boxmon, move) == 0xFFFF)
sub_02069718(boxmon, move);
}
FreeToHeap(wotbl);
FreeToHeap(levelUpLearnset);
ReleaseBoxMonLock(boxmon, decry);
}

Expand Down Expand Up @@ -2896,34 +2888,34 @@ void BoxMonSetMoveInSlot(struct BoxPokemon * boxmon, u16 move, u8 slot)
u32 sub_02069818(struct Pokemon * pokemon, u32 * r5, u16 * sp0)
{
u32 ret = 0;
u16 * wotbl = AllocFromHeap(HEAP_ID_DEFAULT, 22 * sizeof(u16));
u16 *levelUpLearnset = AllocFromHeap(HEAP_ID_DEFAULT, MAX_LEARNED_MOVES * sizeof(u16));
u16 species = (u16)GetMonData(pokemon, MON_DATA_SPECIES, NULL);
u32 form = GetMonData(pokemon, MON_DATA_FORM, NULL);
u8 level = (u8)GetMonData(pokemon, MON_DATA_LEVEL, NULL);
LoadWotbl_HandleAlternateForm(species, (int)form, wotbl);
LoadLevelUpLearnset_HandleAlternateForm(species, (int)form, levelUpLearnset);


if (wotbl[*r5] == 0xFFFF)
if (levelUpLearnset[*r5] == 0xFFFF)
{
FreeToHeap(wotbl);
FreeToHeap(levelUpLearnset);
return 0;
}
while ((wotbl[*r5] & WOTBL_LVL_MASK) != (level << WOTBL_LVL_SHIFT))
while ((levelUpLearnset[*r5] & LEVEL_UP_LEARNSET_LEVEL_MASK) != (level << LEVEL_UP_LEARNSET_LEVEL_SHIFT))
{
(*r5)++;
if (wotbl[*r5] == 0xFFFF)
if (levelUpLearnset[*r5] == 0xFFFF)
{
FreeToHeap(wotbl);
FreeToHeap(levelUpLearnset);
return 0;
}
}
if ((wotbl[*r5] & WOTBL_LVL_MASK) == (level << WOTBL_LVL_SHIFT))
if ((levelUpLearnset[*r5] & LEVEL_UP_LEARNSET_LEVEL_MASK) == (level << LEVEL_UP_LEARNSET_LEVEL_SHIFT))
{
*sp0 = WOTBL_MOVE(wotbl[*r5]);
*sp0 = LEVEL_UP_LEARNSET_MOVE(levelUpLearnset[*r5]);
(*r5)++;
ret = sub_02069698(pokemon, *sp0);
}
FreeToHeap(wotbl);
FreeToHeap(levelUpLearnset);
return ret;
}

Expand Down Expand Up @@ -3078,13 +3070,13 @@ s8 GetFlavorPreferenceFromPID(u32 personality, int flavor)
int Species_LoadLearnsetTable(u16 species, u32 form, u16 * dest)
{
int i;
u16 * wotbl = AllocFromHeap(HEAP_ID_DEFAULT, 22 * sizeof(u16));
LoadWotbl_HandleAlternateForm(species, (int)form, wotbl);
for (i = 0; wotbl[i] != WOTBL_END; i++)
u16 * levelUpLearnset = AllocFromHeap(HEAP_ID_DEFAULT, MAX_LEARNED_MOVES * sizeof(u16));
LoadLevelUpLearnset_HandleAlternateForm(species, (int)form, levelUpLearnset);
for (i = 0; levelUpLearnset[i] != LEVEL_UP_LEARNSET_END; i++)
{
dest[i] = WOTBL_MOVE(wotbl[i]);
dest[i] = LEVEL_UP_LEARNSET_MOVE(levelUpLearnset[i]);
}
FreeToHeap(wotbl);
FreeToHeap(levelUpLearnset);
return i;
}

Expand Down Expand Up @@ -3298,9 +3290,9 @@ u32 GetArceusTypeByHeldItemEffect(u16 heldEffect)
}
}

void LoadWotbl_HandleAlternateForm(int species, int form, u16 * wotbl)
void LoadLevelUpLearnset_HandleAlternateForm(int species, int form, u16 *levelUpLearnset)
{
ReadWholeNarcMemberByIdPair(wotbl, NARC_POKETOOL_PERSONAL_WOTBL, ResolveMonForm(species, form));
ReadWholeNarcMemberByIdPair(levelUpLearnset, NARC_POKETOOL_PERSONAL_WOTBL, ResolveMonForm(species, form));
}

void sub_02069FB0(struct SaveChatotSoundClip *r7, u32 r5, u16 r4, s32 r6, s32 sp18, u32 sp1C, HeapID heapId)
Expand Down
18 changes: 9 additions & 9 deletions arm9/src/scrcmd_move_relearner.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "heap.h"
#include "party.h"
#include "unk_020377F0.h"
#include "unk_02088DD8.h"
#include "move_relearner.h"

extern void* FieldSysGetAttrAddr(struct FieldSystem*, int idx);

Expand Down Expand Up @@ -48,23 +48,23 @@ BOOL ScrCmd_Unk021F(struct ScriptContext* ctx) { //021F
u16 mon_idx = ScriptGetVar(ctx);
struct Party* party = SaveArray_Party_Get(ctx->fieldSystem->saveData);
struct Pokemon* pokemon = Party_GetMonByIndex(party, mon_idx);
u16 *eligibleMoves = GetEligibleLevelUpMoves(pokemon, HEAP_ID_32);
u16 *eligibleMoves = MoveRelearner_GetEligibleLevelUpMoves(pokemon, HEAP_ID_32);

*ret_ptr = (u16)sub_02088EF8(eligibleMoves);
*ret_ptr = (u16)MoveRelearner_IsValidMove(eligibleMoves);
FreeToHeap(eligibleMoves);

return FALSE;
}

void sub_02045E74(struct ScriptContext* ctx, u8 a1, struct Pokemon* pokemon, u16 *eligibleMoves) {
MoveRelearner **moveRelearnerPtr = FieldSysGetAttrAddr(ctx->fieldSystem, SCRIPTENV_RUNNING_APP_DATA);
MoveRelearner *moveRelearner = sub_02088DD8(HEAP_ID_32);
MoveRelearner *moveRelearner = MoveRelearner_New(HEAP_ID_32);
*moveRelearnerPtr = moveRelearner;

moveRelearner->pokemon = pokemon;
moveRelearner->mon = pokemon;

struct SaveData* save = FieldSystem_GetSaveData(ctx->fieldSystem);
moveRelearner->player = Save_PlayerData_GetProfileAddr(save);
moveRelearner->profile = Save_PlayerData_GetProfileAddr(save);

moveRelearner->options = Save_PlayerData_GetOptionsAddr(ctx->fieldSystem->saveData);
moveRelearner->eligibleMoves = eligibleMoves;
Expand All @@ -86,7 +86,7 @@ BOOL ScrCmd_Unk0221(struct ScriptContext* ctx) //0221 - todo: RememberMove?
u16 mon_idx = ScriptGetVar(ctx);
struct Party* party = SaveArray_Party_Get(ctx->fieldSystem->saveData);
struct Pokemon* pokemon = Party_GetMonByIndex(party, mon_idx);
u16 *eligibleMoves = GetEligibleLevelUpMoves(pokemon, HEAP_ID_32);
u16 *eligibleMoves = MoveRelearner_GetEligibleLevelUpMoves(pokemon, HEAP_ID_32);

sub_02045E74(ctx, 1, pokemon, eligibleMoves);
return TRUE;
Expand Down Expand Up @@ -130,7 +130,7 @@ BOOL ScrCmd_Unk0223(struct ScriptContext* ctx) //0223 - todo: RememberMoveRespon
*ret_ptr = 0xFF;
}

sub_02088DF0(moveRelearner);
MoveRelearner_Delete(moveRelearner);
return FALSE;
}

Expand All @@ -151,6 +151,6 @@ BOOL ScrCmd_Unk0225(struct ScriptContext* ctx) //0225 - todo: TeachMoveResponse?
*ret_ptr = 0xFF;
}

sub_02088DF0(moveRelearner);
MoveRelearner_Delete(moveRelearner);
return FALSE;
}
76 changes: 0 additions & 76 deletions arm9/src/unk_02088DD8.c

This file was deleted.

6 changes: 3 additions & 3 deletions arm9/src/use_item_on_mon.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ BOOL CanUseItemOnPokemon(struct Pokemon * pokemon, u16 itemId, s32 moveId, HeapI
}
if (GetItemAttr_PreloadedItemData(itemData, ITEMATTR_PP_RESTORE_ALL))
{
for (int i = 0; i < MON_MOVES; i++)
for (int i = 0; i < MAX_MON_MOVES; i++)
{
if (MonMoveCanRestorePP(pokemon, i) == TRUE)
{
Expand Down Expand Up @@ -388,7 +388,7 @@ BOOL UseItemOnPokemon(struct Pokemon * pokemon, u16 itemId, s32 moveIdx, u16 loc
else if (GetItemAttr_PreloadedItemData(itemData, ITEMATTR_PP_RESTORE_ALL))
{
sp50 = 0;
for (; sp50 < MON_MOVES; sp50++)
for (; sp50 < MAX_MON_MOVES; sp50++)
{
if (MonMoveRestorePP(pokemon, sp50, (s32)GetItemAttr_PreloadedItemData(itemData, ITEMATTR_PP_RESTORE_PARAM)) == 1)
hadEffect = TRUE;
Expand Down Expand Up @@ -707,7 +707,7 @@ void HealParty(struct Party * party)
sp8 = 0;
SetMonData(pokemon, MON_DATA_STATUS, &sp8);

for (j = 0; j < MON_MOVES; j++)
for (j = 0; j < MAX_MON_MOVES; j++)
{
if (MonMoveCanRestorePP(pokemon, j) == 1)
MonMoveRestorePP(pokemon, j, PP_RESTORE_ALL);
Expand Down
Loading
Loading