Fixed recalculating turn order after Mega Evolution not taking into account changed abilities

This commit is contained in:
Maruno17
2022-04-08 18:43:49 +01:00
parent 0680f8665d
commit 4a478ab6d0
4 changed files with 80 additions and 55 deletions

View File

@@ -147,8 +147,9 @@ class Battle
(0..maxBattlerIndex).each do |i| (0..maxBattlerIndex).each do |i|
b = @battlers[i] b = @battlers[i]
next if !b next if !b
# [battler, speed, sub-priority, priority, tie-breaker order] # [battler, speed, sub-priority from ability, sub-priority from item,
bArray = [b, b.pbSpeed, 0, 0, randomOrder[i]] # final sub-priority, priority, tie-breaker order]
entry = [b, b.pbSpeed, 0, 0, 0, 0, randomOrder[i]]
if @choices[b.index][0] == :UseMove || @choices[b.index][0] == :Shift if @choices[b.index][0] == :UseMove || @choices[b.index][0] == :Shift
# Calculate move's priority # Calculate move's priority
if @choices[b.index][0] == :UseMove if @choices[b.index][0] == :UseMove
@@ -157,35 +158,20 @@ class Battle
if b.abilityActive? if b.abilityActive?
pri = Battle::AbilityEffects.triggerPriorityChange(b.ability, b, move, pri) pri = Battle::AbilityEffects.triggerPriorityChange(b.ability, b, move, pri)
end end
bArray[3] = pri entry[5] = pri
@choices[b.index][4] = pri @choices[b.index][4] = pri
end end
# Calculate sub-priority (first/last within priority bracket) # Calculate sub-priority changes (first/last within priority bracket)
# NOTE: Going fast beats going slow. A Pokémon with Stall and Quick
# Claw will go first in its priority bracket if Quick Claw
# triggers, regardless of Stall.
subPri = 0
# Abilities (Stall) # Abilities (Stall)
if b.abilityActive? if b.abilityActive?
newSubPri = Battle::AbilityEffects.triggerPriorityBracketChange(b.ability, b, subPri, self) entry[2] = Battle::AbilityEffects.triggerPriorityBracketChange(b.ability, b, self)
if subPri != newSubPri
subPri = newSubPri
b.effects[PBEffects::PriorityAbility] = true
b.effects[PBEffects::PriorityItem] = false
end
end end
# Items (Quick Claw, Custap Berry, Lagging Tail, Full Incense) # Items (Quick Claw, Custap Berry, Lagging Tail, Full Incense)
if b.itemActive? if b.itemActive?
newSubPri = Battle::ItemEffects.triggerPriorityBracketChange(b.item, b, subPri, self) entry[3] = Battle::ItemEffects.triggerPriorityBracketChange(b.item, b, self)
if subPri != newSubPri
subPri = newSubPri
b.effects[PBEffects::PriorityAbility] = false
b.effects[PBEffects::PriorityItem] = true
end end
end end
bArray[2] = subPri @priority.push(entry)
end
@priority.push(bArray)
end end
needRearranging = true needRearranging = true
else else
@@ -193,38 +179,77 @@ class Battle
needRearranging = true needRearranging = true
@priorityTrickRoom = (@field.effects[PBEffects::TrickRoom] > 0) @priorityTrickRoom = (@field.effects[PBEffects::TrickRoom] > 0)
end end
# Just recheck all battler speeds # Recheck all battler speeds and changes to priority caused by abilities
@priority.each do |orderArray| @priority.each do |entry|
next if !orderArray next if !entry
next if indexArray && !indexArray.include?(orderArray[0].index) next if indexArray && !indexArray.include?(entry[0].index)
oldSpeed = orderArray[1] # Recalculate speed of battler
orderArray[1] = orderArray[0].pbSpeed newSpeed = entry[0].pbSpeed
needRearranging = true if orderArray[1] != oldSpeed needRearranging = true if newSpeed != entry[1]
entry[1] = newSpeed
# Recalculate move's priority in case ability has changed
choice = @choices[entry[0].index]
if choice[0] == :UseMove
move = choice[2]
pri = move.pbPriority(entry[0])
if entry[0].abilityActive?
pri = Battle::AbilityEffects.triggerPriorityChange(entry[0].ability, entry[0], move, pri)
end end
needRearranging = true if pri != entry[5]
entry[5] = pri
choice[4] = pri
end
# Recalculate sub-priority change caused by ability (but not by item)
if entry[0].abilityActive?
subPri = Battle::AbilityEffects.triggerPriorityBracketChange(entry[0].ability, entry[0], self)
needRearranging = true if subPri != entry[2]
entry[2] = subPri
end
end
end
# Calculate each battler's overall sub-priority, and whether its ability or
# item is responsible
# NOTE: Going fast beats going slow. A Pokémon with Stall and Quick Claw
# will go first in its priority bracket if Quick Claw triggers,
# regardless of Stall.
@priority.each do |entry|
entry[0].effects[PBEffects::PriorityAbility] = false
entry[0].effects[PBEffects::PriorityItem] = false
# TODO: Set b.effects[PBEffects::PriorityAbility] and the other one depending
# on the sub-priorities. Calculate final sub-priorities for each battler.
subpri = entry[2] # Sub-priority from ability
if (subpri == 0 && entry[3] != 0) || # Ability has no effect, item has effect
(subpri < 0 && entry[3] >= 1) # Ability makes it slower, item makes it faster
subpri = entry[3] # Sub-priority from item
entry[0].effects[PBEffects::PriorityItem] = true
elsif subpri != 0 # Ability has effect, item had no/superfluous effect
entry[0].effects[PBEffects::PriorityAbility] = true
end
entry[4] = subpri # Final sub-priority
end end
# Reorder the priority array # Reorder the priority array
if needRearranging if needRearranging
@priority.sort! { |a, b| @priority.sort! { |a, b|
if a[3] != b[3] if a[5] != b[5]
# Sort by priority (highest value first) # Sort by priority (highest value first)
b[3] <=> a[3] b[5] <=> a[5]
elsif a[2] != b[2] elsif a[4] != b[4]
# Sort by sub-priority (highest value first) # Sort by sub-priority (highest value first)
b[2] <=> a[2] b[4] <=> a[4]
elsif @priorityTrickRoom elsif @priorityTrickRoom
# Sort by speed (lowest first), and use tie-breaker if necessary # Sort by speed (lowest first), and use tie-breaker if necessary
(a[1] == b[1]) ? b[4] <=> a[4] : a[1] <=> b[1] (a[1] == b[1]) ? b[6] <=> a[6] : a[1] <=> b[1]
else else
# Sort by speed (highest first), and use tie-breaker if necessary # Sort by speed (highest first), and use tie-breaker if necessary
(a[1] == b[1]) ? b[4] <=> a[4] : b[1] <=> a[1] (a[1] == b[1]) ? b[6] <=> a[6] : b[1] <=> a[1]
end end
} }
# Write the priority order to the debug log # Write the priority order to the debug log
logMsg = (fullCalc) ? "[Round order] " : "[Round order recalculated] " logMsg = (fullCalc) ? "[Round order] " : "[Round order recalculated] "
comma = false comma = false
@priority.each do |orderArray| @priority.each do |entry|
logMsg += ", " if comma logMsg += ", " if comma
logMsg += "#{orderArray[0].pbThis(comma)} (#{orderArray[0].index})" logMsg += "#{entry[0].pbThis(comma)} (#{entry[0].index})"
comma = true comma = true
end end
PBDebug.log(logMsg) PBDebug.log(logMsg)
@@ -236,7 +261,7 @@ class Battle
if onlySpeedSort if onlySpeedSort
# Sort battlers by their speed stats and tie-breaker order only. # Sort battlers by their speed stats and tie-breaker order only.
tempArray = [] tempArray = []
@priority.each { |pArray| tempArray.push([pArray[0], pArray[1], pArray[4]]) } @priority.each { |pArray| tempArray.push([pArray[0], pArray[1], pArray[6]]) }
tempArray.sort! { |a, b| (a[1] == b[1]) ? b[2] <=> a[2] : b[1] <=> a[1] } tempArray.sort! { |a, b| (a[1] == b[1]) ? b[2] <=> a[2] : b[1] <=> a[1] }
tempArray.each { |tArray| ret.push(tArray[0]) } tempArray.each { |tArray| ret.push(tArray[0]) }
else else

