-
Notifications
You must be signed in to change notification settings - Fork 1k
/
overworld.asm
2453 lines (2353 loc) · 55.2 KB
/
overworld.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
HandleMidJump::
; Handle the player jumping down
; a ledge in the overworld.
farjp _HandleMidJump
EnterMap::
; Load a new map.
ld a, A_BUTTON | B_BUTTON | SELECT | START | D_RIGHT | D_LEFT | D_UP | D_DOWN
ld [wJoyIgnore], a
call LoadMapData
farcall ClearVariablesOnEnterMap
ld hl, wStatusFlags2
bit BIT_WILD_ENCOUNTER_COOLDOWN, [hl]
jr z, .skipGivingThreeStepsOfNoRandomBattles
ld a, 3 ; minimum number of steps between battles
ld [wNumberOfNoRandomBattleStepsLeft], a
.skipGivingThreeStepsOfNoRandomBattles
ld hl, wStatusFlags4
bit BIT_BATTLE_OVER_OR_BLACKOUT, [hl]
res BIT_BATTLE_OVER_OR_BLACKOUT, [hl]
call z, ResetUsingStrengthOutOfBattleBit
call nz, MapEntryAfterBattle
ld hl, wStatusFlags6
ld a, [hl]
and (1 << BIT_FLY_WARP) | (1 << BIT_DUNGEON_WARP)
jr z, .didNotEnterUsingFlyWarpOrDungeonWarp
res BIT_FLY_WARP, [hl]
farcall EnterMapAnim
call UpdateSprites
.didNotEnterUsingFlyWarpOrDungeonWarp
farcall CheckForceBikeOrSurf ; handle currents in SF islands and forced bike riding in cycling road
ld hl, wStatusFlags3
res BIT_NO_NPC_FACE_PLAYER, [hl]
call UpdateSprites
ld hl, wCurrentMapScriptFlags
set BIT_CUR_MAP_LOADED_1, [hl]
set BIT_CUR_MAP_LOADED_2, [hl]
xor a
ld [wJoyIgnore], a
OverworldLoop::
call DelayFrame
OverworldLoopLessDelay::
call DelayFrame
call LoadGBPal
ld a, [wMovementFlags]
bit BIT_LEDGE_OR_FISHING, a
call nz, HandleMidJump
ld a, [wWalkCounter]
and a
jp nz, .moveAhead ; if the player sprite has not yet completed the walking animation
call JoypadOverworld ; get joypad state (which is possibly simulated)
farcall SafariZoneCheck
ld a, [wSafariZoneGameOver]
and a
jp nz, WarpFound2
ld hl, wStatusFlags3
bit BIT_WARP_FROM_CUR_SCRIPT, [hl]
res BIT_WARP_FROM_CUR_SCRIPT, [hl]
jp nz, WarpFound2
ld a, [wStatusFlags6]
and (1 << BIT_FLY_WARP) | (1 << BIT_DUNGEON_WARP)
jp nz, HandleFlyWarpOrDungeonWarp
ld a, [wCurOpponent]
and a
jp nz, .newBattle
ld a, [wStatusFlags5]
bit BIT_SCRIPTED_MOVEMENT_STATE, a
jr z, .notSimulating
ldh a, [hJoyHeld]
jr .checkIfStartIsPressed
.notSimulating
ldh a, [hJoyPressed]
.checkIfStartIsPressed
bit BIT_START, a
jr z, .startButtonNotPressed
; if START is pressed
xor a ; TEXT_START_MENU
ldh [hTextID], a
jp .displayDialogue
.startButtonNotPressed
bit BIT_A_BUTTON, a
jp z, .checkIfDownButtonIsPressed
; if A is pressed
ld a, [wStatusFlags5]
bit BIT_UNKNOWN_5_2, a
jp nz, .noDirectionButtonsPressed
call IsPlayerCharacterBeingControlledByGame
jr nz, .checkForOpponent
call CheckForHiddenObjectOrBookshelfOrCardKeyDoor
ldh a, [hItemAlreadyFound]
and a
jp z, OverworldLoop ; jump if a hidden object or bookshelf was found, but not if a card key door was found
call IsSpriteOrSignInFrontOfPlayer
ldh a, [hTextID]
and a
jp z, OverworldLoop
.displayDialogue
predef GetTileAndCoordsInFrontOfPlayer
call UpdateSprites
ld a, [wMiscFlags]
bit BIT_TURNING, a
jr nz, .checkForOpponent
bit BIT_SEEN_BY_TRAINER, a
jr nz, .checkForOpponent
lda_coord 8, 9
ld [wTilePlayerStandingOn], a ; checked when using Surf for forbidden tile pairs
call DisplayTextID ; display either the start menu or the NPC/sign text
ld a, [wEnteringCableClub]
and a
jr z, .checkForOpponent
dec a
ld a, 0
ld [wEnteringCableClub], a
jr z, .changeMap
; XXX can this code be reached?
predef LoadSAV
ld a, [wCurMap]
ld [wDestinationMap], a
call PrepareForSpecialWarp
ld a, [wCurMap]
call SwitchToMapRomBank ; switch to the ROM bank of the current map
ld hl, wCurMapTileset
set BIT_NO_PREVIOUS_MAP, [hl]
.changeMap
jp EnterMap
.checkForOpponent
ld a, [wCurOpponent]
and a
jp nz, .newBattle
jp OverworldLoop
.noDirectionButtonsPressed
ld hl, wMiscFlags
res BIT_TURNING, [hl]
call UpdateSprites
ld a, 1
ld [wCheckFor180DegreeTurn], a
ld a, [wPlayerMovingDirection] ; the direction that was pressed last time
and a
jp z, OverworldLoop
; if a direction was pressed last time
ld [wPlayerLastStopDirection], a ; save the last direction
xor a
ld [wPlayerMovingDirection], a ; zero the direction
jp OverworldLoop
.checkIfDownButtonIsPressed
ldh a, [hJoyHeld] ; current joypad state
bit BIT_D_DOWN, a
jr z, .checkIfUpButtonIsPressed
ld a, 1
ld [wSpritePlayerStateData1YStepVector], a
ld a, PLAYER_DIR_DOWN
jr .handleDirectionButtonPress
.checkIfUpButtonIsPressed
bit BIT_D_UP, a
jr z, .checkIfLeftButtonIsPressed
ld a, -1
ld [wSpritePlayerStateData1YStepVector], a
ld a, PLAYER_DIR_UP
jr .handleDirectionButtonPress
.checkIfLeftButtonIsPressed
bit BIT_D_LEFT, a
jr z, .checkIfRightButtonIsPressed
ld a, -1
ld [wSpritePlayerStateData1XStepVector], a
ld a, PLAYER_DIR_LEFT
jr .handleDirectionButtonPress
.checkIfRightButtonIsPressed
bit BIT_D_RIGHT, a
jr z, .noDirectionButtonsPressed
ld a, 1
ld [wSpritePlayerStateData1XStepVector], a
.handleDirectionButtonPress
ld [wPlayerDirection], a ; new direction
ld a, [wStatusFlags5]
bit BIT_SCRIPTED_MOVEMENT_STATE, a
jr nz, .noDirectionChange ; ignore direction changes if we are
ld a, [wCheckFor180DegreeTurn]
and a
jr z, .noDirectionChange
ld a, [wPlayerDirection] ; new direction
ld b, a
ld a, [wPlayerLastStopDirection] ; old direction
cp b
jr z, .noDirectionChange
; Check whether the player did a 180-degree turn.
; It appears that this code was supposed to show the player rotate by having
; the player's sprite face an intermediate direction before facing the opposite
; direction (instead of doing an instantaneous about-face), but the intermediate
; direction is only set for a short period of time. It is unlikely for it to
; ever be visible because DelayFrame is called at the start of OverworldLoop and
; normally not enough cycles would be executed between then and the time the
; direction is set for V-blank to occur while the direction is still set.
swap a ; put old direction in upper half
or b ; put new direction in lower half
cp (PLAYER_DIR_DOWN << 4) | PLAYER_DIR_UP ; change dir from down to up
jr nz, .notDownToUp
ld a, PLAYER_DIR_LEFT
ld [wPlayerMovingDirection], a
jr .holdIntermediateDirectionLoop
.notDownToUp
cp (PLAYER_DIR_UP << 4) | PLAYER_DIR_DOWN ; change dir from up to down
jr nz, .notUpToDown
ld a, PLAYER_DIR_RIGHT
ld [wPlayerMovingDirection], a
jr .holdIntermediateDirectionLoop
.notUpToDown
cp (PLAYER_DIR_RIGHT << 4) | PLAYER_DIR_LEFT ; change dir from right to left
jr nz, .notRightToLeft
ld a, PLAYER_DIR_DOWN
ld [wPlayerMovingDirection], a
jr .holdIntermediateDirectionLoop
.notRightToLeft
cp (PLAYER_DIR_LEFT << 4) | PLAYER_DIR_RIGHT ; change dir from left to right
jr nz, .holdIntermediateDirectionLoop
ld a, PLAYER_DIR_UP
ld [wPlayerMovingDirection], a
.holdIntermediateDirectionLoop
ld hl, wMiscFlags
set BIT_TURNING, [hl]
ld hl, wCheckFor180DegreeTurn
dec [hl]
jr nz, .holdIntermediateDirectionLoop
ld a, [wPlayerDirection]
ld [wPlayerMovingDirection], a
call NewBattle
jp c, .battleOccurred
jp OverworldLoop
.noDirectionChange
ld a, [wPlayerDirection] ; current direction
ld [wPlayerMovingDirection], a ; save direction
call UpdateSprites
ld a, [wWalkBikeSurfState]
cp $02 ; surfing
jr z, .surfing
; not surfing
call CollisionCheckOnLand
jr nc, .noCollision
; collision occurred
push hl
ld hl, wMovementFlags
bit BIT_STANDING_ON_WARP, [hl]
pop hl
jp z, OverworldLoop
; collision occurred while standing on a warp
push hl
call ExtraWarpCheck ; sets carry if there is a potential to warp
pop hl
jp c, CheckWarpsCollision
jp OverworldLoop
.surfing
call CollisionCheckOnWater
jp c, OverworldLoop
.noCollision
ld a, $08
ld [wWalkCounter], a
jr .moveAhead2
.moveAhead
ld a, [wMovementFlags]
bit BIT_SPINNING, a
jr z, .noSpinning
farcall LoadSpinnerArrowTiles
.noSpinning
call UpdateSprites
.moveAhead2
ld hl, wMiscFlags
res BIT_TURNING, [hl]
ld a, [wWalkBikeSurfState]
dec a ; riding a bike?
jr nz, .normalPlayerSpriteAdvancement
ld a, [wMovementFlags]
bit BIT_LEDGE_OR_FISHING, a
jr nz, .normalPlayerSpriteAdvancement
call DoBikeSpeedup
.normalPlayerSpriteAdvancement
call AdvancePlayerSprite
ld a, [wWalkCounter]
and a
jp nz, CheckMapConnections ; it seems like this check will never succeed (the other place where CheckMapConnections is run works)
; walking animation finished
ld a, [wStatusFlags5]
bit BIT_SCRIPTED_MOVEMENT_STATE, a
jr nz, .doneStepCounting ; if button presses are being simulated, don't count steps
; step counting
ld hl, wStepCounter
dec [hl]
ld a, [wStatusFlags2]
bit BIT_WILD_ENCOUNTER_COOLDOWN, a
jr z, .doneStepCounting
ld hl, wNumberOfNoRandomBattleStepsLeft
dec [hl]
jr nz, .doneStepCounting
ld hl, wStatusFlags2
res BIT_WILD_ENCOUNTER_COOLDOWN, [hl]
.doneStepCounting
CheckEvent EVENT_IN_SAFARI_ZONE
jr z, .notSafariZone
farcall SafariZoneCheckSteps
ld a, [wSafariZoneGameOver]
and a
jp nz, WarpFound2
.notSafariZone
ld a, [wIsInBattle]
and a
jp nz, CheckWarpsNoCollision
predef ApplyOutOfBattlePoisonDamage ; also increment daycare mon exp
ld a, [wOutOfBattleBlackout]
and a
jp nz, HandleBlackOut ; if all pokemon fainted
.newBattle
call NewBattle
ld hl, wMovementFlags
res BIT_STANDING_ON_WARP, [hl]
jp nc, CheckWarpsNoCollision ; check for warps if there was no battle
.battleOccurred
ld hl, wStatusFlags3
res BIT_TALKED_TO_TRAINER, [hl]
ld hl, wStatusFlags7
res BIT_TRAINER_BATTLE, [hl]
ld hl, wCurrentMapScriptFlags
set BIT_CUR_MAP_LOADED_1, [hl]
set BIT_CUR_MAP_LOADED_2, [hl]
xor a
ldh [hJoyHeld], a
ld a, [wCurMap]
cp CINNABAR_GYM
jr nz, .notCinnabarGym
SetEvent EVENT_2A7
.notCinnabarGym
ld hl, wStatusFlags4
set BIT_BATTLE_OVER_OR_BLACKOUT, [hl]
ld a, [wCurMap]
cp OAKS_LAB
jp z, .noFaintCheck ; no blacking out if the player lost to the rival in Oak's lab
callfar AnyPartyAlive
ld a, d
and a
jr z, .allPokemonFainted
.noFaintCheck
ld c, 10
call DelayFrames
jp EnterMap
.allPokemonFainted
ld a, $ff
ld [wIsInBattle], a
call RunMapScript
jp HandleBlackOut
; function to determine if there will be a battle and execute it (either a trainer battle or wild battle)
; sets carry if a battle occurred and unsets carry if not
NewBattle::
ld a, [wStatusFlags3]
bit BIT_ON_DUNGEON_WARP, a
jr nz, .noBattle
call IsPlayerCharacterBeingControlledByGame
jr nz, .noBattle ; no battle if the player character is under the game's control
ld a, [wStatusFlags4]
bit BIT_NO_BATTLES, a
jr nz, .noBattle
farjp InitBattle
.noBattle
and a
ret
; function to make bikes twice as fast as walking
DoBikeSpeedup::
ld a, [wNPCMovementScriptPointerTableNum]
and a
ret nz
ld a, [wCurMap]
cp ROUTE_17 ; Cycling Road
jr nz, .goFaster
ldh a, [hJoyHeld]
and D_UP | D_LEFT | D_RIGHT
ret nz
.goFaster
jp AdvancePlayerSprite
; check if the player has stepped onto a warp after having not collided
CheckWarpsNoCollision::
ld a, [wNumberOfWarps]
and a
jp z, CheckMapConnections
ld a, [wNumberOfWarps]
ld b, 0
ld c, a
ld a, [wYCoord]
ld d, a
ld a, [wXCoord]
ld e, a
ld hl, wWarpEntries
CheckWarpsNoCollisionLoop::
ld a, [hli] ; check if the warp's Y position matches
cp d
jr nz, CheckWarpsNoCollisionRetry1
ld a, [hli] ; check if the warp's X position matches
cp e
jr nz, CheckWarpsNoCollisionRetry2
; if a match was found
push hl
push bc
ld hl, wMovementFlags
set BIT_STANDING_ON_WARP, [hl]
farcall IsPlayerStandingOnDoorTileOrWarpTile
pop bc
pop hl
jr c, WarpFound1 ; jump if standing on door or warp
push hl
push bc
call ExtraWarpCheck
pop bc
pop hl
jr nc, CheckWarpsNoCollisionRetry2
; if the extra check passed
ld a, [wStatusFlags7]
bit BIT_FORCED_WARP, a
jr nz, WarpFound1
push de
push bc
call Joypad
pop bc
pop de
ldh a, [hJoyHeld]
and D_DOWN | D_UP | D_LEFT | D_RIGHT
jr z, CheckWarpsNoCollisionRetry2 ; if directional buttons aren't being pressed, do not pass through the warp
jr WarpFound1
; check if the player has stepped onto a warp after having collided
CheckWarpsCollision::
ld a, [wNumberOfWarps]
ld c, a
ld hl, wWarpEntries
.loop
ld a, [hli] ; Y coordinate of warp
ld b, a
ld a, [wYCoord]
cp b
jr nz, .retry1
ld a, [hli] ; X coordinate of warp
ld b, a
ld a, [wXCoord]
cp b
jr nz, .retry2
ld a, [hli]
ld [wDestinationWarpID], a
ld a, [hl]
ldh [hWarpDestinationMap], a
jr WarpFound2
.retry1
inc hl
.retry2
inc hl
inc hl
dec c
jr nz, .loop
jp OverworldLoop
CheckWarpsNoCollisionRetry1::
inc hl
CheckWarpsNoCollisionRetry2::
inc hl
inc hl
jp ContinueCheckWarpsNoCollisionLoop
WarpFound1::
ld a, [hli]
ld [wDestinationWarpID], a
ld a, [hli]
ldh [hWarpDestinationMap], a
WarpFound2::
ld a, [wNumberOfWarps]
sub c
ld [wWarpedFromWhichWarp], a ; save ID of used warp
ld a, [wCurMap]
ld [wWarpedFromWhichMap], a
call CheckIfInOutsideMap
jr nz, .indoorMaps
; this is for handling "outside" maps that can't have the 0xFF destination map
ld a, [wCurMap]
ld [wLastMap], a
ld a, [wCurMapWidth]
ld [wUnusedLastMapWidth], a
ldh a, [hWarpDestinationMap]
ld [wCurMap], a
cp ROCK_TUNNEL_1F
jr nz, .notRockTunnel
ld a, $06
ld [wMapPalOffset], a
call GBFadeOutToBlack
.notRockTunnel
call PlayMapChangeSound
jr .done
; for maps that can have the 0xFF destination map, which means to return to the outside map
; not all these maps are necessarily indoors, though
.indoorMaps
ldh a, [hWarpDestinationMap] ; destination map
cp LAST_MAP
jr z, .goBackOutside
; if not going back to the previous map
ld [wCurMap], a
farcall IsPlayerStandingOnWarpPadOrHole
ld a, [wStandingOnWarpPadOrHole]
dec a ; is the player on a warp pad?
jr nz, .notWarpPad
; if the player is on a warp pad
ld hl, wStatusFlags6
set BIT_FLY_WARP, [hl]
call LeaveMapAnim
jr .skipMapChangeSound
.notWarpPad
call PlayMapChangeSound
.skipMapChangeSound
ld hl, wMovementFlags
res BIT_STANDING_ON_DOOR, [hl]
res BIT_EXITING_DOOR, [hl]
jr .done
.goBackOutside
ld a, [wLastMap]
ld [wCurMap], a
call PlayMapChangeSound
xor a
ld [wMapPalOffset], a
.done
ld hl, wMovementFlags
set BIT_STANDING_ON_DOOR, [hl] ; have the player's sprite step out from the door (if there is one)
call IgnoreInputForHalfSecond
jp EnterMap
ContinueCheckWarpsNoCollisionLoop::
inc b ; increment warp number
dec c ; decrement number of warps
jp nz, CheckWarpsNoCollisionLoop
; if no matching warp was found
CheckMapConnections::
.checkWestMap
ld a, [wXCoord]
cp $ff
jr nz, .checkEastMap
ld a, [wWestConnectedMap]
ld [wCurMap], a
ld a, [wWestConnectedMapXAlignment] ; new X coordinate upon entering west map
ld [wXCoord], a
ld a, [wYCoord]
ld c, a
ld a, [wWestConnectedMapYAlignment] ; Y adjustment upon entering west map
add c
ld c, a
ld [wYCoord], a
ld a, [wWestConnectedMapViewPointer] ; pointer to upper left corner of map without adjustment for Y position
ld l, a
ld a, [wWestConnectedMapViewPointer + 1]
ld h, a
srl c
jr z, .savePointer1
.pointerAdjustmentLoop1
ld a, [wWestConnectedMapWidth] ; width of connected map
add MAP_BORDER * 2
ld e, a
ld d, 0
ld b, 0
add hl, de
dec c
jr nz, .pointerAdjustmentLoop1
.savePointer1
ld a, l
ld [wCurrentTileBlockMapViewPointer], a ; pointer to upper left corner of current tile block map section
ld a, h
ld [wCurrentTileBlockMapViewPointer + 1], a
jp .loadNewMap
.checkEastMap
ld b, a
ld a, [wCurrentMapWidth2] ; map width
cp b
jr nz, .checkNorthMap
ld a, [wEastConnectedMap]
ld [wCurMap], a
ld a, [wEastConnectedMapXAlignment] ; new X coordinate upon entering east map
ld [wXCoord], a
ld a, [wYCoord]
ld c, a
ld a, [wEastConnectedMapYAlignment] ; Y adjustment upon entering east map
add c
ld c, a
ld [wYCoord], a
ld a, [wEastConnectedMapViewPointer] ; pointer to upper left corner of map without adjustment for Y position
ld l, a
ld a, [wEastConnectedMapViewPointer + 1]
ld h, a
srl c
jr z, .savePointer2
.pointerAdjustmentLoop2
ld a, [wEastConnectedMapWidth]
add MAP_BORDER * 2
ld e, a
ld d, 0
ld b, 0
add hl, de
dec c
jr nz, .pointerAdjustmentLoop2
.savePointer2
ld a, l
ld [wCurrentTileBlockMapViewPointer], a ; pointer to upper left corner of current tile block map section
ld a, h
ld [wCurrentTileBlockMapViewPointer + 1], a
jp .loadNewMap
.checkNorthMap
ld a, [wYCoord]
cp $ff
jr nz, .checkSouthMap
ld a, [wNorthConnectedMap]
ld [wCurMap], a
ld a, [wNorthConnectedMapYAlignment] ; new Y coordinate upon entering north map
ld [wYCoord], a
ld a, [wXCoord]
ld c, a
ld a, [wNorthConnectedMapXAlignment] ; X adjustment upon entering north map
add c
ld c, a
ld [wXCoord], a
ld a, [wNorthConnectedMapViewPointer] ; pointer to upper left corner of map without adjustment for X position
ld l, a
ld a, [wNorthConnectedMapViewPointer + 1]
ld h, a
ld b, 0
srl c
add hl, bc
ld a, l
ld [wCurrentTileBlockMapViewPointer], a ; pointer to upper left corner of current tile block map section
ld a, h
ld [wCurrentTileBlockMapViewPointer + 1], a
jp .loadNewMap
.checkSouthMap
ld b, a
ld a, [wCurrentMapHeight2]
cp b
jr nz, .didNotEnterConnectedMap
ld a, [wSouthConnectedMap]
ld [wCurMap], a
ld a, [wSouthConnectedMapYAlignment] ; new Y coordinate upon entering south map
ld [wYCoord], a
ld a, [wXCoord]
ld c, a
ld a, [wSouthConnectedMapXAlignment] ; X adjustment upon entering south map
add c
ld c, a
ld [wXCoord], a
ld a, [wSouthConnectedMapViewPointer] ; pointer to upper left corner of map without adjustment for X position
ld l, a
ld a, [wSouthConnectedMapViewPointer + 1]
ld h, a
ld b, 0
srl c
add hl, bc
ld a, l
ld [wCurrentTileBlockMapViewPointer], a ; pointer to upper left corner of current tile block map section
ld a, h
ld [wCurrentTileBlockMapViewPointer + 1], a
.loadNewMap ; load the connected map that was entered
call LoadMapHeader
call PlayDefaultMusicFadeOutCurrent
ld b, SET_PAL_OVERWORLD
call RunPaletteCommand
; Since the sprite set shouldn't change, this will just update VRAM slots at
; x#SPRITESTATEDATA2_IMAGEBASEOFFSET without loading any tile patterns.
farcall InitMapSprites
call LoadTileBlockMap
jp OverworldLoopLessDelay
.didNotEnterConnectedMap
jp OverworldLoop
; function to play a sound when changing maps
PlayMapChangeSound::
lda_coord 8, 8 ; upper left tile of the 4x4 square the player's sprite is standing on
cp $0b ; door tile in tileset 0
jr nz, .didNotGoThroughDoor
ld a, SFX_GO_INSIDE
jr .playSound
.didNotGoThroughDoor
ld a, SFX_GO_OUTSIDE
.playSound
call PlaySound
ld a, [wMapPalOffset]
and a
ret nz
jp GBFadeOutToBlack
CheckIfInOutsideMap::
; If the player is in an outside map (a town or route), set the z flag
ld a, [wCurMapTileset]
and a ; most towns/routes have tileset 0 (OVERWORLD)
ret z
cp PLATEAU ; Route 23 / Indigo Plateau
ret
; this function is an extra check that sometimes has to pass in order to warp, beyond just standing on a warp
; the "sometimes" qualification is necessary because of CheckWarpsNoCollision's behavior
; depending on the map, either "function 1" or "function 2" is used for the check
; "function 1" passes when the player is at the edge of the map and is facing towards the outside of the map
; "function 2" passes when the the tile in front of the player is among a certain set
; sets carry if the check passes, otherwise clears carry
ExtraWarpCheck::
ld a, [wCurMap]
cp SS_ANNE_3F
jr z, .useFunction1
cp ROCKET_HIDEOUT_B1F
jr z, .useFunction2
cp ROCKET_HIDEOUT_B2F
jr z, .useFunction2
cp ROCKET_HIDEOUT_B4F
jr z, .useFunction2
cp ROCK_TUNNEL_1F
jr z, .useFunction2
ld a, [wCurMapTileset]
and a ; outside tileset (OVERWORLD)
jr z, .useFunction2
cp SHIP ; S.S. Anne tileset
jr z, .useFunction2
cp SHIP_PORT ; Vermilion Port tileset
jr z, .useFunction2
cp PLATEAU ; Indigo Plateau tileset
jr z, .useFunction2
.useFunction1
ld hl, IsPlayerFacingEdgeOfMap
jr .doBankswitch
.useFunction2
ld hl, IsWarpTileInFrontOfPlayer
.doBankswitch
ld b, BANK(IsWarpTileInFrontOfPlayer)
jp Bankswitch
MapEntryAfterBattle::
farcall IsPlayerStandingOnWarp ; for enabling warp testing after collisions
ld a, [wMapPalOffset]
and a
jp z, GBFadeInFromWhite
jp LoadGBPal
HandleBlackOut::
; For when all the player's pokemon faint.
; Does not print the "blacked out" message.
call GBFadeOutToBlack
ld a, $08
call StopMusic
ld hl, wStatusFlags4
res BIT_BATTLE_OVER_OR_BLACKOUT, [hl]
ld a, BANK(ResetStatusAndHalveMoneyOnBlackout) ; also BANK(PrepareForSpecialWarp) and BANK(SpecialEnterMap)
ldh [hLoadedROMBank], a
ld [MBC1RomBank], a
call ResetStatusAndHalveMoneyOnBlackout
call PrepareForSpecialWarp
call PlayDefaultMusicFadeOutCurrent
jp SpecialEnterMap
StopMusic::
ld [wAudioFadeOutControl], a
ld a, SFX_STOP_ALL_MUSIC
ld [wNewSoundID], a
call PlaySound
.wait
ld a, [wAudioFadeOutControl]
and a
jr nz, .wait
jp StopAllSounds
HandleFlyWarpOrDungeonWarp::
call UpdateSprites
call Delay3
xor a
ld [wBattleResult], a
ld [wWalkBikeSurfState], a
ld [wIsInBattle], a
ld [wMapPalOffset], a
ld hl, wStatusFlags6
set BIT_FLY_OR_DUNGEON_WARP, [hl]
res BIT_ALWAYS_ON_BIKE, [hl]
call LeaveMapAnim
ld a, BANK(PrepareForSpecialWarp)
ldh [hLoadedROMBank], a
ld [MBC1RomBank], a
call PrepareForSpecialWarp
jp SpecialEnterMap
LeaveMapAnim::
farjp _LeaveMapAnim
LoadPlayerSpriteGraphics::
; Load sprite graphics based on whether the player is standing, biking, or surfing.
; 0: standing
; 1: biking
; 2: surfing
ld a, [wWalkBikeSurfState]
dec a
jr z, .ridingBike
ldh a, [hTileAnimations]
and a
jr nz, .determineGraphics
jr .startWalking
.ridingBike
; If the bike can't be used,
; start walking instead.
call IsBikeRidingAllowed
jr c, .determineGraphics
.startWalking
xor a
ld [wWalkBikeSurfState], a
ld [wWalkBikeSurfStateCopy], a
jp LoadWalkingPlayerSpriteGraphics
.determineGraphics
ld a, [wWalkBikeSurfState]
and a
jp z, LoadWalkingPlayerSpriteGraphics
dec a
jp z, LoadBikePlayerSpriteGraphics
dec a
jp z, LoadSurfingPlayerSpriteGraphics
jp LoadWalkingPlayerSpriteGraphics
IsBikeRidingAllowed::
; The bike can be used on Route 23 and Indigo Plateau,
; or maps with tilesets in BikeRidingTilesets.
; Return carry if biking is allowed.
ld a, [wCurMap]
cp ROUTE_23
jr z, .allowed
cp INDIGO_PLATEAU
jr z, .allowed
ld a, [wCurMapTileset]
ld b, a
ld hl, BikeRidingTilesets
.loop
ld a, [hli]
cp b
jr z, .allowed
inc a
jr nz, .loop
and a
ret
.allowed
scf
ret
INCLUDE "data/tilesets/bike_riding_tilesets.asm"
; load the tile pattern data of the current tileset into VRAM
LoadTilesetTilePatternData::
ld a, [wTilesetGfxPtr]
ld l, a
ld a, [wTilesetGfxPtr + 1]
ld h, a
ld de, vTileset
ld bc, $600
ld a, [wTilesetBank]
jp FarCopyData2
; this loads the current map's complete tile map (which references blocks, not individual tiles) to wOverworldMap
; it can also load partial tile maps of connected maps into a border of length 3 around the current map
LoadTileBlockMap::
; fill wOverworldMap-wOverworldMapEnd with the background tile
ld hl, wOverworldMap
ld a, [wMapBackgroundTile]
ld d, a
ld bc, wOverworldMapEnd - wOverworldMap
.backgroundTileLoop
ld a, d
ld [hli], a
dec bc
ld a, c
or b
jr nz, .backgroundTileLoop
; load tile map of current map (made of tile block IDs)
; a 3-byte border at the edges of the map is kept so that there is space for map connections
ld hl, wOverworldMap
ld a, [wCurMapWidth]
ldh [hMapWidth], a
add MAP_BORDER * 2 ; east and west
ldh [hMapStride], a ; map width + border
ld b, 0
ld c, a
; make space for north border (next 3 lines)
add hl, bc
add hl, bc
add hl, bc
ld c, MAP_BORDER
add hl, bc ; this puts us past the (west) border
ld a, [wCurMapDataPtr] ; tile map pointer
ld e, a
ld a, [wCurMapDataPtr + 1]
ld d, a ; de = tile map pointer
ld a, [wCurMapHeight]
ld b, a
.rowLoop ; copy one row each iteration
push hl
ldh a, [hMapWidth] ; map width (without border)
ld c, a
.rowInnerLoop
ld a, [de]
inc de
ld [hli], a
dec c
jr nz, .rowInnerLoop
; add the map width plus the border to the base address of the current row to get the next row's address
pop hl
ldh a, [hMapStride] ; map width + border
add l
ld l, a
jr nc, .noCarry
inc h
.noCarry
dec b
jr nz, .rowLoop
.northConnection
ld a, [wNorthConnectedMap]
cp $ff
jr z, .southConnection
call SwitchToMapRomBank
ld a, [wNorthConnectionStripSrc]
ld l, a
ld a, [wNorthConnectionStripSrc + 1]
ld h, a
ld a, [wNorthConnectionStripDest]
ld e, a
ld a, [wNorthConnectionStripDest + 1]
ld d, a
ld a, [wNorthConnectionStripLength]
ldh [hNorthSouthConnectionStripWidth], a
ld a, [wNorthConnectedMapWidth]
ldh [hNorthSouthConnectedMapWidth], a
call LoadNorthSouthConnectionsTileMap
.southConnection
ld a, [wSouthConnectedMap]
cp $ff
jr z, .westConnection
call SwitchToMapRomBank
ld a, [wSouthConnectionStripSrc]
ld l, a
ld a, [wSouthConnectionStripSrc + 1]
ld h, a
ld a, [wSouthConnectionStripDest]
ld e, a
ld a, [wSouthConnectionStripDest + 1]
ld d, a
ld a, [wSouthConnectionStripLength]
ldh [hNorthSouthConnectionStripWidth], a
ld a, [wSouthConnectedMapWidth]
ldh [hNorthSouthConnectedMapWidth], a
call LoadNorthSouthConnectionsTileMap
.westConnection
ld a, [wWestConnectedMap]
cp $ff
jr z, .eastConnection
call SwitchToMapRomBank
ld a, [wWestConnectionStripSrc]
ld l, a
ld a, [wWestConnectionStripSrc + 1]
ld h, a
ld a, [wWestConnectionStripDest]
ld e, a
ld a, [wWestConnectionStripDest + 1]
ld d, a
ld a, [wWestConnectionStripLength]
ld b, a
ld a, [wWestConnectedMapWidth]
ldh [hEastWestConnectedMapWidth], a
call LoadEastWestConnectionsTileMap
.eastConnection
ld a, [wEastConnectedMap]
cp $ff
jr z, .done
call SwitchToMapRomBank
ld a, [wEastConnectionStripSrc]
ld l, a
ld a, [wEastConnectionStripSrc + 1]
ld h, a