Advertisement
Guest User

Random_Pokemon

a guest
Aug 13th, 2017
1,033
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 11.82 KB | None | 0 0
  1. #============================================================
  2. #Random_Pokemon script by leilou
  3. #
  4. #This script is ment to help to create a random Pokémon in Pokémon Essentials
  5. #
  6. #Pokémon Essentials is created by Poccil, based on Flameguru's
  7. #Pokémon Starter Kit and managed and updated by Maruno
  8. #
  9. #I don't claim this script to be perfect.
  10. #Please report bugs at the resources thread on Relic Castle.
  11. #=============================================================
  12. #
  13. #functions:
  14. #
  15. # getRandomPokemon(whitelist,blacklist,level,movewhitelist,moveblacklist)
  16. #   Summary:
  17. #     Returns a random Pokémon with random moves and level(optional)
  18. #   Arguments:
  19. #     whitelist:
  20. #       An array of Pokémon or nil.
  21. #       If an Array is given the script will choose a Pokémon out of the array.
  22. #       If nil is given the script will choose out of all Pokémon.
  23. #       Default: nil
  24. #     blacklist:
  25. #       An array of Pokémon or nil.
  26. #       If an Array is given the script will not choose a Pokémon out of the array.
  27. #       If nil is given the script will not blacklist any Pokémon.
  28. #       Default: nil
  29. #     level:
  30. #       An Integer or nil
  31. #       If an Integer is given the Pokémon will have that level.
  32. #       If nil is given the Pokémon will have a random level between 0 and 100.
  33. #       Default: nil
  34. #     movewhitelist:
  35. #       An array of Moves, false or nil.
  36. #       If an Array is given the Pokémon will only have moves out of this Array.
  37. #       If false is given the Pokémon will have it's natural moves(the ones a
  38. #         wild Pokémon on that level would have).
  39. #       If nil is given the script will choose out of all Moves the Pokémon can
  40. #         learn by leveling up or tm.
  41. #       Default: nil
  42. #     moveblacklist:
  43. #       An array of Moves or nil.
  44. #       If an Array is given the Pokémon will not have a Moves out of the array.
  45. #       If nil is given the script will not blacklist any Moves.
  46. #       Default: nil
  47. #   return value:
  48. #     The generated Pokémon of the type PokeBattle_Pokemon
  49. #
  50. # addPokemon(pokemon)
  51. #   Summary:
  52. #     Places the given Pokémon in the players team/storage.
  53. #     It does the same as pbAddPokemon. The difference it that it takes
  54. #     a PokeBattle_Pokemon as argument which is returned by getRandomPokemon.
  55. #   Arguments:
  56. #     pokemon:
  57. #       A PokeBattle_Pokemon object
  58. #       This is the Pokémon to be added.
  59. #   return value:
  60. #       none
  61. #
  62. # getAllPokemonList
  63. #   Summary:
  64. #     Returns a list of all Pokémon defined in the PBS file pokemon.txt
  65. #     You may want to use this method to generate a whitelist with all Pokémon.
  66. #   Arguments:
  67. #     none
  68. #   return value:
  69. #     A list of Integers that represent the dex number of all Pokémon.
  70. #
  71. # filterUnevolved(pokemonList)
  72. #   Summary:
  73. #     Returns a list of all unevolved Pokeémon in pokemonList.
  74. #   Arguments:
  75. #     pokemonList:
  76. #       An array of Integer or PBSpecies
  77. #       An array of Pokémon. This is the array to be filtered.
  78. #   return value:
  79. #     A list of Integers that represent the dex number of the unevolved Pokémon.
  80. #
  81. # filterType(pokemonList, type1, type2)
  82. #   Summary:
  83. #     Returns a list of all Pokémon in pokemonList of the given type(s).
  84. #   Arguments:
  85. #     pokemonList:
  86. #       An array of Integer or PBSpecies
  87. #       An array of Pokémon. This is the array to be filtered.
  88. #     type1:
  89. #       PBTypes
  90. #       The type to filter for.
  91. #     type2:
  92. #       PBTypes, nil or false
  93. #         When a type is given only Pokémon with the two given types are returned.
  94. #         When nil is given any Pokémon with type1 as type are returned.
  95. #         When false is fiven only Pokémon that are only type1 are returned.
  96. #   return value:
  97. #     An array of Integers that represent the dex number of the Pokémon of the
  98. #     given type(s).
  99. #
  100. # getAllPossibleMoves(pokemon)
  101. #   Summary:
  102. #     This method returns a list of the ids of all natural and tm moves the
  103. #     Pokémon can learn without taking level into account.
  104. #   Arguments:
  105. #     pokemon:
  106. #       PokeBattle_Pokemon
  107. #       The Pokémon in question.
  108. #   return value:
  109. #     An array of Integers representing the ids of all moves that can be learned
  110. #     by the Pokémon.
  111. #
  112. #=============================================================
  113.  
  114. def getRandomPokemon(whitelist = nil, blacklist = nil, level = nil,
  115.       movewhitelist = nil, moveblacklist = nil)
  116.   randomMoves = true
  117.   if movewhitelist == false
  118.     randomMoves = false
  119.   end
  120.   #make sure whitelist and blacklist are in the right format
  121.   if whitelist.is_a?(Array)
  122.     for i in 0 ... whitelist.length
  123.       if whitelist[i].is_a?(String) || whitelist[i].is_a?(Symbol)
  124.         whitelist[i]=getID(PBSpecies,whitelist[i])
  125.       end
  126.       if whitelist[i].is_a?(Integer)
  127.         const = getConstantName(PBSpecies,whitelist[i]) rescue whitelist[i] = nil
  128.         if !hasConst?(PBSpecies,const) #the pokemon doesn't exist
  129.           whitelist[i] = nil
  130.         end
  131.       else
  132.         whitelist[i] = nil
  133.       end
  134.     end
  135.     whitelist.compact!
  136.     if whitelist.length == 0
  137.       return nil #empty whitelist
  138.     end
  139.   else
  140.     whitelist = nil
  141.   end
  142.  
  143.   if !whitelist #no whitelist => choose from all pokemon
  144.     whitelist = getAllPokemonList
  145.   end
  146.  
  147.   if blacklist.is_a?(Array)
  148.     for i in 0 ... blacklist.length
  149.       if blacklist[i].is_a?(String) || blacklist[i].is_a?(Symbol)
  150.         blacklist[i]=getID(PBSpecies,blacklist[i])
  151.       end
  152.       if blacklist[i].is_a?(Integer)
  153.         const = getConstantName(PBSpecies,blacklist[i]) rescue blacklist[i] = nil
  154.         if !hasConst?(PBSpecies,const) #the pokemon doesn't exist
  155.           blacklist[i] = nil
  156.         end
  157.       else
  158.         blacklist[i] = nil
  159.       end
  160.     end
  161.     blacklist.uniq! #remove all duplicates
  162.     blacklist.compact!
  163.     if blacklist.length == 0
  164.       blacklist = nil
  165.     end
  166.   else
  167.     blacklist = nil
  168.   end
  169.  
  170.   #sort blacklist out of whitelist
  171.   if blacklist
  172.     for i in 0 ... whitelist.length
  173.       if blacklist.include?(whitelist[i])
  174.         whitelist[i] = nil
  175.       end
  176.     end
  177.     whitelist.compact!
  178.     if whitelist.length == 0
  179.       return nil #all pkmn of whitelist are in blacklist
  180.     end
  181.   end
  182.      
  183.   id = nil
  184.   #choose pokemon species
  185.   randNum = rand(whitelist.length-1)
  186.   id = whitelist[randNum]
  187.  
  188.  
  189.   #choose level if none is given
  190.   if !(level && level.is_a?(Integer))
  191.     level = rand(99) + 1
  192.   end
  193.   #create a pokemon with player as trainer and without moves
  194.   pokemon=PokeBattle_Pokemon.new(id,level,$Trainer,false)
  195.  
  196.   #randomize moves(all natural and tm moves)
  197.   if randomMoves
  198.     #format move whitelist
  199.     if movewhitelist && movewhitelist.is_a?(Array)
  200.       for i in 0 ... movewhitelist.length
  201.         if movewhitelist[i].is_a?(String) || movewhitelist[i].is_a?(Symbol)
  202.           movewhitelist[i]=getID(PBMoves,movewhitelist[i])
  203.         end
  204.         if whitelist[i].is_a?(Integer)
  205.           const = getConstantName(PBMoves,movewhitelist[i]) rescue movewhitelist[i] = nil
  206.           if !hasConst?(PBMoves,const) #the pokemon doesn't exist
  207.             movewhitelist[i] = nil
  208.           end
  209.         else
  210.           movewhitelist[i] = nil
  211.         end
  212.       end
  213.       movewhitelist.compact!
  214.     else
  215.       movewhitelist = nil
  216.     end
  217.  
  218.     #format move blacklist
  219.     if moveblacklist && moveblacklist.is_a?(Array)
  220.       for i in 0 ... moveblacklist.length
  221.         if moveblacklist[i].is_a?(String) || moveblacklist[i].is_a?(Symbol)
  222.           moveblacklist[i]=getID(PBMoves,moveblacklist[i])
  223.         end
  224.         if moveblacklist[i].is_a?(Integer)
  225.           const = getConstantName(PBMoves,moveblacklist[i]) rescue moveblacklist[i] = nil
  226.           if !hasConst?(PBMoves,const) #the pokemon doesn't exist
  227.             moveblacklist[i] = nil
  228.           end
  229.         else
  230.           moveblacklist[i] = nil
  231.         end
  232.       end
  233.       moveblacklist.compact!
  234.     else
  235.       moveblacklist = nil
  236.     end
  237.  
  238.     #if there is no whitelist make all learnable attacks the whitelist
  239.     if !movewhitelist
  240.       movewhitelist = getAllPossibleMoves(pokemon)
  241.     end
  242.    
  243.     #sort out blacklist moves of whitelist
  244.     if moveblacklist && movewhitelist
  245.       for i in 0 ... movewhitelist.length
  246.         if moveblacklist.includes?(movewhitelist[i])
  247.           movewhitelist[i] = nil
  248.         end
  249.       end
  250.       movewhitelist.compact!
  251.     end
  252.     pokemon.moves = [] #delete all moves
  253.  
  254.     4.times do
  255.       if movewhitelist.length == 0
  256.         break
  257.       end
  258.       randNum = rand(movewhitelist.length-1)
  259.       pokemon.moves.push(PBMove.new(movewhitelist[randNum]))
  260.       movewhitelist.delete_at(randNum)
  261.     end
  262.   else #pokemon learns the moves it would naturally have on this level
  263.     pokemon.resetMoves
  264.   end
  265.  
  266.   return pokemon
  267. end
  268.  
  269. def getAllPossibleMoves(pokemon)
  270.   moves=[] #all learnable moves
  271.   pbEachNaturalMove(pokemon){|move,level|
  272.      moves.push(move) if !moves.include?(move)
  273.   }
  274.   data = load_data("Data/tm.dat")
  275.   for i in 0 ... data.length
  276.     if pokemon.isCompatibleWithMove?(i)
  277.       moves.push(i) if !moves.include?(i)
  278.     end
  279.   end
  280.   return moves
  281. end
  282.  
  283. def addPokemon(pokemon)
  284.   if pbBoxesFull?
  285.     Kernel.pbMessage(_INTL("There's no more room for Pokémon!\1"))
  286.     Kernel.pbMessage(_INTL("The Pokémon Boxes are full and can't accept any more!"))
  287.     return false
  288.   end
  289.   speciesname=PBSpecies.getName(pokemon.species)
  290.   Kernel.pbMessage(_INTL("{1} obtained {2}!\\se[PokemonGet]\1",$Trainer.name,speciesname))
  291.   pbNicknameAndStore(pokemon)
  292.   pbSeenForm(pokemon)
  293. end
  294.  
  295. #returns only the unevolved pokemon in the list
  296. def filterUnevolved(pokemonList)
  297.   #break if wrong input
  298.   if !(pokemonList && pokemonList.is_a?(Array))
  299.     return
  300.   end
  301.  
  302.   for i in 0 ... pokemonList.length
  303.     #make sure everything is formatted the right way
  304.     if pokemonList[i].is_a?(String) || pokemonList[i].is_a?(Symbol)
  305.       pokemonList[i]=getID(PBSpecies,pokemonList[i])
  306.     end
  307.     if pokemonList[i].is_a?(Integer)
  308.       const = getConstantName(PBSpecies,pokemonList[i]) rescue pokemonList[i] = nil
  309.       if !hasConst?(PBSpecies,const) #the pokemon doesn't exist
  310.         pokemonList[i] = nil
  311.       end
  312.       #check if pokemon has a prevolution
  313.       if pokemonList[i] != pbGetPreviousForm(pokemonList[i])
  314.         pokemonList[i] = nil
  315.       end
  316.     else
  317.       pokemonList[i] = nil
  318.     end
  319.   end
  320.   pokemonList.compact!
  321.   return pokemonList
  322. end
  323.  
  324. def filterType(pokemonList, type1, type2=nil)
  325.   #break if wrong input
  326.   if !(pokemonList && pokemonList.is_a?(Array))
  327.     return
  328.   end
  329.  
  330.   dexdata=pbOpenDexData
  331.  
  332.   for i in 0 ... pokemonList.length
  333.     #make sure everything is formatted the right way
  334.     if pokemonList[i].is_a?(String) || pokemonList[i].is_a?(Symbol)
  335.       pokemonList[i]=getID(PBSpecies,pokemonList[i])
  336.     end
  337.     if pokemonList[i].is_a?(Integer)
  338.       const = getConstantName(PBSpecies,pokemonList[i]) rescue pokemonList[i] = nil
  339.       if !hasConst?(PBSpecies,const) #the pokemon doesn't exist
  340.         pokemonList[i] = nil
  341.       end
  342.       #check if pokemon has the requested types
  343.       pbDexDataOffset(dexdata,pokemonList[i],8)
  344.       ptype1=dexdata.fgetb
  345.       pbDexDataOffset(dexdata,pokemonList[i],9)
  346.       ptype2=dexdata.fgetb
  347.       if !(type1==ptype1||type1==ptype2)
  348.         pokemonList[i] = nil
  349.         next
  350.       elsif type2 && !(type2==ptype1||type2==ptype2)
  351.         pokemonList[i] = nil
  352.         next
  353.       elsif (type2 == false) && (ptype1 != ptype2)
  354.         pokemonList[i] = nil
  355.         next
  356.       end
  357.     else
  358.       pokemonList[i] = nil
  359.     end
  360.   end
  361.   pokemonList.compact!
  362.   dexdata.close
  363.   return pokemonList
  364. end
  365.  
  366. def getAllPokemonList
  367.   pokemonList = []
  368.   for i in 0..PBSpecies.maxValue
  369.     for c in PBSpecies.constants
  370.       if PBSpecies.const_get(c.to_sym)==i
  371.         pokemonList.push(i)
  372.       end
  373.     end
  374.   end
  375.   return pokemonList
  376. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement