Advertisement
TechSkylander1518

PField_Battles

Jul 14th, 2022
1,139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 17.31 KB | None | 0 0
  1. #===============================================================================
  2. # Battle preparation
  3. #===============================================================================
  4. class PokemonTemp
  5.   attr_accessor :encounterType
  6.   attr_accessor :evolutionLevels
  7. end
  8.  
  9.  
  10.  
  11. def pbPrepareBattle(battle)
  12.   case $game_screen.weather_type
  13.   when PBFieldWeather::Rain, PBFieldWeather::HeavyRain, PBFieldWeather::Storm
  14.     battle.weather         = PBWeather::RAINDANCE
  15.     battle.weatherduration = -1
  16.   when PBFieldWeather::Snow, PBFieldWeather::Blizzard
  17.     battle.weather         = PBWeather::HAIL
  18.     battle.weatherduration = -1
  19.   when PBFieldWeather::Sandstorm
  20.     battle.weather         = PBWeather::SANDSTORM
  21.     battle.weatherduration = -1
  22.   when PBFieldWeather::Sun
  23.     battle.weather         = PBWeather::SUNNYDAY
  24.     battle.weatherduration = -1
  25.   end
  26.   battle.shiftStyle  = ($PokemonSystem.battlestyle==0)
  27.   battle.battlescene = ($PokemonSystem.battlescene==0)
  28.   battle.environment = pbGetEnvironment
  29. end
  30.  
  31. def pbGetEnvironment
  32.   return PBEnvironment::None if !$game_map
  33.   if $PokemonGlobal && $PokemonGlobal.diving
  34.     return PBEnvironment::Underwater
  35.   elsif $PokemonEncounters && $PokemonEncounters.isCave?
  36.     return PBEnvironment::Cave
  37.   elsif !pbGetMetadata($game_map.map_id,MetadataOutdoor)
  38.     return PBEnvironment::None
  39.   else
  40.     case $game_player.terrain_tag
  41.     when PBTerrain::Grass;      return PBEnvironment::Grass       # Normal grass
  42.     when PBTerrain::Sand;       return PBEnvironment::Sand
  43.     when PBTerrain::Rock;       return PBEnvironment::Rock
  44.     when PBTerrain::DeepWater;  return PBEnvironment::MovingWater
  45.     when PBTerrain::StillWater; return PBEnvironment::StillWater
  46.     when PBTerrain::Water;      return PBEnvironment::MovingWater
  47.     when PBTerrain::TallGrass;  return PBEnvironment::TallGrass   # Tall grass
  48.     when PBTerrain::SootGrass;  return PBEnvironment::Grass       # Sooty tall grass
  49.     when PBTerrain::Puddle;     return PBEnvironment::StillWater
  50.     end
  51.   end
  52.   return PBEnvironment::None
  53. end
  54.  
  55. Events.onStartBattle+=proc {|sender,e|
  56.   $PokemonTemp.evolutionLevels = []
  57.   for i in 0...$Trainer.party.length
  58.     $PokemonTemp.evolutionLevels[i] = $Trainer.party[i].level
  59.   end
  60. }
  61.  
  62.  
  63.  
  64. #===============================================================================
  65. # Start a single wild battle
  66. #===============================================================================
  67. def pbWildBattle(species,level,variable=nil,canescape=true,canlose=false)
  68.   if (Input.press?(Input::CTRL) && $DEBUG) || $Trainer.pokemonCount==0
  69.     if $Trainer.pokemonCount>0
  70.       Kernel.pbMessage(_INTL("SKIPPING BATTLE..."))
  71.     end
  72.     pbSet(variable,1)
  73.     $PokemonGlobal.nextBattleBGM  = nil
  74.     $PokemonGlobal.nextBattleME   = nil
  75.     $PokemonGlobal.nextBattleBack = nil
  76.     return true
  77.   end
  78.   if species.is_a?(String) || species.is_a?(Symbol)
  79.     species = getID(PBSpecies,species)
  80.   end
  81.   handled = [nil]
  82.   Events.onWildBattleOverride.trigger(nil,species,level,handled)
  83.   return handled[0] if handled[0]!=nil
  84.   currentlevels = []
  85.   for i in $Trainer.party
  86.     currentlevels.push(i.level)
  87.   end
  88.   genwildpoke = pbGenerateWildPokemon(species,level)
  89.   Events.onStartBattle.trigger(nil,genwildpoke)
  90.   scene = pbNewBattleScene
  91.   $battle = PokeBattle_Battle.new(scene,$Trainer.party,[genwildpoke],$Trainer,nil)
  92.   $battle.internalbattle = true
  93.   $battle.cantescape     = !canescape
  94.   pbPrepareBattle($battle)
  95.   decision = 0
  96.   pbBattleAnimation(pbGetWildBattleBGM(species),0,[genwildpoke]) {
  97.     pbSceneStandby {
  98.       decision = $battle.pbStartBattle(canlose)
  99.     }
  100.     pbAfterBattle(decision,canlose)
  101.   }
  102.   Input.update
  103.   pbSet(variable,decision)
  104.   Events.onWildBattleEnd.trigger(nil,species,level,decision)
  105.   return (decision!=2)
  106. end
  107.  
  108.  
  109.  
  110. #===============================================================================
  111. # Start a double wild battle
  112. #===============================================================================
  113. def pbDoubleWildBattle(species1,level1,species2,level2,variable=nil,canescape=true,canlose=false)
  114.   if (Input.press?(Input::CTRL) && $DEBUG) || $Trainer.pokemonCount==0
  115.     if $Trainer.pokemonCount>0
  116.       Kernel.pbMessage(_INTL("SKIPPING BATTLE..."))
  117.     end
  118.     pbSet(variable,1)
  119.     $PokemonGlobal.nextBattleBGM  = nil
  120.     $PokemonGlobal.nextBattleME   = nil
  121.     $PokemonGlobal.nextBattleBack = nil
  122.     return true
  123.   end
  124.   if species1.is_a?(String) || species1.is_a?(Symbol)
  125.     species1 = getID(PBSpecies,species1)
  126.   end
  127.   if species2.is_a?(String) || species2.is_a?(Symbol)
  128.     species2 = getID(PBSpecies,species2)
  129.   end
  130.   currentlevels = []
  131.   for i in $Trainer.party
  132.     currentlevels.push(i.level)
  133.   end
  134.   genwildpoke  = pbGenerateWildPokemon(species1,level1)
  135.   genwildpoke2 = pbGenerateWildPokemon(species2,level2)
  136.   Events.onStartBattle.trigger(nil,genwildpoke)
  137.   scene = pbNewBattleScene
  138.   if $PokemonGlobal.partner
  139.     othertrainer = PokeBattle_Trainer.new($PokemonGlobal.partner[1],$PokemonGlobal.partner[0])
  140.     othertrainer.id    = $PokemonGlobal.partner[2]
  141.     othertrainer.party = $PokemonGlobal.partner[3]
  142.     combinedParty = []
  143.     for i in 0...$Trainer.party.length
  144.       combinedParty[i] = $Trainer.party[i]
  145.     end
  146.     for i in 0...othertrainer.party.length
  147.       combinedParty[6+i] = othertrainer.party[i]
  148.     end
  149.     $battle = PokeBattle_Battle.new(scene,combinedParty,[genwildpoke,genwildpoke2],[$Trainer,othertrainer],nil)
  150.     $battle.fullparty1 = true
  151.   else
  152.     $battle = PokeBattle_Battle.new(scene,$Trainer.party,[genwildpoke,genwildpoke2],$Trainer,nil)
  153.     $battle.fullparty1 = false
  154.   end
  155.   $battle.internalbattle = true
  156.   $battle.doublebattle   = battle.pbDoubleBattleAllowed?()
  157.   $battle.cantescape     = !canescape
  158.   pbPrepareBattle($battle)
  159.   decision = 0
  160.   pbBattleAnimation(pbGetWildBattleBGM(species1),2,[genwildpoke,genwildpoke2]) {
  161.     pbSceneStandby {
  162.       decision = $battle.pbStartBattle(canlose)
  163.     }
  164.     pbAfterBattle(decision,canlose)
  165.   }
  166.   Input.update
  167.   pbSet(variable,decision)
  168.   return (decision!=2 && decision!=5)
  169. end
  170.  
  171.  
  172.  
  173. #===============================================================================
  174. # Start a trainer battle against one trainer
  175. #===============================================================================
  176. def pbTrainerBattle(trainerid,trainername,endspeech,
  177.                     doublebattle=false,trainerparty=0,canlose=false,variable=nil)
  178.   if $Trainer.pokemonCount==0
  179.     Kernel.pbMessage(_INTL("SKIPPING BATTLE...")) if $DEBUG
  180.     return false
  181.   end
  182.   if !$PokemonTemp.waitingTrainer && pbMapInterpreterRunning? &&
  183.      ($Trainer.ablePokemonCount>1 || $Trainer.ablePokemonCount>0 && $PokemonGlobal.partner)
  184.     thisEvent = pbMapInterpreter.get_character(0)
  185.     triggeredEvents = $game_player.pbTriggeredTrainerEvents([2],false)
  186.     otherEvent = []
  187.     for i in triggeredEvents
  188.       if i.id!=thisEvent.id && !$game_self_switches[[$game_map.map_id,i.id,"A"]]
  189.         otherEvent.push(i)
  190.       end
  191.     end
  192.     if otherEvent.length==1
  193.       trainer = pbLoadTrainer(trainerid,trainername,trainerparty)
  194.       Events.onTrainerPartyLoad.trigger(nil,trainer)
  195.       if !trainer
  196.         pbMissingTrainer(trainerid,trainername,trainerparty)
  197.         return false
  198.       end
  199.       if trainer[2].length<=6
  200.         $PokemonTemp.waitingTrainer=[trainer,thisEvent.id,endspeech]
  201.         return false
  202.       end
  203.     end
  204.   end
  205.   trainer = pbLoadTrainer(trainerid,trainername,trainerparty)
  206.   Events.onTrainerPartyLoad.trigger(nil,trainer)
  207.   if !trainer
  208.     pbMissingTrainer(trainerid,trainername,trainerparty)
  209.     return false
  210.   end
  211.   if $PokemonGlobal.partner && ($PokemonTemp.waitingTrainer || doublebattle)
  212.     othertrainer = PokeBattle_Trainer.new($PokemonGlobal.partner[1],$PokemonGlobal.partner[0])
  213.     othertrainer.id    = $PokemonGlobal.partner[2]
  214.     othertrainer.party = $PokemonGlobal.partner[3]
  215.     playerparty = []
  216.     for i in 0...$Trainer.party.length
  217.       playerparty[i] = $Trainer.party[i]
  218.     end
  219.     for i in 0...othertrainer.party.length
  220.       playerparty[6+i] = othertrainer.party[i]
  221.     end
  222.     playertrainer = [$Trainer,othertrainer]
  223.     fullparty1    = true
  224.     doublebattle  = true
  225.   else
  226.     playerparty   = $Trainer.party
  227.     playertrainer = $Trainer
  228.     fullparty1    = false
  229.   end
  230.   if $PokemonTemp.waitingTrainer
  231.     combinedParty = []
  232.     fullparty2 = false
  233.     if $PokemonTemp.waitingTrainer[0][2].length>3 || trainer[2].length>3
  234.       for i in 0...$PokemonTemp.waitingTrainer[0][2].length
  235.         combinedParty[i] = $PokemonTemp.waitingTrainer[0][2][i]
  236.       end
  237.       for i in 0...trainer[2].length
  238.         combinedParty[6+i] = trainer[2][i]
  239.       end
  240.       fullparty2 = true
  241.     else
  242.       for i in 0...$PokemonTemp.waitingTrainer[0][2].length
  243.         combinedParty[i] = $PokemonTemp.waitingTrainer[0][2][i]
  244.       end
  245.       for i in 0...trainer[2].length
  246.         combinedParty[3+i] = trainer[2][i]
  247.       end
  248.     end
  249.     scene = pbNewBattleScene
  250.     $battle = PokeBattle_Battle.new(scene,playerparty,combinedParty,playertrainer,
  251.                                    [$PokemonTemp.waitingTrainer[0][0],trainer[0]])
  252.     $battle.fullparty1   = fullparty1
  253.     $battle.fullparty2   = fullparty2
  254.     $battle.doublebattle = $battle.pbDoubleBattleAllowed?
  255.     $battle.endspeech    = $PokemonTemp.waitingTrainer[2]
  256.     $battle.endspeech2   = endspeech
  257.     $battle.items        = [$PokemonTemp.waitingTrainer[0][1],trainer[1]]
  258.     trainerbgm = pbGetTrainerBattleBGM([$PokemonTemp.waitingTrainer[0][0],trainer[0]])
  259.   else
  260.     scene = pbNewBattleScene
  261.     $battle = PokeBattle_Battle.new(scene,playerparty,trainer[2],playertrainer,trainer[0])
  262.     $battle.fullparty1   = fullparty1
  263.     $battle.doublebattle = (doublebattle) ? $battle.pbDoubleBattleAllowed? : false
  264.     $battle.endspeech    = endspeech
  265.     $battle.items        = trainer[1]
  266.     trainerbgm = pbGetTrainerBattleBGM(trainer[0])
  267.   end
  268.   if Input.press?(Input::CTRL) && $DEBUG
  269.     Kernel.pbMessage(_INTL("SKIPPING BATTLE..."))
  270.     Kernel.pbMessage(_INTL("AFTER LOSING..."))
  271.     Kernel.pbMessage($battle.endspeech)
  272.     Kernel.pbMessage($battle.endspeech2) if $battle.endspeech2
  273.     if $PokemonTemp.waitingTrainer
  274.       pbMapInterpreter.pbSetSelfSwitch($PokemonTemp.waitingTrainer[1],"A",true)
  275.       $PokemonTemp.waitingTrainer = nil
  276.     end
  277.     return true
  278.   end
  279.   Events.onStartBattle.trigger(nil,nil)
  280.   $battle.internalbattle = true
  281.   pbPrepareBattle($battle)
  282.   restorebgm = true
  283.   decision = 0
  284.   Audio.me_stop
  285.   tr = [trainer]; tr.push($PokemonTemp.waitingTrainer[0]) if $PokemonTemp.waitingTrainer
  286.   pbBattleAnimation(trainerbgm,($battle.doublebattle) ? 3 : 1,tr) {
  287.     pbSceneStandby {
  288.       decision = $battle.pbStartBattle(canlose)
  289.     }
  290.     pbAfterBattle(decision,canlose)
  291.     if decision==1
  292.       if $PokemonTemp.waitingTrainer
  293.         pbMapInterpreter.pbSetSelfSwitch($PokemonTemp.waitingTrainer[1],"A",true)
  294.       end
  295.     end
  296.   }
  297.   Input.update
  298.   pbSet(variable,decision)
  299.   $PokemonTemp.waitingTrainer = nil
  300.   return (decision==1)
  301. end
  302.  
  303.  
  304.  
  305. #===============================================================================
  306. # Start a trainer battle against two trainers
  307. #===============================================================================
  308. def pbDoubleTrainerBattle(trainerid1, trainername1, trainerparty1, endspeech1,
  309.                           trainerid2, trainername2, trainerparty2, endspeech2,
  310.                           canlose=false,variable=nil)
  311.   trainer1 = pbLoadTrainer(trainerid1,trainername1,trainerparty1)
  312.   Events.onTrainerPartyLoad.trigger(nil,trainer1)
  313.   if !trainer1
  314.     pbMissingTrainer(trainerid1,trainername1,trainerparty1)
  315.   end
  316.   trainer2 = pbLoadTrainer(trainerid2,trainername2,trainerparty2)
  317.   Events.onTrainerPartyLoad.trigger(nil,trainer2)
  318.   if !trainer2
  319.     pbMissingTrainer(trainerid2,trainername2,trainerparty2)
  320.   end
  321.   if !trainer1 || !trainer2
  322.     return false
  323.   end
  324.   if $PokemonGlobal.partner
  325.     othertrainer = PokeBattle_Trainer.new($PokemonGlobal.partner[1],$PokemonGlobal.partner[0])
  326.     othertrainer.id    = $PokemonGlobal.partner[2]
  327.     othertrainer.party = $PokemonGlobal.partner[3]
  328.     playerparty = []
  329.     for i in 0...$Trainer.party.length
  330.       playerparty[i] = $Trainer.party[i]
  331.     end
  332.     for i in 0...othertrainer.party.length
  333.       playerparty[6+i] = othertrainer.party[i]
  334.     end
  335.     playertrainer = [$Trainer,othertrainer]
  336.     fullparty1    = true
  337.   else
  338.     playerparty   = $Trainer.party
  339.     playertrainer = $Trainer
  340.     fullparty1    = false
  341.   end
  342.   combinedParty = []
  343.   for i in 0...trainer1[2].length
  344.     combinedParty[i] = trainer1[2][i]
  345.   end
  346.   for i in 0...trainer2[2].length
  347.     combinedParty[6+i] = trainer2[2][i]
  348.   end
  349.   scene = pbNewBattleScene
  350.   $battle = PokeBattle_Battle.new(scene,playerparty,combinedParty,playertrainer,
  351.                                  [trainer1[0],trainer2[0]])
  352.   $battle.fullparty1   = fullparty1
  353.   $battle.fullparty2   = true
  354.   $battle.doublebattle = $battle.pbDoubleBattleAllowed?()
  355.   $battle.endspeech    = endspeech1
  356.   $battle.endspeech2   = endspeech2
  357.   $battle.items        = [trainer1[1],trainer2[1]]
  358.   trainerbgm = pbGetTrainerBattleBGM([trainer1[0],trainer2[0]])
  359.   if Input.press?(Input::CTRL) && $DEBUG
  360.     Kernel.pbMessage(_INTL("SKIPPING BATTLE..."))
  361.     Kernel.pbMessage(_INTL("AFTER LOSING..."))
  362.     Kernel.pbMessage($battle.endspeech)
  363.     Kernel.pbMessage($battle.endspeech2) if $battle.endspeech2 && $battle.endspeech2!=""
  364.     return true
  365.   end
  366.   Events.onStartBattle.trigger(nil,nil)
  367.   $battle.internalbattle = true
  368.   pbPrepareBattle($battle)
  369.   restorebgm = true
  370.   decision = 0
  371.   pbBattleAnimation(trainerbgm,($battle.doublebattle) ? 3 : 1,[trainer1,trainer2]) {
  372.     pbSceneStandby {
  373.        decision = $battle.pbStartBattle(canlose)
  374.     }
  375.     pbAfterBattle(decision,canlose)
  376.   }
  377.   Input.update
  378.   pbSet(variable,decision)
  379.   return (decision==1)
  380. end
  381.  
  382.  
  383.  
  384. #===============================================================================
  385. # After battles
  386. #===============================================================================
  387. def pbAfterBattle(decision,canlose)
  388.   for i in $Trainer.party
  389.     (i.makeUnmega rescue nil); (i.makeUnprimal rescue nil)
  390.   end
  391.   if $PokemonGlobal.partner
  392.     pbHealAll
  393.     for i in $PokemonGlobal.partner[3]
  394.       i.heal
  395.       (i.makeUnmega rescue nil); (i.makeUnprimal rescue nil)
  396.     end
  397.   end
  398.   if decision==2 || decision==5 # if loss or draw
  399.     if canlose
  400.       for i in $Trainer.party; i.heal; end
  401.       for i in 0...10
  402.         Graphics.update
  403.       end
  404.     end
  405.   end
  406.   Events.onEndBattle.trigger(nil,decision,canlose)
  407. end
  408.  
  409. Events.onEndBattle+=proc {|sender,e|
  410.   decision = e[0]
  411.   canlose  = e[1]
  412.   if USENEWBATTLEMECHANICS || (decision!=2 && decision!=5) # not a loss or a draw
  413.     if $PokemonTemp.evolutionLevels
  414.       pbEvolutionCheck($PokemonTemp.evolutionLevels)
  415.       $PokemonTemp.evolutionLevels = nil
  416.     end
  417.   end
  418.   if decision==1
  419.     for pkmn in $Trainer.pokemonParty
  420.       Kernel.pbPickup(pkmn)
  421.       if isConst?(pkmn.ability,PBAbilities,:HONEYGATHER) && !pkmn.hasItem?
  422.         if hasConst?(PBItems,:HONEY)
  423.           chance = 5+((pkmn.level-1)/10).floor*5
  424.           pkmn.setItem(:HONEY) if rand(100)<chance
  425.         end
  426.       end
  427.     end
  428.   end
  429.   if (decision==2 || decision==5) && !canlose
  430.     $game_system.bgm_unpause
  431.     $game_system.bgs_unpause
  432.     Kernel.pbStartOver
  433.   end
  434. }
  435.  
  436. def pbEvolutionCheck(currentlevels)
  437.   for i in 0...currentlevels.length
  438.     pokemon = $Trainer.party[i]
  439.     next if pokemon.hp==0 && !USENEWBATTLEMECHANICS
  440.     if pokemon && (!currentlevels[i] || pokemon.level!=currentlevels[i])
  441.       newspecies = Kernel.pbCheckEvolution(pokemon)
  442.       if newspecies>0
  443.         evo = PokemonEvolutionScene.new
  444.         evo.pbStartScreen(pokemon,newspecies)
  445.         evo.pbEvolution
  446.         evo.pbEndScreen
  447.       end
  448.     end
  449.   end
  450. end
  451.  
  452. def pbDynamicItemList(*args)
  453.   ret = []
  454.   for i in 0...args.length
  455.     if hasConst?(PBItems,args[i])
  456.       ret.push(getConst(PBItems,args[i].to_sym))
  457.     end
  458.   end
  459.   return ret
  460. end
  461.  
  462. # Runs the Pickup event after a battle if a Pokemon has the ability Pickup.
  463. def Kernel.pbPickup(pokemon)
  464.   return if !isConst?(pokemon.ability,PBAbilities,:PICKUP) || pokemon.egg?
  465.   return if pokemon.item!=0
  466.   return if rand(10)!=0
  467.   pickupList = pbDynamicItemList(
  468.      :POTION,
  469.      :ANTIDOTE,
  470.      :SUPERPOTION,
  471.      :GREATBALL,
  472.      :REPEL,
  473.      :ESCAPEROPE,
  474.      :FULLHEAL,
  475.      :HYPERPOTION,
  476.      :ULTRABALL,
  477.      :REVIVE,
  478.      :RARECANDY,
  479.      :SUNSTONE,
  480.      :MOONSTONE,
  481.      :HEARTSCALE,
  482.      :FULLRESTORE,
  483.      :MAXREVIVE,
  484.      :PPUP,
  485.      :MAXELIXIR
  486.   )
  487.   pickupListRare = pbDynamicItemList(
  488.      :HYPERPOTION,
  489.      :NUGGET,
  490.      :KINGSROCK,
  491.      :FULLRESTORE,
  492.      :ETHER,
  493.      :IRONBALL,
  494.      :DESTINYKNOT,
  495.      :ELIXIR,
  496.      :DESTINYKNOT,
  497.      :LEFTOVERS,
  498.      :DESTINYKNOT
  499.   )
  500.   return if pickupList.length<18
  501.   return if pickupListRare.length<11
  502.   randlist = [30,10,10,10,10,10,10,4,4,1,1]
  503.   items = []
  504.   plevel = [100,pokemon.level].min
  505.   itemstart = (plevel-1)/10
  506.   itemstart = 0 if itemstart<0
  507.   for i in 0...9
  508.     items.push(pickupList[itemstart+i])
  509.   end
  510.   for i in 0...2
  511.     items.push(pickupListRare[itemstart+i])
  512.   end
  513.   rnd = rand(100)
  514.   cumnumber = 0
  515.   for i in 0...randlist.length
  516.     cumnumber += randlist[i]
  517.     if rnd<cumnumber
  518.       pokemon.setItem(items[i])
  519.       break
  520.     end
  521.   end
  522. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement