FANDOM


Changing encounters mid-game
This tutorial was written by -Desbrina-

Updated to v16.2 by Vendily. Please give credit.

Click here for more tutorials!


This tutorial explains how to change the maps encounters part way through a game. It uses a switch to determine what file the encounters are loaded from.

Note: You must change map in order for the new encounters to take effect

In Editor, after the function def pbSaveEncounterData() add the following function:

def pbSaveEncounterData2()
  encdata=load_data("Data/encounters_2.dat") rescue nil
  return if !encdata
  mapinfos=pbLoadRxData("Data/MapInfos")  
  File.open("PBS/encounters_2.txt","wb"){|f|
     sortedkeys=encdata.keys.sort{|a,b| a<=>b}
     for i in sortedkeys
       if encdata[i]
         e=encdata[i]
         mapname=""
         if mapinfos[i]
           map=mapinfos[i].name
           mapname=" # #{map}"
         end
         f.write(sprintf("#########################\r\n"))
         f.write(sprintf("%03d%s\r\n",i,mapname))
         f.write(sprintf("%d,%d,%d\r\n",e[0][EncounterTypes::Land],
             e[0][EncounterTypes::Cave],e[0][EncounterTypes::Water]))
         for j in 0...e[1].length
           enc=e[1][j]
           next if !enc
           f.write(sprintf("%s\r\n",EncounterTypes::Names[j]))
           for k in 0...EncounterTypes::EnctypeChances[j].length
             encentry=(enc[k]) ? enc[k] : [1,5,5]
             species=getConstantName(PBSpecies,encentry[0]) rescue pbGetSpeciesConst(encentry[0])
             if encentry[1]==encentry[2]
               f.write(sprintf("%s,%d\r\n",species,encentry[1]))
             else
               f.write(sprintf("%s,%d,%d\r\n",species,encentry[1],encentry[2]))
             end
           end
         end
       end
     end
  }
end

In Debug, after the pbClearData() in the elsif cmd=="setencounters" section add the following

    elsif cmd=="setencounters2"
      encdata=load_data("Data/encounters_2.dat")
      oldencdata=Marshal.dump(encdata)
      mapedited=false
      map=pbDefaultMap()
      loop do
        map=pbListScreen(_INTL("SET ENCOUNTERS 2"),MapLister.new(map))
        break if map<=0
        mapedited=true if map==pbDefaultMap()
        pbEncounterEditorMap(encdata,map)
      end
      save_data(encdata,"Data/encounters_2.dat")
      pbSaveEncounterData()
      pbClearData()

Again in Debug find the commands.add("setencounters",_INTL("SET ENCOUNTERS")) line and insert the following code afterwards

  commands.add("setencounters2",_INTL("Set Encounters 2"))

In Compiler, after the pbCompileEncounters() Function add the following function

def pbCompileEncounters2
  lines=[]
  linenos=[]
  FileLineData.file="PBS/encounters_2.txt"
  File.open("PBS/encounters_2.txt","rb"){|f|
     lineno=1
     f.each_line {|line|
        if lineno==1 && line[0]==0xEF && line[1]==0xBB && line[2]==0xBF
          line=line[3,line.length-3]
        end
        line=prepline(line)
        if line.length!=0
          lines[lines.length]=line
          linenos[linenos.length]=lineno
        end
        lineno+=1
     }
  }
  encounters={}
  thisenc=nil
  lastenc=-1
  lastenclen=0
  needdensity=false
  lastmapid=-1
  i=0;
  while i<lines.length
    line=lines[i]
    FileLineData.setLine(line,linenos[i])
    mapid=line[/^\d+$/]
    if mapid
      lastmapid=mapid
      if thisenc && (thisenc[1][EncounterTypes::Land] ||
                     thisenc[1][EncounterTypes::LandMorning] ||
                     thisenc[1][EncounterTypes::LandDay] ||
                     thisenc[1][EncounterTypes::LandNight] ||
                     thisenc[1][EncounterTypes::BugContest]) &&
                     thisenc[1][EncounterTypes::Cave]
        raise _INTL("Can't define both Land and Cave encounters in the same area (map ID {1})",mapid)
      end
      thisenc=[EncounterTypes::EnctypeDensities.clone,[]]
      encounters[mapid.to_i]=thisenc
      needdensity=true
      i+=1
      next
    end
    enc=findIndex(EncounterTypes::Names){|val| val==line}
    if enc>=0
      needdensity=false
      enclines=EncounterTypes::EnctypeChances[enc].length
      encarray=[]
      j=i+1; k=0
      while j<lines.length && k<enclines
        line=lines[j]
        FileLineData.setLine(lines[j],linenos[j])
        splitarr=strsplit(line,/\s*,\s*/)
        if !splitarr || splitarr.length<2
          raise _INTL("In encounters_2.txt, expected a species entry line,\r\ngot \"{1}\" instead (probably too few entries in an encounter type).\r\nPlease check the format of the section numbered {2},\r\nwhich is just before this line.\r\n{3}",
             line,lastmapid,FileLineData.linereport)
        end
        splitarr[2]=splitarr[1] if splitarr.length==2
        splitarr[1]=splitarr[1].to_i
        splitarr[2]=splitarr[2].to_i
        maxlevel=PBExperience::MAXLEVEL
        if splitarr[1]<=0 || splitarr[1]>maxlevel
          raise _INTL("Level number is not valid: {1}\r\n{2}",splitarr[1],FileLineData.linereport)
        end
        if splitarr[2]<=0 || splitarr[2]>maxlevel
          raise _INTL("Level number is not valid: {1}\r\n{2}",splitarr[2],FileLineData.linereport)
        end
        if splitarr[1]>splitarr[2]
          raise _INTL("Minimum level is greater than maximum level: {1}\r\n{2}",line,FileLineData.linereport)
        end
        splitarr[0]=parseSpecies(splitarr[0])
        linearr=splitarr
        encarray.push(linearr)
        thisenc[1][enc]=encarray
        j+=1
        k+=1
      end
      if j==lines.length && k<enclines
         raise _INTL("Reached end of file unexpectedly. There were too few entries in the last section, expected {1} entries.\r\nPlease check the format of the section numbered {2}.\r\n{3}",
            enclines,lastmapid,FileLineData.linereport)
      end
      i=j
    elsif needdensity
      needdensity=false
      nums=strsplit(line,/,/)
      if nums && nums.length>=3
        for j in 0...EncounterTypes::EnctypeChances.length
          next if !EncounterTypes::EnctypeChances[j] ||
                  EncounterTypes::EnctypeChances[j].length==0
          next if EncounterTypes::EnctypeCompileDens[j]==0
          thisenc[0][j]=nums[EncounterTypes::EnctypeCompileDens[j]-1].to_i
        end
      else
        raise _INTL("Wrong syntax for densities in encounters_2.txt; got \"{1}\"\r\n{2}",line,FileLineData.linereport)
      end
      i+=1
    else
      raise _INTL("Undefined encounter type {1}, expected one of the following:\r\n{2}\r\n{3}",
         line,EncounterTypes::Names.inspect,FileLineData.linereport)
    end
  end
  save_data(encounters,"Data/encounters_2.dat")
end

Still in Compiler, Search for "pbCompileEncounters" and add the following

    yield(_INTL("Compiling encounter 2 data"))
    pbCompileEncounters2

Last Change you need to do in Compiler: Find a few lines below

begin
  if $DEBUG
    datafiles=["attacksRS.dat",

Add after "encounters.dat", the following

               "encounters_2.dat",

And after "encounter.txt",

               "encounters_2.txt",

In PField_Encounters, at around line 129 change to the code in yellow

  def setup(mapID)
    @density=nil
    @stepcount=0
    @enctypes=[]
    begin
    if $game_switches[169]
       data=load_data("Data/encounters_2.dat")
     else
       data=load_data("Data/encounters.dat")
     end
      if data.is_a?(Hash) && data[mapID]
        @density=data[mapID][0]
        @enctypes=data[mapID][1]
      else
        @density=nil
        @enctypes=[]
      end
    rescue
      @density=nil
      @enctypes=[]
    end
  end

  def pbMapHasEncounter?(mapID,enctype)
    if $game_switches[169]
       data=load_data("Data/encounters_2.dat")
     else
       data=load_data("Data/encounters.dat")
     end
    if data.is_a?(Hash) && data[mapID]
      enctypes=data[mapID][1]
      density=data[mapID][0]
    else
      return false
    end
    return false if density==nil || enctype<0
    return enctypes[enctype] ? true : false  
  end

  def pbMapEncounter(mapID,enctype)
    if enctype<0 || enctype>EncounterTypes::EnctypeChances.length
      raise ArgumentError.new(_INTL("Encounter type out of range"))
    end
    if $game_switches[169]
       data=load_data("Data/encounters_2.dat")
     else
       data=load_data("Data/encounters.dat")
     end
    if data.is_a?(Hash) && data[mapID]
      enctypes=data[mapID][1]
    else
      return nil
    end
    return nil if enctypes[enctype]==nil
    chances=EncounterTypes::EnctypeChances[enctype]
    chancetotal=0
    chances.each {|a| chancetotal+=a}
    rnd=rand(chancetotal)
    chosenpkmn=0
    chance=0
    for i in 0...chances.length
      chance+=chances[i]
      if rnd<chance
        chosenpkmn=i
        break
      end
    end
    encounter=enctypes[enctype][chosenpkmn]
    level=encounter[1]+rand(1+encounter[2]-encounter[1])
    return [encounter[0],level]
  end

In PScreen_PokedexNestForm, at around line 66 change to the code in yellow

    @point4.fill_rect(4,4,
                      PokemonRegionMapScene::SQUAREWIDTH,
                      PokemonRegionMapScene::SQUAREHEIGHT,Color.new(255,0,0))
    if $game_switches[169]
       encdata=load_data("Data/encounters_2.dat")
     else
       encdata=load_data("Data/encounters.dat")
     end
    points=[]

Finally, in PScreen_Phone, in function pbEncounterSpecies, add the code in yellow.

def pbEncounterSpecies(phonenum)
  return "" if !phonenum[6] || phonenum[6]==0
  begin
    if $game_switches[169]
       data=load_data("Data/encounters_2.dat")
     else
       data=load_data("Data/encounters.dat")
     end
    return "" if !data
    enctypes=data[phonenum[6]][1]
  rescue
    return ""
  end
  species=pbRandomEncounterSpecies(enctypes[0]) # Land
  species=pbRandomEncounterSpecies(enctypes[1]) if species==0 # Cave
  species=pbRandomEncounterSpecies(enctypes[9]) if species==0 # LandMorning
  species=pbRandomEncounterSpecies(enctypes[10]) if species==0 # LandDay
  species=pbRandomEncounterSpecies(enctypes[11]) if species==0 # LandNight
  species=pbRandomEncounterSpecies(enctypes[2]) if species==0 # Water
  return "" if species==0
  return PBSpecies.getName(species)
end

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.