aboutsummaryrefslogtreecommitdiffstats
path: root/engine/slop_menu.asm
blob: 4c93e2ed90f2a5f3549487c9b5a5821a1b53805d (plain) (blame)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
; SL0P MENU - a top-level cheat menu opened with SELECT in the overworld
; (hooked in home/overworld.asm). Table-driven: each entry has a name in
; SlopMenuText and a handler in SlopMenuHandlers. A handler returns:
;     carry SET   -> a map warp was started; exit the menu and let it proceed
;     carry CLEAR -> return to the SL0P menu (redraw)
; Handlers may be submenus (WarpSubmenu, GiveItemSubmenu) or direct actions.
;
; To add an entry: add a "NAME" line to SlopMenuText, a `dw Handler` to
; SlopMenuHandlers, and bump SLOP_MENU_COUNT.

DEF SLOP_MENU_COUNT EQU 14

SECTION "Slop Menu", ROMX

SlopMenu::
; ---- open the menu display (shared by the top menu and every submenu) ----
	ldh a, [hUILayoutFlags]
	set BIT_SINGLE_SPACED_LINES, a
	set BIT_DOUBLE_SPACED_MENU, a
	ldh [hUILayoutFlags], a
	ld a, $ff
	ld [wUpdateSpritesEnabled], a
	call ClearSprites
.redraw
	call ClearScreen
	hlcoord 5, 0
	ld de, SlopMenuTitle
	call PlaceString
	hlcoord 4, 2
	lb bc, SLOP_MENU_COUNT + 2, 11
	call TextBoxBorder
	hlcoord 6, 3
	ld de, SlopMenuText
	call PlaceString
; ON marker for the NOWILD toggle (item 10 -> row 13)
	ld a, [wUnusedFlag]
	and a
	jr z, .nowildOff
	hlcoord 13, 13
	ld de, SlopOnText
	call PlaceString
.nowildOff
	call SlopMenuShow
	xor a
	ld [wCurrentMenuItem], a
	ld [wLastMenuItem], a
	ld [wMenuWatchMovingOutOfBounds], a
	ld [wMenuJoypadPollCount], a
	ld a, 5
	ld [wTopMenuItemX], a
	ld a, 3
	ld [wTopMenuItemY], a
	ld a, PAD_A | PAD_B
	ld [wMenuWatchedKeys], a
	ld a, SLOP_MENU_COUNT - 1
	ld [wMaxMenuItem], a
	call HandleMenuInput
	bit B_PAD_B, a
	jr nz, .close
; dispatch: call SlopMenuHandlers[wCurrentMenuItem]
	ld a, [wCurrentMenuItem]
	add a
	ld e, a
	ld d, 0
	ld hl, SlopMenuHandlers
	add hl, de
	ld a, [hli]
	ld h, [hl]
	ld l, a
	ld de, .afterHandler
	push de
	jp hl
.afterHandler
	jr c, .exitProceed
	jr .redraw
.exitProceed
	call SlopMenuRestoreFlags
	ret
.close
	call SlopMenuRestoreFlags
	ld a, $01
	ld [wUpdateSpritesEnabled], a
; CloseTextDisplay rebuilds the overworld (LoadCurrentMapView restores the
; collision map) and ends with `pop af` + bankswitch expecting the caller's bank
; to have been pushed. We bypass DisplayTextID, so push it ourselves - otherwise
; the pop eats a stack word and corrupts the bank/collision state.
	ldh a, [hLoadedROMBank]
	push af
	jp CloseTextDisplay

SlopMenuRestoreFlags:
	ldh a, [hUILayoutFlags]
	res BIT_SINGLE_SPACED_LINES, a
	res BIT_DOUBLE_SPACED_MENU, a
	ldh [hUILayoutFlags], a
	ret

; push wTileMap into VRAM and switch on the menu window display
SlopMenuShow:
	ld b, HIGH(vBGMap1)
	call CopyScreenTileBufferToVRAM
	xor a
	ldh [hWY], a
	call LoadFontTilePatterns
	ld a, $01
	ldh [hAutoBGTransferEnabled], a
	ret

SlopMenuTitle:
	db "SL0P MENU@"
SlopMenuText:
	db   "WARP"
	next "HEAL"
	next "MONEY"
	next "COINS"
	next "BADGES"
	next "DEX"
	next "MAXTEAM"
	next "TEACH"
	next "HMS"
	next "ITEMS"
	next "NOWILD"
	next "REPEL"
	next "EXPLORE"
	next "SAVER@"
SlopOnText:
	db "ON@"
SlopMenuHandlers:
	dw WarpSubmenu
	dw SlopHealParty
	dw SlopMaxCash
	dw SlopMaxCoins
	dw SlopAllBadges
	dw SlopDex
	dw SlopMaxTeam
	dw SlopTeach
	dw SlopGiveHMs
	dw GiveItemSubmenu
	dw SlopToggleNoWild
	dw SlopRepel
	dw SlopExplore
	dw SlopSaver


; ===========================================================================
; WARP submenu
; ===========================================================================
DEF WARP_COUNT EQU 13

WarpSubmenu:
	call ClearScreen
	hlcoord 4, 0
	ld de, WarpTitleText1
	call PlaceString
	hlcoord 5, 1
	ld de, WarpTitleText2
	call PlaceString
	hlcoord 3, 2
	lb bc, 13, 11
	call TextBoxBorder
	hlcoord 5, 3
	ld de, WarpMenuText
	call PlaceString
	call SlopMenuShow
	xor a
	ld [wCurrentMenuItem], a
	ld [wLastMenuItem], a
	ld [wMenuWatchMovingOutOfBounds], a
	ld [wMenuJoypadPollCount], a
	ld a, 4
	ld [wTopMenuItemX], a
	ld a, 3
	ld [wTopMenuItemY], a
	ld a, PAD_A | PAD_B
	ld [wMenuWatchedKeys], a
	ld a, WARP_COUNT - 1
	ld [wMaxMenuItem], a
	call HandleMenuInput
	bit B_PAD_B, a
	jr nz, .cancel
	ld a, [wCurrentMenuItem]
	ld e, a
	ld d, 0
	ld hl, WarpMapTable
	add hl, de
	ld a, [hl]
	ld [wDestinationMap], a
	ld hl, wStatusFlags6
	set BIT_FLY_WARP, [hl]
	scf
	ret
.cancel
	and a
	ret

WarpTitleText1:
	db "SECRET SL0P@"
WarpTitleText2:
	db "WARP MENU@"
WarpMenuText:
	db   "PALLET"
	next "VIRIDIAN"
	next "PEWTER"
	next "CERULEAN"
	next "LAVENDER"
	next "VERMILION"
	next "CELADON"
	next "FUCHSIA"
	next "CINNABAR"
	next "INDIGO"
	next "SAFFRON"
	next "ROUTE 4"
	next "ROUTE 10@"
WarpMapTable:
	db PALLET_TOWN, VIRIDIAN_CITY, PEWTER_CITY, CERULEAN_CITY, LAVENDER_TOWN
	db VERMILION_CITY, CELADON_CITY, FUCHSIA_CITY, CINNABAR_ISLAND, INDIGO_PLATEAU
	db SAFFRON_CITY, ROUTE_4, ROUTE_10


; ===========================================================================
; EXPLORE: a screen-paging overworld camera. Each d-pad press INSTANTLY redraws
; the next screenful of the world -- no character walking. At a map edge it hops
; to the connected map using that connection's alignment offset (or clamps if
; there's none). B restores the origin view and returns to the SL0P menu. It is
; self-contained: it renders straight from the map block buffer (wOverworldMap)
; and never touches the walk engine.
; ===========================================================================
DEF SLOP_PAGE_X EQU 10          ; coords moved per horizontal page (~one screen)
DEF SLOP_PAGE_Y EQU 8          ; coords moved per vertical page
; smooth-scroll animation speed. SLOP_SCROLL_PX must divide 16 (one coord-step
; is 16 px). Bigger = faster/steppier, smaller = slower/smoother.
DEF SLOP_SCROLL_PX EQU 8
DEF SLOP_SCROLL_FRAMES EQU 16 / SLOP_SCROLL_PX

SlopSaver:
; SCREENSAVER: auto-scroll the overworld. Seed an initial direction; the rest is
; identical to EXPLORE (shared body), driven by SlopSaverPickDir in the loop.
	ld a, PAD_DOWN
	ld [wSlopSaverDir], a
	jr SlopExploreShared
SlopExplore:
	xor a
	ld [wSlopSaverDir], a
SlopExploreShared:
; wait for the opening button to be released so it isn't read as EXPLORE input
.waitRelease
	call DelayFrame
	call Joypad                    ; VBlank only reads raw input; Joypad updates hJoyHeld
	ldh a, [hJoyHeld]
	and a
	jr nz, .waitRelease
; save origin (map, coords, block coords, view pointer) to restore on exit
	ld a, [wYCoord]
	ld b, a
	ld a, [wXCoord]
	ld c, a
	push bc
	ld a, [wYBlockCoord]
	ld b, a
	ld a, [wXBlockCoord]
	ld c, a
	push bc
	ld a, [wCurrentTileBlockMapViewPointer]
	ld b, a
	ld a, [wCurrentTileBlockMapViewPointer + 1]
	ld c, a
	push bc
	ld a, [wCurMap]
	push af
	call LoadMapData               ; reload current map + tileset (menu clobbered VRAM)
.renderView
; LoadMapData (entry/cross) re-enables sprites and reloads the player + Pikachu
; follower. Freeze and clear OAM now, before the first frame renders, so they are
; never drawn during EXPLORE/screensaver.
	ld a, $ff
	ld [wUpdateSpritesEnabled], a
	call ClearSprites
	call SlopClampCoords            ; a cross can land outside the new map -> clamp
	call SlopExploreRenderBG        ; fresh render to vBGMap0, hardware scroll reset
.renderArrows
	call LoadFontTilePatterns      ; arrow glyphs ($ed/$ee) into VRAM for the OBJ layer
; draw a direction-arrow sprite for each direction the camera can currently move:
; either there's more map that way, or the map has a connection that way.
	call ClearSprites
	ld a, [wSlopSaverDir]
	and a
	jp nz, .arrowsDone             ; screensaver hides the direction arrows
	ld hl, wShadowOAM
; up: movable unless at the top edge (wYCoord == 0) with no north connection
	ld a, [wYCoord]
	and a
	jr nz, .putUp
	ld a, [wNorthConnectedMap]
	inc a
	jr z, .skipUp
.putUp
	ld de, .arrUp
	call .putArrow
.skipUp
; down: movable unless at the bottom edge with no south connection
	ld a, [wCurrentMapHeight2]
	dec a
	ld b, a
	ld a, [wYCoord]
	cp b
	jr nz, .putDown
	ld a, [wSouthConnectedMap]
	inc a
	jr z, .skipDown
.putDown
	ld de, .arrDown
	call .putArrow
.skipDown
; left: movable unless at the left edge with no west connection
	ld a, [wXCoord]
	and a
	jr nz, .putLeft
	ld a, [wWestConnectedMap]
	inc a
	jr z, .skipLeft
.putLeft
	ld de, .arrLeft
	call .putArrow
.skipLeft
; right: movable unless at the right edge with no east connection
	ld a, [wCurrentMapWidth2]
	dec a
	ld b, a
	ld a, [wXCoord]
	cp b
	jr nz, .putRight
	ld a, [wEastConnectedMap]
	inc a
	jr z, .skipRight
.putRight
	ld de, .arrRight
	call .putArrow
.skipRight
	jr .arrowsDone
.putArrow                        ; de -> Y, X, tile, attr; write to [hl], advance hl
	ld a, [de]
	inc de
	ld [hli], a
	ld a, [de]
	inc de
	ld [hli], a
	ld a, [de]
	inc de
	ld [hli], a
	ld a, [de]
	ld [hli], a
	ret
.arrUp    db  18,  80, $ee, $40   ; down-arrow glyph, Y-flipped -> up
.arrDown  db 144,  80, $ee, $00
.arrLeft  db  80,  12, $ed, $20   ; right-arrow glyph, X-flipped -> left
.arrRight db  80, 156, $ed, $00
.arrowsDone
	ld a, $ff
	ld [wUpdateSpritesEnabled], a  ; freeze shadow OAM so PrepareOAMData keeps our arrows
	ldh a, [rLCDC]
	res B_LCDC_OBJ_SIZE, a         ; 8x8 sprites -> single-tile arrows
	set B_LCDC_OBJS, a             ; show the arrow sprites
	ldh [rLCDC], a
	ld a, $90
	ldh [hWY], a
.input
	call DelayFrame
	ld a, [wSlopSaverDir]
	and a
	jr nz, .saverInput
	call JoypadLowSensitivity
	ldh a, [hJoyPressed]
	ld d, a
	jr .dispatch
.saverInput
	call Joypad                    ; refresh hJoyHeld (VBlank only reads raw input)
	ldh a, [hJoyHeld]              ; any real button wakes/exits the screensaver
	and a
	jp nz, .exit
	call SlopSaverPickDir          ; d = auto-picked scroll direction
.dispatch
	bit B_PAD_B, d
	jp nz, .exit
	bit B_PAD_A, d
	jp nz, .warp
	bit B_PAD_UP, d
	jr z, .ckDown
	ld a, [wYCoord]
	sub SLOP_PAGE_Y
	jp nc, .moveUp
	jp .edgeUp
.ckDown
	bit B_PAD_DOWN, d
	jr z, .ckLeft
	ld a, [wCurrentMapHeight2]
	dec a
	ld b, a                        ; b = max Y
	ld a, [wYCoord]
	add SLOP_PAGE_Y
	cp b
	jp c, .moveDown
	jp z, .moveDown
	jp .edgeDown
.ckLeft
	bit B_PAD_LEFT, d
	jr z, .ckRight
	ld a, [wXCoord]
	sub SLOP_PAGE_X
	jp nc, .moveLeft
	jp .edgeLeft
.ckRight
	bit B_PAD_RIGHT, d
	jp z, .input
	ld a, [wCurrentMapWidth2]
	dec a
	ld b, a                        ; b = max X
	ld a, [wXCoord]
	add SLOP_PAGE_X
	cp b
	jp c, .moveRight
	jp z, .moveRight
	jp .edgeRight
; in-map page moves: smooth-scroll one screen, then just redraw the arrows
; (the scroll already left the destination view on screen in rotated VRAM).
.moveUp
	ld c, SLOP_PAGE_Y
	call SlopScrollUp
	jp .renderArrows
.moveDown
	ld c, SLOP_PAGE_Y
	call SlopScrollDown
	jp .renderArrows
.moveLeft
	ld c, SLOP_PAGE_X
	call SlopScrollLeft
	jp .renderArrows
.moveRight
	ld c, SLOP_PAGE_X
	call SlopScrollRight
	jp .renderArrows
; ---- map-edge handling ----
; A full page won't fit before the edge, so smooth-scroll the remaining (even)
; distance right up to the edge, then either cross into the connected map (one
; unavoidable instant load) or, if there's no connection that way, just stop at
; the edge. This keeps the motion smooth instead of hard-jumping a whole screen.
.edgeUp
	ld a, [wYCoord]
	and $fe                        ; even coords remaining to the top edge
	jr z, .edgeUpCross
	ld c, a
	call SlopScrollUp
.edgeUpCross
	ld hl, wNorthConnectedMap
	ld de, wNorthConnectedMapYAlignment
	jp .crossV
.edgeDown
	ld a, [wCurrentMapHeight2]
	dec a                          ; max Y
	ld hl, wYCoord
	sub [hl]                       ; maxY - wYCoord
	and $fe
	jr z, .edgeDownCross
	ld c, a
	call SlopScrollDown
.edgeDownCross
	ld hl, wSouthConnectedMap
	ld de, wSouthConnectedMapYAlignment
	jp .crossV
.edgeLeft
	ld a, [wXCoord]
	and $fe
	jr z, .edgeLeftCross
	ld c, a
	call SlopScrollLeft
.edgeLeftCross
	ld hl, wWestConnectedMap
	ld de, wWestConnectedMapYAlignment
	jp .crossH
.edgeRight
	ld a, [wCurrentMapWidth2]
	dec a                          ; max X
	ld hl, wXCoord
	sub [hl]                       ; maxX - wXCoord
	and $fe
	jr z, .edgeRightCross
	ld c, a
	call SlopScrollRight
.edgeRightCross
	ld hl, wEastConnectedMap
	ld de, wEastConnectedMapYAlignment
	jp .crossH
; vertical cross: hl -> connected-map byte, de -> its YAlignment. With no
; connection we're already scrolled to the edge, so just redraw the arrows.
.crossV
	ld a, [hl]
	inc a
	jr z, .clampArrows             ; $ff -> no connection
	ld a, [de]                     ; YAlignment -> new Y (absolute)
	ld c, a
	inc de
	ld a, [de]                     ; XAlignment -> added to X
	ld b, a
	ld a, [wXCoord]
	add b
	ld [wXCoord], a
	ld a, c
	ld [wYCoord], a
	ld a, [hl]
	ld [wCurMap], a
	call LoadMapData
	jp .renderView
; horizontal cross: hl -> connected-map byte, de -> its YAlignment.
.crossH
	ld a, [hl]
	inc a
	jr z, .clampArrows
	ld a, [de]                     ; YAlignment -> added to Y
	ld c, a
	inc de
	ld a, [de]                     ; XAlignment -> new X (absolute)
	ld [wXCoord], a
	ld a, [wYCoord]
	add c
	ld [wYCoord], a
	ld a, [hl]
	ld [wCurMap], a
	call LoadMapData
	jp .renderView
.clampArrows
	jp .renderArrows
; ---- A: warp the player to the walkable tile nearest the screen centre ----
; the centre tile is wTileMap(col 8,row 9) = coord (wYCoord,wXCoord); a coord
; offset (dy,dx) maps to wTileMap(8+2dx, 9+2dy). Scan outward for a passable tile.
.warp
	ld hl, SlopWarpOffsets
.warpScan
	ld a, [hli]
	cp $80
	jr z, .warpGo                  ; nothing walkable found -> land on the centre
	ld d, a                        ; d = dy
	ld a, [hli]
	ld e, a                        ; e = dx
	push hl
	ld a, d
	add a
	add 9                          ; row = 9 + 2*dy
	ld b, a
	ld hl, wTileMap
.warpRow
	ld a, b
	and a
	jr z, .warpCol
	ld a, 20
	add l
	ld l, a
	jr nc, .warpRowNC
	inc h
.warpRowNC
	dec b
	jr .warpRow
.warpCol
	ld a, e
	add a
	add 8                          ; col = 8 + 2*dx
	add l
	ld l, a
	jr nc, .warpColNC
	inc h
.warpColNC
	ld a, [hl]
	ld c, a
	call IsTilePassable            ; carry clear -> walkable
	pop hl
	jr c, .warpScan
	ld a, [wYCoord]
	add d
	ld [wYCoord], a
	ld a, [wXCoord]
	add e
	ld [wXCoord], a
