Advertisement
TechSkylander1518

PField_RoamingPokemon

Jul 14th, 2022
1,300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 8.17 KB | None | 0 0
  1. class PokemonGlobalMetadata
  2.   attr_accessor :roamPosition
  3.   attr_accessor :roamHistory
  4.   attr_accessor :roamedAlready
  5.   attr_accessor :roamEncounter
  6.   attr_accessor :roamPokemon
  7.   attr_accessor :roamPokemonCaught
  8.  
  9.   def roamPokemonCaught
  10.     if !@roamPokemonCaught
  11.       @roamPokemonCaught=[]
  12.     end
  13.     return @roamPokemonCaught
  14.   end
  15. end
  16.  
  17.  
  18.  
  19. # Resets all roaming Pokemon that were defeated without having been caught.
  20. def pbResetAllRoamers
  21.   if $PokemonGlobal && $PokemonGlobal.roamPokemon
  22.     for i in 0...$PokemonGlobal.roamPokemon.length
  23.       if $PokemonGlobal.roamPokemon[i]==true && $PokemonGlobal.roamPokemonCaught[i]!=true
  24.         $PokemonGlobal.roamPokemon[i]=nil
  25.       end
  26.     end
  27.   end
  28. end
  29.  
  30. # Gets the roaming areas for a particular Pokémon.
  31. def pbRoamingAreas(index)
  32.   data=RoamingSpecies[index]
  33.   return data[5] if data && data[5]
  34.   return RoamingAreas
  35. end
  36.  
  37. # Puts a roamer in a completely random map available to it.
  38. def pbRandomRoam(index)
  39.   if $PokemonGlobal.roamPosition
  40.     keys=pbRoamingAreas(index).keys
  41.     $PokemonGlobal.roamPosition[index]=keys[rand(keys.length)]
  42.   end
  43. end
  44.  
  45. # Roams all roamers, if their Switch is on.
  46. def pbRoamPokemon(ignoretrail=false)
  47.   # Start all roamers off in random maps
  48.   if !$PokemonGlobal.roamPosition
  49.     $PokemonGlobal.roamPosition={}
  50.     for i in 0...RoamingSpecies.length
  51.       species=getID(PBSpecies,RoamingSpecies[i][0])
  52.       next if !species || species<=0
  53.       keys=pbRoamingAreas(i).keys
  54.       $PokemonGlobal.roamPosition[i]=keys[rand(keys.length)]
  55.     end
  56.   end
  57.   $PokemonGlobal.roamHistory=[] if !$PokemonGlobal.roamHistory
  58.   $PokemonGlobal.roamPokemon=[] if !$PokemonGlobal.roamPokemon
  59.   # Roam each Pokémon in turn
  60.   for i in 0...RoamingSpecies.length
  61.     pbRoamPokemonOne(i,ignoretrail)
  62.   end
  63. end
  64.  
  65. def pbRoamPokemonOne(i,ignoretrail=false)
  66.   poke=RoamingSpecies[i]
  67.   if $game_switches[poke[2]]
  68.     species=getID(PBSpecies,poke[0])
  69.     return if !species || species<=0
  70.     choices=[]
  71.     keys=pbRoamingAreas(i).keys
  72.     currentArea=$PokemonGlobal.roamPosition[i]
  73.     if !currentArea
  74.       $PokemonGlobal.roamPosition[i]=keys[rand(keys.length)]
  75.       currentArea=$PokemonGlobal.roamPosition[i]
  76.     end
  77.     newAreas=pbRoamingAreas(i)[currentArea]
  78.     return if !newAreas
  79.     for area in newAreas
  80.       inhistory=$PokemonGlobal.roamHistory.include?(area)
  81.       inhistory=false if ignoretrail
  82.       choices.push(area) if !inhistory
  83.     end
  84.     if rand(32)==0 && keys.length>0
  85.       area=keys[rand(keys.length)]
  86.       inhistory=$PokemonGlobal.roamHistory.include?(area)
  87.       inhistory=false if ignoretrail
  88.       choices.push(area) if !inhistory
  89.     end
  90.     if choices.length>0
  91.       area=choices[rand(choices.length)]
  92.       $PokemonGlobal.roamPosition[i]=area
  93.     end
  94.   end
  95. end
  96.  
  97. Events.onMapChange+=proc {|sender,e|
  98.    return if !$PokemonGlobal
  99.    mapinfos=$RPGVX ? load_data("Data/MapInfos.rvdata") : load_data("Data/MapInfos.rxdata")
  100.    return if $game_map && mapinfos && e[0]>0 && mapinfos[e[0]] &&
  101.              mapinfos[e[0]].name && $game_map.name==mapinfos[e[0]].name
  102.    pbRoamPokemon
  103.    if $PokemonGlobal.roamHistory.length>=2
  104.      $PokemonGlobal.roamHistory.shift
  105.    end
  106.    $PokemonGlobal.roamedAlready=false
  107.    $PokemonGlobal.roamHistory.push($game_map.map_id)
  108. }
  109.  
  110.  
  111.  
  112. class PokemonTemp
  113.   attr_accessor :nowRoaming
  114.   attr_accessor :roamerIndex
  115. end
  116.  
  117.  
  118.  
  119. Events.onWildBattleOverride+= proc { |sender,e|
  120.    species=e[0]
  121.    level=e[1]
  122.    handled=e[2]
  123.    next if handled[0]!=nil
  124.    next if !$PokemonTemp.nowRoaming
  125.    next if $PokemonTemp.roamerIndex==nil
  126.    next if !$PokemonGlobal.roamEncounter
  127.    handled[0]=pbRoamingPokemonBattle(species,level)
  128. }
  129.  
  130. def pbRoamingPokemonBattle(species,level)
  131.   index=$PokemonTemp.roamerIndex
  132.   if $PokemonGlobal.roamPokemon[index] &&
  133.      $PokemonGlobal.roamPokemon[index].is_a?(PokeBattle_Pokemon)
  134.     genwildpoke=$PokemonGlobal.roamPokemon[index]
  135.   else
  136.     genwildpoke=pbGenerateWildPokemon(species,level,true)
  137.   end
  138.   Events.onStartBattle.trigger(nil,genwildpoke)
  139.   scene=pbNewBattleScene
  140.   $battle=PokeBattle_Battle.new(scene,$Trainer.party,[genwildpoke],$Trainer,nil)
  141.   $battle.internalbattle=true
  142.   $battle.cantescape=false
  143.   $battle.rules["alwaysflee"]=true
  144.   pbPrepareBattle($battle)
  145.   decision=0
  146.   pbBattleAnimation(pbGetWildBattleBGM(species),0,[genwildpoke]) {
  147.     pbSceneStandby {
  148.       decision = $battle.pbStartBattle
  149.     }
  150.     pbAfterBattle(decision,false)
  151.   }
  152.   Input.update
  153.   if decision==1 || decision==4   # Defeated or caught
  154.     $PokemonGlobal.roamPokemon[index]=true
  155.     $PokemonGlobal.roamPokemonCaught[index]=(decision==4)
  156.   else
  157.     $PokemonGlobal.roamPokemon[index]=genwildpoke
  158.   end
  159.   $PokemonGlobal.roamEncounter=nil
  160.   $PokemonGlobal.roamedAlready=true
  161.   Events.onWildBattleEnd.trigger(nil,species,level,decision)
  162.   return (decision!=2 && decision!=5)
  163. end
  164.  
  165. EncounterModifier.register(proc {|encounter|
  166.   $PokemonTemp.nowRoaming=false
  167.   $PokemonTemp.roamerIndex=nil
  168.   return nil if !encounter
  169.   return encounter if $PokemonGlobal.roamedAlready
  170.   return encounter if $PokemonGlobal.partner
  171.   return encounter if $PokemonTemp.pokeradar
  172.   return encounter if rand(4)!=0
  173.   roam=[]
  174.   for i in 0...RoamingSpecies.length
  175.     poke=RoamingSpecies[i]
  176.     species=getID(PBSpecies,poke[0])
  177.     next if !species || species<=0
  178.     if $game_switches[poke[2]] && $PokemonGlobal.roamPokemon[i]!=true
  179.       currentArea=$PokemonGlobal.roamPosition[i]
  180.       if !currentArea
  181.         $PokemonGlobal.roamPosition[i]=keys[rand(keys.length)]
  182.         currentArea=$PokemonGlobal.roamPosition[i]
  183.       end
  184.       roamermeta=pbGetMetadata(currentArea,MetadataMapPosition)
  185.       possiblemaps=[]
  186.       mapinfos=$RPGVX ? load_data("Data/MapInfos.rvdata") : load_data("Data/MapInfos.rxdata")
  187.       for j in 1...mapinfos.length
  188.         jmeta=pbGetMetadata(j,MetadataMapPosition)
  189.         if mapinfos[j] && mapinfos[j].name==$game_map.name &&
  190.            roamermeta && jmeta && roamermeta[0]==jmeta[0]
  191.           possiblemaps.push(j)   # Any map with same name as roamer's current map
  192.         end
  193.       end
  194.       if possiblemaps.include?(currentArea) && pbRoamingMethodAllowed(poke[3])
  195.         # Change encounter to species and level, with BGM on end
  196.         roam.push([i,species,poke[1],poke[4]])
  197.       end
  198.     end
  199.   end
  200.   if roam.length>0
  201.     rnd=rand(roam.length)
  202.     roamEncounter=roam[rnd]
  203.     $PokemonGlobal.roamEncounter=roamEncounter
  204.     $PokemonTemp.nowRoaming=true
  205.     $PokemonTemp.roamerIndex=roamEncounter[0]
  206.     if roamEncounter[3] && roamEncounter[3]!=""
  207.       $PokemonGlobal.nextBattleBGM=roamEncounter[3]
  208.     end
  209.     $PokemonTemp.forceSingleBattle = true
  210.     return [roamEncounter[1],roamEncounter[2]]
  211.   end
  212.   return encounter
  213. })
  214.  
  215. EncounterModifier.registerEncounterEnd(proc {
  216.    $PokemonTemp.nowRoaming=false
  217.    $PokemonTemp.roamerIndex=nil
  218. })
  219.  
  220. def pbRoamingMethodAllowed(enctype)
  221.   encounter=$PokemonEncounters.pbEncounterType
  222.   case enctype
  223.   when 0   # Any encounter method (except triggered ones and Bug Contest)
  224.     return true if encounter==EncounterTypes::Land
  225.     return true if encounter==EncounterTypes::LandMorning
  226.     return true if encounter==EncounterTypes::LandDay
  227.     return true if encounter==EncounterTypes::LandNight
  228.     return true if encounter==EncounterTypes::Water
  229.     return true if encounter==EncounterTypes::Cave
  230.   when 1   # Grass (except Bug Contest)/walking in caves only
  231.     return true if encounter==EncounterTypes::Land
  232.     return true if encounter==EncounterTypes::LandMorning
  233.     return true if encounter==EncounterTypes::LandDay
  234.     return true if encounter==EncounterTypes::LandNight
  235.     return true if encounter==EncounterTypes::Cave
  236.   when 2   # Surfing only
  237.     return true if encounter==EncounterTypes::Water
  238.   when 3   # Fishing only
  239.     return true if encounter==EncounterTypes::OldRod
  240.     return true if encounter==EncounterTypes::GoodRod
  241.     return true if encounter==EncounterTypes::SuperRod
  242.   when 4   # Water-based only
  243.     return true if encounter==EncounterTypes::Water
  244.     return true if encounter==EncounterTypes::OldRod
  245.     return true if encounter==EncounterTypes::GoodRod
  246.     return true if encounter==EncounterTypes::SuperRod
  247.   end
  248.   return false
  249. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement