Refactoring of code relating to switching, effect damage and effects that trigger after a move is used

This commit is contained in:
Maruno17
2021-09-19 19:03:17 +01:00
parent f00f030825
commit 1fb3ff5408
24 changed files with 345 additions and 343 deletions

View File

@@ -297,7 +297,7 @@ class PokeBattle_Battle
pbDisplay(_INTL("The battlefield is weird!"))
end
# Abilities upon entering battle
pbOnActiveAll
pbOnAllBattlersEnteringBattle
# Main battle loop
pbBattleLoop
end

View File

@@ -200,15 +200,12 @@ class PokeBattle_Battle
end
end
break if switched.length==0
pbPriority(true).each do |b|
b.pbEffectsOnSwitchIn(true) if switched.include?(b.index)
end
@battle.pbOnBattlerEnteringBattle(switched)
end
end
def pbGetReplacementPokemonIndex(idxBattler,random=false)
if random
return -1 if !pbCanSwitch?(idxBattler) # Can battler switch out?
choices = [] # Find all Pokémon that can switch in
eachInTeamFromBattlerIndex(idxBattler) do |_pkmn,i|
choices.push(i) if pbCanSwitchLax?(idxBattler,i)
@@ -304,91 +301,159 @@ class PokeBattle_Battle
# Effects upon a Pokémon entering battle
#=============================================================================
# Called at the start of battle only.
def pbOnActiveAll
# Weather-inducing abilities, Trace, Imposter, etc.
def pbOnAllBattlersEnteringBattle
pbCalculatePriority(true)
pbPriority(true).each { |b| b.pbEffectsOnSwitchIn(true) }
battler_indices = []
eachBattler { |b| battler_indices.push(b.index) }
pbOnBattlerEnteringBattle(battler_indices)
pbCalculatePriority
# Check forms are correct
eachBattler { |b| b.pbCheckForm }
end
# Called when a Pokémon switches in (entry effects, entry hazards).
def pbOnActiveOne(battler)
return false if battler.fainted?
# Introduce Shadow Pokémon
if battler.opposes? && battler.shadowPokemon?
pbCommonAnimation("Shadow",battler)
pbDisplay(_INTL("Oh!\nA Shadow Pokémon!"))
# Called when one or more Pokémon switch in. Does a lot of things, including
# entry hazards, form changes and items/abilities that trigger upon switching
# in.
def pbOnBattlerEnteringBattle(*battler_indices)
battler_indices.flatten!
pbPriority(true).each do |b|
b.droppedBelowHalfHP = false
end
# For each battler that entered battle, in speed order
pbPriority(true).each do |b|
next if !battler_indices.include?(b.index) || b.fainted?
pbRecordBattlerAsParticipated(b)
pbMessagesOnBattlerEnteringBattle(b)
# Position/field effects triggered by the battler appearing
pbEffectsOnBattlerEnteringPosition(b) # Healing Wish/Lunar Dance
pbEntryHazards(b)
# Battler faints if it is knocked out because of an entry hazard above
if b.fainted?
b.pbFaint
pbGainExp
pbJudge
next
end
b.pbCheckForm
# Primal Revert upon entering battle
pbPrimalReversion(b.index)
# Ending primordial weather, checking Trace
b.pbContinualAbilityChecks(true)
# Abilities that trigger upon switching in
if (!b.fainted? && b.unstoppableAbility?) || b.abilityActive?
BattleHandlers.triggerAbilityOnSwitchIn(b.ability, b, self)
end
pbEndPrimordialWeather # Checking this again just in case
# Items that trigger upon switching in (Air Balloon message)
if b.itemActive?
BattleHandlers.triggerItemOnSwitchIn(b.item, b, self)
end
# Berry check, status-curing ability check
b.pbHeldItemTriggerCheck
b.pbAbilityStatusCureCheck
end
# Check for triggering of Emergency Exit/Wimp Out/Eject Pack (only one will
# be triggered)
pbPriority(true).each do |b|
break if b.pbAbilitiesOnDamageTaken
end
pbPriority(true).each do |b|
b.droppedBelowHalfHP = false
end
end
def pbRecordBattlerAsParticipated(battler)
# Record money-doubling effect of Amulet Coin/Luck Incense
if !battler.opposes? && [:AMULETCOIN, :LUCKINCENSE].include?(battler.item_id)
@field.effects[PBEffects::AmuletCoin] = true
end
# Update battlers' participants (who will gain Exp/EVs when a battler faints)
eachBattler { |b| b.pbUpdateParticipants }
# Healing Wish/Lunar Dance
battler.pbEffectsOnEnteringPosition
# Entry hazards
end
def pbMessagesOnBattlerEnteringBattle(battler)
# Introduce Shadow Pokémon
if battler.shadowPokemon?
pbCommonAnimation("Shadow", battler)
pbDisplay(_INTL("Oh!\nA Shadow Pokémon!")) if battler.opposes?
end
end
# Called when a Pokémon enters battle, and when Ally Switch is used.
def pbEffectsOnBattlerEnteringPosition(battler)
position = @positions[battler.index]
# Healing Wish
if position.effects[PBEffects::HealingWish]
if battler.canHeal? || battler.status != :NONE
pbCommonAnimation("HealingWish", battler)
pbDisplay(_INTL("The healing wish came true for {1}!", battler.pbThis(true)))
battler.pbRecoverHP(battler.totalhp)
battler.pbCureStatus(false)
position.effects[PBEffects::HealingWish] = false
elsif Settings::MECHANICS_GENERATION < 8
position.effects[PBEffects::HealingWish] = false
end
end
# Lunar Dance
if position.effects[PBEffects::LunarDance]
full_pp = true
battler.eachMove { |m| full_pp = false if m.pp < m.total_pp }
if battler.canHeal? || battler.status != :NONE || !full_pp
pbCommonAnimation("LunarDance", battler)
pbDisplay(_INTL("{1} became cloaked in mystical moonlight!", battler.pbThis))
battler.pbRecoverHP(battler.totalhp)
battler.pbCureStatus(false)
battler.eachMove { |m| m.pp = m.total_pp }
position.effects[PBEffects::LunarDance] = false
elsif Settings::MECHANICS_GENERATION < 8
position.effects[PBEffects::LunarDance] = false
end
end
end
def pbEntryHazards(battler)
battler_side = battler.pbOwnSide
# Stealth Rock
if battler.pbOwnSide.effects[PBEffects::StealthRock] && battler.takesIndirectDamage? &&
if battler_side.effects[PBEffects::StealthRock] && battler.takesIndirectDamage? &&
GameData::Type.exists?(:ROCK) && !battler.hasActiveItem?(:HEAVYDUTYBOOTS)
bTypes = battler.pbTypes(true)
eff = Effectiveness.calculate(:ROCK, bTypes[0], bTypes[1], bTypes[2])
if !Effectiveness.ineffective?(eff)
eff = eff.to_f / Effectiveness::NORMAL_EFFECTIVE
oldHP = battler.hp
battler.pbReduceHP(battler.totalhp*eff/8,false)
pbDisplay(_INTL("Pointed stones dug into {1}!",battler.pbThis))
battler.pbReduceHP(battler.totalhp * eff / 8, false)
pbDisplay(_INTL("Pointed stones dug into {1}!", battler.pbThis))
battler.pbItemHPHealCheck
if battler.pbAbilitiesOnDamageTaken(oldHP) # Switched out
return pbOnActiveOne(battler) # For replacement battler
end
end
end
# Spikes
if battler.pbOwnSide.effects[PBEffects::Spikes]>0 && battler.takesIndirectDamage? &&
if battler_side.effects[PBEffects::Spikes] > 0 && battler.takesIndirectDamage? &&
!battler.airborne? && !battler.hasActiveItem?(:HEAVYDUTYBOOTS)
spikesDiv = [8,6,4][battler.pbOwnSide.effects[PBEffects::Spikes]-1]
oldHP = battler.hp
battler.pbReduceHP(battler.totalhp/spikesDiv,false)
pbDisplay(_INTL("{1} is hurt by the spikes!",battler.pbThis))
spikesDiv = [8, 6, 4][battler_side.effects[PBEffects::Spikes] - 1]
battler.pbReduceHP(battler.totalhp / spikesDiv, false)
pbDisplay(_INTL("{1} is hurt by the spikes!", battler.pbThis))
battler.pbItemHPHealCheck
if battler.pbAbilitiesOnDamageTaken(oldHP) # Switched out
return pbOnActiveOne(battler) # For replacement battler
end
end
# Toxic Spikes
if battler.pbOwnSide.effects[PBEffects::ToxicSpikes]>0 && !battler.fainted? &&
!battler.airborne?
if battler_side.effects[PBEffects::ToxicSpikes] > 0 && !battler.fainted? && !battler.airborne?
if battler.pbHasType?(:POISON)
battler.pbOwnSide.effects[PBEffects::ToxicSpikes] = 0
pbDisplay(_INTL("{1} absorbed the poison spikes!",battler.pbThis))
elsif battler.pbCanPoison?(nil,false) && !battler.hasActiveItem?(:HEAVYDUTYBOOTS)
if battler.pbOwnSide.effects[PBEffects::ToxicSpikes]==2
battler.pbPoison(nil,_INTL("{1} was badly poisoned by the poison spikes!",battler.pbThis),true)
battler_side.effects[PBEffects::ToxicSpikes] = 0
pbDisplay(_INTL("{1} absorbed the poison spikes!", battler.pbThis))
elsif battler.pbCanPoison?(nil, false) && !battler.hasActiveItem?(:HEAVYDUTYBOOTS)
if battler_side.effects[PBEffects::ToxicSpikes] == 2
battler.pbPoison(nil, _INTL("{1} was badly poisoned by the poison spikes!", battler.pbThis), true)
else
battler.pbPoison(nil,_INTL("{1} was poisoned by the poison spikes!",battler.pbThis))
battler.pbPoison(nil, _INTL("{1} was poisoned by the poison spikes!", battler.pbThis))
end
end
end
# Sticky Web
if battler.pbOwnSide.effects[PBEffects::StickyWeb] && !battler.fainted? &&
!battler.airborne? && !battler.hasActiveItem?(:HEAVYDUTYBOOTS)
pbDisplay(_INTL("{1} was caught in a sticky web!",battler.pbThis))
if battler_side.effects[PBEffects::StickyWeb] && !battler.fainted? && !battler.airborne? &&
!battler.hasActiveItem?(:HEAVYDUTYBOOTS)
pbDisplay(_INTL("{1} was caught in a sticky web!", battler.pbThis))
if battler.pbCanLowerStatStage?(:SPEED)
battler.pbLowerStatStage(:SPEED,1,nil)
battler.pbLowerStatStage(:SPEED, 1, nil)
battler.pbItemStatRestoreCheck
end
end
# Battler faints if it is knocked out because of an entry hazard above
if battler.fainted?
battler.pbFaint
pbGainExp
pbJudge
return false
end
battler.pbCheckForm
return true
end
end

