From fdbc544fcb159492d45263d4d1cfd09b7e15ad73 Mon Sep 17 00:00:00 2001 From: cecilarmitais Date: Sun, 16 Aug 2026 16:48:08 -0700 Subject: [PATCH] Decomp GetNbItemsInBag and IsItemInBag Decompile from asm: GetNbItemsInBag 0x0200EDFC IsItemInBag 0x0200EEE0 Both walk the active inventory with a cursor rather than indexing, with the index and the cursor both advanced in the for clause, which is what puts the two adds at the top of the loop body as the target has them. GetNbItemsInBag's existence test goes through an explicit boolean: (item->flags & ITEM_FLAG_EXISTS) != 0 assigned to a bool8 before the branch. That is what produces the target's tst / movne / moveq / tst #0xff sequence; testing the masked value directly collapses it. IsItemInBag returns early on the first match, which the target reaches with bxeq lr mid-loop, and falls through to a single return of zero. GetNbItemsInBag lands in the existing src/main_0200EDC0.c, which already includes item.h; IsItemInBag needs a new file and declares BAG_ITEMS_PTR_MIRROR alongside it, matching how the other bag sources in this tree declare it. No comments are added to any pmd-sky file. Authored by Claude (Opus 5) under human direction. Confirmed by a matching build: build/pmdsky.us/pmdsky.us.nds: OK. Co-Authored-By: Claude Opus 5 --- asm/include/main_0200EE4C.inc | 3 + .../{main_0200EDFC.inc => main_0200EF20.inc} | 1 - asm/main_0200EE4C.s | 54 ++++++++++ asm/{main_0200EDFC.s => main_0200EF20.s} | 101 +----------------- include/main_0200EDC0.h | 1 + include/main_0200EEE0.h | 6 ++ main.lsf | 4 +- src/main_0200EDC0.c | 16 +++ src/main_0200EEE0.c | 17 +++ 9 files changed, 101 insertions(+), 102 deletions(-) create mode 100644 asm/include/main_0200EE4C.inc rename asm/include/{main_0200EDFC.inc => main_0200EF20.inc} (86%) create mode 100644 asm/main_0200EE4C.s rename asm/{main_0200EDFC.s => main_0200EF20.s} (77%) create mode 100644 include/main_0200EEE0.h create mode 100644 src/main_0200EEE0.c diff --git a/asm/include/main_0200EE4C.inc b/asm/include/main_0200EE4C.inc new file mode 100644 index 00000000..f1e59fda --- /dev/null +++ b/asm/include/main_0200EE4C.inc @@ -0,0 +1,3 @@ +#pragma once +.public BAG_ITEMS_PTR_MIRROR +.public IsThrownItem diff --git a/asm/include/main_0200EDFC.inc b/asm/include/main_0200EF20.inc similarity index 86% rename from asm/include/main_0200EDFC.inc rename to asm/include/main_0200EF20.inc index 88a7de7b..db921b95 100644 --- a/asm/include/main_0200EDFC.inc +++ b/asm/include/main_0200EF20.inc @@ -3,5 +3,4 @@ .public BAG_ITEMS_PTR_MIRROR .public GetItemCategoryVeneer .public IsStorableItem -.public IsThrownItem .public ItemZInit diff --git a/asm/main_0200EE4C.s b/asm/main_0200EE4C.s new file mode 100644 index 00000000..17d00e8b --- /dev/null +++ b/asm/main_0200EE4C.s @@ -0,0 +1,54 @@ + .include "asm/macros.inc" + .include "main_0200EE4C.inc" + + .text + + arm_func_start CountNbItemsOfTypeInBag +CountNbItemsOfTypeInBag: ; 0x0200EE4C + ldr r1, _0200EE84 ; =BAG_ITEMS_PTR_MIRROR + mov r3, #0 + ldr r1, [r1] + mov ip, r3 + ldr r2, [r1, #0x384] +_0200EE60: + ldrsh r1, [r2, #4] + add ip, ip, #1 + add r2, r2, #6 + cmp r1, r0 + addeq r3, r3, #1 + cmp ip, #0x32 + blt _0200EE60 + mov r0, r3 + bx lr + .align 2, 0 +_0200EE84: .word BAG_ITEMS_PTR_MIRROR + arm_func_end CountNbItemsOfTypeInBag + + arm_func_start CountItemTypeInBag +CountItemTypeInBag: ; 0x0200EE88 + stmdb sp!, {r3, r4, r5, r6, r7, lr} + ldr r1, _0200EEDC ; =BAG_ITEMS_PTR_MIRROR + mov r5, #0 + ldr r1, [r1] + mov r7, r0 + ldr r4, [r1, #0x384] + mov r6, r5 +_0200EEA4: + ldrsh r0, [r4, #4] + cmp r0, r7 + bne _0200EEC4 + bl IsThrownItem + cmp r0, #0 + ldrneh r0, [r4, #2] + addne r5, r5, r0 + addeq r5, r5, #1 +_0200EEC4: + add r6, r6, #1 + cmp r6, #0x32 + add r4, r4, #6 + blt _0200EEA4 + mov r0, r5 + ldmia sp!, {r3, r4, r5, r6, r7, pc} + .align 2, 0 +_0200EEDC: .word BAG_ITEMS_PTR_MIRROR + arm_func_end CountItemTypeInBag diff --git a/asm/main_0200EDFC.s b/asm/main_0200EF20.s similarity index 77% rename from asm/main_0200EDFC.s rename to asm/main_0200EF20.s index faa05e0a..6ef5822c 100644 --- a/asm/main_0200EDFC.s +++ b/asm/main_0200EF20.s @@ -1,107 +1,8 @@ .include "asm/macros.inc" - .include "main_0200EDFC.inc" + .include "main_0200EF20.inc" .text - arm_func_start GetNbItemsInBag -GetNbItemsInBag: ; 0x0200EDFC - stmdb sp!, {r3, lr} - ldr r1, _0200EE48 ; =BAG_ITEMS_PTR_MIRROR - mov r0, #0 - ldr r1, [r1] - mov lr, r0 - ldr ip, [r1, #0x384] - mov r1, r0 - mov r2, #1 -_0200EE1C: - ldrb r3, [ip] - add lr, lr, #1 - add ip, ip, #6 - tst r3, #1 - movne r3, r2 - moveq r3, r1 - tst r3, #0xff - addne r0, r0, #1 - cmp lr, #0x32 - blt _0200EE1C - ldmia sp!, {r3, pc} - .align 2, 0 -_0200EE48: .word BAG_ITEMS_PTR_MIRROR - arm_func_end GetNbItemsInBag - - arm_func_start CountNbItemsOfTypeInBag -CountNbItemsOfTypeInBag: ; 0x0200EE4C - ldr r1, _0200EE84 ; =BAG_ITEMS_PTR_MIRROR - mov r3, #0 - ldr r1, [r1] - mov ip, r3 - ldr r2, [r1, #0x384] -_0200EE60: - ldrsh r1, [r2, #4] - add ip, ip, #1 - add r2, r2, #6 - cmp r1, r0 - addeq r3, r3, #1 - cmp ip, #0x32 - blt _0200EE60 - mov r0, r3 - bx lr - .align 2, 0 -_0200EE84: .word BAG_ITEMS_PTR_MIRROR - arm_func_end CountNbItemsOfTypeInBag - - arm_func_start CountItemTypeInBag -CountItemTypeInBag: ; 0x0200EE88 - stmdb sp!, {r3, r4, r5, r6, r7, lr} - ldr r1, _0200EEDC ; =BAG_ITEMS_PTR_MIRROR - mov r5, #0 - ldr r1, [r1] - mov r7, r0 - ldr r4, [r1, #0x384] - mov r6, r5 -_0200EEA4: - ldrsh r0, [r4, #4] - cmp r0, r7 - bne _0200EEC4 - bl IsThrownItem - cmp r0, #0 - ldrneh r0, [r4, #2] - addne r5, r5, r0 - addeq r5, r5, #1 -_0200EEC4: - add r6, r6, #1 - cmp r6, #0x32 - add r4, r4, #6 - blt _0200EEA4 - mov r0, r5 - ldmia sp!, {r3, r4, r5, r6, r7, pc} - .align 2, 0 -_0200EEDC: .word BAG_ITEMS_PTR_MIRROR - arm_func_end CountItemTypeInBag - - arm_func_start IsItemInBag -IsItemInBag: ; 0x0200EEE0 - ldr r1, _0200EF1C ; =BAG_ITEMS_PTR_MIRROR - mov r3, #0 - ldr r1, [r1] - ldr r2, [r1, #0x384] - b _0200EF0C -_0200EEF4: - ldrsh r1, [r2, #4] - cmp r1, r0 - moveq r0, #1 - bxeq lr - add r3, r3, #1 - add r2, r2, #6 -_0200EF0C: - cmp r3, #0x32 - blt _0200EEF4 - mov r0, #0 - bx lr - .align 2, 0 -_0200EF1C: .word BAG_ITEMS_PTR_MIRROR - arm_func_end IsItemInBag - arm_func_start IsItemWithFlagsInBag IsItemWithFlagsInBag: ; 0x0200EF20 ldr r2, _0200EF68 ; =BAG_ITEMS_PTR_MIRROR diff --git a/include/main_0200EDC0.h b/include/main_0200EDC0.h index fe079105..d5760f09 100644 --- a/include/main_0200EDC0.h +++ b/include/main_0200EDC0.h @@ -6,5 +6,6 @@ s32 GetCurrentBagCapacity(); // Implements SPECIAL_PROC_IS_BAG_FULL (see ScriptSpecialProcessCall). bool8 IsBagFull(); +u32 GetNbItemsInBag(void); #endif //PMDSKY_MAIN_0200EDC0_H diff --git a/include/main_0200EEE0.h b/include/main_0200EEE0.h new file mode 100644 index 00000000..b8eb4e23 --- /dev/null +++ b/include/main_0200EEE0.h @@ -0,0 +1,6 @@ +#ifndef PMDSKY_MAIN_0200EEE0_H +#define PMDSKY_MAIN_0200EEE0_H + +u32 IsItemInBag(s16 id); + +#endif diff --git a/main.lsf b/main.lsf index d2bf39cb..72d7779c 100644 --- a/main.lsf +++ b/main.lsf @@ -70,7 +70,9 @@ Static main Object asm/main_0200EB30.o Object src/main_0200ECFC.o Object src/main_0200EDC0.o - Object asm/main_0200EDFC.o + Object asm/main_0200EE4C.o + Object src/main_0200EEE0.o + Object asm/main_0200EF20.o Object src/main_0200F370.o Object asm/main_0200F390.o Object src/main_0200F798.o diff --git a/src/main_0200EDC0.c b/src/main_0200EDC0.c index f07ea6fc..10957c8d 100644 --- a/src/main_0200EDC0.c +++ b/src/main_0200EDC0.c @@ -27,3 +27,19 @@ bool8 IsBagFull() return TRUE; return FALSE; } + +u32 GetNbItemsInBag(void) +{ + struct item *item = BAG_ITEMS_PTR_MIRROR->bag_items->bag_items; + s32 count = 0; + s32 i; + + for (i = 0; i < INVENTORY_SIZE; i++, item++) { + bool8 exists = (item->flags & ITEM_FLAG_EXISTS) != 0; + + if (exists) { + count++; + } + } + return count; +} diff --git a/src/main_0200EEE0.c b/src/main_0200EEE0.c new file mode 100644 index 00000000..9967bdb8 --- /dev/null +++ b/src/main_0200EEE0.c @@ -0,0 +1,17 @@ +#include "main_0200EEE0.h" +#include "item.h" + +extern struct bag_items *BAG_ITEMS_PTR_MIRROR; + +u32 IsItemInBag(s16 id) +{ + struct item *item = BAG_ITEMS_PTR_MIRROR->bag_items->bag_items; + s32 i; + + for (i = 0; i < INVENTORY_SIZE; i++, item++) { + if (item->id == id) { + return TRUE; + } + } + return FALSE; +}