Advertisement
Guest User

Untitled

a guest
Apr 15th, 2017
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 153.18 KB | None | 0 0
  1. # Results of battle:
  2. # 0 - Undecided or aborted
  3. # 1 - Player won
  4. # 2 - Player lost
  5. # 3 - Player or wild Pokémon ran from battle, or player forfeited the match
  6. # 4 - Wild Pokémon was caught
  7. # 5 - Draw
  8. ################################################################################
  9. # Placeholder battle peer.
  10. ################################################################################
  11. class PokeBattle_NullBattlePeer
  12. def pbStorePokemon(player,pokemon)
  13. if player.party.length<6
  14. player.party[player.party.length]=pokemon
  15. end
  16. return -1
  17. end
  18.  
  19. def pbOnEnteringBattle(battle,pokemon)
  20. end
  21.  
  22. def pbGetStorageCreator()
  23. return nil
  24. end
  25.  
  26. def pbCurrentBox()
  27. return -1
  28. end
  29.  
  30. def pbBoxName(box)
  31. return ""
  32. end
  33. end
  34.  
  35.  
  36.  
  37. class PokeBattle_BattlePeer
  38. def self.create
  39. return PokeBattle_NullBattlePeer.new()
  40. end
  41. end
  42.  
  43.  
  44.  
  45. ################################################################################
  46. # Success state (used for Battle Arena).
  47. ################################################################################
  48. class PokeBattle_SuccessState
  49. attr_accessor :typemod
  50. attr_accessor :useState # 0 - not used, 1 - failed, 2 - succeeded
  51. attr_accessor :protected
  52. attr_accessor :skill
  53.  
  54. def initialize
  55. clear
  56. end
  57.  
  58. def clear
  59. @typemod = 4
  60. @useState = 0
  61. @protected = false
  62. @skill = 0
  63. end
  64.  
  65. def updateSkill
  66. if @useState==1 && !@protected
  67. @skill-=2
  68. elsif @useState==2
  69. if @typemod>4
  70. @skill+=2 # "Super effective"
  71. elsif @typemod>=1 && @typemod<4
  72. @skill-=1 # "Not very effective"
  73. elsif @typemod==0
  74. @skill-=2 # Ineffective
  75. else
  76. @skill+=1
  77. end
  78. end
  79. @typemod=4
  80. @useState=0
  81. @protected=false
  82. end
  83. end
  84.  
  85.  
  86.  
  87. ################################################################################
  88. # Catching and storing Pokémon.
  89. ################################################################################
  90. module PokeBattle_BattleCommon
  91. def pbStorePokemon(pokemon)
  92. if !(pokemon.isShadow? rescue false)
  93. if pbDisplayConfirm(_INTL("Would you like to give a nickname to {1}?",pokemon.name))
  94. species=PBSpecies.getName(pokemon.species)
  95. nickname=@scene.pbNameEntry(_INTL("{1}'s nickname?",species),pokemon)
  96. pokemon.name=nickname if nickname!=""
  97. end
  98. end
  99. oldcurbox=@peer.pbCurrentBox()
  100. storedbox=@peer.pbStorePokemon(self.pbPlayer,pokemon)
  101. creator=@peer.pbGetStorageCreator()
  102. return if storedbox<0
  103. curboxname=@peer.pbBoxName(oldcurbox)
  104. boxname=@peer.pbBoxName(storedbox)
  105. if storedbox!=oldcurbox
  106. if creator
  107. pbDisplayPaused(_INTL("Box \"{1}\" on {2}'s PC was full.",curboxname,creator))
  108. else
  109. pbDisplayPaused(_INTL("Box \"{1}\" on someone's PC was full.",curboxname))
  110. end
  111. pbDisplayPaused(_INTL("{1} was transferred to box \"{2}\".",pokemon.name,boxname))
  112. else
  113. if creator
  114. pbDisplayPaused(_INTL("{1} was transferred to {2}'s PC.",pokemon.name,creator))
  115. else
  116. pbDisplayPaused(_INTL("{1} was transferred to someone's PC.",pokemon.name))
  117. end
  118. pbDisplayPaused(_INTL("It was stored in box \"{1}\".",boxname))
  119. end
  120. end
  121.  
  122. def pbThrowPokeBall(idxPokemon,ball,rareness=nil,showplayer=false)
  123. itemname=PBItems.getName(ball)
  124. battler=nil
  125. if pbIsOpposing?(idxPokemon)
  126. battler=self.battlers[idxPokemon]
  127. else
  128. battler=self.battlers[idxPokemon].pbOppositeOpposing
  129. end
  130. if battler.isFainted?
  131. battler=battler.pbPartner
  132. end
  133. pbDisplayBrief(_INTL("{1} threw one {2}!",self.pbPlayer.name,itemname))
  134. if battler.isFainted?
  135. pbDisplay(_INTL("But there was no target..."))
  136. return
  137. end
  138. if @opponent && (!pbIsSnagBall?(ball) || !battler.isShadow?)
  139. @scene.pbThrowAndDeflect(ball,1)
  140. pbDisplay(_INTL("The Trainer blocked the Ball!\nDon't be a thief!"))
  141. else
  142. pokemon=battler.pokemon
  143. species=pokemon.species
  144. if $DEBUG && Input.press?(Input::CTRL)
  145. shakes=4
  146. else
  147. if !rareness
  148. dexdata=pbOpenDexData
  149. pbDexDataOffset(dexdata,species,16)
  150. rareness=dexdata.fgetb # Get rareness from dexdata file
  151. dexdata.close
  152. end
  153. a=battler.totalhp
  154. b=battler.hp
  155. rareness=BallHandlers.modifyCatchRate(ball,rareness,self,battler)
  156. x=(((a*3-b*2)*rareness)/(a*3)).floor
  157. if battler.status==PBStatuses::SLEEP || battler.status==PBStatuses::FROZEN
  158. x=(x*2.5).floor
  159. elsif battler.status!=0
  160. x=(x*1.5).floor
  161. end
  162. c=0
  163. if $Trainer
  164. if $Trainer.pokedexOwned>600
  165. c=(x*2.5/6).floor
  166. elsif $Trainer.pokedexOwned>450
  167. c=(x*2/6).floor
  168. elsif $Trainer.pokedexOwned>300
  169. c=(x*1.5/6).floor
  170. elsif $Trainer.pokedexOwned>150
  171. c=(x*1/6).floor
  172. elsif $Trainer.pokedexOwned>30
  173. c=(x*0.5/6).floor
  174. end
  175. end
  176. shakes=0; critical=false
  177. if x>255 || BallHandlers.isUnconditional?(ball,self,battler)
  178. shakes=4
  179. else
  180. x=1 if x<1
  181. y = ( 65536 / ((255.0/x)**0.1875) ).floor
  182. if USECRITICALCAPTURE && pbRandom(256)<c
  183. critical=true
  184. shakes=4 if pbRandom(65536)<y
  185. else
  186. shakes+=1 if pbRandom(65536)<y
  187. shakes+=1 if pbRandom(65536)<y && shakes==1
  188. shakes+=1 if pbRandom(65536)<y && shakes==2
  189. shakes+=1 if pbRandom(65536)<y && shakes==3
  190. end
  191. end
  192. end
  193. PBDebug.log("[Threw Poké Ball] #{itemname}, #{shakes} shakes (4=capture)")
  194. @scene.pbThrow(ball,(critical) ? 1 : shakes,critical,battler.index,showplayer)
  195. case shakes
  196. when 0
  197. pbDisplay(_INTL("Oh no! The Pokémon broke free!"))
  198. BallHandlers.onFailCatch(ball,self,battler)
  199. when 1
  200. pbDisplay(_INTL("Aww... It appeared to be caught!"))
  201. BallHandlers.onFailCatch(ball,self,battler)
  202. when 2
  203. pbDisplay(_INTL("Aargh! Almost had it!"))
  204. BallHandlers.onFailCatch(ball,self,battler)
  205. when 3
  206. pbDisplay(_INTL("Gah! It was so close, too!"))
  207. BallHandlers.onFailCatch(ball,self,battler)
  208. when 4
  209. pbDisplayBrief(_INTL("Gotcha! {1} was caught!",pokemon.name))
  210. @scene.pbThrowSuccess
  211. if pbIsSnagBall?(ball) && @opponent
  212. pbRemoveFromParty(battler.index,battler.pokemonIndex)
  213. battler.pbReset
  214. battler.participants=[]
  215. else
  216. @decision=4
  217. end
  218. if pbIsSnagBall?(ball)
  219. pokemon.ot=self.pbPlayer.name
  220. pokemon.trainerID=self.pbPlayer.id
  221. end
  222. BallHandlers.onCatch(ball,self,pokemon)
  223. pokemon.ballused=pbGetBallType(ball)
  224. pokemon.makeUnmega rescue nil
  225. pokemon.makeUnprimal rescue nil
  226. pokemon.pbRecordFirstMoves
  227. if GAINEXPFORCAPTURE
  228. battler.captured=true
  229. pbGainExp
  230. battler.captured=false
  231. end
  232. if !self.pbPlayer.hasOwned?(species)
  233. self.pbPlayer.setOwned(species)
  234. if $Trainer.pokedex
  235. pbDisplayPaused(_INTL("{1}'s data was added to the Pokédex.",pokemon.name))
  236. @scene.pbShowPokedex(species)
  237. end
  238. end
  239. @scene.pbHideCaptureBall
  240. if pbIsSnagBall?(ball) && @opponent
  241. pokemon.pbUpdateShadowMoves rescue nil
  242. @snaggedpokemon.push(pokemon)
  243. else
  244. pbStorePokemon(pokemon)
  245. end
  246. end
  247. end
  248. end
  249. end
  250.  
  251.  
  252.  
  253. ################################################################################
  254. # Main battle class.
  255. ################################################################################
  256. class PokeBattle_Battle
  257. attr_reader(:scene) # Scene object for this battle
  258. attr_accessor(:decision) # Decision: 0=undecided; 1=win; 2=loss; 3=escaped; 4=caught
  259. attr_accessor(:internalbattle) # Internal battle flag
  260. attr_accessor(:doublebattle) # Double battle flag
  261. attr_accessor(:cantescape) # True if player can't escape
  262. attr_accessor(:shiftStyle) # Shift/Set "battle style" option
  263. attr_accessor(:battlescene) # "Battle scene" option
  264. attr_accessor(:debug) # Debug flag
  265. attr_reader(:player) # Player trainer
  266. attr_reader(:opponent) # Opponent trainer
  267. attr_reader(:party1) # Player's Pokémon party
  268. attr_reader(:party2) # Foe's Pokémon party
  269. attr_reader(:party1order) # Order of Pokémon in the player's party
  270. attr_reader(:party2order) # Order of Pokémon in the opponent's party
  271. attr_accessor(:fullparty1) # True if player's party's max size is 6 instead of 3
  272. attr_accessor(:fullparty2) # True if opponent's party's max size is 6 instead of 3
  273. attr_reader(:battlers) # Currently active Pokémon
  274. attr_accessor(:items) # Items held by opponents
  275. attr_reader(:sides) # Effects common to each side of a battle
  276. attr_reader(:field) # Effects common to the whole of a battle
  277. attr_accessor(:environment) # Battle surroundings
  278. attr_accessor(:weather) # Current weather, custom methods should use pbWeather instead
  279. attr_accessor(:weatherduration) # Duration of current weather, or -1 if indefinite
  280. attr_reader(:switching) # True if during the switching phase of the round
  281. attr_reader(:futuresight) # True if Future Sight is hitting
  282. attr_reader(:struggle) # The Struggle move
  283. attr_accessor(:choices) # Choices made by each Pokémon this round
  284. attr_reader(:successStates) # Success states
  285. attr_accessor(:lastMoveUsed) # Last move used
  286. attr_accessor(:lastMoveUser) # Last move user
  287. attr_accessor(:megaEvolution) # Battle index of each trainer's Pokémon to Mega Evolve
  288. attr_accessor(:amuletcoin) # Whether Amulet Coin's effect applies
  289. attr_accessor(:extramoney) # Money gained in battle by using Pay Day
  290. attr_accessor(:doublemoney) # Whether Happy Hour's effect applies
  291. attr_accessor(:endspeech) # Speech by opponent when player wins
  292. attr_accessor(:endspeech2) # Speech by opponent when player wins
  293. attr_accessor(:endspeechwin) # Speech by opponent when opponent wins
  294. attr_accessor(:endspeechwin2) # Speech by opponent when opponent wins
  295. attr_accessor(:rules)
  296. attr_reader(:turncount)
  297. attr_accessor :controlPlayer
  298. include PokeBattle_BattleCommon
  299.  
  300. MAXPARTYSIZE = 6
  301.  
  302. class BattleAbortedException < Exception; end
  303.  
  304. def pbAbort
  305. raise BattleAbortedException.new("Battle aborted")
  306. end
  307.  
  308. def pbDebugUpdate
  309. end
  310.  
  311. def pbRandom(x)
  312. return rand(x)
  313. end
  314.  
  315. def pbAIRandom(x)
  316. return rand(x)
  317. end
  318.  
  319. ################################################################################
  320. # Initialise battle class.
  321. ################################################################################
  322. def initialize(scene,p1,p2,player,opponent)
  323. if p1.length==0
  324. raise ArgumentError.new(_INTL("Party 1 has no Pokémon."))
  325. return
  326. end
  327. if p2.length==0
  328. raise ArgumentError.new(_INTL("Party 2 has no Pokémon."))
  329. return
  330. end
  331. if p2.length>2 && !opponent
  332. raise ArgumentError.new(_INTL("Wild battles with more than two Pokémon are not allowed."))
  333. return
  334. end
  335. @scene = scene
  336. @decision = 0
  337. @internalbattle = true
  338. @doublebattle = false
  339. @cantescape = false
  340. @shiftStyle = true
  341. @battlescene = true
  342. @debug = false
  343. @debugupdate = 0
  344. if opponent && player.is_a?(Array) && player.length==0
  345. player = player[0]
  346. end
  347. if opponent && opponent.is_a?(Array) && opponent.length==0
  348. opponent = opponent[0]
  349. end
  350. @player = player # PokeBattle_Trainer object
  351. @opponent = opponent # PokeBattle_Trainer object
  352. @party1 = p1
  353. @party2 = p2
  354. @party1order = []
  355. for i in 0...12; @party1order.push(i); end
  356. @party2order = []
  357. for i in 0...12; @party2order.push(i); end
  358. @fullparty1 = false
  359. @fullparty2 = false
  360. @battlers = []
  361. @items = nil
  362. @sides = [PokeBattle_ActiveSide.new, # Player's side
  363. PokeBattle_ActiveSide.new] # Foe's side
  364. @field = PokeBattle_ActiveField.new # Whole field (gravity/rooms)
  365. @environment = PBEnvironment::None # e.g. Tall grass, cave, still water
  366. @weather = 0
  367. @weatherduration = 0
  368. @switching = false
  369. @futuresight = false
  370. @choices = [ [0,0,nil,-1],[0,0,nil,-1],[0,0,nil,-1],[0,0,nil,-1] ]
  371. @successStates = []
  372. for i in 0...4
  373. @successStates.push(PokeBattle_SuccessState.new)
  374. end
  375. @lastMoveUsed = -1
  376. @lastMoveUser = -1
  377. @nextPickupUse = 0
  378. @megaEvolution = []
  379. if @player.is_a?(Array)
  380. @megaEvolution[0]=[-1]*@player.length
  381. else
  382. @megaEvolution[0]=[-1]
  383. end
  384. if @opponent.is_a?(Array)
  385. @megaEvolution[1]=[-1]*@opponent.length
  386. else
  387. @megaEvolution[1]=[-1]
  388. end
  389. @amuletcoin = false
  390. @extramoney = 0
  391. @doublemoney = false
  392. @endspeech = ""
  393. @endspeech2 = ""
  394. @endspeechwin = ""
  395. @endspeechwin2 = ""
  396. @rules = {}
  397. @turncount = 0
  398. @peer = PokeBattle_BattlePeer.create()
  399. @priority = []
  400. @usepriority = false
  401. @snaggedpokemon = []
  402. @runCommand = 0
  403. if hasConst?(PBMoves,:STRUGGLE)
  404. @struggle = PokeBattle_Move.pbFromPBMove(self,PBMove.new(getConst(PBMoves,:STRUGGLE)))
  405. else
  406. @struggle = PokeBattle_Struggle.new(self,nil)
  407. end
  408. @struggle.pp = -1
  409. for i in 0...4
  410. battlers[i] = PokeBattle_Battler.new(self,i)
  411. end
  412. for i in @party1
  413. next if !i
  414. i.itemRecycle = 0
  415. i.itemInitial = i.item
  416. i.belch = false
  417. end
  418. for i in @party2
  419. next if !i
  420. i.itemRecycle = 0
  421. i.itemInitial = i.item
  422. i.belch = false
  423. end
  424. end
  425.  
  426. ################################################################################
  427. # Info about battle.
  428. ################################################################################
  429. def pbDoubleBattleAllowed?
  430. if !@fullparty1 && @party1.length>MAXPARTYSIZE
  431. return false
  432. end
  433. if !@fullparty2 && @party2.length>MAXPARTYSIZE
  434. return false
  435. end
  436. _opponent=@opponent
  437. _player=@player
  438. # Wild battle
  439. if !_opponent
  440. if @party2.length==1
  441. return false
  442. elsif @party2.length==2
  443. return true
  444. else
  445. return false
  446. end
  447. # Trainer battle
  448. else
  449. if _opponent.is_a?(Array)
  450. if _opponent.length==1
  451. _opponent=_opponent[0]
  452. elsif _opponent.length!=2
  453. return false
  454. end
  455. end
  456. _player=_player
  457. if _player.is_a?(Array)
  458. if _player.length==1
  459. _player=_player[0]
  460. elsif _player.length!=2
  461. return false
  462. end
  463. end
  464. if _opponent.is_a?(Array)
  465. sendout1=pbFindNextUnfainted(@party2,0,pbSecondPartyBegin(1))
  466. sendout2=pbFindNextUnfainted(@party2,pbSecondPartyBegin(1))
  467. return false if sendout1<0 || sendout2<0
  468. else
  469. sendout1=pbFindNextUnfainted(@party2,0)
  470. sendout2=pbFindNextUnfainted(@party2,sendout1+1)
  471. return false if sendout1<0 || sendout2<0
  472. end
  473. end
  474. if _player.is_a?(Array)
  475. sendout1=pbFindNextUnfainted(@party1,0,pbSecondPartyBegin(0))
  476. sendout2=pbFindNextUnfainted(@party1,pbSecondPartyBegin(0))
  477. return false if sendout1<0 || sendout2<0
  478. else
  479. sendout1=pbFindNextUnfainted(@party1,0)
  480. sendout2=pbFindNextUnfainted(@party1,sendout1+1)
  481. return false if sendout1<0 || sendout2<0
  482. end
  483. return true
  484. end
  485.  
  486. def pbWeather
  487. for i in 0...4
  488. if @battlers[i].hasWorkingAbility(:CLOUDNINE) ||
  489. @battlers[i].hasWorkingAbility(:AIRLOCK)
  490. return 0
  491. end
  492. end
  493. return @weather
  494. end
  495.  
  496. ################################################################################
  497. # Get battler info.
  498. ################################################################################
  499. def pbIsOpposing?(index)
  500. return (index%2)==1
  501. end
  502.  
  503. def pbOwnedByPlayer?(index)
  504. return false if pbIsOpposing?(index)
  505. return false if @player.is_a?(Array) && index==2
  506. return true
  507. end
  508.  
  509. def pbIsDoubleBattler?(index)
  510. return (index>=2)
  511. end
  512.  
  513. # Only used for Wish
  514. def pbThisEx(battlerindex,pokemonindex)
  515. party=pbParty(battlerindex)
  516. if pbIsOpposing?(battlerindex)
  517. if @opponent
  518. return _INTL("The foe {1}",party[pokemonindex].name)
  519. else
  520. return _INTL("The wild {1}",party[pokemonindex].name)
  521. end
  522. else
  523. return _INTL("{1}",party[pokemonindex].name)
  524. end
  525. end
  526.  
  527. # Checks whether an item can be removed from a Pokémon.
  528. def pbIsUnlosableItem(pkmn,item)
  529. return true if pbIsMail?(item)
  530. return false if pkmn.effects[PBEffects::Transform]
  531. if isConst?(pkmn.ability,PBAbilities,:MULTITYPE)
  532. plates=[:FISTPLATE,:SKYPLATE,:TOXICPLATE,:EARTHPLATE,:STONEPLATE,
  533. :INSECTPLATE,:SPOOKYPLATE,:IRONPLATE,:FLAMEPLATE,:SPLASHPLATE,
  534. :MEADOWPLATE,:ZAPPLATE,:MINDPLATE,:ICICLEPLATE,:DRACOPLATE,
  535. :DREADPLATE,:PIXIEPLATE]
  536. for i in plates
  537. return true if isConst?(item,PBItems,i)
  538. end
  539. end
  540. combos=[[:GIRATINA,:GRISEOUSORB],
  541. [:GENESECT,:BURNDRIVE],
  542. [:GENESECT,:CHILLDRIVE],
  543. [:GENESECT,:DOUSEDRIVE],
  544. [:GENESECT,:SHOCKDRIVE],
  545. # Mega Stones
  546. [:ABOMASNOW,:ABOMASITE],
  547. [:ABSOL,:ABSOLITE],
  548. [:AERODACTYL,:AERODACTYLITE],
  549. [:AGGRON,:AGGRONITE],
  550. [:ALAKAZAM,:ALAKAZITE],
  551. [:ALTARIA,:ALTARIANITE],
  552. [:AMPHAROS,:AMPHAROSITE],
  553. [:AUDINO,:AUDINITE],
  554. [:BANETTE,:BANETTITE],
  555. [:BEEDRILL,:BEEDRILLITE],
  556. [:BLASTOISE,:BLASTOISINITE],
  557. [:BLAZIKEN,:BLAZIKENITE],
  558. [:CAMPERUPT,:CAMERUPTITE],
  559. [:CHARIZARD,:CHARIZARDITEX],
  560. [:CHARIZARD,:CHARIZARDITEY],
  561. [:DIANCIE,:DIANCITE],
  562. [:GALLADE,:GALLADITE],
  563. [:GARCHOMP,:GARCHOMPITE],
  564. [:GARDEVOIR,:GARDEVOIRITE],
  565. [:GENGAR,:GENGARITE],
  566. [:GLALIE,:GLALITITE],
  567. [:GYARADOS,:GYARADOSITE],
  568. [:HERACROSS,:HERACRONITE],
  569. [:HOUNDOOM,:HOUNDOOMINITE],
  570. [:KANGASKHAN,:KANGASKHANITE],
  571. [:LATIAS,:LATIASITE],
  572. [:LATIOS,:LATIOSITE],
  573. [:LOPUNNY,:LOPUNNITE],
  574. [:LUCARIO,:LUCARIONITE],
  575. [:MANECTRIC,:MANECTITE],
  576. [:MAWILE,:MAWILITE],
  577. [:MEDICHAM,:MEDICHAMITE],
  578. [:METAGROSS,:METAGROSSITE],
  579. [:MEWTWO,:MEWTWONITEX],
  580. [:MEWTWO,:MEWTWONITEY],
  581. [:PIDGEOT,:PIDGEOTITE],
  582. [:PINSIR,:PINSIRITE],
  583. [:SABLEYE,:SABLENITE],
  584. [:SALAMENCE,:SALAMENCITE],
  585. [:SCEPTILE,:SCEPTILITE],
  586. [:SCIZOR,:SCIZORITE],
  587. [:SHARPEDO,:SHARPEDONITE],
  588. [:SLOWBRO,:SLOWBRONITE],
  589. [:STEELIX,:STEELIXITE],
  590. [:SWAMPERT,:SWAMPERTITE],
  591. [:TYRANITAR,:TYRANITARITE],
  592. [:VENUSAUR,:VENUSAURITE],
  593. # Primal Reversion stones
  594. [:KYOGRE,:BLUEORB],
  595. [:GROUDON,:REDORB]
  596. ]
  597. for i in combos
  598. if isConst?(pkmn.species,PBSpecies,i[0]) && isConst?(item,PBItems,i[1])
  599. return true
  600. end
  601. end
  602. return false
  603. end
  604.  
  605. def pbCheckGlobalAbility(a)
  606. for i in 0...4 # in order from own first, opposing first, own second, opposing second
  607. if @battlers[i].hasWorkingAbility(a)
  608. return @battlers[i]
  609. end
  610. end
  611. return nil
  612. end
  613.  
  614. def nextPickupUse
  615. @nextPickupUse+=1
  616. return @nextPickupUse
  617. end
  618.  
  619. ################################################################################
  620. # Player-related info.
  621. ################################################################################
  622. def pbPlayer
  623. if @player.is_a?(Array)
  624. return @player[0]
  625. else
  626. return @player
  627. end
  628. end
  629.  
  630. def pbGetOwnerItems(battlerIndex)
  631. return [] if !@items
  632. if pbIsOpposing?(battlerIndex)
  633. if @opponent.is_a?(Array)
  634. return (battlerIndex==1) ? @items[0] : @items[1]
  635. else
  636. return @items
  637. end
  638. else
  639. return []
  640. end
  641. end
  642.  
  643. def pbSetSeen(pokemon)
  644. if pokemon && @internalbattle
  645. self.pbPlayer.seen[pokemon.species]=true
  646. pbSeenForm(pokemon)
  647. end
  648. end
  649.  
  650. def pbGetMegaRingName(battlerIndex)
  651. if pbBelongsToPlayer?(battlerIndex)
  652. rings=[:MEGARING,:MEGABRACELET,:MEGACUFF,:MEGACHARM]
  653. for i in rings
  654. next if !hasConst?(PBItems,i)
  655. return PBItems.getName(i) if $PokemonBag.pbQuantity(i)>0
  656. end
  657. end
  658. # Add your own Mega objects for particular trainer types here
  659. # if isConst?(pbGetOwner(battlerIndex).trainertype,PBTrainers,:BUGCATCHER)
  660. # return _INTL("Mega Net")
  661. # end
  662. return _INTL("Mega Ring")
  663. end
  664.  
  665. def pbHasMegaRing(battlerIndex)
  666. return true if !pbBelongsToPlayer?(battlerIndex)
  667. rings=[:MEGARING,:MEGABRACELET,:MEGACUFF,:MEGACHARM]
  668. for i in rings
  669. next if !hasConst?(PBItems,i)
  670. return true if $PokemonBag.pbQuantity(i)>0
  671. end
  672. return false
  673. end
  674.  
  675. ################################################################################
  676. # Get party info, manipulate parties.
  677. ################################################################################
  678. def pbPokemonCount(party)
  679. count=0
  680. for i in party
  681. next if !i
  682. count+=1 if i.hp>0 && !i.isEgg?
  683. end
  684. return count
  685. end
  686.  
  687. def pbAllFainted?(party)
  688. pbPokemonCount(party)==0
  689. end
  690.  
  691. def pbMaxLevel(party)
  692. lv=0
  693. for i in party
  694. next if !i
  695. lv=i.level if lv<i.level
  696. end
  697. return lv
  698. end
  699.  
  700. def pbMaxLevelFromIndex(index)
  701. party=pbParty(index)
  702. owner=(pbIsOpposing?(index)) ? @opponent : @player
  703. maxlevel=0
  704. if owner.is_a?(Array)
  705. start=0
  706. limit=pbSecondPartyBegin(index)
  707. start=limit if pbIsDoubleBattler?(index)
  708. for i in start...start+limit
  709. next if !party[i]
  710. maxlevel=party[i].level if maxlevel<party[i].level
  711. end
  712. else
  713. for i in party
  714. next if !i
  715. maxlevel=i.level if maxlevel<i.level
  716. end
  717. end
  718. return maxlevel
  719. end
  720.  
  721. def pbParty(index)
  722. return pbIsOpposing?(index) ? party2 : party1
  723. end
  724.  
  725. def pbOpposingParty(index)
  726. return pbIsOpposing?(index) ? party1 : party2
  727. end
  728.  
  729. def pbSecondPartyBegin(battlerIndex)
  730. if pbIsOpposing?(battlerIndex)
  731. return @fullparty2 ? 6 : 3
  732. else
  733. return @fullparty1 ? 6 : 3
  734. end
  735. end
  736.  
  737. def pbPartyLength(battlerIndex)
  738. if pbIsOpposing?(battlerIndex)
  739. return (@opponent.is_a?(Array)) ? pbSecondPartyBegin(battlerIndex) : MAXPARTYSIZE
  740. else
  741. return @player.is_a?(Array) ? pbSecondPartyBegin(battlerIndex) : MAXPARTYSIZE
  742. end
  743. end
  744.  
  745. def pbFindNextUnfainted(party,start,finish=-1)
  746. finish=party.length if finish<0
  747. for i in start...finish
  748. next if !party[i]
  749. return i if party[i].hp>0 && !party[i].isEgg?
  750. end
  751. return -1
  752. end
  753.  
  754. def pbGetLastPokeInTeam(index)
  755. party=pbParty(index)
  756. partyorder=(!pbIsOpposing?(index)) ? @party1order : @party2order
  757. plength=pbPartyLength(index)
  758. pstart=pbGetOwnerIndex(index)*plength
  759. lastpoke=-1
  760. for i in pstart...pstart+plength
  761. p=party[partyorder[i]]
  762. next if !p || p.isEgg? || p.hp<=0
  763. lastpoke=partyorder[i]
  764. end
  765. return lastpoke
  766. end
  767.  
  768. def pbFindPlayerBattler(pkmnIndex)
  769. battler=nil
  770. for k in 0...4
  771. if !pbIsOpposing?(k) && @battlers[k].pokemonIndex==pkmnIndex
  772. battler=@battlers[k]
  773. break
  774. end
  775. end
  776. return battler
  777. end
  778.  
  779. def pbIsOwner?(battlerIndex,partyIndex)
  780. secondParty=pbSecondPartyBegin(battlerIndex)
  781. if !pbIsOpposing?(battlerIndex)
  782. return true if !@player || !@player.is_a?(Array)
  783. return (battlerIndex==0) ? partyIndex<secondParty : partyIndex>=secondParty
  784. else
  785. return true if !@opponent || !@opponent.is_a?(Array)
  786. return (battlerIndex==1) ? partyIndex<secondParty : partyIndex>=secondParty
  787. end
  788. end
  789.  
  790. def pbGetOwner(battlerIndex)
  791. if pbIsOpposing?(battlerIndex)
  792. if @opponent.is_a?(Array)
  793. return (battlerIndex==1) ? @opponent[0] : @opponent[1]
  794. else
  795. return @opponent
  796. end
  797. else
  798. if @player.is_a?(Array)
  799. return (battlerIndex==0) ? @player[0] : @player[1]
  800. else
  801. return @player
  802. end
  803. end
  804. end
  805.  
  806. def pbGetOwnerPartner(battlerIndex)
  807. if pbIsOpposing?(battlerIndex)
  808. if @opponent.is_a?(Array)
  809. return (battlerIndex==1) ? @opponent[1] : @opponent[0]
  810. else
  811. return @opponent
  812. end
  813. else
  814. if @player.is_a?(Array)
  815. return (battlerIndex==0) ? @player[1] : @player[0]
  816. else
  817. return @player
  818. end
  819. end
  820. end
  821.  
  822. def pbGetOwnerIndex(battlerIndex)
  823. if pbIsOpposing?(battlerIndex)
  824. return (@opponent.is_a?(Array)) ? ((battlerIndex==1) ? 0 : 1) : 0
  825. else
  826. return (@player.is_a?(Array)) ? ((battlerIndex==0) ? 0 : 1) : 0
  827. end
  828. end
  829.  
  830. def pbBelongsToPlayer?(battlerIndex)
  831. if @player.is_a?(Array) && @player.length>1
  832. return battlerIndex==0
  833. else
  834. return (battlerIndex%2)==0
  835. end
  836. return false
  837. end
  838.  
  839. def pbPartyGetOwner(battlerIndex,partyIndex)
  840. secondParty=pbSecondPartyBegin(battlerIndex)
  841. if !pbIsOpposing?(battlerIndex)
  842. return @player if !@player || !@player.is_a?(Array)
  843. return (partyIndex<secondParty) ? @player[0] : @player[1]
  844. else
  845. return @opponent if !@opponent || !@opponent.is_a?(Array)
  846. return (partyIndex<secondParty) ? @opponent[0] : @opponent[1]
  847. end
  848. end
  849.  
  850. def pbAddToPlayerParty(pokemon)
  851. party=pbParty(0)
  852. for i in 0...party.length
  853. party[i]=pokemon if pbIsOwner?(0,i) && !party[i]
  854. end
  855. end
  856.  
  857. def pbRemoveFromParty(battlerIndex,partyIndex)
  858. party=pbParty(battlerIndex)
  859. side=(pbIsOpposing?(battlerIndex)) ? @opponent : @player
  860. order=(pbIsOpposing?(battlerIndex)) ? @party2order : @party1order
  861. secondpartybegin=pbSecondPartyBegin(battlerIndex)
  862. party[partyIndex]=nil
  863. if !side || !side.is_a?(Array) # Wild or single opponent
  864. party.compact!
  865. for i in partyIndex...party.length+1
  866. for j in 0...4
  867. next if !@battlers[j]
  868. if pbGetOwner(j)==side && @battlers[j].pokemonIndex==i
  869. @battlers[j].pokemonIndex-=1
  870. break
  871. end
  872. end
  873. end
  874. for i in 0...order.length
  875. order[i]=(i==partyIndex) ? order.length-1 : order[i]-1
  876. end
  877. else
  878. if partyIndex<secondpartybegin-1
  879. for i in partyIndex...secondpartybegin
  880. if i>=secondpartybegin-1
  881. party[i]=nil
  882. else
  883. party[i]=party[i+1]
  884. end
  885. end
  886. for i in 0...order.length
  887. next if order[i]>=secondpartybegin
  888. order[i]=(i==partyIndex) ? secondpartybegin-1 : order[i]-1
  889. end
  890. else
  891. for i in partyIndex...secondpartybegin+pbPartyLength(battlerIndex)
  892. if i>=party.length-1
  893. party[i]=nil
  894. else
  895. party[i]=party[i+1]
  896. end
  897. end
  898. for i in 0...order.length
  899. next if order[i]<secondpartybegin
  900. order[i]=(i==partyIndex) ? secondpartybegin+pbPartyLength(battlerIndex)-1 : order[i]-1
  901. end
  902. end
  903. end
  904. end
  905.  
  906. ################################################################################
  907. # Check whether actions can be taken.
  908. ################################################################################
  909. def pbCanShowCommands?(idxPokemon)
  910. thispkmn=@battlers[idxPokemon]
  911. return false if thispkmn.isFainted?
  912. return false if thispkmn.effects[PBEffects::TwoTurnAttack]>0
  913. return false if thispkmn.effects[PBEffects::HyperBeam]>0
  914. return false if thispkmn.effects[PBEffects::Rollout]>0
  915. return false if thispkmn.effects[PBEffects::Outrage]>0
  916. return false if thispkmn.effects[PBEffects::Uproar]>0
  917. return false if thispkmn.effects[PBEffects::Bide]>0
  918. return true
  919. end
  920.  
  921. ################################################################################
  922. # Attacking.
  923. ################################################################################
  924. def pbCanShowFightMenu?(idxPokemon)
  925. thispkmn=@battlers[idxPokemon]
  926. if !pbCanShowCommands?(idxPokemon)
  927. return false
  928. end
  929. # No moves that can be chosen
  930. if !pbCanChooseMove?(idxPokemon,0,false) &&
  931. !pbCanChooseMove?(idxPokemon,1,false) &&
  932. !pbCanChooseMove?(idxPokemon,2,false) &&
  933. !pbCanChooseMove?(idxPokemon,3,false)
  934. return false
  935. end
  936. # Encore
  937. return false if thispkmn.effects[PBEffects::Encore]>0
  938. return true
  939. end
  940.  
  941. def pbCanChooseMove?(idxPokemon,idxMove,showMessages,sleeptalk=false)
  942. thispkmn=@battlers[idxPokemon]
  943. thismove=thispkmn.moves[idxMove]
  944. opp1=thispkmn.pbOpposing1
  945. opp2=thispkmn.pbOpposing2
  946. if !thismove||thismove.id==0
  947. return false
  948. end
  949. if thismove.pp<=0 && thismove.totalpp>0 && !sleeptalk
  950. if showMessages
  951. pbDisplayPaused(_INTL("There's no PP left for this move!"))
  952. end
  953. return false
  954. end
  955. if thispkmn.hasWorkingItem(:ASSAULTVEST) && thismove.pbIsStatus?
  956. if showMessages
  957. pbDisplayPaused(_INTL("The effects of the {1} prevent status moves from being used!",
  958. PBItems.getName(thispkmn.item)))
  959. end
  960. return false
  961. end
  962. if thispkmn.effects[PBEffects::ChoiceBand]>=0 &&
  963. (thispkmn.hasWorkingItem(:CHOICEBAND) ||
  964. thispkmn.hasWorkingItem(:CHOICESPECS) ||
  965. thispkmn.hasWorkingItem(:CHOICESCARF))
  966. hasmove=false
  967. for i in 0...4
  968. if thispkmn.moves[i].id==thispkmn.effects[PBEffects::ChoiceBand]
  969. hasmove=true; break
  970. end
  971. end
  972. if hasmove && thismove.id!=thispkmn.effects[PBEffects::ChoiceBand]
  973. if showMessages
  974. pbDisplayPaused(_INTL("{1} allows the use of only {2}!",
  975. PBItems.getName(thispkmn.item),
  976. PBMoves.getName(thispkmn.effects[PBEffects::ChoiceBand])))
  977. end
  978. return false
  979. end
  980. end
  981. if opp1.effects[PBEffects::Imprison]
  982. if thismove.id==opp1.moves[0].id ||
  983. thismove.id==opp1.moves[1].id ||
  984. thismove.id==opp1.moves[2].id ||
  985. thismove.id==opp1.moves[3].id
  986. if showMessages
  987. pbDisplayPaused(_INTL("{1} can't use the sealed {2}!",thispkmn.pbThis,thismove.name))
  988. end
  989. #PBDebug.log("[CanChoose][#{opp1.pbThis} has: #{opp1.moves[0].name}, #{opp1.moves[1].name},#{opp1.moves[2].name},#{opp1.moves[3].name}]")
  990. return false
  991. end
  992. end
  993. if opp2.effects[PBEffects::Imprison]
  994. if thismove.id==opp2.moves[0].id ||
  995. thismove.id==opp2.moves[1].id ||
  996. thismove.id==opp2.moves[2].id ||
  997. thismove.id==opp2.moves[3].id
  998. if showMessages
  999. pbDisplayPaused(_INTL("{1} can't use the sealed {2}!",thispkmn.pbThis,thismove.name))
  1000. end
  1001. #PBDebug.log("[CanChoose][#{opp2.pbThis} has: #{opp2.moves[0].name}, #{opp2.moves[1].name},#{opp2.moves[2].name},#{opp2.moves[3].name}]")
  1002. return false
  1003. end
  1004. end
  1005. if thispkmn.effects[PBEffects::Taunt]>0 && thismove.basedamage==0
  1006. if showMessages
  1007. pbDisplayPaused(_INTL("{1} can't use {2} after the taunt!",thispkmn.pbThis,thismove.name))
  1008. end
  1009. return false
  1010. end
  1011. if thispkmn.effects[PBEffects::Torment]
  1012. if thismove.id==thispkmn.lastMoveUsed
  1013. if showMessages
  1014. pbDisplayPaused(_INTL("{1} can't use the same move twice in a row due to the torment!",thispkmn.pbThis))
  1015. end
  1016. return false
  1017. end
  1018. end
  1019. if thismove.id==thispkmn.effects[PBEffects::DisableMove] && !sleeptalk
  1020. if showMessages
  1021. pbDisplayPaused(_INTL("{1}'s {2} is disabled!",thispkmn.pbThis,thismove.name))
  1022. end
  1023. return false
  1024. end
  1025. if thismove.function==0x158 && # Belch
  1026. (!thispkmn.pokemon || !thispkmn.pokemon.belch)
  1027. if showMessages
  1028. pbDisplayPaused(_INTL("{1} hasn't eaten any held berry, so it can't possibly belch!",thispkmn.pbThis))
  1029. end
  1030. return false
  1031. end
  1032. if thispkmn.effects[PBEffects::Encore]>0 && idxMove!=thispkmn.effects[PBEffects::EncoreIndex]
  1033. return false
  1034. end
  1035. return true
  1036. end
  1037.  
  1038. def pbAutoChooseMove(idxPokemon,showMessages=true)
  1039. thispkmn=@battlers[idxPokemon]
  1040. if thispkmn.isFainted?
  1041. @choices[idxPokemon][0]=0
  1042. @choices[idxPokemon][1]=0
  1043. @choices[idxPokemon][2]=nil
  1044. return
  1045. end
  1046. if thispkmn.effects[PBEffects::Encore]>0 &&
  1047. pbCanChooseMove?(idxPokemon,thispkmn.effects[PBEffects::EncoreIndex],false)
  1048. PBDebug.log("[Auto choosing Encore move] #{thispkmn.moves[thispkmn.effects[PBEffects::EncoreIndex]].name}")
  1049. @choices[idxPokemon][0]=1 # "Use move"
  1050. @choices[idxPokemon][1]=thispkmn.effects[PBEffects::EncoreIndex] # Index of move
  1051. @choices[idxPokemon][2]=thispkmn.moves[thispkmn.effects[PBEffects::EncoreIndex]]
  1052. @choices[idxPokemon][3]=-1 # No target chosen yet
  1053. if @doublebattle
  1054. thismove=thispkmn.moves[thispkmn.effects[PBEffects::EncoreIndex]]
  1055. target=thispkmn.pbTarget(thismove)
  1056. if target==PBTargets::SingleNonUser
  1057. target=@scene.pbChooseTarget(idxPokemon)
  1058. pbRegisterTarget(idxPokemon,target) if target>=0
  1059. elsif target==PBTargets::UserOrPartner
  1060. target=@scene.pbChooseTarget(idxPokemon)
  1061. pbRegisterTarget(idxPokemon,target) if target>=0 && (target&1)==(idxPokemon&1)
  1062. end
  1063. end
  1064. else
  1065. if !pbIsOpposing?(idxPokemon)
  1066. pbDisplayPaused(_INTL("{1} has no moves left!",thispkmn.name)) if showMessages
  1067. end
  1068. @choices[idxPokemon][0]=1 # "Use move"
  1069. @choices[idxPokemon][1]=-1 # Index of move to be used
  1070. @choices[idxPokemon][2]=@struggle # Use Struggle
  1071. @choices[idxPokemon][3]=-1 # No target chosen yet
  1072. end
  1073. end
  1074.  
  1075. def pbRegisterMove(idxPokemon,idxMove,showMessages=true)
  1076. thispkmn=@battlers[idxPokemon]
  1077. thismove=thispkmn.moves[idxMove]
  1078. return false if !pbCanChooseMove?(idxPokemon,idxMove,showMessages)
  1079. @choices[idxPokemon][0]=1 # "Use move"
  1080. @choices[idxPokemon][1]=idxMove # Index of move to be used
  1081. @choices[idxPokemon][2]=thismove # PokeBattle_Move object of the move
  1082. @choices[idxPokemon][3]=-1 # No target chosen yet
  1083. return true
  1084. end
  1085.  
  1086. def pbChoseMove?(i,move)
  1087. return false if @battlers[i].isFainted?
  1088. if @choices[i][0]==1 && @choices[i][1]>=0
  1089. choice=@choices[i][1]
  1090. return isConst?(@battlers[i].moves[choice].id,PBMoves,move)
  1091. end
  1092. return false
  1093. end
  1094.  
  1095. def pbChoseMoveFunctionCode?(i,code)
  1096. return false if @battlers[i].isFainted?
  1097. if @choices[i][0]==1 && @choices[i][1]>=0
  1098. choice=@choices[i][1]
  1099. return @battlers[i].moves[choice].function==code
  1100. end
  1101. return false
  1102. end
  1103.  
  1104. def pbRegisterTarget(idxPokemon,idxTarget)
  1105. @choices[idxPokemon][3]=idxTarget # Set target of move
  1106. return true
  1107. end
  1108.  
  1109. def pbPriority(ignorequickclaw=false,log=false)
  1110. return @priority if @usepriority # use stored priority if round isn't over yet
  1111. @priority.clear
  1112. speeds=[]
  1113. priorities=[]
  1114. quickclaw=[]; lagging=[]
  1115. minpri=0; maxpri=0
  1116. temp=[]
  1117. # Calculate each Pokémon's speed
  1118. for i in 0...4
  1119. speeds[i]=@battlers[i].pbSpeed
  1120. quickclaw[i]=false
  1121. lagging[i]=false
  1122. if !ignorequickclaw && @choices[i][0]==1 # Chose to use a move
  1123. if !quickclaw[i] && @battlers[i].hasWorkingItem(:CUSTAPBERRY) &&
  1124. !@battlers[i].pbOpposing1.hasWorkingAbility(:UNNERVE) &&
  1125. !@battlers[i].pbOpposing2.hasWorkingAbility(:UNNERVE)
  1126. if (@battlers[i].hasWorkingAbility(:GLUTTONY) && @battlers[i].hp<=(@battlers[i].totalhp/2).floor) ||
  1127. @battlers[i].hp<=(@battlers[i].totalhp/4).floor
  1128. pbCommonAnimation("UseItem",@battlers[i],nil)
  1129. quickclaw[i]=true
  1130. pbDisplayBrief(_INTL("{1}'s {2} let it move first!",
  1131. @battlers[i].pbThis,PBItems.getName(@battlers[i].item)))
  1132. @battlers[i].pbConsumeItem
  1133. end
  1134. end
  1135. if !quickclaw[i] && @battlers[i].hasWorkingItem(:QUICKCLAW)
  1136. if pbRandom(10)<2
  1137. pbCommonAnimation("UseItem",@battlers[i],nil)
  1138. quickclaw[i]=true
  1139. pbDisplayBrief(_INTL("{1}'s {2} let it move first!",
  1140. @battlers[i].pbThis,PBItems.getName(@battlers[i].item)))
  1141. end
  1142. end
  1143. if !quickclaw[i] &&
  1144. (@battlers[i].hasWorkingAbility(:STALL) ||
  1145. @battlers[i].hasWorkingItem(:LAGGINGTAIL) ||
  1146. @battlers[i].hasWorkingItem(:FULLINCENSE))
  1147. lagging[i]=true
  1148. end
  1149. end
  1150. end
  1151. # Calculate each Pokémon's priority bracket, and get the min/max priorities
  1152. for i in 0...4
  1153. # Assume that doing something other than using a move is priority 0
  1154. pri=0
  1155. if @choices[i][0]==1 # Chose to use a move
  1156. pri=@choices[i][2].priority
  1157. pri+=1 if @battlers[i].hasWorkingAbility(:PRANKSTER) &&
  1158. @choices[i][2].pbIsStatus?
  1159. pri+=1 if @battlers[i].hasWorkingAbility(:GALEWINGS) &&
  1160. isConst?(@choices[i][2].type,PBTypes,:FLYING)
  1161. end
  1162. priorities[i]=pri
  1163. if i==0
  1164. minpri=pri
  1165. maxpri=pri
  1166. else
  1167. minpri=pri if minpri>pri
  1168. maxpri=pri if maxpri<pri
  1169. end
  1170. end
  1171. # Find and order all moves with the same priority
  1172. curpri=maxpri
  1173. loop do
  1174. temp.clear
  1175. for j in 0...4
  1176. temp.push(j) if priorities[j]==curpri
  1177. end
  1178. # Sort by speed
  1179. if temp.length==1
  1180. @priority[@priority.length]=@battlers[temp[0]]
  1181. elsif temp.length>1
  1182. n=temp.length
  1183. for m in 0...temp.length-1
  1184. for i in 1...temp.length
  1185. # For each pair of battlers, rank the second compared to the first
  1186. # -1 means rank higher, 0 means rank equal, 1 means rank lower
  1187. cmp=0
  1188. if quickclaw[temp[i]]
  1189. cmp=-1
  1190. if quickclaw[temp[i-1]]
  1191. if speeds[temp[i]]==speeds[temp[i-1]]
  1192. cmp=0
  1193. else
  1194. cmp=(speeds[temp[i]]>speeds[temp[i-1]]) ? -1 : 1
  1195. end
  1196. end
  1197. elsif quickclaw[temp[i-1]]
  1198. cmp=1
  1199. elsif lagging[temp[i]]
  1200. cmp=1
  1201. if lagging[temp[i-1]]
  1202. if speeds[temp[i]]==speeds[temp[i-1]]
  1203. cmp=0
  1204. else
  1205. cmp=(speeds[temp[i]]>speeds[temp[i-1]]) ? 1 : -1
  1206. end
  1207. end
  1208. elsif lagging[temp[i-1]]
  1209. cmp=-1
  1210. elsif speeds[temp[i]]!=speeds[temp[i-1]]
  1211. if @field.effects[PBEffects::TrickRoom]>0
  1212. cmp=(speeds[temp[i]]>speeds[temp[i-1]]) ? 1 : -1
  1213. else
  1214. cmp=(speeds[temp[i]]>speeds[temp[i-1]]) ? -1 : 1
  1215. end
  1216. end
  1217. if cmp<0 || # Swap the pair according to the second battler's rank
  1218. (cmp==0 && pbRandom(2)==0)
  1219. swaptmp=temp[i]
  1220. temp[i]=temp[i-1]
  1221. temp[i-1]=swaptmp
  1222. end
  1223. end
  1224. end
  1225. # Battlers in this bracket are properly sorted, so add them to @priority
  1226. for i in temp
  1227. @priority[@priority.length]=@battlers[i]
  1228. end
  1229. end
  1230. curpri-=1
  1231. break if curpri<minpri
  1232. end
  1233. # Write the priority order to the debug log
  1234. if log
  1235. d="[Priority] "; comma=false
  1236. for i in 0...4
  1237. if @priority[i] && !@priority[i].isFainted?
  1238. d+=", " if comma
  1239. d+="#{@priority[i].pbThis(comma)} (#{@priority[i].index})"; comma=true
  1240. end
  1241. end
  1242. PBDebug.log(d)
  1243. end
  1244. @usepriority=true
  1245. return @priority
  1246. end
  1247.  
  1248. ################################################################################
  1249. # Switching Pokémon.
  1250. ################################################################################
  1251. def pbCanSwitchLax?(idxPokemon,pkmnidxTo,showMessages)
  1252. if pkmnidxTo>=0
  1253. party=pbParty(idxPokemon)
  1254. if pkmnidxTo>=party.length
  1255. return false
  1256. end
  1257. if !party[pkmnidxTo]
  1258. return false
  1259. end
  1260. if party[pkmnidxTo].isEgg?
  1261. pbDisplayPaused(_INTL("An Egg can't battle!")) if showMessages
  1262. return false
  1263. end
  1264. if !pbIsOwner?(idxPokemon,pkmnidxTo)
  1265. owner=pbPartyGetOwner(idxPokemon,pkmnidxTo)
  1266. pbDisplayPaused(_INTL("You can't switch {1}'s Pokémon with one of yours!",owner.name)) if showMessages
  1267. return false
  1268. end
  1269. if party[pkmnidxTo].hp<=0
  1270. pbDisplayPaused(_INTL("{1} has no energy left to battle!",party[pkmnidxTo].name)) if showMessages
  1271. return false
  1272. end
  1273. if @battlers[idxPokemon].pokemonIndex==pkmnidxTo ||
  1274. @battlers[idxPokemon].pbPartner.pokemonIndex==pkmnidxTo
  1275. pbDisplayPaused(_INTL("{1} is already in battle!",party[pkmnidxTo].name)) if showMessages
  1276. return false
  1277. end
  1278. end
  1279. return true
  1280. end
  1281.  
  1282. def pbCanSwitch?(idxPokemon,pkmnidxTo,showMessages,ignoremeanlook=false)
  1283. thispkmn=@battlers[idxPokemon]
  1284. # Multi-Turn Attacks/Mean Look
  1285. if !pbCanSwitchLax?(idxPokemon,pkmnidxTo,showMessages)
  1286. return false
  1287. end
  1288. isOpposing=pbIsOpposing?(idxPokemon)
  1289. party=pbParty(idxPokemon)
  1290. for i in 0...4
  1291. next if isOpposing!=pbIsOpposing?(i)
  1292. if choices[i][0]==2 && choices[i][1]==pkmnidxTo
  1293. pbDisplayPaused(_INTL("{1} has already been selected.",party[pkmnidxTo].name)) if showMessages
  1294. return false
  1295. end
  1296. end
  1297. if thispkmn.hasWorkingItem(:SHEDSHELL)
  1298. return true
  1299. end
  1300. if USENEWBATTLEMECHANICS && thispkmn.pbHasType?(:GHOST)
  1301. return true
  1302. end
  1303. if thispkmn.effects[PBEffects::MultiTurn]>0 ||
  1304. (!ignoremeanlook && thispkmn.effects[PBEffects::MeanLook]>=0)
  1305. pbDisplayPaused(_INTL("{1} can't be switched out!",thispkmn.pbThis)) if showMessages
  1306. return false
  1307. end
  1308. if @field.effects[PBEffects::FairyLock]>0
  1309. pbDisplayPaused(_INTL("{1} can't be switched out!",thispkmn.pbThis)) if showMessages
  1310. return false
  1311. end
  1312. if thispkmn.effects[PBEffects::Ingrain]
  1313. pbDisplayPaused(_INTL("{1} can't be switched out!",thispkmn.pbThis)) if showMessages
  1314. return false
  1315. end
  1316. opp1=thispkmn.pbOpposing1
  1317. opp2=thispkmn.pbOpposing2
  1318. opp=nil
  1319. if thispkmn.pbHasType?(:STEEL)
  1320. opp=opp1 if opp1.hasWorkingAbility(:MAGNETPULL)
  1321. opp=opp2 if opp2.hasWorkingAbility(:MAGNETPULL)
  1322. end
  1323. if !thispkmn.isAirborne?
  1324. opp=opp1 if opp1.hasWorkingAbility(:ARENATRAP)
  1325. opp=opp2 if opp2.hasWorkingAbility(:ARENATRAP)
  1326. end
  1327. if !thispkmn.hasWorkingAbility(:SHADOWTAG)
  1328. opp=opp1 if opp1.hasWorkingAbility(:SHADOWTAG)
  1329. opp=opp2 if opp2.hasWorkingAbility(:SHADOWTAG)
  1330. end
  1331. if opp
  1332. abilityname=PBAbilities.getName(opp.ability)
  1333. pbDisplayPaused(_INTL("{1}'s {2} prevents switching!",opp.pbThis,abilityname)) if showMessages
  1334. return false
  1335. end
  1336. return true
  1337. end
  1338.  
  1339. def pbRegisterSwitch(idxPokemon,idxOther)
  1340. return false if !pbCanSwitch?(idxPokemon,idxOther,false)
  1341. @choices[idxPokemon][0]=2 # "Switch Pokémon"
  1342. @choices[idxPokemon][1]=idxOther # Index of other Pokémon to switch with
  1343. @choices[idxPokemon][2]=nil
  1344. side=(pbIsOpposing?(idxPokemon)) ? 1 : 0
  1345. owner=pbGetOwnerIndex(idxPokemon)
  1346. if @megaEvolution[side][owner]==idxPokemon
  1347. @megaEvolution[side][owner]=-1
  1348. end
  1349. return true
  1350. end
  1351.  
  1352. def pbCanChooseNonActive?(index)
  1353. party=pbParty(index)
  1354. for i in 0...party.length
  1355. return true if pbCanSwitchLax?(index,i,false)
  1356. end
  1357. return false
  1358. end
  1359.  
  1360. def pbSwitch(favorDraws=false)
  1361. if !favorDraws
  1362. return if @decision>0
  1363. else
  1364. return if @decision==5
  1365. end
  1366. pbJudge()
  1367. return if @decision>0
  1368. firstbattlerhp=@battlers[0].hp
  1369. switched=[]
  1370. for index in 0...4
  1371. next if !@doublebattle && pbIsDoubleBattler?(index)
  1372. next if @battlers[index] && !@battlers[index].isFainted?
  1373. next if !pbCanChooseNonActive?(index)
  1374. if !pbOwnedByPlayer?(index)
  1375. if !pbIsOpposing?(index) || (@opponent && pbIsOpposing?(index))
  1376. newenemy=pbSwitchInBetween(index,false,false)
  1377. newenemyname=newenemy
  1378. if isConst?(@party2[newenemy].ability,PBAbilities,:ILLUSION)
  1379. newenemyname=pbGetLastPokeInTeam(index)
  1380. end
  1381. opponent=pbGetOwner(index)
  1382. if !@doublebattle && firstbattlerhp>0 && @shiftStyle && @opponent &&
  1383. @internalbattle && pbCanChooseNonActive?(0) && pbIsOpposing?(index) &&
  1384. @battlers[0].effects[PBEffects::Outrage]==0
  1385. pbDisplayPaused(_INTL("{1} is about to send in {2}.",opponent.fullname,@party2[newenemyname].name))
  1386. if pbDisplayConfirm(_INTL("Will {1} change Pokémon?",self.pbPlayer.name))
  1387. newpoke=pbSwitchPlayer(0,true,true)
  1388. if newpoke>=0
  1389. newpokename=newpoke
  1390. if isConst?(@party1[newpoke].ability,PBAbilities,:ILLUSION)
  1391. newpokename=pbGetLastPokeInTeam(0)
  1392. end
  1393. pbDisplayBrief(_INTL("{1}, that's enough! Come back!",@battlers[0].name))
  1394. pbRecallAndReplace(0,newpoke,newpokename)
  1395. switched.push(0)
  1396. end
  1397. end
  1398. end
  1399. pbRecallAndReplace(index,newenemy,newenemyname,false,false)
  1400. switched.push(index)
  1401. end
  1402. elsif @opponent
  1403. newpoke=pbSwitchInBetween(index,true,false)
  1404. newpokename=newpoke
  1405. if isConst?(@party1[newpoke].ability,PBAbilities,:ILLUSION)
  1406. newpokename=pbGetLastPokeInTeam(index)
  1407. end
  1408. pbRecallAndReplace(index,newpoke,newpokename)
  1409. switched.push(index)
  1410. else
  1411. switch=false
  1412. if !pbDisplayConfirm(_INTL("Use next Pokémon?"))
  1413. switch=(pbRun(index,true)<=0)
  1414. else
  1415. switch=true
  1416. end
  1417. if switch
  1418. newpoke=pbSwitchInBetween(index,true,false)
  1419. newpokename=newpoke
  1420. if isConst?(@party1[newpoke].ability,PBAbilities,:ILLUSION)
  1421. newpokename=pbGetLastPokeInTeam(index)
  1422. end
  1423. pbRecallAndReplace(index,newpoke,newpokename)
  1424. switched.push(index)
  1425. end
  1426. end
  1427. end
  1428. if switched.length>0
  1429. priority=pbPriority
  1430. for i in priority
  1431. i.pbAbilitiesOnSwitchIn(true) if switched.include?(i.index)
  1432. end
  1433. end
  1434. end
  1435.  
  1436. def pbSendOut(index,pokemon)
  1437. pbSetSeen(pokemon)
  1438. @peer.pbOnEnteringBattle(self,pokemon)
  1439. if pbIsOpposing?(index)
  1440. @scene.pbTrainerSendOut(index,pokemon)
  1441. else
  1442. @scene.pbSendOut(index,pokemon)
  1443. end
  1444. @scene.pbResetMoveIndex(index)
  1445. end
  1446.  
  1447. def pbReplace(index,newpoke,batonpass=false)
  1448. party=pbParty(index)
  1449. oldpoke=@battlers[index].pokemonIndex
  1450. # Initialise the new Pokémon
  1451. @battlers[index].pbInitialize(party[newpoke],newpoke,batonpass)
  1452. # Reorder the party for this battle
  1453. partyorder=(!pbIsOpposing?(index)) ? @party1order : @party2order
  1454. bpo=-1; bpn=-1
  1455. for i in 0...partyorder.length
  1456. bpo=i if partyorder[i]==oldpoke
  1457. bpn=i if partyorder[i]==newpoke
  1458. end
  1459. p=partyorder[bpo]; partyorder[bpo]=partyorder[bpn]; partyorder[bpn]=p
  1460. # Send out the new Pokémon
  1461. pbSendOut(index,party[newpoke])
  1462. pbSetSeen(party[newpoke])
  1463. end
  1464.  
  1465. def pbRecallAndReplace(index,newpoke,newpokename=-1,batonpass=false,moldbreaker=false)
  1466. @battlers[index].pbResetForm
  1467. if !@battlers[index].isFainted?
  1468. @scene.pbRecall(index)
  1469. end
  1470. pbMessagesOnReplace(index,newpoke,newpokename)
  1471. pbReplace(index,newpoke,batonpass)
  1472. return pbOnActiveOne(@battlers[index],false,moldbreaker)
  1473. end
  1474.  
  1475. def pbMessagesOnReplace(index,newpoke,newpokename=-1)
  1476. newpokename=newpoke if newpokename<0
  1477. party=pbParty(index)
  1478. if pbOwnedByPlayer?(index)
  1479. # if !party[newpoke]
  1480. # p [index,newpoke,party[newpoke],pbAllFainted?(party)]
  1481. # PBDebug.log([index,newpoke,party[newpoke],"pbMOR"].inspect)
  1482. # for i in 0...party.length
  1483. # PBDebug.log([i,party[i].hp].inspect)
  1484. # end
  1485. # raise BattleAbortedException.new
  1486. # end
  1487. opposing=@battlers[index].pbOppositeOpposing
  1488. if opposing.isFainted? || opposing.hp==opposing.totalhp
  1489. pbDisplayBrief(_INTL("Go! {1}!",party[newpokename].name))
  1490. elsif opposing.hp>=(opposing.totalhp/2)
  1491. pbDisplayBrief(_INTL("Do it! {1}!",party[newpokename].name))
  1492. elsif opposing.hp>=(opposing.totalhp/4)
  1493. pbDisplayBrief(_INTL("Go for it, {1}!",party[newpokename].name))
  1494. else
  1495. pbDisplayBrief(_INTL("Your opponent's weak!\nGet 'em, {1}!",party[newpokename].name))
  1496. end
  1497. PBDebug.log("[Send out Pokémon] Player sent out #{party[newpokename].name} in position #{index}")
  1498. else
  1499. # if !party[newpoke]
  1500. # p [index,newpoke,party[newpoke],pbAllFainted?(party)]
  1501. # PBDebug.log([index,newpoke,party[newpoke],"pbMOR"].inspect)
  1502. # for i in 0...party.length
  1503. # PBDebug.log([i,party[i].hp].inspect)
  1504. # end
  1505. # raise BattleAbortedException.new
  1506. # end
  1507. owner=pbGetOwner(index)
  1508. pbDisplayBrief(_INTL("{1} sent\r\nout {2}!",owner.fullname,party[newpokename].name))
  1509. PBDebug.log("[Send out Pokémon] Opponent sent out #{party[newpokename].name} in position #{index}")
  1510. end
  1511. end
  1512.  
  1513. def pbSwitchInBetween(index,lax,cancancel)
  1514. if !pbOwnedByPlayer?(index)
  1515. return @scene.pbChooseNewEnemy(index,pbParty(index))
  1516. else
  1517. return pbSwitchPlayer(index,lax,cancancel)
  1518. end
  1519. end
  1520.  
  1521. def pbSwitchPlayer(index,lax,cancancel)
  1522. if @debug
  1523. return @scene.pbChooseNewEnemy(index,pbParty(index))
  1524. else
  1525. return @scene.pbSwitch(index,lax,cancancel)
  1526. end
  1527. end
  1528.  
  1529. ################################################################################
  1530. # Using an item.
  1531. ################################################################################
  1532. # Uses an item on a Pokémon in the player's party.
  1533. def pbUseItemOnPokemon(item,pkmnIndex,userPkmn,scene)
  1534. pokemon=@party1[pkmnIndex]
  1535. battler=nil
  1536. name=pbGetOwner(userPkmn.index).fullname
  1537. name=pbGetOwner(userPkmn.index).name if pbBelongsToPlayer?(userPkmn.index)
  1538. pbDisplayBrief(_INTL("{1} used the\r\n{2}.",name,PBItems.getName(item)))
  1539. PBDebug.log("[Use item] Player used #{PBItems.getName(item)} on #{pokemon.name}")
  1540. ret=false
  1541. if pokemon.isEgg?
  1542. pbDisplay(_INTL("But it had no effect!"))
  1543. else
  1544. for i in 0...4
  1545. if !pbIsOpposing?(i) && @battlers[i].pokemonIndex==pkmnIndex
  1546. battler=@battlers[i]
  1547. end
  1548. end
  1549. ret=ItemHandlers.triggerBattleUseOnPokemon(item,pokemon,battler,scene)
  1550. end
  1551. if !ret && pbBelongsToPlayer?(userPkmn.index)
  1552. if $PokemonBag.pbCanStore?(item)
  1553. $PokemonBag.pbStoreItem(item)
  1554. else
  1555. raise _INTL("Couldn't return unused item to Bag somehow.")
  1556. end
  1557. end
  1558. return ret
  1559. end
  1560.  
  1561. # Uses an item on an active Pokémon.
  1562. def pbUseItemOnBattler(item,index,userPkmn,scene)
  1563. PBDebug.log("[Use item] Player used #{PBItems.getName(item)} on #{@battlers[index].pbThis(true)}")
  1564. ret=ItemHandlers.triggerBattleUseOnBattler(item,@battlers[index],scene)
  1565. if !ret && pbBelongsToPlayer?(userPkmn.index)
  1566. if $PokemonBag.pbCanStore?(item)
  1567. $PokemonBag.pbStoreItem(item)
  1568. else
  1569. raise _INTL("Couldn't return unused item to Bag somehow.")
  1570. end
  1571. end
  1572. return ret
  1573. end
  1574.  
  1575. def pbRegisterItem(idxPokemon,idxItem,idxTarget=nil)
  1576. if idxTarget>=0 && @battlers[idxTarget].effects[PBEffects::Embargo]>0
  1577. pbDisplay(_INTL("Embargo's effect prevents the item's use on {1}!",@battlers[idxTarget].pbThis(true)))
  1578. return false
  1579. end
  1580. if ItemHandlers.hasUseInBattle(idxItem)
  1581. if idxPokemon==0 # Player's first Pokémon
  1582. if ItemHandlers.triggerBattleUseOnBattler(idxItem,@battlers[idxPokemon],self)
  1583. ItemHandlers.triggerUseInBattle(idxItem,@battlers[idxPokemon],self)
  1584. if @doublebattle
  1585. @choices[idxPokemon+2][0]=3 # "Use an item"
  1586. @choices[idxPokemon+2][1]=idxItem # ID of item to be used
  1587. @choices[idxPokemon+2][2]=idxTarget # Index of Pokémon to use item on
  1588. end
  1589. else
  1590. if $PokemonBag.pbCanStore?(idxItem)
  1591. $PokemonBag.pbStoreItem(idxItem)
  1592. else
  1593. raise _INTL("Couldn't return unusable item to Bag somehow.")
  1594. end
  1595. return false
  1596. end
  1597. else
  1598. if ItemHandlers.triggerBattleUseOnBattler(idxItem,@battlers[idxPokemon],self)
  1599. pbDisplay(_INTL("It's impossible to aim without being focused!"))
  1600. end
  1601. return false
  1602. end
  1603. end
  1604. @choices[idxPokemon][0]=3 # "Use an item"
  1605. @choices[idxPokemon][1]=idxItem # ID of item to be used
  1606. @choices[idxPokemon][2]=idxTarget # Index of Pokémon to use item on
  1607. side=(pbIsOpposing?(idxPokemon)) ? 1 : 0
  1608. owner=pbGetOwnerIndex(idxPokemon)
  1609. if @megaEvolution[side][owner]==idxPokemon
  1610. @megaEvolution[side][owner]=-1
  1611. end
  1612. return true
  1613. end
  1614.  
  1615. def pbEnemyUseItem(item,battler)
  1616. return 0 if !@internalbattle
  1617. items=pbGetOwnerItems(battler.index)
  1618. return if !items
  1619. opponent=pbGetOwner(battler.index)
  1620. for i in 0...items.length
  1621. if items[i]==item
  1622. items.delete_at(i)
  1623. break
  1624. end
  1625. end
  1626. itemname=PBItems.getName(item)
  1627. pbDisplayBrief(_INTL("{1} used the\r\n{2}!",opponent.fullname,itemname))
  1628. PBDebug.log("[Use item] Opponent used #{itemname} on #{battler.pbThis(true)}")
  1629. if isConst?(item,PBItems,:POTION)
  1630. battler.pbRecoverHP(20,true)
  1631. pbDisplay(_INTL("{1}'s HP was restored.",battler.pbThis))
  1632. elsif isConst?(item,PBItems,:SUPERPOTION)
  1633. battler.pbRecoverHP(50,true)
  1634. pbDisplay(_INTL("{1}'s HP was restored.",battler.pbThis))
  1635. elsif isConst?(item,PBItems,:HYPERPOTION)
  1636. battler.pbRecoverHP(200,true)
  1637. pbDisplay(_INTL("{1}'s HP was restored.",battler.pbThis))
  1638. elsif isConst?(item,PBItems,:MAXPOTION)
  1639. battler.pbRecoverHP(battler.totalhp-battler.hp,true)
  1640. pbDisplay(_INTL("{1}'s HP was restored.",battler.pbThis))
  1641. elsif isConst?(item,PBItems,:FULLRESTORE)
  1642. fullhp=(battler.hp==battler.totalhp)
  1643. battler.pbRecoverHP(battler.totalhp-battler.hp,true)
  1644. battler.status=0; battler.statusCount=0
  1645. battler.effects[PBEffects::Confusion]=0
  1646. if fullhp
  1647. pbDisplay(_INTL("{1} became healthy!",battler.pbThis))
  1648. else
  1649. pbDisplay(_INTL("{1}'s HP was restored.",battler.pbThis))
  1650. end
  1651. elsif isConst?(item,PBItems,:FULLHEAL)
  1652. battler.status=0; battler.statusCount=0
  1653. battler.effects[PBEffects::Confusion]=0
  1654. pbDisplay(_INTL("{1} became healthy!",battler.pbThis))
  1655. elsif isContst?(item,PBItems,:BIGMALASADA)
  1656. battler.status=0; battler.statusCount=0
  1657. battler.effects[PBEffects::Confusion]=0
  1658. pbDisplay(_INTL("{1} became healthy!",battler.pbThis))
  1659. elsif isConst?(item,PBItems,:XATTACK)
  1660. if battler.pbCanIncreaseStatStage?(PBStats::ATTACK,battler)
  1661. battler.pbIncreaseStat(PBStats::ATTACK,1,battler,true)
  1662. end
  1663. elsif isConst?(item,PBItems,:XDEFEND)
  1664. if battler.pbCanIncreaseStatStage?(PBStats::DEFENSE,battler)
  1665. battler.pbIncreaseStat(PBStats::DEFENSE,1,battler,true)
  1666. end
  1667. elsif isConst?(item,PBItems,:XSPEED)
  1668. if battler.pbCanIncreaseStatStage?(PBStats::SPEED,battler)
  1669. battler.pbIncreaseStat(PBStats::SPEED,1,battler,true)
  1670. end
  1671. elsif isConst?(item,PBItems,:XSPECIAL)
  1672. if battler.pbCanIncreaseStatStage?(PBStats::SPATK,battler)
  1673. battler.pbIncreaseStat(PBStats::SPATK,1,battler,true)
  1674. end
  1675. elsif isConst?(item,PBItems,:XSPDEF)
  1676. if battler.pbCanIncreaseStatStage?(PBStats::SPDEF,battler)
  1677. battler.pbIncreaseStat(PBStats::SPDEF,1,battler,true)
  1678. end
  1679. elsif isConst?(item,PBItems,:XACCURACY)
  1680. if battler.pbCanIncreaseStatStage?(PBStats::ACCURACY,battler)
  1681. battler.pbIncreaseStat(PBStats::ACCURACY,1,battler,true)
  1682. end
  1683. end
  1684. end
  1685.  
  1686. ################################################################################
  1687. # Fleeing from battle.
  1688. ################################################################################
  1689. def pbCanRun?(idxPokemon)
  1690. return false if @opponent
  1691. return false if @cantescape && !pbIsOpposing?(idsPokemon)
  1692. thispkmn=@battlers[idxPokemon]
  1693. return true if thispkmn.pbHasType?(:GHOST) && USENEWBATTLEMECHANICS
  1694. return true if thispkmn.hasWorkingItem(:SMOKEBALL)
  1695. return true if thispkmn.hasWorkingAbility(:RUNAWAY)
  1696. return pbCanSwitch?(idxPokemon,-1,false)
  1697. end
  1698.  
  1699. def pbRun(idxPokemon,duringBattle=false)
  1700. thispkmn=@battlers[idxPokemon]
  1701. if pbIsOpposing?(idxPokemon)
  1702. return 0 if @opponent
  1703. @choices[i][0]=5 # run
  1704. @choices[i][1]=0
  1705. @choices[i][2]=nil
  1706. return -1
  1707. end
  1708. if @opponent
  1709. if $DEBUG && Input.press?(Input::CTRL)
  1710. if pbDisplayConfirm(_INTL("Treat this battle as a win?"))
  1711. @decision=1
  1712. return 1
  1713. elsif pbDisplayConfirm(_INTL("Treat this battle as a loss?"))
  1714. @decision=2
  1715. return 1
  1716. end
  1717. elsif @internalbattle
  1718. pbDisplayPaused(_INTL("No! There's no running from a Trainer battle!"))
  1719. elsif pbDisplayConfirm(_INTL("Would you like to forfeit the match and quit now?"))
  1720. pbDisplay(_INTL("{1} forfeited the match!",self.pbPlayer.name))
  1721. @decision=3
  1722. return 1
  1723. end
  1724. return 0
  1725. end
  1726. if $DEBUG && Input.press?(Input::CTRL)
  1727. pbDisplayPaused(_INTL("Got away safely!"))
  1728. @decision=3
  1729. return 1
  1730. end
  1731. if @cantescape
  1732. pbDisplayPaused(_INTL("Can't escape!"))
  1733. return 0
  1734. end
  1735. if thispkmn.pbHasType?(:GHOST) && USENEWBATTLEMECHANICS
  1736. pbDisplayPaused(_INTL("Got away safely!"))
  1737. @decision=3
  1738. return 1
  1739. end
  1740. if thispkmn.hasWorkingAbility(:RUNAWAY)
  1741. if duringBattle
  1742. pbDisplayPaused(_INTL("Got away safely!"))
  1743. else
  1744. pbDisplayPaused(_INTL("{1} escaped using Run Away!",thispkmn.pbThis))
  1745. end
  1746. @decision=3
  1747. return 1
  1748. end
  1749. if thispkmn.hasWorkingItem(:SMOKEBALL)
  1750. if duringBattle
  1751. pbDisplayPaused(_INTL("Got away safely!"))
  1752. else
  1753. pbDisplayPaused(_INTL("{1} escaped using its {2}!",thispkmn.pbThis,PBItems.getName(thispkmn.item)))
  1754. end
  1755. @decision=3
  1756. return 1
  1757. end
  1758. if !duringBattle && !pbCanSwitch?(idxPokemon,-1,false)
  1759. pbDisplayPaused(_INTL("Can't escape!"))
  1760. return 0
  1761. end
  1762. # Note: not pbSpeed, because using unmodified Speed
  1763. speedPlayer=@battlers[idxPokemon].speed
  1764. opposing=@battlers[idxPokemon].pbOppositeOpposing
  1765. opposing=opposing.pbPartner if opposing.isFainted?
  1766. if !opposing.isFainted?
  1767. speedEnemy=opposing.speed
  1768. if speedPlayer>speedEnemy
  1769. rate=256
  1770. else
  1771. speedEnemy=1 if speedEnemy<=0
  1772. rate=speedPlayer*128/speedEnemy
  1773. rate+=@runCommand*30
  1774. rate&=0xFF
  1775. end
  1776. else
  1777. rate=256
  1778. end
  1779. ret=1
  1780. if pbAIRandom(256)<rate
  1781. pbDisplayPaused(_INTL("Got away safely!"))
  1782. @decision=3
  1783. else
  1784. pbDisplayPaused(_INTL("Can't escape!"))
  1785. ret=-1
  1786. end
  1787. @runCommand+=1 if !duringBattle
  1788. return ret
  1789. end
  1790.  
  1791. ################################################################################
  1792. # Mega Evolve battler.
  1793. ################################################################################
  1794. def pbCanMegaEvolve?(index)
  1795. return false if $game_switches[NO_MEGA_EVOLUTION]
  1796. return false if !@battlers[index].hasMega?
  1797. return true if $DEBUG && Input.press?(Input::CTRL)
  1798. return false if !pbHasMegaRing(index)
  1799. side=(pbIsOpposing?(index)) ? 1 : 0
  1800. owner=pbGetOwnerIndex(index)
  1801. return false if @megaEvolution[side][owner]!=-1
  1802. return false if @battlers[index].effects[PBEffects::SkyDrop]
  1803. return true
  1804. end
  1805.  
  1806. def pbRegisterMegaEvolution(index)
  1807. side=(pbIsOpposing?(index)) ? 1 : 0
  1808. owner=pbGetOwnerIndex(index)
  1809. @megaEvolution[side][owner]=index
  1810. end
  1811.  
  1812. def pbMegaEvolve(index)
  1813. return if !@battlers[index] || !@battlers[index].pokemon
  1814. return if !(@battlers[index].hasMega? rescue false)
  1815. return if (@battlers[index].isMega? rescue true)
  1816. ownername=pbGetOwner(index).fullname
  1817. ownername=pbGetOwner(index).name if pbBelongsToPlayer?(index)
  1818. case (@battlers[index].pokemon.megaMessage rescue 0)
  1819. when 1 # Rayquaza
  1820. pbDisplay(_INTL("{1}'s fervent wish has reached {2}!",ownername,@battlers[index].pbThis))
  1821. else
  1822. pbDisplay(_INTL("{1}'s {2} is reacting to {3}'s {4}!",
  1823. @battlers[index].pbThis,PBItems.getName(@battlers[index].item),
  1824. ownername,pbGetMegaRingName(index)))
  1825. end
  1826. pbCommonAnimation("MegaEvolution",@battlers[index],nil)
  1827. @battlers[index].pokemon.makeMega
  1828. @battlers[index].form=@battlers[index].pokemon.form
  1829. @battlers[index].pbUpdate(true)
  1830. @scene.pbChangePokemon(@battlers[index],@battlers[index].pokemon)
  1831. pbCommonAnimation("MegaEvolution2",@battlers[index],nil)
  1832. meganame=(@battlers[index].pokemon.megaName rescue nil)
  1833. if !meganame || meganame==""
  1834. meganame=_INTL("Mega {1}",PBSpecies.getName(@battlers[index].pokemon.species))
  1835. end
  1836. pbDisplay(_INTL("{1} has Mega Evolved into {2}!",@battlers[index].pbThis,meganame))
  1837. PBDebug.log("[Mega Evolution] #{@battlers[index].pbThis} Mega Evolved")
  1838. side=(pbIsOpposing?(index)) ? 1 : 0
  1839. owner=pbGetOwnerIndex(index)
  1840. @megaEvolution[side][owner]=-2
  1841. end
  1842.  
  1843. ################################################################################
  1844. # Primal Revert battler.
  1845. ################################################################################
  1846. def pbPrimalReversion(index)
  1847. return if !@battlers[index] || !@battlers[index].pokemon
  1848. return if !(@battlers[index].hasPrimal? rescue false)
  1849. return if (@battlers[index].isPrimal? rescue true)
  1850. if isConst?(@battlers[index].pokemon.species,PBSpecies,:KYOGRE)
  1851. pbCommonAnimation("PrimalKyogre",@battlers[index],nil)
  1852. elsif isConst?(@battlers[index].pokemon.species,PBSpecies,:GROUDON)
  1853. pbCommonAnimation("PrimalGroudon",@battlers[index],nil)
  1854. end
  1855. @battlers[index].pokemon.makePrimal
  1856. @battlers[index].form=@battlers[index].pokemon.form
  1857. @battlers[index].pbUpdate(true)
  1858. @scene.pbChangePokemon(@battlers[index],@battlers[index].pokemon)
  1859. if isConst?(@battlers[index].pokemon.species,PBSpecies,:KYOGRE)
  1860. pbCommonAnimation("PrimalKyogre2",@battlers[index],nil)
  1861. elsif isConst?(@battlers[index].pokemon.species,PBSpecies,:GROUDON)
  1862. pbCommonAnimation("PrimalGroudon2",@battlers[index],nil)
  1863. end
  1864. pbDisplay(_INTL("{1}'s Primal Reversion!\nIt reverted to its primal form!",@battlers[index].pbThis))
  1865. PBDebug.log("[Primal Reversion] #{@battlers[index].pbThis} Primal Reverted")
  1866. end
  1867.  
  1868. ################################################################################
  1869. # Call battler.
  1870. ################################################################################
  1871. def pbCall(index)
  1872. owner=pbGetOwner(index)
  1873. pbDisplay(_INTL("{1} called {2}!",owner.name,@battlers[index].name))
  1874. pbDisplay(_INTL("{1}!",@battlers[index].name))
  1875. PBDebug.log("[Call to Pokémon] #{owner.name} called to #{@battlers[index].pbThis(true)}")
  1876. if @battlers[index].isShadow?
  1877. if @battlers[index].inHyperMode?
  1878. @battlers[index].pokemon.hypermode=false
  1879. @battlers[index].pokemon.adjustHeart(-300)
  1880. pbDisplay(_INTL("{1} came to its senses from the Trainer's call!",@battlers[index].pbThis))
  1881. else
  1882. pbDisplay(_INTL("But nothing happened!"))
  1883. end
  1884. elsif @battlers[index].status!=PBStatuses::SLEEP &&
  1885. @battlers[index].pbCanIncreaseStatStage?(PBStats::ACCURACY,@battlers[index])
  1886. @battlers[index].pbIncreaseStat(PBStats::ACCURACY,1,@battlers[index],true)
  1887. else
  1888. pbDisplay(_INTL("But nothing happened!"))
  1889. end
  1890. end
  1891.  
  1892. ################################################################################
  1893. # Gaining Experience.
  1894. ################################################################################
  1895. def pbGainEXP
  1896. return if !@internalbattle
  1897. successbegin=true
  1898. for i in 0...4 # Not ordered by priority
  1899. if !@doublebattle && pbIsDoubleBattler?(i)
  1900. @battlers[i].participants=[]
  1901. next
  1902. end
  1903. if pbIsOpposing?(i) && @battlers[i].participants.length>0 &&
  1904. (@battlers[i].isFainted? || @battlers[i].captured)
  1905. haveexpall=(hasConst?(PBItems,:EXPALL) && $PokemonBag.pbQuantity(:EXPALL)>0)
  1906. # First count the number of participants
  1907. partic=0
  1908. expshare=0
  1909. for j in @battlers[i].participants
  1910. next if !@party1[j] || !pbIsOwner?(0,j)
  1911. partic+=1 if @party1[j].hp>0 && !@party1[j].isEgg?
  1912. end
  1913. if !haveexpall
  1914. for j in 0...@party1.length
  1915. next if !@party1[j] || !pbIsOwner?(0,j)
  1916. expshare+=1 if @party1[j].hp>0 && !@party1[j].isEgg? &&
  1917. (isConst?(@party1[j].item,PBItems,:EXPSHARE) ||
  1918. isConst?(@party1[j].itemInitial,PBItems,:EXPSHARE))
  1919. end
  1920. end
  1921. # Now calculate EXP for the participants
  1922. if partic>0 || expshare>0 || haveexpall
  1923. if !@opponent && successbegin && pbAllFainted?(@party2)
  1924. @scene.pbWildBattleSuccess
  1925. successbegin=false
  1926. end
  1927. for j in 0...@party1.length
  1928. next if !@party1[j] || !pbIsOwner?(0,j)
  1929. next if @party1[j].hp<=0 || @party1[j].isEgg?
  1930. haveexpshare=(isConst?(@party1[j].item,PBItems,:EXPSHARE) ||
  1931. isConst?(@party1[j].itemInitial,PBItems,:EXPSHARE))
  1932. next if !haveexpshare && !@battlers[i].participants.include?(j)
  1933. pbGainExpOne(j,@battlers[i],partic,expshare,haveexpall)
  1934. end
  1935. if haveexpall
  1936. showmessage=true
  1937. for j in 0...@party1.length
  1938. next if !@party1[j] || !pbIsOwner?(0,j)
  1939. next if @party1[j].hp<=0 || @party1[j].isEgg?
  1940. next if isConst?(@party1[j].item,PBItems,:EXPSHARE) ||
  1941. isConst?(@party1[j].itemInitial,PBItems,:EXPSHARE)
  1942. next if @battlers[i].participants.include?(j)
  1943. pbDisplayPaused(_INTL("The rest of your team gained Exp. Points thanks to the {1}!",
  1944. PBItems.getName(getConst(PBItems,:EXPALL)))) if showmessage
  1945. showmessage=false
  1946. pbGainExpOne(j,@battlers[i],partic,expshare,haveexpall,false)
  1947. end
  1948. end
  1949. end
  1950. # Now clear the participants array
  1951. @battlers[i].participants=[]
  1952. end
  1953. end
  1954. end
  1955.  
  1956. def pbGainExpOne(index,defeated,partic,expshare,haveexpall,showmessages=true)
  1957. thispoke=@party1[index]
  1958. # Original species, not current species
  1959. level=defeated.level
  1960. baseexp=defeated.pokemon.baseExp
  1961. evyield=defeated.pokemon.evYield
  1962. ispartic=0
  1963. ispartic=1 if defeated.participants.include?(index)
  1964. haveexpshare=(isConst?(thispoke.item,PBItems,:EXPSHARE) ||
  1965. isConst?(thispoke.itemInitial,PBItems,:EXPSHARE)) ? 1 : 0
  1966. exp=0
  1967. if expshare>0
  1968. if partic==0 # No participants, all Exp goes to Exp Share holders
  1969. exp=(level*baseexp).floor
  1970. exp=(exp/(NOSPLITEXP ? 1 : expshare)).floor*haveexpshare
  1971. else
  1972. if NOSPLITEXP
  1973. exp=(level*baseexp).floor*ispartic
  1974. exp=(level*baseexp/2).floor*haveexpshare if ispartic==0
  1975. else
  1976. exp=(level*baseexp/2).floor
  1977. exp=(exp/partic).floor*ispartic + (exp/expshare).floor*haveexpshare
  1978. end
  1979. end
  1980. elsif ispartic==1
  1981. exp=(level*baseexp/(NOSPLITEXP ? 1 : partic)).floor
  1982. elsif haveexpall
  1983. exp=(level*baseexp/2).floor
  1984. end
  1985. return if exp<=0
  1986. exp=(exp*3/2).floor if @opponent
  1987. if USESCALEDEXPFORMULA
  1988. exp=(exp/5).floor
  1989. leveladjust=(2*level+10.0)/(level+thispoke.level+10.0)
  1990. leveladjust=leveladjust**5
  1991. leveladjust=Math.sqrt(leveladjust)
  1992. exp=(exp*leveladjust).floor
  1993. exp+=1 if ispartic>0 || haveexpshare>0
  1994. else
  1995. exp=(exp/7).floor
  1996. end
  1997. isOutsider=(thispoke.trainerID!=self.pbPlayer.id ||
  1998. (thispoke.language!=0 && thispoke.language!=self.pbPlayer.language))
  1999. if isOutsider
  2000. if thispoke.language!=0 && thispoke.language!=self.pbPlayer.language
  2001. exp=(exp*1.7).floor
  2002. else
  2003. exp=(exp*3/2).floor
  2004. end
  2005. end
  2006. exp=(exp*3/2).floor if isConst?(thispoke.item,PBItems,:LUCKYEGG) ||
  2007. isConst?(thispoke.itemInitial,PBItems,:LUCKYEGG)
  2008. growthrate=thispoke.growthrate
  2009. newexp=PBExperience.pbAddExperience(thispoke.exp,exp,growthrate)
  2010. exp=newexp-thispoke.exp
  2011. if exp>0
  2012. if showmessages
  2013. if isOutsider
  2014. pbDisplayPaused(_INTL("{1} gained a boosted {2} Exp. Points!",thispoke.name,exp))
  2015. else
  2016. pbDisplayPaused(_INTL("{1} gained {2} Exp. Points!",thispoke.name,exp))
  2017. end
  2018. end
  2019. # Gain effort value points, using RS effort values
  2020. totalev=0
  2021. for k in 0...6
  2022. totalev+=thispoke.ev[k]
  2023. end
  2024. for k in 0...6
  2025. evgain=evyield[k]
  2026. evgain*=2 if isConst?(thispoke.item,PBItems,:MACHOBRACE) ||
  2027. isConst?(thispoke.itemInitial,PBItems,:MACHOBRACE)
  2028. case k
  2029. when PBStats::HP
  2030. evgain+=4 if isConst?(thispoke.item,PBItems,:POWERWEIGHT) ||
  2031. isConst?(thispoke.itemInitial,PBItems,:POWERWEIGHT)
  2032. when PBStats::ATTACK
  2033. evgain+=4 if isConst?(thispoke.item,PBItems,:POWERBRACER) ||
  2034. isConst?(thispoke.itemInitial,PBItems,:POWERBRACER)
  2035. when PBStats::DEFENSE
  2036. evgain+=4 if isConst?(thispoke.item,PBItems,:POWERBELT) ||
  2037. isConst?(thispoke.itemInitial,PBItems,:POWERBELT)
  2038. when PBStats::SPATK
  2039. evgain+=4 if isConst?(thispoke.item,PBItems,:POWERLENS) ||
  2040. isConst?(thispoke.itemInitial,PBItems,:POWERLENS)
  2041. when PBStats::SPDEF
  2042. evgain+=4 if isConst?(thispoke.item,PBItems,:POWERBAND) ||
  2043. isConst?(thispoke.itemInitial,PBItems,:POWERBAND)
  2044. when PBStats::SPEED
  2045. evgain+=4 if isConst?(thispoke.item,PBItems,:POWERANKLET) ||
  2046. isConst?(thispoke.itemInitial,PBItems,:POWERANKLET)
  2047. end
  2048. evgain*=2 if thispoke.pokerusStage>=1 # Infected or cured
  2049. if evgain>0
  2050. # Can't exceed overall limit
  2051. evgain-=totalev+evgain-PokeBattle_Pokemon::EVLIMIT if totalev+evgain>PokeBattle_Pokemon::EVLIMIT
  2052. # Can't exceed stat limit
  2053. evgain-=thispoke.ev[k]+evgain-PokeBattle_Pokemon::EVSTATLIMIT if thispoke.ev[k]+evgain>PokeBattle_Pokemon::EVSTATLIMIT
  2054. # Add EV gain
  2055. thispoke.ev[k]+=evgain
  2056. if thispoke.ev[k]>PokeBattle_Pokemon::EVSTATLIMIT
  2057. print "Single-stat EV limit #{PokeBattle_Pokemon::EVSTATLIMIT} exceeded.\r\nStat: #{k} EV gain: #{evgain} EVs: #{thispoke.ev.inspect}"
  2058. thispoke.ev[k]=PokeBattle_Pokemon::EVSTATLIMIT
  2059. end
  2060. totalev+=evgain
  2061. if totalev>PokeBattle_Pokemon::EVLIMIT
  2062. print "EV limit #{PokeBattle_Pokemon::EVLIMIT} exceeded.\r\nTotal EVs: #{totalev} EV gain: #{evgain} EVs: #{thispoke.ev.inspect}"
  2063. end
  2064. end
  2065. end
  2066. newlevel=PBExperience.pbGetLevelFromExperience(newexp,growthrate)
  2067. tempexp=0
  2068. curlevel=thispoke.level
  2069. if newlevel<curlevel
  2070. debuginfo="#{thispoke.name}: #{thispoke.level}/#{newlevel} | #{thispoke.exp}/#{newexp} | gain: #{exp}"
  2071. raise RuntimeError.new(_INTL("The new level ({1}) is less than the Pokémon's\r\ncurrent level ({2}), which shouldn't happen.\r\n[Debug: {3}]",
  2072. newlevel,curlevel,debuginfo))
  2073. return
  2074. end
  2075. if thispoke.respond_to?("isShadow?") && thispoke.isShadow?
  2076. thispoke.exp+=exp
  2077. else
  2078. tempexp1=thispoke.exp
  2079. tempexp2=0
  2080. # Find battler
  2081. battler=pbFindPlayerBattler(index)
  2082. loop do
  2083. # EXP Bar animation
  2084. startexp=PBExperience.pbGetStartExperience(curlevel,growthrate)
  2085. endexp=PBExperience.pbGetStartExperience(curlevel+1,growthrate)
  2086. tempexp2=(endexp<newexp) ? endexp : newexp
  2087. thispoke.exp=tempexp2
  2088. @scene.pbEXPBar(thispoke,battler,startexp,endexp,tempexp1,tempexp2)
  2089. tempexp1=tempexp2
  2090. curlevel+=1
  2091. if curlevel>newlevel
  2092. thispoke.calcStats
  2093. battler.pbUpdate(false) if battler
  2094. @scene.pbRefresh
  2095. break
  2096. end
  2097. oldtotalhp=thispoke.totalhp
  2098. oldattack=thispoke.attack
  2099. olddefense=thispoke.defense
  2100. oldspeed=thispoke.speed
  2101. oldspatk=thispoke.spatk
  2102. oldspdef=thispoke.spdef
  2103. if battler && battler.pokemon && @internalbattle
  2104. battler.pokemon.changeHappiness("level up")
  2105. end
  2106. thispoke.calcStats
  2107. battler.pbUpdate(false) if battler
  2108. @scene.pbRefresh
  2109. pbDisplayPaused(_INTL("{1} grew to Level {2}!",thispoke.name,curlevel))
  2110. @scene.pbLevelUp(thispoke,battler,oldtotalhp,oldattack,
  2111. olddefense,oldspeed,oldspatk,oldspdef)
  2112. # Finding all moves learned at this level
  2113. movelist=thispoke.getMoveList
  2114. for k in movelist
  2115. if k[0]==thispoke.level # Learned a new move
  2116. pbLearnMove(index,k[1])
  2117. end
  2118. end
  2119. end
  2120. end
  2121. end
  2122. end
  2123.  
  2124. ################################################################################
  2125. # Learning a move.
  2126. ################################################################################
  2127. def pbLearnMove(pkmnIndex,move)
  2128. pokemon=@party1[pkmnIndex]
  2129. return if !pokemon
  2130. pkmnname=pokemon.name
  2131. battler=pbFindPlayerBattler(pkmnIndex)
  2132. movename=PBMoves.getName(move)
  2133. for i in 0...4
  2134. return if pokemon.moves[i].id==move
  2135. if pokemon.moves[i].id==0
  2136. pokemon.moves[i]=PBMove.new(move)
  2137. battler.moves[i]=PokeBattle_Move.pbFromPBMove(self,pokemon.moves[i]) if battler
  2138. pbDisplayPaused(_INTL("{1} learned {2}!",pkmnname,movename))
  2139. PBDebug.log("[Learn move] #{pkmnname} learned #{movename}")
  2140. return
  2141. end
  2142. end
  2143. loop do
  2144. pbDisplayPaused(_INTL("{1} is trying to learn {2}.",pkmnname,movename))
  2145. pbDisplayPaused(_INTL("But {1} can't learn more than four moves.",pkmnname))
  2146. if pbDisplayConfirm(_INTL("Delete a move to make room for {1}?",movename))
  2147. pbDisplayPaused(_INTL("Which move should be forgotten?"))
  2148. forgetmove=@scene.pbForgetMove(pokemon,move)
  2149. if forgetmove>=0
  2150. oldmovename=PBMoves.getName(pokemon.moves[forgetmove].id)
  2151. pokemon.moves[forgetmove]=PBMove.new(move) # Replaces current/total PP
  2152. battler.moves[forgetmove]=PokeBattle_Move.pbFromPBMove(self,pokemon.moves[forgetmove]) if battler
  2153. pbDisplayPaused(_INTL("1, 2, and... ... ..."))
  2154. pbDisplayPaused(_INTL("Poof!"))
  2155. pbDisplayPaused(_INTL("{1} forgot {2}.",pkmnname,oldmovename))
  2156. pbDisplayPaused(_INTL("And..."))
  2157. pbDisplayPaused(_INTL("{1} learned {2}!",pkmnname,movename))
  2158. PBDebug.log("[Learn move] #{pkmnname} forgot #{oldmovename} and learned #{movename}")
  2159. return
  2160. elsif pbDisplayConfirm(_INTL("Should {1} stop learning {2}?",pkmnname,movename))
  2161. pbDisplayPaused(_INTL("{1} did not learn {2}.",pkmnname,movename))
  2162. return
  2163. end
  2164. elsif pbDisplayConfirm(_INTL("Should {1} stop learning {2}?",pkmnname,movename))
  2165. pbDisplayPaused(_INTL("{1} did not learn {2}.",pkmnname,movename))
  2166. return
  2167. end
  2168. end
  2169. end
  2170.  
  2171. ################################################################################
  2172. # Abilities.
  2173. ################################################################################
  2174. def pbOnActiveAll
  2175. for i in 0...4 # Currently unfainted participants will earn EXP even if they faint afterwards
  2176. @battlers[i].pbUpdateParticipants if pbIsOpposing?(i)
  2177. @amuletcoin=true if !pbIsOpposing?(i) &&
  2178. (isConst?(@battlers[i].item,PBItems,:AMULETCOIN) ||
  2179. isConst?(@battlers[i].item,PBItems,:LUCKINCENSE))
  2180. end
  2181. for i in 0...4
  2182. if !@battlers[i].isFainted?
  2183. if @battlers[i].isShadow? && pbIsOpposing?(i)
  2184. pbCommonAnimation("Shadow",@battlers[i],nil)
  2185. pbDisplay(_INTL("Oh!\nA Shadow Pokémon!"))
  2186. end
  2187. end
  2188. end
  2189. # Weather-inducing abilities, Trace, Imposter, etc.
  2190. @usepriority=false
  2191. priority=pbPriority
  2192. for i in priority
  2193. i.pbAbilitiesOnSwitchIn(true)
  2194. end
  2195. # Check forms are correct
  2196. for i in 0...4
  2197. next if @battlers[i].isFainted?
  2198. @battlers[i].pbCheckForm
  2199. end
  2200. end
  2201.  
  2202. def pbOnActiveOne(pkmn,onlyabilities=false,moldbreaker=false)
  2203. return false if pkmn.isFainted?
  2204. if !onlyabilities
  2205. for i in 0...4 # Currently unfainted participants will earn EXP even if they faint afterwards
  2206. @battlers[i].pbUpdateParticipants if pbIsOpposing?(i)
  2207. @amuletcoin=true if !pbIsOpposing?(i) &&
  2208. (isConst?(@battlers[i].item,PBItems,:AMULETCOIN) ||
  2209. isConst?(@battlers[i].item,PBItems,:LUCKINCENSE))
  2210. end
  2211. if pkmn.isShadow? && pbIsOpposing?(pkmn.index)
  2212. pbCommonAnimation("Shadow",pkmn,nil)
  2213. pbDisplay(_INTL("Oh!\nA Shadow Pokémon!"))
  2214. end
  2215. # Healing Wish
  2216. if pkmn.effects[PBEffects::HealingWish]
  2217. PBDebug.log("[Lingering effect triggered] #{pkmn.pbThis}'s Healing Wish")
  2218. pbCommonAnimation("HealingWish",pkmn,nil)
  2219. pbDisplayPaused(_INTL("The healing wish came true for {1}!",pkmn.pbThis(true)))
  2220. pkmn.pbRecoverHP(pkmn.totalhp,true)
  2221. pkmn.pbCureStatus(false)
  2222. pkmn.effects[PBEffects::HealingWish]=false
  2223. end
  2224. # Lunar Dance
  2225. if pkmn.effects[PBEffects::LunarDance]
  2226. PBDebug.log("[Lingering effect triggered] #{pkmn.pbThis}'s Lunar Dance")
  2227. pbCommonAnimation("LunarDance",pkmn,nil)
  2228. pbDisplayPaused(_INTL("{1} became cloaked in mystical moonlight!",pkmn.pbThis))
  2229. pkmn.pbRecoverHP(pkmn.totalhp,true)
  2230. pkmn.pbCureStatus(false)
  2231. for i in 0...4
  2232. pkmn.moves[i].pp=pkmn.moves[i].totalpp
  2233. end
  2234. pkmn.effects[PBEffects::LunarDance]=false
  2235. end
  2236. # Spikes
  2237. if pkmn.pbOwnSide.effects[PBEffects::Spikes]>0 && !pkmn.isAirborne?(moldbreaker)
  2238. if !pkmn.hasWorkingAbility(:MAGICGUARD)
  2239. PBDebug.log("[Entry hazard] #{pkmn.pbThis} triggered Spikes")
  2240. spikesdiv=[8,6,4][pkmn.pbOwnSide.effects[PBEffects::Spikes]-1]
  2241. @scene.pbDamageAnimation(pkmn,0)
  2242. pkmn.pbReduceHP((pkmn.totalhp/spikesdiv).floor)
  2243. pbDisplayPaused(_INTL("{1} is hurt by the spikes!",pkmn.pbThis))
  2244. end
  2245. end
  2246. pkmn.pbFaint if pkmn.isFainted?
  2247. # Stealth Rock
  2248. if pkmn.pbOwnSide.effects[PBEffects::StealthRock] && !pkmn.isFainted?
  2249. if !pkmn.hasWorkingAbility(:MAGICGUARD)
  2250. atype=getConst(PBTypes,:ROCK) || 0
  2251. eff=PBTypes.getCombinedEffectiveness(atype,pkmn.type1,pkmn.type2)
  2252. if eff>0
  2253. PBDebug.log("[Entry hazard] #{pkmn.pbThis} triggered Stealth Rock")
  2254. @scene.pbDamageAnimation(pkmn,0)
  2255. pkmn.pbReduceHP(((pkmn.totalhp*eff)/32).floor)
  2256. pbDisplayPaused(_INTL("Pointed stones dug into {1}!",pkmn.pbThis))
  2257. end
  2258. end
  2259. end
  2260. pkmn.pbFaint if pkmn.isFainted?
  2261. # Toxic Spikes
  2262. if pkmn.pbOwnSide.effects[PBEffects::ToxicSpikes]>0 && !pkmn.isFainted?
  2263. if !pkmn.isAirborne?(moldbreaker)
  2264. if pkmn.pbHasType?(:POISON)
  2265. PBDebug.log("[Entry hazard] #{pkmn.pbThis} absorbed Toxic Spikes")
  2266. pkmn.pbOwnSide.effects[PBEffects::ToxicSpikes]=0
  2267. pbDisplayPaused(_INTL("{1} absorbed the poison spikes!",pkmn.pbThis))
  2268. elsif pkmn.pbCanPoisonSpikes?(moldbreaker)
  2269. PBDebug.log("[Entry hazard] #{pkmn.pbThis} triggered Toxic Spikes")
  2270. if pkmn.pbOwnSide.effects[PBEffects::ToxicSpikes]==2
  2271. i.pbPoison(nil,_INTL("{1} was badly poisoned by the poison spikes!",i.pbThis,true))
  2272. else
  2273. i.pbPoison(nil,_INTL("{1} was poisoned by the poison spikes!",i.pbThis))
  2274. end
  2275. end
  2276. end
  2277. end
  2278. # Sticky Web
  2279. if pkmn.pbOwnSide.effects[PBEffects::StickyWeb] && !pkmn.isFainted? &&
  2280. !pkmn.isAirborne?(moldbreaker)
  2281. if pkmn.pbCanReduceStatStage?(PBStats::SPEED,nil,false,nil,moldbreaker)
  2282. PBDebug.log("[Entry hazard] #{pkmn.pbThis} triggered Sticky Web")
  2283. pbDisplayPaused(_INTL("{1} was caught in a sticky web!",pkmn.pbThis))
  2284. pkmn.pbReduceStat(PBStats::SPEED,1,nil,false,nil,true,moldbreaker)
  2285. end
  2286. end
  2287. end
  2288. pkmn.pbAbilityCureCheck
  2289. if pkmn.isFainted?
  2290. pbGainEXP
  2291. pbJudge # pbSwitch
  2292. return false
  2293. end
  2294. # pkmn.pbAbilitiesOnSwitchIn(true)
  2295. if !onlyabilities
  2296. pkmn.pbCheckForm
  2297. pkmn.pbBerryCureCheck
  2298. end
  2299. return true
  2300. end
  2301.  
  2302. def pbPrimordialWeather
  2303. # End Primordial Sea, Desolate Land, Delta Stream
  2304. hasabil=false
  2305. case @weather
  2306. when PBWeather::HEAVYRAIN
  2307. for i in 0...4
  2308. if isConst?(@battlers[i].ability.PBAbilities,:PRIMORDIALSEA) &&
  2309. !@battlers[i].isFainted?
  2310. hasabil=true; break
  2311. end
  2312. if !hasabil
  2313. @weather=0
  2314. pbDisplayBrief("The heavy rain has lifted!") if showMessage
  2315. end
  2316. end
  2317. when PBWeather::HARSHSUN
  2318. for i in 0...4
  2319. if isConst?(@battlers[i].ability.PBAbilities,:DESOLATELAND) &&
  2320. !@battlers[i].isFainted?
  2321. hasabil=true; break
  2322. end
  2323. if !hasabil
  2324. @weather=0
  2325. pbDisplayBrief("The harsh sunlight faded!") if showMessage
  2326. end
  2327. end
  2328. when PBWeather::STRONGWINDS
  2329. for i in 0...4
  2330. if isConst?(@battlers[i].ability.PBAbilities,:DELTASTREAM) &&
  2331. !@battlers[i].isFainted?
  2332. hasabil=true; break
  2333. end
  2334. if !hasabil
  2335. @weather=0
  2336. pbDisplayBrief("The mysterious air current has dissipated!") if showMessage
  2337. end
  2338. end
  2339. end
  2340. end
  2341.  
  2342. ################################################################################
  2343. # Judging.
  2344. ################################################################################
  2345. def pbJudgeCheckpoint(attacker,move=0)
  2346. end
  2347.  
  2348. def pbDecisionOnTime
  2349. count1=0
  2350. count2=0
  2351. hptotal1=0
  2352. hptotal2=0
  2353. for i in @party1
  2354. next if !i
  2355. if i.hp>0 && !i.isEgg?
  2356. count1+=1
  2357. hptotal1+=i.hp
  2358. end
  2359. end
  2360. for i in @party2
  2361. next if !i
  2362. if i.hp>0 && !i.isEgg?
  2363. count2+=1
  2364. hptotal2+=i.hp
  2365. end
  2366. end
  2367. return 1 if count1>count2 # win
  2368. return 2 if count1<count2 # loss
  2369. return 1 if hptotal1>hptotal2 # win
  2370. return 2 if hptotal1<hptotal2 # loss
  2371. return 5 # draw
  2372. end
  2373.  
  2374. def pbDecisionOnTime2
  2375. count1=0
  2376. count2=0
  2377. hptotal1=0
  2378. hptotal2=0
  2379. for i in @party1
  2380. next if !i
  2381. if i.hp>0 && !i.isEgg?
  2382. count1+=1
  2383. hptotal1+=(i.hp*100/i.totalhp)
  2384. end
  2385. end
  2386. hptotal1/=count1 if count1>0
  2387. for i in @party2
  2388. next if !i
  2389. if i.hp>0 && !i.isEgg?
  2390. count2+=1
  2391. hptotal2+=(i.hp*100/i.totalhp)
  2392. end
  2393. end
  2394. hptotal2/=count2 if count2>0
  2395. return 1 if count1>count2 # win
  2396. return 2 if count1<count2 # loss
  2397. return 1 if hptotal1>hptotal2 # win
  2398. return 2 if hptotal1<hptotal2 # loss
  2399. return 5 # draw
  2400. end
  2401.  
  2402. def pbDecisionOnDraw
  2403. return 5 # draw
  2404. end
  2405.  
  2406. def pbJudge
  2407. # PBDebug.log("[Counts: #{pbPokemonCount(@party1)}/#{pbPokemonCount(@party2)}]")
  2408. if pbAllFainted?(@party1) && pbAllFainted?(@party2)
  2409. @decision=pbDecisionOnDraw() # Draw
  2410. return
  2411. end
  2412. if pbAllFainted?(@party1)
  2413. @decision=2 # Loss
  2414. return
  2415. end
  2416. if pbAllFainted?(@party2)
  2417. @decision=1 # Win
  2418. return
  2419. end
  2420. end
  2421.  
  2422. ################################################################################
  2423. # Messages and animations.
  2424. ################################################################################
  2425. def pbDisplay(msg)
  2426. @scene.pbDisplayMessage(msg)
  2427. end
  2428.  
  2429. def pbDisplayPaused(msg)
  2430. @scene.pbDisplayPausedMessage(msg)
  2431. end
  2432.  
  2433. def pbDisplayBrief(msg)
  2434. @scene.pbDisplayMessage(msg,true)
  2435. end
  2436.  
  2437. def pbDisplayConfirm(msg)
  2438. @scene.pbDisplayConfirmMessage(msg)
  2439. end
  2440.  
  2441. def pbShowCommands(msg,commands,cancancel=true)
  2442. @scene.pbShowCommands(msg,commands,cancancel)
  2443. end
  2444.  
  2445. def pbAnimation(move,attacker,opponent,hitnum=0)
  2446. if @battlescene
  2447. @scene.pbAnimation(move,attacker,opponent,hitnum)
  2448. end
  2449. end
  2450.  
  2451. def pbCommonAnimation(name,attacker,opponent,hitnum=0)
  2452. if @battlescene
  2453. @scene.pbCommonAnimation(name,attacker,opponent,hitnum)
  2454. end
  2455. end
  2456.  
  2457. ################################################################################
  2458. # Battle core.
  2459. ################################################################################
  2460. def pbStartBattle(canlose=false)
  2461. PBDebug.log("")
  2462. PBDebug.log("******************************************")
  2463. begin
  2464. pbStartBattleCore(canlose)
  2465. rescue BattleAbortedException
  2466. @decision=0
  2467. @scene.pbEndBattle(@decision)
  2468. end
  2469. return @decision
  2470. end
  2471.  
  2472. def pbStartBattleCore(canlose)
  2473. if !@fullparty1 && @party1.length>MAXPARTYSIZE
  2474. raise ArgumentError.new(_INTL("Party 1 has more than {1} Pokémon.",MAXPARTYSIZE))
  2475. end
  2476. if !@fullparty2 && @party2.length>MAXPARTYSIZE
  2477. raise ArgumentError.new(_INTL("Party 2 has more than {1} Pokémon.",MAXPARTYSIZE))
  2478. end
  2479. #========================
  2480. # Initialize wild Pokémon
  2481. #========================
  2482. if !@opponent
  2483. if @party2.length==1
  2484. if @doublebattle
  2485. raise _INTL("Only two wild Pokémon are allowed in double battles")
  2486. end
  2487. wildpoke=@party2[0]
  2488. @battlers[1].pbInitialize(wildpoke,0,false)
  2489. @peer.pbOnEnteringBattle(self,wildpoke)
  2490. pbSetSeen(wildpoke)
  2491. @scene.pbStartBattle(self)
  2492. pbDisplayPaused(_INTL("Wild {1} appeared!",wildpoke.name))
  2493. elsif @party2.length==2
  2494. if !@doublebattle
  2495. raise _INTL("Only one wild Pokémon is allowed in single battles")
  2496. end
  2497. @battlers[1].pbInitialize(@party2[0],0,false)
  2498. @battlers[3].pbInitialize(@party2[1],0,false)
  2499. @peer.pbOnEnteringBattle(self,@party2[0])
  2500. @peer.pbOnEnteringBattle(self,@party2[1])
  2501. pbSetSeen(@party2[0])
  2502. pbSetSeen(@party2[1])
  2503. @scene.pbStartBattle(self)
  2504. pbDisplayPaused(_INTL("Wild {1} and\r\n{2} appeared!",
  2505. @party2[0].name,@party2[1].name))
  2506. else
  2507. raise _INTL("Only one or two wild Pokémon are allowed")
  2508. end
  2509. #=======================================
  2510. # Initialize opponents in double battles
  2511. #=======================================
  2512. elsif @doublebattle
  2513. if @opponent.is_a?(Array)
  2514. if @opponent.length==1
  2515. @opponent=@opponent[0]
  2516. elsif @opponent.length!=2
  2517. raise _INTL("Opponents with zero or more than two people are not allowed")
  2518. end
  2519. end
  2520. if @player.is_a?(Array)
  2521. if @player.length==1
  2522. @player=@player[0]
  2523. elsif @player.length!=2
  2524. raise _INTL("Player trainers with zero or more than two people are not allowed")
  2525. end
  2526. end
  2527. @scene.pbStartBattle(self)
  2528. if @opponent.is_a?(Array)
  2529. pbDisplayPaused(_INTL("{1} and {2} want to battle!",@opponent[0].fullname,@opponent[1].fullname))
  2530. sendout1=pbFindNextUnfainted(@party2,0,pbSecondPartyBegin(1))
  2531. raise _INTL("Opponent 1 has no unfainted Pokémon") if sendout1<0
  2532. sendout2=pbFindNextUnfainted(@party2,pbSecondPartyBegin(1))
  2533. raise _INTL("Opponent 2 has no unfainted Pokémon") if sendout2<0
  2534. @battlers[1].pbInitialize(@party2[sendout1],sendout1,false)
  2535. pbDisplayBrief(_INTL("{1} sent\r\nout {2}!",@opponent[0].fullname,@battlers[1].name))
  2536. pbSendOut(1,@party2[sendout1])
  2537. @battlers[3].pbInitialize(@party2[sendout2],sendout2,false)
  2538. pbDisplayBrief(_INTL("{1} sent\r\nout {2}!",@opponent[1].fullname,@battlers[3].name))
  2539. pbSendOut(3,@party2[sendout2])
  2540. else
  2541. pbDisplayPaused(_INTL("{1}\r\nwould like to battle!",@opponent.fullname))
  2542. sendout1=pbFindNextUnfainted(@party2,0)
  2543. sendout2=pbFindNextUnfainted(@party2,sendout1+1)
  2544. if sendout1<0 || sendout2<0
  2545. raise _INTL("Opponent doesn't have two unfainted Pokémon")
  2546. end
  2547. @battlers[1].pbInitialize(@party2[sendout1],sendout1,false)
  2548. @battlers[3].pbInitialize(@party2[sendout2],sendout2,false)
  2549. pbDisplayBrief(_INTL("{1} sent\r\nout {2} and {3}!",
  2550. @opponent.fullname,@battlers[1].name,@battlers[3].name))
  2551. pbSendOut(1,@party2[sendout1])
  2552. pbSendOut(3,@party2[sendout2])
  2553. end
  2554. #======================================
  2555. # Initialize opponent in single battles
  2556. #======================================
  2557. else
  2558. sendout=pbFindNextUnfainted(@party2,0)
  2559. raise _INTL("Trainer has no unfainted Pokémon") if sendout<0
  2560. if @opponent.is_a?(Array)
  2561. raise _INTL("Opponent trainer must be only one person in single battles") if @opponent.length!=1
  2562. @opponent=@opponent[0]
  2563. end
  2564. if @player.is_a?(Array)
  2565. raise _INTL("Player trainer must be only one person in single battles") if @player.length!=1
  2566. @player=@player[0]
  2567. end
  2568. trainerpoke=@party2[sendout]
  2569. @scene.pbStartBattle(self)
  2570. pbDisplayPaused(_INTL("{1}\r\nwould like to battle!",@opponent.fullname))
  2571. @battlers[1].pbInitialize(trainerpoke,sendout,false)
  2572. pbDisplayBrief(_INTL("{1} sent\r\nout {2}!",@opponent.fullname,@battlers[1].name))
  2573. pbSendOut(1,trainerpoke)
  2574. end
  2575. #=====================================
  2576. # Initialize players in double battles
  2577. #=====================================
  2578. if @doublebattle
  2579. if @player.is_a?(Array)
  2580. sendout1=pbFindNextUnfainted(@party1,0,pbSecondPartyBegin(0))
  2581. raise _INTL("Player 1 has no unfainted Pokémon") if sendout1<0
  2582. sendout2=pbFindNextUnfainted(@party1,pbSecondPartyBegin(0))
  2583. raise _INTL("Player 2 has no unfainted Pokémon") if sendout2<0
  2584. @battlers[0].pbInitialize(@party1[sendout1],sendout1,false)
  2585. @battlers[2].pbInitialize(@party1[sendout2],sendout2,false)
  2586. pbDisplayBrief(_INTL("{1} sent\r\nout {2}! Go! {3}!",
  2587. @player[1].fullname,@battlers[2].name,@battlers[0].name))
  2588. pbSetSeen(@party1[sendout1])
  2589. pbSetSeen(@party1[sendout2])
  2590. else
  2591. sendout1=pbFindNextUnfainted(@party1,0)
  2592. sendout2=pbFindNextUnfainted(@party1,sendout1+1)
  2593. if sendout1<0 || sendout2<0
  2594. raise _INTL("Player doesn't have two unfainted Pokémon")
  2595. end
  2596. @battlers[0].pbInitialize(@party1[sendout1],sendout1,false)
  2597. @battlers[2].pbInitialize(@party1[sendout2],sendout2,false)
  2598. pbDisplayBrief(_INTL("Go! {1} and {2}!",@battlers[0].name,@battlers[2].name))
  2599. end
  2600. pbSendOut(0,@party1[sendout1])
  2601. pbSendOut(2,@party1[sendout2])
  2602. #====================================
  2603. # Initialize player in single battles
  2604. #====================================
  2605. else
  2606. sendout=pbFindNextUnfainted(@party1,0)
  2607. if sendout<0
  2608. raise _INTL("Player has no unfainted Pokémon")
  2609. end
  2610. @battlers[0].pbInitialize(@party1[sendout],sendout,false)
  2611. pbDisplayBrief(_INTL("Go! {1}!",@battlers[0].name))
  2612. pbSendOut(0,@party1[sendout])
  2613. end
  2614. #==================
  2615. # Initialize battle
  2616. #==================
  2617. if @weather==PBWeather::SUNNYDAY
  2618. pbCommonAnimation("Sunny",nil,nil)
  2619. pbDisplay(_INTL("The sunlight is strong."))
  2620. elsif @weather==PBWeather::RAINDANCE
  2621. pbCommonAnimation("Rain",nil,nil)
  2622. pbDisplay(_INTL("It is raining."))
  2623. elsif @weather==PBWeather::SANDSTORM
  2624. pbCommonAnimation("Sandstorm",nil,nil)
  2625. pbDisplay(_INTL("A sandstorm is raging."))
  2626. elsif @weather==PBWeather::HAIL
  2627. pbCommonAnimation("Hail",nil,nil)
  2628. pbDisplay(_INTL("Hail is falling."))
  2629. elsif @weather==PBWeather::HEAVYRAIN
  2630. pbCommonAnimation("HeavyRain",nil,nil)
  2631. pbDisplay(_INTL("It is raining heavily."))
  2632. elsif @weather==PBWeather::HARSHSUN
  2633. pbCommonAnimation("HarshSun",nil,nil)
  2634. pbDisplay(_INTL("The sunlight is extremely harsh."))
  2635. elsif @weather==PBWeather::STRONGWINDS
  2636. pbCommonAnimation("StrongWinds",nil,nil)
  2637. pbDisplay(_INTL("The wind is strong."))
  2638. end
  2639. pbOnActiveAll # Abilities
  2640. @turncount=0
  2641. loop do # Now begin the battle loop
  2642. PBDebug.log("")
  2643. PBDebug.log("***Round #{@turncount+1}***")
  2644. if @debug && @turncount>=100
  2645. @decision=pbDecisionOnTime()
  2646. PBDebug.log("")
  2647. PBDebug.log("***Undecided after 100 rounds, aborting***")
  2648. pbAbort
  2649. break
  2650. end
  2651. PBDebug.logonerr{
  2652. pbCommandPhase
  2653. }
  2654. break if @decision>0
  2655. PBDebug.logonerr{
  2656. pbAttackPhase
  2657. }
  2658. break if @decision>0
  2659. PBDebug.logonerr{
  2660. pbEndOfRoundPhase
  2661. }
  2662. break if @decision>0
  2663. @turncount+=1
  2664. end
  2665. return pbEndOfBattle(canlose)
  2666. end
  2667.  
  2668. ################################################################################
  2669. # Command phase.
  2670. ################################################################################
  2671. def pbCommandMenu(i)
  2672. return @scene.pbCommandMenu(i)
  2673. end
  2674.  
  2675. def pbItemMenu(i)
  2676. return @scene.pbItemMenu(i)
  2677. end
  2678.  
  2679. def pbAutoFightMenu(i)
  2680. return false
  2681. end
  2682.  
  2683. def pbCommandPhase
  2684. @scene.pbBeginCommandPhase
  2685. @scene.pbResetCommandIndices
  2686. for i in 0...4 # Reset choices if commands can be shown
  2687. if pbCanShowCommands?(i) || @battlers[i].isFainted?
  2688. @choices[i][0]=0
  2689. @choices[i][1]=0
  2690. @choices[i][2]=nil
  2691. @choices[i][3]=-1
  2692. else
  2693. unless !@doublebattle && pbIsDoubleBattler?(i)
  2694. PBDebug.log("[Reusing commands] #{@battlers[i].pbThis(true)}")
  2695. end
  2696. end
  2697. end
  2698. # Reset choices to perform Mega Evolution if it wasn't done somehow
  2699. for i in 0..1
  2700. for j in 0...@megaEvolution[i].length
  2701. @megaEvolution[i][j]=-1 if @megaEvolution[i][j]>=0
  2702. end
  2703. end
  2704. for i in 0...4
  2705. break if @decision!=0
  2706. next if @choices[i][0]!=0
  2707. if !pbOwnedByPlayer?(i) || @controlPlayer
  2708. if !@battlers[i].isFainted? && pbCanShowCommands?(i)
  2709. @scene.pbChooseEnemyCommand(i)
  2710. end
  2711. else
  2712. commandDone=false
  2713. commandEnd=false
  2714. if pbCanShowCommands?(i)
  2715. loop do
  2716. cmd=pbCommandMenu(i)
  2717. if cmd==0 # Fight
  2718. if pbCanShowFightMenu?(i)
  2719. commandDone=true if pbAutoFightMenu(i)
  2720. until commandDone
  2721. index=@scene.pbFightMenu(i)
  2722. if index<0
  2723. side=(pbIsOpposing?(i)) ? 1 : 0
  2724. owner=pbGetOwnerIndex(i)
  2725. if @megaEvolution[side][owner]==i
  2726. @megaEvolution[side][owner]=-1
  2727. end
  2728. break
  2729. end
  2730. next if !pbRegisterMove(i,index)
  2731. if @doublebattle
  2732. thismove=@battlers[i].moves[index]
  2733. target=@battlers[i].pbTarget(thismove)
  2734. if target==PBTargets::SingleNonUser # single non-user
  2735. target=@scene.pbChooseTarget(i)
  2736. next if target<0
  2737. pbRegisterTarget(i,target)
  2738. elsif target==PBTargets::UserOrPartner # Acupressure
  2739. target=@scene.pbChooseTarget(i)
  2740. next if target<0 || (target&1)==1
  2741. pbRegisterTarget(i,target)
  2742. end
  2743. end
  2744. commandDone=true
  2745. end
  2746. else
  2747. pbAutoChooseMove(i)
  2748. commandDone=true
  2749. end
  2750. elsif cmd!=0 && @battlers[i].effects[PBEffects::SkyDrop]
  2751. pbDisplay(_INTL("Sky Drop won't let {1} go!",@battlers[i].pbThis(true)))
  2752. elsif cmd==1 # Bag
  2753. if !@internalbattle
  2754. if pbOwnedByPlayer?(i)
  2755. pbDisplay(_INTL("Items can't be used here."))
  2756. end
  2757. else
  2758. item=pbItemMenu(i)
  2759. if item[0]>0
  2760. if pbRegisterItem(i,item[0],item[1])
  2761. commandDone=true
  2762. end
  2763. end
  2764. end
  2765. elsif cmd==2 # Pokémon
  2766. pkmn=pbSwitchPlayer(i,false,true)
  2767. if pkmn>=0
  2768. commandDone=true if pbRegisterSwitch(i,pkmn)
  2769. end
  2770. elsif cmd==3 # Run
  2771. run=pbRun(i)
  2772. if run>0
  2773. commandDone=true
  2774. return
  2775. elsif run<0
  2776. commandDone=true
  2777. side=(pbIsOpposing?(i)) ? 1 : 0
  2778. owner=pbGetOwnerIndex(i)
  2779. if @megaEvolution[side][owner]==i
  2780. @megaEvolution[side][owner]=-1
  2781. end
  2782. end
  2783. elsif cmd==4 # Call
  2784. thispkmn=@battlers[i]
  2785. @choices[i][0]=4 # "Call Pokémon"
  2786. @choices[i][1]=0
  2787. @choices[i][2]=nil
  2788. side=(pbIsOpposing?(i)) ? 1 : 0
  2789. owner=pbGetOwnerIndex(i)
  2790. if @megaEvolution[side][owner]==i
  2791. @megaEvolution[side][owner]=-1
  2792. end
  2793. commandDone=true
  2794. elsif cmd==-1 # Go back to first battler's choice
  2795. @megaEvolution[0][0]=-1 if @megaEvolution[0][0]>=0
  2796. @megaEvolution[1][0]=-1 if @megaEvolution[1][0]>=0
  2797. # Restore the item the player's first Pokémon was due to use
  2798. if @choices[0][0]==3 && $PokemonBag && $PokemonBag.pbCanStore?(@choices[0][1])
  2799. $PokemonBag.pbStoreItem(@choices[0][1])
  2800. end
  2801. pbCommandPhase
  2802. return
  2803. end
  2804. break if commandDone
  2805. end
  2806. end
  2807. end
  2808. end
  2809. end
  2810.  
  2811. ################################################################################
  2812. # Attack phase.
  2813. ################################################################################
  2814. def pbAttackPhase
  2815. @scene.pbBeginAttackPhase
  2816. for i in 0...4
  2817. @successStates[i].clear
  2818. if @choices[i][0]!=1 && @choices[i][0]!=2
  2819. @battlers[i].effects[PBEffects::DestinyBond]=false
  2820. @battlers[i].effects[PBEffects::Grudge]=false
  2821. end
  2822. @battlers[i].turncount+=1 if !@battlers[i].isFainted?
  2823. @battlers[i].effects[PBEffects::Rage]=false if !pbChoseMove?(i,:RAGE)
  2824. end
  2825. # Calculate priority at this time
  2826. @usepriority=false
  2827. priority=pbPriority(false,true)
  2828. # Mega Evolution
  2829. megaevolved=[]
  2830. for i in priority
  2831. next if @choices[i.index][0]!=1
  2832. side=(pbIsOpposing?(i.index)) ? 1 : 0
  2833. owner=pbGetOwnerIndex(i.index)
  2834. if @megaEvolution[side][owner]==i.index
  2835. pbMegaEvolve(i.index)
  2836. megaevolved.push(i.index)
  2837. end
  2838. end
  2839. if megaevolved.length>0
  2840. for i in priority
  2841. i.pbAbilitiesOnSwitchIn(true) if megaevolved.include?(i.index)
  2842. end
  2843. end
  2844. # Call at Pokémon
  2845. for i in priority
  2846. if @choices[i.index][0]==4
  2847. pbCall(i.index)
  2848. end
  2849. end
  2850. # Switch out Pokémon
  2851. @switching=true
  2852. switched=[]
  2853. for i in priority
  2854. if @choices[i.index][0]==2
  2855. index=@choices[i.index][1] # party position of Pokémon to switch to
  2856. newpokename=index
  2857. if isConst?(pbParty(i.index)[index].ability,PBAbilities,:ILLUSION)
  2858. newpokename=pbGetLastPokeInTeam(i.index)
  2859. end
  2860. self.lastMoveUser=i.index
  2861. if !pbOwnedByPlayer?(i.index)
  2862. owner=pbGetOwner(i.index)
  2863. pbDisplayBrief(_INTL("{1} withdrew {2}!",owner.fullname,i.name))
  2864. PBDebug.log("[Withdrew Pokémon] Opponent withdrew #{i.pbThis(true)}")
  2865. else
  2866. pbDisplayBrief(_INTL("{1}, that's enough!\r\nCome back!",i.name))
  2867. PBDebug.log("[Withdrew Pokémon] Player withdrew #{i.pbThis(true)}")
  2868. end
  2869. for j in priority
  2870. next if !i.pbIsOpposing?(j.index)
  2871. # if Pursuit and this target ("i") was chosen
  2872. if pbChoseMoveFunctionCode?(j.index,0x88) && # Pursuit
  2873. !j.hasMovedThisRound?
  2874. if j.status!=PBStatuses::SLEEP && j.status!=PBStatuses::FROZEN &&
  2875. !j.effects[PBEffects::SkyDrop] &&
  2876. (!j.hasWorkingAbility(:TRUANT) || !j.effects[PBEffects::Truant])
  2877. @choices[j.index][3]=i.index # Make sure to target the switching Pokémon
  2878. j.pbUseMove(@choices[j.index]) # This calls pbGainEXP as appropriate
  2879. j.effects[PBEffects::Pursuit]=true
  2880. @switching=false
  2881. return if @decision>0
  2882. end
  2883. end
  2884. break if i.isFainted?
  2885. end
  2886. if !pbRecallAndReplace(i.index,index,newpokename)
  2887. # If a forced switch somehow occurs here in single battles
  2888. # the attack phase now ends
  2889. if !@doublebattle
  2890. @switching=false
  2891. return
  2892. end
  2893. else
  2894. switched.push(i.index)
  2895. end
  2896. end
  2897. end
  2898. if switched.length>0
  2899. for i in priority
  2900. i.pbAbilitiesOnSwitchIn(true) if switched.include?(i.index)
  2901. end
  2902. end
  2903. @switching=false
  2904. # Use items
  2905. for i in priority
  2906. if pbIsOpposing?(i.index) && @choices[i.index][0]==3
  2907. pbEnemyUseItem(@choices[i.index][1],i)
  2908. elsif @choices[i.index][0]==3
  2909. # Player use item
  2910. item=@choices[i.index][1]
  2911. if item>0
  2912. usetype=$ItemData[item][ITEMBATTLEUSE]
  2913. if usetype==1 || usetype==3
  2914. if @choices[i.index][2]>=0
  2915. pbUseItemOnPokemon(item,@choices[i.index][2],i,@scene)
  2916. end
  2917. elsif usetype==2 || usetype==4
  2918. if !ItemHandlers.hasUseInBattle(item) # Poké Ball/Poké Doll used already
  2919. pbUseItemOnBattler(item,@choices[i.index][2],i,@scene)
  2920. end
  2921. end
  2922. end
  2923. end
  2924. end
  2925. # Use attacks
  2926. for i in priority
  2927. if pbChoseMoveFunctionCode?(i.index,0x115) # Focus Punch
  2928. pbCommonAnimation("FocusPunch",i,nil)
  2929. pbDisplay(_INTL("{1} is tightening its focus!",i.pbThis))
  2930. end
  2931. end
  2932. 10.times do
  2933. # Forced to go next
  2934. advance=false
  2935. for i in priority
  2936. next if !i.effects[PBEffects::MoveNext]
  2937. next if i.hasMovedThisRound?
  2938. advance=i.pbProcessTurn(@choices[i.index])
  2939. break if advance
  2940. end
  2941. return if @decision>0
  2942. next if advance
  2943. # Regular priority order
  2944. for i in priority
  2945. next if i.effects[PBEffects::Quash]
  2946. next if i.hasMovedThisRound?
  2947. advance=i.pbProcessTurn(@choices[i.index])
  2948. break if advance
  2949. end
  2950. return if @decision>0
  2951. next if advance
  2952. # Quashed
  2953. for i in priority
  2954. next if !i.effects[PBEffects::Quash]
  2955. next if i.hasMovedThisRound?
  2956. advance=i.pbProcessTurn(@choices[i.index])
  2957. break if advance
  2958. end
  2959. return if @decision>0
  2960. next if advance
  2961. # Check for all done
  2962. for i in priority
  2963. advance=true if @choices[i.index][0]==1 && !i.hasMovedThisRound?
  2964. break if advance
  2965. end
  2966. next if advance
  2967. break
  2968. end
  2969. pbWait(20)
  2970. end
  2971.  
  2972. ################################################################################
  2973. # End of round.
  2974. ################################################################################
  2975. def pbEndOfRoundPhase
  2976. PBDebug.log("[End of round]")
  2977. for i in 0...4
  2978. @battlers[i].effects[PBEffects::Roost]=false
  2979. @battlers[i].effects[PBEffects::Protect]=false
  2980. @battlers[i].effects[PBEffects::ProtectNegation]=false
  2981. @battlers[i].effects[PBEffects::KingsShield]=false
  2982. @battlers[i].effects[PBEffects::SpikyShield]=false
  2983. @battlers[i].effects[PBEffects::Endure]=false
  2984. @battlers[i].effects[PBEffects::HyperBeam]-=1 if @battlers[i].effects[PBEffects::HyperBeam]>0
  2985. @battlers[i].effects[PBEffects::Electrify]=false
  2986. @battlers[i].effects[PBEffects::MoveNext]=false
  2987. @battlers[i].effects[PBEffects::Quash]=false
  2988. @battlers[i].effects[PBEffects::Powder]=false
  2989. @battlers[i].effects[PBEffects::FirstPledge]=0
  2990. end
  2991. @usepriority=false # recalculate priority
  2992. priority=pbPriority(true) # Ignoring Quick Claw here
  2993. # Weather
  2994. case @weather
  2995. when PBWeather::SUNNYDAY
  2996. @weatherduration=@weatherduration-1 if @weatherduration>0
  2997. if @weatherduration==0
  2998. pbDisplay(_INTL("The sunlight faded."))
  2999. @weather=0
  3000. PBDebug.log("[End of effect] Sunlight weather ended")
  3001. else
  3002. pbCommonAnimation("Sunny",nil,nil)
  3003. # pbDisplay(_INTL("The sunlight is strong."))
  3004. if pbWeather==PBWeather::SUNNYDAY
  3005. for i in priority
  3006. if i.hasWorkingAbility(:SOLARPOWER)
  3007. PBDebug.log("[Ability triggered] #{i.pbThis}'s Solar Power")
  3008. @scene.pbDamageAnimation(i,0)
  3009. i.pbReduceHP((i.totalhp/8).floor)
  3010. pbDisplay(_INTL("{1} was hurt by the sunlight!",i.pbThis))
  3011. if i.isFainted?
  3012. return if !i.pbFaint
  3013. end
  3014. end
  3015. end
  3016. end
  3017. end
  3018. when PBWeather::RAINDANCE
  3019. @weatherduration=@weatherduration-1 if @weatherduration>0
  3020. if @weatherduration==0
  3021. pbDisplay(_INTL("The rain stopped."))
  3022. @weather=0
  3023. PBDebug.log("[End of effect] Rain weather ended")
  3024. else
  3025. pbCommonAnimation("Rain",nil,nil)
  3026. # pbDisplay(_INTL("Rain continues to fall."));
  3027. end
  3028. when PBWeather::SANDSTORM
  3029. @weatherduration=@weatherduration-1 if @weatherduration>0
  3030. if @weatherduration==0
  3031. pbDisplay(_INTL("The sandstorm subsided."))
  3032. @weather=0
  3033. PBDebug.log("[End of effect] Sandstorm weather ended")
  3034. else
  3035. pbCommonAnimation("Sandstorm",nil,nil)
  3036. # pbDisplay(_INTL("The sandstorm rages."))
  3037. if pbWeather==PBWeather::SANDSTORM
  3038. PBDebug.log("[Lingering effect triggered] Sandstorm weather damage")
  3039. for i in priority
  3040. next if i.isFainted?
  3041. if !i.pbHasType?(:GROUND) && !i.pbHasType?(:ROCK) && !i.pbHasType?(:STEEL) &&
  3042. !i.hasWorkingAbility(:SANDVEIL) &&
  3043. !i.hasWorkingAbility(:SANDRUSH) &&
  3044. !i.hasWorkingAbility(:SANDFORCE) &&
  3045. !i.hasWorkingAbility(:MAGICGUARD) &&
  3046. !i.hasWorkingAbility(:OVERCOAT) &&
  3047. !i.hasWorkingItem(:SAFETYGOGGLES) &&
  3048. ![0xCA,0xCB].include?(PBMoveData.new(i.effects[PBEffects::TwoTurnAttack]).function) # Dig, Dive
  3049. @scene.pbDamageAnimation(i,0)
  3050. i.pbReduceHP((i.totalhp/16).floor)
  3051. pbDisplay(_INTL("{1} is buffeted by the sandstorm!",i.pbThis))
  3052. if i.isFainted?
  3053. return if !i.pbFaint
  3054. end
  3055. end
  3056. end
  3057. end
  3058. end
  3059. when PBWeather::HAIL
  3060. @weatherduration=@weatherduration-1 if @weatherduration>0
  3061. if @weatherduration==0
  3062. pbDisplay(_INTL("The hail stopped."))
  3063. @weather=0
  3064. PBDebug.log("[End of effect] Hail weather ended")
  3065. else
  3066. pbCommonAnimation("Hail",nil,nil)
  3067. # pbDisplay(_INTL("Hail continues to fall."))
  3068. if pbWeather==PBWeather::HAIL
  3069. PBDebug.log("[Lingering effect triggered] Hail weather damage")
  3070. for i in priority
  3071. next if i.isFainted?
  3072. if !i.pbHasType?(:ICE) &&
  3073. !i.hasWorkingAbility(:ICEBODY) &&
  3074. !i.hasWorkingAbility(:SNOWCLOAK) &&
  3075. !i.hasWorkingAbility(:MAGICGUARD) &&
  3076. !i.hasWorkingAbility(:OVERCOAT) &&
  3077. !i.hasWorkingItem(:SAFETYGOGGLES) &&
  3078. ![0xCA,0xCB].include?(PBMoveData.new(i.effects[PBEffects::TwoTurnAttack]).function) # Dig, Dive
  3079. @scene.pbDamageAnimation(i,0)
  3080. i.pbReduceHP((i.totalhp/16).floor)
  3081. pbDisplay(_INTL("{1} is buffeted by the hail!",i.pbThis))
  3082. if i.isFainted?
  3083. return if !i.pbFaint
  3084. end
  3085. end
  3086. end
  3087. end
  3088. end
  3089. when PBWeather::HEAVYRAIN
  3090. hasabil=false
  3091. for i in 0...4
  3092. if isConst?(@battlers[i].ability.PBAbilities,:PRIMORDIALSEA) && !@battlers[i].isFainted?
  3093. hasabil=true; break
  3094. end
  3095. end
  3096. @weatherduration=0 if !hasabil
  3097. if @weatherduration==0
  3098. pbDisplay(_INTL("The heavy rain stopped."))
  3099. @weather=0
  3100. PBDebug.log("[End of effect] Primordial Sea's rain weather ended")
  3101. else
  3102. pbCommonAnimation("HeavyRain",nil,nil)
  3103. end
  3104. when PBWeather::HARSHSUN
  3105. hasabil=false
  3106. for i in 0...4
  3107. if isConst?(@battlers[i].ability.PBAbilities,:DESOLATELAND) && !@battlers[i].isFainted?
  3108. hasabil=true; break
  3109. end
  3110. end
  3111. @weatherduration=0 if !hasabil
  3112. if @weatherduration==0
  3113. pbDisplay(_INTL("The harsh sunlight faded."))
  3114. @weather=0
  3115. PBDebug.log("[End of effect] Desolate Land's sunlight weather ended")
  3116. else
  3117. pbCommonAnimation("HarshSun",nil,nil)
  3118. if pbWeather==PBWeather::HARSHSUN
  3119. for i in priority
  3120. if i.hasWorkingAbility(:SOLARPOWER)
  3121. PBDebug.log("[Ability triggered] #{i.pbThis}'s Solar Power")
  3122. @scene.pbDamageAnimation(i,0)
  3123. i.pbReduceHP((i.totalhp/8).floor)
  3124. pbDisplay(_INTL("{1} was hurt by the sunlight!",i.pbThis))
  3125. if i.isFainted?
  3126. return if !i.pbFaint
  3127. end
  3128. end
  3129. end
  3130. end
  3131. end
  3132. when PBWeather::STRONGWINDS
  3133. hasabil=false
  3134. for i in 0...4
  3135. if isConst?(@battlers[i].ability.PBAbilities,:DELTASTREAM) && !@battlers[i].isFainted?
  3136. hasabil=true; break
  3137. end
  3138. end
  3139. @weatherduration=0 if !hasabil
  3140. if @weatherduration==0
  3141. pbDisplay(_INTL("The air current subsided."))
  3142. @weather=0
  3143. PBDebug.log("[End of effect] Delta Stream's wind weather ended")
  3144. else
  3145. pbCommonAnimation("StrongWinds",nil,nil)
  3146. end
  3147. end
  3148. # Shadow Sky weather
  3149. if isConst?(@weather,PBWeather,:SHADOWSKY)
  3150. @weatherduration=@weatherduration-1 if @weatherduration>0
  3151. if @weatherduration==0
  3152. pbDisplay(_INTL("The shadow sky faded."))
  3153. @weather=0
  3154. PBDebug.log("[End of effect] Shadow Sky weather ended")
  3155. else
  3156. pbCommonAnimation("ShadowSky",nil,nil)
  3157. # pbDisplay(_INTL("The shadow sky continues."));
  3158. if isConst?(pbWeather,PBWeather,:SHADOWSKY)
  3159. PBDebug.log("[Lingering effect triggered] Shadow Sky weather damage")
  3160. for i in priority
  3161. next if i.isFainted?
  3162. if !i.isShadow?
  3163. @scene.pbDamageAnimation(i,0)
  3164. i.pbReduceHP((i.totalhp/16).floor)
  3165. pbDisplay(_INTL("{1} was hurt by the shadow sky!",i.pbThis))
  3166. if i.isFainted?
  3167. return if !i.pbFaint
  3168. end
  3169. end
  3170. end
  3171. end
  3172. end
  3173. end
  3174. # Future Sight/Doom Desire
  3175. for i in battlers # not priority
  3176. next if i.isFainted?
  3177. if i.effects[PBEffects::FutureSight]>0
  3178. i.effects[PBEffects::FutureSight]-=1
  3179. if i.effects[PBEffects::FutureSight]==0
  3180. move=i.effects[PBEffects::FutureSightMove]
  3181. PBDebug.log("[Lingering effect triggered] #{PBMoves.getName(move)} struck #{i.pbThis(true)}")
  3182. pbDisplay(_INTL("{1} took the {2} attack!",i.pbThis,PBMoves.getName(move)))
  3183. moveuser=nil
  3184. for j in battlers
  3185. next if j.pbIsOpposing?(i.effects[PBEffects::FutureSightUserPos])
  3186. if j.pokemonIndex==i.effects[PBEffects::FutureSightUser] && !j.isFainted?
  3187. moveuser=j; break
  3188. end
  3189. end
  3190. if !moveuser
  3191. party=pbParty(i.effects[PBEffects::FutureSightUserPos])
  3192. if party[i.effects[PBEffects::FutureSightUser]].hp>0
  3193. moveuser=PokeBattle_Battler.new(self,i.effects[PBEffects::FutureSightUserPos])
  3194. moveuser.pbInitDummyPokemon(party[i.effects[PBEffects::FutureSightUser]],
  3195. i.effects[PBEffects::FutureSightUser])
  3196. end
  3197. end
  3198. if !moveuser
  3199. pbDisplay(_INTL("But it failed!"))
  3200. else
  3201. @futuresight=true
  3202. moveuser.pbUseMoveSimple(move,-1,i.index)
  3203. @futuresight=false
  3204. end
  3205. i.effects[PBEffects::FutureSight]=0
  3206. i.effects[PBEffects::FutureSightMove]=0
  3207. i.effects[PBEffects::FutureSightUser]=-1
  3208. i.effects[PBEffects::FutureSightUserPos]=-1
  3209. if i.isFainted?
  3210. return if !i.pbFaint
  3211. next
  3212. end
  3213. end
  3214. end
  3215. end
  3216. for i in priority
  3217. next if i.isFainted?
  3218. # Rain Dish
  3219. if i.hasWorkingAbility(:RAINDISH) &&
  3220. (pbWeather==PBWeather::RAINDANCE ||
  3221. pbWeather==PBWeather::HEAVYRAIN)
  3222. PBDebug.log("[Ability triggered] #{i.pbThis}'s Rain Dish")
  3223. hpgain=i.pbRecoverHP((i.totalhp/16).floor,true)
  3224. pbDisplay(_INTL("{1}'s {2} restored its HP a little!",i.pbThis,PBAbilities.getName(i.ability))) if hpgain>0
  3225. end
  3226. # Dry Skin
  3227. if i.hasWorkingAbility(:DRYSKIN)
  3228. if pbWeather==PBWeather::RAINDANCE ||
  3229. pbWeather==PBWeather::HEAVYRAIN
  3230. PBDebug.log("[Ability triggered] #{i.pbThis}'s Dry Skin (in rain)")
  3231. hpgain=i.pbRecoverHP((i.totalhp/8).floor,true)
  3232. pbDisplay(_INTL("{1}'s {2} was healed by the rain!",i.pbThis,PBAbilities.getName(i.ability))) if hpgain>0
  3233. elsif pbWeather==PBWeather::SUNNYDAY ||
  3234. pbWeather==PBWeather::HARSHSUN
  3235. PBDebug.log("[Ability triggered] #{i.pbThis}'s Dry Skin (in sun)")
  3236. @scene.pbDamageAnimation(i,0)
  3237. hploss=i.pbReduceHP((i.totalhp/8).floor)
  3238. pbDisplay(_INTL("{1}'s {2} was hurt by the sunlight!",i.pbThis,PBAbilities.getName(i.ability))) if hploss>0
  3239. end
  3240. end
  3241. # Ice Body
  3242. if i.hasWorkingAbility(:ICEBODY) && pbWeather==PBWeather::HAIL
  3243. PBDebug.log("[Ability triggered] #{i.pbThis}'s Ice Body")
  3244. hpgain=i.pbRecoverHP((i.totalhp/16).floor,true)
  3245. pbDisplay(_INTL("{1}'s {2} restored its HP a little!",i.pbThis,PBAbilities.getName(i.ability))) if hpgain>0
  3246. end
  3247. if i.isFainted?
  3248. return if !i.pbFaint
  3249. end
  3250. end
  3251. # Wish
  3252. for i in priority
  3253. next if i.isFainted?
  3254. if i.effects[PBEffects::Wish]>0
  3255. i.effects[PBEffects::Wish]-=1
  3256. if i.effects[PBEffects::Wish]==0
  3257. PBDebug.log("[Lingering effect triggered] #{i.pbThis}'s Wish")
  3258. hpgain=i.pbRecoverHP(i.effects[PBEffects::WishAmount],true)
  3259. if hpgain>0
  3260. wishmaker=pbThisEx(i.index,i.effects[PBEffects::WishMaker])
  3261. pbDisplay(_INTL("{1}'s wish came true!",wishmaker))
  3262. end
  3263. end
  3264. end
  3265. end
  3266. # Fire Pledge + Grass Pledge combination damage
  3267. for i in 0...2
  3268. if sides[i].effects[PBEffects::SeaOfFire]>0 &&
  3269. pbWeather!=PBWeather::RAINDANCE &&
  3270. pbWeather!=PBWeather::HEAVYRAIN
  3271. @battle.pbCommonAnimation("SeaOfFire",nil,nil) if i==0
  3272. @battle.pbCommonAnimation("SeaOfFireOpp",nil,nil) if i==1
  3273. for j in priority
  3274. next if (j.index&1)!=i
  3275. next if j.pbHasType?(:FIRE) || j.hasWorkingAbility(:MAGICGUARD)
  3276. @scene.pbDamageAnimation(j,0)
  3277. hploss=j.pbReduceHP((j.totalhp/8).floor)
  3278. pbDisplay(_INTL("{1} is hurt by the sea of fire!",j.pbThis)) if hploss>0
  3279. if j.isFainted?
  3280. return if !j.pbFaint
  3281. end
  3282. end
  3283. end
  3284. end
  3285. for i in priority
  3286. next if i.isFainted?
  3287. # Shed Skin, Hydration
  3288. if (i.hasWorkingAbility(:SHEDSKIN) && pbRandom(10)<3) ||
  3289. (i.hasWorkingAbility(:HYDRATION) && (pbWeather==PBWeather::RAINDANCE ||
  3290. pbWeather==PBWeather::HEAVYRAIN))
  3291. if i.status>0
  3292. PBDebug.log("[Ability triggered] #{i.pbThis}'s #{PBAbilities.getName(i.ability)}")
  3293. s=i.status
  3294. i.pbCureStatus(false)
  3295. case s
  3296. when PBStatuses::SLEEP
  3297. pbDisplay(_INTL("{1}'s {2} cured its sleep problem!",i.pbThis,PBAbilities.getName(i.ability)))
  3298. when PBStatuses::POISON
  3299. pbDisplay(_INTL("{1}'s {2} cured its poison problem!",i.pbThis,PBAbilities.getName(i.ability)))
  3300. when PBStatuses::BURN
  3301. pbDisplay(_INTL("{1}'s {2} healed its burn!",i.pbThis,PBAbilities.getName(i.ability)))
  3302. when PBStatuses::PARALYSIS
  3303. pbDisplay(_INTL("{1}'s {2} cured its paralysis!",i.pbThis,PBAbilities.getName(i.ability)))
  3304. when PBStatuses::FROZEN
  3305. pbDisplay(_INTL("{1}'s {2} thawed it out!",i.pbThis,PBAbilities.getName(i.ability)))
  3306. end
  3307. end
  3308. end
  3309. # Healer
  3310. if i.hasWorkingAbility(:HEALER) && pbRandom(10)<3
  3311. partner=i.pbPartner
  3312. if partner && partner.status>0
  3313. PBDebug.log("[Ability triggered] #{i.pbThis}'s #{PBAbilities.getName(i.ability)}")
  3314. s=partner.status
  3315. partner.pbCureStatus(false)
  3316. case s
  3317. when PBStatuses::SLEEP
  3318. pbDisplay(_INTL("{1}'s {2} cured its partner's sleep problem!",i.pbThis,PBAbilities.getName(i.ability)))
  3319. when PBStatuses::POISON
  3320. pbDisplay(_INTL("{1}'s {2} cured its partner's poison problem!",i.pbThis,PBAbilities.getName(i.ability)))
  3321. when PBStatuses::BURN
  3322. pbDisplay(_INTL("{1}'s {2} healed its partner's burn!",i.pbThis,PBAbilities.getName(i.ability)))
  3323. when PBStatuses::PARALYSIS
  3324. pbDisplay(_INTL("{1}'s {2} cured its partner's paralysis!",i.pbThis,PBAbilities.getName(i.ability)))
  3325. when PBStatuses::FROZEN
  3326. pbDisplay(_INTL("{1}'s {2} thawed its partner out!",i.pbThis,PBAbilities.getName(i.ability)))
  3327. end
  3328. end
  3329. end
  3330. end
  3331. for i in priority
  3332. next if i.isFainted?
  3333. # Grassy Terrain (healing)
  3334. if @field.effects[PBEffects::GrassyTerrain]>0 && !i.isAirborne?
  3335. hpgain=i.pbRecoverHP((i.totalhp/16).floor,true)
  3336. pbDisplay(_INTL("{1}'s HP was restored.",i.pbThis)) if hpgain>0
  3337. end
  3338. # Held berries/Leftovers/Black Sludge
  3339. i.pbBerryCureCheck(true)
  3340. if i.isFainted?
  3341. return if !i.pbFaint
  3342. end
  3343. end
  3344. # Aqua Ring
  3345. for i in priority
  3346. next if i.isFainted?
  3347. if i.effects[PBEffects::AquaRing]
  3348. PBDebug.log("[Lingering effect triggered] #{i.pbThis}'s Aqua Ring")
  3349. hpgain=(i.totalhp/16).floor
  3350. hpgain=(hpgain*1.3).floor if i.hasWorkingItem(:BIGROOT)
  3351. hpgain=i.pbRecoverHP(hpgain,true)
  3352. pbDisplay(_INTL("Aqua Ring restored {1}'s HP!",i.pbThis)) if hpgain>0
  3353. end
  3354. end
  3355. # Ingrain
  3356. for i in priority
  3357. next if i.isFainted?
  3358. if i.effects[PBEffects::Ingrain]
  3359. PBDebug.log("[Lingering effect triggered] #{i.pbThis}'s Ingrain")
  3360. hpgain=(i.totalhp/16).floor
  3361. hpgain=(hpgain*1.3).floor if i.hasWorkingItem(:BIGROOT)
  3362. hpgain=i.pbRecoverHP(hpgain,true)
  3363. pbDisplay(_INTL("{1} absorbed nutrients with its roots!",i.pbThis)) if hpgain>0
  3364. end
  3365. end
  3366. # Leech Seed
  3367. for i in priority
  3368. next if i.isFainted?
  3369. if i.effects[PBEffects::LeechSeed]>=0 && !i.hasWorkingAbility(:MAGICGUARD)
  3370. recipient=@battlers[i.effects[PBEffects::LeechSeed]]
  3371. if recipient && !recipient.isFainted?
  3372. PBDebug.log("[Lingering effect triggered] #{i.pbThis}'s Leech Seed")
  3373. pbCommonAnimation("LeechSeed",recipient,i)
  3374. hploss=i.pbReduceHP((i.totalhp/8).floor,true)
  3375. if i.hasWorkingAbility(:LIQUIDOOZE)
  3376. recipient.pbReduceHP(hploss,true)
  3377. pbDisplay(_INTL("{1} sucked up the liquid ooze!",recipient.pbThis))
  3378. else
  3379. if recipient.effects[PBEffects::HealBlock]==0
  3380. hploss=(hploss*1.3).floor if recipient.hasWorkingItem(:BIGROOT)
  3381. recipient.pbRecoverHP(hploss,true)
  3382. end
  3383. pbDisplay(_INTL("{1}'s health was sapped by Leech Seed!",i.pbThis))
  3384. end
  3385. if i.isFainted?
  3386. return if !i.pbFaint
  3387. end
  3388. if recipient.isFainted?
  3389. return if !recipient.pbFaint
  3390. end
  3391. end
  3392. end
  3393. end
  3394. for i in priority
  3395. next if i.isFainted?
  3396. # Poison/Bad poison
  3397. if i.status==PBStatuses::POISON
  3398. if i.statusCount>0
  3399. i.effects[PBEffects::Toxic]+=1
  3400. i.effects[PBEffects::Toxic]=[15,i.effects[PBEffects::Toxic]].min
  3401. end
  3402. if i.hasWorkingAbility(:POISONHEAL)
  3403. pbCommonAnimation("Poison",i,nil)
  3404. if i.effects[PBEffects::HealBlock]==0 && i.hp<i.totalhp
  3405. PBDebug.log("[Ability triggered] #{i.pbThis}'s Poison Heal")
  3406. i.pbRecoverHP((i.totalhp/8).floor,true)
  3407. pbDisplay(_INTL("{1} is healed by poison!",i.pbThis))
  3408. end
  3409. else
  3410. if !i.hasWorkingAbility(:MAGICGUARD)
  3411. PBDebug.log("[Status damage] #{i.pbThis} took damage from poison/toxic")
  3412. if i.statusCount==0
  3413. i.pbReduceHP((i.totalhp/8).floor)
  3414. else
  3415. i.pbReduceHP(((i.totalhp*i.effects[PBEffects::Toxic])/16).floor)
  3416. end
  3417. i.pbContinueStatus
  3418. end
  3419. end
  3420. end
  3421. # Burn
  3422. if i.status==PBStatuses::BURN
  3423. if !i.hasWorkingAbility(:MAGICGUARD)
  3424. PBDebug.log("[Status damage] #{i.pbThis} took damage from burn")
  3425. if i.hasWorkingAbility(:HEATPROOF)
  3426. PBDebug.log("[Ability triggered] #{i.pbThis}'s Heatproof")
  3427. i.pbReduceHP((i.totalhp/16).floor)
  3428. else
  3429. i.pbReduceHP((i.totalhp/8).floor)
  3430. end
  3431. end
  3432. i.pbContinueStatus
  3433. end
  3434. # Nightmare
  3435. if i.effects[PBEffects::Nightmare]
  3436. if i.status==PBStatuses::SLEEP
  3437. if !i.hasWorkingAbility(:MAGICGUARD)
  3438. PBDebug.log("[Lingering effect triggered] #{i.pbThis}'s nightmare")
  3439. i.pbReduceHP((i.totalhp/4).floor,true)
  3440. pbDisplay(_INTL("{1} is locked in a nightmare!",i.pbThis))
  3441. end
  3442. else
  3443. i.effects[PBEffects::Nightmare]=false
  3444. end
  3445. end
  3446. if i.isFainted?
  3447. return if !i.pbFaint
  3448. next
  3449. end
  3450. end
  3451. # Curse
  3452. for i in priority
  3453. next if i.isFainted?
  3454. if i.effects[PBEffects::Curse] && !i.hasWorkingAbility(:MAGICGUARD)
  3455. PBDebug.log("[Lingering effect triggered] #{i.pbThis}'s curse")
  3456. i.pbReduceHP((i.totalhp/4).floor,true)
  3457. pbDisplay(_INTL("{1} is afflicted by the curse!",i.pbThis))
  3458. end
  3459. if i.isFainted?
  3460. return if !i.pbFaint
  3461. next
  3462. end
  3463. end
  3464. # Multi-turn attacks (Bind/Clamp/Fire Spin/Magma Storm/Sand Tomb/Whirlpool/Wrap)
  3465. for i in priority
  3466. next if i.isFainted?
  3467. if i.effects[PBEffects::MultiTurn]>0
  3468. i.effects[PBEffects::MultiTurn]-=1
  3469. movename=PBMoves.getName(i.effects[PBEffects::MultiTurnAttack])
  3470. if i.effects[PBEffects::MultiTurn]==0
  3471. PBDebug.log("[End of effect] Trapping move #{movename} affecting #{i.pbThis} ended")
  3472. pbDisplay(_INTL("{1} was freed from {2}!",i.pbThis,movename))
  3473. else
  3474. if isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:BIND)
  3475. pbCommonAnimation("Bind",i,nil)
  3476. elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:CLAMP)
  3477. pbCommonAnimation("Clamp",i,nil)
  3478. elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:FIRESPIN)
  3479. pbCommonAnimation("FireSpin",i,nil)
  3480. elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:MAGMASTORM)
  3481. pbCommonAnimation("MagmaStorm",i,nil)
  3482. elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:SANDTOMB)
  3483. pbCommonAnimation("SandTomb",i,nil)
  3484. elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:WRAP)
  3485. pbCommonAnimation("Wrap",i,nil)
  3486. elsif isConst?(i.effects[PBEffects::MultiTurnAttack],PBMoves,:INFESTATION)
  3487. pbCommonAnimation("Infestation",i,nil)
  3488. else
  3489. pbCommonAnimation("Wrap",i,nil)
  3490. end
  3491. if !i.hasWorkingAbility(:MAGICGUARD)
  3492. PBDebug.log("[Lingering effect triggered] #{i.pbThis} took damage from trapping move #{movename}")
  3493. @scene.pbDamageAnimation(i,0)
  3494. amt=(USENEWBATTLEMECHANICS) ? (i.totalhp/8).floor : (i.totalhp/16).floor
  3495. if @battlers[i.effects[PBEffects::MultiTurnUser]].hasWorkingItem(:BINDINGBAND)
  3496. amt=(USENEWBATTLEMECHANICS) ? (i.totalhp/6).floor : (i.totalhp/8).floor
  3497. end
  3498. i.pbReduceHP(amt)
  3499. pbDisplay(_INTL("{1} is hurt by {2}!",i.pbThis,movename))
  3500. end
  3501. end
  3502. end
  3503. if i.isFainted?
  3504. return if !i.pbFaint
  3505. end
  3506. end
  3507. # Taunt
  3508. for i in priority
  3509. next if i.isFainted?
  3510. if i.effects[PBEffects::Taunt]>0
  3511. i.effects[PBEffects::Taunt]-=1
  3512. if i.effects[PBEffects::Taunt]==0
  3513. pbDisplay(_INTL("{1}'s taunt wore off!",i.pbThis))
  3514. PBDebug.log("[End of effect] #{i.pbThis} is no longer taunted")
  3515. end
  3516. end
  3517. end
  3518. # Encore
  3519. for i in priority
  3520. next if i.isFainted?
  3521. if i.effects[PBEffects::Encore]>0
  3522. if i.moves[i.effects[PBEffects::EncoreIndex]].id!=i.effects[PBEffects::EncoreMove]
  3523. i.effects[PBEffects::Encore]=0
  3524. i.effects[PBEffects::EncoreIndex]=0
  3525. i.effects[PBEffects::EncoreMove]=0
  3526. PBDebug.log("[End of effect] #{i.pbThis} is no longer encored (encored move was lost)")
  3527. else
  3528. i.effects[PBEffects::Encore]-=1
  3529. if i.effects[PBEffects::Encore]==0 || i.moves[i.effects[PBEffects::EncoreIndex]].pp==0
  3530. i.effects[PBEffects::Encore]=0
  3531. pbDisplay(_INTL("{1}'s encore ended!",i.pbThis))
  3532. PBDebug.log("[End of effect] #{i.pbThis} is no longer encored")
  3533. end
  3534. end
  3535. end
  3536. end
  3537. # Disable/Cursed Body
  3538. for i in priority
  3539. next if i.isFainted?
  3540. if i.effects[PBEffects::Disable]>0
  3541. i.effects[PBEffects::Disable]-=1
  3542. if i.effects[PBEffects::Disable]==0
  3543. i.effects[PBEffects::DisableMove]=0
  3544. pbDisplay(_INTL("{1} is no longer disabled!",i.pbThis))
  3545. PBDebug.log("[End of effect] #{i.pbThis} is no longer disabled")
  3546. end
  3547. end
  3548. end
  3549. # Magnet Rise
  3550. for i in priority
  3551. next if i.isFainted?
  3552. if i.effects[PBEffects::MagnetRise]>0
  3553. i.effects[PBEffects::MagnetRise]-=1
  3554. if i.effects[PBEffects::MagnetRise]==0
  3555. pbDisplay(_INTL("{1} stopped levitating.",i.pbThis))
  3556. PBDebug.log("[End of effect] #{i.pbThis} is no longer levitating by Magnet Rise")
  3557. end
  3558. end
  3559. end
  3560. # Telekinesis
  3561. for i in priority
  3562. next if i.isFainted?
  3563. if i.effects[PBEffects::Telekinesis]>0
  3564. i.effects[PBEffects::Telekinesis]-=1
  3565. if i.effects[PBEffects::Telekinesis]==0
  3566. pbDisplay(_INTL("{1} stopped levitating.",i.pbThis))
  3567. PBDebug.log("[End of effect] #{i.pbThis} is no longer levitating by Telekinesis")
  3568. end
  3569. end
  3570. end
  3571. # Heal Block
  3572. for i in priority
  3573. next if i.isFainted?
  3574. if i.effects[PBEffects::HealBlock]>0
  3575. i.effects[PBEffects::HealBlock]-=1
  3576. if i.effects[PBEffects::HealBlock]==0
  3577. pbDisplay(_INTL("{1}'s Heal Block wore off!",i.pbThis))
  3578. PBDebug.log("[End of effect] #{i.pbThis} is no longer Heal Blocked")
  3579. end
  3580. end
  3581. end
  3582. # Embargo
  3583. for i in priority
  3584. next if i.isFainted?
  3585. if i.effects[PBEffects::Embargo]>0
  3586. i.effects[PBEffects::Embargo]-=1
  3587. if i.effects[PBEffects::Embargo]==0
  3588. pbDisplay(_INTL("{1} can use items again!",i.pbThis(true)))
  3589. PBDebug.log("[End of effect] #{i.pbThis} is no longer affected by an embargo")
  3590. end
  3591. end
  3592. end
  3593. # Yawn
  3594. for i in priority
  3595. next if i.isFainted?
  3596. if i.effects[PBEffects::Yawn]>0
  3597. i.effects[PBEffects::Yawn]-=1
  3598. if i.effects[PBEffects::Yawn]==0 && i.pbCanSleepYawn?
  3599. PBDebug.log("[Lingering effect triggered] #{i.pbThis}'s Yawn")
  3600. i.pbSleep
  3601. end
  3602. end
  3603. end
  3604. # Perish Song
  3605. perishSongUsers=[]
  3606. for i in priority
  3607. next if i.isFainted?
  3608. if i.effects[PBEffects::PerishSong]>0
  3609. i.effects[PBEffects::PerishSong]-=1
  3610. pbDisplay(_INTL("{1}'s Perish count fell to {2}!",i.pbThis,i.effects[PBEffects::PerishSong]))
  3611. PBDebug.log("[Lingering effect triggered] #{i.pbThis}'s Perish Song count dropped to #{i.effects[PBEffects::PerishSong]}")
  3612. if i.effects[PBEffects::PerishSong]==0
  3613. perishSongUsers.push(i.effects[PBEffects::PerishSongUser])
  3614. i.pbReduceHP(i.hp,true)
  3615. end
  3616. end
  3617. if i.isFainted?
  3618. return if !i.pbFaint
  3619. end
  3620. end
  3621. if perishSongUsers.length>0
  3622. # If all remaining Pokemon fainted by a Perish Song triggered by a single side
  3623. if (perishSongUsers.find_all{|item| pbIsOpposing?(item) }.length==perishSongUsers.length) ||
  3624. (perishSongUsers.find_all{|item| !pbIsOpposing?(item) }.length==perishSongUsers.length)
  3625. pbJudgeCheckpoint(@battlers[perishSongUsers[0]])
  3626. end
  3627. end
  3628. if @decision>0
  3629. pbGainEXP
  3630. return
  3631. end
  3632. # Reflect
  3633. for i in 0...2
  3634. if sides[i].effects[PBEffects::Reflect]>0
  3635. sides[i].effects[PBEffects::Reflect]-=1
  3636. if sides[i].effects[PBEffects::Reflect]==0
  3637. pbDisplay(_INTL("Your team's Reflect faded!")) if i==0
  3638. pbDisplay(_INTL("The opposing team's Reflect faded!")) if i==1
  3639. PBDebug.log("[End of effect] Reflect ended on the player's side") if i==0
  3640. PBDebug.log("[End of effect] Reflect ended on the opponent's side") if i==1
  3641. end
  3642. end
  3643. end
  3644. # Light Screen
  3645. for i in 0...2
  3646. if sides[i].effects[PBEffects::LightScreen]>0
  3647. sides[i].effects[PBEffects::LightScreen]-=1
  3648. if sides[i].effects[PBEffects::LightScreen]==0
  3649. pbDisplay(_INTL("Your team's Light Screen faded!")) if i==0
  3650. pbDisplay(_INTL("The opposing team's Light Screen faded!")) if i==1
  3651. PBDebug.log("[End of effect] Light Screen ended on the player's side") if i==0
  3652. PBDebug.log("[End of effect] Light Screen ended on the opponent's side") if i==1
  3653. end
  3654. end
  3655. end
  3656. # Aurora Veil
  3657. for i in 0...2
  3658. if sides[i].effects[PBEffects::AuroraVeil]>0
  3659. sides[i].effects[PBEffects::AuroraVeil]-=1
  3660. if sides[i].effects[PBEffects::AuroraVeil]=0
  3661. pbDisplay(_INTL("Your team's Aurora Veil faded!")) if i==0
  3662. pbDisplay(_INTL("The opposing team's Aurora Veil faded!")) if i==1
  3663. PBDebug.log("[End of effect] Aurora Veil ended on the player's side") if i==0
  3664. PBDebug.log("[End of effect] Aurora Veil ended on the opponent's side") if i==1
  3665. end
  3666. end
  3667. end
  3668. # Safeguard
  3669. for i in 0...2
  3670. if sides[i].effects[PBEffects::Safeguard]>0
  3671. sides[i].effects[PBEffects::Safeguard]-=1
  3672. if sides[i].effects[PBEffects::Safeguard]==0
  3673. pbDisplay(_INTL("Your team is no longer protected by Safeguard!")) if i==0
  3674. pbDisplay(_INTL("The opposing team is no longer protected by Safeguard!")) if i==1
  3675. PBDebug.log("[End of effect] Safeguard ended on the player's side") if i==0
  3676. PBDebug.log("[End of effect] Safeguard ended on the opponent's side") if i==1
  3677. end
  3678. end
  3679. end
  3680. # Mist
  3681. for i in 0...2
  3682. if sides[i].effects[PBEffects::Mist]>0
  3683. sides[i].effects[PBEffects::Mist]-=1
  3684. if sides[i].effects[PBEffects::Mist]==0
  3685. pbDisplay(_INTL("Your team's Mist faded!")) if i==0
  3686. pbDisplay(_INTL("The opposing team's Mist faded!")) if i==1
  3687. PBDebug.log("[End of effect] Mist ended on the player's side") if i==0
  3688. PBDebug.log("[End of effect] Mist ended on the opponent's side") if i==1
  3689. end
  3690. end
  3691. end
  3692. # Tailwind
  3693. for i in 0...2
  3694. if sides[i].effects[PBEffects::Tailwind]>0
  3695. sides[i].effects[PBEffects::Tailwind]-=1
  3696. if sides[i].effects[PBEffects::Tailwind]==0
  3697. pbDisplay(_INTL("Your team's Tailwind petered out!")) if i==0
  3698. pbDisplay(_INTL("The opposing team's Tailwind petered out!")) if i==1
  3699. PBDebug.log("[End of effect] Tailwind ended on the player's side") if i==0
  3700. PBDebug.log("[End of effect] Tailwind ended on the opponent's side") if i==1
  3701. end
  3702. end
  3703. end
  3704. # Lucky Chant
  3705. for i in 0...2
  3706. if sides[i].effects[PBEffects::LuckyChant]>0
  3707. sides[i].effects[PBEffects::LuckyChant]-=1
  3708. if sides[i].effects[PBEffects::LuckyChant]==0
  3709. pbDisplay(_INTL("Your team's Lucky Chant faded!")) if i==0
  3710. pbDisplay(_INTL("The opposing team's Lucky Chant faded!")) if i==1
  3711. PBDebug.log("[End of effect] Lucky Chant ended on the player's side") if i==0
  3712. PBDebug.log("[End of effect] Lucky Chant ended on the opponent's side") if i==1
  3713. end
  3714. end
  3715. end
  3716. # End of Pledge move combinations
  3717. for i in 0...2
  3718. if sides[i].effects[PBEffects::Swamp]>0
  3719. sides[i].effects[PBEffects::Swamp]-=1
  3720. if sides[i].effects[PBEffects::Swamp]==0
  3721. pbDisplay(_INTL("The swamp around your team disappeared!")) if i==0
  3722. pbDisplay(_INTL("The swamp around the opposing team disappeared!")) if i==1
  3723. PBDebug.log("[End of effect] Grass Pledge's swamp ended on the player's side") if i==0
  3724. PBDebug.log("[End of effect] Grass Pledge's swamp ended on the opponent's side") if i==1
  3725. end
  3726. end
  3727. if sides[i].effects[PBEffects::SeaOfFire]>0
  3728. sides[i].effects[PBEffects::SeaOfFire]-=1
  3729. if sides[i].effects[PBEffects::SeaOfFire]==0
  3730. pbDisplay(_INTL("The sea of fire around your team disappeared!")) if i==0
  3731. pbDisplay(_INTL("The sea of fire around the opposing team disappeared!")) if i==1
  3732. PBDebug.log("[End of effect] Fire Pledge's sea of fire ended on the player's side") if i==0
  3733. PBDebug.log("[End of effect] Fire Pledge's sea of fire ended on the opponent's side") if i==1
  3734. end
  3735. end
  3736. if sides[i].effects[PBEffects::Rainbow]>0
  3737. sides[i].effects[PBEffects::Rainbow]-=1
  3738. if sides[i].effects[PBEffects::Rainbow]==0
  3739. pbDisplay(_INTL("The rainbow around your team disappeared!")) if i==0
  3740. pbDisplay(_INTL("The rainbow around the opposing team disappeared!")) if i==1
  3741. PBDebug.log("[End of effect] Water Pledge's rainbow ended on the player's side") if i==0
  3742. PBDebug.log("[End of effect] Water Pledge's rainbow ended on the opponent's side") if i==1
  3743. end
  3744. end
  3745. end
  3746. # Gravity
  3747. if @field.effects[PBEffects::Gravity]>0
  3748. @field.effects[PBEffects::Gravity]-=1
  3749. if @field.effects[PBEffects::Gravity]==0
  3750. pbDisplay(_INTL("Gravity returned to normal."))
  3751. PBDebug.log("[End of effect] Strong gravity ended")
  3752. end
  3753. end
  3754. # Trick Room
  3755. if @field.effects[PBEffects::TrickRoom]>0
  3756. @field.effects[PBEffects::TrickRoom]-=1
  3757. if @field.effects[PBEffects::TrickRoom]==0
  3758. pbDisplay(_INTL("The twisted dimensions returned to normal."))
  3759. PBDebug.log("[End of effect] Trick Room ended")
  3760. end
  3761. end
  3762. # Wonder Room
  3763. if @field.effects[PBEffects::WonderRoom]>0
  3764. @field.effects[PBEffects::WonderRoom]-=1
  3765. if @field.effects[PBEffects::WonderRoom]==0
  3766. pbDisplay(_INTL("Wonder Room wore off, and the Defense and Sp. Def stats returned to normal!"))
  3767. PBDebug.log("[End of effect] Wonder Room ended")
  3768. end
  3769. end
  3770. # Magic Room
  3771. if @field.effects[PBEffects::MagicRoom]>0
  3772. @field.effects[PBEffects::MagicRoom]-=1
  3773. if @field.effects[PBEffects::MagicRoom]==0
  3774. pbDisplay(_INTL("The area returned to normal."))
  3775. PBDebug.log("[End of effect] Magic Room ended")
  3776. end
  3777. end
  3778. # Mud Sport
  3779. if @field.effects[PBEffects::MudSportField]>0
  3780. @field.effects[PBEffects::MudSportField]-=1
  3781. if @field.effects[PBEffects::MudSportField]==0
  3782. pbDisplay(_INTL("The effects of Mud Sport have faded."))
  3783. PBDebug.log("[End of effect] Mud Sport ended")
  3784. end
  3785. end
  3786. # Water Sport
  3787. if @field.effects[PBEffects::WaterSportField]>0
  3788. @field.effects[PBEffects::WaterSportField]-=1
  3789. if @field.effects[PBEffects::WaterSportField]==0
  3790. pbDisplay(_INTL("The effects of Water Sport have faded."))
  3791. PBDebug.log("[End of effect] Water Sport ended")
  3792. end
  3793. end
  3794. # Electric Terrain
  3795. if @field.effects[PBEffects::ElectricTerrain]>0
  3796. @field.effects[PBEffects::ElectricTerrain]-=1
  3797. if @field.effects[PBEffects::ElectricTerrain]==0
  3798. pbDisplay(_INTL("The electric current disappeared from the battlefield."))
  3799. PBDebug.log("[End of effect] Electric Terrain ended")
  3800. end
  3801. end
  3802. # Grassy Terrain (counting down)
  3803. if @field.effects[PBEffects::GrassyTerrain]>0
  3804. @field.effects[PBEffects::GrassyTerrain]-=1
  3805. if @field.effects[PBEffects::GrassyTerrain]==0
  3806. pbDisplay(_INTL("The grass disappeared from the battlefield."))
  3807. PBDebug.log("[End of effect] Grassy Terrain ended")
  3808. end
  3809. end
  3810. # Misty Terrain
  3811. if @field.effects[PBEffects::MistyTerrain]>0
  3812. @field.effects[PBEffects::MistyTerrain]-=1
  3813. if @field.effects[PBEffects::MistyTerrain]==0
  3814. pbDisplay(_INTL("The mist disappeared from the battlefield."))
  3815. PBDebug.log("[End of effect] Misty Terrain ended")
  3816. end
  3817. end
  3818. # Psychic Terrain
  3819. if @field.effects[PBEffects::PsychicTerrain]>0
  3820. @field.effects[PBEffects::PsychicTerrain]-=1
  3821. if @field.effects[PBEffects::PsychicTerrain]==0
  3822. pbDisplay(_INTL("The battlefield returned to normal."))
  3823. PBDebug.log("[End of effect] Psychic Terrain ended")
  3824. end
  3825. end
  3826. # Uproar
  3827. for i in priority
  3828. next if i.isFainted?
  3829. if i.effects[PBEffects::Uproar]>0
  3830. for j in priority
  3831. if !j.isFainted? && j.status==PBStatuses::SLEEP && !j.hasWorkingAbility(:SOUNDPROOF)
  3832. PBDebug.log("[Lingering effect triggered] Uproar woke up #{j.pbThis(true)}")
  3833. j.pbCureStatus(false)
  3834. pbDisplay(_INTL("{1} woke up in the uproar!",j.pbThis))
  3835. end
  3836. end
  3837. i.effects[PBEffects::Uproar]-=1
  3838. if i.effects[PBEffects::Uproar]==0
  3839. pbDisplay(_INTL("{1} calmed down.",i.pbThis))
  3840. PBDebug.log("[End of effect] #{i.pbThis} is no longer uproaring")
  3841. else
  3842. pbDisplay(_INTL("{1} is making an uproar!",i.pbThis))
  3843. end
  3844. end
  3845. end
  3846. for i in priority
  3847. next if i.isFainted?
  3848. # Speed Boost
  3849. # A Pokémon's turncount is 0 if it became active after the beginning of a round
  3850. if i.turncount>0 && i.hasWorkingAbility(:SPEEDBOOST)
  3851. if i.pbIncreaseStatWithCause(PBStats::SPEED,1,i,PBAbilities.getName(i.ability))
  3852. PBDebug.log("[Ability triggered] #{i.pbThis}'s #{PBAbilities.getName(i.ability)}")
  3853. end
  3854. end
  3855. # Bad Dreams
  3856. if i.status==PBStatuses::SLEEP && !i.hasWorkingAbility(:MAGICGUARD)
  3857. if i.pbOpposing1.hasWorkingAbility(:BADDREAMS) ||
  3858. i.pbOpposing2.hasWorkingAbility(:BADDREAMS)
  3859. PBDebug.log("[Ability triggered] #{i.pbThis}'s opponent's Bad Dreams")
  3860. hploss=i.pbReduceHP((i.totalhp/8).floor,true)
  3861. pbDisplay(_INTL("{1} is having a bad dream!",i.pbThis)) if hploss>0
  3862. end
  3863. end
  3864. if i.isFainted?
  3865. return if !i.pbFaint
  3866. next
  3867. end
  3868. # Pickup
  3869. if i.hasWorkingAbility(:PICKUP) && i.item<=0
  3870. item=0; index=-1; use=0
  3871. for j in 0...4
  3872. next if j==i.index
  3873. if @battlers[j].effects[PBEffects::PickupUse]>use
  3874. item=@battlers[j].effects[PBEffects::PickupItem]
  3875. index=j
  3876. use=@battlers[j].effects[PBEffects::PickupUse]
  3877. end
  3878. end
  3879. if item>0
  3880. i.item=item
  3881. @battlers[index].effects[PBEffects::PickupItem]=0
  3882. @battlers[index].effects[PBEffects::PickupUse]=0
  3883. @battlers[index].pokemon.itemRecycle=0 if @battlers[index].pokemon.itemRecycle==item
  3884. if !@opponent && # In a wild battle
  3885. i.pokemon.itemInitial==0 &&
  3886. @battlers[index].pokemon.itemInitial==item
  3887. i.pokemon.itemInitial=item
  3888. @battlers[index].pokemon.itemInitial=0
  3889. end
  3890. pbDisplay(_INTL("{1} found one {2}!",i.pbThis,PBItems.getName(item)))
  3891. i.pbBerryCureCheck(true)
  3892. end
  3893. end
  3894. # Harvest
  3895. if i.hasWorkingAbility(:HARVEST) && i.item<=0 && i.pokemon.itemRecycle>0
  3896. if pbIsBerry?(i.pokemon.itemRecycle) &&
  3897. (pbWeather==PBWeather::SUNNYDAY ||
  3898. pbWeather==PBWeather::HARSHSUN || pbRandom(10)<5)
  3899. i.item=i.pokemon.itemRecycle
  3900. i.pokemon.itemRecycle=0
  3901. i.pokemon.itemInitial=item if i.pokemon.itemInitial==0
  3902. pbDisplay(_INTL("{1} harvested one {2}!",i.pbThis,PBItems.getName(i.item)))
  3903. i.pbBerryCureCheck(true)
  3904. end
  3905. end
  3906. # Moody
  3907. if i.hasWorkingAbility(:MOODY)
  3908. randomup=[]; randomdown=[]
  3909. for j in [PBStats::ATTACK,PBStats::DEFENSE,PBStats::SPEED,PBStats::SPATK,
  3910. PBStats::SPDEF,PBStats::ACCURACY,PBStats::EVASION]
  3911. randomup.push(j) if i.pbCanIncreaseStatStage?(j,i)
  3912. randomdown.push(j) if i.pbCanReduceStatStage?(j,i)
  3913. end
  3914. if randomup.length>0
  3915. PBDebug.log("[Ability triggered] #{i.pbThis}'s Moody (raise stat)")
  3916. r=pbRandom(randomup.length)
  3917. i.pbIncreaseStatWithCause(randomup[r],2,i,PBAbilities.getName(i.ability))
  3918. for j in 0...randomdown.length
  3919. if randomdown[j]==randomup[r]
  3920. randomdown[j]=nil; randomdown.compact!
  3921. break
  3922. end
  3923. end
  3924. end
  3925. if randomdown.length>0
  3926. PBDebug.log("[Ability triggered] #{i.pbThis}'s Moody (lower stat)")
  3927. r=pbRandom(randomdown.length)
  3928. i.pbReduceStatWithCause(randomdown[r],1,i,PBAbilities.getName(i.ability))
  3929. end
  3930. end
  3931. end
  3932. for i in priority
  3933. next if i.isFainted?
  3934. # Toxic Orb
  3935. if i.hasWorkingItem(:TOXICORB) && i.status==0 && i.pbCanPoison?(nil,false)
  3936. PBDebug.log("[Item triggered] #{i.pbThis}'s Toxic Orb")
  3937. i.pbPoison(nil,_INTL("{1} was badly poisoned by its {2}!",i.pbThis,
  3938. PBItems.getName(i.item)),true)
  3939. end
  3940. # Flame Orb
  3941. if i.hasWorkingItem(:FLAMEORB) && i.status==0 && i.pbCanBurn?(nil,false)
  3942. PBDebug.log("[Item triggered] #{i.pbThis}'s Flame Orb")
  3943. i.pbBurn(nil,_INTL("{1} was burned by its {2}!",i.pbThis,PBItems.getName(i.item)))
  3944. end
  3945. # Sticky Barb
  3946. if i.hasWorkingItem(:STICKYBARB) && !i.hasWorkingAbility(:MAGICGUARD)
  3947. PBDebug.log("[Item triggered] #{i.pbThis}'s Sticky Barb")
  3948. @scene.pbDamageAnimation(i,0)
  3949. i.pbReduceHP((i.totalhp/8).floor)
  3950. pbDisplay(_INTL("{1} is hurt by its {2}!",i.pbThis,PBItems.getName(i.item)))
  3951. end
  3952. if i.isFainted?
  3953. return if !i.pbFaint
  3954. end
  3955. end
  3956. # Form checks
  3957. for i in 0...4
  3958. next if @battlers[i].isFainted?
  3959. @battlers[i].pbCheckForm
  3960. end
  3961. pbGainEXP
  3962. pbSwitch
  3963. return if @decision>0
  3964. for i in priority
  3965. next if i.isFainted?
  3966. i.pbAbilitiesOnSwitchIn(false)
  3967. end
  3968. # Healing Wish/Lunar Dance - should go here
  3969. # Spikes/Toxic Spikes/Stealth Rock - should go here (in order of their 1st use)
  3970. for i in 0...4
  3971. if @battlers[i].turncount>0 && @battlers[i].hasWorkingAbility(:TRUANT)
  3972. @battlers[i].effects[PBEffects::Truant]=!@battlers[i].effects[PBEffects::Truant]
  3973. end
  3974. if @battlers[i].effects[PBEffects::LockOn]>0 # Also Mind Reader
  3975. @battlers[i].effects[PBEffects::LockOn]-=1
  3976. @battlers[i].effects[PBEffects::LockOnPos]=-1 if @battlers[i].effects[PBEffects::LockOn]==0
  3977. end
  3978. @battlers[i].effects[PBEffects::Flinch]=false
  3979. @battlers[i].effects[PBEffects::FollowMe]=0
  3980. @battlers[i].effects[PBEffects::HelpingHand]=false
  3981. @battlers[i].effects[PBEffects::MagicCoat]=false
  3982. @battlers[i].effects[PBEffects::Snatch]=false
  3983. @battlers[i].effects[PBEffects::Charge]-=1 if @battlers[i].effects[PBEffects::Charge]>0
  3984. @battlers[i].lastHPLost=0
  3985. @battlers[i].tookDamage=false
  3986. @battlers[i].lastAttacker.clear
  3987. @battlers[i].effects[PBEffects::Counter]=-1
  3988. @battlers[i].effects[PBEffects::CounterTarget]=-1
  3989. @battlers[i].effects[PBEffects::MirrorCoat]=-1
  3990. @battlers[i].effects[PBEffects::MirrorCoatTarget]=-1
  3991. end
  3992. for i in 0...2
  3993. if !@sides[i].effects[PBEffects::EchoedVoiceUsed]
  3994. @sides[i].effects[PBEffects::EchoedVoiceCounter]=0
  3995. end
  3996. @sides[i].effects[PBEffects::EchoedVoiceUsed]=false
  3997. @sides[i].effects[PBEffects::QuickGuard]=false
  3998. @sides[i].effects[PBEffects::WideGuard]=false
  3999. @sides[i].effects[PBEffects::CraftyShield]=false
  4000. @sides[i].effects[PBEffects::Round]=0
  4001. end
  4002. @field.effects[PBEffects::FusionBolt]=false
  4003. @field.effects[PBEffects::FusionFlare]=false
  4004. @field.effects[PBEffects::IonDeluge]=false
  4005. @field.effects[PBEffects::FairyLock]-=1 if @field.effects[PBEffects::FairyLock]>0
  4006. # invalidate stored priority
  4007. @usepriority=false
  4008. end
  4009.  
  4010. ################################################################################
  4011. # End of battle.
  4012. ################################################################################
  4013. def pbEndOfBattle(canlose=false)
  4014. case @decision
  4015. ##### WIN #####
  4016. when 1
  4017. PBDebug.log("")
  4018. PBDebug.log("***Player won***")
  4019. if @opponent
  4020. @scene.pbTrainerBattleSuccess
  4021. if @opponent.is_a?(Array)
  4022. pbDisplayPaused(_INTL("{1} defeated {2} and {3}!",self.pbPlayer.name,@opponent[0].fullname,@opponent[1].fullname))
  4023. else
  4024. pbDisplayPaused(_INTL("{1} defeated\r\n{2}!",self.pbPlayer.name,@opponent.fullname))
  4025. end
  4026. @scene.pbShowOpponent(0)
  4027. pbDisplayPaused(@endspeech.gsub(/\\[Pp][Nn]/,self.pbPlayer.name))
  4028. if @opponent.is_a?(Array)
  4029. @scene.pbHideOpponent
  4030. @scene.pbShowOpponent(1)
  4031. pbDisplayPaused(@endspeech2.gsub(/\\[Pp][Nn]/,self.pbPlayer.name))
  4032. end
  4033. # Calculate money gained for winning
  4034. if @internalbattle
  4035. tmoney=0
  4036. if @opponent.is_a?(Array) # Double battles
  4037. maxlevel1=0; maxlevel2=0; limit=pbSecondPartyBegin(1)
  4038. for i in 0...limit
  4039. if @party2[i]
  4040. maxlevel1=@party2[i].level if maxlevel1<@party2[i].level
  4041. end
  4042. if @party2[i+limit]
  4043. maxlevel2=@party2[i+limit].level if maxlevel1<@party2[i+limit].level
  4044. end
  4045. end
  4046. tmoney+=maxlevel1*@opponent[0].moneyEarned
  4047. tmoney+=maxlevel2*@opponent[1].moneyEarned
  4048. else
  4049. maxlevel=0
  4050. for i in @party2
  4051. next if !i
  4052. maxlevel=i.level if maxlevel<i.level
  4053. end
  4054. tmoney+=maxlevel*@opponent.moneyEarned
  4055. end
  4056. # If Amulet Coin/Luck Incense's effect applies, double money earned
  4057. tmoney*=2 if @amuletcoin
  4058. # If Happy Hour's effect applies, double money earned
  4059. tmoney*=2 if @doublemoney
  4060. oldmoney=self.pbPlayer.money
  4061. self.pbPlayer.money+=tmoney
  4062. moneygained=self.pbPlayer.money-oldmoney
  4063. if moneygained>0
  4064. pbDisplayPaused(_INTL("{1} got ${2}\r\nfor winning!",self.pbPlayer.name,tmoney))
  4065. end
  4066. end
  4067. end
  4068. if @internalbattle && @extramoney>0
  4069. @extramoney*=2 if @amuletcoin
  4070. @extramoney*=2 if @doublemoney
  4071. oldmoney=self.pbPlayer.money
  4072. self.pbPlayer.money+=@extramoney
  4073. moneygained=self.pbPlayer.money-oldmoney
  4074. if moneygained>0
  4075. pbDisplayPaused(_INTL("{1} picked up ${2}!",self.pbPlayer.name,@extramoney))
  4076. end
  4077. end
  4078. for pkmn in @snaggedpokemon
  4079. pbStorePokemon(pkmn)
  4080. self.pbPlayer.shadowcaught=[] if !self.pbPlayer.shadowcaught
  4081. self.pbPlayer.shadowcaught[pkmn.species]=true
  4082. end
  4083. @snaggedpokemon.clear
  4084. ##### LOSE, DRAW #####
  4085. when 2, 5
  4086. PBDebug.log("")
  4087. PBDebug.log("***Player lost***") if @decision==2
  4088. PBDebug.log("***Player drew with opponent***") if @decision==5
  4089. if @internalbattle
  4090. pbDisplayPaused(_INTL("{1} is out of usable Pokémon!",self.pbPlayer.name))
  4091. moneylost=pbMaxLevelFromIndex(0) # Player's Pokémon only, not partner's
  4092. multiplier=[8,16,24,36,48,60,80,100,120]
  4093. moneylost*=multiplier[[multiplier.length-1,self.pbPlayer.numbadges].min]
  4094. moneylost=self.pbPlayer.money if moneylost>self.pbPlayer.money
  4095. moneylost=0 if $game_switches[NO_MONEY_LOSS]
  4096. oldmoney=self.pbPlayer.money
  4097. self.pbPlayer.money-=moneylost
  4098. lostmoney=oldmoney-self.pbPlayer.money
  4099. if @opponent
  4100. if @opponent.is_a?(Array)
  4101. pbDisplayPaused(_INTL("{1} lost against {2} and {3}!",self.pbPlayer.name,@opponent[0].fullname,@opponent[1].fullname))
  4102. else
  4103. pbDisplayPaused(_INTL("{1} lost against\r\n{2}!",self.pbPlayer.name,@opponent.fullname))
  4104. end
  4105. if moneylost>0
  4106. pbDisplayPaused(_INTL("{1} paid ${2}\r\nas the prize money...",self.pbPlayer.name,lostmoney))
  4107. pbDisplayPaused(_INTL("...")) if !canlose
  4108. end
  4109. else
  4110. if moneylost>0
  4111. pbDisplayPaused(_INTL("{1} panicked and lost\r\n${2}...",self.pbPlayer.name,lostmoney))
  4112. pbDisplayPaused(_INTL("...")) if !canlose
  4113. end
  4114. end
  4115. pbDisplayPaused(_INTL("{1} blacked out!",self.pbPlayer.name)) if !canlose
  4116. elsif @decision==2
  4117. @scene.pbShowOpponent(0)
  4118. pbDisplayPaused(@endspeechwin.gsub(/\\[Pp][Nn]/,self.pbPlayer.name))
  4119. if @opponent.is_a?(Array)
  4120. @scene.pbHideOpponent
  4121. @scene.pbShowOpponent(1)
  4122. pbDisplayPaused(@endspeechwin2.gsub(/\\[Pp][Nn]/,self.pbPlayer.name))
  4123. end
  4124. end
  4125. end
  4126. # Pass on Pokérus within the party
  4127. infected=[]
  4128. for i in 0...$Trainer.party.length
  4129. if $Trainer.party[i].pokerusStage==1
  4130. infected.push(i)
  4131. end
  4132. end
  4133. if infected.length>=1
  4134. for i in infected
  4135. strain=$Trainer.party[i].pokerus/16
  4136. if i>0 && $Trainer.party[i-1].pokerusStage==0
  4137. $Trainer.party[i-1].givePokerus(strain) if rand(3)==0
  4138. end
  4139. if i<$Trainer.party.length-1 && $Trainer.party[i+1].pokerusStage==0
  4140. $Trainer.party[i+1].givePokerus(strain) if rand(3)==0
  4141. end
  4142. end
  4143. end
  4144. @scene.pbEndBattle(@decision)
  4145. for i in @battlers
  4146. i.pbResetForm
  4147. if i.hasWorkingAbility(:NATURALCURE)
  4148. i.status=0
  4149. end
  4150. end
  4151. for i in $Trainer.party
  4152. i.setItem(i.itemInitial)
  4153. i.itemInitial=i.itemRecycle=0
  4154. i.belch=false
  4155. end
  4156. return @decision
  4157. end
  4158. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement