Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Encounter Area Script

Encounter Area Script




What does the script do?
- It Makes the encounter list depend on terrain tags.
you can define which monsters appear on land, water, and so on.
Each map has 7 areas (area1 = all tiles with tag 1).
- lets you change the encounter list in an event call (v1.2)
for more info, read the instructions in the code.

version 1.2
[rgss] 
#===========================================================
# ** Monster Area
#-----------------------------------------------------------
# by silver wind
# V1.2
#
# This script brings back a feature from RM2K3.
# It lets you define areas on the map.
# each area has different monsters (encounter list).
#
# * Define a new area :
#   use a tile with terrain tag (other than 0)
#   and simply draw with it on the map.
#   notes:
#    - use tags 1-7. tag 0 means no encounters
#    - may not work with events with a tile graphic.
#    - you can use a white tile on layer 3,
#      so the area is invisible.
#
# * Define monsters in each map :
#   see 'configure' below.
#
# * change the encouners in an area using a script :
#   script call: Encounters.set(hash)
#                 the hash should contain 1 or more lines of the form:
#                 tag_number => list of troop names
#                 ie: Encounters.set( {1=>'Ghost*2'} )
#===========================================================
class Area
  attr_reader :id
  attr_accessor :monster_list
 
  def initialize(id,list)
    @id = id
    @monster_list = list
  end
   
end
 
class Map_Area
 
  def initialize(map_id)
    @id = map_id
    @areas = {}
  end
 
  # i : area id
  def [](i)
    # return the area's monster list
    return [] if @areas.nil?
    @areas.monster_list
  end
 
  # i : area id
  def add(area)
    i = area.id
    @areas = area
  end
 
  def empty?(tag)
    # is the troop list empty?
    list = self[tag]
    return list.size == 0
  end
 
  def add_monster(area,id)
    return unless legal?(area)
    i = area
    # make a new area
    if (@areas)==nil
      new_area = Area.new( i, [] )
      add(new_area)
    end
    # add to area
    @areas.monster_list << id
  end
 
  def legal?(area)
     (area >= 1 or area <= 7)
  end
 
end
 
class Monster_Area
 
  def get_map
    $game_map.map_id
  end
 
  def initialize
    @areas = {}
    for i in map_list
      @areas = Map_Area.new(i)
    end
    setup
  end
 
  def [](area_id)
    # return the area's monster list
    # of the current map
    i = get_map
    j = area_id
    @areas[j]
  end
 
  def map_list
    maps = load_data('Data/MapInfos.rxdata')
    return maps.keys
  end
 
  # if you wish to know a map's id:
  def get_map_id( name )
    maps = load_data('Data/MapInfos.rxdata')
    maps.each_key {|key|
                    m = maps[key]
                    return key if m.name == name
                    }
    return nil
  end
 
  # id = the area number, which is the terain tag's number
  def new_area(map, id, monsters )
    ar = Area.new( id, monsters )
    @areas[map].add(ar)
  end
 
  def setup             # new, data was a normal array
    @data = { 0=>[],
#=============================================================
# Configure
#-------------------------------------------------------------
#
#  Each line defines the areas in one map.
#  genarally, use:
#  [ [area001_troops], [area002_troops] (and so on)],
# for example, the line [ [1,2], [3,4] ]
# means troops 1&2 in area 001, 3&4 in area 002.
#
# * maps not set below will have no monsters in them.
# * this can be set in an event on the map.
#-------------------------------------------------------------
           # map 1
           1=>[],
           # map 2
           2=>[],
           
           17=>[]
           # add more lines if you need
          }
#=============================================================
    @data.each { |map,list|
       # for each map in data
       for i in 0...list.size
         foes = list
         # i+1 gives tags 1~7
         tag = i+1
         new_area(map, tag, foes)
       end
      }
 
  end
 
  def get_troop
    # get current area
    area = $game_player.terrain_tag
    return nil unless (area >= 0 and area <= 7)
    # get monster list
    list = self[area]
    return nil if list.size==0    
    n = rand(list.size)
    troop_id = list[n]
    # If troop is valid
    if $data_troops[troop_id] != nil
      return troop_id
    end
   
  end
 
  def empty_area?                  
    area = $game_player.terrain_tag
    return true unless (area >= 0 and area <= 7)
    map = get_map
    @areas[map].empty?(area)
  end
 
  def add(area, troop)         # new - add encounters in an event
    i = get_map
    id = troop
    @areas.add_monster(area,id)
  end
 
end
 
 
class Scene_Title
 
  alias areas_new_game command_new_game
  def command_new_game
    $monster_area = Monster_Area.new
    areas_new_game
  end
 
 
end
 
class Scene_Map
 
  def update
    # Loop
    loop do
      # Update map, interpreter, and player order
      # (this update order is important for when conditions are fulfilled
      # to run any event, and the player isn't provided the opportunity to
      # move in an instant)
      $game_map.update
      $game_system.map_interpreter.update
      $game_player.update
      # Update system (timer), screen
      $game_system.update
      $game_screen.update
      # Abort loop if player isn't place moving
      unless $game_temp.player_transferring
        break
      end
      # Run place move
      transfer_player
      # Abort loop if transition processing
      if $game_temp.transition_processing
        break
      end
    end
    # Update sprite set
    @spriteset.update
    # Update message window
    @message_window.update
    # If game over
    if $game_temp.gameover
      # Switch to game over screen
      $scene = Scene_Gameover.new
      return
    end
    # If returning to title screen
    if $game_temp.to_title
      # Change to title screen
      $scene = Scene_Title.new
      return
    end
    # If transition processing
    if $game_temp.transition_processing
      # Clear transition processing flag
      $game_temp.transition_processing = false
      # Execute transition
      if $game_temp.transition_name == ""
        Graphics.transition(20)
      else
        Graphics.transition(40, "Graphics/Transitions/" +
          $game_temp.transition_name)
      end
    end
    # If showing message window
    if $game_temp.message_window_showing
      return
    end
   
    #=======================================================
    # my edit
    if $game_player.encounter_count == 0
      unless $game_system.map_interpreter.running? or
             $game_system.encounter_disabled
        #
        troop_id = $monster_area.get_troop
        if troop_id != nil
          # Set battle calling flag
          $game_temp.battle_calling = true
          $game_temp.battle_troop_id = troop_id
          $game_temp.battle_can_escape = true
          $game_temp.battle_can_lose = false
          $game_temp.battle_proc = nil
        end
      end
    end
   #=======================================================
    # If B button was pressed
    if Input.trigger?(Input::B)
      # If event is running, or menu is not forbidden
      unless $game_system.map_interpreter.running? or
             $game_system.menu_disabled
        # Set menu calling flag or beep flag
        $game_temp.menu_calling = true
        $game_temp.menu_beep = true
      end
    end
    # If debug mode is ON and F9 key was pressed
    if $DEBUG and Input.press?(Input::F9)
      # Set debug calling flag
      $game_temp.debug_calling = true
    end
    # If player is not moving
    unless $game_player.moving?
      # Run calling of each screen
      if $game_temp.battle_calling
        call_battle
      elsif $game_temp.shop_calling
        call_shop
      elsif $game_temp.name_calling
        call_name
      elsif $game_temp.menu_calling
        call_menu
      elsif $game_temp.save_calling
        call_save
      elsif $game_temp.debug_calling
        call_debug
      end
    end
  end
 
end
 
#================================================
#  * Encounters
#================================================
module Encounters
  module_function
 
  def get_troop_id(name)
    for id in (1...$data_troops.length)
      troop = $data_troops[id]
      return id if troop.name == name
    end
    return nil
  end
 
  def set(hash)
    hash.each {|tag,troop|
      for name in troop
        # get id
        id = get_troop_id(name)
        # if such troop doesn't exist
        next if id.nil?
        # else
        $monster_area.add(tag,id)
      end
     }
  end
 
end
[/rgss]

older version (1.1)
[rgss] 
#===========================================================
# ** Monster Area
#-----------------------------------------------------------
# by silver wind
# V1.0
#
# This script uses terrain tags to define areas on the map.
# each area has different monsters (encounter list).
#
# * Define a new area :
#   use a tile with terrain tag (other than 0)
#   simply draw on the map with it.
#   notes:
#    - use tags 1-7. tag 0 means no encounters
#    - may not work with events with a tile graphic.
#    - you can use a white tile on layer 3,
#      so the area is invisible.
#
# * Add monsters to an area :
#   see 'configure' below.
#===========================================================
class Area
  attr_reader :id
  attr_reader :monster_list
 
  def initialize(id,list)
    @id = id
    @monster_list = list
  end
 
end
 
class Map_Area
 
  def initialize(map_id)
    @id = map_id
    @areas = {}
  end
 
  # i : area id
  def [](i)
    # return the area's monster list
    return nil if @areas.nil?
    @areas.monster_list
  end
 
  # i : area id
  def add(area)
    i = area.id
    @areas = area
  end
 
end
 
class Monster_Area
 
  def get_map
    $game_map.map_id
  end
 
  def initialize
    @areas = {}
    for i in map_list
      @areas = Map_Area.new(i)
    end
    setup
  end
 
  def [](area_id)
    # return the area's monster list
    # of the current map
    i = get_map
    j = area_id
    @areas[j]
  end
 
  def map_list
    maps = load_data('Data/MapInfos.rxdata')
    return maps.keys
  end
 
  # returns a map's id:
  def get_map_id( name )
    maps = load_data('Data/MapInfos.rxdata')
    maps.each_key {|key|
                    m = maps[key]
                    return key if m.name == name
                    }
    return nil
  end
 
  # id = the area number, which is the terain tag's number
  def new_area(map, id, monsters )
    ar = Area.new( id, monsters )
    @areas[map].add(ar)
  end
 
  def setup
    data = [ [],
#=============================================================
# Configure
#-------------------------------------------------------------
#
#  Each line defines the areas in one map.
#  genarally, use:
#  [ [area001_troops], [area002_troops] (and so on)],
# for example, the line [ [1,2], [3,4] ]
# means troops 1&2 in area 001, 3&4 in area 002.
           # map 1
           [ [2], [3,4]  ],
           # map 2
           [],
           # add more lines if you need
          ]
#=============================================================
    for i in 1...data.size
      foes = data
      for j in 0...foes.size
        foe_list = foes[j]
        # turn 0-7 to 1-8
        k = j+ 1
        new_area(i, k, foe_list)
      end
    end
   
  end
 
  def get_troop
    # get current area
    area = $game_player.terrain_tag
    return nil unless (area >= 0 and area <= 7)
    # get monster list
    list = self[area]
    n = rand(list.size)
    troop_id = list[n]
    # If troop is valid
    if $data_troops[troop_id] != nil
      return troop_id
    end
   
  end
 
end
 
 
class Scene_Title
 
  alias areas_new_game command_new_game
  def command_new_game
    $monster_area = Monster_Area.new
    areas_new_game
  end
 
 
end
 
class Scene_Map
 
  def update
    # Loop
    loop do
      # Update map, interpreter, and player order
      # (this update order is important for when conditions are fulfilled
      # to run any event, and the player isn't provided the opportunity to
      # move in an instant)
      $game_map.update
      $game_system.map_interpreter.update
      $game_player.update
      # Update system (timer), screen
      $game_system.update
      $game_screen.update
      # Abort loop if player isn't place moving
      unless $game_temp.player_transferring
        break
      end
      # Run place move
      transfer_player
      # Abort loop if transition processing
      if $game_temp.transition_processing
        break
      end
    end
    # Update sprite set
    @spriteset.update
    # Update message window
    @message_window.update
    # If game over
    if $game_temp.gameover
      # Switch to game over screen
      $scene = Scene_Gameover.new
      return
    end
    # If returning to title screen
    if $game_temp.to_title
      # Change to title screen
      $scene = Scene_Title.new
      return
    end
    # If transition processing
    if $game_temp.transition_processing
      # Clear transition processing flag
      $game_temp.transition_processing = false
      # Execute transition
      if $game_temp.transition_name == ""
        Graphics.transition(20)
      else
        Graphics.transition(40, "Graphics/Transitions/" +
          $game_temp.transition_name)
      end
    end
    # If showing message window
    if $game_temp.message_window_showing
      return
    end
   
    #=======================================================
    # my edit
    if $game_player.encounter_count == 0
      unless $game_system.map_interpreter.running? or
             $game_system.encounter_disabled
        #
        troop_id = $monster_area.get_troop
        if troop_id != nil
          # Set battle calling flag
          $game_temp.battle_calling = true
          $game_temp.battle_troop_id = troop_id
          $game_temp.battle_can_escape = true
          $game_temp.battle_can_lose = false
          $game_temp.battle_proc = nil
        end
      end
    end
   #=======================================================
    # If B button was pressed
    if Input.trigger?(Input::B)
      # If event is running, or menu is not forbidden
      unless $game_system.map_interpreter.running? or
             $game_system.menu_disabled
        # Set menu calling flag or beep flag
        $game_temp.menu_calling = true
        $game_temp.menu_beep = true
      end
    end
    # If debug mode is ON and F9 key was pressed
    if $DEBUG and Input.press?(Input::F9)
      # Set debug calling flag
      $game_temp.debug_calling = true
    end
    # If player is not moving
    unless $game_player.moving?
      # Run calling of each screen
      if $game_temp.battle_calling
        call_battle
      elsif $game_temp.shop_calling
        call_shop
      elsif $game_temp.name_calling
        call_name
      elsif $game_temp.menu_calling
        call_menu
      elsif $game_temp.save_calling
        call_save
      elsif $game_temp.debug_calling
        call_debug
      end
    end
  end
 
end
 
[/rgss]


If you have any questions, feel free to ask
:)
 
Just one thing- you know using an invisible tile on layer 3 screws up passabilities, right? So how else do you draw up invisible monster areas?

Anyway, this looks pretty cool, and useful if that's what you want for your game.
 

Atoa

Member

@silver wind
Quite similar to an add-on i was planning to maker for my battle system, although mine would also manage battle backs.

@Pokémaniac
You can simply duplicate tiles on the tileset and add different tags for them.
 
@pokemaniac, you're right. The solution is simple tough- only place those tiles on passable areas.
@Atoa
Nice idea, haven't thought of it ^^
 
Yes. If you're familiar to scripting, the long answer is : it'll work as long as it's placed above main and below any scripts which edits Scene_Map's update
 
There's only one problem I have with this script. You have to configure EVERY single map that's in your game. If you don't, and you set the encounters by Map Properties, it gives you an error(attached with this message). Can you configure it so that if the terrain tag is 0, it uses just the regular "Map Properties" encounters?
Thanks in advance.'


Monster%20Areas%20Script.jpg
 
BTW, since you didn't comment anything in your script, OK, OK, I also forget about that quite often, you can write both attr_readers like this...

attr_reader :id, :monster_list

Any attribute lets you initialize 1 or more variables and construct their respective methods at request.
 
ok, this solves the problem pyrodude mentioned.

[rgss] 
#===========================================================
# ** Monster Area
#-----------------------------------------------------------
# by silver wind
# V1.1
#
# This script brings back a feature from RM2K3.
# It lets you define areas on the map.
# each area has different monsters (encounter list).
#
# * Define a new area :
#   use a tile with terrain tag (other than 0)
#   and simply draw with it on the map.
#   notes:
#    - use tags 1-7. tag 0 means no encounters
#    - may not work with events with a tile graphic.
#    - you can use a white tile on layer 3,
#      so the area is invisible.
#
# * Add monsters to an area :
#   see 'configure' below.
#   you can also use an event script command, ie:
#   set Ghost to appear on tag 1& basilisks on tag 2:
#   list = {1=>['Ghost*2','Gost*3'],
#     2=>['Basilisk*2']
#    }
#   Encounters.set(list)
#===========================================================
 
class Area
  attr_reader :id
  attr_accessor :monster_list
 
  def initialize(id,list)
    @id = id
    @monster_list = list
  end
   
end
 
class Map_Area
 
  def initialize(map_id)
    @id = map_id
    @areas = {}
  end
 
  # i : area id
  def [](i)
    # return the area's monster list
    return [] if @areas.nil?  
    @areas.monster_list
  end
 
  # i : area id
  def add(area)
    i = area.id
    @areas = area
  end
 
  def empty?(tag)
    # is the troop list empty?
    list = self[tag]
    return list.size == 0
  end
 
  def add_monster(area,id)
    return unless legal?(area)
    i = area
    # make a new area
    if (@areas)==nil
      new_area = Area.new( i, [] )
      add(new_area)
    end
    # add to area
    @areas.monster_list << id
  end
 
  def legal?(area)
     (area >= 1 or area <= 7)
  end
 
end
 
class Monster_Area
 
  def get_map
    $game_map.map_id
  end
 
  def initialize
    @areas = {}
    for i in map_list
      @areas = Map_Area.new(i)
    end
    setup
  end
 
  def [](area_id)
    # return the area's monster list
    # of the current map
    i = get_map
    j = area_id
    @areas[j]
  end
 
  def map_list
    maps = load_data('Data/MapInfos.rxdata')
    return maps.keys
  end
 
  # if you wish to know a map's id:
  def get_map_id( name )
    maps = load_data('Data/MapInfos.rxdata')
    maps.each_key {|key|
                    m = maps[key]
                    return key if m.name == name
                    }
    return nil
  end
 
  # id = the area number, which is the terain tag's number
  def new_area(map, id, monsters )
    ar = Area.new( id, monsters )
    @areas[map].add(ar)
  end
 
  def setup            
    @data = { 0=>[],
#=============================================================
# Configure
#-------------------------------------------------------------
#
#  Each line defines the areas in one map.
#  genarally, use:
#  [ [area001_troops], [area002_troops] (and so on)],
# for example, the line [ [1,2], [3,4] ]
# means troops 1&2 in area 001, 3&4 in area 002.
#
# * maps not set below will have no monsters in them.
#-------------------------------------------------------------
           # map 1
           1=>[],
           # map 2
           2=>[]
           # add more lines if you need
          }
#=============================================================
    @data.each { |map,list|
       # for each map in data
       for i in 0...list.size
         foes = list
         # i+1 gives tags 1~7
         tag = i+1
         new_area(map, tag, foes)
       end
      }
 
  end
 
  def get_troop
    # get current area
    area = $game_player.terrain_tag
    return nil unless (area >= 0 and area <= 7)
    # get monster list
    list = self[area]
    return nil if list.size==0    
    n = rand(list.size)
    troop_id = list[n]
    # If troop is valid
    if $data_troops[troop_id] != nil
      return troop_id
    end
   
  end
 
  def empty_area?                
    area = $game_player.terrain_tag
    return true unless (area >= 0 and area <= 7)
    map = get_map
    @areas[map].empty?(area)
  end
 
  def add(area, mon)        
    i = get_map
    id = mon
    @areas.add_monster(area,id)
  end
 
end
 
 
class Scene_Title
 
  alias areas_new_game command_new_game
  def command_new_game
    $monster_area = Monster_Area.new
    areas_new_game
  end
 
 
end
 
class Scene_Map
 
  def update
    # Loop
    loop do
      # Update map, interpreter, and player order
      # (this update order is important for when conditions are fulfilled
      # to run any event, and the player isn't provided the opportunity to
      # move in an instant)
      $game_map.update
      $game_system.map_interpreter.update
      $game_player.update
      # Update system (timer), screen
      $game_system.update
      $game_screen.update
      # Abort loop if player isn't place moving
      unless $game_temp.player_transferring
        break
      end
      # Run place move
      transfer_player
      # Abort loop if transition processing
      if $game_temp.transition_processing
        break
      end
    end
    # Update sprite set
    @spriteset.update
    # Update message window
    @message_window.update
    # If game over
    if $game_temp.gameover
      # Switch to game over screen
      $scene = Scene_Gameover.new
      return
    end
    # If returning to title screen
    if $game_temp.to_title
      # Change to title screen
      $scene = Scene_Title.new
      return
    end
    # If transition processing
    if $game_temp.transition_processing
      # Clear transition processing flag
      $game_temp.transition_processing = false
      # Execute transition
      if $game_temp.transition_name == ""
        Graphics.transition(20)
      else
        Graphics.transition(40, "Graphics/Transitions/" +
          $game_temp.transition_name)
      end
    end
    # If showing message window
    if $game_temp.message_window_showing
      return
    end
   
    #=======================================================
    # my edit
    if $game_player.encounter_count == 0
      unless $game_system.map_interpreter.running? or
             $game_system.encounter_disabled
        #
        troop_id = $monster_area.get_troop
        if troop_id != nil
          # Set battle calling flag
          $game_temp.battle_calling = true
          $game_temp.battle_troop_id = troop_id
          $game_temp.battle_can_escape = true
          $game_temp.battle_can_lose = false
          $game_temp.battle_proc = nil
        end
      end
    end
   #=======================================================
    # If B button was pressed
    if Input.trigger?(Input::B)
      # If event is running, or menu is not forbidden
      unless $game_system.map_interpreter.running? or
             $game_system.menu_disabled
        # Set menu calling flag or beep flag
        $game_temp.menu_calling = true
        $game_temp.menu_beep = true
      end
    end
    # If debug mode is ON and F9 key was pressed
    if $DEBUG and Input.press?(Input::F9)
      # Set debug calling flag
      $game_temp.debug_calling = true
    end
    # If player is not moving
    unless $game_player.moving?
      # Run calling of each screen
      if $game_temp.battle_calling
        call_battle
      elsif $game_temp.shop_calling
        call_shop
      elsif $game_temp.name_calling
        call_name
      elsif $game_temp.menu_calling
        call_menu
      elsif $game_temp.save_calling
        call_save
      elsif $game_temp.debug_calling
        call_debug
      end
    end
  end
 
end
 
#================================================
#  * Encounters
#================================================
module Encounters
  module_function
 
  def set(hash)
    hash.each {|tag,monsters|
      for name in monsters
        # get id
        for i in 1...$data_troops.size    
          troop = $data_troops
          if troop.name == name
            id = troop.id
          end
        end
        return if id.nil?              
        $monster_area.add(tag,id)
      end
     }
  end
 
end
 
[/rgss]
New in this version: You can set the map encounters using an event, see details in the script. You can't change them once they're set, though. I can make a quick fix for that if anyone asks.
@kyonides
Thanks for taking the time to comment ^^
The attr_accessor is there because of the line saying:
@areas.monster_list << id
About the comments, well the new classes are very simple. I'm usually commenting a lot, this time I felt no need to.
 
Mmm, is this for the default or sideview battle systems? Or is it for some Zelda like games? Because I haven't seen any trace of script changes for any battle system in your script. Is it really complete?
 

Gust

Member

kyonides":37nxq6rl said:
Mmm, is this for the default or sideview battle systems? Or is it for some Zelda like games? Because I haven't seen any trace of script changes for any battle system in your script. Is it really complete?

It just sets game_temp attributes so Scene_Map calls the default battle, just like the random encounters or events "start battle" would do. To change the battle system, one should simply change the "call_battle" method from Scene_Map.
 
So, it's not possible to change what monsters you run into with an event, just add to the list of monsters you run into?
 
you're not supposed to use events to set the monsters, at least not in this version of the script.. I have a newer version (below), which is easier to set with events.
use a script call saying: Encounters.set(hash) to set the monsters.
the hash is of the form: id=> 'list of troop names'
for example: Encounters.set({2=>'Ghost*2'})
^ changes the encounter list of tag #2 in the current map. it'll now have only Ghost*2.
Code:
 

#===========================================================

# ** Monster Area

#-----------------------------------------------------------

# by silver wind

# V1.2

#

# This script brings back a feature from RM2K3.

# It lets you define areas on the map. 

# each area has different monsters (encounter list).

#

# * Define a new area :

#   use a tile with terrain tag (other than 0)

#   and simply draw with it on the map.

#   notes:

#    - use tags 1-7. tag 0 means no encounters

#    - may not work with events with a tile graphic.

#    - you can use a white tile on layer 3, 

#      so the area is invisible.

#

# * Define monsters in each map :

#   see 'configure' below.

#

# * change the encouners in an area using a script :

#   script call: Encounters.set(hash)

#                 the hash should contain 1 or more lines of the form:

#                 tag_number => list of troop names

#                 ie: Encounters.set( {1=>'Ghost*2'} )

#===========================================================

class Area

  attr_reader :id

  attr_accessor :monster_list

  

  def initialize(id,list)

    @id = id

    @monster_list = list

  end

    

end

 

class Map_Area

 

  def initialize(map_id)

    @id = map_id

    @areas = {}

  end

  

  # i : area id

  def [](i)

    # return the area's monster list

    return [] if @areas[i].nil? 

    @areas[i].monster_list

  end

  

  # i : area id

  def add(area)

    i = area.id

    @areas[i] = area

  end

  

  def empty?(tag)

    # is the troop list empty?

    list = self[tag]

    return list.size == 0

  end

  

  def add_monster(area,id)

    return unless legal?(area)

    i = area

    # make a new area

    if (@areas[i])==nil

      new_area = Area.new( i, [] )

      add(new_area)

    end

    # add to area

    @areas[i].monster_list << id

  end

  

  def legal?(area)

     (area >= 1 or area <= 7)

  end

  

end

 

class Monster_Area

  

  def get_map

    $game_map.map_id

  end

  

  def initialize

    @areas = {}

    for i in map_list

      @areas[i] = Map_Area.new(i)

    end

    setup

  end

  

  def [](area_id)

    # return the area's monster list

    # of the current map

    i = get_map

    j = area_id

    @areas[i][j]

  end

 

  def map_list

    maps = load_data('Data/MapInfos.rxdata')

    return maps.keys

  end

  

  # if you wish to know a map's id:

  def get_map_id( name )

    maps = load_data('Data/MapInfos.rxdata')

    maps.each_key {|key| 

                    m = maps[key]

                    return key if m.name == name

                    }

    return nil

  end

  

  # id = the area number, which is the terain tag's number

  def new_area(map, id, monsters )

    ar = Area.new( id, monsters )

    @areas[map].add(ar)

  end

  

  def setup             # new, data was a normal array

    @data = { 0=>[],

#=============================================================

# Configure

#-------------------------------------------------------------

#

#  Each line defines the areas in one map.

#  genarally, use: 

#  [ [area001_troops], [area002_troops] (and so on)],

# for example, the line [ [1,2], [3,4] ]

# means troops 1&2 in area 001, 3&4 in area 002.

#

# * maps not set below will have no monsters in them.

# * this can be set in an event on the map.

#-------------------------------------------------------------

           # map 1

           1=>[],

           # map 2

           2=>[],

           

           17=>[]

           # add more lines if you need

          }

#=============================================================

    @data.each { |map,list| 

       # for each map in data 

       for i in 0...list.size

         foes = list[i]

         # i+1 gives tags 1~7

         tag = i+1

         new_area(map, tag, foes)

       end 

      }

 

  end

 

  def get_troop

    # get current area

    area = $game_player.terrain_tag

    return nil unless (area >= 0 and area <= 7)

    # get monster list

    list = self[area]

    return nil if list.size==0    

    n = rand(list.size)

    troop_id = list[n]

    # If troop is valid

    if $data_troops[troop_id] != nil

      return troop_id

    end

    

  end

  

  def empty_area?                  

    area = $game_player.terrain_tag

    return true unless (area >= 0 and area <= 7)

    map = get_map

    @areas[map].empty?(area)

  end

  

  def add(area, troop)         # new - add encounters in an event

    i = get_map

    id = troop 

    @areas[i].add_monster(area,id)

  end

  

end

 

 

class Scene_Title

  

  alias areas_new_game command_new_game

  def command_new_game

    $monster_area = Monster_Area.new

    areas_new_game

  end

  

  

end

 

class Scene_Map

 

  def update

    # Loop

    loop do

      # Update map, interpreter, and player order

      # (this update order is important for when conditions are fulfilled 

      # to run any event, and the player isn't provided the opportunity to

      # move in an instant)

      $game_map.update

      $game_system.map_interpreter.update

      $game_player.update

      # Update system (timer), screen

      $game_system.update

      $game_screen.update

      # Abort loop if player isn't place moving

      unless $game_temp.player_transferring

        break

      end

      # Run place move

      transfer_player

      # Abort loop if transition processing

      if $game_temp.transition_processing

        break

      end

    end

    # Update sprite set

    @spriteset.update

    # Update message window

    @message_window.update

    # If game over

    if $game_temp.gameover

      # Switch to game over screen

      $scene = Scene_Gameover.new

      return

    end

    # If returning to title screen

    if $game_temp.to_title

      # Change to title screen

      $scene = Scene_Title.new

      return

    end

    # If transition processing

    if $game_temp.transition_processing

      # Clear transition processing flag

      $game_temp.transition_processing = false

      # Execute transition

      if $game_temp.transition_name == ""

        Graphics.transition(20)

      else

        Graphics.transition(40, "Graphics/Transitions/" +

          $game_temp.transition_name)

      end

    end

    # If showing message window

    if $game_temp.message_window_showing

      return

    end

    

    #=======================================================

    # my edit

    if $game_player.encounter_count == 0

      unless $game_system.map_interpreter.running? or

             $game_system.encounter_disabled

        #

        troop_id = $monster_area.get_troop

        if troop_id != nil

          # Set battle calling flag

          $game_temp.battle_calling = true

          $game_temp.battle_troop_id = troop_id

          $game_temp.battle_can_escape = true

          $game_temp.battle_can_lose = false

          $game_temp.battle_proc = nil

        end

      end

    end

   #=======================================================

    # If B button was pressed

    if Input.trigger?(Input::B)

      # If event is running, or menu is not forbidden

      unless $game_system.map_interpreter.running? or

             $game_system.menu_disabled

        # Set menu calling flag or beep flag

        $game_temp.menu_calling = true

        $game_temp.menu_beep = true

      end

    end

    # If debug mode is ON and F9 key was pressed

    if $DEBUG and Input.press?(Input::F9)

      # Set debug calling flag

      $game_temp.debug_calling = true

    end

    # If player is not moving

    unless $game_player.moving?

      # Run calling of each screen

      if $game_temp.battle_calling

        call_battle

      elsif $game_temp.shop_calling

        call_shop

      elsif $game_temp.name_calling

        call_name

      elsif $game_temp.menu_calling

        call_menu

      elsif $game_temp.save_calling

        call_save

      elsif $game_temp.debug_calling

        call_debug

      end

    end

  end

  

end

 

#================================================

#  * Encounters

#================================================

module Encounters

  module_function

  

  def get_troop_id(name)

    for id in (1...$data_troops.length)

      troop = $data_troops[id]

      return id if troop.name == name

    end

    return nil

  end

  

  def set(hash)

    hash.each {|tag,troop|

      for name in troop

        # get id

        id = get_troop_id(name)

        # if such troop doesn't exist

        next if id.nil?

        # else

        $monster_area.add(tag,id)

      end

     }

  end

  

end

Edit: oops, didn't check the date. ^^;;
but maybe this script will be useful to other people
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top