pokered/engine/menu/swap_items.asm

150 lines
3.6 KiB
NASM
Raw Normal View History

2016-09-18 01:17:57 +00:00
HandleItemListSwapping:
ld a, [wListMenuID]
cp ITEMLISTMENU
jp nz, DisplayListMenuIDLoop ; only rearrange item list menus
2016-09-18 01:17:57 +00:00
push hl
ld hl, wListPointer
ld a, [hli]
ld h, [hl]
ld l, a
2016-09-18 01:17:57 +00:00
inc hl ; hl = beginning of list entries
ld a, [wCurrentMenuItem]
ld b, a
ld a, [wListScrollOffset]
2016-09-18 01:17:57 +00:00
add b
add a
ld c, a
ld b, 0
add hl, bc ; hl = address of currently selected item entry
ld a, [hl]
2016-09-18 01:17:57 +00:00
pop hl
inc a
jp z, DisplayListMenuIDLoop ; ignore attempts to swap the Cancel menu item
ld a, [wMenuItemToSwap] ; ID of item chosen for swapping (counts from 1)
2016-09-18 01:17:57 +00:00
and a ; has the first item to swap already been chosen?
jr nz, .swapItems
2016-09-18 01:17:57 +00:00
; if not, set the currently selected item as the first item
ld a, [wCurrentMenuItem]
2016-09-18 01:17:57 +00:00
inc a
ld b, a
ld a, [wListScrollOffset] ; index of top (visible) menu item within the list
2016-09-18 01:17:57 +00:00
add b
ld [wMenuItemToSwap], a ; ID of item chosen for swapping (counts from 1)
ld c, 20
2016-09-18 01:17:57 +00:00
call DelayFrames
jp DisplayListMenuIDLoop
.swapItems
ld a, [wCurrentMenuItem]
2016-09-18 01:17:57 +00:00
inc a
ld b, a
ld a, [wListScrollOffset]
2016-09-18 01:17:57 +00:00
add b
ld b, a
ld a, [wMenuItemToSwap] ; ID of item chosen for swapping (counts from 1)
2016-09-18 01:17:57 +00:00
cp b ; is the currently selected item the same as the first item to swap?
jp z, DisplayListMenuIDLoop ; ignore attempts to swap an item with itself
2016-09-18 01:17:57 +00:00
dec a
ld [wMenuItemToSwap], a ; ID of item chosen for swapping (counts from 1)
ld c, 20
2016-09-18 01:17:57 +00:00
call DelayFrames
push hl
push de
ld hl, wListPointer
ld a, [hli]
ld h, [hl]
ld l, a
2016-09-18 01:17:57 +00:00
inc hl ; hl = beginning of list entries
ld d, h
ld e, l ; de = beginning of list entries
ld a, [wCurrentMenuItem]
ld b, a
ld a, [wListScrollOffset]
2016-09-18 01:17:57 +00:00
add b
add a
ld c, a
ld b, 0
add hl, bc ; hl = address of currently selected item entry
ld a, [wMenuItemToSwap] ; ID of item chosen for swapping (counts from 1)
2016-09-18 01:17:57 +00:00
add a
add e
ld e, a
jr nc, .noCarry
2016-09-18 01:17:57 +00:00
inc d
.noCarry ; de = address of first item to swap
ld a, [de]
ld b, a
ld a, [hli]
2016-09-18 01:17:57 +00:00
cp b
jr z, .swapSameItemType
2016-09-18 01:17:57 +00:00
.swapDifferentItems
ld [$ff95], a ; [$ff95] = second item ID
ld a, [hld]
ld [$ff96], a ; [$ff96] = second item quantity
ld a, [de]
ld [hli], a ; put first item ID in second item slot
2016-09-18 01:17:57 +00:00
inc de
ld a, [de]
ld [hl], a ; put first item quantity in second item slot
ld a, [$ff96]
ld [de], a ; put second item quantity in first item slot
2016-09-18 01:17:57 +00:00
dec de
ld a, [$ff95]
ld [de], a ; put second item ID in first item slot
2016-09-18 01:17:57 +00:00
xor a
ld [wMenuItemToSwap], a ; 0 means no item is currently being swapped
2016-09-18 01:17:57 +00:00
pop de
pop hl
jp DisplayListMenuIDLoop
.swapSameItemType
inc de
ld a, [hl]
ld b, a
ld a, [de]
2016-09-18 01:17:57 +00:00
add b ; a = sum of both item quantities
cp 100 ; is the sum too big for one item slot?
jr c, .combineItemSlots
2016-09-18 01:17:57 +00:00
; swap enough items from the first slot to max out the second slot if they can't be combined
sub 99
ld [de], a
ld a, 99
ld [hl], a
2016-09-18 01:17:57 +00:00
jr .done
.combineItemSlots
ld [hl], a ; put the sum in the second item slot
ld hl, wListPointer
ld a, [hli]
ld h, [hl]
ld l, a
2016-09-18 01:17:57 +00:00
dec [hl] ; decrease the number of items
ld a, [hl]
ld [wListCount], a ; update number of items variable
cp 1
jr nz, .skipSettingMaxMenuItemID
ld [wMaxMenuItem], a ; if the number of items is only one now, update the max menu item ID
2016-09-18 01:17:57 +00:00
.skipSettingMaxMenuItemID
dec de
ld h, d
ld l, e
2016-09-18 01:17:57 +00:00
inc hl
inc hl ; hl = address of item after first item to swap
.moveItemsUpLoop ; erase the first item slot and move up all the following item slots to fill the gap
ld a, [hli]
ld [de], a
2016-09-18 01:17:57 +00:00
inc de
inc a ; reached the $ff terminator?
jr z, .afterMovingItemsUp
ld a, [hli]
ld [de], a
2016-09-18 01:17:57 +00:00
inc de
jr .moveItemsUpLoop
.afterMovingItemsUp
xor a
ld [wListScrollOffset], a
ld [wCurrentMenuItem], a
2016-09-18 01:17:57 +00:00
.done
xor a
ld [wMenuItemToSwap], a ; 0 means no item is currently being swapped
2016-09-18 01:17:57 +00:00
pop de
pop hl
jp DisplayListMenuIDLoop