Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pret
GitHub Repository: pret/pokered
Path: blob/master/engine/menus/main_menu.asm
1271 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
ld a, [wTopMenuItemY]
499
cp 16 ; is the cursor on Cancel?
500
jr nz, .loop
501
.exitMenu
502
ld a, SFX_PRESS_AB
503
call PlaySound
504
ret
505
.eraseOldMenuCursor
506
ld [wTopMenuItemX], a
507
call EraseMenuCursor
508
jp .loop
509
.checkDirectionKeys
510
ld a, [wTopMenuItemY]
511
bit B_PAD_DOWN, b
512
jr nz, .downPressed
513
bit B_PAD_UP, b
514
jr nz, .upPressed
515
cp 8 ; cursor in Battle Animation section?
516
jr z, .cursorInBattleAnimation
517
cp 13 ; cursor in Battle Style section?
518
jr z, .cursorInBattleStyle
519
cp 16 ; cursor on Cancel?
520
jr z, .loop
521
.cursorInTextSpeed
522
bit B_PAD_LEFT, b
523
jp nz, .pressedLeftInTextSpeed
524
jp .pressedRightInTextSpeed
525
.downPressed
526
cp 16
527
ld b, -13
528
ld hl, wOptionsTextSpeedCursorX
529
jr z, .updateMenuVariables
530
ld b, 5
531
cp 3
532
inc hl
533
jr z, .updateMenuVariables
534
cp 8
535
inc hl
536
jr z, .updateMenuVariables
537
ld b, 3
538
inc hl
539
jr .updateMenuVariables
540
.upPressed
541
cp 8
542
ld b, -5
543
ld hl, wOptionsTextSpeedCursorX
544
jr z, .updateMenuVariables
545
cp 13
546
inc hl
547
jr z, .updateMenuVariables
548
cp 16
549
ld b, -3
550
inc hl
551
jr z, .updateMenuVariables
552
ld b, 13
553
inc hl
554
.updateMenuVariables
555
add b
556
ld [wTopMenuItemY], a
557
ld a, [hl]
558
ld [wTopMenuItemX], a
559
call PlaceUnfilledArrowMenuCursor
560
jp .loop
561
.cursorInBattleAnimation
562
ld a, [wOptionsBattleAnimCursorX] ; battle animation cursor X coordinate
563
xor 1 ^ 10 ; toggle between 1 and 10
564
ld [wOptionsBattleAnimCursorX], a
565
jp .eraseOldMenuCursor
566
.cursorInBattleStyle
567
ld a, [wOptionsBattleStyleCursorX] ; battle style cursor X coordinate
568
xor 1 ^ 10 ; toggle between 1 and 10
569
ld [wOptionsBattleStyleCursorX], a
570
jp .eraseOldMenuCursor
571
.pressedLeftInTextSpeed
572
ld a, [wOptionsTextSpeedCursorX] ; text speed cursor X coordinate
573
cp 1
574
jr z, .updateTextSpeedXCoord
575
cp 7
576
jr nz, .fromSlowToMedium
577
sub 6
578
jr .updateTextSpeedXCoord
579
.fromSlowToMedium
580
sub 7
581
jr .updateTextSpeedXCoord
582
.pressedRightInTextSpeed
583
ld a, [wOptionsTextSpeedCursorX] ; text speed cursor X coordinate
584
cp 14
585
jr z, .updateTextSpeedXCoord
586
cp 7
587
jr nz, .fromFastToMedium
588
add 7
589
jr .updateTextSpeedXCoord
590
.fromFastToMedium
591
add 6
592
.updateTextSpeedXCoord
593
ld [wOptionsTextSpeedCursorX], a ; text speed cursor X coordinate
594
jp .eraseOldMenuCursor
595
596
TextSpeedOptionText:
597
db "TEXT SPEED"
598
next " FAST MEDIUM SLOW@"
599
600
BattleAnimationOptionText:
601
db "BATTLE ANIMATION"
602
next " ON OFF@"
603
604
BattleStyleOptionText:
605
db "BATTLE STYLE"
606
next " SHIFT SET@"
607
608
OptionMenuCancelText:
609
db "CANCEL@"
610
611
; sets the options variable according to the current placement of the menu cursors in the options menu
612
SetOptionsFromCursorPositions:
613
ld hl, TextSpeedOptionData
614
ld a, [wOptionsTextSpeedCursorX] ; text speed cursor X coordinate
615
ld c, a
616
.loop
617
ld a, [hli]
618
cp c
619
jr z, .textSpeedMatchFound
620
inc hl
621
jr .loop
622
.textSpeedMatchFound
623
ld a, [hl]
624
ld d, a
625
ld a, [wOptionsBattleAnimCursorX] ; battle animation cursor X coordinate
626
dec a
627
jr z, .battleAnimationOn
628
.battleAnimationOff
629
set BIT_BATTLE_ANIMATION, d
630
jr .checkBattleStyle
631
.battleAnimationOn
632
res BIT_BATTLE_ANIMATION, d
633
.checkBattleStyle
634
ld a, [wOptionsBattleStyleCursorX] ; battle style cursor X coordinate
635
dec a
636
jr z, .battleStyleShift
637
.battleStyleSet
638
set BIT_BATTLE_SHIFT, d
639
jr .storeOptions
640
.battleStyleShift
641
res BIT_BATTLE_SHIFT, d
642
.storeOptions
643
ld a, d
644
ld [wOptions], a
645
ret
646
647
; reads the options variable and places menu cursors in the correct positions within the options menu
648
SetCursorPositionsFromOptions:
649
ld hl, TextSpeedOptionData + 1
650
ld a, [wOptions]
651
ld c, a
652
and $3f
653
push bc
654
ld de, 2
655
call IsInArray
656
pop bc
657
dec hl
658
ld a, [hl]
659
ld [wOptionsTextSpeedCursorX], a ; text speed cursor X coordinate
660
hlcoord 0, 3
661
call .placeUnfilledRightArrow
662
sla c
663
ld a, 1 ; On
664
jr nc, .storeBattleAnimationCursorX
665
ld a, 10 ; Off
666
.storeBattleAnimationCursorX
667
ld [wOptionsBattleAnimCursorX], a ; battle animation cursor X coordinate
668
hlcoord 0, 8
669
call .placeUnfilledRightArrow
670
sla c
671
ld a, 1
672
jr nc, .storeBattleStyleCursorX
673
ld a, 10
674
.storeBattleStyleCursorX
675
ld [wOptionsBattleStyleCursorX], a ; battle style cursor X coordinate
676
hlcoord 0, 13
677
call .placeUnfilledRightArrow
678
; cursor in front of Cancel
679
hlcoord 0, 16
680
ld a, 1
681
.placeUnfilledRightArrow
682
ld e, a
683
ld d, 0
684
add hl, de
685
ld [hl], "▷"
686
ret
687
688
; table that indicates how the 3 text speed options affect frame delays
689
; Format:
690
; 00: X coordinate of menu cursor
691
; 01: delay after printing a letter (in frames)
692
TextSpeedOptionData:
693
db 14, TEXT_DELAY_SLOW
694
db 7, TEXT_DELAY_MEDIUM
695
db 1, TEXT_DELAY_FAST
696
db 7, -1 ; end (default X coordinate)
697
698
CheckForPlayerNameInSRAM:
699
; Check if the player name data in SRAM has a string terminator character
700
; (indicating that a name may have been saved there) and return whether it does
701
; in carry.
702
ld a, RAMG_SRAM_ENABLE
703
ld [rRAMG], a
704
ld a, BMODE_ADVANCED
705
ld [rBMODE], a
706
ASSERT BANK(sPlayerName) == BMODE_ADVANCED
707
ld [rRAMB], a
708
ld b, NAME_LENGTH
709
ld hl, sPlayerName
710
.loop
711
ld a, [hli]
712
cp "@"
713
jr z, .found
714
dec b
715
jr nz, .loop
716
; not found
717
xor a
718
ld [rRAMG], a
719
ld [rBMODE], a
720
and a
721
ret
722
.found
723
xor a
724
ld [rRAMG], a
725
ld [rBMODE], a
726
scf
727
ret
728
729