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

@@ -134,25 +134,27 @@ ItemHandlers::UseInField.add(:MAXREPEL, proc { |item|
next pbRepel(item, 250)
})
Events.onStepTaken += proc {
next if $PokemonGlobal.repel <= 0 || $game_player.terrain_tag.ice # Shouldn't count down if on ice
$PokemonGlobal.repel -= 1
next if $PokemonGlobal.repel > 0
repels = []
GameData::Item.each { |itm| repels.push(itm.id) if itm.has_flag?("Repel") }
if repels.none? { |item| $bag.has?(item) }
pbMessage(_INTL("The repellent's effect wore off!"))
next
end
next if !pbConfirmMessage(_INTL("The repellent's effect wore off! Would you like to use another one?"))
ret = nil
pbFadeOutIn {
scene = PokemonBag_Scene.new
screen = PokemonBagScreen.new(scene, $bag)
ret = screen.pbChooseItemScreen(proc { |item| repels.include?(item) })
EventHandlers.add(:on_player_step_taken, :repel_counter,
proc {
next if $PokemonGlobal.repel <= 0 || $game_player.terrain_tag.ice # Shouldn't count down if on ice
$PokemonGlobal.repel -= 1
next if $PokemonGlobal.repel > 0
repels = []
GameData::Item.each { |itm| repels.push(itm.id) if itm.has_flag?("Repel") }
if repels.none? { |item| $bag.has?(item) }
pbMessage(_INTL("The repellent's effect wore off!"))
next
end
next if !pbConfirmMessage(_INTL("The repellent's effect wore off! Would you like to use another one?"))
ret = nil
pbFadeOutIn {
scene = PokemonBag_Scene.new
screen = PokemonBagScreen.new(scene, $bag)
ret = screen.pbChooseItemScreen(proc { |item| repels.include?(item) })
}
pbUseItem($bag, ret) if ret
}
pbUseItem($bag, ret) if ret
}
)
ItemHandlers::UseInField.add(:BLACKFLUTE, proc { |item|
pbUseItemMessage(item)

View File

@@ -131,47 +131,48 @@ end
#===============================================================================
# Phone-related counters
#===============================================================================
Events.onMapUpdate += proc { |_sender, _e|
next if !$player || !$player.has_pokegear
# Reset time to next phone call if necessary
if !$PokemonGlobal.phoneTime || $PokemonGlobal.phoneTime <= 0
$PokemonGlobal.phoneTime = 20 * 60 * Graphics.frame_rate
$PokemonGlobal.phoneTime += rand(20 * 60 * Graphics.frame_rate)
end
# Don't count down various phone times if other things are happening
$PokemonGlobal.phoneNumbers = [] if !$PokemonGlobal.phoneNumbers
next if $game_temp.in_menu || $game_temp.in_battle || $game_temp.message_window_showing
next if $game_player.move_route_forcing || pbMapInterpreterRunning?
# Count down time to next phone call
$PokemonGlobal.phoneTime -= 1
# Count down time to next can-battle for each trainer contact
if $PokemonGlobal.phoneTime % Graphics.frame_rate == 0 # Every second
$PokemonGlobal.phoneNumbers.each do |num|
next if !num[0] || num.length != 8 # if not visible or not a trainer
# Reset time to next can-battle if necessary
if num[4] == 0
num[3] = rand(20...40) * 60 # 20-40 minutes
num[4] = 1
end
# Count down time to next can-battle
num[3] -= 1
# Ready to battle
if num[3] <= 0 && num[4] == 1
num[4] = 2 # set ready-to-battle flag
pbSetReadyToBattle(num)
EventHandlers.add(:on_frame_update, :phone_call_counter,
proc {
next if !$player&.has_pokegear
# Reset time to next phone call if necessary
if !$PokemonGlobal.phoneTime || $PokemonGlobal.phoneTime <= 0
$PokemonGlobal.phoneTime = rand(20...40) * 60 * Graphics.frame_rate
end
# Don't count down various phone times if other things are happening
$PokemonGlobal.phoneNumbers = [] if !$PokemonGlobal.phoneNumbers
next if $game_temp.in_menu || $game_temp.in_battle || $game_temp.message_window_showing
next if $game_player.move_route_forcing || pbMapInterpreterRunning?
# Count down time to next phone call
$PokemonGlobal.phoneTime -= 1
# Count down time to next can-battle for each trainer contact
if $PokemonGlobal.phoneTime % Graphics.frame_rate == 0 # Every second
$PokemonGlobal.phoneNumbers.each do |num|
next if !num[0] || num.length != 8 # if not visible or not a trainer
# Reset time to next can-battle if necessary
if num[4] == 0
num[3] = rand(20...40) * 60 # 20-40 minutes
num[4] = 1
end
# Count down time to next can-battle
num[3] -= 1
# Ready to battle
if num[3] <= 0 && num[4] == 1
num[4] = 2 # set ready-to-battle flag
pbSetReadyToBattle(num)
end
end
end
end
# Time for a random phone call; generate one
if $PokemonGlobal.phoneTime <= 0
# find all trainer phone numbers
phonenum = pbRandomPhoneTrainer
if phonenum
call = pbPhoneGenerateCall(phonenum)
pbPhoneCall(call, phonenum)
# Time for a random phone call; generate one
if $PokemonGlobal.phoneTime <= 0
# find all trainer phone numbers
phonenum = pbRandomPhoneTrainer
if phonenum
call = pbPhoneGenerateCall(phonenum)
pbPhoneCall(call, phonenum)
end
end
end
}
}
)
#===============================================================================
# Player calls a contact

View File

@@ -151,90 +151,102 @@ end
################################################################################
# Event handlers
################################################################################
EncounterModifier.register(proc { |encounter|
if GameData::EncounterType.get($game_temp.encounter_type).type != :land ||
$PokemonGlobal.bicycle || $PokemonGlobal.partner
pbPokeRadarCancel
next encounter
end
ring = pbPokeRadarGetShakingGrass
if ring >= 0 # Encounter triggered by stepping into rustling grass
# Get rarity of shaking grass
rarity = 0 # 0 = rustle, 1 = vigorous rustle, 2 = shiny rustle
$game_temp.poke_radar_data[3].each { |g| rarity = g[3] if g[2] == ring }
if $game_temp.poke_radar_data[2] > 0 # Chain count, i.e. is chaining
if rarity == 2 ||
rand(100) < 58 + (ring * 10) + ([$game_temp.poke_radar_data[2], 40].min / 4) + ($game_temp.poke_radar_data[4] ? 10 : 0)
# Continue the chain
encounter = [$game_temp.poke_radar_data[0], $game_temp.poke_radar_data[1]]
$game_temp.force_single_battle = true
else
# Break the chain, force an encounter with a different species
100.times do
break if encounter && encounter[0] != $game_temp.poke_radar_data[0]
encounter = $PokemonEncounters.choose_wild_pokemon($PokemonEncounters.encounter_type)
end
if encounter[0] == $game_temp.poke_radar_data[0] && encounter[1] == $game_temp.poke_radar_data[1]
# Chain couldn't be broken somehow; continue it after all
EventHandlers.add(:on_wild_species_chosen, :poke_radar_chain,
proc { |encounter|
if GameData::EncounterType.get($game_temp.encounter_type).type != :land ||
$PokemonGlobal.bicycle || $PokemonGlobal.partner
pbPokeRadarCancel
next
end
ring = pbPokeRadarGetShakingGrass
if ring >= 0 # Encounter triggered by stepping into rustling grass
# Get rarity of shaking grass
rarity = 0 # 0 = rustle, 1 = vigorous rustle, 2 = shiny rustle
$game_temp.poke_radar_data[3].each { |g| rarity = g[3] if g[2] == ring }
if $game_temp.poke_radar_data[2] > 0 # Chain count, i.e. is chaining
chain_chance = 58 + (ring * 10)
chain_chance += [$game_temp.poke_radar_data[2], 40].min / 4 # Chain length
chain_chance += 10 if $game_temp.poke_radar_data[4] # Previous in chain was caught
if rarity == 2 || rand(100) < chain_chance
# Continue the chain
encounter[0] = $game_temp.poke_radar_data[0] # Species
encounter[1] = $game_temp.poke_radar_data[1] # Level
$game_temp.force_single_battle = true
else
pbPokeRadarCancel
# Break the chain, force an encounter with a different species
100.times do
break if encounter && encounter[0] != $game_temp.poke_radar_data[0]
new_encounter = $PokemonEncounters.choose_wild_pokemon($PokemonEncounters.encounter_type)
encounter[0] = new_encounter[0]
encounter[1] = new_encounter[1]
end
if encounter[0] == $game_temp.poke_radar_data[0] && encounter[1] == $game_temp.poke_radar_data[1]
# Chain couldn't be broken somehow; continue it after all
$game_temp.force_single_battle = true
else
pbPokeRadarCancel
end
end
else # Not chaining; will start one
# Force random wild encounter, vigorous shaking means rarer species
new_encounter = pbPokeRadarGetEncounter(rarity)
encounter[0] = new_encounter[0]
encounter[1] = new_encounter[1]
$game_temp.force_single_battle = true
end
else # Not chaining; will start one
# Force random wild encounter, vigorous shaking means rarer species
encounter = pbPokeRadarGetEncounter(rarity)
$game_temp.force_single_battle = true
elsif encounter # Encounter triggered by stepping in non-rustling grass
pbPokeRadarCancel
end
elsif encounter # Encounter triggered by stepping in non-rustling grass
}
)
EventHandlers.add(:on_wild_pokemon_created, :poke_radar_shiny,
proc { |pkmn|
next if !$game_temp.poke_radar_data
grasses = $game_temp.poke_radar_data[3]
next if !grasses
grasses.each do |grass|
next if $game_player.x != grass[0] || $game_player.y != grass[1]
pkmn.shiny = true if grass[3] == 2
break
end
}
)
EventHandlers.add(:on_wild_battle_end, :poke_radar_continue_chain,
proc { |species, level, decision|
if $game_temp.poke_radar_data && [1, 4].include?(decision) # Defeated/caught
$game_temp.poke_radar_data[0] = species
$game_temp.poke_radar_data[1] = level
$game_temp.poke_radar_data[2] += 1
$stats.poke_radar_longest_chain = [$game_temp.poke_radar_data[2], $stats.poke_radar_longest_chain].max
# Catching makes the next Radar encounter more likely to continue the chain
$game_temp.poke_radar_data[4] = (decision == 4)
pbPokeRadarHighlightGrass(false)
else
pbPokeRadarCancel
end
}
)
EventHandlers.add(:on_player_step_taken, :poke_radar,
proc {
if $PokemonGlobal.pokeradarBattery && $PokemonGlobal.pokeradarBattery > 0 &&
!$game_temp.poke_radar_data
$PokemonGlobal.pokeradarBattery -= 1
end
terrain = $game_map.terrain_tag($game_player.x, $game_player.y)
if !terrain.land_wild_encounters || !terrain.shows_grass_rustle
pbPokeRadarCancel
end
}
)
EventHandlers.add(:on_enter_map, :cancel_poke_radar,
proc { |_old_map_id|
pbPokeRadarCancel
end
next encounter
})
Events.onWildPokemonCreate += proc { |_sender, e|
pokemon = e[0]
next if !$game_temp.poke_radar_data
grasses = $game_temp.poke_radar_data[3]
next if !grasses
grasses.each do |grass|
next if $game_player.x != grass[0] || $game_player.y != grass[1]
pokemon.shiny = true if grass[3] == 2
break
end
}
Events.onWildBattleEnd += proc { |_sender, e|
species = e[0]
level = e[1]
decision = e[2]
if $game_temp.poke_radar_data && [1, 4].include?(decision) # Defeated/caught
$game_temp.poke_radar_data[0] = species
$game_temp.poke_radar_data[1] = level
$game_temp.poke_radar_data[2] += 1
$stats.poke_radar_longest_chain = [$game_temp.poke_radar_data[2], $stats.poke_radar_longest_chain].max
# Catching makes the next Radar encounter more likely to continue the chain
$game_temp.poke_radar_data[4] = (decision == 4)
pbPokeRadarHighlightGrass(false)
else
pbPokeRadarCancel
end
}
Events.onStepTaken += proc { |_sender, _e|
if $PokemonGlobal.pokeradarBattery && $PokemonGlobal.pokeradarBattery > 0 &&
!$game_temp.poke_radar_data
$PokemonGlobal.pokeradarBattery -= 1
end
terrain = $game_map.terrain_tag($game_player.x, $game_player.y)
if !terrain.land_wild_encounters || !terrain.shows_grass_rustle
pbPokeRadarCancel
end
}
Events.onMapChange += proc { |_sender, _e|
pbPokeRadarCancel
}
}
)
################################################################################
# Item handlers