View File

@@ -127,6 +127,7 @@ class PokeBattle_Battle
return if !battler || !battler.pokemon
return if !battler.hasMega? || battler.mega?
trainerName = pbGetOwnerName(idxBattler)
old_ability = battler.ability_id
# Break Illusion
if battler.hasActiveAbility?(:ILLUSION)
BattleHandlers.triggerTargetAbilityOnHit(battler.ability,nil,battler,nil,self)
@@ -157,7 +158,8 @@ class PokeBattle_Battle
end
pbCalculatePriority(false,[idxBattler]) if Settings::RECALCULATE_TURN_ORDER_AFTER_MEGA_EVOLUTION
# Trigger ability
battler.pbEffectsOnSwitchIn
battler.pbOnLosingAbility(old_ability)
battler.pbTriggerAbilityOnGainingIt
end
#=============================================================================
@@ -165,7 +167,7 @@ class PokeBattle_Battle
#=============================================================================
def pbPrimalReversion(idxBattler)
battler = @battlers[idxBattler]
return if !battler || !battler.pokemon
return if !battler || !battler.pokemon || battler.fainted?
return if !battler.hasPrimal? || battler.primal?
if battler.isSpecies?(:KYOGRE)
pbCommonAnimation("PrimalKyogre",battler)

View File

