Skip to content

Commit

Permalink
Merge pull request #1995 from Jaizu/adjust_friendship
Browse files Browse the repository at this point in the history
Make AdjustFriendship more readable
  • Loading branch information
GriffinRichards authored May 21, 2024
2 parents 999731f + 411d7e6 commit aa4fa92
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions src/pokemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -5877,16 +5877,11 @@ u16 ModifyStatByNature(u8 nature, u16 stat, u8 statIndex)
return retVal;
}

#define IS_LEAGUE_BATTLE \
((gBattleTypeFlags & BATTLE_TYPE_TRAINER) \
&& (gTrainers[gTrainerBattleOpponent_A].trainerClass == TRAINER_CLASS_ELITE_FOUR \
|| gTrainers[gTrainerBattleOpponent_A].trainerClass == TRAINER_CLASS_LEADER \
|| gTrainers[gTrainerBattleOpponent_A].trainerClass == TRAINER_CLASS_CHAMPION)) \

void AdjustFriendship(struct Pokemon *mon, u8 event)
{
u16 species, heldItem;
u8 holdEffect;
s8 mod;

if (ShouldSkipFriendshipChange())
return;
Expand Down Expand Up @@ -5916,26 +5911,43 @@ void AdjustFriendship(struct Pokemon *mon, u8 event)
if (friendship > 199)
friendshipLevel++;

if ((event != FRIENDSHIP_EVENT_WALKING || !(Random() & 1))
&& (event != FRIENDSHIP_EVENT_LEAGUE_BATTLE || IS_LEAGUE_BATTLE))
if (event == FRIENDSHIP_EVENT_WALKING)
{
s8 mod = sFriendshipEventModifiers[event][friendshipLevel];
if (mod > 0 && holdEffect == HOLD_EFFECT_FRIENDSHIP_UP)
mod = (150 * mod) / 100;
friendship += mod;
if (mod > 0)
{
if (GetMonData(mon, MON_DATA_POKEBALL, 0) == ITEM_LUXURY_BALL)
friendship++;
if (GetMonData(mon, MON_DATA_MET_LOCATION, 0) == GetCurrentRegionMapSectionId())
friendship++;
}
if (friendship < 0)
friendship = 0;
if (friendship > MAX_FRIENDSHIP)
friendship = MAX_FRIENDSHIP;
SetMonData(mon, MON_DATA_FRIENDSHIP, &friendship);
// 50% chance every 128 steps
if (Random() & 1)
return;
}
if (event == FRIENDSHIP_EVENT_LEAGUE_BATTLE)
{
// Only if it's a trainer battle with league progression significance
if (!(gBattleTypeFlags & BATTLE_TYPE_TRAINER))
return;
if (!(gTrainers[gTrainerBattleOpponent_A].trainerClass == TRAINER_CLASS_LEADER
|| gTrainers[gTrainerBattleOpponent_A].trainerClass == TRAINER_CLASS_ELITE_FOUR
|| gTrainers[gTrainerBattleOpponent_A].trainerClass == TRAINER_CLASS_CHAMPION))
return;
}

mod = sFriendshipEventModifiers[event][friendshipLevel];
if (mod > 0 && holdEffect == HOLD_EFFECT_FRIENDSHIP_UP)
// 50% increase, rounding down
mod = (150 * mod) / 100;

friendship += mod;
if (mod > 0)
{
if (GetMonData(mon, MON_DATA_POKEBALL, NULL) == ITEM_LUXURY_BALL)
friendship++;
if (GetMonData(mon, MON_DATA_MET_LOCATION, NULL) == GetCurrentRegionMapSectionId())
friendship++;
}

if (friendship < 0)
friendship = 0;
if (friendship > MAX_FRIENDSHIP)
friendship = MAX_FRIENDSHIP;

SetMonData(mon, MON_DATA_FRIENDSHIP, &friendship);
}
}

Expand Down

0 comments on commit aa4fa92

Please sign in to comment.