Merged Events and EncounterModifier into module EventHandlers

This commit is contained in:
Maruno17
2021-12-30 18:59:35 +00:00
parent a6c092a574
commit 7da449aec3
28 changed files with 812 additions and 807 deletions

View File

@@ -188,18 +188,20 @@ def pbGetEnvironment
return ret
end
Events.onStartBattle += proc { |_sender|
# Record current levels of Pokémon in party, to see if they gain a level
# during battle and may need to evolve afterwards
$game_temp.party_levels_before_battle = []
$game_temp.party_critical_hits_dealt = []
$game_temp.party_direct_damage_taken = []
$player.party.each_with_index do |pkmn, i|
$game_temp.party_levels_before_battle[i] = pkmn.level
$game_temp.party_critical_hits_dealt[i] = 0
$game_temp.party_direct_damage_taken[i] = 0
end
}
# Record current levels of Pokémon in party, to see if they gain a level during
# battle and may need to evolve afterwards
EventHandlers.add(:on_start_battle, :record_party_status,
proc {
$game_temp.party_levels_before_battle = []
$game_temp.party_critical_hits_dealt = []
$game_temp.party_direct_damage_taken = []
$player.party.each_with_index do |pkmn, i|
$game_temp.party_levels_before_battle[i] = pkmn.level
$game_temp.party_critical_hits_dealt[i] = 0
$game_temp.party_direct_damage_taken[i] = 0
end
}
)
def pbCanDoubleBattle?
return $PokemonGlobal.partner || $player.able_pokemon_count >= 2
@@ -230,7 +232,7 @@ def pbWildBattleCore(*args)
end
# Record information about party Pokémon to be used at the end of battle (e.g.
# comparing levels for an evolution check)
Events.onStartBattle.trigger(nil)
EventHandlers.trigger(:on_start_battle)
# Generate wild Pokémon based on the species and level
foeParty = []
sp = nil
@@ -314,8 +316,8 @@ def pbWildBattle(species, level, outcomeVar = 1, canRun = true, canLose = false)
# Potentially call a different pbWildBattle-type method instead (for roaming
# Pokémon, Safari battles, Bug Contest battles)
handled = [nil]
Events.onWildBattleOverride.trigger(nil, species, level, handled)
return handled[0] if handled[0] != nil
EventHandlers.trigger(:on_calling_wild_battle, species, level, handled)
return handled[0] if !handled[0].nil?
# Set some battle rules
setBattleRule("outcomeVar", outcomeVar) if outcomeVar != 1
setBattleRule("cannotRun") if !canRun
@@ -323,7 +325,7 @@ def pbWildBattle(species, level, outcomeVar = 1, canRun = true, canLose = false)
# Perform the battle
decision = pbWildBattleCore(species, level)
# Used by the Poké Radar to update/break the chain
Events.onWildBattleEnd.trigger(nil, species, level, decision)
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
return (decision != 2 && decision != 5)
end
@@ -375,7 +377,7 @@ def pbTrainerBattleCore(*args)
end
# Record information about party Pokémon to be used at the end of battle (e.g.
# comparing levels for an evolution check)
Events.onStartBattle.trigger(nil)
EventHandlers.trigger(:on_start_battle)
# Generate trainers and their parties based on the arguments given
foeTrainers = []
foeItems = []
@@ -394,7 +396,7 @@ def pbTrainerBattleCore(*args)
trainer = pbLoadTrainer(arg[0], arg[1], arg[2])
pbMissingTrainer(arg[0], arg[1], arg[2]) if !trainer
return 0 if !trainer
Events.onTrainerPartyLoad.trigger(nil, trainer)
EventHandlers.trigger(:on_trainer_load, trainer)
foeTrainers.push(trainer)
foePartyStarts.push(foeParty.length)
trainer.party.each { |pkmn| foeParty.push(pkmn) }
@@ -495,7 +497,7 @@ def pbTrainerBattle(trainerID, trainerName, endSpeech = nil,
trainer = pbLoadTrainer(trainerID, trainerName, trainerPartyID)
pbMissingTrainer(trainerID, trainerName, trainerPartyID) if !trainer
return false if !trainer
Events.onTrainerPartyLoad.trigger(nil, trainer)
EventHandlers.trigger(:on_trainer_load, trainer)
# If there is exactly 1 other triggered trainer event, and this trainer has
# 6 or fewer Pokémon, record this trainer for a double battle caused by the
# other triggered trainer event
@@ -580,34 +582,34 @@ def pbAfterBattle(decision, canLose)
$player.party.each { |pkmn| pkmn.heal }
(Graphics.frame_rate / 4).times { Graphics.update }
end
Events.onEndBattle.trigger(nil, decision, canLose)
EventHandlers.trigger(:on_end_battle, decision, canLose)
$game_player.straighten
end
Events.onEndBattle += proc { |_sender, e|
decision = e[0]
canLose = e[1]
# Check for evolutions
pbEvolutionCheck if Settings::CHECK_EVOLUTION_AFTER_ALL_BATTLES ||
(decision != 2 && decision != 5) # not a loss or a draw
$game_temp.party_levels_before_battle = nil
$game_temp.party_critical_hits_dealt = nil
$game_temp.party_direct_damage_taken = nil
# Check for blacking out or gaining Pickup/Huney Gather items
case decision
when 1, 4 # Win, capture
$player.pokemon_party.each do |pkmn|
pbPickup(pkmn)
pbHoneyGather(pkmn)
EventHandlers.add(:on_end_battle, :evolve_and_black_out,
proc { |decision, canLose|
# Check for evolutions
pbEvolutionCheck if Settings::CHECK_EVOLUTION_AFTER_ALL_BATTLES ||
(decision != 2 && decision != 5) # not a loss or a draw
$game_temp.party_levels_before_battle = nil
$game_temp.party_critical_hits_dealt = nil
$game_temp.party_direct_damage_taken = nil
# Check for blacking out or gaining Pickup/Huney Gather items
case decision
when 1, 4 # Win, capture
$player.pokemon_party.each do |pkmn|
pbPickup(pkmn)
pbHoneyGather(pkmn)
end
when 2, 5 # Lose, draw
if !canLose
$game_system.bgm_unpause
$game_system.bgs_unpause
pbStartOver
end
end
when 2, 5 # Lose, draw
if !canLose
$game_system.bgm_unpause
$game_system.bgs_unpause
pbStartOver
end
end
}
}
)
def pbEvolutionCheck
$player.party.each_with_index do |pkmn, i|

View File

@@ -446,7 +446,7 @@ def pbGenerateWildPokemon(species, level, isRoamer = false)
end
end
# Trigger events that may alter the generated Pokémon further
Events.onWildPokemonCreate.trigger(nil, genwildpoke)
EventHandlers.trigger(:on_wild_pokemon_created, genwildpoke)
return genwildpoke
end
@@ -455,11 +455,11 @@ end
def pbEncounter(enc_type)
$game_temp.encounter_type = enc_type
encounter1 = $PokemonEncounters.choose_wild_pokemon(enc_type)
encounter1 = EncounterModifier.trigger(encounter1)
EventHandlers.trigger(:on_wild_species_chosen, encounter1)
return false if !encounter1
if $PokemonEncounters.have_double_wild_battle?
encounter2 = $PokemonEncounters.choose_wild_pokemon(enc_type)
encounter2 = EncounterModifier.trigger(encounter2)
EventHandlers.trigger(:on_wild_species_chosen, encounter2)
return false if !encounter2
pbDoubleWildBattle(encounter1[0], encounter1[1], encounter2[0], encounter2[1])
else
@@ -467,6 +467,5 @@ def pbEncounter(enc_type)
end
$game_temp.encounter_type = nil
$game_temp.force_single_battle = false
EncounterModifier.triggerEncounterEnd
return true
end

View File

@@ -6,35 +6,36 @@
################################################################################
# Make all wild Pokémon shiny while a certain Switch is ON (see Settings).
Events.onWildPokemonCreate += proc { |_sender, e|
pkmn = e[0]
if $game_switches[Settings::SHINY_WILD_POKEMON_SWITCH]
pkmn.shiny = true
end
}
EventHandlers.add(:on_wild_pokemon_created, :make_shiny_switch,
proc { |pkmn|
pkmn.shiny = true if $game_switches[Settings::SHINY_WILD_POKEMON_SWITCH]
}
)
# Used in the random dungeon map. Makes the levels of all wild Pokémon in that
# Used in the random dungeon map. Makes the levels of all wild Pokémon in that
# map depend on the levels of Pokémon in the player's party.
# This is a simple method, and can/should be modified to account for evolutions
# and other such details. Of course, you don't HAVE to use this code.
Events.onWildPokemonCreate += proc { |_sender, e|
pkmn = e[0]
if $game_map.map_id == 51
EventHandlers.add(:on_wild_pokemon_created, :level_depends_on_party,
proc { |pkmn|
next if $game_map.map_id != 51
new_level = pbBalancedLevel($player.party) - 4 + rand(5) # For variety
new_level = new_level.clamp(1, GameData::GrowthRate.max_level)
pkmn.level = new_level
pkmn.calc_stats
pkmn.reset_moves
end
}
}
)
# This is the basis of a trainer modifier. It works both for trainers loaded
# when you battle them, and for partner trainers when they are registered.
# Note that you can only modify a partner trainer's Pokémon, and not the trainer
# themselves nor their items this way, as those are generated from scratch
# before each battle.
#Events.onTrainerPartyLoad += proc { |_sender, trainer|
# if trainer # An NPCTrainer object containing party/items/lose text, etc.
# YOUR CODE HERE
# end
#}
#EventHandlers.trigger(:on_trainer_load, :put_a_name_here,
# proc { |trainer|
# if trainer # An NPCTrainer object containing party/items/lose text, etc.
# YOUR CODE HERE
# end
# }
#)

View File

@@ -93,16 +93,17 @@ end
# When the player moves to a new map (with a different name), make all roaming
# Pokémon roam.
Events.onMapChange += proc { |_sender, e|
oldMapID = e[0]
# Get and compare map names
mapInfos = pbLoadMapInfos
next if mapInfos && oldMapID > 0 && mapInfos[oldMapID] &&
mapInfos[oldMapID].name && $game_map.name == mapInfos[oldMapID].name
# Make roaming Pokémon roam
pbRoamPokemon
$PokemonGlobal.roamedAlready = false
}
EventHandlers.add(:on_enter_map, :move_roaming_pokemon,
proc { |old_map_id|
# Get and compare map names
mapInfos = pbLoadMapInfos
next if mapInfos && old_map_id > 0 && mapInfos[old_map_id] &&
mapInfos[old_map_id].name && $game_map.name == mapInfos[old_map_id].name
# Make roaming Pokémon roam
pbRoamPokemon
$PokemonGlobal.roamedAlready = false
}
)
@@ -135,64 +136,69 @@ def pbRoamingMethodAllowed(roamer_method)
return false
end
EncounterModifier.register(proc { |encounter|
$game_temp.roamer_index_for_encounter = nil
next nil if !encounter
# Give the regular encounter if encountering a roaming Pokémon isn't possible
next encounter if $PokemonGlobal.roamedAlready
next encounter if $PokemonGlobal.partner
next encounter if $game_temp.poke_radar_data
next encounter if rand(100) < 75 # 25% chance of encountering a roaming Pokémon
# Look at each roaming Pokémon in turn and decide whether it's possible to
# encounter it
currentRegion = pbGetCurrentRegion
currentMapName = $game_map.name
possible_roamers = []
Settings::ROAMING_SPECIES.each_with_index do |data, i|
# data = [species, level, Game Switch, roamer method, battle BGM, area maps hash]
next if !GameData::Species.exists?(data[0])
next if data[2] > 0 && !$game_switches[data[2]] # Isn't roaming
next if $PokemonGlobal.roamPokemon[i] == true # Roaming Pokémon has been caught
# Get the roamer's current map
roamerMap = $PokemonGlobal.roamPosition[i]
if !roamerMap
mapIDs = pbRoamingAreas(i).keys # Hash of area patrolled by the roaming Pokémon
next if !mapIDs || mapIDs.length == 0 # No roaming area defined somehow
roamerMap = mapIDs[rand(mapIDs.length)]
$PokemonGlobal.roamPosition[i] = roamerMap
EventHandlers.add(:on_wild_species_chosen, :roaming_pokemon,
proc { |encounter|
$game_temp.roamer_index_for_encounter = nil
next if !encounter
# Give the regular encounter if encountering a roaming Pokémon isn't possible
next if $PokemonGlobal.roamedAlready
next if $PokemonGlobal.partner
next if $game_temp.poke_radar_data
next if rand(100) < 75 # 25% chance of encountering a roaming Pokémon
# Look at each roaming Pokémon in turn and decide whether it's possible to
# encounter it
currentRegion = pbGetCurrentRegion
currentMapName = $game_map.name
possible_roamers = []
Settings::ROAMING_SPECIES.each_with_index do |data, i|
# data = [species, level, Game Switch, roamer method, battle BGM, area maps hash]
next if !GameData::Species.exists?(data[0])
next if data[2] > 0 && !$game_switches[data[2]] # Isn't roaming
next if $PokemonGlobal.roamPokemon[i] == true # Roaming Pokémon has been caught
# Get the roamer's current map
roamerMap = $PokemonGlobal.roamPosition[i]
if !roamerMap
mapIDs = pbRoamingAreas(i).keys # Hash of area patrolled by the roaming Pokémon
next if !mapIDs || mapIDs.length == 0 # No roaming area defined somehow
roamerMap = mapIDs[rand(mapIDs.length)]
$PokemonGlobal.roamPosition[i] = roamerMap
end
# If roamer isn't on the current map, check if it's on a map with the same
# name and in the same region
if roamerMap != $game_map.map_id
map_metadata = GameData::MapMetadata.try_get(roamerMap)
next if !map_metadata || !map_metadata.town_map_position ||
map_metadata.town_map_position[0] != currentRegion
next if pbGetMapNameFromId(roamerMap) != currentMapName
end
# Check whether the roamer's roamer method is currently possible
next if !pbRoamingMethodAllowed(data[3])
# Add this roaming Pokémon to the list of possible roaming Pokémon to encounter
possible_roamers.push([i, data[0], data[1], data[4]]) # [i, species, level, BGM]
end
# If roamer isn't on the current map, check if it's on a map with the same
# name and in the same region
if roamerMap != $game_map.map_id
map_metadata = GameData::MapMetadata.try_get(roamerMap)
next if !map_metadata || !map_metadata.town_map_position ||
map_metadata.town_map_position[0] != currentRegion
next if pbGetMapNameFromId(roamerMap) != currentMapName
end
# Check whether the roamer's roamer method is currently possible
next if !pbRoamingMethodAllowed(data[3])
# Add this roaming Pokémon to the list of possible roaming Pokémon to encounter
possible_roamers.push([i, data[0], data[1], data[4]]) # [i, species, level, BGM]
end
# No encounterable roaming Pokémon were found, just have the regular encounter
next encounter if possible_roamers.length == 0
# Pick a roaming Pokémon to encounter out of those available
roamer = possible_roamers[rand(possible_roamers.length)]
$PokemonGlobal.roamEncounter = roamer
$game_temp.roamer_index_for_encounter = roamer[0]
$PokemonGlobal.nextBattleBGM = roamer[3] if roamer[3] && !roamer[3].empty?
$game_temp.force_single_battle = true
next [roamer[1], roamer[2]] # Species, level
})
# No encounterable roaming Pokémon were found, just have the regular encounter
next if possible_roamers.length == 0
# Pick a roaming Pokémon to encounter out of those available
roamer = possible_roamers.sample
$PokemonGlobal.roamEncounter = roamer
$game_temp.roamer_index_for_encounter = roamer[0]
$PokemonGlobal.nextBattleBGM = roamer[3] if roamer[3] && !roamer[3].empty?
$game_temp.force_single_battle = true
encounter[0] = roamer[1] # Species
encounter[1] = roamer[2] # Level
}
)
Events.onWildBattleOverride += proc { |_sender, e|
species = e[0]
level = e[1]
handled = e[2]
next if handled[0] != nil
next if !$PokemonGlobal.roamEncounter || $game_temp.roamer_index_for_encounter.nil?
handled[0] = pbRoamingPokemonBattle(species, level)
}
EventHandlers.add(:on_calling_wild_battle, :roaming_pokemon,
proc { |species, level, handled|
# handled is an array: [nil]. If [true] or [false], the battle has already
# been overridden (the boolean is its outcome), so don't do anything that
# would override it again
next if !handled[0].nil?
next if !$PokemonGlobal.roamEncounter || $game_temp.roamer_index_for_encounter.nil?
handled[0] = pbRoamingPokemonBattle(species, level)
}
)
def pbRoamingPokemonBattle(species, level)
# Get the roaming Pokémon to encounter; generate it based on the species and
@@ -214,12 +220,9 @@ def pbRoamingPokemonBattle(species, level)
end
$PokemonGlobal.roamEncounter = nil
$PokemonGlobal.roamedAlready = true
$game_temp.roamer_index_for_encounter = nil
# Used by the Poké Radar to update/break the chain
Events.onWildBattleEnd.trigger(nil, species, level, decision)
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
return (decision != 2 && decision != 5)
end
EncounterModifier.registerEncounterEnd(proc {
$game_temp.roamer_index_for_encounter = nil
})