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
|
OverworldDoFrameFunction:
ld a, [wOverworldNPCFlags]
bit HIDE_ALL_NPC_SPRITES, a
ret nz
ldh a, [hBankROM]
push af
ld a, BANK(SetScreenScrollWram)
call BankswitchROM
call SetScreenScrollWram
call Func_c554
ld a, BANK(HandleAllNPCMovement)
call BankswitchROM
call HandleAllNPCMovement
call HandleAllSpriteAnimations
ld a, BANK(DoLoadedFramesetSubgroupsFrame)
call BankswitchROM
call DoLoadedFramesetSubgroupsFrame
call UpdateRNGSources
pop af
call BankswitchROM
ret
; enable the play time counter and execute the game event at [wGameEvent].
; then return to the overworld, or restart the game (only after Credits).
ExecuteGameEvent:
ld a, 1
ld [wPlayTimeCounterEnable], a
ldh a, [hBankROM]
push af
.loop
call _ExecuteGameEvent
jr nc, .restart
farcall LoadMap
jr .loop
.restart
pop af
call BankswitchROM
ret
; execute a game event at [wGameEvent] from GameEventPointerTable
_ExecuteGameEvent:
ld a, [wGameEvent]
cp NUM_GAME_EVENTS
jr c, .got_game_event
ld a, GAME_EVENT_CHALLENGE_MACHINE
.got_game_event
ld hl, GameEventPointerTable
jp JumpToFunctionInTable
GameEventPointerTable:
dw GameEvent_Overworld
dw GameEvent_Duel
dw GameEvent_BattleCenter
dw GameEvent_GiftCenter
dw GameEvent_Credits
dw GameEvent_ContinueDuel
dw GameEvent_ChallengeMachine
dw GameEvent_Overworld
GameEvent_Overworld:
scf
ret
GameEvent_GiftCenter:
ldh a, [hBankROM]
push af
call PauseSong
ld a, MUSIC_CARD_POP
call PlaySong
ld a, GAME_EVENT_GIFT_CENTER
ld [wActiveGameEvent], a
ld a, [wd10e]
or $10
ld [wd10e], a
farcall Func_b177
ld a, [wd10e]
and $ef
ld [wd10e], a
call ResumeSong
pop af
call BankswitchROM
scf
ret
GameEvent_BattleCenter:
ld a, GAME_EVENT_BATTLE_CENTER
ld [wActiveGameEvent], a
xor a
ld [wSongOverride], a
ld a, -1
ld [wDuelResult], a
ld a, MUSIC_DUEL_THEME_1
ld [wDuelTheme], a
ld a, MUSIC_CARD_POP
call PlaySong
bank1call SetUpAndStartLinkDuel
scf
ret
GameEvent_Duel:
ld a, GAME_EVENT_DUEL
ld [wActiveGameEvent], a
xor a
ld [wSongOverride], a
call EnableSRAM
xor a
ld [sPlayerInChallengeMachine], a
call DisableSRAM
call SaveGeneralSaveData
bank1call StartDuel_VSAIOpp
scf
ret
GameEvent_ChallengeMachine:
ld a, MUSIC_PC_MAIN_MENU
ld [wDefaultSong], a
call PlayDefaultSong
call EnableSRAM
xor a
ld [sPlayerInChallengeMachine], a
call DisableSRAM
.asm_38ed
farcall ChallengeMachine_Start
ld a, MUSIC_OVERWORLD
ld [wDefaultSong], a
call PlayDefaultSong
scf
ret
GameEvent_ContinueDuel:
xor a
ld [wSongOverride], a
bank1call TryContinueDuel
call EnableSRAM
ld a, [sPlayerInChallengeMachine]
call DisableSRAM
cp $ff
jr z, GameEvent_ChallengeMachine.asm_38ed
scf
ret
GameEvent_Credits:
farcall PlayCreditsSequence
or a
ret
GetReceivedLegendaryCards:
ld a, EVENT_RECEIVED_LEGENDARY_CARDS
farcall GetEventValue
call EnableSRAM
ld [sReceivedLegendaryCards], a
call DisableSRAM
ret
; return in a the permission byte corresponding to the current map's x,y coordinates at bc
GetPermissionOfMapPosition:
push hl
call GetPermissionByteOfMapPosition
ld a, [hl]
pop hl
ret
; set to a the permission byte corresponding to the current map's x,y coordinates at bc
SetPermissionOfMapPosition:
push hl
push af
call GetPermissionByteOfMapPosition
pop af
ld [hl], a
pop hl
ret
; set the permission byte corresponding to the current map's x,y coordinates at bc
; to the value of register a anded by its current value
UpdatePermissionOfMapPosition:
push hl
push bc
push de
cpl
ld e, a
call GetPermissionByteOfMapPosition
ld a, [hl]
and e
ld [hl], a
pop de
pop bc
pop hl
ret
; returns in hl the address within wPermissionMap that corresponds to
; the current map's x,y coordinates at bc
GetPermissionByteOfMapPosition:
push bc
srl b
srl c
swap c
ld a, c
and $f0
or b
ld c, a
ld b, $0
ld hl, wPermissionMap
add hl, bc
pop bc
ret
; copy c bytes of data from hl in bank wTempPointerBank to de, b times.
CopyGfxDataFromTempBank:
ldh a, [hBankROM]
push af
ld a, [wTempPointerBank]
call BankswitchROM
call CopyGfxData
pop af
call BankswitchROM
ret
; Movement offsets for player movements
PlayerMovementOffsetTable:
db 0, -1 ; NORTH
db 1, 0 ; EAST
db 0, 1 ; SOUTH
db -1, 0 ; WEST
; Movement offsets for player movements, in tiles
PlayerMovementOffsetTable_Tiles:
db 0, -2 ; NORTH
db 2, 0 ; EAST
db 0, 2 ; SOUTH
db -2, 0 ; WEST
OverworldMapNames:
tx OverworldMapMasonLaboratoryText
tx OverworldMapMasonLaboratoryText
tx OverworldMapIshiharasHouseText
tx OverworldMapFightingClubText
tx OverworldMapRockClubText
tx OverworldMapWaterClubText
tx OverworldMapLightningClubText
tx OverworldMapGrassClubText
tx OverworldMapPsychicClubText
tx OverworldMapScienceClubText
tx OverworldMapFireClubText
tx OverworldMapChallengeHallText
tx OverworldMapPokemonDomeText
tx OverworldMapMysteryHouseText
Func_3997:
ldh a, [hBankROM]
push af
ld a, BANK(Func_1c056)
call BankswitchROM
call Func_1c056
pop af
call BankswitchROM
ret
; returns in hl a pointer to the first element for the a'th NPC
GetLoadedNPCID:
ld l, LOADED_NPC_ID
call GetItemInLoadedNPCIndex
ret
; return in hl a pointer to the a'th items element l
GetItemInLoadedNPCIndex:
push bc
cp LOADED_NPC_MAX
jr c, .asm_39b4
debug_nop
xor a
.asm_39b4
add a
add a
ld h, a
add a
add h
add l
ld l, a
ld h, $0
ld bc, wLoadedNPCs
add hl, bc
pop bc
ret
; Finds the index on wLoadedNPCs table of the npc in wTempNPC
; returns it in a and puts it into wLoadedNPCTempIndex
; c flag set if no npc found
FindLoadedNPC:
push hl
push bc
push de
xor a
ld [wLoadedNPCTempIndex], a
ld b, a
ld c, LOADED_NPC_MAX
ld de, LOADED_NPC_LENGTH
ld hl, wLoadedNPCs
ld a, [wTempNPC]
.findNPCLoop
cp [hl]
jr z, .foundNPCMatch
add hl, de
inc b
dec c
jr nz, .findNPCLoop
scf
jr z, .exit
.foundNPCMatch
ld a, b
ld [wLoadedNPCTempIndex], a
or a
.exit
pop de
pop bc
pop hl
ret
GetNextNPCMovementByte:
push bc
ldh a, [hBankROM]
push af
ld a, BANK(ExecuteNPCMovement)
call BankswitchROM
ld a, [bc]
ld c, a
pop af
call BankswitchROM
ld a, c
pop bc
ret
PlayDefaultSong:
push hl
push bc
call AssertSongFinished
or a
push af
call GetDefaultSong
ld c, a
pop af
jr z, .asm_3a11
ld a, c
ld hl, wSongOverride
cp [hl]
jr z, .asm_3a1c
.asm_3a11
ld a, c
cp NUM_SONGS
jr nc, .asm_3a1c
ld [wSongOverride], a
call PlaySong
.asm_3a1c
pop bc
pop hl
ret
; returns [wDefaultSong] or MUSIC_RONALD in a
GetDefaultSong:
ld a, [wRonaldIsInMap]
or a
jr z, .default_song
; only return Ronald's theme if it's
; not in one of the following maps
ld a, [wOverworldMapSelection]
cp OWMAP_ISHIHARAS_HOUSE
jr z, .default_song
cp OWMAP_CHALLENGE_HALL
jr z, .default_song
cp OWMAP_POKEMON_DOME
jr z, .default_song
ld a, MUSIC_RONALD
ret
.default_song
ld a, [wDefaultSong]
ret
|