-
Notifications
You must be signed in to change notification settings - Fork 5
/
home.asm
4506 lines (4184 loc) · 87.9 KB
/
home.asm
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
; The rst vectors are unused.
SECTION "rst 00", ROM0 [$00]
rst $38
SECTION "rst 08", ROM0 [$08]
rst $38
SECTION "rst 10", ROM0 [$10]
rst $38
SECTION "rst 18", ROM0 [$18]
rst $38
SECTION "rst 20", ROM0 [$20]
rst $38
SECTION "rst 28", ROM0 [$28]
rst $38
SECTION "rst 30", ROM0 [$30]
rst $38
SECTION "rst 38", ROM0 [$38]
jp $F080
; Hardware interrupts
SECTION "vblank", ROM0 [$40]
jp VBlank
SECTION "hblank", ROM0 [$48]
rst $38
SECTION "timer", ROM0 [$50]
jp Timer
SECTION "serial", ROM0 [$58]
jp Serial
SECTION "joypad", ROM0 [$60]
reti
SECTION "junk", ROM0 [$68]
INCBIN "baserom.gbc", $68, $98
SECTION "Entry", ROM0 [$100]
nop
jp Start
SECTION "Header", ROM0 [$104]
; The header is generated by rgbfix.
; The space here is allocated to prevent code from being overwritten.
ds $150 - $104
SECTION "Main", ROM0
Start::
jp Init
Joypad::
ld a, [H_LOADEDROMBANK]
push af
ld a, Bank(ReadJoypad)
ld [H_LOADEDROMBANK], a
ld [MBC1RomBank], a
call ReadJoypad
pop af
ld [H_LOADEDROMBANK], a
ld [MBC1RomBank], a
ret
DisableLCD::
xor a
ld [rIF], a
ld a, [rIE]
ld b, a
res 0, a
ld [rIE], a
.wait
ld a, [rLY]
cp LY_VBLANK
jr nz, .wait
ld a, [rLCDC]
and $ff ^ rLCDC_ENABLE_MASK
ld [rLCDC], a
ld a, b
ld [rIE], a
ret
EnableLCD::
ld a, [rLCDC]
set rLCDC_ENABLE, a
ld [rLCDC], a
ret
ClearSprites::
xor a
ld hl, wOAMBuffer
ld b, 40 * 4
.loop
ld [hli], a
dec b
jr nz, .loop
ret
HideSprites::
ld a, 160
ld hl, wOAMBuffer
ld de, 4
ld b, 40
.loop
ld [hl], a
add hl, de
dec b
jr nz, .loop
ret
INCLUDE "home/copy.asm"
INCLUDE "data/collision.asm"
INCLUDE "home/copy2.asm"
INCLUDE "home/text.asm"
INCLUDE "home/vcopy.asm"
INCLUDE "home/init.asm"
INCLUDE "home/vblank.asm"
INCLUDE "home/fade.asm"
INCLUDE "home/serial.asm"
INCLUDE "home/timer.asm"
INCLUDE "home/audio.asm"
UpdateSprites::
ld a, [wUpdateSpritesEnabled]
dec a
ret nz
ld a, [H_LOADEDROMBANK]
push af
ld a, Bank(_UpdateSprites)
ld [H_LOADEDROMBANK], a
ld [MBC1RomBank], a
call _UpdateSprites
pop af
ld [H_LOADEDROMBANK], a
ld [MBC1RomBank], a
ret
INCLUDE "data/mart_inventories.asm"
TextScriptEndingChar::
db "@"
TextScriptEnd::
ld hl,TextScriptEndingChar
ret
ExclamationText::
text "!"
done
GroundRoseText::
text "どこかで じめんがもりあがった!"
done
BoulderText::
text "「かいりき」 で うごかせるかも<……>"
done
MartSignText::
text "<PKMN> グッズが いっぱい!"
line "フレンドリィショップ"
done
PokeCenterSignText::
text "<PKMN>の たいりょく かいふく!"
line "<PKMN>センター"
done
PickUpItemText::
TX_ASM
predef PickUpItem
jp TextScriptEnd
INCLUDE "home/pic.asm"
ResetPlayerSpriteData::
ld hl, wSpriteStateData1
call ResetPlayerSpriteData_ClearSpriteData
ld hl, wSpriteStateData2
call ResetPlayerSpriteData_ClearSpriteData
ld a, $1
ld [wSpriteStateData1], a
ld [wSpriteStateData2 + $0e], a
ld hl, wSpriteStateData1 + 4
ld [hl], $3c ; set Y screen pos
inc hl
inc hl
ld [hl], $40 ; set X screen pos
ret
; overwrites sprite data with zeroes
ResetPlayerSpriteData_ClearSpriteData::
ld bc, $10
xor a
jp FillMemory
FadeOutAudio::
ld a, [wAudioFadeOutControl]
and a ; currently fading out audio?
jr nz, .fadingOut
ld a, [wd72c]
bit 1, a
ret nz
ld a, $77
ld [rNR50], a
ret
.fadingOut
ld a, [wAudioFadeOutCounter]
and a
jr z, .counterReachedZero
dec a
ld [wAudioFadeOutCounter], a
ret
.counterReachedZero
ld a, [wAudioFadeOutCounterReloadValue]
ld [wAudioFadeOutCounter], a
ld a, [rNR50]
and a ; has the volume reached 0?
jr z, .fadeOutComplete
ld b, a
and $f
dec a
ld c, a
ld a, b
and $f0
swap a
dec a
swap a
or c
ld [rNR50], a
ret
.fadeOutComplete
ld a, [wAudioFadeOutControl]
ld b, a
xor a
ld [wAudioFadeOutControl], a
ld a, $ff
ld [wNewSoundID], a
call PlaySound
ld a, [wAudioSavedROMBank]
ld [wAudioROMBank], a
ld a, b
ld [wNewSoundID], a
jp PlaySound
; this function is used to display sign messages, sprite dialog, etc.
; INPUT: [hSpriteIndexOrTextID] = sprite ID or text ID
DisplayTextID::
ld a,[H_LOADEDROMBANK]
push af
callba DisplayTextIDInit ; initialization
ld hl,wTextPredefFlag
bit 0,[hl]
res 0,[hl]
jr nz,.skipSwitchToMapBank
ld a,[wCurMap]
call SwitchToMapRomBank
.skipSwitchToMapBank
ld a,30 ; half a second
sta H_FRAMECOUNTER ; used as joypad poll timer
ld hl,wMapTextPtr
ld a,[hli]
ld h,[hl]
ld l,a ; hl = map text pointer
ld d,$00
ld a,[hSpriteIndexOrTextID] ; text ID
ld [wSpriteIndex],a
and a
jp z,DisplayStartMenu
cp TEXT_SAFARI_GAME_OVER
jp z,DisplaySafariGameOverText
cp TEXT_MON_FAINTED
jp z,DisplayPokemonFaintedText
cp TEXT_BLACKED_OUT
jp z,DisplayPlayerBlackedOutText
cp TEXT_REPEL_WORE_OFF
jp z,DisplayRepelWoreOffText
ld a,[wNumSprites]
ld e,a
ld a,[hSpriteIndexOrTextID] ; sprite ID
cp e
jr z,.spriteHandling
jr nc,.skipSpriteHandling
.spriteHandling
; get the text ID of the sprite
push hl
push de
push bc
callba UpdateSpriteFacingOffsetAndDelayMovement ; update the graphics of the sprite the player is talking to (to face the right direction)
pop bc
pop de
ld hl,wMapSpriteData ; NPC text entries
ld a,[hSpriteIndexOrTextID]
dec a
add a
add l
ld l,a
jr nc,.noCarry
inc h
.noCarry
inc hl
ld a,[hl] ; a = text ID of the sprite
pop hl
.skipSpriteHandling
; look up the address of the text in the map's text entries
dec a
ld e,a
sla e
add hl,de
ld a,[hli]
ld h,[hl]
ld l,a ; hl = address of the text
ld a,[hl] ; a = first byte of text
; check first byte of text for special cases
cp $fe ; Pokemart NPC
jp z,DisplayPokemartDialogue
cp $ff ; Pokemon Center NPC
jp z,DisplayPokemonCenterDialogue
cp $fc ; Item Storage PC
jp z,FuncTX_ItemStoragePC
cp $fd ; Bill's PC
jp z,FuncTX_BillsPC
cp $f9 ; Pokemon Center PC
jp z,FuncTX_PokemonCenterPC
cp $f5 ; Vending Machine
jr nz,.notVendingMachine
callba VendingMachineMenu ; jump banks to vending machine routine
jr AfterDisplayingTextID
.notVendingMachine
cp $f7 ; prize menu
jp z, FuncTX_GameCornerPrizeMenu
cp $f6 ; cable connection NPC in Pokemon Center
jr nz,.notSpecialCase
callab CableClubNPC
jr AfterDisplayingTextID
.notSpecialCase
call PrintText_NoCreatingTextBox ; display the text
ld a,[wDoNotWaitForButtonPressAfterDisplayingText]
and a
jr nz,HoldTextDisplayOpen
AfterDisplayingTextID:: ; 14A8
ld a,[wEnteringCableClub]
and a
jr nz,HoldTextDisplayOpen
call WaitForTextScrollButtonPress ; wait for a button press after displaying all the text
HoldTextDisplayOpen:: ; 14B1
call Joypad
ld a,[hJoyHeld]
bit 0,a ; is the A button being pressed?
jr nz,HoldTextDisplayOpen
CloseTextDisplay:: ; 14BA
ld a,[wCurMap]
call SwitchToMapRomBank
ld a,$90
ld [hWY],a ; move the window off the screen
call DelayFrame
call LoadGBPal
xor a
ld [H_AUTOBGTRANSFERENABLED],a ; disable continuous WRAM to VRAM transfer each V-blank
; loop to make sprites face the directions they originally faced before the dialogue
ld hl,wSpriteStateData2 + $19
ld c,$0f
ld de,$0010
.restoreSpriteFacingDirectionLoop
ld a,[hl]
dec h
ld [hl],a
inc h
add hl,de
dec c
jr nz,.restoreSpriteFacingDirectionLoop
ld a,BANK(InitMapSprites)
ld [H_LOADEDROMBANK],a
ld [MBC1RomBank],a
call InitMapSprites ; reload sprite tile pattern data (since it was partially overwritten by text tile patterns)
ld hl,wFontLoaded
res 0,[hl]
ld a,[wd732]
bit 3,a ; used fly warp
call z,LoadPlayerSpriteGraphics
call LoadCurrentMapView
pop af
ld [H_LOADEDROMBANK],a
ld [MBC1RomBank],a
jp UpdateSprites
DisplayPokemartDialogue::
push hl
ld hl,PokemartGreetingText
call PrintText
pop hl
inc hl
call LoadItemList
ld a,PRICEDITEMLISTMENU
ld [wListMenuID],a
ld a,[H_LOADEDROMBANK]
push af
ld a,Bank(DisplayPokemartDialogue_)
ld [H_LOADEDROMBANK],a
ld [MBC1RomBank],a
call DisplayPokemartDialogue_
pop af
ld [H_LOADEDROMBANK],a
ld [MBC1RomBank],a
jp AfterDisplayingTextID
PokemartGreetingText::
text "ようこそ!"
next "おさがしものですか?"
done
LoadItemList::
ld a,1
ld [wUpdateSpritesEnabled],a
ld a,h
ld [wItemListPointer],a
ld a,l
ld [wItemListPointer + 1],a
ld de,wItemList
.loop
ld a,[hli]
ld [de],a
inc de
cp $ff
jr nz,.loop
ret
DisplayPokemonCenterDialogue::
; zeroing these doesn't appear to serve any purpose
xor a
ld [$ff8b],a
ld [$ff8c],a
ld [$ff8d],a
inc hl
ld a,[H_LOADEDROMBANK]
push af
ld a,Bank(DisplayPokemonCenterDialogue_)
ld [H_LOADEDROMBANK],a
ld [MBC1RomBank],a
call DisplayPokemonCenterDialogue_
pop af
ld [H_LOADEDROMBANK],a
ld [MBC1RomBank],a
jp AfterDisplayingTextID
DisplaySafariGameOverText::
callab PrintSafariGameOverText
jp AfterDisplayingTextID
DisplayPokemonFaintedText::
ld hl,PokemonFaintedText
call PrintText
jp AfterDisplayingTextID
PokemonFaintedText::
TX_RAM wcd6d
text "は ちからつきた"
done
DisplayPlayerBlackedOutText::
ld hl,PlayerBlackedOutText
call PrintText
jp HoldTextDisplayOpen
PlayerBlackedOutText::
text "<PLAYER>の てもとには"
line "たたかえる<PKMN>が もういない!"
para "<PLAYER>は"
line "めのまえが まっくらに なった!"
prompt
DisplayRepelWoreOffText::
ld hl,RepelWoreOffText
call PrintText
jp AfterDisplayingTextID
RepelWoreOffText::
text "スプレーの こうかがきれた"
done
INCLUDE "engine/menu/start_menu.asm"
; function to count how many bits are set in a string of bytes
; INPUT:
; hl = address of string of bytes
; b = length of string of bytes
; OUTPUT:
; [wNumSetBits] = number of set bits
CountSetBits::
ld c,0
.loop
ld a,[hli]
ld e,a
ld d,8
.innerLoop ; count how many bits are set in the current byte
srl e
ld a,0
adc c
ld c,a
dec d
jr nz,.innerLoop
dec b
jr nz,.loop
ld a,c
ld [wNumSetBits],a
ret
; subtracts the amount the player paid from their money
; sets carry flag if there is enough money and unsets carry flag if not
SubtractAmountPaidFromMoney::
jpba SubtractAmountPaidFromMoney_
; adds the amount the player sold to their money
AddAmountSoldToMoney::
ld de,wPlayerMoney + 2
ld hl,$ffa1 ; total price of items
ld c,3 ; length of money in bytes
predef AddBCDPredef ; add total price to money
ld a,MONEY_BOX
ld [wTextBoxID],a
call DisplayTextBoxID ; redraw money text box
ld a, SFX_PURCHASE
call PlaySoundWaitForCurrent
jp WaitForSoundToFinish
; function to remove an item (in varying quantities) from the player's bag or PC box
; INPUT:
; HL = address of inventory (either wNumBagItems or wNumBoxItems)
; [wWhichPokemon] = index (within the inventory) of the item to remove
; [wItemQuantity] = quantity to remove
RemoveItemFromInventory::
ld a,[H_LOADEDROMBANK]
push af
ld a,BANK(RemoveItemFromInventory_)
ld [H_LOADEDROMBANK],a
ld [MBC1RomBank],a
call RemoveItemFromInventory_
pop af
ld [H_LOADEDROMBANK],a
ld [MBC1RomBank],a
ret
; function to add an item (in varying quantities) to the player's bag or PC box
; INPUT:
; HL = address of inventory (either wNumBagItems or wNumBoxItems)
; [wcf91] = item ID
; [wItemQuantity] = item quantity
; sets carry flag if successful, unsets carry flag if unsuccessful
AddItemToInventory::
push bc
ld a,[H_LOADEDROMBANK]
push af
ld a,BANK(AddItemToInventory_)
ld [H_LOADEDROMBANK],a
ld [MBC1RomBank],a
call AddItemToInventory_
pop bc
ld a,b
ld [H_LOADEDROMBANK],a
ld [MBC1RomBank],a
pop bc
ret
; INPUT:
; [wListMenuID] = list menu ID
; [wListPointer] = address of the list (2 bytes)
DisplayListMenuID::
xor a
ld [H_AUTOBGTRANSFERENABLED], a ; disable auto-transfer
ld a,1
sta hJoy7 ; joypad state update flag
ld a,[wBattleType]
and a ; is it the Old Man battle?
jr nz,.specialBattleType
ld a,$01 ; hardcoded bank
jr .bankswitch
.specialBattleType ; Old Man battle
ld a, BANK(DisplayBattleMenu)
.bankswitch
call BankswitchHome
ld hl,wd730
set 6,[hl] ; turn off letter printing delay
xor a
ld [wMenuItemToSwap],a ; 0 means no item is currently being swapped
ld [wListCount],a
ld a,[wListPointer]
ld l,a
ld a,[wListPointer + 1]
ld h,a ; hl = address of the list
ld a,[hl] ; the first byte is the number of entries in the list
ld [wListCount],a
ld a,LIST_MENU_BOX
ld [wTextBoxID],a
call DisplayTextBoxID ; draw the menu text box
call UpdateSprites ; disable sprites behind the text box
; the code up to .skipMovingSprites appears to be useless
coord hl, 4, 2 ; coordinates of upper left corner of menu text box
lb de, 9, 14 ; height and width of menu text box
ld a,[wListMenuID]
and a ; is it a PC pokemon list?
jr nz,.skipMovingSprites
call UpdateSprites
.skipMovingSprites
ld a,1 ; max menu item ID is 1 if the list has less than 2 entries
ld [wMenuWatchMovingOutOfBounds],a
ld a,[wListCount]
cp 2 ; does the list have less than 2 entries?
jr c,.setMenuVariables
ld a,2 ; max menu item ID is 2 if the list has at least 2 entries
.setMenuVariables
ld [wMaxMenuItem],a
ld a,4
ld [wTopMenuItemY],a
ld a,5
ld [wTopMenuItemX],a
ld a,A_BUTTON | B_BUTTON | SELECT
ld [wMenuWatchedKeys],a
ld c,10
call DelayFrames
DisplayListMenuIDLoop::
xor a
ld [H_AUTOBGTRANSFERENABLED],a ; disable transfer
call PrintListMenuEntries
ld a,1
ld [H_AUTOBGTRANSFERENABLED],a ; enable transfer
call Delay3
ld a,[wBattleType]
and a ; is it the Old Man battle?
jr z,.notOldManBattle
.oldManBattle
ld a,"▶"
Coorda 5, 4 ; place menu cursor in front of first menu entry
ld c,80
call DelayFrames
xor a
ld [wCurrentMenuItem],a
coord hl, 5, 4
ld a,l
ld [wMenuCursorLocation],a
ld a,h
ld [wMenuCursorLocation + 1],a
jr .buttonAPressed
.notOldManBattle
call LoadGBPal
call HandleMenuInput
push af
call PlaceMenuCursor
pop af
bit 0,a ; was the A button pressed?
jp z,.checkOtherKeys
.buttonAPressed
ld a,[wCurrentMenuItem]
call PlaceUnfilledArrowMenuCursor
; pointless because both values are overwritten before they are read
ld a,$01
ld [wMenuExitMethod],a
ld [wChosenMenuItem],a
xor a
ld [wMenuWatchMovingOutOfBounds],a
ld a,[wCurrentMenuItem]
ld c,a
ld a,[wListScrollOffset]
add c
ld c,a
ld a,[wListCount]
and a ; is the list empty?
jp z,ExitListMenu ; if so, exit the menu
dec a
cp c ; did the player select Cancel?
jp c,ExitListMenu ; if so, exit the menu
ld a,c
ld [wWhichPokemon],a
ld a,[wListMenuID]
cp ITEMLISTMENU
jr nz,.skipMultiplying
; if it's an item menu
sla c ; item entries are 2 bytes long, so multiply by 2
.skipMultiplying
ld a,[wListPointer]
ld l,a
ld a,[wListPointer + 1]
ld h,a
inc hl ; hl = beginning of list entries
ld b,0
add hl,bc
ld a,[hl]
ld [wcf91],a
ld a,[wListMenuID]
and a ; is it a PC pokemon list?
jr z,.pokemonList
push hl
call GetItemPrice
pop hl
ld a,[wListMenuID]
cp ITEMLISTMENU
jr nz,.skipGettingQuantity
; if it's an item menu
inc hl
ld a,[hl] ; a = item quantity
ld [wMaxItemQuantity],a
.skipGettingQuantity
ld a,[wcf91]
ld [wd0b5],a
ld a,BANK(ItemNames)
ld [wPredefBank],a
call GetName
jr .storeChosenEntry
.pokemonList
ld hl,wPartyCount
ld a,[wListPointer]
cp l ; is it a list of party pokemon or box pokemon?
ld hl,wPartyMonNicks
jr z,.getPokemonName
ld hl, wBoxMonNicks ; box pokemon names
.getPokemonName
ld a,[wWhichPokemon]
call GetPartyMonName
.storeChosenEntry ; store the menu entry that the player chose and return
ld de,wcd6d
call CopyStringToCF4B ; copy name to wcf4b
ld a,CHOSE_MENU_ITEM
ld [wMenuExitMethod],a
ld a,[wCurrentMenuItem]
ld [wChosenMenuItem],a
xor a
sta hJoy7 ; joypad state update flag
ld hl,wd730
res 6,[hl] ; turn on letter printing delay
jp BankswitchBack
.checkOtherKeys ; check B, SELECT, Up, and Down keys
bit 1,a ; was the B button pressed?
jp nz,ExitListMenu ; if so, exit the menu
bit 2,a ; was the select button pressed?
jp nz,HandleItemListSwapping ; if so, allow the player to swap menu entries
ld b,a
bit 7,b ; was Down pressed?
ld hl,wListScrollOffset
jr z,.upPressed
.downPressed
ld a,[hl]
add 3
ld b,a
ld a,[wListCount]
cp b ; will going down scroll past the Cancel button?
jp c,DisplayListMenuIDLoop
inc [hl] ; if not, go down
jp DisplayListMenuIDLoop
.upPressed
ld a,[hl]
and a
jp z,DisplayListMenuIDLoop
dec [hl]
jp DisplayListMenuIDLoop
DisplayChooseQuantityMenu::
; text box dimensions/coordinates for just quantity
coord hl, 15, 9
ld b,1 ; height
ld c,3 ; width
ld a,[wListMenuID]
cp PRICEDITEMLISTMENU
jr nz,.drawTextBox
; text box dimensions/coordinates for quantity and price
coord hl, 7, 9
ld b,1 ; height
ld c,11 ; width
.drawTextBox
call TextBoxBorder
coord hl, 16, 10
ld a,[wListMenuID]
cp PRICEDITEMLISTMENU
jr nz,.printInitialQuantity
ld a, "円"
Coorda 18, 10
coord hl, 8, 10
.printInitialQuantity
ld de,InitialQuantityText
call PlaceString
xor a
ld [wItemQuantity],a ; initialize current quantity to 0
jp .incrementQuantity
.waitForKeyPressLoop
call JoypadLowSensitivity
ld a,[hJoyPressed] ; newly pressed buttons
bit 0,a ; was the A button pressed?
jp nz,.buttonAPressed
bit 1,a ; was the B button pressed?
jp nz,.buttonBPressed
bit 6,a ; was Up pressed?
jr nz,.incrementQuantity
bit 7,a ; was Down pressed?
jr nz,.decrementQuantity
jr .waitForKeyPressLoop
.incrementQuantity
ld a,[wMaxItemQuantity]
inc a
ld b,a
ld hl,wItemQuantity ; current quantity
inc [hl]
ld a,[hl]
cp b
jr nz,.handleNewQuantity
; wrap to 1 if the player goes above the max quantity
ld a,1
ld [hl],a
jr .handleNewQuantity
.decrementQuantity
ld hl,wItemQuantity ; current quantity
dec [hl]
jr nz,.handleNewQuantity
; wrap to the max quantity if the player goes below 1
ld a,[wMaxItemQuantity]
ld [hl],a
.handleNewQuantity
coord hl, 17, 10
ld a,[wListMenuID]
cp PRICEDITEMLISTMENU
jr nz,.printQuantity
.printPrice
ld c,$03
ld a,[wItemQuantity]
ld b,a
ld hl,hMoney ; total price
; initialize total price to 0
xor a
ld [hli],a
ld [hli],a
ld [hl],a
.addLoop ; loop to multiply the individual price by the quantity to get the total price
ld de,hMoney + 2
ld hl,hItemPrice + 2
push bc
predef AddBCDPredef ; add the individual price to the current sum
pop bc
dec b
jr nz,.addLoop
ld a,[hHalveItemPrices]
and a ; should the price be halved (for selling items)?
jr z,.skipHalvingPrice
xor a
ld [hDivideBCDDivisor],a
ld [hDivideBCDDivisor + 1],a
ld a,$02
ld [hDivideBCDDivisor + 2],a
predef DivideBCDPredef3 ; halves the price
; store the halved price
ld a,[hDivideBCDQuotient]
ld [hMoney],a
ld a,[hDivideBCDQuotient + 1]
ld [hMoney + 1],a
ld a,[hDivideBCDQuotient + 2]
ld [hMoney + 2],a
.skipHalvingPrice
coord hl, 12, 10
ld de,SpacesBetweenQuantityAndPriceText
call PlaceString
ld de,hMoney ; total price
ld c,$83
call PrintBCDNumber
coord hl, 9, 10
.printQuantity
ld de,wItemQuantity ; current quantity
lb bc, LEADING_ZEROES | 1, 2 ; 1 byte, 2 digits
call PrintNumber
jp .waitForKeyPressLoop
.buttonAPressed ; the player chose to make the transaction
xor a
; BUG: select bug
;ld [wMenuItemToSwap],a ; 0 means no item is currently being swapped
ret
.buttonBPressed ; the player chose to cancel the transaction
; BUG: select bug
;xor a
;ld [wMenuItemToSwap],a ; 0 means no item is currently being swapped
ld a,$ff
ret
InitialQuantityText::
db "×01@"
SpacesBetweenQuantityAndPriceText::
db " @"
ExitListMenu::
ld a,[wCurrentMenuItem]
ld [wChosenMenuItem],a
ld a,CANCELLED_MENU
ld [wMenuExitMethod],a
ld [wMenuWatchMovingOutOfBounds],a
xor a
sta hJoy7
ld hl,wd730
res 6,[hl]
call BankswitchBack
; BUG: select bug
;xor a
;ld [wMenuItemToSwap],a ; 0 means no item is currently being swapped
scf
ret
PrintListMenuEntries::
coord hl, 5, 3
ld b,9
ld c,14
call ClearScreenArea
ld a,[wListPointer]
ld e,a
ld a,[wListPointer + 1]
ld d,a
inc de ; de = beginning of list entries
ld a,[wListScrollOffset]
ld c,a
ld a,[wListMenuID]
cp ITEMLISTMENU
ld a,c
jr nz,.skipMultiplying
; if it's an item menu
; item entries are 2 bytes long, so multiply by 2
sla a
sla c
.skipMultiplying
add e
ld e,a
jr nc,.noCarry
inc d
.noCarry
coord hl, 6, 4 ; coordinates of first list entry name
ld b,4 ; print 4 names
.loop
ld a,b
ld [wWhichPokemon],a
ld a,[de]
ld [wd11e],a
cp $ff
jp z,.printCancelMenuItem
push bc
push de
push hl
push hl
push de
ld a,[wListMenuID]
and a
jr z,.pokemonPCMenu
cp MOVESLISTMENU
jr z,.movesMenu
.itemMenu
call GetItemName
jr .placeNameString
.pokemonPCMenu
push hl
ld hl,wPartyCount
ld a,[wListPointer]
cp l ; is it a list of party pokemon or box pokemon?
ld hl,wPartyMonNicks
jr z,.getPokemonName
ld hl, wBoxMonNicks ; box pokemon names
.getPokemonName
ld a,[wWhichPokemon]
ld b,a
ld a,4
sub b
ld b,a
ld a,[wListScrollOffset]
add b
call GetPartyMonName
pop hl
jr .placeNameString
.movesMenu
call GetMoveName
.placeNameString
call PlaceString
pop de
pop hl
ld a,[wPrintItemPrices]
and a ; should prices be printed?
jr z,.skipPrintingItemPrice
.printItemPrice
push hl
ld a,[de]
ld de,ItemPrices
ld [wcf91],a
call GetItemPrice ; get price
pop hl
ld bc, 6 ; 6 columns right
add hl,bc