From 85ca0645a90e9f23f3788a00a581610c721763ea Mon Sep 17 00:00:00 2001 From: moostoet <70690976+moostoet@users.noreply.github.com> Date: Mon, 20 May 2024 22:52:12 +0200 Subject: [PATCH] Add configuration to prevent permanent item theft (Covet / Thief) from NPCs (#4605) * feat: implement logic for gen5+ covet/thief * refactor: add define, misc. changes * fix: define parenthesises * fix: revert to bool32 * fix: formatting --- include/config/battle.h | 1 + src/battle_util.c | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/include/config/battle.h b/include/config/battle.h index 18379d4add..1a272f358f 100644 --- a/include/config/battle.h +++ b/include/config/battle.h @@ -154,6 +154,7 @@ #define B_X_ITEMS_BUFF GEN_LATEST // In Gen7+, the X Items raise a stat by 2 stages instead of 1. #define B_MENTAL_HERB GEN_LATEST // In Gen5+, the Mental Herb cures Taunt, Encore, Torment, Heal Block, and Disable in addition to Infatuation from before. #define B_TRAINERS_KNOCK_OFF_ITEMS TRUE // If TRUE, trainers can steal/swap your items (non-berries are restored after battle). In vanilla games trainers cannot steal items. +#define B_RETURN_STOLEN_NPC_ITEMS GEN_LATEST // In Gen5+, Thief and Covet no longer steal items from NPCs. #define B_RESTORE_HELD_BATTLE_ITEMS GEN_LATEST // In Gen9, all non-berry items are restored after battle. #define B_SOUL_DEW_BOOST GEN_LATEST // In Gens3-6, Soul Dew boosts Latis' Sp. Atk and Sp. Def. In Gen7+ it boosts the power of their Psychic and Dragon type moves instead. #define B_NET_BALL_MODIFIER GEN_LATEST // In Gen7+, Net Ball's catch multiplier is x5 instead of x3. diff --git a/src/battle_util.c b/src/battle_util.c index 67f36e80ef..b836ad7f0b 100644 --- a/src/battle_util.c +++ b/src/battle_util.c @@ -10991,15 +10991,23 @@ void SortBattlersBySpeed(u8 *battlers, bool32 slowToFast) void TryRestoreHeldItems(void) { u32 i; - u16 lostItem = ITEM_NONE; + bool32 returnNPCItems = B_RETURN_STOLEN_NPC_ITEMS >= GEN_5 && gBattleTypeFlags & BATTLE_TYPE_TRAINER; for (i = 0; i < PARTY_SIZE; i++) { - if (B_RESTORE_HELD_BATTLE_ITEMS >= GEN_9 || gBattleStruct->itemLost[i].stolen) + // Check if held items should be restored after battle based on generation + if (B_RESTORE_HELD_BATTLE_ITEMS >= GEN_9 || gBattleStruct->itemLost[i].stolen || returnNPCItems) { - lostItem = gBattleStruct->itemLost[i].originalItem; - if (lostItem != ITEM_NONE && ItemId_GetPocket(lostItem) != POCKET_BERRIES) - SetMonData(&gPlayerParty[i], MON_DATA_HELD_ITEM, &lostItem); // Restore stolen non-berry items + u16 lostItem = gBattleStruct->itemLost[i].originalItem; + + // Check if the lost item is a berry and the mon is not holding it + if (ItemId_GetPocket(lostItem) == POCKET_BERRIES && GetMonData(&gPlayerParty[i], MON_DATA_HELD_ITEM) != lostItem) + lostItem = ITEM_NONE; + + // Check if the lost item should be restored + if ((lostItem != ITEM_NONE || returnNPCItems) && ItemId_GetPocket(lostItem) != POCKET_BERRIES) + SetMonData(&gPlayerParty[i], MON_DATA_HELD_ITEM, &lostItem); + } } }