Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pret
GitHub Repository: pret/pokered
Path: blob/master/engine/menus/main_menu.asm
2023 views
1
MainMenu:
2
; Check save file
3
call InitOptions
4
xor a
5
ld [wOptionsInitialized], a
6
inc a
7
ld [wSaveFileStatus], a
8
call CheckForPlayerNameInSRAM
9
jr nc, .mainMenuLoop
10
11
predef TryLoadSaveFile
12
13
.mainMenuLoop
14
ld c, 20
15
call DelayFrames
16
xor a ; LINK_STATE_NONE
17
ld [wLinkState], a
18
ld hl, wPartyAndBillsPCSavedMenuItem
19
ld [hli], a
20
ld [hli], a
21
ld [hli], a
22
ld [hl], a
23
ld [wDefaultMap], a
24
ld hl, wStatusFlags4
25
res BIT_LINK_CONNECTED, [hl]
26
call ClearScreen
27
call RunDefaultPaletteCommand
28
call LoadTextBoxTilePatterns
29
call LoadFontTilePatterns
30
ld hl, wStatusFlags5
31
set BIT_NO_TEXT_DELAY, [hl]
32
ld a, [wSaveFileStatus]
33
cp 1
34
jr z, .noSaveFile
35
; there's a save file
36
hlcoord 0, 0
37
ld b, 6
38
ld c, 13
39
call TextBoxBorder
40
hlcoord 2, 2
41
ld de, ContinueText
42
call PlaceString
43
jr .next2
44
.noSaveFile
45
hlcoord 0, 0
46
ld b, 4
47
ld c, 13
48
call TextBoxBorder
49
hlcoord 2, 2
50
ld de, NewGameText
51
call PlaceString
52
.next2
53
ld hl, wStatusFlags5
54
res BIT_NO_TEXT_DELAY, [hl]
55
call UpdateSprites
56
xor a
57
ld [wCurrentMenuItem], a
58
ld [wLastMenuItem], a
59
ld [wMenuJoypadPollCount], a
60
inc a
61
ld [wTopMenuItemX], a
62
inc a
63
ld [wTopMenuItemY], a
64
ld a, PAD_A | PAD_B | PAD_START
65
ld [wMenuWatchedKeys], a
66
ld a, [wSaveFileStatus]
67
ld [wMaxMenuItem], a
68
call HandleMenuInput
69
bit B_PAD_B, a
70
jp nz, DisplayTitleScreen ; if so, go back to the title screen
71
ld c, 20
72
call DelayFrames
73
ld a, [wCurrentMenuItem]
74
ld b, a
75
ld a, [wSaveFileStatus]
76
cp 2
77
jp z, .skipInc
78
; If there's no save file, increment the current menu item so that the numbers
79
; are the same whether or not there's a save file.
80
inc b
81
.skipInc
82
ld a, b
83
and a
84
jr z, .choseContinue
85
cp 1
86
jp z, StartNewGame
87
call DisplayOptionMenu
88
ld a, TRUE
89
ld [wOptionsInitialized], a
90
jp .mainMenuLoop
91
.choseContinue
92
call DisplayContinueGameInfo
93
ld hl, wCurrentMapScriptFlags
94
set BIT_CUR_MAP_LOADED_1, [hl]
95
.inputLoop
96
xor a
97
ldh [hJoyPressed], a
98
ldh [hJoyReleased], a
99
ldh [hJoyHeld], a
100
call Joypad
101
ldh a, [hJoyHeld]
102
bit B_PAD_A, a
103
jr nz, .pressedA
104
bit B_PAD_B, a
105
jp nz, .mainMenuLoop
106
jr .inputLoop
107
.pressedA
108
call GBPalWhiteOutWithDelay3
109
call ClearScreen
110
ld a, PLAYER_DIR_DOWN
111
ld [wPlayerDirection], a
112
ld c, 10
113
call DelayFrames
114
ld a, [wNumHoFTeams]
115
and a
116
jp z, SpecialEnterMap
117
ld a, [wCurMap]
118
cp HALL_OF_FAME
119
jp nz, SpecialEnterMap
120
xor a
121
ld [wDestinationMap], a
122
ld hl, wStatusFlags6
123
set BIT_FLY_OR_DUNGEON_WARP, [hl]
124
call PrepareForSpecialWarp
125
jp SpecialEnterMap
126
127
InitOptions:
128
ld a, 1 << BIT_FAST_TEXT_DELAY
129
ld [wLetterPrintingDelayFlags], a
130
ld a, TEXT_DELAY_MEDIUM
131
ld [wOptions], a
132
ret
133
134
LinkMenu:
135
xor a
136
ld [wLetterPrintingDelayFlags], a
137
ld hl, wStatusFlags4
138
set BIT_LINK_CONNECTED, [hl]
139
ld hl, LinkMenuEmptyText
140
call PrintText
141
call SaveScreenTilesToBuffer1
142
ld hl, WhereWouldYouLikeText
143
call PrintText
144
hlcoord 5, 5
145
ld b, $6
146
ld c, $d
147
call TextBoxBorder
148
call UpdateSprites
149
hlcoord 7, 7
150
ld de, CableClubOptionsText
151
call PlaceString
152
xor a
153
ld [wUnusedLinkMenuByte], a
154
ld [wCableClubDestinationMap], a
155
ld hl, wTopMenuItemY
156
ld a, 7
157
ld [hli], a
158
ASSERT wTopMenuItemY + 1 == wTopMenuItemX
159
ld a, 6
160
ld [hli], a
161
ASSERT wTopMenuItemX + 1 == wCurrentMenuItem
162
xor a
163
ld [hli], a
164
inc hl
165
ASSERT wCurrentMenuItem + 2 == wMaxMenuItem
166
ld a, 2
167
ld [hli], a
168
ASSERT wMaxMenuItem + 1 == wMenuWatchedKeys
169
ASSERT 2 + 1 == PAD_A | PAD_B
170
inc a
171
ld [hli], a
172
ASSERT wMenuWatchedKeys + 1 == wLastMenuItem
173
xor a
174
ld [hl], a
175
.waitForInputLoop
176
call HandleMenuInput
177
and PAD_A | PAD_B
178
add a
179
add a
180
ld b, a
181
ld a, [wCurrentMenuItem]
182
add b
183
add $d0
184
ld [wLinkMenuSelectionSendBuffer], a
185
ld [wLinkMenuSelectionSendBuffer + 1], a
186
.exchangeMenuSelectionLoop
187
call Serial_ExchangeLinkMenuSelection
188
ld a, [wLinkMenuSelectionReceiveBuffer]
189
ld b, a
190
and $f0
191
cp $d0
192
jr z, .checkEnemyMenuSelection
193
ld a, [wLinkMenuSelectionReceiveBuffer + 1]
194
ld b, a
195
and $f0
196
cp $d0
197
jr nz, .exchangeMenuSelectionLoop
198
.checkEnemyMenuSelection
199
ld a, b
200
and $c ; did the enemy press A or B?
201
jr nz, .enemyPressedAOrB
202
; the enemy didn't press A or B
203
ld a, [wLinkMenuSelectionSendBuffer]
204
and $c ; did the player press A or B?
205
jr z, .waitForInputLoop ; if neither the player nor the enemy pressed A or B, try again
206
jr .doneChoosingMenuSelection ; if the player pressed A or B but the enemy didn't, use the player's selection
207
.enemyPressedAOrB
208
ld a, [wLinkMenuSelectionSendBuffer]
209
and $c ; did the player press A or B?
210
jr z, .useEnemyMenuSelection ; if the enemy pressed A or B but the player didn't, use the enemy's selection
211
; the enemy and the player both pressed A or B
212
; The gameboy that is clocking the connection wins.
213
ldh a, [hSerialConnectionStatus]
214
cp USING_INTERNAL_CLOCK
215
jr z, .doneChoosingMenuSelection
216
.useEnemyMenuSelection
217
ld a, b
218
ld [wLinkMenuSelectionSendBuffer], a
219
and $3
220
ld [wCurrentMenuItem], a
221
.doneChoosingMenuSelection
222
ldh a, [hSerialConnectionStatus]
223
cp USING_INTERNAL_CLOCK
224
jr nz, .skipStartingTransfer
225
call DelayFrame
226
call DelayFrame
227
ld a, SC_START | SC_INTERNAL
228
ldh [rSC], a
229
.skipStartingTransfer
230
ld b, ' '
231
ld c, ' '
232
ld d, ''
233
ld a, [wLinkMenuSelectionSendBuffer]
234
and PAD_B << 2 ; was B button pressed?
235
jr nz, .updateCursorPosition
236
; A button was pressed
237
ld a, [wCurrentMenuItem]
238
cp $2
239
jr z, .updateCursorPosition
240
ld c, d
241
ld d, b
242
dec a
243
jr z, .updateCursorPosition
244
ld b, c
245
ld c, d
246
.updateCursorPosition
247
ld a, b
248
ldcoord_a 6, 7
249
ld a, c
250
ldcoord_a 6, 9
251
ld a, d
252
ldcoord_a 6, 11
253
ld c, 40
254
call DelayFrames
255
call LoadScreenTilesFromBuffer1
256
ld a, [wLinkMenuSelectionSendBuffer]
257
and PAD_B << 2 ; was B button pressed?
258
jr nz, .choseCancel ; cancel if B pressed
259
ld a, [wCurrentMenuItem]
260
cp $2
261
jr z, .choseCancel
262
xor a
263
ld [wWalkBikeSurfState], a ; start walking
264
ld a, [wCurrentMenuItem]
265
and a
266
ld a, COLOSSEUM
267
jr nz, .next
268
ld a, TRADE_CENTER
269
.next
270
ld [wCableClubDestinationMap], a
271
ld hl, PleaseWaitText
272
call PrintText
273
ld c, 50
274
call DelayFrames
275
ld hl, wStatusFlags6
276
res BIT_DEBUG_MODE, [hl]
277
ld a, [wDefaultMap]
278
ld [wDestinationMap], a
279
call PrepareForSpecialWarp
280
ld c, 20
281
call DelayFrames
282
xor a
283
ld [wMenuJoypadPollCount], a
284
ld [wSerialExchangeNybbleSendData], a
285
inc a ; LINK_STATE_IN_CABLE_CLUB
286
ld [wLinkState], a
287
ld [wEnteringCableClub], a
288
jr SpecialEnterMap
289
.choseCancel
290
xor a
291
ld [wMenuJoypadPollCount], a
292
vc_hook Wireless_net_stop
293
call Delay3
294
call CloseLinkConnection
295
ld hl, LinkCanceledText
296
vc_hook Wireless_net_end
297
call PrintText
298
ld hl, wStatusFlags4
299
res BIT_LINK_CONNECTED, [hl]
300
ret
301
302
WhereWouldYouLikeText:
303
text_far _WhereWouldYouLikeText
304
text_end
305
306
PleaseWaitText:
307
text_far _PleaseWaitText
308
text_end
309
310
LinkCanceledText:
311
text_far _LinkCanceledText
312
text_end
313
314
StartNewGame:
315
ld hl, wStatusFlags6
316
; Ensure debug mode is not used when starting a regular new game.
317
; Debug mode persists in saved games for both debug and non-debug builds, and is
318
; only reset here by the main menu.
319
res BIT_DEBUG_MODE, [hl]
320
; fallthrough
321
StartNewGameDebug:
322
call OakSpeech
323
ld c, 20
324
call DelayFrames
325
326
; enter map after using a special warp or loading the game from the main menu
327
SpecialEnterMap::
328
xor a
329
ldh [hJoyPressed], a
330
ldh [hJoyHeld], a
331
ldh [hJoy5], a
332
ld [wCableClubDestinationMap], a
333
ld hl, wStatusFlags6
334
set BIT_GAME_TIMER_COUNTING, [hl]
335
call ResetPlayerSpriteData
336
ld c, 20
337
call DelayFrames
338
ld a, [wEnteringCableClub]
339
and a
340
ret nz
341
jp EnterMap
342
343
ContinueText:
344
db "CONTINUE"
345
next ""
346
; fallthrough
347
348
NewGameText:
349
db "NEW GAME"
350
next "OPTION@"
351
352
CableClubOptionsText:
353
db "TRADE CENTER"
354
next "COLOSSEUM"
355
next "CANCEL@"
356
357
DisplayContinueGameInfo:
358
xor a
359
ldh [hAutoBGTransferEnabled], a
360
hlcoord 4, 7
361
ld b, 8
362
ld c, 14
363
call TextBoxBorder
364
hlcoord 5, 9
365
ld de, SaveScreenInfoText
366
call PlaceString
367
hlcoord 12, 9
368
ld de, wPlayerName
369
call PlaceString
370
hlcoord 17, 11
371
call PrintNumBadges
372
hlcoord 16, 13
373
call PrintNumOwnedMons
374
hlcoord 13, 15
375
call PrintPlayTime
376
ld a, 1
377
ldh [hAutoBGTransferEnabled], a
378
ld c, 30
379
jp DelayFrames
380
381
PrintSaveScreenText:
382
xor a
383
ldh [hAutoBGTransferEnabled], a
384
hlcoord 4, 0
385
ld b, $8
386
ld c, $e
387
call TextBoxBorder
388
call LoadTextBoxTilePatterns
389
call UpdateSprites
390
hlcoord 5, 2
391
ld de, SaveScreenInfoText
392
call PlaceString
393
hlcoord 12, 2
394
ld de, wPlayerName
395
call PlaceString
396
hlcoord 17, 4
397
call PrintNumBadges
398
hlcoord 16, 6
399
call PrintNumOwnedMons
400
hlcoord 13, 8
401
call PrintPlayTime
402
ld a, $1
403
ldh [hAutoBGTransferEnabled], a
404
ld c, 30
405
jp DelayFrames
406
407
PrintNumBadges:
408
push hl
409
ld hl, wObtainedBadges
410
ld b, $1
411
call CountSetBits
412
pop hl
413
ld de, wNumSetBits
414
lb bc, 1, 2
415
jp PrintNumber
416
417
PrintNumOwnedMons:
418
push hl
419
ld hl, wPokedexOwned
420
ld b, wPokedexOwnedEnd - wPokedexOwned
421
call CountSetBits
422
pop hl
423
ld de, wNumSetBits
424
lb bc, 1, 3
425
jp PrintNumber
426
427
PrintPlayTime:
428
ld de, wPlayTimeHours
429
lb bc, 1, 3
430
call PrintNumber
431
ld [hl], $6d
432
inc hl
433
ld de, wPlayTimeMinutes
434
lb bc, LEADING_ZEROES | 1, 2
435
jp PrintNumber
436
437
SaveScreenInfoText:
438
db "PLAYER"
439
next "BADGES "
440
next "#DEX "
441
next "TIME@"
442
443
DisplayOptionMenu:
444
hlcoord 0, 0
445
ld b, 3
446
ld c, 18
447
call TextBoxBorder
448
hlcoord 0, 5
449
ld b, 3
450
ld c, 18
451
call TextBoxBorder
452
hlcoord 0, 10
453
ld b, 3
454
ld c, 18
455
call TextBoxBorder
456
hlcoord 1, 1
457
ld de, TextSpeedOptionText
458
call PlaceString
459
hlcoord 1, 6
460
ld de, BattleAnimationOptionText
461
call PlaceString
462
hlcoord 1, 11
463
ld de, BattleStyleOptionText
464
call PlaceString
465
hlcoord 2, 16
466
ld de, OptionMenuCancelText
467
call PlaceString
468
xor a
469
ld [wCurrentMenuItem], a
470
ld [wLastMenuItem], a
471
ASSERT BIT_FAST_TEXT_DELAY == 0
472
inc a ; 1 << BIT_FAST_TEXT_DELAY
473
ld [wLetterPrintingDelayFlags], a
474
ld [wOptionsCancelCursorX], a
475
ld a, 3 ; text speed cursor Y coordinate
476
ld [wTopMenuItemY], a
477
call SetCursorPositionsFromOptions
478
ld a, [wOptionsTextSpeedCursorX] ; text speed cursor X coordinate
479
ld [wTopMenuItemX], a
480
ld a, $01
481
ldh [hAutoBGTransferEnabled], a ; enable auto background transfer
482
call Delay3
483
.loop
484
call PlaceMenuCursor
485
call SetOptionsFromCursorPositions
486
.getJoypadStateLoop
487
call JoypadLowSensitivity
488
ldh a, [hJoy5]
489
ld b, a
490
and ~PAD_SELECT ; any key besides select pressed?
491
jr z, .getJoypadStateLoop
492
bit B_PAD_B, b
493
jr nz, .exitMenu
494
bit B_PAD_START, b
495
jr nz, .exitMenu
496
bit B_PAD_A, b
497
jr z, .checkDirectionKeys
498
; A was pressed
499
ld a, [wTopMenuItemY]
500
cp 16 ; is the cursor on Cancel?
501
jr nz, .loop
502
.exitMenu
503
ld a, SFX_PRESS_AB
504
call PlaySound
505
ret
506
.eraseOldMenuCursor
507
ld [wTopMenuItemX], a
508
call EraseMenuCursor
509
jp .loop
510
.checkDirectionKeys
511
ld a, [wTopMenuItemY]
512
bit B_PAD_DOWN, b
513
jr nz, .downPressed
514
bit B_PAD_UP, b
515
jr nz, .upPressed
516
cp 8 ; cursor in Battle Animation section?
517
jr z, .cursorInBattleAnimation
518
cp 13 ; cursor in Battle Style section?
519
jr z, .cursorInBattleStyle
520
cp 16 ; cursor on Cancel?
521
jr z, .loop
522
; cursor in Text Speed
523
bit B_PAD_LEFT, b
524
jp nz, .pressedLeftInTextSpeed
525
jp .pressedRightInTextSpeed
526
.downPressed
527
cp 16
528
ld b, -13
529
ld hl, wOptionsTextSpeedCursorX
530
jr z, .updateMenuVariables
531
ld b, 5
532
cp 3
533
inc hl
534
jr z, .updateMenuVariables
535
cp 8
536
inc hl
537
jr z, .updateMenuVariables
538
ld b, 3
539
inc hl
540
jr .updateMenuVariables
541
.upPressed
542
cp 8
543
ld b, -5
544
ld hl, wOptionsTextSpeedCursorX
545
jr z, .updateMenuVariables
546
cp 13
547
inc hl
548
jr z, .updateMenuVariables
549
cp 16
550
ld b, -3
551
inc hl
552
jr z, .updateMenuVariables
553
ld b, 13
554
inc hl
555
.updateMenuVariables
556
add b
557
ld [wTopMenuItemY], a
558
ld a, [hl]
559
ld [wTopMenuItemX], a
560
call PlaceUnfilledArrowMenuCursor
561
jp .loop
562
.cursorInBattleAnimation
563
ld a, [wOptionsBattleAnimCursorX] ; battle animation cursor X coordinate
564
xor 1 ^ 10 ; toggle between 1 and 10
565
ld [wOptionsBattleAnimCursorX], a
566
jp .eraseOldMenuCursor
567
.cursorInBattleStyle
568
ld a, [wOptionsBattleStyleCursorX] ; battle style cursor X coordinate
569
xor 1 ^ 10 ; toggle between 1 and 10
570
ld [wOptionsBattleStyleCursorX], a
571
jp .eraseOldMenuCursor
572
.pressedLeftInTextSpeed
573
ld a, [wOptionsTextSpeedCursorX] ; text speed cursor X coordinate
574
cp 1
575
jr z, .updateTextSpeedXCoord
576
cp 7
577
jr nz, .fromSlowToMedium
578
sub 6
579
jr .updateTextSpeedXCoord
580
.fromSlowToMedium
581
sub 7
582
jr .updateTextSpeedXCoord
583
.pressedRightInTextSpeed
584
ld a, [wOptionsTextSpeedCursorX] ; text speed cursor X coordinate
585
cp 14
586
jr z, .updateTextSpeedXCoord
587
cp 7
588
jr nz, .fromFastToMedium
589
add 7
590
jr .updateTextSpeedXCoord
591
.fromFastToMedium
592
add 6
593
.updateTextSpeedXCoord
594
ld [wOptionsTextSpeedCursorX], a ; text speed cursor X coordinate
595
jp .eraseOldMenuCursor
596
597
TextSpeedOptionText:
598
db "TEXT SPEED"
599
next " FAST MEDIUM SLOW@"
600
601
BattleAnimationOptionText:
602
db "BATTLE ANIMATION"
603
next " ON OFF@"
604
605
BattleStyleOptionText:
606
db "BATTLE STYLE"
607
next " SHIFT SET@"
608
609
OptionMenuCancelText:
610
db "CANCEL@"
611
612
; sets the options variable according to the current placement of the menu cursors in the options menu
613
SetOptionsFromCursorPositions:
614
ld hl, TextSpeedOptionData
615
ld a, [wOptionsTextSpeedCursorX] ; text speed cursor X coordinate
616
ld c, a
617
.loop
618
ld a, [hli]
619
cp c
620
jr z, .textSpeedMatchFound
621
inc hl
622
jr .loop
623
.textSpeedMatchFound
624
ld a, [hl]
625
ld d, a
626
ld a, [wOptionsBattleAnimCursorX] ; battle animation cursor X coordinate
627
dec a
628
jr z, .battleAnimationOn
629
; battle animation Off
630
set BIT_BATTLE_ANIMATION, d
631
jr .checkBattleStyle
632
.battleAnimationOn
633
res BIT_BATTLE_ANIMATION, d
634
.checkBattleStyle
635
ld a, [wOptionsBattleStyleCursorX] ; battle style cursor X coordinate
636
dec a
637
jr z, .battleStyleShift
638
; battle style Set
639
set BIT_BATTLE_SHIFT, d
640
jr .storeOptions
641
.battleStyleShift
642
res BIT_BATTLE_SHIFT, d
643
.storeOptions
644
ld a, d
645
ld [wOptions], a
646
ret
647
648
; reads the options variable and places menu cursors in the correct positions within the options menu
649
SetCursorPositionsFromOptions:
650
ld hl, TextSpeedOptionData + 1
651
ld a, [wOptions]
652
ld c, a
653
and $3f
654
push bc
655
ld de, 2
656
call IsInArray
657
pop bc
658
dec hl
659
ld a, [hl]
660
ld [wOptionsTextSpeedCursorX], a ; text speed cursor X coordinate
661
hlcoord 0, 3
662
call .placeUnfilledRightArrow
663
sla c
664
ld a, 1 ; On
665
jr nc, .storeBattleAnimationCursorX
666
ld a, 10 ; Off
667
.storeBattleAnimationCursorX
668
ld [wOptionsBattleAnimCursorX], a ; battle animation cursor X coordinate
669
hlcoord 0, 8
670
call .placeUnfilledRightArrow
671
sla c
672
ld a, 1
673
jr nc, .storeBattleStyleCursorX
674
ld a, 10
675
.storeBattleStyleCursorX
676
ld [wOptionsBattleStyleCursorX], a ; battle style cursor X coordinate
677
hlcoord 0, 13
678
call .placeUnfilledRightArrow
679
; cursor in front of Cancel
680
hlcoord 0, 16
681
ld a, 1
682
.placeUnfilledRightArrow
683
ld e, a
684
ld d, 0
685
add hl, de
686
ld [hl], ''
687
ret
688
689
; table that indicates how the 3 text speed options affect frame delays
690
; Format:
691
; 00: X coordinate of menu cursor
692
; 01: delay after printing a letter (in frames)
693
TextSpeedOptionData:
694
db 14, TEXT_DELAY_SLOW
695
db 7, TEXT_DELAY_MEDIUM
696
db 1, TEXT_DELAY_FAST
697
db 7, -1 ; end (default X coordinate)
698
699
CheckForPlayerNameInSRAM:
700
; Check if the player name data in SRAM has a string terminator character
701
; (indicating that a name may have been saved there) and return whether it does
702
; in carry.
703
ld a, RAMG_SRAM_ENABLE
704
ld [rRAMG], a
705
ld a, BMODE_ADVANCED
706
ld [rBMODE], a
707
ASSERT BANK(sPlayerName) == BMODE_ADVANCED
708
ld [rRAMB], a
709
ld b, NAME_LENGTH
710
ld hl, sPlayerName
711
.loop
712
ld a, [hli]
713
cp '@'
714
jr z, .found
715
dec b
716
jr nz, .loop
717
; not found
718
xor a
719
ld [rRAMG], a
720
ld [rBMODE], a
721
and a
722
ret
723
.found
724
xor a
725
ld [rRAMG], a
726
ld [rBMODE], a
727
scf
728
ret
729
730