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, [wItemAttributeValue]
2016-03-27 16:47:28 +00:00
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, [wItemAttributeValue]
2016-03-27 16:47:28 +00:00
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, [wItemAttributeValue]
2016-03-27 16:47:28 +00:00
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
2019-03-18 15:43:58 +00:00
cp LOW(wNumPCItems)
2016-03-27 16:47:28 +00:00
jr nz, .not_pc
ld a, d
2019-03-18 15:43:58 +00:00
cp HIGH(wNumPCItems)
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, MAX_ITEM_STACK
2016-03-27 16:47:28 +00:00
sub [hl]
add b
ld b, a
ld a, [wItemQuantityChange]
2016-03-27 16:47:28 +00:00
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, [wItemQuantityChange]
ld [wItemQuantity], a
2016-03-27 16:47:28 +00:00
.loop2
inc hl
ld a, [hli]
cp -1
jr z, .terminator2
cp c
jr nz, .loop2
ld a, [wItemQuantity]
2016-03-27 16:47:28 +00:00
add [hl]
cp MAX_ITEM_STACK + 1
2016-03-27 16:47:28 +00:00
jr nc, .newstack
ld [hl], a
jr .done
.newstack
ld [hl], MAX_ITEM_STACK
sub MAX_ITEM_STACK
ld [wItemQuantity], a
2016-03-27 16:47:28 +00:00
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, [wItemQuantity]
2016-03-27 16:47:28 +00:00
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
2016-03-27 16:47:28 +00:00
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, [wItemQuantityChange]
2016-03-27 16:47:28 +00:00
ld b, a
ld a, [hl]
sub b
jr c, .nope
ld [hl], a
ld [wItemQuantity], a
2016-03-27 16:47:28 +00:00
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, [wItemQuantityChange]
2016-03-27 16:47:28 +00:00
add [hl]
cp MAX_ITEM_STACK + 1
2016-03-27 16:47:28 +00:00
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, [wItemQuantityChange]
2016-03-27 16:47:28 +00:00
ld b, a
ld a, [hl]
sub b
jr c, .nope
ld [hl], a
ld [wItemQuantity], a
2016-03-27 16:47:28 +00:00
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
2016-03-27 16:47:28 +00:00
inc a
.skip_one
inc a
.done
add TM01
dec a
ld c, a
ret
2018-06-24 14:09:41 +00:00
_CheckTossableItem::
; Return 1 in wItemAttributeValue 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:
; Return 1 in wItemAttributeValue 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::
; Return the pocket for wCurItem in wItemAttributeValue.
2016-03-27 16:47:28 +00:00
ld a, ITEMATTR_POCKET
call GetItemAttr
and $f
ld [wItemAttributeValue], a
2016-03-27 16:47:28 +00:00
ret
2018-06-24 14:09:41 +00:00
CheckItemContext:
; Return the context for wCurItem in wItemAttributeValue.
2016-03-27 16:47:28 +00:00
ld a, ITEMATTR_HELP
call GetItemAttr
and $f
ld [wItemAttributeValue], a
2016-03-27 16:47:28 +00:00
ret
2018-06-24 14:09:41 +00:00
CheckItemMenu:
; Return the menu for wCurItem in wItemAttributeValue.
2016-03-27 16:47:28 +00:00
ld a, ITEMATTR_HELP
call GetItemAttr
swap a
and $f
ld [wItemAttributeValue], a
2016-03-27 16:47:28 +00:00
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 [wItemAttributeValue], a
2016-03-27 16:47:28 +00:00
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 [wItemAttributeValue], a
2016-03-27 16:47:28 +00:00
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_LO
2016-03-27 16:47:28 +00:00
call GetItemAttr
ld e, a
ld a, ITEMATTR_PRICE_HI
call GetItemAttr
ld d, a
pop bc
pop hl
ret