class BugContestState
attr_accessor :ballcount
attr_accessor :decision
attr_accessor :steps
attr_accessor :lastPokemon
attr_reader :timer
ContestantNames=[
_INTL("BUG CATCHER BENNY"),
_INTL("COOLTRAINER NICK"),
_INTL("COLLECTOR KIPP"),
_INTL("YOUNGSTER SAMUEL"),
_INTL("BUG CATCHER DON"),
_INTL("PICNICKER CINDY"),
_INTL("CAMPER BARRY"),
_INTL("BUG CATCHER JOSH")
]
TimerSeconds=1200
def initialize
clear
@lastContest=nil
end
def pbContestHeld?
return false if !@lastContest
return Time.new.to_i-@lastContest<86400
end
def expired?
return false if !undecided?
curtime=@timer+TimerSeconds*Graphics.frame_rate
curtime=[curtime-Graphics.frame_count,0].max
return (curtime<=0)
end
def clear
@ballcount=0
@ended=false
@inProgress=false
@decision=0
@encounterMap=0
@lastPokemon=nil
@otherparty=[]
@contestants=[]
@places=[]
@start=nil
@reception=[]
end
def undecided?
return (@inProgress && @decision==0)
end
def decided?
return (@inProgress && @decision!=0) || @ended
end
def pbSetPokemon(chosenpoke)
@chosenPokemon=chosenpoke
end
# Reception map is handled separately from contest map
# since the reception map can be outdoors, with its own
# grassy patches.
def pbSetReception(*arg)
@reception=[]
for i in arg
@reception.push(i)
end
end
def pbOffLimits?(map)
return false if map==@contestMap
for i in @reception
return false if map==i
end
return true
end
def pbSetJudgingPoint(startMap,startX,startY)
@start=[startMap,startX,startY,6]
end
def pbSetContestMap(map)
@contestMap=map
end
def pbJudge
judgearray=[]
if @lastPokemon
judgearray.push([-1,@lastPokemon.species,
pbBugContestScore(@lastPokemon)])
end
@contestants=[]
[5,ContestantNames.length].min.times do
loop do
value=rand(ContestantNames.length)
if [email protected]?{|i| i==value }
@contestants.push(value)
break
end
end
end
enctype=EncounterTypes::BugContest
if !$PokemonEncounters.pbMapHasEncounter?(@contestMap,enctype)
enctype=EncounterTypes::Land
end
for cont in @contestants
enc=$PokemonEncounters.pbMapEncounter(@contestMap,enctype)
if !enc
raise _INTL("No encounters, so can't judge contest")
end
pokemon=PokeBattle_Pokemon.new(enc[0],enc[1],$Trainer)
score=pbBugContestScore(pokemon)
judgearray.push([cont,pokemon.species,score])
end
if judgearray.length<3
raise _INTL("Too few bug catching contestants")
end
judgearray.sort!{|a,b| b[2]<=>a[2]} # sort by score in descending order
@places.push(judgearray[0])
@places.push(judgearray[1])
@places.push(judgearray[2])
end
def pbGetPlaceInfo(place)
cont=@places[place][0]
if cont<0
$game_variables[1]=$Trainer.name
else
$game_variables[1]=ContestantNames[cont]
end
$game_variables[2]=PBSpecies.getName(@places[place][1])
$game_variables[3]=@places[place][2]
end
def pbClearIfEnded
if !@inProgress
clear unless @start && @start[0]==$game_map.map_id
end
end
def pbStartJudging
@decision=1
pbJudge
if $scene.is_a?(Scene_Map)
pbFadeOutIn(99999){
$game_temp.player_transferring = true
$game_temp.player_new_map_id = @start[0]
$game_temp.player_new_x = @start[1]
$game_temp.player_new_y = @start[2]
$game_temp.player_new_direction = @start[3]
$MapFactory.setup(@start[0])
$scene.transfer_player
}
end
end
def pbIsContestant?(i)
return @contestants.any?{|item| i==item }
end
def pbStart(ballcount)
@ballcount=ballcount
@inProgress=true
@otherparty=[]
@lastPokemon=nil
@lastContest=nil
@timer=Graphics.frame_count
@places=[]
chosenpkmn=$Trainer.party[@chosenPokemon]
for i in 0...$Trainer.party.length
if i!=@chosenPokemon
@otherparty.push($Trainer.party[i])
end
end
@contestants=[]
[5,ContestantNames.length].min.times do
loop do
value=rand(ContestantNames.length)
if [email protected]?{|i| i==value }
@contestants.push(value)
break
end
end
end
$Trainer.party=[chosenpkmn]
@decision=0
@ended=false
end
def place
for i in 0...3
return i if @places[i][0]<0
end
return 3
end
def pbEnd(interrupted=false)
return if !@inProgress
for poke in @otherparty
$Trainer.party.push(poke)
end
if !interrupted
if @lastPokemon
pbNicknameAndStore(@lastPokemon)
end
@ended=true
else
@ended=false
end
@lastPokemon=nil
@otherparty=[]
@reception=[]
@ballcount=0
@inProgress=false
@decision=0
@lastContest=Time.now.to_i
$game_map.need_refresh=true
end
end
def pbBugContestScore(pokemon)
a=0
a+=16 if (pokemon.iv[2]&1)>0
a+=8 if (pokemon.iv[1]&1)>0
a+=4 if (pokemon.iv[4]&1)>0
a+=1 if (pokemon.iv[3]&1)>0
score=(pokemon.totalhp*8)
score+=pokemon.attack
score+=pokemon.defense
score+=pokemon.speed
score+=pokemon.spatk
score+=pokemon.spdef
score+=a+(pokemon.hp/8)+1
return score
end
def pbBugContestState
if !$PokemonGlobal.bugContestState
$PokemonGlobal.bugContestState=BugContestState.new
end
return $PokemonGlobal.bugContestState
end
def pbInBugContest?
return pbBugContestState.undecided?
end
def pbBugContestUndecided?
return pbBugContestState.undecided?
end
def pbBugContestDecided?
return pbBugContestState.decided?
end
class PokeBattle_BugContestBattle < PokeBattle_Battle
attr_accessor :ballcount
def initialize(*arg)
@ballcount=0
super(*arg)
end
def pbItemMenu(i)
@ballcount-=1 if @ballcount>0
return PBItems::SAFARIBALL
end
def pbCommandMenu(i)
return @scene.pbCommandMenuEx(i,[
_INTL("Park Balls: {1}",@ballcount),
_INTL("FIGHT"),
_INTL("POKéMON"),
_INTL("BALL"),
_INTL("RUN")
])
end
def pbStorePokemon(pokemon)
if pbBugContestState.lastPokemon
lastPokemon=pbBugContestState.lastPokemon
pbDisplayPaused(_INTL("You already caught a {1}.",lastPokemon.name))
helptext=_INTL("STOCK POKéMON:\n {1} Lv{2} MaxHP: {3}\nTHIS POKéMON:\n {4} Lv{5} MaxHP: {6}",
lastPokemon.name,lastPokemon.level,lastPokemon.totalhp,
pokemon.name,pokemon.level,pokemon.totalhp
)
@scene.pbShowHelp(helptext)
if pbDisplayConfirm(_INTL("Switch Pokémon?"))
pbBugContestState.lastPokemon=pokemon
@scene.pbHideHelp
else
@scene.pbHideHelp
return
end
else
pbBugContestState.lastPokemon=pokemon
end
pbDisplay(_INTL("Caught {1}!",pokemon.name))
end
def pbEndOfRoundPhase
super
if @ballcount<=0 && @decision==0
@decision=3
end
end
end
class TimerDisplay
def initialize(start,maxtime)
@timer=Window_AdvancedTextPokemon.newWithSize("",360,0,120,64)
@timer.z=99999
@total_sec=nil
@start=start
@maxtime=maxtime
end
def dispose
@timer.dispose
end
def disposed?
@timer.disposed?
end
def update
curtime=[(@start+@maxtime)-Graphics.frame_count,0].max
curtime/=Graphics.frame_rate
if curtime != @total_sec
# Calculate total number of seconds
@total_sec = curtime
# Make a string for displaying the timer
min = @total_sec / 60
sec = @total_sec % 60
@timer.text = _ISPRINTF("<ac>{1:02d}:{2:02d}", min, sec)
end
end
end
Events.onMapChange+=proc{|sender,e|
pbBugContestState.pbClearIfEnded
}
Events.onMapSceneChange+=proc{|sender,e|
scene=e[0]
mapChanged=e[1]
if pbInBugContest? && pbBugContestState.decision==0
scene.spriteset.addUserSprite(TimerDisplay.new(
pbBugContestState.timer,
BugContestState::TimerSeconds*Graphics.frame_rate))
end
}
Events.onMapUpdate+=proc{|sender,e|
if !$Trainer || !$PokemonGlobal || !$game_player ||
!$game_map
# do nothing
elsif !$game_player.move_route_forcing &&
!pbMapInterpreterRunning? &&
!$game_temp.message_window_showing
if pbBugContestState.expired?
$game_system.se_play("endofsafari.wav")
Kernel.pbMessage("ANNOUNCER: Ding-dong!")
Kernel.pbMessage("Time's up!")
pbBugContestState.pbStartJudging
end
end
}
Events.onMapChanging+=proc{|sender,e|
newmapID=e[0]
newmap=e[1]
if pbInBugContest?
if pbBugContestState.pbOffLimits?(newmapID)
# Clear bug contest if player flies/warps/teleports
# out of the contest
pbBugContestState.pbEnd(true)
end
end
}
def Kernel.pbBugContestStartOver
for i in $Trainer.party; i.heal; end
pbBugContestState.pbStartJudging
end
Events.onWildBattleOverride+= proc { |sender,e|
species=e[0]
level=e[1]
handled=e[2]
next if handled[0]!=nil
next if !pbInBugContest?
handled[0]=pbBugContestBattle(species,level)
}
def pbBugContestBattle(species,level)
if (Input.press?(Input::CTRL) && $DEBUG) || $Trainer.pokemonCount==0
if $Trainer.pokemonCount>0
Kernel.pbMessage(_INTL("SKIPPING BATTLE..."))
end
$game_variables[variable]=1 if variable
$PokemonGlobal.nextBattleBGM=nil
$PokemonGlobal.nextBattleME=nil
$PokemonGlobal.nextBattleBack=nil
return true
end
currentlevels=[]
for i in $Trainer.party
currentlevels.push(i.level)
end
genwildpoke=pbGenerateWildPokemon(species,level)
pokerus=(genwildpoke.pokerus==1)
scene=PokeBattle_Scene.new
battle=PokeBattle_BugContestBattle.new(scene,$Trainer.party,[genwildpoke],$Trainer,nil)
battle.ballcount=pbBugContestState.ballcount
battle.internalbattle=true
wildbgm=pbGetWildBattleBGM(species)
playingBGS=$game_system.getPlayingBGS
playingBGM=$game_system.getPlayingBGM
$game_system.bgm_pause
$game_system.bgs_pause
restorebgm=true
decision=0
pbBattleAnimation(wildbgm) {
decision=battle.pbStartBattle
if decision==2
$game_system.bgm_unpause
$game_system.bgs_unpause
Kernel.pbBugContestStartOver
restorebgm=false
else
pbEvolutionCheck(currentlevels)
if decision==1
for pkmn in $Trainer.party
Kernel.pbPickup(pkmn)
end
end
pbPokerusCheck if pokerus
end
}
if restorebgm
$game_system.bgm_resume(playingBGM)
$game_system.bgs_resume(playingBGS)
end
$PokemonGlobal.nextBattleBGM=nil
$PokemonGlobal.nextBattleME=nil
$PokemonGlobal.nextBattleBack=nil
pbBugContestState.ballcount=battle.ballcount
$PokemonEncounters.clearStepCount
Input.update
Events.onWildBattleEnd.trigger(nil,species,level,decision)
if pbBugContestState.ballcount==0
Kernel.pbMessage("ANNOUNCER: The Bug-Catching Contest is over!")
$game_system.se_play("endofsafari.wav")
pbBugContestState.pbStartJudging
end
return (decision!=2)
end