.warpGo
	ld a, $ff
	ld [wDestinationWarpID], a     ; enter at wYCoord/wXCoord (not a warp position)
	ld a, [wYCoord]
	and 1
	ld [wYBlockCoord], a
	ld a, [wXCoord]
	and 1
	ld [wXBlockCoord], a
	call SlopSetViewPtr
	call LoadMapData               ; enter the map (player sprite, wild data, warps)
	pop af                         ; drop the 4 saved-origin values (we're staying here)
	pop bc
	pop bc
	pop bc
	scf                            ; carry set -> close menu, resume overworld here
	ret
.exit
	pop af
	ld [wCurMap], a
	pop bc
	ld a, b
	ld [wCurrentTileBlockMapViewPointer], a
	ld a, c
	ld [wCurrentTileBlockMapViewPointer + 1], a
	pop bc
	ld a, b
	ld [wYBlockCoord], a
	ld a, c
	ld [wXBlockCoord], a
	pop bc
	ld a, b
	ld [wYCoord], a
	ld a, c
	ld [wXCoord], a
	call LoadMapData               ; restore origin view (renders from restored pointer)
	and a                          ; carry clear -> back to SL0P menu
	ret

; Clamp wYCoord to [0,maxY] and wXCoord to [0,maxX] of the current map. Crossing a
; connection applies its alignment offset blindly, so at an edge position the
; connection doesn't span the camera can land outside the neighbour; rendering
; that reads past wOverworldMap into other WRAM = on-screen garbage.
SlopClampCoords:
	ld a, [wCurrentMapHeight2]
	dec a
	ld b, a                        ; maxY
	ld a, [wYCoord]
	call .clamp
	ld [wYCoord], a
	ld a, [wCurrentMapWidth2]
	dec a
	ld b, a                        ; maxX
	ld a, [wXCoord]
	call .clamp
	ld [wXCoord], a
	ret
; a = coord, b = max -> a clamped to [0,max]. Out-of-range values >= $80 are
; treated as "past the near edge" (negative) and clamped to 0, otherwise to max.
.clamp
	cp b
	ret c
	ret z
	bit 7, a
	jr nz, .zero
	ld a, b
	ret
.zero
	xor a
	ret

; wCurrentTileBlockMapViewPointer = wOverworldMap + (wYCoord/2 + 1)*stride + wXCoord/2
SlopSetViewPtr:
	ld a, [wCurMapWidth]
	add MAP_BORDER * 2
	ld c, a
	ld b, 0
	ld a, [wYCoord]
	srl a
	inc a
	ld hl, 0
.loop
	add hl, bc
	dec a
	jr nz, .loop
	ld a, [wXCoord]
	srl a
	add l
	ld l, a
	jr nc, .nc
	inc h
.nc
	ld bc, wOverworldMap
	add hl, bc
	ld a, l
	ld [wCurrentTileBlockMapViewPointer], a
	ld a, h
	ld [wCurrentTileBlockMapViewPointer + 1], a
	ret

; render the current view freshly to vBGMap0 and reset the hardware scroll so
; the camera is block-aligned at SCX/SCY = 0. Used on entry, map-cross and
; clamp (i.e. everywhere except the animated in-map page moves).
SlopExploreRenderBG:
	xor a
	ld [wYBlockCoord], a           ; block-aligned camera centred on wYCoord/wXCoord
	ld [wXBlockCoord], a
	ldh [hAutoBGTransferEnabled], a ; use RedrawRowOrColumn path, not AutoBgMapTransfer
	ld [wMapViewVRAMPointer], a
	ldh [hSCX], a
	ldh [hSCY], a
	ld a, HIGH(vBGMap0)
	ld [wMapViewVRAMPointer + 1], a
	call SlopSetViewPtr
	call LoadCurrentMapView
	ld b, HIGH(vBGMap0)
	jp CopyScreenTileBufferToVRAM

; ---------------------------------------------------------------------------
; Smooth EXPLORE scrolling. Each routine pans the camera `c` coords in one
; direction, 16 px per coord-step, reusing the walking engine's rotating-VRAM
; machinery: advance wMapViewVRAMPointer, re-render wTileMap for the new
; position, inject the freshly exposed 2-tile edge with a Schedule*Redraw
; (applied by RedrawRowOrColumn in VBlank), and ramp hSCX/hSCY SLOP_SCROLL_PX
; px per frame. After an even coord count it ends block-aligned exactly where
; SlopExploreRenderBG would have placed the view, so wTileMap / the block
; pointer / arrow logic / warp scan are all consistent afterwards.
; ---------------------------------------------------------------------------
SlopScrollRight:
	xor a
	ldh [hAutoBGTransferEnabled], a
.loop
	push bc
	ld a, [wXCoord]
	inc a
	ld [wXCoord], a
	ld a, [wMapViewVRAMPointer]     ; VRAM col +2 (wrap within the 32-tile row)
	ld e, a
	and $e0
	ld d, a
	ld a, e
	add 2
	and $1f
	or d
	ld [wMapViewVRAMPointer], a
	ld hl, wXBlockCoord
	inc [hl]
	ld a, [hl]
	cp 2
	jr nz, .go
	ld [hl], 0                     ; crossed a block east -> advance block pointer
	ld hl, wCurrentTileBlockMapViewPointer
	inc [hl]
	jr nz, .go
	inc hl
	inc [hl]
.go
	call LoadCurrentMapView
	call ScheduleEastColumnRedraw
	ld c, SLOP_SCROLL_FRAMES
.frames
	ldh a, [hSCX]
	add SLOP_SCROLL_PX
	ldh [hSCX], a
	call DelayFrame
	dec c
	jr nz, .frames
	pop bc
	dec c
	jr nz, .loop
	ret

SlopScrollLeft:
	xor a
	ldh [hAutoBGTransferEnabled], a
.loop
	push bc
	ld a, [wXCoord]
	dec a
	ld [wXCoord], a
	ld a, [wMapViewVRAMPointer]     ; VRAM col -2
	ld e, a
	and $e0
	ld d, a
	ld a, e
	sub 2
	and $1f
	or d
	ld [wMapViewVRAMPointer], a
	ld hl, wXBlockCoord
	dec [hl]
	ld a, [hl]
	inc a
	jr nz, .go                     ; was 0 -> now $ff: crossed a block west
	ld [hl], 1
	ld hl, wCurrentTileBlockMapViewPointer
	ld a, [hl]
	sub 1
	ld [hl], a
	jr nc, .go
	inc hl
	dec [hl]
.go
	call LoadCurrentMapView
	call ScheduleWestColumnRedraw
	ld c, SLOP_SCROLL_FRAMES
.frames
	ldh a, [hSCX]
	sub SLOP_SCROLL_PX
	ldh [hSCX], a
	call DelayFrame
	dec c
	jr nz, .frames
	pop bc
	dec c
	jr nz, .loop
	ret

SlopScrollDown:
	xor a
	ldh [hAutoBGTransferEnabled], a
.loop
	push bc
	ld a, [wYCoord]
	inc a
	ld [wYCoord], a
	ld a, [wMapViewVRAMPointer]     ; VRAM row +1 (=+$40), wrap high byte into vBGMap0
	add $40
	ld [wMapViewVRAMPointer], a
	jr nc, .vramDone
	ld a, [wMapViewVRAMPointer + 1]
	inc a
	and $03
	or $98
	ld [wMapViewVRAMPointer + 1], a
.vramDone
	ld hl, wYBlockCoord
	inc [hl]
	ld a, [hl]
	cp 2
	jr nz, .go
	ld [hl], 0                     ; crossed a block south -> advance block pointer
	ld a, [wCurMapWidth]
	add MAP_BORDER * 2
	ld b, a
	ld hl, wCurrentTileBlockMapViewPointer
	ld a, [hl]
	add b
	ld [hl], a
	jr nc, .go
	inc hl
	inc [hl]
.go
	call LoadCurrentMapView
	call ScheduleSouthRowRedraw
	ld c, SLOP_SCROLL_FRAMES
.frames
	ldh a, [hSCY]
	add SLOP_SCROLL_PX
	ldh [hSCY], a
	call DelayFrame
	dec c
	jr nz, .frames
	pop bc
	dec c
	jr nz, .loop
	ret

SlopScrollUp:
	xor a
	ldh [hAutoBGTransferEnabled], a
.loop
	push bc
	ld a, [wYCoord]
	dec a
	ld [wYCoord], a
	ld a, [wMapViewVRAMPointer]     ; VRAM row -1 (=-$40)
	sub $40
	ld [wMapViewVRAMPointer], a
	jr nc, .vramDone
	ld a, [wMapViewVRAMPointer + 1]
	dec a
	and $03
	or $98
	ld [wMapViewVRAMPointer + 1], a
.vramDone
	ld hl, wYBlockCoord
	dec [hl]
	ld a, [hl]
	inc a
	jr nz, .go                     ; was 0 -> now $ff: crossed a block north
	ld [hl], 1
	ld a, [wCurMapWidth]
	add MAP_BORDER * 2
	ld b, a
	ld hl, wCurrentTileBlockMapViewPointer
	ld a, [hl]
	sub b
	ld [hl], a
	jr nc, .go
	inc hl
	dec [hl]
.go
	call LoadCurrentMapView
	call ScheduleNorthRowRedraw
	ld c, SLOP_SCROLL_FRAMES
.frames
	ldh a, [hSCY]
	sub SLOP_SCROLL_PX
	ldh [hSCY], a
	call DelayFrame
	dec c
	jr nz, .frames
	pop bc
	dec c
	jr nz, .loop
	ret

; ===========================================================================
; SCREENSAVER direction logic: a persistent-direction wander. Keep drifting one
; way; only turn when the current direction gets blocked, or occasionally at a
; junction. Avoids immediate U-turns so it glides smoothly (DFS-ish) across the
; overworld instead of jittering. The chosen direction is fed into the normal
; EXPLORE dispatch as if the d-pad were pressed, so it inherits smooth scrolling
; and smooth map-edge crossing for free.
; ===========================================================================
SlopDirBits:
	db PAD_UP, PAD_DOWN, PAD_LEFT, PAD_RIGHT

; -> a = bitmask of directions the screensaver can actually make progress in.
; A direction is open if there are >= 2 coords of room that way (enough for one
; even scroll step) OR a map connection to cross into. The >= 2 test matters
; because moves round down to even coords, and a map's far edge (maxY/maxX =
; wCurrentMap*2 - 1) is always odd -- so "coord != max" would never register the
; edge as blocked and the wander would freeze one step short of it.
SlopValidDirMask:
	ld c, 0
; up: room upward = wYCoord
	ld a, [wYCoord]
	cp 2
	jr nc, .up
	ld a, [wNorthConnectedMap]
	inc a
	jr z, .noUp
.up
	set B_PAD_UP, c
.noUp
; down: room downward = maxY - wYCoord
	ld a, [wCurrentMapHeight2]
	dec a
	ld b, a                        ; b = maxY
	ld a, [wYCoord]
	ld d, a
	ld a, b
	sub d                          ; a = maxY - wYCoord
	cp 2
	jr nc, .down
	ld a, [wSouthConnectedMap]
	inc a
	jr z, .noDown
.down
	set B_PAD_DOWN, c
.noDown
; left: room leftward = wXCoord
	ld a, [wXCoord]
	cp 2
	jr nc, .left
	ld a, [wWestConnectedMap]
	inc a
	jr z, .noLeft
.left
	set B_PAD_LEFT, c
.noLeft
; right: room rightward = maxX - wXCoord
	ld a, [wCurrentMapWidth2]
	dec a
	ld b, a                        ; b = maxX
	ld a, [wXCoord]
	ld d, a
	ld a, b
	sub d                          ; a = maxX - wXCoord
	cp 2
	jr nc, .right
	ld a, [wEastConnectedMap]
	inc a
	jr z, .noRight
.right
	set B_PAD_RIGHT, c
.noRight
	ld a, c
	ret

; a = nonzero mask -> a = one set bit of it, chosen pseudo-randomly
SlopRandomBit:
	ld b, a                        ; b = mask
	ldh a, [hRandomSub]
	and $03
	ld c, a                        ; random start index 0..3
	ld d, 4
.loop
	ld a, c
	and $03
	ld hl, SlopDirBits
	add l
	ld l, a
	ld a, 0
	adc h
	ld h, a
	ld a, [hl]
	and b
	ret nz                         ; this direction is in the mask -> use it
	inc c
	dec d
	jr nz, .loop
	ld a, b                        ; fallback (a nonzero mask always hits above)
	ret

; a = dir bit -> a = the opposite direction bit
SlopRevDir:
	cp PAD_UP
	jr nz, .notUp
	ld a, PAD_DOWN
	ret
.notUp
	cp PAD_DOWN
	jr nz, .notDown
	ld a, PAD_UP
	ret
.notDown
	cp PAD_LEFT
	jr nz, .notLeft
	ld a, PAD_RIGHT
	ret
.notLeft
	ld a, PAD_LEFT                  ; was RIGHT
	ret

; pick the next screensaver direction; store in wSlopSaverDir, return it in d.
; Strategy: keep heading the same way until that direction gets blocked, then
; turn onto a random still-open direction, preferring not to double back.
SlopSaverPickDir:
	call SlopValidDirMask
	ld e, a                        ; e = valid-direction mask
	and a
	jr z, .keepCurrent             ; nothing valid (shouldn't happen)
	ld a, [wSlopSaverDir]
	ld b, a                        ; b = current direction
	and e
	jr nz, .keepCurrent            ; still valid -> carry straight on
; blocked at a bound: turn onto another open direction (avoid a U-turn if we can)
	ld a, b
	call SlopRevDir
	cpl
	and e                          ; valid directions excluding a U-turn
	jr nz, .turn
	ld a, e                        ; only a U-turn is available
.turn
	call SlopRandomBit
	jr .store
.keepCurrent
	ld a, [wSlopSaverDir]
.store
	ld [wSlopSaverDir], a
	ld d, a
	ret

; (dy, dx) coord offsets scanned outward from the centre for a walkable tile
SlopWarpOffsets:
	db  0, 0
	db -1, 0,  1, 0,  0,-1,  0, 1
	db -1,-1, -1, 1,  1,-1,  1, 1
	db -2, 0,  2, 0,  0,-2,  0, 2
	db -2,-1, -2, 1,  2,-1,  2, 1, -1,-2, -1, 2,  1,-2,  1, 2
	db -3, 0,  3, 0,  0,-3,  0, 3
	db $80

; ===========================================================================
; ITEMS submenu: give 99 of the chosen item (stacks via GiveItem)
; ===========================================================================
DEF GIVE_COUNT EQU 12

GiveItemSubmenu:
	call ClearScreen
	hlcoord 6, 0
	ld de, GiveTitleText
	call PlaceString
	hlcoord 1, 1
	lb bc, GIVE_COUNT + 2, 16
	call TextBoxBorder
	hlcoord 3, 2
	ld de, GiveItemText
	call PlaceString
	call SlopMenuShow
	xor a
	ld [wCurrentMenuItem], a
	ld [wLastMenuItem], a
	ld [wMenuWatchMovingOutOfBounds], a
	ld [wMenuJoypadPollCount], a
	ld a, 2
	ld [wTopMenuItemX], a
	ld [wTopMenuItemY], a
	ld a, PAD_A | PAD_B
	ld [wMenuWatchedKeys], a
	ld a, GIVE_COUNT - 1
	ld [wMaxMenuItem], a
	call HandleMenuInput
	bit B_PAD_B, a
	jr nz, .back
	ld a, [wCurrentMenuItem]
	ld e, a
	ld d, 0
	ld hl, GiveItemTable
	add hl, de
	ld b, [hl]
	ld c, 99
	call GiveItem
.back
	and a
	ret

GiveTitleText:
	db "GIVE x99@"
GiveItemText:
	db   "MASTER BALL"
	next "ULTRA BALL"
	next "RARE CANDY"
	next "FULL RESTORE"
	next "MAX REVIVE"
	next "MAX ELIXER"
	next "PP UP"
	next "MOON STONE"
	next "FIRE STONE"
	next "THUNDERSTONE"
	next "WATER STONE"
	next "LEAF STONE@"
GiveItemTable:
	db MASTER_BALL, ULTRA_BALL, RARE_CANDY, FULL_RESTORE
	db MAX_REVIVE, MAX_ELIXER, PP_UP, MOON_STONE
	db FIRE_STONE, THUNDER_STONE, WATER_STONE, LEAF_STONE


; ===========================================================================
; Direct-action cheats (return carry clear -> back to the SL0P menu)
; ===========================================================================

; HEAL: full HP + clear status for the whole party.
SlopHealParty:
	ld a, [wPartyCount]
	and a
	jr z, .done
	ld c, a
	ld hl, wPartyMon1
.loop
	push hl
	ld de, $04
	add hl, de
	xor a
	ld [hl], a                 ; status = 0
	pop hl
	push hl
	ld de, $22
	add hl, de                 ; -> maxHP
	ld a, [hli]
	ld b, a
	ld e, [hl]
	ld d, b
	pop hl
	push hl
	inc hl                     ; -> curHP
	ld a, d
	ld [hli], a
	ld a, e
	ld [hl], a
	pop hl
	ld de, $2C
	add hl, de
	dec c
	jr nz, .loop
.done
	and a
	ret

; TEACH: give every party member an OP coverage moveset + PP
; (Hyper Beam / Earthquake / Ice Beam / Thunderbolt)
SlopTeach:
	ld a, [wPartyCount]
	and a
	jr z, .done
	ld c, a
	ld hl, wPartyMon1
.loop
	push bc
	push hl
	ld de, $08                 ; -> moves
	add hl, de
	ld a, $3F                  ; Hyper Beam
	ld [hli], a
	ld a, $59                  ; Earthquake
	ld [hli], a
	ld a, $3A                  ; Ice Beam
	ld [hli], a
	ld a, $55                  ; Thunderbolt
	ld [hli], a
	pop hl
	push hl
	ld de, $1D                 ; -> PP
	add hl, de
	ld b, 4
.pp
	ld a, $1F                  ; 31 PP each
	ld [hli], a
	dec b
	jr nz, .pp
	pop hl
	ld de, $2C
	add hl, de
	pop bc
	dec c
	jr nz, .loop
.done
	and a
	ret

; HMS: give all five HMs (Cut/Fly/Surf/Strength/Flash)
SlopGiveHMs:
	ld b, HM_CUT
.loop
	push bc
	ld c, 1
	call GiveItem
	pop bc
	inc b
	ld a, b
	cp HM_FLASH + 1
	jr nz, .loop
	and a
	ret

; MONEY: 999999 (BCD)
SlopMaxCash:
	ld a, $99
	ld [wPlayerMoney], a
	ld [wPlayerMoney + 1], a
	ld [wPlayerMoney + 2], a
	and a
	ret

; COINS: 9999 game-corner coins (BCD)
SlopMaxCoins:
	ld a, $99
	ld [wPlayerCoins], a
	ld [wPlayerCoins + 1], a
	and a
	ret

; NOWILD toggle: no wild encounters (hooked in TryDoWildEncounter)
SlopToggleNoWild:
	ld a, [wUnusedFlag]
	xor $01
	ld [wUnusedFlag], a
	and a
	ret

; REPEL: refill repel steps
SlopRepel:
	ld a, 255
	ld [wRepelRemainingSteps], a
	and a
	ret

; BADGES: all 8
SlopAllBadges:
	ld a, $ff
	ld [wObtainedBadges], a
	and a
	ret

; DEX: mark every Pokemon owned + seen (19-byte flag arrays; last byte = 7 bits)
SlopDex:
	ld hl, wPokedexOwned
	call .fill
	ld hl, wPokedexSeen
	call .fill
	and a
	ret
.fill
	ld c, 18
.floop
	ld a, $ff
	ld [hli], a
	dec c
	jr nz, .floop
	ld [hl], $7f
	ret

; MAXTEAM: level 100, perfect DVs, maxed EVs, 999 stats + HP, exp = lvl 100
SlopMaxTeam:
	ld a, [wPartyCount]
	and a
	jr z, .done
	ld c, a
	ld hl, wPartyMon1
.loop
	push bc
	push hl
	; current HP (+$01) = 999
	inc hl
	ld a, $03
	ld [hli], a
	ld a, $E7
	ld [hl], a
	pop hl
	push hl
	; exp (+$0E) = 1,000,000
	ld de, $0E
	add hl, de
	ld a, $0F
	ld [hli], a
	ld a, $42
	ld [hli], a
	ld a, $40
	ld [hli], a
	; EVs (+$11..$1A) = FFFF x5, then DVs (+$1B..$1C) = FFFF -> 12 bytes of $FF
	ld b, 12
.ffloop
	ld a, $ff
	ld [hli], a
	dec b
	jr nz, .ffloop
	pop hl
	push hl
	; level (+$21) = 100
	ld de, $21
	add hl, de
	ld a, 100
	ld [hli], a
	; maxHP + 4 stats (+$22..$2B) = 999 x5
	ld b, 5
.statloop
	ld a, $03
	ld [hli], a
	ld a, $E7
	ld [hli], a
	dec b
	jr nz, .statloop
	pop hl
	ld de, $2C
	add hl, de
	pop bc
	dec c
	jr nz, .loop
.done
	and a
	ret