Refactored battle-starting methods into WildBattle.start and TrainerBattle.start, etc.

This commit is contained in:
Maruno17
2022-05-15 20:28:06 +01:00
parent 07d7403fbf
commit 22f0c8627a
10 changed files with 700 additions and 470 deletions

View File

@@ -330,6 +330,8 @@ class SafariBattle
@ballCount = 0
end
def disablePokeBalls=(value); end
def sendToBoxes=(value); end
def defaultWeather=(value); @weather = value; end
def defaultTerrain=(value); end

View File

@@ -210,9 +210,9 @@ def pbBattleOnStepTaken(repel_active)
if $PokemonEncounters.have_double_wild_battle?
encounter2 = $PokemonEncounters.choose_wild_pokemon(encounter_type)
EventHandlers.trigger(:on_wild_species_chosen, encounter2)
pbDoubleWildBattle(encounter[0], encounter[1], encounter2[0], encounter2[1])
WildBattle.start(encounter, encounter2, can_override: true)
else
pbWildBattle(encounter[0], encounter[1])
WildBattle.start(encounter, can_override: true)
end
$game_temp.encounter_type = nil
$game_temp.encounter_triggered = true

View File

@@ -461,9 +461,9 @@ def pbEncounter(enc_type, only_single = true)
encounter2 = $PokemonEncounters.choose_wild_pokemon(enc_type)
EventHandlers.trigger(:on_wild_species_chosen, encounter2)
return false if !encounter2
pbDoubleWildBattle(encounter1[0], encounter1[1], encounter2[0], encounter2[1])
WildBattle.start(encounter1, encounter2, can_override: true)
else
pbWildBattle(encounter1[0], encounter1[1])
WildBattle.start(encounter1, can_override: true)
end
$game_temp.encounter_type = nil
$game_temp.force_single_battle = false

View File

@@ -212,7 +212,7 @@ def pbRoamingPokemonBattle(species, level)
setBattleRule("single")
setBattleRule("roamerFlees")
# Perform the battle
decision = pbWildBattleCore($PokemonGlobal.roamPokemon[idxRoamer])
decision = WildBattle.start_core($PokemonGlobal.roamPokemon[idxRoamer])
# Update Roaming Pokémon data based on result of battle
if [1, 4].include?(decision) # Defeated or caught
$PokemonGlobal.roamPokemon[idxRoamer] = true

View File

