Files
infinitefusion-e18/Data/Scripts/020_System and utilities/003_PSystem_FileUtilities.rb

522 lines
18 KiB
Ruby

#===============================================================================
# Load Pokémon sprites
#===============================================================================
def pbLoadPokemonBitmap(pokemon,back=false)
return pbLoadPokemonBitmapSpecies(pokemon,pokemon.species,back)
end
# NOTE: Returns an AnimatedBitmap, not a Bitmap
def pbLoadPokemonBitmapSpecies(pokemon,species,back=false)
ret = nil
if pokemon.egg?
bitmapFileName = sprintf("Graphics/Battlers/%segg_%d",getConstantName(PBSpecies,species),pokemon.form) rescue nil
if !pbResolveBitmap(bitmapFileName)
bitmapFileName = sprintf("Graphics/Battlers/%03degg_%d",species,pokemon.form)
if !pbResolveBitmap(bitmapFileName)
bitmapFileName = sprintf("Graphics/Battlers/%segg",getConstantName(PBSpecies,species)) rescue nil
if !pbResolveBitmap(bitmapFileName)
bitmapFileName = sprintf("Graphics/Battlers/%03degg",species)
if !pbResolveBitmap(bitmapFileName)
bitmapFileName = sprintf("Graphics/Battlers/egg")
end
end
end
end
bitmapFileName = pbResolveBitmap(bitmapFileName)
else
bitmapFileName = pbCheckPokemonBitmapFiles([species,back,(pokemon.female?),
pokemon.shiny?,(pokemon.form rescue 0),pokemon.shadowPokemon?])
# Alter bitmap if supported
alterBitmap = (MultipleForms.getFunction(species,"alterBitmap") rescue nil)
end
if bitmapFileName && alterBitmap
animatedBitmap = AnimatedBitmap.new(bitmapFileName)
copiedBitmap = animatedBitmap.copy
animatedBitmap.dispose
copiedBitmap.each { |bitmap| alterBitmap.call(pokemon,bitmap) }
ret = copiedBitmap
elsif bitmapFileName
ret = AnimatedBitmap.new(bitmapFileName)
end
return ret
end
# NOTE: Returns an AnimatedBitmap, not a Bitmap
def pbLoadSpeciesBitmap(species,female=false,form=0,shiny=false,shadow=false,back=false,egg=false)
ret = nil
if egg
bitmapFileName = sprintf("Graphics/Battlers/%segg_%d",getConstantName(PBSpecies,species),form) rescue nil
if !pbResolveBitmap(bitmapFileName)
bitmapFileName = sprintf("Graphics/Battlers/%03degg_%d",species,form)
if !pbResolveBitmap(bitmapFileName)
bitmapFileName = sprintf("Graphics/Battlers/%segg",getConstantName(PBSpecies,species)) rescue nil
if !pbResolveBitmap(bitmapFileName)
bitmapFileName = sprintf("Graphics/Battlers/%03degg",species)
if !pbResolveBitmap(bitmapFileName)
bitmapFileName = sprintf("Graphics/Battlers/egg")
end
end
end
end
bitmapFileName = pbResolveBitmap(bitmapFileName)
else
bitmapFileName = pbCheckPokemonBitmapFiles([species,back,female,shiny,form,shadow])
end
if bitmapFileName
ret = AnimatedBitmap.new(bitmapFileName)
end
return ret
end
def pbCheckPokemonBitmapFiles(params)
factors = []
factors.push([5,params[5],false]) if params[5] && params[5]!=false # shadow
factors.push([2,params[2],false]) if params[2] && params[2]!=false # gender
factors.push([3,params[3],false]) if params[3] && params[3]!=false # shiny
factors.push([4,params[4],0]) if params[4] && params[4]!=0 # form
factors.push([0,params[0],0]) # species
trySpecies = 0
tryGender = false
tryShiny = false
tryBack = params[1]
tryForm = 0
tryShadow = false
for i in 0...2**factors.length
factors.each_with_index do |factor,index|
newVal = ((i/(2**index))%2==0) ? factor[1] : factor[2]
case factor[0]
when 0 then trySpecies = newVal
when 2 then tryGender = newVal
when 3 then tryShiny = newVal
when 4 then tryForm = newVal
when 5 then tryShadow = newVal
end
end
for j in 0...2 # Try using the species' internal name and then its ID number
next if trySpecies==0 && j==0
trySpeciesText = (j==0) ? getConstantName(PBSpecies,trySpecies) : sprintf("%03d",trySpecies)
bitmapFileName = sprintf("Graphics/Battlers/%s%s%s%s%s%s",
trySpeciesText,
(tryGender) ? "f" : "",
(tryShiny) ? "s" : "",
(tryBack) ? "b" : "",
(tryForm!=0) ? "_"+tryForm.to_s : "",
(tryShadow) ? "_shadow" : "") rescue nil
ret = pbResolveBitmap(bitmapFileName)
return ret if ret
end
end
return nil
end
def pbLoadPokemonShadowBitmap(pokemon)
bitmapFileName = pbCheckPokemonShadowBitmapFiles(pokemon.species,pokemon.form)
return AnimatedBitmap.new(pbResolveBitmap(bitmapFileName)) if bitmapFileName
return nil
end
def pbLoadPokemonShadowBitmapSpecies(pokemon,species)
bitmapFileName = pbCheckPokemonShadowBitmapFiles(species,pokemon.form)
return AnimatedBitmap.new(pbResolveBitmap(bitmapFileName)) if bitmapFileName
return nil
end
def pbCheckPokemonShadowBitmapFiles(species,form,fullmetrics=nil)
if form>0
bitmapFileName = sprintf("Graphics/Battlers/%s_%d_battleshadow",getConstantName(PBSpecies,species),form) rescue nil
ret = pbResolveBitmap(bitmapFileName)
return bitmapFileName if ret
bitmapFileName = sprintf("Graphics/Battlers/%03d_%d_battleshadow",species,form)
ret = pbResolveBitmap(bitmapFileName)
return bitmapFileName if ret
end
bitmapFileName = sprintf("Graphics/Battlers/%s_battleshadow",getConstantName(PBSpecies,species)) rescue nil
ret = pbResolveBitmap(bitmapFileName)
return bitmapFileName if ret
bitmapFileName = sprintf("Graphics/Battlers/%03d_battleshadow",species)
ret = pbResolveBitmap(bitmapFileName)
return bitmapFileName if ret
# Load metrics and use that graphic
fullmetrics = pbLoadSpeciesMetrics if !fullmetrics
size = (fullmetrics[SpeciesData::METRIC_SHADOW_SIZE][pbGetFSpeciesFromForm(species,form)] || 2)
bitmapFileName = sprintf("Graphics/Pictures/Battle/battler_shadow_%d",size)
return bitmapFileName if pbResolveBitmap(bitmapFileName)
return nil
end
#===============================================================================
# Load Pokémon icons
#===============================================================================
def pbLoadPokemonIcon(pokemon)
return AnimatedBitmap.new(pbPokemonIconFile(pokemon)).deanimate
end
def pbPokemonIconFile(pokemon)
return pbCheckPokemonIconFiles([pokemon.species,pokemon.female?,
pokemon.shiny?,(pokemon.form rescue 0),pokemon.shadowPokemon?],
pokemon.egg?)
end
def pbCheckPokemonIconFiles(params,egg=false)
species = params[0]
if egg
bitmapFileName = sprintf("Graphics/Icons/icon%segg_%d",getConstantName(PBSpecies,species),params[3]) rescue nil
if !pbResolveBitmap(bitmapFileName)
bitmapFileName = sprintf("Graphics/Icons/icon%03degg_%d",species,params[3])
if !pbResolveBitmap(bitmapFileName)
bitmapFileName = sprintf("Graphics/Icons/icon%segg",getConstantName(PBSpecies,species)) rescue nil
if !pbResolveBitmap(bitmapFileName)
bitmapFileName = sprintf("Graphics/Icons/icon%03degg",species)
if !pbResolveBitmap(bitmapFileName)
bitmapFileName = sprintf("Graphics/Icons/iconEgg")
end
end
end
end
return pbResolveBitmap(bitmapFileName)
end
factors = []
factors.push([4,params[4],false]) if params[4] && params[4]!=false # shadow
factors.push([1,params[1],false]) if params[1] && params[1]!=false # gender
factors.push([2,params[2],false]) if params[2] && params[2]!=false # shiny
factors.push([3,params[3],0]) if params[3] && params[3]!=0 # form
factors.push([0,params[0],0]) # species
trySpecies = 0
tryGender = false
tryShiny = false
tryForm = 0
tryShadow = false
for i in 0...2**factors.length
factors.each_with_index do |factor,index|
newVal = ((i/(2**index))%2==0) ? factor[1] : factor[2]
case factor[0]
when 0 then trySpecies = newVal
when 1 then tryGender = newVal
when 2 then tryShiny = newVal
when 3 then tryForm = newVal
when 4 then tryShadow = newVal
end
end
for j in 0...2 # Try using the species' internal name and then its ID number
next if trySpecies==0 && j==0
trySpeciesText = (j==0) ? getConstantName(PBSpecies,trySpecies) : sprintf("%03d",trySpecies)
bitmapFileName = sprintf("Graphics/Icons/icon%s%s%s%s%s",
trySpeciesText,
(tryGender) ? "f" : "",
(tryShiny) ? "s" : "",
(tryForm!=0) ? "_"+tryForm.to_s : "",
(tryShadow) ? "_shadow" : "") rescue nil
ret = pbResolveBitmap(bitmapFileName)
return ret if ret
end
end
return nil
end
#===============================================================================
# Load Pokémon footprint graphics
#===============================================================================
def pbPokemonFootprintFile(pokemon,form=0) # Used by the Pokédex
return nil if !pokemon
if pokemon.is_a?(Numeric)
bitmapFileName = sprintf("Graphics/Icons/Footprints/footprint%s_%d",
getConstantName(PBSpecies,pokemon),form) rescue nil
if !pbResolveBitmap(bitmapFileName)
bitmapFileName = sprintf("Graphics/Icons/Footprints/footprint%03d_%d",
pokemon,form) rescue nil
if !pbResolveBitmap(bitmapFileName)
bitmapFileName = sprintf("Graphics/Icons/Footprints/footprint%s",
getConstantName(PBSpecies,pokemon)) rescue nil
if !pbResolveBitmap(bitmapFileName)
bitmapFileName = sprintf("Graphics/Icons/Footprints/footprint%03d",pokemon)
end
end
end
else
bitmapFileName = sprintf("Graphics/Icons/Footprints/footprint%s_%d",
getConstantName(PBSpecies,pokemon.species),(pokemon.form rescue 0)) rescue nil
if !pbResolveBitmap(bitmapFileName)
bitmapFileName = sprintf("Graphics/Icons/Footprints/footprint%03d_%d",
pokemon.species,(pokemon.form rescue 0)) rescue nil
if !pbResolveBitmap(bitmapFileName)
bitmapFileName = sprintf("Graphics/Icons/Footprints/footprint%s",
getConstantName(PBSpecies,pokemon.species)) rescue nil
if !pbResolveBitmap(bitmapFileName)
bitmapFileName = sprintf("Graphics/Icons/Footprints/footprint%03d",
pokemon.species)
end
end
end
end
return pbResolveBitmap(bitmapFileName)
end
#===============================================================================
# Analyse audio files
#===============================================================================
def pbResolveAudioSE(file)
return nil if !file
if RTP.exists?("Audio/SE/"+file,["",".wav",".mp3",".ogg"])
return RTP.getPath("Audio/SE/"+file,["",".wav",".mp3",".ogg"])
end
return nil
end
def pbCryFrameLength(pokemon,form=0,pitch=nil)
return 0 if !pokemon
pitch = 100 if !pitch
pitch = pitch.to_f/100
return 0 if pitch<=0
playtime = 0.0
if pokemon.is_a?(Numeric)
pkmnwav = pbResolveAudioSE(pbCryFile(pokemon,form))
playtime = getPlayTime(pkmnwav) if pkmnwav
elsif !pokemon.egg?
if pokemon.respond_to?("chatter") && pokemon.chatter
playtime = pokemon.chatter.time
pitch = 1.0
else
pkmnwav = pbResolveAudioSE(pbCryFile(pokemon))
playtime = getPlayTime(pkmnwav) if pkmnwav
end
end
playtime /= pitch # sound is lengthened the lower the pitch
# 4 is added to provide a buffer between sounds
return (playtime*Graphics.frame_rate).ceil+4
end
#===============================================================================
# Load/play Pokémon cry files
#===============================================================================
def pbPlayCry(pokemon,volume=90,pitch=nil)
return if !pokemon
if pokemon.is_a?(Numeric) || pokemon.is_a?(String) || pokemon.is_a?(Symbol)
pbPlayCrySpecies(pokemon,0,volume,pitch)
elsif pokemon.is_a?(Pokemon)
pbPlayCryPokemon(pokemon,volume,pitch)
end
end
def pbPlayCrySpecies(pokemon,form=0,volume=90,pitch=nil)
return if !pokemon
pokemon = getID(PBSpecies,pokemon)
return if !pokemon.is_a?(Numeric)
pkmnwav = pbCryFile(pokemon,form)
if pkmnwav
pitch ||= 100
pbSEPlay(RPG::AudioFile.new(pkmnwav,volume,pitch)) rescue nil
end
end
def pbPlayCryPokemon(pokemon,volume=90,pitch=nil)
return if !pokemon || pokemon.egg?
if pokemon.respond_to?("chatter") && pokemon.chatter
pokemon.chatter.play
return
end
pkmnwav = pbCryFile(pokemon)
if pkmnwav
pitch ||= (pokemon.hp*25/pokemon.totalhp)+75
pbSEPlay(RPG::AudioFile.new(pkmnwav,volume,pitch)) rescue nil
end
end
def pbCryFile(pokemon,form=0)
return nil if !pokemon
pokemon = getID(PBSpecies,pokemon)
if pokemon.is_a?(Numeric)
filename = sprintf("Cries/%sCry_%d",getConstantName(PBSpecies,pokemon),form) rescue nil
if !pbResolveAudioSE(filename)
filename = sprintf("Cries/%03dCry_%d",pokemon,form)
if !pbResolveAudioSE(filename)
filename = sprintf("Cries/%sCry",getConstantName(PBSpecies,pokemon)) rescue nil
if !pbResolveAudioSE(filename)
filename = sprintf("Cries/%03dCry",pokemon)
end
end
end
return filename if pbResolveAudioSE(filename)
elsif !pokemon.egg?
form = (pokemon.form rescue 0)
filename = sprintf("Cries/%sCry_%d",getConstantName(PBSpecies,pokemon.species),form) rescue nil
if !pbResolveAudioSE(filename)
filename = sprintf("Cries/%03dCry_%d",pokemon.species,form)
if !pbResolveAudioSE(filename)
filename = sprintf("Cries/%sCry",getConstantName(PBSpecies,pokemon.species)) rescue nil
if !pbResolveAudioSE(filename)
filename = sprintf("Cries/%03dCry",pokemon.species)
end
end
end
return filename if pbResolveAudioSE(filename)
end
return nil
end
#===============================================================================
# Load various wild battle music
#===============================================================================
def pbGetWildBattleBGM(_wildParty) # wildParty is an array of Pokémon objects
if $PokemonGlobal.nextBattleBGM
return $PokemonGlobal.nextBattleBGM.clone
end
ret = nil
if !ret
# Check map metadata
music = GameData::MapMetadata.get($game_map.map_id).wild_battle_BGM
ret = pbStringToAudioFile(music) if music && music!=""
end
if !ret
# Check global metadata
music = GameData::Metadata.get.wild_battle_BGM
ret = pbStringToAudioFile(music) if music && music!=""
end
ret = pbStringToAudioFile("Battle wild") if !ret
return ret
end
def pbGetWildVictoryME
if $PokemonGlobal.nextBattleME
return $PokemonGlobal.nextBattleME.clone
end
ret = nil
if !ret
# Check map metadata
music = GameData::MapMetadata.get($game_map.map_id).wild_victory_ME
ret = pbStringToAudioFile(music) if music && music!=""
end
if !ret
# Check global metadata
music = GameData::Metadata.get.wild_victory_ME
ret = pbStringToAudioFile(music) if music && music!=""
end
ret = pbStringToAudioFile("Battle victory") if !ret
ret.name = "../../Audio/ME/"+ret.name
return ret
end
def pbGetWildCaptureME
if $PokemonGlobal.nextBattleCaptureME
return $PokemonGlobal.nextBattleCaptureME.clone
end
ret = nil
if !ret
# Check map metadata
music = GameData::MapMetadata.get($game_map.map_id).wild_capture_ME
ret = pbStringToAudioFile(music) if music && music!=""
end
if !ret
# Check global metadata
music = GameData::Metadata.get.wild_capture_ME
ret = pbStringToAudioFile(music) if music && music!=""
end
ret = pbStringToAudioFile("Battle capture success") if !ret
ret.name = "../../Audio/ME/"+ret.name
return ret
end
#===============================================================================
# Load/play various trainer battle music
#===============================================================================
def pbPlayTrainerIntroME(trainer_type)
trainer_type_data = GameData::TrainerType.get(trainer_type)
return if !trainer_type_data.intro_ME || trainer_type_data.intro_ME == ""
bgm = pbStringToAudioFile(trainer_type_data.intro_ME)
pbMEPlay(bgm)
end
def pbGetTrainerBattleBGM(trainer) # can be a PokeBattle_Trainer or an array of them
if $PokemonGlobal.nextBattleBGM
return $PokemonGlobal.nextBattleBGM.clone
end
ret = nil
music = nil
trainerarray = (trainer.is_a?(Array)) ? trainer : [trainer]
trainerarray.each do |t|
trainer_type_data = GameData::TrainerType.get(t.trainertype)
music = trainer_type_data.battle_BGM if trainer_type_data.battle_BGM
end
ret = pbStringToAudioFile(music) if music && music!=""
if !ret
# Check map metadata
music = GameData::MapMetadata.get($game_map.map_id).trainer_battle_BGM
if music && music!=""
ret = pbStringToAudioFile(music)
end
end
if !ret
# Check global metadata
music = GameData::Metadata.get.trainer_battle_BGM
if music && music!=""
ret = pbStringToAudioFile(music)
end
end
ret = pbStringToAudioFile("Battle trainer") if !ret
return ret
end
def pbGetTrainerBattleBGMFromType(trainertype)
if $PokemonGlobal.nextBattleBGM
return $PokemonGlobal.nextBattleBGM.clone
end
trainer_type_data = GameData::TrainerType.get(trainertype)
ret = trainer_type_data.battle_BGM if trainer_type_data.battle_BGM
if !ret
# Check map metadata
music = GameData::MapMetadata.get($game_map.map_id).trainer_battle_BGM
ret = pbStringToAudioFile(music) if music && music!=""
end
if !ret
# Check global metadata
music = GameData::Metadata.get.trainer_battle_BGM
ret = pbStringToAudioFile(music) if music && music!=""
end
ret = pbStringToAudioFile("Battle trainer") if !ret
return ret
end
def pbGetTrainerVictoryME(trainer) # can be a PokeBattle_Trainer or an array of them
if $PokemonGlobal.nextBattleME
return $PokemonGlobal.nextBattleME.clone
end
music = nil
trainerarray = (trainer.is_a?(Array)) ? trainer : [trainer]
trainerarray.each do |t|
trainer_type_data = GameData::TrainerType.get(t.trainertype)
music = trainer_type_data.victory_ME if trainer_type_data.victory_ME
end
ret = nil
if music && music!=""
ret = pbStringToAudioFile(music)
end
if !ret
# Check map metadata
music = GameData::MapMetadata.get($game_map.map_id).trainer_victory_ME
if music && music!=""
ret = pbStringToAudioFile(music)
end
end
if !ret
# Check global metadata
music = GameData::Metadata.get.trainer_victory_ME
if music && music!=""
ret = pbStringToAudioFile(music)
end
end
ret = pbStringToAudioFile("Battle victory") if !ret
ret.name = "../../Audio/ME/"+ret.name
return ret
end