diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/item.c | 478 | ||||
-rw-r--r-- | src/item_pc.c | 2 |
2 files changed, 479 insertions, 1 deletions
diff --git a/src/item.c b/src/item.c index 5ea771c05..f1b7fdb6e 100644 --- a/src/item.c +++ b/src/item.c @@ -1,8 +1,486 @@ #include "global.h" +#include "berry.h" +#include "event_data.h" #include "item.h" #include "item_use.h" +#include "load_save.h" +#include "string_util.h" +#include "strings.h" #include "constants/hold_effects.h" #include "constants/items.h" // Item descriptions and data #include "data/items.h" + +u16 GetBagItemQuantity(u16 * ptr) +{ + return gSaveBlock2Ptr->encryptionKey ^ *ptr; +} + +void SetBagItemQuantity(u16 * ptr, u16 value) +{ + *ptr = value ^ gSaveBlock2Ptr->encryptionKey; +} + +u16 GetPcItemQuantity(u16 * ptr) +{ + return 0 ^ *ptr; +} + +void SetPcItemQuantity(u16 * ptr, u16 value) +{ + *ptr = value ^ 0; +} + +void ApplyNewEncryptionKeyToBagItems(u32 key) +{ + u32 i, j; + + for (i = 0; i < NUM_BAG_POCKETS; i++) + { + for (j = 0; j < gBagPockets[i].capacity; j++) + { + ApplyNewEncryptionKeyToHword(&gBagPockets[i].itemSlots[j].quantity, key); + } + } +} + +void ApplyNewEncryptionKeyToBagItems_(u32 key) { + ApplyNewEncryptionKeyToBagItems(key); +} + +void sub_8099E44(void) +{ + gBagPockets[POCKET_ITEMS - 1].itemSlots = gSaveBlock1Ptr->bagPocket_Items; + gBagPockets[POCKET_ITEMS - 1].capacity = BAG_ITEMS_COUNT; + gBagPockets[POCKET_KEY_ITEMS - 1].itemSlots = gSaveBlock1Ptr->bagPocket_KeyItems; + gBagPockets[POCKET_KEY_ITEMS - 1].capacity = BAG_KEYITEMS_COUNT; + gBagPockets[POCKET_POKE_BALLS - 1].itemSlots = gSaveBlock1Ptr->bagPocket_PokeBalls; + gBagPockets[POCKET_POKE_BALLS - 1].capacity = BAG_POKEBALLS_COUNT; + gBagPockets[POCKET_TM_CASE - 1].itemSlots = gSaveBlock1Ptr->bagPocket_TMHM; + gBagPockets[POCKET_TM_CASE - 1].capacity = BAG_TMHM_COUNT; + gBagPockets[POCKET_BERRY_POUCH - 1].itemSlots = gSaveBlock1Ptr->bagPocket_Berries; + gBagPockets[POCKET_BERRY_POUCH - 1].capacity = BAG_BERRIES_COUNT; +} + +void CopyItemName(u16 itemId, u8 * dest) +{ + if (itemId == ITEM_ENIGMA_BERRY) + { + StringCopy(dest, sub_809C8A0(43)->name); + StringAppend(dest, gUnknown_84162BD); + } + else + { + StringCopy(dest, ItemId_GetName(itemId)); + } +} + +s8 sub_8099ECC(u8 pocketId) +{ + u16 i; + + for (i = 0; i < gBagPockets[pocketId].capacity; i++) + { + if (gBagPockets[pocketId].itemSlots[i].itemId == ITEM_NONE) + return i; + } + + return -1; +} + +bool8 sub_8099F08(u8 pocketId) +{ + u8 i; + + for (i = 0; i < gBagPockets[pocketId - 1].capacity; i++) + { + if (gBagPockets[pocketId - 1].itemSlots[i].itemId != ITEM_NONE) + return TRUE; + } + + return FALSE; +} + +bool8 CheckBagHasItem(u16 itemId, u16 count) +{ + u8 i; + u8 pocket; + + if (ItemId_GetPocket(itemId) == 0) + return FALSE; + + pocket = ItemId_GetPocket(itemId) - 1; + // Check for item slots that contain the item + for (i = 0; i < gBagPockets[pocket].capacity; i++) + { + if (gBagPockets[pocket].itemSlots[i].itemId == itemId) + { + u16 quantity; + // Does this item slot contain enough of the item? + quantity = GetBagItemQuantity(&gBagPockets[pocket].itemSlots[i].quantity); + if (quantity >= count) + return TRUE; + // RS and Emerald check whether there is enough of the + // item across all stacks. + // For whatever reason, FR/LG assume there's only one + // stack of the item. + else + return FALSE; + } + } + return FALSE; +} + +bool8 sub_8099FAC(void) +{ + u8 itemId; + bool8 exists; + + exists = CheckBagHasItem(ITEM_BERRY_POUCH, 1); + if (!exists) + { + gSpecialVar_Result = FALSE; + return FALSE; + } + for (itemId = ITEM_CHERI_BERRY; itemId <= ITEM_ENIGMA_BERRY; itemId++) + { + exists = CheckBagHasItem(itemId, 1); + if (exists) + { + gSpecialVar_Result = TRUE; + return TRUE; + } + } + + gSpecialVar_Result = FALSE; + return FALSE; +} + +bool8 CheckBagHasSpace(u16 itemId, u16 count) +{ + u8 i; + u8 pocket; + + if (ItemId_GetPocket(itemId) == 0) + return FALSE; + + pocket = ItemId_GetPocket(itemId) - 1; + // Check for item slots that contain the item + for (i = 0; i < gBagPockets[pocket].capacity; i++) + { + if (gBagPockets[pocket].itemSlots[i].itemId == itemId) + { + u16 quantity; + // Does this stack have room for more?? + quantity = GetBagItemQuantity(&gBagPockets[pocket].itemSlots[i].quantity); + if (quantity + count <= 999) + return TRUE; + // RS and Emerald check whether there is enough of the + // item across all stacks. + // For whatever reason, FR/LG assume there's only one + // stack of the item. + else + return FALSE; + } + } + + if (sub_8099ECC(pocket) != -1) + return TRUE; + + return FALSE; +} + +bool8 AddBagItem(u16 itemId, u16 count) +{ + u8 i; + u8 pocket; + s8 idx; + + if (ItemId_GetPocket(itemId) == 0) + return FALSE; + + pocket = ItemId_GetPocket(itemId) - 1; + for (i = 0; i < gBagPockets[pocket].capacity; i++) + { + if (gBagPockets[pocket].itemSlots[i].itemId == itemId) + { + u16 quantity; + // Does this stack have room for more?? + quantity = GetBagItemQuantity(&gBagPockets[pocket].itemSlots[i].quantity); + if (quantity + count <= 999) + { + quantity += count; + SetBagItemQuantity(&gBagPockets[pocket].itemSlots[i].quantity, quantity); + return TRUE; + } + // RS and Emerald check whether there is enough of the + // item across all stacks. + // For whatever reason, FR/LG assume there's only one + // stack of the item. + else + return FALSE; + } + } + + if (pocket == POCKET_TM_CASE - 1 && !CheckBagHasItem(ITEM_TM_CASE, 1)) + { + idx = sub_8099ECC(POCKET_KEY_ITEMS - 1); + if (idx == -1) + return FALSE; + gBagPockets[POCKET_KEY_ITEMS - 1].itemSlots[idx].itemId = ITEM_TM_CASE; + SetBagItemQuantity(&gBagPockets[POCKET_KEY_ITEMS - 1].itemSlots[idx].quantity, 1); + } + + if (pocket == POCKET_BERRY_POUCH - 1 && !CheckBagHasItem(ITEM_BERRY_POUCH, 1)) + { + idx = sub_8099ECC(POCKET_KEY_ITEMS - 1); + if (idx == -1) + return FALSE; + gBagPockets[POCKET_KEY_ITEMS - 1].itemSlots[idx].itemId = ITEM_BERRY_POUCH; + SetBagItemQuantity(&gBagPockets[POCKET_KEY_ITEMS - 1].itemSlots[idx].quantity, 1); + FlagSet(FLAG_0x847); + } + + if (itemId == ITEM_BERRY_POUCH) + FlagSet(FLAG_0x847); + + idx = sub_8099ECC(pocket); + if (idx == -1) + return FALSE; + + gBagPockets[pocket].itemSlots[idx].itemId = itemId; + SetBagItemQuantity(&gBagPockets[pocket].itemSlots[idx].quantity, count); + return TRUE; +} + +bool8 RemoveBagItem(u16 itemId, u16 count) +{ + u8 i; + u8 pocket; + + if (ItemId_GetPocket(itemId) == 0) + return FALSE; + + if (itemId == ITEM_NONE) + return FALSE; + + pocket = ItemId_GetPocket(itemId) - 1; + // Check for item slots that contain the item + for (i = 0; i < gBagPockets[pocket].capacity; i++) + { + if (gBagPockets[pocket].itemSlots[i].itemId == itemId) + { + u16 quantity; + // Does this item slot contain enough of the item? + quantity = GetBagItemQuantity(&gBagPockets[pocket].itemSlots[i].quantity); + if (quantity >= count) + { + quantity -= count; + SetBagItemQuantity(&gBagPockets[pocket].itemSlots[i].quantity, quantity); + if (quantity == 0) + gBagPockets[pocket].itemSlots[i].itemId = ITEM_NONE; + return TRUE; + } + // RS and Emerald check whether there is enough of the + // item across all stacks. + // For whatever reason, FR/LG assume there's only one + // stack of the item. + else + return FALSE; + } + } + return FALSE; +} + +u8 GetPocketByItemId(u16 itemId) +{ + return ItemId_GetPocket(itemId); // wow such important +} + +void ClearItemSlots(struct ItemSlot * slots, u8 capacity) +{ + u16 i; + + for (i = 0; i < capacity; i++) + { + slots[i].itemId = ITEM_NONE; + SetBagItemQuantity(&slots[i].quantity, 0); + } +} + +void sub_809A2A4(void) +{ + u16 i; + + for (i = 0; i < PC_ITEMS_COUNT; i++) + { + gSaveBlock1Ptr->pcItems[i].itemId = ITEM_NONE; + SetPcItemQuantity(&gSaveBlock1Ptr->pcItems[i].quantity, 0); + } +} + +void sub_809A2DC(void) +{ + u16 i; + + for (i = 0; i < 5; i++) + { + ClearItemSlots(gBagPockets[i].itemSlots, gBagPockets[i].capacity); + } +} + +s8 sub_809A304(void) +{ + s8 i; + + for (i = 0; i < PC_ITEMS_COUNT; i++) + { + if (gSaveBlock1Ptr->pcItems[i].itemId == ITEM_NONE) + return i; + } + + return -1; +} + +u8 sub_809A33C(void) +{ + u8 count = 0; + u8 i; + + for (i = 0; i < PC_ITEMS_COUNT; i++) + { + if (gSaveBlock1Ptr->pcItems[i].itemId != ITEM_NONE) + count++; + } + + return count; +} + +bool8 CheckPCHasItem(u16 itemId, u16 count) +{ + u8 i; + u16 quantity; + + for (i = 0; i < PC_ITEMS_COUNT; i++) + { + if (gSaveBlock1Ptr->pcItems[i].itemId == itemId) + { + quantity = GetPcItemQuantity(&gSaveBlock1Ptr->pcItems[i].quantity); + if (quantity >= count) + return TRUE; + } + } + + return FALSE; +} + +bool8 AddPCItem(u16 itemId, u16 count) +{ + u8 i; + u16 quantity; + s8 idx; + + for (i = 0; i < PC_ITEMS_COUNT; i++) + { + if (gSaveBlock1Ptr->pcItems[i].itemId == itemId) + { + quantity = GetPcItemQuantity(&gSaveBlock1Ptr->pcItems[i].quantity); + if (quantity + count <= 999) + { + quantity += count; + SetPcItemQuantity(&gSaveBlock1Ptr->pcItems[i].quantity, quantity); + return TRUE; + } + else + return FALSE; + } + } + + idx = sub_809A304(); + if (idx == -1) + return FALSE; + + gSaveBlock1Ptr->pcItems[idx].itemId = itemId; + SetPcItemQuantity(&gSaveBlock1Ptr->pcItems[idx].quantity, count); + return TRUE; +} + +void RemoveItemFromPC(u16 itemId, u16 count) +{ + u32 i; + u16 quantity; + + if (itemId == ITEM_NONE) + return; + + for (i = 0; i < PC_ITEMS_COUNT; i++) + { + if (gSaveBlock1Ptr->pcItems[i].itemId == itemId) + break; + } + + if (i != PC_ITEMS_COUNT) + { + quantity = GetPcItemQuantity(&gSaveBlock1Ptr->pcItems[i].quantity) - count; + SetPcItemQuantity(&gSaveBlock1Ptr->pcItems[i].quantity, quantity); + if (quantity == 0) + gSaveBlock1Ptr->pcItems[i].itemId = ITEM_NONE; + } +} + +void ItemPcCompaction(void) +{ + u16 i, j; + struct ItemSlot tmp; + + for (i = 0; i < PC_ITEMS_COUNT - 1; i++) + { + for (j = i + 1; j < PC_ITEMS_COUNT; j++) + { + if (gSaveBlock1Ptr->pcItems[i].itemId == ITEM_NONE) + { + tmp = gSaveBlock1Ptr->pcItems[i]; + gSaveBlock1Ptr->pcItems[i] = gSaveBlock1Ptr->pcItems[j]; + gSaveBlock1Ptr->pcItems[j] = tmp; + } + } + } +} + +void sub_809A540(void) +{ + switch (gSaveBlock1Ptr->registeredItem) + { + case ITEM_MACH_BIKE: + gSaveBlock1Ptr->registeredItem = ITEM_ACRO_BIKE; + break; + case ITEM_ACRO_BIKE: + gSaveBlock1Ptr->registeredItem = ITEM_MACH_BIKE; + break; + } +} + +void sub_809A578(struct ItemSlot * a, struct ItemSlot * b) +{ + struct ItemSlot c; + c = *a; + *a = *b; + *b = c; +} + +void sub_809A584(struct ItemSlot * slots, u8 capacity) +{ + u16 i, j; + + for (i = 0; i < capacity - 1; i++) + { + for (j = i + 1; j < capacity; j++) + { + if (GetBagItemQuantity(&slots[i].quantity) == 0) + { + sub_809A578(&slots[i], &slots[j]); + } + } + } +} diff --git a/src/item_pc.c b/src/item_pc.c index 3e139503e..e2fa33f0a 100644 --- a/src/item_pc.c +++ b/src/item_pc.c @@ -676,7 +676,7 @@ static u16 ItemPc_GetItemIdBySlotId(u16 idx) static u16 ItemPc_GetItemQuantityBySlotId(u16 idx) { - return GetBagItemId(&gSaveBlock1Ptr->pcItems[idx].quantity); + return GetPcItemQuantity(&gSaveBlock1Ptr->pcItems[idx].quantity); } static void ItemPc_CountPcItems(void) |