Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pret
GitHub Repository: pret/pokered
Path: blob/master/engine/battle/wild_encounters.asm
1271 views
1
; try to initiate a wild pokemon encounter
2
; returns success in Z
3
TryDoWildEncounter:
4
ld a, [wNPCMovementScriptPointerTableNum]
5
and a
6
ret nz
7
ld a, [wMovementFlags]
8
and a ; is player exiting a door, jumping over a ledge, or fishing?
9
ret nz
10
callfar IsPlayerStandingOnDoorTileOrWarpTile
11
jr nc, .notStandingOnDoorOrWarpTile
12
.CantEncounter
13
ld a, $1
14
and a
15
ret
16
.notStandingOnDoorOrWarpTile
17
callfar IsPlayerJustOutsideMap
18
jr z, .CantEncounter
19
ld a, [wRepelRemainingSteps]
20
and a
21
jr z, .next
22
dec a
23
jr z, .lastRepelStep
24
ld [wRepelRemainingSteps], a
25
.next
26
; determine if wild pokemon can appear in the half-block we're standing in
27
; is the bottom right tile (9,9) of the half-block we're standing in a grass/water tile?
28
hlcoord 9, 9
29
ld c, [hl]
30
ld a, [wGrassTile]
31
cp c
32
ld a, [wGrassRate]
33
jr z, .CanEncounter
34
ld a, $14 ; in all tilesets with a water tile, this is its id
35
cp c
36
ld a, [wWaterRate]
37
jr z, .CanEncounter
38
; even if not in grass/water, standing anywhere we can encounter pokemon
39
; so long as the map is "indoor" and has wild pokemon defined.
40
; ...as long as it's not Viridian Forest or Safari Zone.
41
ld a, [wCurMap]
42
cp FIRST_INDOOR_MAP ; is this an indoor map?
43
jr c, .CantEncounter2
44
ld a, [wCurMapTileset]
45
cp FOREST ; Viridian Forest/Safari Zone
46
jr z, .CantEncounter2
47
ld a, [wGrassRate]
48
.CanEncounter
49
; compare encounter chance with a random number to determine if there will be an encounter
50
ld b, a
51
ldh a, [hRandomAdd]
52
cp b
53
jr nc, .CantEncounter2
54
ldh a, [hRandomSub]
55
ld b, a
56
ld hl, WildMonEncounterSlotChances
57
.determineEncounterSlot
58
ld a, [hli]
59
cp b
60
jr nc, .gotEncounterSlot
61
inc hl
62
jr .determineEncounterSlot
63
.gotEncounterSlot
64
; determine which wild pokemon (grass or water) can appear in the half-block we're standing in
65
ld c, [hl]
66
ld hl, wGrassMons
67
lda_coord 8, 9
68
cp $14 ; is the bottom left tile (8,9) of the half-block we're standing in a water tile?
69
jr nz, .gotWildEncounterType ; else, it's treated as a grass tile by default
70
ld hl, wWaterMons
71
; since the bottom right tile of a "left shore" half-block is $14 but the bottom left tile is not,
72
; "left shore" half-blocks (such as the one in the east coast of Cinnabar) load grass encounters.
73
.gotWildEncounterType
74
ld b, 0
75
add hl, bc
76
ld a, [hli]
77
ld [wCurEnemyLevel], a
78
ld a, [hl]
79
ld [wCurPartySpecies], a
80
ld [wEnemyMonSpecies2], a
81
ld a, [wRepelRemainingSteps]
82
and a
83
jr z, .willEncounter
84
ld a, [wPartyMon1Level]
85
ld b, a
86
ld a, [wCurEnemyLevel]
87
cp b
88
jr c, .CantEncounter2 ; repel prevents encounters if the leading party mon's level is higher than the wild mon
89
jr .willEncounter
90
.lastRepelStep
91
ld [wRepelRemainingSteps], a
92
ld a, TEXT_REPEL_WORE_OFF
93
ldh [hTextID], a
94
call EnableAutoTextBoxDrawing
95
call DisplayTextID
96
.CantEncounter2
97
ld a, $1
98
and a
99
ret
100
.willEncounter
101
xor a
102
ret
103
104
INCLUDE "data/wild/probabilities.asm"
105
106