Advertisement
Vendily

Ambient Pokémon Cries

Jul 21st, 2017
1,931
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.04 KB | None | 0 0
  1. #===============================================================================
  2. # * Ambient Pokémon Cries - by Vendily
  3. #===============================================================================
  4. # This script plays random cries of pokémon that can be encountered on the map
  5. #  for ambiance. It does not activate for maps that don't have pokemon,
  6. #  and optionally only when a switch is active.
  7. # To ensure you get no errors, this script must be under PField_Field. It can
  8. #  be anywhere but it must be under that section. (It's because it uses the
  9. #  Events Module, which is defined in PField_Field)
  10. #===============================================================================
  11. # * The time between cries in seconds (arbitrarily default 60)
  12. # * The variance in time for cries in seconds (arbitrarily default +/-rand(5))
  13. # * The Global Switch that is checked to see if ambiance should be used, set
  14. #      to -1 to always play ambiance if possible
  15. # * The volume to play the cry at (default 65)
  16. # * If the game should play roamer cries, which are the only cries that play
  17. #    should one be found on the current map (default true)
  18. #===============================================================================
  19. TIME_BETWEEN_CRIES  = 60
  20. RANDOM_TIME_FACTOR  = 5
  21. AMBIANCE_SWITCH     = -1
  22. CRY_VOLUME          = 65
  23. CRY_ROAMERS         = true
  24.  
  25. class PokemonEncounters
  26.   def pbAllValidEncounterTypes(mapID)
  27.     data=load_data("Data/encounters.dat")
  28.     if data.is_a?(Hash) && data[mapID]
  29.       enctypes=data[mapID][1]
  30.     else
  31.       return nil
  32.     end
  33.     ret=[]
  34.     enctypes.each_index{|enc|
  35.       ret.push(enc) if enctypes[enc]
  36.     }
  37.     return ret
  38.   end
  39. end
  40.  
  41. class PokemonTemp
  42.   attr_accessor :lastCryTime
  43. end
  44.  
  45. def pbPlayAmbiance
  46.   if AMBIANCE_SWITCH<0 || $game_switches[AMBIANCE_SWITCH]
  47.     roam=[]
  48.     if CRY_ROAMERS
  49.       for i in 0...RoamingSpecies.length
  50.         poke=RoamingSpecies[i]
  51.         species=getID(PBSpecies,poke[0])
  52.         next if !species || species<=0
  53.         if $game_switches[poke[2]] && $PokemonGlobal.roamPokemon[i]!=true
  54.           currentArea=$PokemonGlobal.roamPosition[i]
  55.           if !currentArea
  56.             $PokemonGlobal.roamPosition[i]=keys[rand(keys.length)]
  57.             currentArea=$PokemonGlobal.roamPosition[i]
  58.           end
  59.           roamermeta=pbGetMetadata(currentArea,MetadataMapPosition)
  60.           possiblemaps=[]
  61.           mapinfos=$RPGVX ? load_data("Data/MapInfos.rvdata") : load_data("Data/MapInfos.rxdata")
  62.           for j in 1...mapinfos.length
  63.             jmeta=pbGetMetadata(j,MetadataMapPosition)
  64.             if mapinfos[j] && mapinfos[j].name==$game_map.name &&
  65.               roamermeta && jmeta && roamermeta[0]==jmeta[0]
  66.               possiblemaps.push(j)   # Any map with same name as roamer's current map
  67.             end
  68.           end
  69.           if possiblemaps.include?(currentArea) && pbRoamingMethodAllowed(poke[3])
  70.             # Change encounter to species and level, with BGM on end
  71.             roam.push(species)
  72.           end
  73.         end
  74.       end
  75.     end
  76.     if roam.length>0
  77.       pbPlayCry(roam[rand(roam.length)],CRY_VOLUME)
  78.     else
  79.       enctypes=$PokemonEncounters.pbAllValidEncounterTypes($game_map.map_id) rescue []
  80.       if enctypes && enctypes.length>0
  81.         invalenc=true
  82.         while invalenc
  83.           enc=enctypes[rand(enctypes.length)]
  84.           if (enc==EncounterTypes::LandNight && !PBDayNight.isNight?) ||
  85.              (enc==EncounterTypes::LandDay && !PBDayNight.isDay?) ||
  86.              (enc==EncounterTypes::LandMorning && !PBDayNight.isMorning?)
  87.           else
  88.             invalenc=false
  89.           end
  90.         end
  91.         crypoke=$PokemonEncounters.pbEncounteredPokemon(enc)[0] rescue nil
  92.       end
  93.       if crypoke
  94.         pbPlayCry(crypoke,CRY_VOLUME)
  95.       end
  96.     end
  97.   end
  98. end
  99.  
  100. Events.onMapUpdate+=proc {|sender,e|   # Ambiance check
  101.   last=$PokemonTemp.lastCryTime
  102.   now=pbGetTimeNow
  103.   if !last || (now-last>(TIME_BETWEEN_CRIES+((rand(2)==0 ? -1 : 1)*rand(RANDOM_TIME_FACTOR))))
  104.     pbPlayAmbiance
  105.     $PokemonTemp.lastCryTime=now
  106.   end
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement