• Just a reminder that providing specifics on, sharing links to, or naming websites where ROMs can be accessed is against the rules. If your post has any of this information it will be removed.
  • Our friends from the Johto Times are hosting a favorite Pokémon poll - and we'd love for you to participate! Click here for information on how to vote for your favorites!
  • Scottie, Todd, Serena, Kris - which Pokémon protagonist is your favorite? Let us know by voting in our poll!
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

Problem with Changing Encounters mid-game

  • 28
    Posts
    9
    Years
    • Seen May 11, 2024
    Hi Everyone. Recently, I setup my game so I could toggle a switch and change the encounters on a map mid-game. I followed this Tutorial:

    https://pokemonessentials.wikia.com/wiki/Tutorial:Changing_encounters_mid-game

    as a baseline. It didn't work at first because of version differences I'm assuming. What I ended up doing was copying the sections listed above the changed areas and renaming the files referenced to encounters_2.txt and encounters_2.dat. I placed the changed scripts below.

    Amazingly, this works for the most part. I just have two problems and I was hoping someone would have a fix.

    1) I can only change the encounters listed by using the editor in the debug menu in a test game. In other words changing the encounters_2.txt file doesn't work. In fact If I compile the game for testing, the text file will change back to whatever was listed from the editor. So does anyone know how to get the text file to change the encounters instead of the editor.

    2) in order for the switch to take effect you need to leave the map and come back. So, is there a way to refresh the map or force it to reload with an event script instead of leaving the map and coming back. I would hate to have to transfer the player out and in of the map each time the switch is toggled.

    Thanks for any help guys!

    Code:
    def pbSaveEncounterData()
      encdata=load_data("Data/encounters.dat") rescue nil
      return if !encdata
      mapinfos=pbLoadRxData("Data/MapInfos")  
      File.open("PBS/encounters.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
    
    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

    Code:
     elsif cmd=="setencounters"
          encdata=load_data("Data/encounters.dat")
          oldencdata=Marshal.dump(encdata)
          mapedited=false
          map=pbDefaultMap()
          loop do
            map=pbListScreen(_INTL("SET ENCOUNTERS"),MapLister.new(map))
            break if map<=0
            mapedited=true if map==pbDefaultMap()
            pbEncounterEditorMap(encdata,map)
          end
          save_data(encdata,"Data/encounters.dat")
          pbSaveEncounterData()
          pbClearData()
        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")
          pbSaveEncounterData2()
          pbClearData()

    Code:
      commands.add("setencounters",_INTL("Set Encounters")) 
      commands.add("setencounters2",_INTL("Set Encounters 2"))
    Code:
    [CODE]def pbCompileEncounters
      lines=[]
      linenos=[]
      FileLineData.file="PBS/encounters.txt"
      File.open("PBS/encounters.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::BugContest] ||
                         thisenc[1][EncounterTypes::LandNight]) &&
                         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.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.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.dat")
    end
    
    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::BugContest] ||
                         thisenc[1][EncounterTypes::LandNight]) &&
                         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

    Code:
      def setup(mapID)
        @density=nil
        @stepcount=0
        @enctypes=[]
        begin
          if $game_switches[169]
              data=load_data("Data/encounters_2.dat")
             elsif
              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")
            elsif
              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")
            elsif
              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
    Code:
        @point=BitmapWrapper.new(PokemonRegionMapScene::SQUAREWIDTH+4,
                                 PokemonRegionMapScene::SQUAREHEIGHT+4)
        @point.fill_rect(0,0,
                         PokemonRegionMapScene::SQUAREWIDTH+4,
                         PokemonRegionMapScene::SQUAREHEIGHT+4,Color.new(255,0,0))
        @point2=BitmapWrapper.new(PokemonRegionMapScene::SQUAREWIDTH+4,
                                  PokemonRegionMapScene::SQUAREHEIGHT+4)
        @point2.fill_rect(4,0,
                          PokemonRegionMapScene::SQUAREWIDTH,
                          PokemonRegionMapScene::SQUAREHEIGHT+4,Color.new(255,0,0))
        @point3=BitmapWrapper.new(PokemonRegionMapScene::SQUAREWIDTH+4,
                                  PokemonRegionMapScene::SQUAREHEIGHT+4)
        @point3.fill_rect(0,4,
                          PokemonRegionMapScene::SQUAREWIDTH+4,
                          PokemonRegionMapScene::SQUAREHEIGHT,Color.new(255,0,0))
        @point4=BitmapWrapper.new(PokemonRegionMapScene::SQUAREWIDTH+4,
                                  PokemonRegionMapScene::SQUAREHEIGHT+4)
        @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")
        elsif
          encdata=load_data("Data/encounters.dat")
        end
     
    You've missed a step, a valuable one...
    Code:
        # Depends on PBSpecies
        yield(_INTL("Compiling encounter data"))
        pbCompileEncounters
    Do you see what this code is doing?

    Also, I'd change all your switch calls from elsif to just else...
    Code:
        if $game_switches[169]
          ##### blah blah
        [COLOR=Red]else[/COLOR]
          ##### blah blah
        end
     
    Thanks Nick I was able to get it working right.

    However, you still have to change maps before the change takes effect. Is there a script, or command you can write into the event that will cause the encounters to reload and thus avoid the map change?

    Thanks guys.
     
    Actually that command in the event script caused the whole game to crash immediately.

    Here's the error code

    Code:
    ---------------------------
    Pokemon Perish Song
    ---------------------------
    Exception: RuntimeError
    
    Message: Script error within event 11, map 5 (Route 1):
    
    Exception: NoMethodError
    
    Message: (eval):1:in `pbExecuteScript'undefined method `map_fresh' for #<Game_Map:0xa84e3d0>
    
    ***Full script:
    
    $game_map.map_fresh
    
    
    Interpreter:243:in `pbExecuteScript'
    
    Interpreter:1606:in `eval'
    
    Interpreter:243:in `pbExecuteScript'
    
    Interpreter:1606:in `command_355'
    
    Interpreter:494:in `execute_command'
    
    Interpreter:193:in `update'
    
    Interpreter:106:in `loop'
    
    Interpreter:198:in `update'
    
    Scene_Map:103:in `update'
    
    Scene_Map:101:in `loop'
    
    
    
    Interpreter:276:in `pbExecuteScript'
    
    Interpreter:1606:in `command_355'
    
    Interpreter:494:in `execute_command'
    
    Interpreter:193:in `update'
    
    Interpreter:106:in `loop'
    
    Interpreter:198:in `update'
    
    Scene_Map:103:in `update'
    
    Scene_Map:101:in `loop'
    
    Scene_Map:114:in `update'
    
    Scene_Map:68:in `main'
    
    
    
    This exception was logged in 
    
    C:\Users\Raphael\Saved Games/Pokemon Perish Song/errorlog.txt.
    
    Press Ctrl+C to copy this message to the clipboard.
    ---------------------------
    OK   
    ---------------------------
    Okay,
    I searched for the refresh map command in debug because I know there is an option in the menu to refresh the map. I believe its $game_map.need_refresh = true , However I don't think it matters.

    I added it to the event that triggers the switch and it did absolutely nothing. I also used the option in the debug menu and again nothing. I'm guessing reloading the map only reloads the various events on the map but not the encounters. I guess what we really need here is something that reloads the encounters data So any idea where that command might be or if it even exists?

    Again thanks Nickloose for trying.
     
    Last edited by a moderator:
    Okay its finally all ready. Major Props to all the people who helped out. Here's the completed script and the proper event to get it all working.

    In your PBS file folder copy "encounters.txt" and name the new file "encounters_2.txt"
    In your Data File Folder copy "encounters.dat" and name the new file "encounters_2.dat"

    under PField_Encounters around 160:
    Code:
    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

    Under PScreen_PokedexNestForm around line 50:

    Code:
       @point=BitmapWrapper.new(PokemonRegionMapScene::SQUAREWIDTH+4,
                                 PokemonRegionMapScene::SQUAREHEIGHT+4)
        @point.fill_rect(0,0,
                         PokemonRegionMapScene::SQUAREWIDTH+4,
                         PokemonRegionMapScene::SQUAREHEIGHT+4,Color.new(255,0,0))
        @point2=BitmapWrapper.new(PokemonRegionMapScene::SQUAREWIDTH+4,
                                  PokemonRegionMapScene::SQUAREHEIGHT+4)
        @point2.fill_rect(4,0,
                          PokemonRegionMapScene::SQUAREWIDTH,
                          PokemonRegionMapScene::SQUAREHEIGHT+4,Color.new(255,0,0))
        @point3=BitmapWrapper.new(PokemonRegionMapScene::SQUAREWIDTH+4,
                                  PokemonRegionMapScene::SQUAREHEIGHT+4)
        @point3.fill_rect(0,4,
                          PokemonRegionMapScene::SQUAREWIDTH+4,
                          PokemonRegionMapScene::SQUAREHEIGHT,Color.new(255,0,0))
        @point4=BitmapWrapper.new(PokemonRegionMapScene::SQUAREWIDTH+4,
                                  PokemonRegionMapScene::SQUAREHEIGHT+4)
        @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

    Under Debug around line 270:

    Code:
      commands.add("roam",_INTL("Advance Roaming"))
      commands.add("setencounters",_INTL("Set Encounters")) 
      commands.add("setencounters2",_INTL("Set Encounters 2"))
      commands.add("setmetadata",_INTL("Set Metadata"))

    Under Debug Around line 720:
    Code:
     elsif cmd=="setencounters"
          encdata=load_data("Data/encounters.dat")
          oldencdata=Marshal.dump(encdata)
          mapedited=false
          map=pbDefaultMap()
          loop do
            map=pbListScreen(_INTL("SET ENCOUNTERS"),MapLister.new(map))
            break if map<=0
            mapedited=true if map==pbDefaultMap()
            pbEncounterEditorMap(encdata,map)
          end
          save_data(encdata,"Data/encounters.dat")
          pbSaveEncounterData()
          pbClearData()
        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")
          pbSaveEncounterData2()
          pbClearData()

    Under Editor around line 720:

    Code:
    def pbSaveEncounterData()
      encdata=load_data("Data/encounters.dat") rescue nil
      return if !encdata
      mapinfos=pbLoadRxData("Data/MapInfos")  
      File.open("PBS/encounters.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
    
    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

    Under Compiler around line 1000:

    Code:
    def pbCompileEncounters
      lines=[]
      linenos=[]
      FileLineData.file="PBS/encounters.txt"
      File.open("PBS/encounters.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::BugContest] ||
                         thisenc[1][EncounterTypes::LandNight]) &&
                         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.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.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.dat")
    end
    
    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::BugContest] ||
                         thisenc[1][EncounterTypes::LandNight]) &&
                         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

    Under Compiler around line 4160:
    Code:
     pbCompileTrainerLists
        # Depends on PBSpecies
        yield(_INTL("Compiling encounter data"))
        pbCompileEncounters
        # Depends on PBSpecies
        yield(_INTL("Compiling encounter 2 data"))
        pbCompileEncounters2
        # Depends on PBSpecies, PBMoves
        yield(_INTL("Compiling shadow move data"))
        pbCompileShadowMoves

    Create an event on the map you want to have the encounters change and on the first page
    Control Switches:[0169:Encounters 2] = ON
    $PokemonEncounters.setup($game_map.map_id)
    Control Self Switch: A =ON

    On the Second Page:
    Control Switches:[0169:Encounters 2] = OFF
    $PokemonEncounters.setup($game_map.map_id)
    Control Self Switch: A =OFF

    Make sure both pages are Player Touch. Basically, the player runs over the spot turns the switch on the encounters switch reload and then flip to the second page. Then the player runs over the spot again, switch back reload and flip to the first page.
     
    Back
    Top