Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pret
GitHub Repository: pret/pokered
Path: blob/master/engine/items/inventory.asm
1271 views
1
; function to add an item (in varying quantities) to the player's bag or PC box
2
; INPUT:
3
; hl = address of inventory (either wNumBagItems or wNumBoxItems)
4
; [wCurItem] = item ID
5
; [wItemQuantity] = item quantity
6
; sets carry flag if successful, unsets carry flag if unsuccessful
7
AddItemToInventory_::
8
ld a, [wItemQuantity] ; a = item quantity
9
push af
10
push bc
11
push de
12
push hl
13
push hl
14
ld d, PC_ITEM_CAPACITY ; how many items the PC can hold
15
ld a, LOW(wNumBagItems)
16
cp l
17
jr nz, .checkIfInventoryFull
18
ld a, HIGH(wNumBagItems)
19
cp h
20
jr nz, .checkIfInventoryFull
21
; if the destination is the bag
22
ld d, BAG_ITEM_CAPACITY ; how many items the bag can hold
23
.checkIfInventoryFull
24
ld a, [hl]
25
sub d
26
ld d, a
27
ld a, [hli]
28
and a
29
jr z, .addNewItem
30
.notAtEndOfInventory
31
ld a, [hli]
32
ld b, a ; b = ID of current item in table
33
ld a, [wCurItem] ; a = ID of item being added
34
cp b ; does the current item in the table match the item being added?
35
jp z, .increaseItemQuantity ; if so, increase the item's quantity
36
inc hl
37
ld a, [hl]
38
cp $ff ; is it the end of the table?
39
jr nz, .notAtEndOfInventory
40
.addNewItem ; add an item not yet in the inventory
41
pop hl
42
ld a, d
43
and a ; is there room for a new item slot?
44
jr z, .done
45
; if there is room
46
inc [hl] ; increment the number of items in the inventory
47
ld a, [hl] ; the number of items will be the index of the new item
48
add a
49
dec a
50
ld c, a
51
ld b, 0
52
add hl, bc ; hl = address to store the item
53
ld a, [wCurItem]
54
ld [hli], a ; store item ID
55
ld a, [wItemQuantity]
56
ld [hli], a ; store item quantity
57
ld [hl], $ff ; store terminator
58
jp .success
59
.increaseItemQuantity ; increase the quantity of an item already in the inventory
60
ld a, [wItemQuantity]
61
ld b, a ; b = quantity to add
62
ld a, [hl] ; a = existing item quantity
63
add b ; a = new item quantity
64
cp 100
65
jp c, .storeNewQuantity ; if the new quantity is less than 100, store it
66
; if the new quantity is greater than or equal to 100,
67
; try to max out the current slot and add the rest in a new slot
68
sub 99
69
ld [wItemQuantity], a ; a = amount left over (to put in the new slot)
70
ld a, d
71
and a ; is there room for a new item slot?
72
jr z, .increaseItemQuantityFailed
73
; if so, store 99 in the current slot and store the rest in a new slot
74
ld a, 99
75
ld [hli], a
76
jp .notAtEndOfInventory
77
.increaseItemQuantityFailed
78
pop hl
79
and a
80
jr .done
81
.storeNewQuantity
82
ld [hl], a
83
pop hl
84
.success
85
scf
86
.done
87
pop hl
88
pop de
89
pop bc
90
pop bc
91
ld a, b
92
ld [wItemQuantity], a ; restore the initial value from when the function was called
93
ret
94
95
; function to remove an item (in varying quantities) from the player's bag or PC box
96
; INPUT:
97
; hl = address of inventory (either wNumBagItems or wNumBoxItems)
98
; [wWhichPokemon] = index (within the inventory) of the item to remove
99
; [wItemQuantity] = quantity to remove
100
RemoveItemFromInventory_::
101
push hl
102
inc hl
103
ld a, [wWhichPokemon] ; index (within the inventory) of the item being removed
104
sla a
105
add l
106
ld l, a
107
jr nc, .noCarry
108
inc h
109
.noCarry
110
inc hl
111
ld a, [wItemQuantity] ; quantity being removed
112
ld e, a
113
ld a, [hl] ; a = current quantity
114
sub e
115
ld [hld], a ; store new quantity
116
ld [wMaxItemQuantity], a
117
and a
118
jr nz, .skipMovingUpSlots
119
; if the remaining quantity is 0,
120
; remove the emptied item slot and move up all the following item slots
121
.moveSlotsUp
122
ld e, l
123
ld d, h
124
inc de
125
inc de ; de = address of the slot following the emptied one
126
.loop ; loop to move up the following slots
127
ld a, [de]
128
inc de
129
ld [hli], a
130
cp $ff
131
jr nz, .loop
132
; update menu info
133
xor a
134
ld [wListScrollOffset], a
135
ld [wCurrentMenuItem], a
136
ld [wBagSavedMenuItem], a
137
ld [wSavedListScrollOffset], a
138
pop hl
139
ld a, [hl] ; a = number of items in inventory
140
dec a ; decrement the number of items
141
ld [hl], a ; store new number of items
142
ld [wListCount], a
143
cp 2
144
jr c, .done
145
ld [wMaxMenuItem], a
146
jr .done
147
.skipMovingUpSlots
148
pop hl
149
.done
150
ret
151
152