KDLPro

Encounter Code Range v1.0

Mar 7th, 2021 (edited)
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. LoadWildMonData:
  2.     call _GrassWildmonLookup
  3.     jr c, .copy
  4.     ld hl, wMornEncounterRate
  5.     xor a
  6.     ld [hli], a
  7.     ld [hli], a
  8.     ld [hl], a
  9.     jr .done_copy
  10.  
  11. .copy
  12.     inc hl
  13.     inc hl
  14.     ld de, wMornEncounterRate
  15.     ld bc, 3
  16.     call CopyBytes
  17. .done_copy
  18.     call _WaterWildmonLookup
  19.     ld a, 0
  20.     jr nc, .no_copy
  21.     inc hl
  22.     inc hl
  23.     ld a, [hl]
  24. .no_copy
  25.     ld [wWaterEncounterRate], a
  26.     ret
  27.  
  28. FindNest:
  29. ; Parameters:
  30. ; e: 0 = Johto, 1 = Kanto
  31. ; wNamedObjectIndex: species
  32.     hlcoord 0, 0
  33.     ld bc, SCREEN_WIDTH * SCREEN_HEIGHT
  34.     xor a
  35.     call ByteFill
  36.     ld a, e
  37.     and a
  38.     jr nz, .kanto
  39.     decoord 0, 0
  40.     ld hl, JohtoGrassWildMons
  41.     call .FindGrass
  42.     ld hl, JohtoWaterWildMons
  43.     call .FindWater
  44.     call .RoamMon1
  45.     call .RoamMon2
  46.     ret
  47.  
  48. .kanto
  49.     decoord 0, 0
  50.     ld hl, KantoGrassWildMons
  51.     call .FindGrass
  52.     ld hl, KantoWaterWildMons
  53.     jp .FindWater
  54.  
  55. .FindGrass:
  56.     ld a, [hl]
  57.     cp -1
  58.     ret z
  59.     push hl
  60.     ld a, [hli]
  61.     ld b, a
  62.     ld a, [hli]
  63.     ld c, a
  64.     inc hl
  65.     inc hl
  66.     inc hl
  67.     ld a, NUM_GRASSMON * 3
  68.     call .SearchMapForMon
  69.     jr nc, .next_grass
  70.     ld [de], a
  71.     inc de
  72.  
  73. .next_grass
  74.     pop hl
  75.     ld bc, GRASS_WILDDATA_LENGTH
  76.     add hl, bc
  77.     jr .FindGrass
  78.  
  79. .FindWater:
  80.     ld a, [hl]
  81.     cp -1
  82.     ret z
  83.     push hl
  84.     ld a, [hli]
  85.     ld b, a
  86.     ld a, [hli]
  87.     ld c, a
  88.     inc hl
  89.     ld a, NUM_WATERMON
  90.     call .SearchMapForMon
  91.     jr nc, .next_water
  92.     ld [de], a
  93.     inc de
  94.  
  95. .next_water
  96.     pop hl
  97.     ld bc, WATER_WILDDATA_LENGTH
  98.     add hl, bc
  99.     jr .FindWater
  100.  
  101. .SearchMapForMon:
  102.     inc hl
  103. .ScanMapLoop:
  104.     push af
  105.     ld a, [wNamedObjectIndex]
  106.     cp [hl]
  107.     jr z, .found
  108.     inc hl
  109.     inc hl
  110.     inc hl
  111.     pop af
  112.     dec a
  113.     jr nz, .ScanMapLoop
  114.     and a
  115.     ret
  116.  
  117. .found
  118.     pop af
  119.     jp .AppendNest
  120.  
  121. .AppendNest:
  122.     push de
  123.     call GetWorldMapLocation
  124.     ld c, a
  125.     hlcoord 0, 0
  126.     ld de, SCREEN_WIDTH * SCREEN_HEIGHT
  127. .AppendNestLoop:
  128.     ld a, [hli]
  129.     cp c
  130.     jr z, .found_nest
  131.     dec de
  132.     ld a, e
  133.     or d
  134.     jr nz, .AppendNestLoop
  135.     ld a, c
  136.     pop de
  137.     scf
  138.     ret
  139.  
  140. .found_nest
  141.     pop de
  142.     and a
  143.     ret
  144.  
  145. .RoamMon1:
  146.     ld a, [wRoamMon1Species]
  147.     ld b, a
  148.     ld a, [wNamedObjectIndex]
  149.     cp b
  150.     ret nz
  151.     ld a, [wRoamMon1MapGroup]
  152.     ld b, a
  153.     ld a, [wRoamMon1MapNumber]
  154.     ld c, a
  155.     call .AppendNest
  156.     ret nc
  157.     ld [de], a
  158.     inc de
  159.     ret
  160.  
  161. .RoamMon2:
  162.     ld a, [wRoamMon2Species]
  163.     ld b, a
  164.     ld a, [wNamedObjectIndex]
  165.     cp b
  166.     ret nz
  167.     ld a, [wRoamMon2MapGroup]
  168.     ld b, a
  169.     ld a, [wRoamMon2MapNumber]
  170.     ld c, a
  171.     call .AppendNest
  172.     ret nc
  173.     ld [de], a
  174.     inc de
  175.     ret
  176.  
  177. TryWildEncounter::
  178. ; Try to trigger a wild encounter.
  179.     call .EncounterRate
  180.     jr nc, .no_battle
  181.     call ChooseWildEncounter
  182.     jr nz, .no_battle
  183.     call CheckRepelEffect
  184.     jr nc, .no_battle
  185.     xor a
  186.     ret
  187.  
  188. .no_battle
  189.     xor a ; BATTLETYPE_NORMAL
  190.     ld [wTempWildMonSpecies], a
  191.     ld [wBattleType], a
  192.     ld a, 1
  193.     and a
  194.     ret
  195.  
  196. .EncounterRate:
  197.     call GetMapEncounterRate
  198.     call ApplyMusicEffectOnEncounterRate
  199.     call ApplyCleanseTagEffectOnEncounterRate
  200.     call Random
  201.     cp b
  202.     ret
  203.  
  204. GetMapEncounterRate:
  205.     ld hl, wMornEncounterRate
  206.     call CheckOnWater
  207.     ld a, wWaterEncounterRate - wMornEncounterRate
  208.     jr z, .ok
  209.     ld a, [wTimeOfDay]
  210. .ok
  211.     ld c, a
  212.     ld b, 0
  213.     add hl, bc
  214.     ld b, [hl]
  215.     ret
  216.  
  217. ApplyMusicEffectOnEncounterRate::
  218. ; Pokemon March and Ruins of Alph signal double encounter rate.
  219. ; Pokemon Lullaby halves encounter rate.
  220.     ld a, [wMapMusic]
  221.     cp MUSIC_POKEMON_MARCH
  222.     jr z, .double
  223.     cp MUSIC_RUINS_OF_ALPH_RADIO
  224.     jr z, .double
  225.     cp MUSIC_POKEMON_LULLABY
  226.     ret nz
  227.     srl b
  228.     ret
  229.  
  230. .double
  231.     sla b
  232.     ret
  233.  
  234. ApplyCleanseTagEffectOnEncounterRate::
  235. ; Cleanse Tag halves encounter rate.
  236.     ld hl, wPartyMon1Item
  237.     ld de, PARTYMON_STRUCT_LENGTH
  238.     ld a, [wPartyCount]
  239.     ld c, a
  240. .loop
  241.     ld a, [hl]
  242.     cp CLEANSE_TAG
  243.     jr z, .cleansetag
  244.     add hl, de
  245.     dec c
  246.     jr nz, .loop
  247.     ret
  248.  
  249. .cleansetag
  250.     srl b
  251.     ret
  252.  
  253. ChooseWildEncounter:
  254. ; Pick a random mon out of ContestMons.
  255.     call LoadWildMonDataPointer
  256.     jp nc, .nowildbattle
  257.     call CheckEncounterRoamMon
  258.     jp c, .startwildbattle
  259.  
  260.     inc hl
  261.     inc hl
  262.     inc hl
  263.     call CheckOnWater
  264.     ld de, WaterMonProbTable
  265.     jr z, .watermon
  266.     inc hl
  267.     inc hl
  268.     ld a, [wTimeOfDay]
  269.     ld bc, NUM_GRASSMON * 3
  270.     call AddNTimes
  271.     ld de, GrassMonProbTable
  272.  
  273. .watermon
  274. ; hl contains the pointer to the wild mon data, let's save that to the stack
  275.     push hl
  276. .randomloop
  277.     call Random
  278.     cp 100
  279.     jr nc, .randomloop
  280.     inc a ; 1 <= a <= 100
  281.     ld h, d
  282.     ld l, e
  283. ; This next loop chooses which mon to load up.
  284. .prob_bracket_loop
  285.     ld a, [hli]
  286.     cp b
  287.     jr nc, .got_it
  288.     inc hl
  289.     jr .prob_bracket_loop
  290.  
  291. .got_it
  292.     ld c, [hl]
  293.     ld b, 0
  294.     pop hl
  295.     add hl, bc ; this selects our mon
  296. ; Min level
  297.     ld a, [hli]
  298.     ld b, a
  299.  
  300. ; Max level
  301.     inc hl
  302.     ld a, [hl]
  303.  
  304.     sub b
  305.     jr nz, .RandomLevel
  306.  
  307. ; If min and max are the same.
  308.     ld a, b
  309.     jr .GotLevel
  310.  
  311. .RandomLevel:
  312. ; Get a random level between the min and max.
  313.     ld c, a
  314.     inc c
  315.     call Random
  316.     ldh a, [hRandomAdd]
  317.     call SimpleDivide
  318.     add b
  319.  
  320. .GotLevel:
  321.     ld [wCurPartyLevel], a
  322.     ld b, [hl]
  323.     ; ld a, b
  324.     call ValidateTempWildMonSpecies
  325.     jr c, .nowildbattle
  326.  
  327.     ld a, b ; This is in the wrong place.
  328.     cp UNOWN
  329.     jr nz, .done
  330.  
  331.     ld a, [wUnlockedUnowns]
  332.     and a
  333.     jr z, .nowildbattle
  334.  
  335. .done
  336.     jr .loadwildmon
  337.  
  338. .nowildbattle
  339.     ld a, 1
  340.     and a
  341.     ret
  342.  
  343. .loadwildmon
  344.     ld a, b
  345.     ld [wTempWildMonSpecies], a
  346.  
  347. .startwildbattle
  348.     xor a
  349.     ret
  350.  
  351. TryWildEncounter_BugContest:
  352.     ld a, [wPlayerStandingTile]
  353.     call CheckSuperTallGrassTile
  354.     ld b, 40 percent
  355.     jr z, .ok
  356.     ld b, 20 percent
  357.  
  358. .ok
  359.     farcall ApplyMusicEffectOnEncounterRate
  360.     farcall ApplyCleanseTagEffectOnEncounterRate
  361.     call Random
  362.     ldh a, [hRandomAdd]
  363.     cp b
  364.     ret c
  365.     ld a, 1
  366.     and a
  367.     ret
  368.  
  369. INCLUDE "data/wild/probabilities.asm"
  370.  
  371. CheckRepelEffect::
  372. ; If there is no active Repel, there's no need to be here.
  373.     ld a, [wRepelEffect]
  374.     and a
  375.     jr z, .encounter
  376. ; Get the first Pokemon in your party that isn't fainted.
  377.     ld hl, wPartyMon1HP
  378.     ld bc, PARTYMON_STRUCT_LENGTH - 1
  379. .loop
  380.     ld a, [hli]
  381.     or [hl]
  382.     jr nz, .ok
  383.     add hl, bc
  384.     jr .loop
  385.  
  386. .ok
  387. ; to PartyMonLevel
  388. rept 4
  389.     dec hl
  390. endr
  391.  
  392.     ld a, [wCurPartyLevel]
  393.     cp [hl]
  394.     jr nc, .encounter
  395.     and a
  396.     ret
  397.  
  398. .encounter
  399.     scf
  400.     ret
  401.  
  402. LoadWildMonDataPointer:
  403.     call CheckOnWater
  404.     jr z, _WaterWildmonLookup
  405.  
  406. _GrassWildmonLookup:
  407.     ld hl, SwarmGrassWildMons
  408.     ld bc, GRASS_WILDDATA_LENGTH
  409.     call _SwarmWildmonCheck
  410.     ret c
  411.     ld hl, JohtoGrassWildMons
  412.     ld de, KantoGrassWildMons
  413.     call _JohtoWildmonCheck
  414.     ld bc, GRASS_WILDDATA_LENGTH
  415.     jr _NormalWildmonOK
  416.  
  417. _WaterWildmonLookup:
  418.     ld hl, SwarmWaterWildMons
  419.     ld bc, WATER_WILDDATA_LENGTH
  420.     call _SwarmWildmonCheck
  421.     ret c
  422.     ld hl, JohtoWaterWildMons
  423.     ld de, KantoWaterWildMons
  424.     call _JohtoWildmonCheck
  425.     ld bc, WATER_WILDDATA_LENGTH
  426.     jr _NormalWildmonOK
  427.  
  428. _JohtoWildmonCheck:
  429.     call IsInJohto
  430.     and a
  431.     ret z
  432.     ld h, d
  433.     ld l, e
  434.     ret
  435.  
  436. _SwarmWildmonCheck:
  437.     call CopyCurrMapDE
  438.     push hl
  439.     ld hl, wSwarmFlags
  440.     bit SWARMFLAGS_DUNSPARCE_SWARM_F, [hl]
  441.     pop hl
  442.     jr z, .CheckYanma
  443.     ld a, [wDunsparceMapGroup]
  444.     cp d
  445.     jr nz, .CheckYanma
  446.     ld a, [wDunsparceMapNumber]
  447.     cp e
  448.     jr nz, .CheckYanma
  449.     call LookUpWildmonsForMapDE
  450.     jr nc, _NoSwarmWildmon
  451.     scf
  452.     ret
  453.  
  454. .CheckYanma:
  455.     push hl
  456.     ld hl, wSwarmFlags
  457.     bit SWARMFLAGS_YANMA_SWARM_F, [hl]
  458.     pop hl
  459.     jr z, _NoSwarmWildmon
  460.     ld a, [wYanmaMapGroup]
  461.     cp d
  462.     jr nz, _NoSwarmWildmon
  463.     ld a, [wYanmaMapNumber]
  464.     cp e
  465.     jr nz, _NoSwarmWildmon
  466.     call LookUpWildmonsForMapDE
  467.     jr nc, _NoSwarmWildmon
  468.     scf
  469.     ret
  470.  
  471. _NoSwarmWildmon:
  472.     and a
  473.     ret
  474.  
  475. _NormalWildmonOK:
  476.     call CopyCurrMapDE
  477.     jr LookUpWildmonsForMapDE
  478.  
  479. CopyCurrMapDE:
  480.     ld a, [wMapGroup]
  481.     ld d, a
  482.     ld a, [wMapNumber]
  483.     ld e, a
  484.     ret
  485.  
  486. LookUpWildmonsForMapDE:
  487. .loop
  488.     push hl
  489.     ld a, [hl]
  490.     inc a
  491.     jr z, .nope
  492.     ld a, d
  493.     cp [hl]
  494.     jr nz, .next
  495.     inc hl
  496.     ld a, e
  497.     cp [hl]
  498.     jr z, .yup
  499.  
  500. .next
  501.     pop hl
  502.     add hl, bc
  503.     jr .loop
  504.  
  505. .nope
  506.     pop hl
  507.     and a
  508.     ret
  509.  
  510. .yup
  511.     pop hl
  512.     scf
  513.     ret
  514.  
  515. InitRoamMons:
  516. ; initialize wRoamMon structs
  517.  
  518. ; species
  519.     ld a, RAIKOU
  520.     ld [wRoamMon1Species], a
  521.     ld a, ENTEI
  522.     ld [wRoamMon2Species], a
  523.  
  524. ; level
  525.     ld a, 40
  526.     ld [wRoamMon1Level], a
  527.     ld [wRoamMon2Level], a
  528.  
  529. ; raikou starting map
  530.     ld a, GROUP_ROUTE_42
  531.     ld [wRoamMon1MapGroup], a
  532.     ld a, MAP_ROUTE_42
  533.     ld [wRoamMon1MapNumber], a
  534.  
  535. ; entei starting map
  536.     ld a, GROUP_ROUTE_37
  537.     ld [wRoamMon2MapGroup], a
  538.     ld a, MAP_ROUTE_37
  539.     ld [wRoamMon2MapNumber], a
  540.  
  541. ; hp
  542.     xor a ; generate new stats
  543.     ld [wRoamMon1HP], a
  544.     ld [wRoamMon2HP], a
  545.  
  546.     ret
  547.  
  548. CheckEncounterRoamMon:
  549.     push hl
  550. ; Don't trigger an encounter if we're on water.
  551.     call CheckOnWater
  552.     jr z, .DontEncounterRoamMon
  553. ; Load the current map group and number to de
  554.     call CopyCurrMapDE
  555. ; Randomly select a beast.
  556.     call Random
  557.     cp 100 ; 25/64 chance
  558.     jr nc, .DontEncounterRoamMon
  559.     and %00000011 ; Of that, a 3/4 chance.  Running total: 75/256, or around 29.3%.
  560.     jr z, .DontEncounterRoamMon
  561.     dec a ; 1/3 chance that it's Entei, 1/3 chance that it's Raikou
  562. ; Compare its current location with yours
  563.     ld hl, wRoamMon1MapGroup
  564.     ld c, a
  565.     ld b, 0
  566.     ld a, 7 ; length of the roam_struct
  567.     call AddNTimes
  568.     ld a, d
  569.     cp [hl]
  570.     jr nz, .DontEncounterRoamMon
  571.     inc hl
  572.     ld a, e
  573.     cp [hl]
  574.     jr nz, .DontEncounterRoamMon
  575. ; We've decided to take on a beast, so stage its information for battle.
  576.     dec hl
  577.     dec hl
  578.     dec hl
  579.     ld a, [hli]
  580.     ld [wTempWildMonSpecies], a
  581.     ld a, [hl]
  582.     ld [wCurPartyLevel], a
  583.     ld a, BATTLETYPE_ROAMING
  584.     ld [wBattleType], a
  585.  
  586.     pop hl
  587.     scf
  588.     ret
  589.  
  590. .DontEncounterRoamMon:
  591.     pop hl
  592.     and a
  593.     ret
  594.  
  595. UpdateRoamMons:
  596.     ld a, [wRoamMon1MapGroup]
  597.     cp GROUP_N_A
  598.     jr z, .SkipRaikou
  599.     ld b, a
  600.     ld a, [wRoamMon1MapNumber]
  601.     ld c, a
  602.     call .Update
  603.     ld a, b
  604.     ld [wRoamMon1MapGroup], a
  605.     ld a, c
  606.     ld [wRoamMon1MapNumber], a
  607.  
  608. .SkipRaikou:
  609.     ld a, [wRoamMon2MapGroup]
  610.     cp GROUP_N_A
  611.     jr z, .SkipEntei
  612.     ld b, a
  613.     ld a, [wRoamMon2MapNumber]
  614.     ld c, a
  615.     call .Update
  616.     ld a, b
  617.     ld [wRoamMon2MapGroup], a
  618.     ld a, c
  619.     ld [wRoamMon2MapNumber], a
  620.  
  621. .SkipEntei:
  622.     ld a, [wRoamMon3MapGroup]
  623.     cp GROUP_N_A
  624.     jr z, .Finished
  625.     ld b, a
  626.     ld a, [wRoamMon3MapNumber]
  627.     ld c, a
  628.     call .Update
  629.     ld a, b
  630.     ld [wRoamMon3MapGroup], a
  631.     ld a, c
  632.     ld [wRoamMon3MapNumber], a
  633.  
  634. .Finished:
  635.     jp _BackUpMapIndices
  636.  
  637. .Update:
  638.     ld hl, RoamMaps
  639. .loop
  640. ; Are we at the end of the table?
  641.     ld a, [hl]
  642.     cp -1
  643.     ret z
  644. ; Is this the correct entry?
  645.     ld a, b
  646.     cp [hl]
  647.     jr nz, .next
  648.     inc hl
  649.     ld a, c
  650.     cp [hl]
  651.     jr z, .yes
  652. ; We don't have the correct entry yet, so let's continue.  A 0 terminates each entry.
  653. .next
  654.     ld a, [hli]
  655.     and a
  656.     jr nz, .next
  657.     jr .loop
  658.  
  659. ; We have the correct entry now, so let's choose a random map from it.
  660. .yes
  661.     inc hl
  662.     ld d, h
  663.     ld e, l
  664. .update_loop
  665.     ld h, d
  666.     ld l, e
  667. ; Choose which map to warp to.
  668.     call Random
  669.     and %00011111 ; 1/8n chance it moves to a completely random map, where n is the number of roaming connections from the current map.
  670.     jr z, JumpRoamMon
  671.     and %11
  672.     cp [hl]
  673.     jr nc, .update_loop ; invalid index, try again
  674.     inc hl
  675.     ld c, a
  676.     ld b, 0
  677.     add hl, bc
  678.     add hl, bc
  679.     ld a, [wRoamMons_LastMapGroup]
  680.     cp [hl]
  681.     jr nz, .done
  682.     inc hl
  683.     ld a, [wRoamMons_LastMapNumber]
  684.     cp [hl]
  685.     jr z, .update_loop
  686.     dec hl
  687.  
  688. .done
  689.     ld a, [hli]
  690.     ld b, a
  691.     ld c, [hl]
  692.     ret
  693.  
  694. JumpRoamMons:
  695.     ld a, [wRoamMon1MapGroup]
  696.     cp GROUP_N_A
  697.     jr z, .SkipRaikou
  698.     call JumpRoamMon
  699.     ld a, b
  700.     ld [wRoamMon1MapGroup], a
  701.     ld a, c
  702.     ld [wRoamMon1MapNumber], a
  703.  
  704. .SkipRaikou:
  705.     ld a, [wRoamMon2MapGroup]
  706.     cp GROUP_N_A
  707.     jr z, .SkipEntei
  708.     call JumpRoamMon
  709.     ld a, b
  710.     ld [wRoamMon2MapGroup], a
  711.     ld a, c
  712.     ld [wRoamMon2MapNumber], a
  713.  
  714. .SkipEntei:
  715.     ld a, [wRoamMon3MapGroup]
  716.     cp GROUP_N_A
  717.     jr z, .Finished
  718.     call JumpRoamMon
  719.     ld a, b
  720.     ld [wRoamMon3MapGroup], a
  721.     ld a, c
  722.     ld [wRoamMon3MapNumber], a
  723.  
  724. .Finished:
  725.     jp _BackUpMapIndices
  726.  
  727. JumpRoamMon:
  728. .loop
  729.     ld hl, RoamMaps
  730. .innerloop1
  731.     ; 0-15 are all valid indexes into RoamMaps,
  732.     ; so this retry loop is unnecessary
  733.     ; since NUM_ROAMMON_MAPS happens to be 16
  734.     call Random
  735.     maskbits NUM_ROAMMON_MAPS
  736.     cp NUM_ROAMMON_MAPS
  737.     jr nc, .innerloop1
  738.     inc a
  739.     ld b, a
  740. .innerloop2 ; Loop to get hl to the address of the chosen roam map.
  741.     dec b
  742.     jr z, .ok
  743. .innerloop3 ; Loop to skip the current roam map, which is terminated by a 0.
  744.     ld a, [hli]
  745.     and a
  746.     jr nz, .innerloop3
  747.     jr .innerloop2
  748. ; Check to see if the selected map is the one the player is currently in.  If so, try again.
  749. .ok
  750.     ld a, [wMapGroup]
  751.     cp [hl]
  752.     jr nz, .done
  753.     inc hl
  754.     ld a, [wMapNumber]
  755.     cp [hl]
  756.     jr z, .loop
  757.     dec hl
  758. ; Return the map group and number in bc.
  759. .done
  760.     ld a, [hli]
  761.     ld b, a
  762.     ld c, [hl]
  763.     ret
  764.  
  765. _BackUpMapIndices:
  766.     ld a, [wRoamMons_CurMapNumber]
  767.     ld [wRoamMons_LastMapNumber], a
  768.     ld a, [wRoamMons_CurMapGroup]
  769.     ld [wRoamMons_LastMapGroup], a
  770.     ld a, [wMapNumber]
  771.     ld [wRoamMons_CurMapNumber], a
  772.     ld a, [wMapGroup]
  773.     ld [wRoamMons_CurMapGroup], a
  774.     ret
  775.  
  776. INCLUDE "data/wild/roammon_maps.asm"
  777.  
  778. ValidateTempWildMonSpecies:
  779. ; Due to a development oversight, this function is called with the wild Pokemon's level, not its species, in a.
  780.     and a
  781.     jr z, .nowildmon ; = 0
  782.     cp NUM_POKEMON + 1 ; 252
  783.     jr nc, .nowildmon ; >= 252
  784.     and a ; 1 <= Species <= 251
  785.     ret
  786.  
  787. .nowildmon
  788.     scf
  789.     ret
  790.  
  791. ; Finds a rare wild Pokemon in the route of the trainer calling, then checks if it's been Seen already.
  792. ; The trainer will then tell you about the Pokemon if you haven't seen it.
  793. RandomUnseenWildMon:
  794.     farcall GetCallerLocation
  795.     ld d, b
  796.     ld e, c
  797.     ld hl, JohtoGrassWildMons
  798.     ld bc, GRASS_WILDDATA_LENGTH
  799.     call LookUpWildmonsForMapDE
  800.     jr c, .GetGrassmon
  801.     ld hl, KantoGrassWildMons
  802.     call LookUpWildmonsForMapDE
  803.     jr nc, .done
  804.  
  805. .GetGrassmon:
  806.     push hl
  807.     ld bc, 5 + 4 * 3 ; Location of the level of the 5th wild Pokemon in that map
  808.     add hl, bc
  809.     ld a, [wTimeOfDay]
  810.     ld bc, NUM_GRASSMON * 3
  811.     call AddNTimes
  812. .randloop1
  813.     call Random
  814.     and %11
  815.     jr z, .randloop1
  816.     dec a
  817.     ld c, a
  818.     ld b, 0
  819.     add hl, bc
  820.     add hl, bc
  821. ; We now have the pointer to one of the last (rarest) three wild Pokemon found in that area.
  822.     inc hl
  823.     ld c, [hl] ; Contains the species index of this rare Pokemon
  824.     pop hl
  825.     ld de, 5 + 0 * 3
  826.     add hl, de
  827.     inc hl ; Species index of the most common Pokemon on that route
  828.     ld b, 4
  829. .loop2
  830.     ld a, [hli]
  831.     cp c ; Compare this most common Pokemon with the rare one stored in c.
  832.     jr z, .done
  833.     inc hl
  834.     inc hl
  835.     dec b
  836.     jr nz, .loop2
  837. ; This Pokemon truly is rare.
  838.     push bc
  839.     dec c
  840.     ld a, c
  841.     call CheckSeenMon
  842.     pop bc
  843.     jr nz, .done
  844. ; Since we haven't seen it, have the caller tell us about it.
  845.     ld de, wStringBuffer1
  846.     call CopyName1
  847.     ld a, c
  848.     ld [wNamedObjectIndex], a
  849.     call GetPokemonName
  850.     ld hl, .JustSawSomeRareMonText
  851.     call PrintText
  852.     xor a
  853.     ld [wScriptVar], a
  854.     ret
  855.  
  856. .done
  857.     ld a, $1
  858.     ld [wScriptVar], a
  859.     ret
  860.  
  861. .JustSawSomeRareMonText:
  862.     text_far _JustSawSomeRareMonText
  863.     text_end
  864.  
  865. RandomPhoneWildMon:
  866.     farcall GetCallerLocation
  867.     ld d, b
  868.     ld e, c
  869.     ld hl, JohtoGrassWildMons
  870.     ld bc, GRASS_WILDDATA_LENGTH
  871.     call LookUpWildmonsForMapDE
  872.     jr c, .ok
  873.     ld hl, KantoGrassWildMons
  874.     call LookUpWildmonsForMapDE
  875.  
  876. .ok
  877.     ld bc, 5 + 0 * 3
  878.     add hl, bc
  879.     ld a, [wTimeOfDay]
  880.     inc a
  881.     ld bc, NUM_GRASSMON * 3
  882. .loop
  883.     dec a
  884.     jr z, .done
  885.     add hl, bc
  886.     jr .loop
  887.  
  888. .done
  889.     call Random
  890.     and %11
  891.     ld c, a
  892.     ld b, 0
  893.     add hl, bc
  894.     add hl, bc
  895.     inc hl
  896.     ld a, [hl]
  897.     ld [wNamedObjectIndex], a
  898.     call GetPokemonName
  899.     ld hl, wStringBuffer1
  900.     ld de, wStringBuffer4
  901.     ld bc, MON_NAME_LENGTH
  902.     jp CopyBytes
  903.  
  904. RandomPhoneMon:
  905. ; Get a random monster owned by the trainer who's calling.
  906.     farcall GetCallerLocation
  907.     ld hl, TrainerGroups
  908.     ld a, d
  909.     dec a
  910.     ld c, a
  911.     ld b, 0
  912.     add hl, bc
  913.     add hl, bc
  914.     ld a, BANK(TrainerGroups)
  915.     call GetFarWord
  916.  
  917. .skip_trainer
  918.     dec e
  919.     jr z, .skipped
  920. .skip
  921.     ld a, BANK(Trainers)
  922.     call GetFarByte
  923.     inc hl
  924.     cp -1
  925.     jr nz, .skip
  926.     jr .skip_trainer
  927. .skipped
  928.  
  929. .skip_name
  930.     ld a, BANK(Trainers)
  931.     call GetFarByte
  932.     inc hl
  933.     cp "@"
  934.     jr nz, .skip_name
  935.  
  936.     ld a, BANK(Trainers)
  937.     call GetFarByte
  938.     inc hl
  939.     ld bc, 2 ; level, species
  940.     cp TRAINERTYPE_NORMAL
  941.     jr z, .got_mon_length
  942.     ld bc, 2 + NUM_MOVES ; level, species, moves
  943.     cp TRAINERTYPE_MOVES
  944.     jr z, .got_mon_length
  945.     ld bc, 2 + 1 ; level, species, item
  946.     cp TRAINERTYPE_ITEM
  947.     jr z, .got_mon_length
  948.     ; TRAINERTYPE_ITEM_MOVES
  949.     ld bc, 2 + 1 + NUM_MOVES ; level, species, item, moves
  950. .got_mon_length
  951.  
  952.     ld e, 0
  953.     push hl
  954. .count_mon
  955.     inc e
  956.     add hl, bc
  957.     ld a, BANK(Trainers)
  958.     call GetFarByte
  959.     cp -1
  960.     jr nz, .count_mon
  961.     pop hl
  962.  
  963. .rand
  964.     call Random
  965.     maskbits PARTY_LENGTH
  966.     cp e
  967.     jr nc, .rand
  968.  
  969.     inc a
  970. .get_mon
  971.     dec a
  972.     jr z, .got_mon
  973.     add hl, bc
  974.     jr .get_mon
  975. .got_mon
  976.  
  977.     inc hl ; species
  978.     ld a, BANK(Trainers)
  979.     call GetFarByte
  980.     ld [wNamedObjectIndex], a
  981.     call GetPokemonName
  982.     ld hl, wStringBuffer1
  983.     ld de, wStringBuffer4
  984.     ld bc, MON_NAME_LENGTH
  985.     jp CopyBytes
  986.  
  987. INCLUDE "data/wild/johto_grass.asm"
  988. INCLUDE "data/wild/johto_water.asm"
  989. INCLUDE "data/wild/kanto_grass.asm"
  990. INCLUDE "data/wild/kanto_water.asm"
  991. INCLUDE "data/wild/swarm_grass.asm"
  992. INCLUDE "data/wild/swarm_water.asm"
Add Comment
Please, Sign In to add comment