inventory.asm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ; function to add an item (in varying quantities) to the player's bag or PC box
  2. ; INPUT:
  3. ; hl = address of inventory (either wNumBagItems or wNumBoxItems)
  4. ; [wcf91] = item ID
  5. ; [wItemQuantity] = item quantity
  6. ; sets carry flag if successful, unsets carry flag if unsuccessful
  7. AddItemToInventory_:
  8. ld a, [wItemQuantity] ; a = item quantity
  9. push af
  10. push bc
  11. push de
  12. push hl
  13. push hl
  14. ld d, PC_ITEM_CAPACITY ; how many items the PC can hold
  15. ld a, wNumBagItems & $FF
  16. cp l
  17. jr nz, .checkIfInventoryFull
  18. ld a, wNumBagItems >> 8
  19. cp h
  20. jr nz, .checkIfInventoryFull
  21. ; if the destination is the bag
  22. ld d, BAG_ITEM_CAPACITY ; how many items the bag can hold
  23. .checkIfInventoryFull
  24. ld a, [hl]
  25. sub d
  26. ld d, a
  27. ld a, [hli]
  28. and a
  29. jr z, .addNewItem
  30. .loop
  31. ld a, [hli]
  32. ld b, a ; b = ID of current item in table
  33. ld a, [wcf91] ; a = ID of item being added
  34. cp b ; does the current item in the table match the item being added?
  35. jp z, .increaseItemQuantity ; if so, increase the item's quantity
  36. inc hl
  37. ld a, [hl]
  38. cp $ff ; is it the end of the table?
  39. jr nz, .loop
  40. .addNewItem ; add an item not yet in the inventory
  41. pop hl
  42. ld a, d
  43. and a ; is there room for a new item slot?
  44. jr z, .done
  45. ; if there is room
  46. inc [hl] ; increment the number of items in the inventory
  47. ld a, [hl] ; the number of items will be the index of the new item
  48. add a
  49. dec a
  50. ld c, a
  51. ld b, 0
  52. add hl, bc ; hl = address to store the item
  53. ld a, [wcf91]
  54. ld [hli], a ; store item ID
  55. ld a, [wItemQuantity]
  56. ld [hli], a ; store item quantity
  57. ld [hl], $ff ; store terminator
  58. jp .success
  59. .increaseItemQuantity ; increase the quantity of an item already in the inventory
  60. ld a, [wItemQuantity]
  61. ld b, a ; b = quantity to add
  62. ld a, [hl] ; a = existing item quantity
  63. add b ; a = new item quantity
  64. cp 100
  65. jp c, .storeNewQuantity ; if the new quantity is less than 100, store it
  66. ; if the new quantity is greater than or equal to 100,
  67. ; try to max out the current slot and add the rest in a new slot
  68. sub 99
  69. ld [wItemQuantity], a ; a = amount left over (to put in the new slot)
  70. ld a, d
  71. and a ; is there room for a new item slot?
  72. jr z, .increaseItemQuantityFailed
  73. ; if so, store 99 in the current slot and store the rest in a new slot
  74. ld a, 99
  75. ld [hli], a
  76. jp .loop
  77. .increaseItemQuantityFailed
  78. pop hl
  79. and a
  80. jr .done
  81. .storeNewQuantity
  82. ld [hl], a
  83. pop hl
  84. .success
  85. scf
  86. .done
  87. pop hl
  88. pop de
  89. pop bc
  90. pop bc
  91. ld a, b
  92. ld [wItemQuantity], a ; restore the initial value from when the function was called
  93. ret
  94. ; function to remove an item (in varying quantities) from the player's bag or PC box
  95. ; INPUT:
  96. ; hl = address of inventory (either wNumBagItems or wNumBoxItems)
  97. ; [wWhichPokemon] = index (within the inventory) of the item to remove
  98. ; [wItemQuantity] = quantity to remove
  99. RemoveItemFromInventory_:
  100. push hl
  101. inc hl
  102. ld a, [wWhichPokemon] ; index (within the inventory) of the item being removed
  103. sla a
  104. add l
  105. ld l, a
  106. jr nc, .noCarry
  107. inc h
  108. .noCarry
  109. inc hl
  110. ld a, [wItemQuantity] ; quantity being removed
  111. ld e, a
  112. ld a, [hl] ; a = current quantity
  113. sub e
  114. ld [hld], a ; store new quantity
  115. ld [wMaxItemQuantity], a
  116. and a
  117. jr nz, .skipMovingUpSlots
  118. ; if the remaining quantity is 0,
  119. ; remove the emptied item slot and move up all the following item slots
  120. .moveSlotsUp
  121. ld e, l
  122. ld d, h
  123. inc de
  124. inc de ; de = address of the slot following the emptied one
  125. .loop ; loop to move up the following slots
  126. ld a, [de]
  127. inc de
  128. ld [hli], a
  129. cp $ff
  130. jr nz, .loop
  131. ; update menu info
  132. xor a
  133. ld [wListScrollOffset], a
  134. ld [wCurrentMenuItem], a
  135. ld [wBagSavedMenuItem], a
  136. ld [wSavedListScrollOffset], a
  137. pop hl
  138. ld a, [hl] ; a = number of items in inventory
  139. dec a ; decrement the number of items
  140. ld [hl], a ; store new number of items
  141. ld [wListCount], a
  142. cp 2
  143. jr c, .done
  144. ld [wMaxMenuItem], a
  145. jr .done
  146. .skipMovingUpSlots
  147. pop hl
  148. .done
  149. ret