; try to initiate a wild pokemon encounter ; returns success in Z TryDoWildEncounter: ld a, [wNPCMovementScriptPointerTableNum] and a ret nz ld a, [wd736] and a ret nz callfar IsPlayerStandingOnDoorTileOrWarpTile jr nc, .notStandingOnDoorOrWarpTile .CantEncounter ld a, $1 and a ret .notStandingOnDoorOrWarpTile callfar IsPlayerJustOutsideMap jr z, .CantEncounter ld a, [wRepelRemainingSteps] and a jr z, .next dec a jp z, .lastRepelStep ld [wRepelRemainingSteps], a .next ; determine if wild pokemon can appear in the half-block we're standing in ; is the bottom right tile (9,9) of the half-block we're standing in a grass/water tile? hlcoord 9, 9 ld c, [hl] ld a, [wGrassTile] cp c ld a, [wGrassRate] jr z, .CanEncounter cp CITRINE ld a, $0E ; So the Citrine grass works. cp c ld a, [wGrassRate] jr z, .CanEncounter cp CELESTE ld a, $19 ; So the Celeste grass works. cp c ld a, [wGrassRate] jr z, .CanEncounter ld a, $14 ; in all tilesets with a water tile, this is its id cp c ld a, [wWaterRate] jr z, .CanEncounter ; even if not in grass/water, standing anywhere we can encounter pokemon ; so long as the map is "indoor" and has wild pokemon defined. ; ...as long as it's not Viridian Forest or Safari Zone. ld a, [wCurMap] cp FIRST_INDOOR_MAP ; is this an indoor map? jp c, .CantEncounter2 ld a, [wCurMapTileset] cp FOREST ; Viridian Forest/Safari Zone jp z, .CantEncounter2 cp CELESTE jp z, .CantEncounter2 ld a, [wGrassRate] .CanEncounter ; compare encounter chance with a random number to determine if there will be an encounter ld b, a ldh a, [hRandomAdd] cp b jr nc, .CantEncounter2 ldh a, [hRandomSub] ld b, a ld hl, WildMonEncounterSlotChances .determineEncounterSlot ld a, [hli] cp b jr nc, .gotEncounterSlot inc hl jr .determineEncounterSlot .gotEncounterSlot ; determine which wild pokemon (grass or water) can appear in the half-block we're standing in ld c, [hl] ld hl, wGrassMons lda_coord 8, 9 cp $14 ; is the bottom left tile (8,9) of the half-block we're standing in a water tile? jr nz, .gotWildEncounterType ; else, it's treated as a grass tile by default ld hl, wWaterMons ; since the bottom right tile of a "left shore" half-block is $14 but the bottom left tile is not, ; "left shore" half-blocks (such as the one in the east coast of Cinnabar) load grass encounters. .gotWildEncounterType ; Meltan functionality. ; This used to be in engine/battle/core.asm. ; However, it was a bit buggy as the implementation was forced. ; So instead, we do this: CheckEvent EVENT_MYSTERY_BOX_ACTIVATED jr nz, .meltanEncounter ; If so, skip this. ld b, 0 add hl, bc ld a, [hli] ld [wCurEnemyLVL], a ld a, [hl] ld [wcf91], a ld [wEnemyMonSpecies2], a ld a, [wRepelRemainingSteps] and a jr z, .willEncounter ld a, [wPartyMon1Level] ld b, a ld a, [wCurEnemyLVL] cp b jr c, .CantEncounter2 ; repel prevents encounters if the leading party mon's level is higher than the wild mon jp .willEncounter .meltanEncounter ; needed this function as otherwise we really mess up regular processing for no reason ld a, $01 ld [wDontSwitchOffMysteryBoxYet], a ; Using this variable here accounts for running from Meltan. ld b, 0 add hl, bc ld a, 5 ld [wCurEnemyLVL], a ld a, MELTAN ld [wcf91], a ld [wEnemyMonSpecies2], a ld a, [wRepelRemainingSteps] and a jr z, .willEncounter ld a, [wPartyMon1Level] ld b, a ld a, [wCurEnemyLVL] cp b jr c, .CantEncounter2 jr .willEncounter .lastRepelStep ld [wRepelRemainingSteps], a ld a, TEXT_REPEL_WORE_OFF ldh [hSpriteIndexOrTextID], a call EnableAutoTextBoxDrawing call DisplayTextID .CantEncounter2 ld a, $1 and a ret .willEncounter xor a ld [wIsTrainerBattle], a ret INCLUDE "data/wild/probabilities.asm"