2017-01-01 01:23:54 +00:00
|
|
|
; function to add an item (in varying quantities) to the player's bag or PC box
|
|
|
|
; INPUT:
|
|
|
|
; hl = address of inventory (either wNumBagItems or wNumBoxItems)
|
|
|
|
; [wcf91] = item ID
|
|
|
|
; [wItemQuantity] = item quantity
|
|
|
|
; sets carry flag if successful, unsets carry flag if unsuccessful
|
|
|
|
AddItemToInventory_:
|
2017-12-30 00:54:27 +00:00
|
|
|
ld a, [wItemQuantity] ; a = item quantity
|
2017-01-01 01:23:54 +00:00
|
|
|
push af
|
|
|
|
push bc
|
|
|
|
push de
|
|
|
|
push hl
|
|
|
|
push hl
|
2017-12-30 00:54:27 +00:00
|
|
|
ld d, PC_ITEM_CAPACITY ; how many items the PC can hold
|
|
|
|
ld a, wNumBagItems & $FF
|
2017-01-01 01:23:54 +00:00
|
|
|
cp l
|
2017-12-30 00:54:27 +00:00
|
|
|
jr nz, .checkIfInventoryFull
|
|
|
|
ld a, wNumBagItems >> 8
|
2017-01-01 01:23:54 +00:00
|
|
|
cp h
|
2017-12-30 00:54:27 +00:00
|
|
|
jr nz, .checkIfInventoryFull
|
2017-01-01 01:23:54 +00:00
|
|
|
; if the destination is the bag
|
2017-12-30 00:54:27 +00:00
|
|
|
ld d, BAG_ITEM_CAPACITY ; how many items the bag can hold
|
2017-01-01 01:23:54 +00:00
|
|
|
.checkIfInventoryFull
|
2017-12-30 00:54:27 +00:00
|
|
|
ld a, [hl]
|
2017-01-01 01:23:54 +00:00
|
|
|
sub d
|
2017-12-30 00:54:27 +00:00
|
|
|
ld d, a
|
|
|
|
ld a, [hli]
|
2017-01-01 01:23:54 +00:00
|
|
|
and a
|
2017-12-30 00:54:27 +00:00
|
|
|
jr z, .addNewItem
|
2017-01-01 01:23:54 +00:00
|
|
|
.loop
|
2017-12-30 00:54:27 +00:00
|
|
|
ld a, [hli]
|
|
|
|
ld b, a ; b = ID of current item in table
|
|
|
|
ld a, [wcf91] ; a = ID of item being added
|
2017-01-01 01:23:54 +00:00
|
|
|
cp b ; does the current item in the table match the item being added?
|
2017-12-30 00:54:27 +00:00
|
|
|
jp z, .increaseItemQuantity ; if so, increase the item's quantity
|
2017-01-01 01:23:54 +00:00
|
|
|
inc hl
|
2017-12-30 00:54:27 +00:00
|
|
|
ld a, [hl]
|
|
|
|
cp $ff ; is it the end of the table?
|
|
|
|
jr nz, .loop
|
2017-01-01 01:23:54 +00:00
|
|
|
.addNewItem ; add an item not yet in the inventory
|
|
|
|
pop hl
|
2017-12-30 00:54:27 +00:00
|
|
|
ld a, d
|
2017-01-01 01:23:54 +00:00
|
|
|
and a ; is there room for a new item slot?
|
2017-12-30 00:54:27 +00:00
|
|
|
jr z, .done
|
2017-01-01 01:23:54 +00:00
|
|
|
; if there is room
|
|
|
|
inc [hl] ; increment the number of items in the inventory
|
2017-12-30 00:54:27 +00:00
|
|
|
ld a, [hl] ; the number of items will be the index of the new item
|
2017-01-01 01:23:54 +00:00
|
|
|
add a
|
|
|
|
dec a
|
2017-12-30 00:54:27 +00:00
|
|
|
ld c, a
|
|
|
|
ld b, 0
|
|
|
|
add hl, bc ; hl = address to store the item
|
|
|
|
ld a, [wcf91]
|
|
|
|
ld [hli], a ; store item ID
|
|
|
|
ld a, [wItemQuantity]
|
|
|
|
ld [hli], a ; store item quantity
|
|
|
|
ld [hl], $ff ; store terminator
|
2017-01-01 01:23:54 +00:00
|
|
|
jp .success
|
|
|
|
.increaseItemQuantity ; increase the quantity of an item already in the inventory
|
2017-12-30 00:54:27 +00:00
|
|
|
ld a, [wItemQuantity]
|
|
|
|
ld b, a ; b = quantity to add
|
|
|
|
ld a, [hl] ; a = existing item quantity
|
2017-01-01 01:23:54 +00:00
|
|
|
add b ; a = new item quantity
|
2017-12-30 00:54:27 +00:00
|
|
|
cp 100
|
|
|
|
jp c, .storeNewQuantity ; if the new quantity is less than 100, store it
|
2017-01-01 01:23:54 +00:00
|
|
|
; if the new quantity is greater than or equal to 100,
|
|
|
|
; try to max out the current slot and add the rest in a new slot
|
2017-12-30 00:54:27 +00:00
|
|
|
sub 99
|
|
|
|
ld [wItemQuantity], a ; a = amount left over (to put in the new slot)
|
|
|
|
ld a, d
|
2017-01-01 01:23:54 +00:00
|
|
|
and a ; is there room for a new item slot?
|
2017-12-30 00:54:27 +00:00
|
|
|
jr z, .increaseItemQuantityFailed
|
2017-01-01 01:23:54 +00:00
|
|
|
; if so, store 99 in the current slot and store the rest in a new slot
|
2017-12-30 00:54:27 +00:00
|
|
|
ld a, 99
|
|
|
|
ld [hli], a
|
2017-01-01 01:23:54 +00:00
|
|
|
jp .loop
|
|
|
|
.increaseItemQuantityFailed
|
|
|
|
pop hl
|
|
|
|
and a
|
|
|
|
jr .done
|
|
|
|
.storeNewQuantity
|
2017-12-30 00:54:27 +00:00
|
|
|
ld [hl], a
|
2017-01-01 01:23:54 +00:00
|
|
|
pop hl
|
|
|
|
.success
|
|
|
|
scf
|
|
|
|
.done
|
|
|
|
pop hl
|
|
|
|
pop de
|
|
|
|
pop bc
|
|
|
|
pop bc
|
2017-12-30 00:54:27 +00:00
|
|
|
ld a, b
|
|
|
|
ld [wItemQuantity], a ; restore the initial value from when the function was called
|
2017-01-01 01:23:54 +00:00
|
|
|
ret
|
|
|
|
|
|
|
|
; function to remove an item (in varying quantities) from the player's bag or PC box
|
|
|
|
; INPUT:
|
|
|
|
; hl = address of inventory (either wNumBagItems or wNumBoxItems)
|
|
|
|
; [wWhichPokemon] = index (within the inventory) of the item to remove
|
|
|
|
; [wItemQuantity] = quantity to remove
|
|
|
|
RemoveItemFromInventory_:
|
|
|
|
push hl
|
|
|
|
inc hl
|
2017-12-30 00:54:27 +00:00
|
|
|
ld a, [wWhichPokemon] ; index (within the inventory) of the item being removed
|
2017-01-01 01:23:54 +00:00
|
|
|
sla a
|
|
|
|
add l
|
2017-12-30 00:54:27 +00:00
|
|
|
ld l, a
|
|
|
|
jr nc, .noCarry
|
2017-01-01 01:23:54 +00:00
|
|
|
inc h
|
|
|
|
.noCarry
|
|
|
|
inc hl
|
2017-12-30 00:54:27 +00:00
|
|
|
ld a, [wItemQuantity] ; quantity being removed
|
|
|
|
ld e, a
|
|
|
|
ld a, [hl] ; a = current quantity
|
2017-01-01 01:23:54 +00:00
|
|
|
sub e
|
2017-12-30 00:54:27 +00:00
|
|
|
ld [hld], a ; store new quantity
|
|
|
|
ld [wMaxItemQuantity], a
|
2017-01-01 01:23:54 +00:00
|
|
|
and a
|
2017-12-30 00:54:27 +00:00
|
|
|
jr nz, .skipMovingUpSlots
|
2017-01-01 01:23:54 +00:00
|
|
|
; if the remaining quantity is 0,
|
|
|
|
; remove the emptied item slot and move up all the following item slots
|
|
|
|
.moveSlotsUp
|
2017-12-30 00:54:27 +00:00
|
|
|
ld e, l
|
|
|
|
ld d, h
|
2017-01-01 01:23:54 +00:00
|
|
|
inc de
|
|
|
|
inc de ; de = address of the slot following the emptied one
|
|
|
|
.loop ; loop to move up the following slots
|
2017-12-30 00:54:27 +00:00
|
|
|
ld a, [de]
|
2017-01-01 01:23:54 +00:00
|
|
|
inc de
|
2017-12-30 00:54:27 +00:00
|
|
|
ld [hli], a
|
|
|
|
cp $ff
|
|
|
|
jr nz, .loop
|
2017-01-01 01:23:54 +00:00
|
|
|
; update menu info
|
|
|
|
xor a
|
2017-12-30 00:54:27 +00:00
|
|
|
ld [wListScrollOffset], a
|
|
|
|
ld [wCurrentMenuItem], a
|
|
|
|
ld [wBagSavedMenuItem], a
|
|
|
|
ld [wSavedListScrollOffset], a
|
2017-01-01 01:23:54 +00:00
|
|
|
pop hl
|
2017-12-30 00:54:27 +00:00
|
|
|
ld a, [hl] ; a = number of items in inventory
|
2017-01-01 01:23:54 +00:00
|
|
|
dec a ; decrement the number of items
|
2017-12-30 00:54:27 +00:00
|
|
|
ld [hl], a ; store new number of items
|
|
|
|
ld [wListCount], a
|
|
|
|
cp 2
|
|
|
|
jr c, .done
|
|
|
|
ld [wMaxMenuItem], a
|
2017-01-01 01:23:54 +00:00
|
|
|
jr .done
|
|
|
|
.skipMovingUpSlots
|
|
|
|
pop hl
|
|
|
|
.done
|
|
|
|
ret
|