@@ -54,14 +54,14 @@ def pbOrganizedBattleEx(opponent, challengedata, endspeech, endspeechwin)
olditems = $player.party.transform { |p| p.item_id }
olditems2 = opponent.party.transform { |p| p.item_id }
# Create the battle scene (the visual side of it)
scene = pbNewBattleScene
scene = BattleCreationHelperMethods.create_battle_scene
# Create the battle class (the mechanics side of it)
battle = challengedata.createBattle(scene, $player, opponent)
battle.internalBattle = false
battle.endSpeeches = [endspeech]
battle.endSpeechesWin = [endspeechwin]
# Set various other properties in the battle class
pbPrepareBattle(battle)
BattleCreationHelperMethods.prepare_battle(battle)
# Perform the battle itself
decision = 0
pbBattleAnimation(pbGetTrainerBattleBGM(opponent)) {
@@ -114,7 +114,7 @@ end
def pbPlayBattle(battledata)
return if !battledata
scene = pbNewBattleScene
scene = BattleCreationHelperMethods.create_battle_scene
scene.abortable = true
lastbattle = Marshal.restore(battledata)
case lastbattle[0]

View File

@@ -121,11 +121,11 @@ def pbSafariBattle(species, level)
# Calculate who the trainer is
playerTrainer = $player
# Create the battle scene (the visual side of it)
scene = pbNewBattleScene
scene = BattleCreationHelperMethods.create_battle_scene
# Create the battle class (the mechanics side of it)
battle = SafariBattle.new(scene, playerTrainer, foeParty)
battle.ballCount = pbSafariState.ballcount
pbPrepareBattle(battle)
BattleCreationHelperMethods.prepare_battle(battle)
# Perform the battle itself
decision = 0
pbBattleAnimation(pbGetWildBattleBGM(foeParty), 0, foeParty) {

View File

@@ -364,18 +364,18 @@ def pbBugContestBattle(species, level)
playerParty = $player.party
playerPartyStarts = [0]
# Create the battle scene (the visual side of it)
scene = pbNewBattleScene
scene = BattleCreationHelperMethods.create_battle_scene
# Create the battle class (the mechanics side of it)
battle = BugContestBattle.new(scene, playerParty, foeParty, playerTrainer, nil)
battle.party1starts = playerPartyStarts
battle.ballCount = pbBugContestState.ballcount
setBattleRule("single")
pbPrepareBattle(battle)
BattleCreationHelperMethods.prepare_battle(battle)
# Perform the battle itself
decision = 0
pbBattleAnimation(pbGetWildBattleBGM(foeParty), 0, foeParty) {
decision = battle.pbStartBattle
pbAfterBattle(decision, true)
BattleCreationHelperMethods.after_battle(decision, true)
if [2, 5].include?(decision) # Lost or drew
$game_system.bgm_unpause
$game_system.bgs_unpause
@@ -390,19 +390,7 @@ def pbBugContestBattle(species, level)
pbBugContestState.pbStartJudging
end
# Save the result of the battle in Game Variable 1
# 0 - Undecided or aborted
# 1 - Player won
# 2 - Player lost
# 3 - Player or wild Pokémon ran from battle, or player forfeited the match
# 4 - Wild Pokémon was caught
# 5 - Draw
case decision
when 1, 4 # Won, caught
$stats.wild_battles_won += 1
when 2, 3, 5 # Lost, fled, draw
$stats.wild_battles_lost += 1
end
pbSet(1, decision)
BattleCreationHelperMethods(decision, 1)
# Used by the Poké Radar to update/break the chain
EventHandlers.trigger(:on_wild_battle_end, species, level, decision)
# Return false if the player lost or drew the battle, and true if any other result

View File

@@ -176,7 +176,8 @@ MenuHandlers.add(:debug_menu, :test_wild_battle, {
GameData::Species.get(species).name), params)
if level > 0
$game_temp.encounter_type = nil
pbWildBattle(species, level)
setBattleRule("canLose")
WildBattle.start(species, level)
end
end
next false
@@ -205,8 +206,9 @@ MenuHandlers.add(:debug_menu, :test_wild_battle_advanced, {
next
end
setBattleRule(sprintf("%dv%d", size0, pkmn.length))
setBattleRule("canLose")
$game_temp.encounter_type = nil
pbWildBattleCore(*pkmn)
WildBattle.start(*pkmn)
break
elsif pkmnCmd == pkmnCmds.length - 2 # Set player side size
if !pbCanDoubleBattle?
@@ -254,7 +256,8 @@ MenuHandlers.add(:debug_menu, :test_trainer_battle, {
"effect" => proc {
trainerdata = pbListScreen(_INTL("SINGLE TRAINER"), TrainerBattleLister.new(0, false))
if trainerdata
pbTrainerBattle(trainerdata[0], trainerdata[1], nil, false, trainerdata[2], true)
setBattleRule("canLose")
TrainerBattle.start(trainerdata[0], trainerdata[1], trainerdata[2])
end
next false
}
@@ -293,9 +296,10 @@ MenuHandlers.add(:debug_menu, :test_trainer_battle_advanced, {
next
end
setBattleRule(sprintf("%dv%d", size0, size1))
setBattleRule("canLose")
battleArgs = []
trainers.each { |t| battleArgs.push(t[1]) }
pbTrainerBattleCore(*battleArgs)
TrainerBattle.start(*battleArgs)
break
elsif trainerCmd == trainerCmds.length - 2 # Set opponent side size
if trainers.length == 0 || (trainers.length == 1 && trainers[0][1].party_count == 1)

View File

@@ -523,7 +523,7 @@ module Compiler
backdrop = $~[1].gsub(/^\s+/, "").gsub(/\s+$/, "")
push_comment(firstpage.list, command) if rewriteComments
elsif command[/^EndSpeech\:\s*([\s\S]+)$/i]
endspeeches.push($~[1].gsub(/^\s+/, "").gsub(/\s+$/, ""))
endspeeches.push(command)
push_comment(firstpage.list, command) if rewriteComments
elsif command[/^Outcome\:\s*(\d+)$/i]
outcome = $~[1].to_i
@@ -562,6 +562,7 @@ module Compiler
safetrcombo = sprintf(":%s,\"%s\"", trtype, safequote(trname)) # :YOUNGSTER,"Joey"
introplay = sprintf("pbTrainerIntro(:%s)", trtype)
# Write first page
push_comment(firstpage.list, endspeeches[0]) if endspeeches[0] # Just so it isn't lost
push_script(firstpage.list, introplay) # pbTrainerIntro
push_script(firstpage.list, "pbNoticePlayer(get_self)")
push_text(firstpage.list, battles[0])
@@ -572,13 +573,10 @@ module Compiler
push_script(firstpage.list, sprintf("setBattleRule(\"backdrop\",\"%s\")", safequote(backdrop))) if backdrop
push_script(firstpage.list, sprintf("setBattleRule(\"outcomeVar\",%d)", outcome)) if outcome > 1
push_script(firstpage.list, "setBattleRule(\"canLose\")") if continue
espeech = (endspeeches[0]) ? sprintf("_I(\"%s\")", safequote2(endspeeches[0])) : "nil"
if battleid > 0
battleString = sprintf("pbTrainerBattle(%s,%s,nil,%d)", safetrcombo, espeech, battleid)
elsif endspeeches[0]
battleString = sprintf("pbTrainerBattle(%s,%s)", safetrcombo, espeech)
battleString = sprintf("TrainerBattle.start(%s,%d)", safetrcombo, battleid)
else
battleString = sprintf("pbTrainerBattle(%s)", safetrcombo)
battleString = sprintf("TrainerBattle.start(%s)", safetrcombo)
end
push_branch(firstpage.list, battleString)
if battles.length > 1 # Has rematches
@@ -621,12 +619,7 @@ module Compiler
push_script(rematchpage.list, sprintf("setBattleRule(\"backdrop\",%s)", safequote(backdrop)), 1) if backdrop
push_script(rematchpage.list, sprintf("setBattleRule(\"outcomeVar\",%d)", outcome), 1) if outcome > 1
push_script(rematchpage.list, "setBattleRule(\"canLose\")", 1) if continue
espeech = nil
if endspeeches.length > 0
espeech = (endspeeches[i]) ? endspeeches[i] : endspeeches[endspeeches.length - 1]
end
espeech = (espeech) ? sprintf("_I(\"%s\")", safequote2(espeech)) : "nil"
battleString = sprintf("pbTrainerBattle(%s,%s,nil,%d)", safetrcombo, espeech, battleid + i)
battleString = sprintf("TrainerBattle.start(%s,%d)", safetrcombo, battleid + i)
push_branch(rematchpage.list, battleString, 1)
push_script(rematchpage.list, sprintf("pbPhoneIncrement(%s,%d)", safetrcombo, battles.length), 2)
push_self_switch(rematchpage.list, "A", true, 2)
@@ -912,6 +905,150 @@ module Compiler
return ret
end
def replace_old_battle_scripts(event, list, index)
changed = false
script = list[index].parameters[1]
if script[/^\s*pbWildBattle\((.+)\)\s*$/]
battle_params = $1.split(",")
list[index].parameters[1] = sprintf("WildBattle.start(#{battle_params[0].strip}, #{battle_params[1].strip})")
old_indent = list[index].indent
new_events = []
if battle_params[3] && battle_params[3][/false/]
push_script(new_events, "setBattleRule(\"cannotRun\")", old_indent)
end
if battle_params[4] && battle_params[4][/true/]
push_script(new_events, "setBattleRule(\"canLose\")", old_indent)
end
if battle_params[2] && battle_params[2].strip != "1"
push_script(new_events, "setBattleRule(\"outcome\", #{battle_params[2].strip})", old_indent)
end
list[index, 0] = new_events if new_events.length > 0
changed = true
elsif script[/^\s*pbDoubleWildBattle\((.+)\)\s*$/]
battle_params = $1.split(",")
pkmn1 = "#{battle_params[0].strip}, #{battle_params[1].strip}"
pkmn2 = "#{battle_params[2].strip}, #{battle_params[3].strip}"
list[index].parameters[1] = sprintf("WildBattle.start(#{pkmn1}, #{pkmn2})")
old_indent = list[index].indent
new_events = []
if battle_params[3] && battle_params[5][/false/]
push_script(new_events, "setBattleRule(\"cannotRun\")", old_indent)
end
if battle_params[4] && battle_params[6][/true/]
push_script(new_events, "setBattleRule(\"canLose\")", old_indent)
end
if battle_params[2] && battle_params[4].strip != "1"
push_script(new_events, "setBattleRule(\"outcome\", #{battle_params[4].strip})", old_indent)
end
list[index, 0] = new_events if new_events.length > 0
changed = true
elsif script[/^\s*pbTripleWildBattle\((.+)\)\s*$/]
battle_params = $1.split(",")
pkmn1 = "#{battle_params[0].strip}, #{battle_params[1].strip}"
pkmn2 = "#{battle_params[2].strip}, #{battle_params[3].strip}"
pkmn3 = "#{battle_params[4].strip}, #{battle_params[5].strip}"
list[index].parameters[1] = sprintf("WildBattle.start(#{pkmn1}, #{pkmn2}, #{pkmn3})")
old_indent = list[index].indent
new_events = []
if battle_params[3] && battle_params[7][/false/]
push_script(new_events, "setBattleRule(\"cannotRun\")", old_indent)
end
if battle_params[4] && battle_params[8][/true/]
push_script(new_events, "setBattleRule(\"canLose\")", old_indent)
end
if battle_params[2] && battle_params[6].strip != "1"
push_script(new_events, "setBattleRule(\"outcome\", #{battle_params[6].strip})", old_indent)
end
list[index, 0] = new_events if new_events.length > 0
changed = true
elsif script[/^\s*pbTrainerBattle\((.+)\)\s*$/]
battle_params = $1.split(",")
trainer1 = "#{battle_params[0].strip}, #{battle_params[1].strip}"
trainer1 += ", #{battle_params[4].strip}" if battle_params[4].strip != "nil"
list[index].parameters[1] = "TrainerBattle.start(#{trainer1})"
old_indent = list[index].indent
new_events = []
if battle_params[2] && !battle_params[2].strip.empty? && battle_params[2].strip != "nil"
speech = battle_params[2].gsub(/^\s*_I\(\s*"\s*/, "")
speech.gsub!(/\"\s*\)\s*$/, "").strip
push_comment(new_events, "EndSpeech: #{speech}", old_indent)
end
if battle_params[3] && battle_params[3][/true/]
push_script(new_events, "setBattleRule(\"double\")", old_indent)
end
if battle_params[5] && battle_params[5][/true/]
push_script(new_events, "setBattleRule(\"canLose\")", old_indent)
end
if battle_params[6] && battle_params[6].strip != "1"
push_script(new_events, "setBattleRule(\"outcome\", #{battle_params[6].strip})", old_indent)
end
list[index, 0] = new_events if new_events.length > 0
changed = true
elsif script[/^\s*pbDoubleTrainerBattle\((.+)\)\s*$/]
battle_params = $1.split(",")
trainer1 = "#{battle_params[0].strip}, #{battle_params[1].strip}"
trainer1 += ", #{battle_params[2].strip}" if battle_params[2].strip != "nil"
trainer2 = "#{battle_params[4].strip}, #{battle_params[5].strip}"
trainer2 += ", #{battle_params[6].strip}" if battle_params[6] && battle_params[6].strip != "nil"
list[index].parameters[1] = "TrainerBattle.start(#{trainer1}, #{trainer2})"
old_indent = list[index].indent
new_events = []
if battle_params[3] && !battle_params[3].strip.empty? && battle_params[3].strip != "nil"
speech = battle_params[3].gsub(/^\s*_I\(\s*"\s*/, "")
speech.gsub!(/\"\s*\)\s*$/, "").strip
push_comment(new_events, "EndSpeech1: #{speech}", old_indent)
end
if battle_params[7] && !battle_params[7].strip.empty? && battle_params[7].strip != "nil"
speech = battle_params[7].gsub(/^\s*_I\(\s*"\s*/, "")
speech.gsub!(/\"\s*\)\s*$/, "").strip
push_comment(new_events, "EndSpeech2: #{speech}", old_indent)
end
if battle_params[8] && battle_params[8][/true/]
push_script(new_events, "setBattleRule(\"canLose\")", old_indent)
end
if battle_params[9] && battle_params[9].strip != "1"
push_script(new_events, "setBattleRule(\"outcome\", #{battle_params[9].strip})", old_indent)
end
list[index, 0] = new_events if new_events.length > 0
changed = true
elsif script[/^\s*pbTripleTrainerBattle\((.+)\)\s*$/]
battle_params = $1.split(",")
trainer1 = "#{battle_params[0].strip}, #{battle_params[1].strip}"
trainer1 += ", #{battle_params[2].strip}" if battle_params[2].strip != "nil"
trainer2 = "#{battle_params[4].strip}, #{battle_params[5].strip}"
trainer2 += ", #{battle_params[6].strip}" if battle_params[6] && battle_params[6].strip != "nil"
trainer3 = "#{battle_params[8].strip}, #{battle_params[9].strip}"
trainer3 += ", #{battle_params[10].strip}" if battle_params[10] && battle_params[10].strip != "nil"
list[index].parameters[1] = "TrainerBattle.start(#{trainer1}, #{trainer2}, #{trainer3})"
old_indent = list[index].indent
new_events = []
if battle_params[3] && !battle_params[3].strip.empty? && battle_params[3].strip != "nil"
speech = battle_params[3].gsub(/^\s*_I\(\s*"\s*/, "")
speech.gsub!(/\"\s*\)\s*$/, "").strip
push_comment(new_events, "EndSpeech1: #{speech}", old_indent)
end
if battle_params[7] && !battle_params[7].strip.empty? && battle_params[7].strip != "nil"
speech = battle_params[7].gsub(/^\s*_I\(\s*"\s*/, "")
speech.gsub!(/\"\s*\)\s*$/, "").strip
push_comment(new_events, "EndSpeech2: #{speech}", old_indent)
end
if battle_params[7] && !battle_params[7].strip.empty? && battle_params[11].strip != "nil"
speech = battle_params[11].gsub(/^\s*_I\(\s*"\s*/, "")
speech.gsub!(/\"\s*\)\s*$/, "").strip
push_comment(new_events, "EndSpeech3: #{speech}", old_indent)
end
if battle_params[12] && battle_params[12][/true/]
push_script(new_events, "setBattleRule(\"canLose\")", old_indent)
end
if battle_params[13] && battle_params[13].strip != "1"
push_script(new_events, "setBattleRule(\"outcome\", #{battle_params[13].strip})", old_indent)
end
list[index, 0] = new_events if new_events.length > 0
changed = true
end
return changed
end
def fix_event_use(event, _mapID, mapData)
return nil if event_is_empty?(event)
changed = false
@@ -1283,6 +1420,7 @@ module Compiler
when 111 # Conditional Branch
if list[i].parameters[0] == 12 # script
script = list[i].parameters[1]
changed = true if replace_old_battle_scripts(event, list, i)
if script[trainerMoneyRE] # Compares $player.money with a value
# Checking money directly
operator = $1
@@ -1313,7 +1451,7 @@ module Compiler
# Using pbItemBall on non-item events, change it
list[i].parameters[1] = script.sub(/pbItemBall/, "pbReceiveItem")
changed = true
elsif script[/^\s*(Kernel\.)?(pbTrainerBattle|pbDoubleTrainerBattle)/]
elsif script[/^\s*(TrainerBattle.start)/]
# Check if trainer battle conditional branch is empty
j = i + 1
isempty = true