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
|
; More overworld event handling.
WarpToSpawnPoint:: ; 97c28
ld hl, StatusFlags2
res 1, [hl] ; safari zone?
res 2, [hl] ; bug contest
ret
; 97c30
RunMemScript:: ; 97c30
; If there is no script here, we don't need to be here.
ld a, [wMapReentryScriptQueueFlag]
and a
ret z
; Execute the script at (wMapReentryScriptBank):(wMapReentryScriptAddress).
ld hl, wMapReentryScriptAddress
ld a, [hli]
ld h, [hl]
ld l, a
ld a, [wMapReentryScriptBank]
call CallScript
scf
; Clear the buffer for the next script.
push af
xor a
ld hl, wMapReentryScriptQueueFlag
ld bc, 8
call ByteFill
pop af
ret
; 97c4f
LoadScriptBDE:: ; 97c4f
; If there's already a script here, don't overwrite.
ld hl, wMapReentryScriptQueueFlag
ld a, [hl]
and a
ret nz
; Set the flag
ld [hl], 1
inc hl
; Load the script pointer b:de into (wMapReentryScriptBank):(wMapReentryScriptAddress)
ld [hl], b
inc hl
ld [hl], e
inc hl
ld [hl], d
scf
ret
; 97c5f
CheckFacingTileEvent:: ; 97c5f
call GetFacingTileCoord
ld [EngineBuffer1], a
ld c, a
callba CheckFacingTileForStd
jr c, .done
call CheckCutTreeTile
jr nz, .whirlpool
callba TryCutOW
jr .done
.whirlpool
ld a, [EngineBuffer1]
call CheckWhirlpoolTile
jr nz, .waterfall
callba TryWhirlpoolOW
jr .done
.waterfall
ld a, [EngineBuffer1]
call CheckWaterfallTile
jr nz, .headbutt
callba TryWaterfallOW
jr .done
.headbutt
ld a, [EngineBuffer1]
call CheckHeadbuttTreeTile
jr nz, .surf
callba TryHeadbuttOW
jr c, .done
jr .noevent
.surf
callba TrySurfOW
jr nc, .noevent
jr .done
.noevent
xor a
ret
.done
call PlayClickSFX
ld a, $ff
scf
ret
; 97cc0
RandomEncounter:: ; 97cc0
; Random encounter
call CheckWildEncounterCooldown
jr c, .nope
call CanUseSweetScent
jr nc, .nope
ld hl, StatusFlags2
bit 2, [hl] ; bug contest
jr nz, .bug_contest
callba TryWildEncounter
jr nz, .nope
jr .ok
.bug_contest
call _TryWildEncounter_BugContest
jr nc, .nope
jr .ok_bug_contest
.nope
ld a, 1
and a
ret
.ok
ld a, BANK(WildBattleScript)
ld hl, WildBattleScript
jr .done
.ok_bug_contest
ld a, BANK(BugCatchingContestBattleScript)
ld hl, BugCatchingContestBattleScript
jr .done
.done
call CallScript
scf
ret
; 97cf9
WildBattleScript: ; 97cf9
randomwildmon
startbattle
reloadmapafterbattle
end
; 97cfd
CanUseSweetScent:: ; 97cfd
ld hl, StatusFlags
bit 5, [hl]
jr nz, .no
ld a, [wPermission]
cp CAVE
jr z, .ice_check
cp DUNGEON
jr z, .ice_check
callba CheckGrassCollision
jr nc, .no
.ice_check
ld a, [PlayerStandingTile]
call CheckIceTile
jr z, .no
scf
ret
.no
and a
ret
; 97d23
_TryWildEncounter_BugContest: ; 97d23
call TryWildEncounter_BugContest
ret nc
call ChooseWildEncounter_BugContest
callba CheckRepelEffect
ret
; 97d31
ChooseWildEncounter_BugContest:: ; 97d31
; Pick a random mon out of ContestMons.
.loop
call Random
cp 100 << 1
jr nc, .loop
srl a
ld hl, ContestMons
ld de, 4
.CheckMon
sub [hl]
jr c, .GotMon
add hl, de
jr .CheckMon
.GotMon
inc hl
; Species
ld a, [hli]
ld [TempWildMonSpecies], a
; Min level
ld a, [hli]
ld d, a
; Max level
ld a, [hl]
sub d
jr nz, .RandomLevel
; If min and max are the same.
ld a, d
jr .GotLevel
.RandomLevel
; Get a random level between the min and max.
ld c, a
inc c
call Random
ld a, [hRandomAdd]
call SimpleDivide
add d
.GotLevel
ld [CurPartyLevel], a
xor a
ret
; 97d64
TryWildEncounter_BugContest: ; 97d64
ld a, [PlayerStandingTile]
call CheckSuperTallGrassTile
ld b, 40 percent
jr z, .ok
ld b, 20 percent
.ok
callba ApplyMusicEffectOnEncounterRate
callba ApplyCleanseTagEffectOnEncounterRate
call Random
ld a, [hRandomAdd]
cp b
ret c
ld a, 1
and a
ret
; 97d87
ContestMons: ; 97d87
; %, species, min, max
db 20, CATERPIE, 7, 18
db 20, WEEDLE, 7, 18
db 10, METAPOD, 9, 18
db 10, KAKUNA, 9, 18
db 5, BUTTERFREE, 12, 15
db 5, BEEDRILL, 12, 15
db 10, VENONAT, 10, 16
db 10, PARAS, 10, 17
db 5, SCYTHER, 13, 14
db 5, PINSIR, 13, 14
db -1, VENOMOTH, 30, 40
; 97db3
DoBikeStep:: ; 97db3
nop
nop
; If the bike shop owner doesn't have our number, or
; if we've already gotten the call, we don't have to
; be here.
ld hl, StatusFlags2
bit 4, [hl] ; bike shop call
jr z, .NoCall
; If we're not on the bike, we don't have to be here.
ld a, [PlayerState]
cp PLAYER_BIKE
jr nz, .NoCall
; If we're not in an area of phone service, we don't
; have to be here.
call GetMapHeaderPhoneServiceNybble
and a
jr nz, .NoCall
; Check the bike step count and check whether we've
; taken 65536 of them yet.
ld hl, wBikeStep
ld a, [hli]
ld d, a
ld e, [hl]
cp 255
jr nz, .increment
ld a, e
cp 255
jr z, .dont_increment
.increment
inc de
ld [hl], e
dec hl
ld [hl], d
.dont_increment
; If we've taken at least 1024 steps, have the bike
; shop owner try to call us.
ld a, d
cp 1024 >> 8
jr c, .NoCall
; If a call has already been queued, don't overwrite
; that call.
ld a, [wSpecialPhoneCallID]
and a
jr nz, .NoCall
; Queue the call.
ld a, SPECIALCALL_BIKESHOP
ld [wSpecialPhoneCallID], a
xor a
ld [wSpecialPhoneCallID + 1], a
ld hl, StatusFlags2
res 4, [hl] ; bike shop call
scf
ret
.NoCall
xor a
ret
; 97df9
ClearCmdQueue:: ; 97df9
ld hl, wCmdQueue
ld de, 6
ld c, 4
xor a
.loop
ld [hl], a
add hl, de
dec c
jr nz, .loop
ret
; 97e08
HandleCmdQueue:: ; 97e08
ld hl, wCmdQueue
xor a
.loop
ld [hMapObjectIndexBuffer], a
ld a, [hl]
and a
jr z, .skip
push hl
ld b, h
ld c, l
call HandleQueuedCommand
pop hl
.skip
ld de, CMDQUEUE_ENTRY_SIZE
add hl, de
ld a, [hMapObjectIndexBuffer]
inc a
cp CMDQUEUE_CAPACITY
jr nz, .loop
ret
; 97e25
GetNthCmdQueueEntry: ; 97e25 unreferenced
ld hl, wCmdQueue
ld bc, CMDQUEUE_ENTRY_SIZE
call AddNTimes
ld b, h
ld c, l
ret
; 97e31
WriteCmdQueue:: ; 97e31
push bc
push de
call .GetNextEmptyEntry
ld d, h
ld e, l
pop hl
pop bc
ret c
ld a, b
ld bc, CMDQUEUE_ENTRY_SIZE - 1
call FarCopyBytes
xor a
ld [hl], a
ret
; 97e45
.GetNextEmptyEntry: ; 97e45
ld hl, wCmdQueue
ld de, CMDQUEUE_ENTRY_SIZE
ld c, CMDQUEUE_CAPACITY
.loop
ld a, [hl]
and a
jr z, .done
add hl, de
dec c
jr nz, .loop
scf
ret
.done
ld a, CMDQUEUE_CAPACITY
sub c
and a
ret
; 97e5c
DelCmdQueue:: ; 97e5c
ld hl, wCmdQueue
ld de, CMDQUEUE_ENTRY_SIZE
ld c, CMDQUEUE_CAPACITY
.loop
ld a, [hl]
cp b
jr z, .done
add hl, de
dec c
jr nz, .loop
and a
ret
.done
xor a
ld [hl], a
scf
ret
; 97e72
_DelCmdQueue: ; 97e72
ld hl, CMDQUEUE_TYPE
add hl, bc
ld [hl], 0
ret
; 97e79
HandleQueuedCommand: ; 97e79
ld hl, CMDQUEUE_TYPE
add hl, bc
ld a, [hl]
cp 5
jr c, .okay
xor a
.okay
ld e, a
ld d, 0
ld hl, .Jumptable_ba
rept 3
add hl, de
endr
ld a, [hli]
push af
ld a, [hli]
ld h, [hl]
ld l, a
pop af
rst FarCall
ret
; 97e94
.Jumptable_ba: ; 97e94
dba CmdQueue_Null
dba CmdQueue_Null2
dba CmdQueue_StoneTable
dba CmdQueue_Type3
dba CmdQueue_Type4
; 97ea3
CmdQueueAnonymousJumptable: ; 97ea3
ld hl, CMDQUEUE_05
add hl, bc
ld a, [hl]
pop hl
rst JumpTable
ret
; 97eab
CmdQueueAnonJT_Increment: ; 97eab
ld hl, CMDQUEUE_05
add hl, bc
inc [hl]
ret
; 97eb1
CmdQueueAnonJT_Decrement: ; 97eb1
ld hl, CMDQUEUE_05
add hl, bc
dec [hl]
ret
; 97eb7
CmdQueue_Null: ; 97eb7
ret
; 97eb8
CmdQueue_Null2: ; 97eb8
call ret_2f3e
ret
; 97ebc
CmdQueue_Type4: ; 97ebc
call CmdQueueAnonymousJumptable
; anonymous dw
dw .zero
dw .one
; 97ec3
.zero: ; 97ec3
ld a, [hSCY]
ld hl, 4
add hl, bc
ld [hl], a
call CmdQueueAnonJT_Increment
.one: ; 97ecd
ld hl, 1
add hl, bc
ld a, [hl]
dec a
ld [hl], a
jr z, .asm_97eee
and $1
jr z, .asm_97ee4
ld hl, 2
add hl, bc
ld a, [hSCY]
sub [hl]
ld [hSCY], a
ret
.asm_97ee4
ld hl, 2
add hl, bc
ld a, [hSCY]
add [hl]
ld [hSCY], a
ret
.asm_97eee
ld hl, 4
add hl, bc
ld a, [hl]
ld [hSCY], a
call _DelCmdQueue
ret
; 97ef9
CmdQueue_Type3: ; 97ef9
call CmdQueueAnonymousJumptable
; anonymous dw
dw .zero
dw .one
dw .two
; 97f02
.zero: ; 97f02
call .IsPlayerFacingDown
jr z, .PlayerNotFacingDown
call CmdQueueAnonJT_Increment
.one: ; 97f0a
call .IsPlayerFacingDown
jr z, .PlayerNotFacingDown
call CmdQueueAnonJT_Increment
ld hl, 2
add hl, bc
ld a, [hl]
ld [wd173], a
ret
; 97f1b
.two: ; 97f1b
call .IsPlayerFacingDown
jr z, .PlayerNotFacingDown
call CmdQueueAnonJT_Decrement
ld hl, 3
add hl, bc
ld a, [hl]
ld [wd173], a
ret
; 97f2c
.PlayerNotFacingDown: ; 97f2c
ld a, $7f
ld [wd173], a
ld hl, 5
add hl, bc
ld [hl], 0
ret
; 97f38
.IsPlayerFacingDown: ; 97f38
push bc
ld bc, PlayerStruct
call GetSpriteDirection
and a
pop bc
ret
; 97f42
CmdQueue_StoneTable: ; 97f42
ld de, PlayerStruct
ld a, NUM_OBJECT_STRUCTS
.loop
push af
ld hl, OBJECT_SPRITE
add hl, de
ld a, [hl]
and a
jr z, .next
ld hl, OBJECT_MOVEMENTTYPE
add hl, de
ld a, [hl]
cp STEP_TYPE_SKYFALL_TOP
jr nz, .next
ld hl, OBJECT_NEXT_TILE
add hl, de
ld a, [hl]
call CheckPitTile
jr nz, .next
ld hl, OBJECT_DIRECTION_WALKING
add hl, de
ld a, [hl]
cp STANDING
jr nz, .next
call HandleStoneQueue
jr c, .fall_down_hole
.next
ld hl, OBJECT_STRUCT_LENGTH
add hl, de
ld d, h
ld e, l
pop af
dec a
jr nz, .loop
ret
.fall_down_hole
pop af
ret
; 97f7e
|