View File

@@ -162,10 +162,11 @@ class Battle
if battler.isSpecies?(:GENGAR) && battler.mega? if battler.isSpecies?(:GENGAR) && battler.mega?
battler.effects[PBEffects::Telekinesis] = 0 battler.effects[PBEffects::Telekinesis] = 0
end end
pbCalculatePriority(false, [idxBattler]) if Settings::RECALCULATE_TURN_ORDER_AFTER_MEGA_EVOLUTION
# Trigger ability # Trigger ability
battler.pbOnLosingAbility(old_ability) battler.pbOnLosingAbility(old_ability)
battler.pbTriggerAbilityOnGainingIt battler.pbTriggerAbilityOnGainingIt
# Recalculate turn order
pbCalculatePriority(false, [idxBattler]) if Settings::RECALCULATE_TURN_ORDER_AFTER_MEGA_EVOLUTION
end end
#============================================================================= #=============================================================================

View File

@@ -141,8 +141,8 @@ module Battle::AbilityEffects
return trigger(PriorityChange, ability, battler, move, priority, ret: priority) return trigger(PriorityChange, ability, battler, move, priority, ret: priority)
end end
def self.triggerPriorityBracketChange(ability, battler, sub_priority, battle) def self.triggerPriorityBracketChange(ability, battler, battle)
return trigger(PriorityBracketChange, ability, battler, sub_priority, battle, ret: sub_priority) return trigger(PriorityBracketChange, ability, battler, battle, ret: 0)
end end
def self.triggerPriorityBracketUse(ability, battler, battle) def self.triggerPriorityBracketUse(ability, battler, battle)
@@ -838,14 +838,14 @@ Battle::AbilityEffects::PriorityChange.add(:TRIAGE,
#=============================================================================== #===============================================================================
Battle::AbilityEffects::PriorityBracketChange.add(:QUICKDRAW, Battle::AbilityEffects::PriorityBracketChange.add(:QUICKDRAW,
proc { |ability, battler, subPri, battle| proc { |ability, battler, battle|
next 1 if subPri == 0 && battle.pbRandom(100) < 30 next 1 if battle.pbRandom(100) < 30
} }
) )
Battle::AbilityEffects::PriorityBracketChange.add(:STALL, Battle::AbilityEffects::PriorityBracketChange.add(:STALL,
proc { |ability, battler, subPri, battle| proc { |ability, battler, battle|
next -1 if subPri == 0 next -1
} }
) )

View File

@@ -83,8 +83,8 @@ module Battle::ItemEffects
#============================================================================= #=============================================================================
def self.triggerPriorityBracketChange(item, battler, sub_pri, battle) def self.triggerPriorityBracketChange(item, battler, battle)
return trigger(PriorityBracketChange, item, battler, sub_pri, battle, ret: sub_pri) return trigger(PriorityBracketChange, item, battler, battle, ret: 0)
end end
def self.triggerPriorityBracketUse(item, battler, battle) def self.triggerPriorityBracketUse(item, battler, battle)
@@ -633,23 +633,22 @@ Battle::ItemEffects::StatusCure.add(:RAWSTBERRY,
#=============================================================================== #===============================================================================
Battle::ItemEffects::PriorityBracketChange.add(:CUSTAPBERRY, Battle::ItemEffects::PriorityBracketChange.add(:CUSTAPBERRY,
proc { |item, battler, subPri, battle| proc { |item, battler, battle|
next if !battler.canConsumePinchBerry? next 1 if battler.canConsumePinchBerry?
next 1 if subPri < 1
} }
) )
Battle::ItemEffects::PriorityBracketChange.add(:LAGGINGTAIL, Battle::ItemEffects::PriorityBracketChange.add(:LAGGINGTAIL,
proc { |item, battler, subPri, battle| proc { |item, battler, battle|
next -1 if subPri == 0 next -1
} }
) )
Battle::ItemEffects::PriorityBracketChange.copy(:LAGGINGTAIL, :FULLINCENSE) Battle::ItemEffects::PriorityBracketChange.copy(:LAGGINGTAIL, :FULLINCENSE)
Battle::ItemEffects::PriorityBracketChange.add(:QUICKCLAW, Battle::ItemEffects::PriorityBracketChange.add(:QUICKCLAW,
proc { |item, battler, subPri, battle| proc { |item, battler, battle|
next 1 if subPri < 1 && battle.pbRandom(100) < 20 next 1 if battle.pbRandom(100) < 20
} }
) )