pokecrystal-board/engine/items/switch_items.asm

273 lines
4.2 KiB
NASM
Raw Normal View History

2018-06-24 07:09:41 -07:00
SwitchItemsInBag:
2015-12-13 11:15:16 -08:00
ld a, [wSwitchItem]
and a
jr z, .init
ld b, a
2015-12-17 14:51:32 -08:00
ld a, [wScrollingMenuCursorPosition]
2015-12-13 11:15:16 -08:00
inc a
cp b
jr z, .trivial
2015-12-17 14:51:32 -08:00
ld a, [wScrollingMenuCursorPosition]
2015-12-23 11:00:29 -08:00
call ItemSwitch_GetNthItem
2015-12-13 11:15:16 -08:00
ld a, [hl]
2015-12-23 11:00:29 -08:00
cp -1
2015-12-13 11:15:16 -08:00
ret z
ld a, [wSwitchItem]
dec a
ld [wSwitchItem], a
call .try_combining_stacks
jp c, .combine_stacks
2015-12-17 14:51:32 -08:00
ld a, [wScrollingMenuCursorPosition]
2015-12-13 11:15:16 -08:00
ld c, a
ld a, [wSwitchItem]
cp c
jr c, .above
jr .below
2015-12-13 11:15:16 -08:00
.init:
2015-12-17 14:51:32 -08:00
ld a, [wScrollingMenuCursorPosition]
2015-12-13 11:15:16 -08:00
inc a
ld [wSwitchItem], a
ret
.trivial:
2015-12-13 11:15:16 -08:00
xor a
ld [wSwitchItem], a
ret
.below:
2015-12-13 11:15:16 -08:00
ld a, [wSwitchItem]
call ItemSwitch_CopyItemToBuffer
2015-12-17 14:51:32 -08:00
ld a, [wScrollingMenuCursorPosition]
2015-12-13 11:15:16 -08:00
ld d, a
ld a, [wSwitchItem]
ld e, a
call ItemSwitch_GetItemOffset
2015-12-13 11:15:16 -08:00
push bc
ld a, [wSwitchItem]
2015-12-23 11:00:29 -08:00
call ItemSwitch_GetNthItem
2015-12-13 11:15:16 -08:00
dec hl
push hl
call ItemSwitch_GetItemFormatSize
2015-12-13 11:15:16 -08:00
add hl, bc
ld d, h
ld e, l
pop hl
pop bc
call ItemSwitch_BackwardsCopyBytes
2015-12-17 14:51:32 -08:00
ld a, [wScrollingMenuCursorPosition]
call ItemSwitch_CopyBufferToItem
2015-12-13 11:15:16 -08:00
xor a
ld [wSwitchItem], a
ret
.above:
2015-12-13 11:15:16 -08:00
ld a, [wSwitchItem]
call ItemSwitch_CopyItemToBuffer
2015-12-17 14:51:32 -08:00
ld a, [wScrollingMenuCursorPosition]
2015-12-13 11:15:16 -08:00
ld d, a
ld a, [wSwitchItem]
ld e, a
call ItemSwitch_GetItemOffset
2015-12-13 11:15:16 -08:00
push bc
ld a, [wSwitchItem]
2015-12-23 11:00:29 -08:00
call ItemSwitch_GetNthItem
2015-12-13 11:15:16 -08:00
ld d, h
ld e, l
call ItemSwitch_GetItemFormatSize
2015-12-13 11:15:16 -08:00
add hl, bc
pop bc
call CopyBytes
2015-12-17 14:51:32 -08:00
ld a, [wScrollingMenuCursorPosition]
call ItemSwitch_CopyBufferToItem
2015-12-13 11:15:16 -08:00
xor a
ld [wSwitchItem], a
ret
.try_combining_stacks:
2015-12-13 11:15:16 -08:00
ld a, [wSwitchItem]
2015-12-23 11:00:29 -08:00
call ItemSwitch_GetNthItem
2015-12-13 11:15:16 -08:00
ld d, h
ld e, l
2015-12-17 14:51:32 -08:00
ld a, [wScrollingMenuCursorPosition]
2015-12-23 11:00:29 -08:00
call ItemSwitch_GetNthItem
2015-12-13 11:15:16 -08:00
ld a, [de]
cp [hl]
jr nz, .no_combine
2015-12-17 14:51:32 -08:00
ld a, [wScrollingMenuCursorPosition]
call ItemSwitch_GetItemQuantity
cp MAX_ITEM_STACK
jr z, .no_combine
2015-12-13 11:15:16 -08:00
ld a, [wSwitchItem]
call ItemSwitch_GetItemQuantity
cp MAX_ITEM_STACK
jr nz, .combine
.no_combine
2015-12-13 11:15:16 -08:00
and a
ret
.combine
2015-12-13 11:15:16 -08:00
scf
ret
.combine_stacks:
2015-12-13 11:15:16 -08:00
ld a, [wSwitchItem]
2015-12-23 11:00:29 -08:00
call ItemSwitch_GetNthItem
2015-12-13 11:15:16 -08:00
inc hl
push hl
2015-12-17 14:51:32 -08:00
ld a, [wScrollingMenuCursorPosition]
2015-12-23 11:00:29 -08:00
call ItemSwitch_GetNthItem
2015-12-13 11:15:16 -08:00
inc hl
ld a, [hl]
pop hl
add [hl]
cp MAX_ITEM_STACK + 1
jr c, .merge_stacks
sub MAX_ITEM_STACK
2015-12-13 11:15:16 -08:00
push af
2015-12-17 14:51:32 -08:00
ld a, [wScrollingMenuCursorPosition]
2015-12-23 11:00:29 -08:00
call ItemSwitch_GetNthItem
2015-12-13 11:15:16 -08:00
inc hl
ld [hl], MAX_ITEM_STACK
2015-12-13 11:15:16 -08:00
ld a, [wSwitchItem]
2015-12-23 11:00:29 -08:00
call ItemSwitch_GetNthItem
2015-12-13 11:15:16 -08:00
inc hl
pop af
ld [hl], a
xor a
ld [wSwitchItem], a
ret
.merge_stacks:
2015-12-13 11:15:16 -08:00
push af
2015-12-17 14:51:32 -08:00
ld a, [wScrollingMenuCursorPosition]
2015-12-23 11:00:29 -08:00
call ItemSwitch_GetNthItem
2015-12-13 11:15:16 -08:00
inc hl
pop af
ld [hl], a
ld hl, wMenuData_ItemsPointerAddr
2015-12-13 11:15:16 -08:00
ld a, [hli]
ld h, [hl]
ld l, a
ld a, [wSwitchItem]
cp [hl]
jr nz, .not_combining_last_item
2015-12-13 11:15:16 -08:00
dec [hl]
ld a, [wSwitchItem]
2015-12-23 11:00:29 -08:00
call ItemSwitch_GetNthItem
ld [hl], -1 ; end
2015-12-13 11:15:16 -08:00
xor a
ld [wSwitchItem], a
ret
.not_combining_last_item:
2015-12-13 11:15:16 -08:00
dec [hl]
call ItemSwitch_GetItemFormatSize
2015-12-13 11:15:16 -08:00
push bc
ld a, [wSwitchItem]
2015-12-23 11:00:29 -08:00
call ItemSwitch_GetNthItem
2015-12-13 11:15:16 -08:00
pop bc
push hl
add hl, bc
pop de
.copy_loop
2015-12-13 11:15:16 -08:00
ld a, [hli]
ld [de], a
inc de
cp -1 ; end?
jr nz, .copy_loop
2015-12-13 11:15:16 -08:00
xor a
ld [wSwitchItem], a
ret
ItemSwitch_CopyItemToBuffer:
2015-12-23 11:00:29 -08:00
call ItemSwitch_GetNthItem
ld de, wSwitchItemBuffer
call ItemSwitch_GetItemFormatSize
2015-12-13 11:15:16 -08:00
call CopyBytes
ret
ItemSwitch_CopyBufferToItem:
2015-12-23 11:00:29 -08:00
call ItemSwitch_GetNthItem
2015-12-13 11:15:16 -08:00
ld d, h
ld e, l
ld hl, wSwitchItemBuffer
call ItemSwitch_GetItemFormatSize
2015-12-13 11:15:16 -08:00
call CopyBytes
ret
2018-06-24 07:09:41 -07:00
ItemSwitch_GetNthItem:
2015-12-13 11:15:16 -08:00
push af
call ItemSwitch_GetItemFormatSize
ld hl, wMenuData_ItemsPointerAddr
2015-12-13 11:15:16 -08:00
ld a, [hli]
ld h, [hl]
ld l, a
inc hl
pop af
call AddNTimes
ret
ItemSwitch_GetItemOffset:
2015-12-13 11:15:16 -08:00
push hl
call ItemSwitch_GetItemFormatSize
2015-12-13 11:15:16 -08:00
ld a, d
sub e
2015-12-23 11:00:29 -08:00
jr nc, .dont_negate
2015-12-13 11:15:16 -08:00
dec a
cpl
2015-12-23 11:00:29 -08:00
.dont_negate
2015-12-13 11:15:16 -08:00
ld hl, 0
call AddNTimes
ld b, h
ld c, l
pop hl
ret
ItemSwitch_GetItemFormatSize:
2015-12-13 11:15:16 -08:00
push hl
ld a, [wMenuData_ScrollingMenuItemFormat]
2015-12-13 11:15:16 -08:00
ld c, a
ld b, 0
ld hl, .item_format_sizes
2015-12-13 11:15:16 -08:00
add hl, bc
add hl, bc
2015-12-13 11:15:16 -08:00
ld c, [hl]
inc hl
ld b, [hl]
pop hl
ret
.item_format_sizes:
; entries correspond to SCROLLINGMENU_ITEMS_* constants
dw 0 ; unused
dw 1 ; SCROLLINGMENU_ITEMS_NORMAL
dw 2 ; SCROLLINGMENU_ITEMS_QUANTITY
2015-12-13 11:15:16 -08:00
ItemSwitch_GetItemQuantity:
2015-12-13 11:15:16 -08:00
push af
call ItemSwitch_GetItemFormatSize
2015-12-13 11:15:16 -08:00
ld a, c
2015-12-23 11:00:29 -08:00
cp 2
jr nz, .no_quantity
2015-12-13 11:15:16 -08:00
pop af
2015-12-23 11:00:29 -08:00
call ItemSwitch_GetNthItem
2015-12-13 11:15:16 -08:00
inc hl
ld a, [hl]
ret
.no_quantity
2015-12-13 11:15:16 -08:00
pop af
ld a, 1
2015-12-13 11:15:16 -08:00
ret
ItemSwitch_BackwardsCopyBytes:
2015-12-13 11:15:16 -08:00
.loop
ld a, [hld]
ld [de], a
dec de
dec bc
ld a, b
or c
jr nz, .loop
ret