FANDOM


Edit TMs in pokemon.txt
This tutorial was written by -FL-. Please give credit.

This tutorial is related to the following pages:

Click here for more tutorials!


#===============================================================================
# * Edit TMs in pokemon.txt - by FL (Credits will be apreciated)
#===============================================================================
#
# This script is for Pokémon Essentials. It allows exporting all TM data into
# pokémon PBS and vice-versa, so you can edit TMs on pokemon.txt and export
# this data back to tm PBS files. 
#
# Every time before something is exported, a backup is created. Take note that
# only tm.txt is used actually by game.
#
#===============================================================================
#
# To this scripts works, put it above main.
#
# -At PokemonDebug, put the line
# 'commands.add("tmpbs",_INTL("TM PBS Options..."))' at someplace in
# pbDebugMenu. I suggest after line 
# 'commands.add("toggleinternal",_INTL("Toggle Internal Flag"))', so it will
# be the last option on the list.
#
# -Before line 'elsif cmd=="quickhatch"' add 'elsif cmd=="tmpbs";tmpbsOptions'.
#
# -At PokemonEditor, after line 
# 'eggEmerald=File.open("Data/eggEmerald.dat","rb")' add line
# 'tmpbs=File.open("Data/tmpbs.dat","rb")'. 
#
# -After line 'eggEmerald.close' add line 'tmpbs.close'.
#
# -Before line 'pokedata.write("Compatibility=#{compat1},#{compat2}\r\n")' add:
#
#  tmpbs.pos=(i-1)*8
#  offset=tmpbs.fgetdw
#  length=tmpbs.fgetdw
#  if length>0
#    pokedata.write("TMPBS=")
#    tmpbs.pos=offset
#    first=true
#    j=0; loop do break unless j<length
#      atk=tmpbs.fgetw
#      pokedata.write(",") if !first
#      break if atk==0
#      if atk>0
#        cmove=getConstantName(PBMoves,atk) rescue pbGetMoveConst(atk)
#        pokedata.write("#{cmove}")
#        first=false
#      end
#      j+=1
#    end
#    pokedata.write("\r\n")
#  end
#
# -At Compiler, after line '"EggMoves"=>[0,"*E",PBMoves],' add line 
# '"TMPBS"=>[0,"*E",PBMoves],'. After line 'eggmoves=[]' add line 'tmpbs=[]'.
#
# -After line 'eggmoves[currentmap].push(value)' add:
#
#  elsif key=="TMPBS"
#    tmpbs[currentmap]=[] if !tmpbs[currentmap]
#    tmpbs[currentmap].push(value)'.
#
# -Before line 'MessageTypes.setMessages(MessageTypes::Species,speciesnames)'
# add:
#
#  File.open("Data/tmpbs.dat","wb"){|f|
#    mx=[maxValue,tmpbs.length-1].max
#    offset=mx*8
#    for i in 1..mx
#      f.fputdw(offset)
#      f.fputdw(tmpbs[i] ? tmpbs[i].length : 0)
#      offset+=tmpbs[i] ? tmpbs[i].length*2 : 0
#    end
#    for i in 1..mx
#      next if !tmpbs[i]
#      for j in tmpbs[i]
#        f.fputw(j)
#      end
#    end
#  }
#
# If you save something that changes pokémon in the external editor, 
# THE TMPBS DATA ON POKEMON PBS WILL BE ERASED! In order for avoid this, you 
# can open the editor scripts (rename EditorScripts.rxdata to Scripts.rxdata
# and open using RPG Maker XP editor) and do the same changes at
# PokemonEditor/Compiler sections.
#
# You call the export function from the debug menu. When using as first time,
# export all tm PBS data into pokemon PBS choosing the first option.
#
# Before you compress your game, you can delete the tmpbs.dat file on 
# Data folder.
#
#===============================================================================

# Make a backup (.bak) every time before exporting the files.
# Turning it off is not recommended.
TMPBS_BACKUP = true 

# Sort TMs alphabetically when exporting into pokemon.txt.
# When false it uses the move index number
TMPBS_ORDER_POKEMON = true

# Sort TMs alphabetically when exporting into tm.txt.
# when false it uses the move index number
TMPBS_ORDER_TM = true

def tmpbsOptions
  tmpbscmd=0
  loop do
    tmpbscmds=[
      _INTL("pokemon <- tm"),
      _INTL("pokemon -> tm"),
    ]
    tmpbscmd=Kernel.pbShowCommands(nil,tmpbscmds,-1,tmpbscmd)
    break if tmpbscmd<0
    case tmpbscmd
      when 0 # To pokemon.txt
        if Kernel.pbConfirmMessage(_INTL("Are you sure?"))
          backupName = ""
          backupName = tmpbsBackup("pokemon") if TMPBS_BACKUP
          movesBySpeciesArray=[]
          (PBSpecies.maxValue+1).times do
            movesBySpeciesArray.push([])
          end
          tmData=load_data("Data/tm.dat")
          for move in 0...tmData.size
            next if !tmData[move]
            for species in tmData[move]
              movesBySpeciesArray[species].push(move)
            end
          end
          if TMPBS_ORDER_POKEMON
            for speciesMoves in movesBySpeciesArray
              speciesMoves.sort! {
                  |x,y| PBMoves.getName(x) <=> PBMoves.getName(y) }
            end
          end  
          File.open("Data/tmpbs.dat","wb"){|f|
            mx=movesBySpeciesArray.length-1
            offset=mx*8
            for i in 1..mx
              f.fputdw(offset)
              f.fputdw(movesBySpeciesArray[i] ? 
                  movesBySpeciesArray[i].length : 0)
              offset+=movesBySpeciesArray[i] ?
                  movesBySpeciesArray[i].length*2 : 0
            end
            for i in 1..mx
              next if !movesBySpeciesArray[i]
              for j in movesBySpeciesArray[i]
                f.fputw(j)
              end
            end
          }
          pbSavePokemonData # Saves tmpbs.dat in pokemon.txt
          Kernel.pbMessage("Backup #{backupName} created.") if TMPBS_BACKUP
          Kernel.pbMessage(
            "For using pokemon.txt, please recompile the game.")
        end
      when 1 # To tm.txt
        if Kernel.pbConfirmMessage(_INTL("Are you sure?"))
          backupName = ""
          backupName = tmpbsBackup("tm") if TMPBS_BACKUP
          speciesByMovesArray=[]
          (PBMoves.maxValue+1).times do
            speciesByMovesArray.push([])
          end
          pbRgssOpen("Data/tmpbs.dat","rb"){|f|
            for species in 1...PBSpecies.maxValue
              f.pos=(species-1)*8
              offset=f.fgetdw
              length=f.fgetdw
              if length>0
                f.pos=offset
                length.times do
                  atk=f.fgetw
                  speciesByMovesArray[atk].push(species)
                end
              end
            end
          }
          orderArray=[] # Used for sorting the names
          for i in 0...speciesByMovesArray.length
            orderArray[i]=i
          end  
          if TMPBS_ORDER_TM 
            orderArray.sort!{|x,y| PBMoves.getName(x) <=> PBMoves.getName(y)}
          end
          File.open("PBS/tm.txt","wb"){|f|
            for i in 1...speciesByMovesArray.length
              Graphics.update
              Win32API.SetWindowText(_INTL("Processing moves {1}...",i))
              move = orderArray[i]
              next if (!speciesByMovesArray[move] || 
                  speciesByMovesArray[move].empty?)
              movename=getConstantName(PBMoves,move
                  ) rescue pbGetMoveConst(move) rescue nil
              next if !movename || movename==""
              f.write("\#-------------------\r\n")
              f.write(sprintf("[%s]\r\n",movename))
              x=[]
              for species in 0...speciesByMovesArray[move].length
                speciesname=getConstantName(PBSpecies,speciesByMovesArray[
                    move][species]) rescue pbGetSpeciesConst(
                    speciesByMovesArray[move][species]) rescue nil
                next if !speciesname || speciesname==""
                x.push(speciesname)
              end
              f.write(x.join(",")+"\r\n")
            end
          }
          Kernel.pbMessage("Backup #{backupName} created.") if TMPBS_BACKUP
          Kernel.pbMessage("For using tm.txt, please recompile the game.")
        end
    end  
  end
end

def tmpbsBackup(pbsName)
  timeFormat = "%Y%m%d%H%M%S"
  timeFormatted = Time.new.strftime(timeFormat)
  backupName = "#{pbsName}_#{timeFormatted}.bak"
  File.open("PBS/#{pbsName}.txt","rb"){|r|
    File.open("PBS/#{backupName}","wb"){|w|
      while s = r.read(4096)
        w.write s
      end
    }
  }
  return backupName
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.