pokecrystal/engine/items/items.asm

582 lines
6.7 KiB
NASM
Raw Normal View History

2018-06-24 14:09:41 +00:00
_ReceiveItem::
2016-03-27 16:47:28 +00:00
call DoesHLEqualNumItems
jp nz, PutItemInPocket
push hl
call CheckItemPocket
pop de
ld a, [wItemAttributeParamBuffer]
dec a
ld hl, .Pockets
rst JumpTable
ret
2018-06-24 14:09:41 +00:00
.Pockets:
2018-01-26 03:19:24 +00:00
; entries correspond to item types
2016-03-27 16:47:28 +00:00
dw .Item
dw .KeyItem
dw .Ball
dw .TMHM
2018-06-24 14:09:41 +00:00
.Item:
2016-03-27 16:47:28 +00:00
ld h, d
ld l, e
jp PutItemInPocket
2018-06-24 14:09:41 +00:00
.KeyItem:
2016-03-27 16:47:28 +00:00
ld h, d
ld l, e
jp ReceiveKeyItem
2018-06-24 14:09:41 +00:00
.Ball:
2018-01-23 22:39:09 +00:00
ld hl, wNumBalls
2016-03-27 16:47:28 +00:00
jp PutItemInPocket
2018-06-24 14:09:41 +00:00
.TMHM:
2016-03-27 16:47:28 +00:00
ld h, d
ld l, e
2018-01-23 22:39:09 +00:00
ld a, [wCurItem]
2016-03-27 16:47:28 +00:00
ld c, a
call GetTMHMNumber
jp ReceiveTMHM
2018-06-24 14:09:41 +00:00
_TossItem::
2016-03-27 16:47:28 +00:00
call DoesHLEqualNumItems
jr nz, .remove
push hl
call CheckItemPocket
pop de
ld a, [wItemAttributeParamBuffer]
dec a
ld hl, .Pockets
rst JumpTable
ret
.Pockets:
2018-01-26 03:19:24 +00:00
; entries correspond to item types
2016-03-27 16:47:28 +00:00
dw .Item
dw .KeyItem
dw .Ball
dw .TMHM
2018-06-24 14:09:41 +00:00
.Ball:
2018-01-23 22:39:09 +00:00
ld hl, wNumBalls
2016-03-27 16:47:28 +00:00
jp RemoveItemFromPocket
2018-06-24 14:09:41 +00:00
.TMHM:
2016-03-27 16:47:28 +00:00
ld h, d
ld l, e
2018-01-23 22:39:09 +00:00
ld a, [wCurItem]
2016-03-27 16:47:28 +00:00
ld c, a
call GetTMHMNumber
jp TossTMHM
2018-06-24 14:09:41 +00:00
.KeyItem:
2016-03-27 16:47:28 +00:00
ld h, d
ld l, e
jp TossKeyItem
2018-06-24 14:09:41 +00:00
.Item:
2016-03-27 16:47:28 +00:00
ld h, d
ld l, e
.remove
jp RemoveItemFromPocket
2018-06-24 14:09:41 +00:00
_CheckItem::
2016-03-27 16:47:28 +00:00
call DoesHLEqualNumItems
jr nz, .nope
push hl
call CheckItemPocket
pop de
ld a, [wItemAttributeParamBuffer]
dec a
ld hl, .Pockets
rst JumpTable
ret
.Pockets:
2018-01-26 03:19:24 +00:00
; entries correspond to item types
2016-03-27 16:47:28 +00:00
dw .Item
dw .KeyItem
dw .Ball
dw .TMHM
2018-06-24 14:09:41 +00:00
.Ball:
2018-01-23 22:39:09 +00:00
ld hl, wNumBalls
2016-03-27 16:47:28 +00:00
jp CheckTheItem
2018-06-24 14:09:41 +00:00
.TMHM:
2016-03-27 16:47:28 +00:00
ld h, d
ld l, e
2018-01-23 22:39:09 +00:00
ld a, [wCurItem]
2016-03-27 16:47:28 +00:00
ld c, a
call GetTMHMNumber
jp CheckTMHM
2018-06-24 14:09:41 +00:00
.KeyItem:
2016-03-27 16:47:28 +00:00
ld h, d
ld l, e
jp CheckKeyItems
2018-06-24 14:09:41 +00:00
.Item:
2016-03-27 16:47:28 +00:00
ld h, d
ld l, e
.nope
jp CheckTheItem
2018-06-24 14:09:41 +00:00
DoesHLEqualNumItems:
2016-03-27 16:47:28 +00:00
ld a, l
2018-01-23 22:39:09 +00:00
cp LOW(wNumItems)
2016-03-27 16:47:28 +00:00
ret nz
ld a, h
2018-01-23 22:39:09 +00:00
cp HIGH(wNumItems)
2016-03-27 16:47:28 +00:00
ret
2018-06-24 14:09:41 +00:00
GetPocketCapacity:
2016-03-27 16:47:28 +00:00
ld c, MAX_ITEMS
ld a, e
2018-01-23 22:39:09 +00:00
cp LOW(wNumItems)
2016-03-27 16:47:28 +00:00
jr nz, .not_bag
ld a, d
2018-01-23 22:39:09 +00:00
cp HIGH(wNumItems)
2016-03-27 16:47:28 +00:00
ret z
.not_bag
ld c, MAX_PC_ITEMS
ld a, e
2018-01-23 22:39:09 +00:00
cp LOW(wPCItems)
2016-03-27 16:47:28 +00:00
jr nz, .not_pc
ld a, d
2018-01-23 22:39:09 +00:00
cp HIGH(wPCItems)
2016-03-27 16:47:28 +00:00
ret z
.not_pc
ld c, MAX_BALLS
ret
2018-06-24 14:09:41 +00:00
PutItemInPocket:
2016-03-27 16:47:28 +00:00
ld d, h
ld e, l
inc hl
2018-01-23 22:39:09 +00:00
ld a, [wCurItem]
2016-03-27 16:47:28 +00:00
ld c, a
ld b, 0
.loop
ld a, [hli]
cp -1
jr z, .terminator
cp c
jr nz, .next
ld a, 99
sub [hl]
add b
ld b, a
ld a, [wItemQuantityChangeBuffer]
cp b
jr z, .ok
jr c, .ok
.next
inc hl
jr .loop
.terminator
call GetPocketCapacity
ld a, [de]
cp c
jr c, .ok
and a
ret
.ok
ld h, d
ld l, e
2018-01-23 22:39:09 +00:00
ld a, [wCurItem]
2016-03-27 16:47:28 +00:00
ld c, a
ld a, [wItemQuantityChangeBuffer]
ld [wItemQuantityBuffer], a
.loop2
inc hl
ld a, [hli]
cp -1
jr z, .terminator2
cp c
jr nz, .loop2
ld a, [wItemQuantityBuffer]
add [hl]
cp 100
jr nc, .newstack
ld [hl], a
jr .done
.newstack
ld [hl], 99
sub 99
ld [wItemQuantityBuffer], a
jr .loop2
.terminator2
dec hl
2018-01-23 22:39:09 +00:00
ld a, [wCurItem]
2016-03-27 16:47:28 +00:00
ld [hli], a
ld a, [wItemQuantityBuffer]
ld [hli], a
ld [hl], -1
ld h, d
ld l, e
inc [hl]
.done
scf
ret
2018-06-24 14:09:41 +00:00
RemoveItemFromPocket:
2016-03-27 16:47:28 +00:00
ld d, h
ld e, l
ld a, [hli]
ld c, a
2018-01-23 22:39:09 +00:00
ld a, [wCurItemQuantity]
2016-03-27 16:47:28 +00:00
cp c
jr nc, .ok ; memory
ld c, a
ld b, $0
add hl, bc
add hl, bc
2018-01-23 22:39:09 +00:00
ld a, [wCurItem]
2016-03-27 16:47:28 +00:00
cp [hl]
inc hl
jr z, .skip
ld h, d
ld l, e
inc hl
.ok
2018-01-23 22:39:09 +00:00
ld a, [wCurItem]
2016-03-27 16:47:28 +00:00
ld b, a
.loop
ld a, [hli]
cp b
jr z, .skip
cp -1
jr z, .nope
inc hl
jr .loop
.skip
ld a, [wItemQuantityChangeBuffer]
ld b, a
ld a, [hl]
sub b
jr c, .nope
ld [hl], a
ld [wItemQuantityBuffer], a
and a
jr nz, .yup
dec hl
ld b, h
ld c, l
inc hl
inc hl
.loop2
ld a, [hli]
ld [bc], a
inc bc
cp -1
jr nz, .loop2
ld h, d
ld l, e
dec [hl]
.yup
scf
ret
.nope
and a
ret
2018-06-24 14:09:41 +00:00
CheckTheItem:
2018-01-23 22:39:09 +00:00
ld a, [wCurItem]
2016-03-27 16:47:28 +00:00
ld c, a
.loop
inc hl
ld a, [hli]
cp -1
jr z, .done
cp c
jr nz, .loop
scf
ret
.done
and a
ret
2018-06-24 14:09:41 +00:00
ReceiveKeyItem:
2018-01-23 22:39:09 +00:00
ld hl, wNumKeyItems
2016-03-27 16:47:28 +00:00
ld a, [hli]
cp MAX_KEY_ITEMS
jr nc, .nope
ld c, a
ld b, 0
add hl, bc
2018-01-23 22:39:09 +00:00
ld a, [wCurItem]
2016-03-27 16:47:28 +00:00
ld [hli], a
ld [hl], -1
2018-01-23 22:39:09 +00:00
ld hl, wNumKeyItems
2016-03-27 16:47:28 +00:00
inc [hl]
scf
ret
.nope
and a
ret
2018-06-24 14:09:41 +00:00
TossKeyItem:
2018-01-23 22:39:09 +00:00
ld a, [wCurItemQuantity]
2016-03-27 16:47:28 +00:00
ld e, a
ld d, 0
2018-01-23 22:39:09 +00:00
ld hl, wNumKeyItems
2016-03-27 16:47:28 +00:00
ld a, [hl]
cp e
jr nc, .ok
call .Toss
ret nc
jr .ok2
.ok
dec [hl]
inc hl
add hl, de
.ok2
ld d, h
ld e, l
inc hl
.loop
ld a, [hli]
ld [de], a
inc de
cp -1
jr nz, .loop
scf
ret
2018-06-24 14:09:41 +00:00
.Toss:
2018-01-23 22:39:09 +00:00
ld hl, wNumKeyItems
ld a, [wCurItem]
2016-03-27 16:47:28 +00:00
ld c, a
.loop3
inc hl
ld a, [hl]
cp c
jr z, .ok3
cp -1
jr nz, .loop3
xor a
ret
.ok3
2018-01-23 22:39:09 +00:00
ld a, [wNumKeyItems]
2016-03-27 16:47:28 +00:00
dec a
2018-01-23 22:39:09 +00:00
ld [wNumKeyItems], a
2016-03-27 16:47:28 +00:00
scf
ret
2018-06-24 14:09:41 +00:00
CheckKeyItems:
2018-01-23 22:39:09 +00:00
ld a, [wCurItem]
2016-03-27 16:47:28 +00:00
ld c, a
2018-01-23 22:39:09 +00:00
ld hl, wKeyItems
2016-03-27 16:47:28 +00:00
.loop
ld a, [hli]
cp c
jr z, .done
cp -1
jr nz, .loop
and a
ret
.done
scf
ret
2018-06-24 14:09:41 +00:00
ReceiveTMHM:
2016-03-27 16:47:28 +00:00
dec c
ld b, 0
2018-01-23 22:39:09 +00:00
ld hl, wTMsHMs
2016-03-27 16:47:28 +00:00
add hl, bc
ld a, [wItemQuantityChangeBuffer]
add [hl]
cp 100
jr nc, .toomany
ld [hl], a
scf
ret
.toomany
and a
ret
2018-06-24 14:09:41 +00:00
TossTMHM:
2016-03-27 16:47:28 +00:00
dec c
ld b, 0
2018-01-23 22:39:09 +00:00
ld hl, wTMsHMs
2016-03-27 16:47:28 +00:00
add hl, bc
ld a, [wItemQuantityChangeBuffer]
ld b, a
ld a, [hl]
sub b
jr c, .nope
ld [hl], a
ld [wItemQuantityBuffer], a
jr nz, .yup
ld a, [wTMHMPocketScrollPosition]
and a
jr z, .yup
dec a
ld [wTMHMPocketScrollPosition], a
.yup
scf
ret
.nope
and a
ret
2018-06-24 14:09:41 +00:00
CheckTMHM:
2016-03-27 16:47:28 +00:00
dec c
ld b, $0
2018-01-23 22:39:09 +00:00
ld hl, wTMsHMs
2016-03-27 16:47:28 +00:00
add hl, bc
ld a, [hl]
and a
ret z
scf
ret
2018-06-24 14:09:41 +00:00
GetTMHMNumber::
2016-03-27 16:47:28 +00:00
; Return the number of a TM/HM by item id c.
ld a, c
; Skip any dummy items.
cp ITEM_C3 ; TM04-05
jr c, .done
cp ITEM_DC ; TM28-29
jr c, .skip
dec a
.skip
dec a
.done
sub TM01
inc a
ld c, a
ret
2018-06-24 14:09:41 +00:00
GetNumberedTMHM:
2016-03-27 16:47:28 +00:00
; Return the item id of a TM/HM by number c.
ld a, c
; Skip any gaps.
cp ITEM_C3 - (TM01 - 1)
jr c, .done
cp ITEM_DC - (TM01 - 1) - 1
jr c, .skip_one
.skip_two
inc a
.skip_one
inc a
.done
add TM01
dec a
ld c, a
ret
2018-06-24 14:09:41 +00:00
_CheckTossableItem::
2018-01-23 22:39:09 +00:00
; Return 1 in wItemAttributeParamBuffer and carry if wCurItem can't be removed from the bag.
2016-03-27 16:47:28 +00:00
ld a, ITEMATTR_PERMISSIONS
call GetItemAttr
2018-01-26 03:19:24 +00:00
bit CANT_TOSS_F, a
2016-03-27 16:47:28 +00:00
jr nz, ItemAttr_ReturnCarry
and a
ret
2018-06-24 14:09:41 +00:00
CheckSelectableItem:
2018-01-23 22:39:09 +00:00
; Return 1 in wItemAttributeParamBuffer and carry if wCurItem can't be selected.
2016-03-27 16:47:28 +00:00
ld a, ITEMATTR_PERMISSIONS
call GetItemAttr
2018-01-26 03:19:24 +00:00
bit CANT_SELECT_F, a
2016-03-27 16:47:28 +00:00
jr nz, ItemAttr_ReturnCarry
and a
ret
2018-06-24 14:09:41 +00:00
CheckItemPocket::
2018-01-23 22:39:09 +00:00
; Return the pocket for wCurItem in wItemAttributeParamBuffer.
2016-03-27 16:47:28 +00:00
ld a, ITEMATTR_POCKET
call GetItemAttr
and $f
ld [wItemAttributeParamBuffer], a
ret
2018-06-24 14:09:41 +00:00
CheckItemContext:
2018-01-23 22:39:09 +00:00
; Return the context for wCurItem in wItemAttributeParamBuffer.
2016-03-27 16:47:28 +00:00
ld a, ITEMATTR_HELP
call GetItemAttr
and $f
ld [wItemAttributeParamBuffer], a
ret
2018-06-24 14:09:41 +00:00
CheckItemMenu:
2018-01-23 22:39:09 +00:00
; Return the menu for wCurItem in wItemAttributeParamBuffer.
2016-03-27 16:47:28 +00:00
ld a, ITEMATTR_HELP
call GetItemAttr
swap a
and $f
ld [wItemAttributeParamBuffer], a
ret
2018-06-24 14:09:41 +00:00
GetItemAttr:
2018-01-23 22:39:09 +00:00
; Get attribute a of wCurItem.
2016-03-27 16:47:28 +00:00
push hl
push bc
ld hl, ItemAttributes
ld c, a
ld b, 0
add hl, bc
xor a
ld [wItemAttributeParamBuffer], a
2018-01-23 22:39:09 +00:00
ld a, [wCurItem]
2016-03-27 16:47:28 +00:00
dec a
ld c, a
2017-12-13 01:15:07 +00:00
ld a, ITEMATTR_STRUCT_LENGTH
2016-03-27 16:47:28 +00:00
call AddNTimes
ld a, BANK(ItemAttributes)
call GetFarByte
pop bc
pop hl
ret
2018-06-24 14:09:41 +00:00
ItemAttr_ReturnCarry:
2016-03-27 16:47:28 +00:00
ld a, 1
ld [wItemAttributeParamBuffer], a
scf
ret
2018-06-24 14:09:41 +00:00
GetItemPrice:
2018-01-23 22:39:09 +00:00
; Return the price of wCurItem in de.
2016-03-27 16:47:28 +00:00
push hl
push bc
ld a, ITEMATTR_PRICE
call GetItemAttr
ld e, a
ld a, ITEMATTR_PRICE_HI
call GetItemAttr
ld d, a
pop bc
pop hl
ret