1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
SwitchItemsInBag:
ld a, [wSwitchItem]
and a
jr z, .init
ld b, a
ld a, [wScrollingMenuCursorPosition]
inc a
cp b
jr z, .trivial
ld a, [wScrollingMenuCursorPosition]
call ItemSwitch_GetNthItem
ld a, [hl]
cp -1
ret z
ld a, [wSwitchItem]
dec a
ld [wSwitchItem], a
call .try_combining_stacks
jp c, .combine_stacks
ld a, [wScrollingMenuCursorPosition]
ld c, a
ld a, [wSwitchItem]
cp c
jr c, .above
jr .below
.init:
ld a, [wScrollingMenuCursorPosition]
inc a
ld [wSwitchItem], a
ret
.trivial:
xor a
ld [wSwitchItem], a
ret
.below:
ld a, [wSwitchItem]
call ItemSwitch_CopyItemToBuffer
ld a, [wScrollingMenuCursorPosition]
ld d, a
ld a, [wSwitchItem]
ld e, a
call ItemSwitch_GetItemOffset
push bc
ld a, [wSwitchItem]
call ItemSwitch_GetNthItem
dec hl
push hl
call ItemSwitch_GetItemFormatSize
add hl, bc
ld d, h
ld e, l
pop hl
pop bc
call ItemSwitch_BackwardsCopyBytes
ld a, [wScrollingMenuCursorPosition]
call ItemSwitch_CopyBufferToItem
xor a
ld [wSwitchItem], a
ret
.above:
ld a, [wSwitchItem]
call ItemSwitch_CopyItemToBuffer
ld a, [wScrollingMenuCursorPosition]
ld d, a
ld a, [wSwitchItem]
ld e, a
call ItemSwitch_GetItemOffset
push bc
ld a, [wSwitchItem]
call ItemSwitch_GetNthItem
ld d, h
ld e, l
call ItemSwitch_GetItemFormatSize
add hl, bc
pop bc
call CopyBytes
ld a, [wScrollingMenuCursorPosition]
call ItemSwitch_CopyBufferToItem
xor a
ld [wSwitchItem], a
ret
.try_combining_stacks:
ld a, [wSwitchItem]
call ItemSwitch_GetNthItem
ld d, h
ld e, l
ld a, [wScrollingMenuCursorPosition]
call ItemSwitch_GetNthItem
ld a, [de]
cp [hl]
jr nz, .no_combine
ld a, [wScrollingMenuCursorPosition]
call ItemSwitch_GetItemQuantity
cp MAX_ITEM_STACK
jr z, .no_combine
ld a, [wSwitchItem]
call ItemSwitch_GetItemQuantity
cp MAX_ITEM_STACK
jr nz, .combine
.no_combine
and a
ret
.combine
scf
ret
.combine_stacks:
ld a, [wSwitchItem]
call ItemSwitch_GetNthItem
inc hl
push hl
ld a, [wScrollingMenuCursorPosition]
call ItemSwitch_GetNthItem
inc hl
ld a, [hl]
pop hl
add [hl]
cp MAX_ITEM_STACK + 1
jr c, .merge_stacks
sub MAX_ITEM_STACK
push af
ld a, [wScrollingMenuCursorPosition]
call ItemSwitch_GetNthItem
inc hl
ld [hl], MAX_ITEM_STACK
ld a, [wSwitchItem]
call ItemSwitch_GetNthItem
inc hl
pop af
ld [hl], a
xor a
ld [wSwitchItem], a
ret
.merge_stacks:
push af
ld a, [wScrollingMenuCursorPosition]
call ItemSwitch_GetNthItem
inc hl
pop af
ld [hl], a
ld hl, wMenuData_ItemsPointerAddr
ld a, [hli]
ld h, [hl]
ld l, a
ld a, [wSwitchItem]
cp [hl]
jr nz, .not_combining_last_item
dec [hl]
ld a, [wSwitchItem]
call ItemSwitch_GetNthItem
ld [hl], -1 ; end
xor a
ld [wSwitchItem], a
ret
.not_combining_last_item:
dec [hl]
call ItemSwitch_GetItemFormatSize
push bc
ld a, [wSwitchItem]
call ItemSwitch_GetNthItem
pop bc
push hl
add hl, bc
pop de
.copy_loop
ld a, [hli]
ld [de], a
inc de
cp -1 ; end?
jr nz, .copy_loop
xor a
ld [wSwitchItem], a
ret
ItemSwitch_CopyItemToBuffer:
call ItemSwitch_GetNthItem
ld de, wSwitchItemBuffer
call ItemSwitch_GetItemFormatSize
call CopyBytes
ret
ItemSwitch_CopyBufferToItem:
call ItemSwitch_GetNthItem
ld d, h
ld e, l
ld hl, wSwitchItemBuffer
call ItemSwitch_GetItemFormatSize
call CopyBytes
ret
ItemSwitch_GetNthItem:
push af
call ItemSwitch_GetItemFormatSize
ld hl, wMenuData_ItemsPointerAddr
ld a, [hli]
ld h, [hl]
ld l, a
inc hl
pop af
call AddNTimes
ret
ItemSwitch_GetItemOffset:
push hl
call ItemSwitch_GetItemFormatSize
ld a, d
sub e
jr nc, .dont_negate
dec a
cpl
.dont_negate
ld hl, 0
call AddNTimes
ld b, h
ld c, l
pop hl
ret
ItemSwitch_GetItemFormatSize:
push hl
ld a, [wMenuData_ScrollingMenuItemFormat]
ld c, a
ld b, 0
ld hl, .item_format_sizes
add hl, bc
add hl, bc
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
ItemSwitch_GetItemQuantity:
push af
call ItemSwitch_GetItemFormatSize
ld a, c
cp 2
jr nz, .no_quantity
pop af
call ItemSwitch_GetNthItem
inc hl
ld a, [hl]
ret
.no_quantity
pop af
ld a, 1
ret
ItemSwitch_BackwardsCopyBytes:
.loop
ld a, [hld]
ld [de], a
dec de
dec bc
ld a, b
or c
jr nz, .loop
ret
|