wild_encounters.asm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, [wd736]
  8. and a
  9. ret nz
  10. callab IsPlayerStandingOnDoorTileOrWarpTile
  11. jr nc, .notStandingOnDoorOrWarpTile
  12. .CantEncounter
  13. ld a, $1
  14. and a
  15. ret
  16. .notStandingOnDoorOrWarpTile
  17. callab 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. coord hl, 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 REDS_HOUSE_1F ; 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. ld a, [hRandomAdd]
  52. cp b
  53. jr nc, .CantEncounter2
  54. ld 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. aCoord 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 [wCurEnemyLVL], a
  78. ld a, [hl]
  79. ld [wcf91], 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, [wCurEnemyLVL]
  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. ld [hSpriteIndexOrTextID], 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. WildMonEncounterSlotChances:
  104. ; There are 10 slots for wild pokemon, and this is the table that defines how common each of
  105. ; those 10 slots is. A random number is generated and then the first byte of each pair in this
  106. ; table is compared against that random number. If the random number is less than or equal
  107. ; to the first byte, then that slot is chosen. The second byte is double the slot number.
  108. db $32, $00 ; 51/256 = 19.9% chance of slot 0
  109. db $65, $02 ; 51/256 = 19.9% chance of slot 1
  110. db $8C, $04 ; 39/256 = 15.2% chance of slot 2
  111. db $A5, $06 ; 25/256 = 9.8% chance of slot 3
  112. db $BE, $08 ; 25/256 = 9.8% chance of slot 4
  113. db $D7, $0A ; 25/256 = 9.8% chance of slot 5
  114. db $E4, $0C ; 13/256 = 5.1% chance of slot 6
  115. db $F1, $0E ; 13/256 = 5.1% chance of slot 7
  116. db $FC, $10 ; 11/256 = 4.3% chance of slot 8
  117. db $FF, $12 ; 3/256 = 1.2% chance of slot 9