@@ -60,7 +60,7 @@ class PokeBattle_Battle
return if @decision>0
# Switch Pokémon
pbRecallAndReplace(b.index,idxNewPkmn)
b.pbEffectsOnSwitchIn(true)
pbOnBattlerEnteringBattle(b.index)
end
end

View File

@@ -267,13 +267,10 @@ class PokeBattle_Battle
priority.each do |b|
next if b.opposes?(side)
next if !b.takesIndirectDamage? || b.pbHasType?(:FIRE)
oldHP = b.hp
@scene.pbDamageAnimation(b)
b.pbReduceHP(b.totalhp/8,false)
pbDisplay(_INTL("{1} is hurt by the sea of fire!",b.pbThis))
b.pbItemHPHealCheck
b.pbAbilitiesOnDamageTaken(oldHP)
b.pbFaint if b.fainted?
b.pbTakeEffectDamage(b.totalhp / 8, false) { |hp_lost|
pbDisplay(_INTL("{1} is hurt by the sea of fire!", b.pbThis))
}
end
end
# Status-curing effects/abilities and HP-healing items
@@ -314,16 +311,12 @@ class PokeBattle_Battle
next if !b.takesIndirectDamage?
recipient = @battlers[b.effects[PBEffects::LeechSeed]]
next if !recipient || recipient.fainted?
oldHP = b.hp
oldHPRecipient = recipient.hp
pbCommonAnimation("LeechSeed",recipient,b)
hpLoss = b.pbReduceHP(b.totalhp/8)
recipient.pbRecoverHPFromDrain(hpLoss,b,
_INTL("{1}'s health is sapped by Leech Seed!",b.pbThis))
recipient.pbAbilitiesOnDamageTaken(oldHPRecipient) if recipient.hp<oldHPRecipient
b.pbItemHPHealCheck
b.pbAbilitiesOnDamageTaken(oldHP)
b.pbFaint if b.fainted?
b.pbTakeEffectDamage(b.totalhp / 8) { |hp_lost|
recipient.pbRecoverHPFromDrain(hp_lost, b,
_INTL("{1}'s health is sapped by Leech Seed!", b.pbThis))
recipient.pbAbilitiesOnDamageTaken
}
recipient.pbFaint if recipient.fainted?
end
# Damage from Hyper Mode (Shadow Pokémon)
@@ -357,45 +350,37 @@ class PokeBattle_Battle
pbHideAbilitySplash(b)
end
elsif b.takesIndirectDamage?
oldHP = b.hp
dmg = (b.statusCount==0) ? b.totalhp/8 : b.totalhp*b.effects[PBEffects::Toxic]/16
b.pbContinueStatus { b.pbReduceHP(dmg,false) }
b.pbItemHPHealCheck
b.pbAbilitiesOnDamageTaken(oldHP)
b.pbAbilitiesOnDamageTaken
b.pbFaint if b.fainted?
end
end
# Damage from burn
priority.each do |b|
next if b.status != :BURN || !b.takesIndirectDamage?
oldHP = b.hp
dmg = (Settings::MECHANICS_GENERATION >= 7) ? b.totalhp/16 : b.totalhp/8
dmg = (dmg/2.0).round if b.hasActiveAbility?(:HEATPROOF)
b.pbContinueStatus { b.pbReduceHP(dmg,false) }
b.pbItemHPHealCheck
b.pbAbilitiesOnDamageTaken(oldHP)
b.pbAbilitiesOnDamageTaken
b.pbFaint if b.fainted?
end
# Damage from sleep (Nightmare)
priority.each do |b|
b.effects[PBEffects::Nightmare] = false if !b.asleep?
next if !b.effects[PBEffects::Nightmare] || !b.takesIndirectDamage?
oldHP = b.hp
b.pbReduceHP(b.totalhp/4)
pbDisplay(_INTL("{1} is locked in a nightmare!",b.pbThis))
b.pbItemHPHealCheck
b.pbAbilitiesOnDamageTaken(oldHP)
b.pbFaint if b.fainted?
b.pbTakeEffectDamage(b.totalhp / 4) { |hp_lost|
pbDisplay(_INTL("{1} is locked in a nightmare!", b.pbThis))
}
end
# Curse
priority.each do |b|
next if !b.effects[PBEffects::Curse] || !b.takesIndirectDamage?
oldHP = b.hp
b.pbReduceHP(b.totalhp/4)
pbDisplay(_INTL("{1} is afflicted by the curse!",b.pbThis))
b.pbItemHPHealCheck
b.pbAbilitiesOnDamageTaken(oldHP)
b.pbFaint if b.fainted?
b.pbTakeEffectDamage(b.totalhp / 4) { |hp_lost|
pbDisplay(_INTL("{1} is afflicted by the curse!", b.pbThis))
}
end
# Trapping attacks (Bind/Clamp/Fire Spin/Magma Storm/Sand Tomb/Whirlpool/Wrap)
priority.each do |b|
@@ -421,11 +406,9 @@ class PokeBattle_Battle
hpLoss = (Settings::MECHANICS_GENERATION >= 6) ? b.totalhp/6 : b.totalhp/8
end
@scene.pbDamageAnimation(b)
b.pbReduceHP(hpLoss,false)
pbDisplay(_INTL("{1} is hurt by {2}!",b.pbThis,moveName))
b.pbItemHPHealCheck
# NOTE: No need to call pbAbilitiesOnDamageTaken as b can't switch out.
b.pbFaint if b.fainted?
b.pbTakeEffectDamage(hpLoss, false) { |hp_lost|
pbDisplay(_INTL("{1} is hurt by {2}!", b.pbThis, moveName))
}
end
end
end
@@ -647,7 +630,8 @@ class PokeBattle_Battle
b.effects[PBEffects::ThroatChop] -= 1 if b.effects[PBEffects::ThroatChop]>0
b.lastHPLost = 0
b.lastHPLostFromFoe = 0
b.tookDamage = false
b.droppedBelowHalfHP = false
b.tookDamageThisRound = false
b.tookPhysicalHit = false
b.statsRaised = false
b.statsLowered = false