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
  1.  
  2. #===========================================================
  3. # ** Monster Area
  4. #-----------------------------------------------------------
  5. # by silver wind
  6. # V1.2
  7. #
  8. # This script brings back a feature from RM2K3.
  9. # It lets you define areas on the map.
  10. # each area has different monsters (encounter list).
  11. #
  12. # * Define a new area :
  13. #   use a tile with terrain tag (other than 0)
  14. #   and simply draw with it on the map.
  15. #   notes:
  16. #    - use tags 1-7. tag 0 means no encounters
  17. #    - may not work with events with a tile graphic.
  18. #    - you can use a white tile on layer 3,
  19. #      so the area is invisible.
  20. #
  21. # * Define monsters in each map :
  22. #   see 'configure' below.
  23. #
  24. # * change the encouners in an area using a script :
  25. #   script call: Encounters.set(hash)
  26. #                 the hash should contain 1 or more lines of the form:
  27. #                 tag_number => list of troop names
  28. #                 ie: Encounters.set( {1=>'Ghost*2'} )
  29. #===========================================================
  30. class Area
  31.   attr_reader :id
  32.   attr_accessor :monster_list
  33.  
  34.   def initialize(id,list)
  35.     @id = id
  36.     @monster_list = list
  37.   end
  38.    
  39. end
  40.  
  41. class Map_Area
  42.  
  43.   def initialize(map_id)
  44.     @id = map_id
  45.     @areas = {}
  46.   end
  47.  
  48.   # i : area id
  49.   def [](i)
  50.     # return the area's monster list
  51.     return [] if @areas[i].nil?
  52.     @areas[i].monster_list
  53.   end
  54.  
  55.   # i : area id
  56.   def add(area)
  57.     i = area.id
  58.     @areas[i] = area
  59.   end
  60.  
  61.   def empty?(tag)
  62.     # is the troop list empty?
  63.     list = self[tag]
  64.     return list.size == 0
  65.   end
  66.  
  67.   def add_monster(area,id)
  68.     return unless legal?(area)
  69.     i = area
  70.     # make a new area
  71.     if (@areas[i])==nil
  72.       new_area = Area.new( i, [] )
  73.       add(new_area)
  74.     end
  75.     # add to area
  76.     @areas[i].monster_list << id
  77.   end
  78.  
  79.   def legal?(area)
  80.      (area >= 1 or area <= 7)
  81.   end
  82.  
  83. end
  84.  
  85. class Monster_Area
  86.  
  87.   def get_map
  88.     $game_map.map_id
  89.   end
  90.  
  91.   def initialize
  92.     @areas = {}
  93.     for i in map_list
  94.       @areas[i] = Map_Area.new(i)
  95.     end
  96.     setup
  97.   end
  98.  
  99.   def [](area_id)
  100.     # return the area's monster list
  101.     # of the current map
  102.     i = get_map
  103.     j = area_id
  104.     @areas[i][j]
  105.   end
  106.  
  107.   def map_list
  108.     maps = load_data('Data/MapInfos.rxdata')
  109.     return maps.keys
  110.   end
  111.  
  112.   # if you wish to know a map's id:
  113.   def get_map_id( name )
  114.     maps = load_data('Data/MapInfos.rxdata')
  115.     maps.each_key {|key|
  116.                     m = maps[key]
  117.                     return key if m.name == name
  118.                     }
  119.     return nil
  120.   end
  121.  
  122.   # id = the area number, which is the terain tag's number
  123.   def new_area(map, id, monsters )
  124.     ar = Area.new( id, monsters )
  125.     @areas[map].add(ar)
  126.   end
  127.  
  128.   def setup             # new, data was a normal array
  129.     @data = { 0=>[],
  130. #=============================================================
  131. # Configure
  132. #-------------------------------------------------------------
  133. #
  134. #  Each line defines the areas in one map.
  135. #  genarally, use:
  136. #  [ [area001_troops], [area002_troops] (and so on)],
  137. # for example, the line [ [1,2], [3,4] ]
  138. # means troops 1&2 in area 001, 3&4 in area 002.
  139. #
  140. # * maps not set below will have no monsters in them.
  141. # * this can be set in an event on the map.
  142. #-------------------------------------------------------------
  143.            # map 1
  144.            1=>[],
  145.            # map 2
  146.            2=>[],
  147.            
  148.            17=>[]
  149.            # add more lines if you need
  150.           }
  151. #=============================================================
  152.     @data.each { |map,list|
  153.        # for each map in data
  154.        for i in 0...list.size
  155.          foes = list[i]
  156.          # i+1 gives tags 1~7
  157.          tag = i+1
  158.          new_area(map, tag, foes)
  159.        end
  160.       }
  161.  
  162.   end
  163.  
  164.   def get_troop
  165.     # get current area
  166.     area = $game_player.terrain_tag
  167.     return nil unless (area >= 0 and area <= 7)
  168.     # get monster list
  169.     list = self[area]
  170.     return nil if list.size==0    
  171.     n = rand(list.size)
  172.     troop_id = list[n]
  173.     # If troop is valid
  174.     if $data_troops[troop_id] != nil
  175.       return troop_id
  176.     end
  177.    
  178.   end
  179.  
  180.   def empty_area?                  
  181.     area = $game_player.terrain_tag
  182.     return true unless (area >= 0 and area <= 7)
  183.     map = get_map
  184.     @areas[map].empty?(area)
  185.   end
  186.  
  187.   def add(area, troop)         # new - add encounters in an event
  188.     i = get_map
  189.     id = troop
  190.     @areas[i].add_monster(area,id)
  191.   end
  192.  
  193. end
  194.  
  195.  
  196. class Scene_Title
  197.  
  198.   alias areas_new_game command_new_game
  199.   def command_new_game
  200.     $monster_area = Monster_Area.new
  201.     areas_new_game
  202.   end
  203.  
  204.  
  205. end
  206.  
  207. class Scene_Map
  208.  
  209.   def update
  210.     # Loop
  211.     loop do
  212.       # Update map, interpreter, and player order
  213.       # (this update order is important for when conditions are fulfilled
  214.       # to run any event, and the player isn't provided the opportunity to
  215.       # move in an instant)
  216.       $game_map.update
  217.       $game_system.map_interpreter.update
  218.       $game_player.update
  219.       # Update system (timer), screen
  220.       $game_system.update
  221.       $game_screen.update
  222.       # Abort loop if player isn't place moving
  223.       unless $game_temp.player_transferring
  224.         break
  225.       end
  226.       # Run place move
  227.       transfer_player
  228.       # Abort loop if transition processing
  229.       if $game_temp.transition_processing
  230.         break
  231.       end
  232.     end
  233.     # Update sprite set
  234.     @spriteset.update
  235.     # Update message window
  236.     @message_window.update
  237.     # If game over
  238.     if $game_temp.gameover
  239.       # Switch to game over screen
  240.       $scene = Scene_Gameover.new
  241.       return
  242.     end
  243.     # If returning to title screen
  244.     if $game_temp.to_title
  245.       # Change to title screen
  246.       $scene = Scene_Title.new
  247.       return
  248.     end
  249.     # If transition processing
  250.     if $game_temp.transition_processing
  251.       # Clear transition processing flag
  252.       $game_temp.transition_processing = false
  253.       # Execute transition
  254.       if $game_temp.transition_name == ""
  255.         Graphics.transition(20)
  256.       else
  257.         Graphics.transition(40, "Graphics/Transitions/" +
  258.           $game_temp.transition_name)
  259.       end
  260.     end
  261.     # If showing message window
  262.     if $game_temp.message_window_showing
  263.       return
  264.     end
  265.    
  266.     #=======================================================
  267.     # my edit
  268.     if $game_player.encounter_count == 0
  269.       unless $game_system.map_interpreter.running? or
  270.              $game_system.encounter_disabled
  271.         #
  272.         troop_id = $monster_area.get_troop
  273.         if troop_id != nil
  274.           # Set battle calling flag
  275.           $game_temp.battle_calling = true
  276.           $game_temp.battle_troop_id = troop_id
  277.           $game_temp.battle_can_escape = true
  278.           $game_temp.battle_can_lose = false
  279.           $game_temp.battle_proc = nil
  280.         end
  281.       end
  282.     end
  283.    #=======================================================
  284.     # If B button was pressed
  285.     if Input.trigger?(Input::B)
  286.       # If event is running, or menu is not forbidden
  287.       unless $game_system.map_interpreter.running? or
  288.              $game_system.menu_disabled
  289.         # Set menu calling flag or beep flag
  290.         $game_temp.menu_calling = true
  291.         $game_temp.menu_beep = true
  292.       end
  293.     end
  294.     # If debug mode is ON and F9 key was pressed
  295.     if $DEBUG and Input.press?(Input::F9)
  296.       # Set debug calling flag
  297.       $game_temp.debug_calling = true
  298.     end
  299.     # If player is not moving
  300.     unless $game_player.moving?
  301.       # Run calling of each screen
  302.       if $game_temp.battle_calling
  303.         call_battle
  304.       elsif $game_temp.shop_calling
  305.         call_shop
  306.       elsif $game_temp.name_calling
  307.         call_name
  308.       elsif $game_temp.menu_calling
  309.         call_menu
  310.       elsif $game_temp.save_calling
  311.         call_save
  312.       elsif $game_temp.debug_calling
  313.         call_debug
  314.       end
  315.     end
  316.   end
  317.  
  318. end
  319.  
  320. #================================================
  321. #  * Encounters
  322. #================================================
  323. module Encounters
  324.   module_function
  325.  
  326.   def get_troop_id(name)
  327.     for id in (1...$data_troops.length)
  328.       troop = $data_troops[id]
  329.       return id if troop.name == name
  330.     end
  331.     return nil
  332.   end
  333.  
  334.   def set(hash)
  335.     hash.each {|tag,troop|
  336.       for name in troop
  337.         # get id
  338.         id = get_troop_id(name)
  339.         # if such troop doesn't exist
  340.         next if id.nil?
  341.         # else
  342.         $monster_area.add(tag,id)
  343.       end
  344.      }
  345.   end
  346.  
  347. end
Expand to see the code.


older version (1.1)
  1.  
  2. #===========================================================
  3. # ** Monster Area
  4. #-----------------------------------------------------------
  5. # by silver wind
  6. # V1.0
  7. #
  8. # This script uses terrain tags to define areas on the map.
  9. # each area has different monsters (encounter list).
  10. #
  11. # * Define a new area :
  12. #   use a tile with terrain tag (other than 0)
  13. #   simply draw on the map with it.
  14. #   notes:
  15. #    - use tags 1-7. tag 0 means no encounters
  16. #    - may not work with events with a tile graphic.
  17. #    - you can use a white tile on layer 3,
  18. #      so the area is invisible.
  19. #
  20. # * Add monsters to an area :
  21. #   see 'configure' below.
  22. #===========================================================
  23. class Area
  24.   attr_reader :id
  25.   attr_reader :monster_list
  26.  
  27.   def initialize(id,list)
  28.     @id = id
  29.     @monster_list = list
  30.   end
  31.  
  32. end
  33.  
  34. class Map_Area
  35.  
  36.   def initialize(map_id)
  37.     @id = map_id
  38.     @areas = {}
  39.   end
  40.  
  41.   # i : area id
  42.   def [](i)
  43.     # return the area's monster list
  44.     return nil if @areas[i].nil?
  45.     @areas[i].monster_list
  46.   end
  47.  
  48.   # i : area id
  49.   def add(area)
  50.     i = area.id
  51.     @areas[i] = area
  52.   end
  53.  
  54. end
  55.  
  56. class Monster_Area
  57.  
  58.   def get_map
  59.     $game_map.map_id
  60.   end
  61.  
  62.   def initialize
  63.     @areas = {}
  64.     for i in map_list
  65.       @areas[i] = Map_Area.new(i)
  66.     end
  67.     setup
  68.   end
  69.  
  70.   def [](area_id)
  71.     # return the area's monster list
  72.     # of the current map
  73.     i = get_map
  74.     j = area_id
  75.     @areas[i][j]
  76.   end
  77.  
  78.   def map_list
  79.     maps = load_data('Data/MapInfos.rxdata')
  80.     return maps.keys
  81.   end
  82.  
  83.   # returns a map's id:
  84.   def get_map_id( name )
  85.     maps = load_data('Data/MapInfos.rxdata')
  86.     maps.each_key {|key|
  87.                     m = maps[key]
  88.                     return key if m.name == name
  89.                     }
  90.     return nil
  91.   end
  92.  
  93.   # id = the area number, which is the terain tag's number
  94.   def new_area(map, id, monsters )
  95.     ar = Area.new( id, monsters )
  96.     @areas[map].add(ar)
  97.   end
  98.  
  99.   def setup
  100.     data = [ [],
  101. #=============================================================
  102. # Configure
  103. #-------------------------------------------------------------
  104. #
  105. #  Each line defines the areas in one map.
  106. #  genarally, use:
  107. #  [ [area001_troops], [area002_troops] (and so on)],
  108. # for example, the line [ [1,2], [3,4] ]
  109. # means troops 1&2 in area 001, 3&4 in area 002.
  110.            # map 1
  111.            [ [2], [3,4]  ],
  112.            # map 2
  113.            [],
  114.            # add more lines if you need
  115.           ]
  116. #=============================================================
  117.     for i in 1...data.size
  118.       foes = data[i]
  119.       for j in 0...foes.size
  120.         foe_list = foes[j]
  121.         # turn 0-7 to 1-8
  122.         k = j+ 1
  123.         new_area(i, k, foe_list)
  124.       end
  125.     end
  126.    
  127.   end
  128.  
  129.   def get_troop
  130.     # get current area
  131.     area = $game_player.terrain_tag
  132.     return nil unless (area >= 0 and area <= 7)
  133.     # get monster list
  134.     list = self[area]
  135.     n = rand(list.size)
  136.     troop_id = list[n]
  137.     # If troop is valid
  138.     if $data_troops[troop_id] != nil
  139.       return troop_id
  140.     end
  141.    
  142.   end
  143.  
  144. end
  145.  
  146.  
  147. class Scene_Title
  148.  
  149.   alias areas_new_game command_new_game
  150.   def command_new_game
  151.     $monster_area = Monster_Area.new
  152.     areas_new_game
  153.   end
  154.  
  155.  
  156. end
  157.  
  158. class Scene_Map
  159.  
  160.   def update
  161.     # Loop
  162.     loop do
  163.       # Update map, interpreter, and player order
  164.       # (this update order is important for when conditions are fulfilled
  165.       # to run any event, and the player isn't provided the opportunity to
  166.       # move in an instant)
  167.       $game_map.update
  168.       $game_system.map_interpreter.update
  169.       $game_player.update
  170.       # Update system (timer), screen
  171.       $game_system.update
  172.       $game_screen.update
  173.       # Abort loop if player isn't place moving
  174.       unless $game_temp.player_transferring
  175.         break
  176.       end
  177.       # Run place move
  178.       transfer_player
  179.       # Abort loop if transition processing
  180.       if $game_temp.transition_processing
  181.         break
  182.       end
  183.     end
  184.     # Update sprite set
  185.     @spriteset.update
  186.     # Update message window
  187.     @message_window.update
  188.     # If game over
  189.     if $game_temp.gameover
  190.       # Switch to game over screen
  191.       $scene = Scene_Gameover.new
  192.       return
  193.     end
  194.     # If returning to title screen
  195.     if $game_temp.to_title
  196.       # Change to title screen
  197.       $scene = Scene_Title.new
  198.       return
  199.     end
  200.     # If transition processing
  201.     if $game_temp.transition_processing
  202.       # Clear transition processing flag
  203.       $game_temp.transition_processing = false
  204.       # Execute transition
  205.       if $game_temp.transition_name == ""
  206.         Graphics.transition(20)
  207.       else
  208.         Graphics.transition(40, "Graphics/Transitions/" +
  209.           $game_temp.transition_name)
  210.       end
  211.     end
  212.     # If showing message window
  213.     if $game_temp.message_window_showing
  214.       return
  215.     end
  216.    
  217.     #=======================================================
  218.     # my edit
  219.     if $game_player.encounter_count == 0
  220.       unless $game_system.map_interpreter.running? or
  221.              $game_system.encounter_disabled
  222.         #
  223.         troop_id = $monster_area.get_troop
  224.         if troop_id != nil
  225.           # Set battle calling flag
  226.           $game_temp.battle_calling = true
  227.           $game_temp.battle_troop_id = troop_id
  228.           $game_temp.battle_can_escape = true
  229.           $game_temp.battle_can_lose = false
  230.           $game_temp.battle_proc = nil
  231.         end
  232.       end
  233.     end
  234.    #=======================================================
  235.     # If B button was pressed
  236.     if Input.trigger?(Input::B)
  237.       # If event is running, or menu is not forbidden
  238.       unless $game_system.map_interpreter.running? or
  239.              $game_system.menu_disabled
  240.         # Set menu calling flag or beep flag
  241.         $game_temp.menu_calling = true
  242.         $game_temp.menu_beep = true
  243.       end
  244.     end
  245.     # If debug mode is ON and F9 key was pressed
  246.     if $DEBUG and Input.press?(Input::F9)
  247.       # Set debug calling flag
  248.       $game_temp.debug_calling = true
  249.     end
  250.     # If player is not moving
  251.     unless $game_player.moving?
  252.       # Run calling of each screen
  253.       if $game_temp.battle_calling
  254.         call_battle
  255.       elsif $game_temp.shop_calling
  256.         call_shop
  257.       elsif $game_temp.name_calling
  258.         call_name
  259.       elsif $game_temp.menu_calling
  260.         call_menu
  261.       elsif $game_temp.save_calling
  262.         call_save
  263.       elsif $game_temp.debug_calling
  264.         call_debug
  265.       end
  266.     end
  267.   end
  268.  
  269. end
  270.  
Expand to see the code.



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.
@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 ^^
This script is placed above main and below all of the other scripts, right?
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.'


Image
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.

  1.  
  2. #===========================================================
  3. # ** Monster Area
  4. #-----------------------------------------------------------
  5. # by silver wind
  6. # V1.1
  7. #
  8. # This script brings back a feature from RM2K3.
  9. # It lets you define areas on the map.
  10. # each area has different monsters (encounter list).
  11. #
  12. # * Define a new area :
  13. #   use a tile with terrain tag (other than 0)
  14. #   and simply draw with it on the map.
  15. #   notes:
  16. #    - use tags 1-7. tag 0 means no encounters
  17. #    - may not work with events with a tile graphic.
  18. #    - you can use a white tile on layer 3,
  19. #      so the area is invisible.
  20. #
  21. # * Add monsters to an area :
  22. #   see 'configure' below.
  23. #   you can also use an event script command, ie:
  24. #   set Ghost to appear on tag 1& basilisks on tag 2:
  25. #   list = {1=>['Ghost*2','Gost*3'],
  26. #     2=>['Basilisk*2']
  27. #    }
  28. #   Encounters.set(list)
  29. #===========================================================
  30.  
  31. class Area
  32.   attr_reader :id
  33.   attr_accessor :monster_list
  34.  
  35.   def initialize(id,list)
  36.     @id = id
  37.     @monster_list = list
  38.   end
  39.    
  40. end
  41.  
  42. class Map_Area
  43.  
  44.   def initialize(map_id)
  45.     @id = map_id
  46.     @areas = {}
  47.   end
  48.  
  49.   # i : area id
  50.   def [](i)
  51.     # return the area's monster list
  52.     return [] if @areas[i].nil?  
  53.     @areas[i].monster_list
  54.   end
  55.  
  56.   # i : area id
  57.   def add(area)
  58.     i = area.id
  59.     @areas[i] = area
  60.   end
  61.  
  62.   def empty?(tag)
  63.     # is the troop list empty?
  64.     list = self[tag]
  65.     return list.size == 0
  66.   end
  67.  
  68.   def add_monster(area,id)
  69.     return unless legal?(area)
  70.     i = area
  71.     # make a new area
  72.     if (@areas[i])==nil
  73.       new_area = Area.new( i, [] )
  74.       add(new_area)
  75.     end
  76.     # add to area
  77.     @areas[i].monster_list << id
  78.   end
  79.  
  80.   def legal?(area)
  81.      (area >= 1 or area <= 7)
  82.   end
  83.  
  84. end
  85.  
  86. class Monster_Area
  87.  
  88.   def get_map
  89.     $game_map.map_id
  90.   end
  91.  
  92.   def initialize
  93.     @areas = {}
  94.     for i in map_list
  95.       @areas[i] = Map_Area.new(i)
  96.     end
  97.     setup
  98.   end
  99.  
  100.   def [](area_id)
  101.     # return the area's monster list
  102.     # of the current map
  103.     i = get_map
  104.     j = area_id
  105.     @areas[i][j]
  106.   end
  107.  
  108.   def map_list
  109.     maps = load_data('Data/MapInfos.rxdata')
  110.     return maps.keys
  111.   end
  112.  
  113.   # if you wish to know a map's id:
  114.   def get_map_id( name )
  115.     maps = load_data('Data/MapInfos.rxdata')
  116.     maps.each_key {|key|
  117.                     m = maps[key]
  118.                     return key if m.name == name
  119.                     }
  120.     return nil
  121.   end
  122.  
  123.   # id = the area number, which is the terain tag's number
  124.   def new_area(map, id, monsters )
  125.     ar = Area.new( id, monsters )
  126.     @areas[map].add(ar)
  127.   end
  128.  
  129.   def setup            
  130.     @data = { 0=>[],
  131. #=============================================================
  132. # Configure
  133. #-------------------------------------------------------------
  134. #
  135. #  Each line defines the areas in one map.
  136. #  genarally, use:
  137. #  [ [area001_troops], [area002_troops] (and so on)],
  138. # for example, the line [ [1,2], [3,4] ]
  139. # means troops 1&2 in area 001, 3&4 in area 002.
  140. #
  141. # * maps not set below will have no monsters in them.
  142. #-------------------------------------------------------------
  143.            # map 1
  144.            1=>[],
  145.            # map 2
  146.            2=>[]
  147.            # add more lines if you need
  148.           }
  149. #=============================================================
  150.     @data.each { |map,list|
  151.        # for each map in data
  152.        for i in 0...list.size
  153.          foes = list[i]
  154.          # i+1 gives tags 1~7
  155.          tag = i+1
  156.          new_area(map, tag, foes)
  157.        end
  158.       }
  159.  
  160.   end
  161.  
  162.   def get_troop
  163.     # get current area
  164.     area = $game_player.terrain_tag
  165.     return nil unless (area >= 0 and area <= 7)
  166.     # get monster list
  167.     list = self[area]
  168.     return nil if list.size==0    
  169.     n = rand(list.size)
  170.     troop_id = list[n]
  171.     # If troop is valid
  172.     if $data_troops[troop_id] != nil
  173.       return troop_id
  174.     end
  175.    
  176.   end
  177.  
  178.   def empty_area?                
  179.     area = $game_player.terrain_tag
  180.     return true unless (area >= 0 and area <= 7)
  181.     map = get_map
  182.     @areas[map].empty?(area)
  183.   end
  184.  
  185.   def add(area, mon)        
  186.     i = get_map
  187.     id = mon
  188.     @areas[i].add_monster(area,id)
  189.   end
  190.  
  191. end
  192.  
  193.  
  194. class Scene_Title
  195.  
  196.   alias areas_new_game command_new_game
  197.   def command_new_game
  198.     $monster_area = Monster_Area.new
  199.     areas_new_game
  200.   end
  201.  
  202.  
  203. end
  204.  
  205. class Scene_Map
  206.  
  207.   def update
  208.     # Loop
  209.     loop do
  210.       # Update map, interpreter, and player order
  211.       # (this update order is important for when conditions are fulfilled
  212.       # to run any event, and the player isn't provided the opportunity to
  213.       # move in an instant)
  214.       $game_map.update
  215.       $game_system.map_interpreter.update
  216.       $game_player.update
  217.       # Update system (timer), screen
  218.       $game_system.update
  219.       $game_screen.update
  220.       # Abort loop if player isn't place moving
  221.       unless $game_temp.player_transferring
  222.         break
  223.       end
  224.       # Run place move
  225.       transfer_player
  226.       # Abort loop if transition processing
  227.       if $game_temp.transition_processing
  228.         break
  229.       end
  230.     end
  231.     # Update sprite set
  232.     @spriteset.update
  233.     # Update message window
  234.     @message_window.update
  235.     # If game over
  236.     if $game_temp.gameover
  237.       # Switch to game over screen
  238.       $scene = Scene_Gameover.new
  239.       return
  240.     end
  241.     # If returning to title screen
  242.     if $game_temp.to_title
  243.       # Change to title screen
  244.       $scene = Scene_Title.new
  245.       return
  246.     end
  247.     # If transition processing
  248.     if $game_temp.transition_processing
  249.       # Clear transition processing flag
  250.       $game_temp.transition_processing = false
  251.       # Execute transition
  252.       if $game_temp.transition_name == ""
  253.         Graphics.transition(20)
  254.       else
  255.         Graphics.transition(40, "Graphics/Transitions/" +
  256.           $game_temp.transition_name)
  257.       end
  258.     end
  259.     # If showing message window
  260.     if $game_temp.message_window_showing
  261.       return
  262.     end
  263.    
  264.     #=======================================================
  265.     # my edit
  266.     if $game_player.encounter_count == 0
  267.       unless $game_system.map_interpreter.running? or
  268.              $game_system.encounter_disabled
  269.         #
  270.         troop_id = $monster_area.get_troop
  271.         if troop_id != nil
  272.           # Set battle calling flag
  273.           $game_temp.battle_calling = true
  274.           $game_temp.battle_troop_id = troop_id
  275.           $game_temp.battle_can_escape = true
  276.           $game_temp.battle_can_lose = false
  277.           $game_temp.battle_proc = nil
  278.         end
  279.       end
  280.     end
  281.    #=======================================================
  282.     # If B button was pressed
  283.     if Input.trigger?(Input::B)
  284.       # If event is running, or menu is not forbidden
  285.       unless $game_system.map_interpreter.running? or
  286.              $game_system.menu_disabled
  287.         # Set menu calling flag or beep flag
  288.         $game_temp.menu_calling = true
  289.         $game_temp.menu_beep = true
  290.       end
  291.     end
  292.     # If debug mode is ON and F9 key was pressed
  293.     if $DEBUG and Input.press?(Input::F9)
  294.       # Set debug calling flag
  295.       $game_temp.debug_calling = true
  296.     end
  297.     # If player is not moving
  298.     unless $game_player.moving?
  299.       # Run calling of each screen
  300.       if $game_temp.battle_calling
  301.         call_battle
  302.       elsif $game_temp.shop_calling
  303.         call_shop
  304.       elsif $game_temp.name_calling
  305.         call_name
  306.       elsif $game_temp.menu_calling
  307.         call_menu
  308.       elsif $game_temp.save_calling
  309.         call_save
  310.       elsif $game_temp.debug_calling
  311.         call_debug
  312.       end
  313.     end
  314.   end
  315.  
  316. end
  317.  
  318. #================================================
  319. #  * Encounters
  320. #================================================
  321. module Encounters
  322.   module_function
  323.  
  324.   def set(hash)
  325.     hash.each {|tag,monsters|
  326.       for name in monsters
  327.         # get id
  328.         for i in 1...$data_troops.size    
  329.           troop = $data_troops[i]
  330.           if troop.name == name
  331.             id = troop.id
  332.           end
  333.         end
  334.         return if id.nil?              
  335.         $monster_area.add(tag,id)
  336.       end
  337.      }
  338.   end
  339.  
  340. end
  341.  
Expand to see the code.

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[i].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?
kyonides wrote:
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.
I found the line I was looking for: $game_temp.battle_troop_id = troop_id
yeah, it doesn't need to touch Scene_Battle. It is finished, though I may add some features to it.
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.
  1.  
  2. #===========================================================
  3. # ** Monster Area
  4. #-----------------------------------------------------------
  5. # by silver wind
  6. # V1.2
  7. #
  8. # This script brings back a feature from RM2K3.
  9. # It lets you define areas on the map.
  10. # each area has different monsters (encounter list).
  11. #
  12. # * Define a new area :
  13. #   use a tile with terrain tag (other than 0)
  14. #   and simply draw with it on the map.
  15. #   notes:
  16. #    - use tags 1-7. tag 0 means no encounters
  17. #    - may not work with events with a tile graphic.
  18. #    - you can use a white tile on layer 3,
  19. #      so the area is invisible.
  20. #
  21. # * Define monsters in each map :
  22. #   see 'configure' below.
  23. #
  24. # * change the encouners in an area using a script :
  25. #   script call: Encounters.set(hash)
  26. #                 the hash should contain 1 or more lines of the form:
  27. #                 tag_number => list of troop names
  28. #                 ie: Encounters.set( {1=>'Ghost*2'} )
  29. #===========================================================
  30. class Area
  31.   attr_reader :id
  32.   attr_accessor :monster_list
  33.  
  34.   def initialize(id,list)
  35.     @id = id
  36.     @monster_list = list
  37.   end
  38.    
  39. end
  40.  
  41. class Map_Area
  42.  
  43.   def initialize(map_id)
  44.     @id = map_id
  45.     @areas = {}
  46.   end
  47.  
  48.   # i : area id
  49.   def [](i)
  50.     # return the area's monster list
  51.     return [] if @areas[i].nil?
  52.     @areas[i].monster_list
  53.   end
  54.  
  55.   # i : area id
  56.   def add(area)
  57.     i = area.id
  58.     @areas[i] = area
  59.   end
  60.  
  61.   def empty?(tag)
  62.     # is the troop list empty?
  63.     list = self[tag]
  64.     return list.size == 0
  65.   end
  66.  
  67.   def add_monster(area,id)
  68.     return unless legal?(area)
  69.     i = area
  70.     # make a new area
  71.     if (@areas[i])==nil
  72.       new_area = Area.new( i, [] )
  73.       add(new_area)
  74.     end
  75.     # add to area
  76.     @areas[i].monster_list << id
  77.   end
  78.  
  79.   def legal?(area)
  80.      (area >= 1 or area <= 7)
  81.   end
  82.  
  83. end
  84.  
  85. class Monster_Area
  86.  
  87.   def get_map
  88.     $game_map.map_id
  89.   end
  90.  
  91.   def initialize
  92.     @areas = {}
  93.     for i in map_list
  94.       @areas[i] = Map_Area.new(i)
  95.     end
  96.     setup
  97.   end
  98.  
  99.   def [](area_id)
  100.     # return the area's monster list
  101.     # of the current map
  102.     i = get_map
  103.     j = area_id
  104.     @areas[i][j]
  105.   end
  106.  
  107.   def map_list
  108.     maps = load_data('Data/MapInfos.rxdata')
  109.     return maps.keys
  110.   end
  111.  
  112.   # if you wish to know a map's id:
  113.   def get_map_id( name )
  114.     maps = load_data('Data/MapInfos.rxdata')
  115.     maps.each_key {|key|
  116.                     m = maps[key]
  117.                     return key if m.name == name
  118.                     }
  119.     return nil
  120.   end
  121.  
  122.   # id = the area number, which is the terain tag's number
  123.   def new_area(map, id, monsters )
  124.     ar = Area.new( id, monsters )
  125.     @areas[map].add(ar)
  126.   end
  127.  
  128.   def setup             # new, data was a normal array
  129.     @data = { 0=>[],
  130. #=============================================================
  131. # Configure
  132. #-------------------------------------------------------------
  133. #
  134. #  Each line defines the areas in one map.
  135. #  genarally, use:
  136. #  [ [area001_troops], [area002_troops] (and so on)],
  137. # for example, the line [ [1,2], [3,4] ]
  138. # means troops 1&2 in area 001, 3&4 in area 002.
  139. #
  140. # * maps not set below will have no monsters in them.
  141. # * this can be set in an event on the map.
  142. #-------------------------------------------------------------
  143.            # map 1
  144.            1=>[],
  145.            # map 2
  146.            2=>[],
  147.            
  148.            17=>[]
  149.            # add more lines if you need
  150.           }
  151. #=============================================================
  152.     @data.each { |map,list|
  153.        # for each map in data
  154.        for i in 0...list.size
  155.          foes = list[i]
  156.          # i+1 gives tags 1~7
  157.          tag = i+1
  158.          new_area(map, tag, foes)
  159.        end
  160.       }
  161.  
  162.   end
  163.  
  164.   def get_troop
  165.     # get current area
  166.     area = $game_player.terrain_tag
  167.     return nil unless (area >= 0 and area <= 7)
  168.     # get monster list
  169.     list = self[area]
  170.     return nil if list.size==0    
  171.     n = rand(list.size)
  172.     troop_id = list[n]
  173.     # If troop is valid
  174.     if $data_troops[troop_id] != nil
  175.       return troop_id
  176.     end
  177.    
  178.   end
  179.  
  180.   def empty_area?                  
  181.     area = $game_player.terrain_tag
  182.     return true unless (area >= 0 and area <= 7)
  183.     map = get_map
  184.     @areas[map].empty?(area)
  185.   end
  186.  
  187.   def add(area, troop)         # new - add encounters in an event
  188.     i = get_map
  189.     id = troop
  190.     @areas[i].add_monster(area,id)
  191.   end
  192.  
  193. end
  194.  
  195.  
  196. class Scene_Title
  197.  
  198.   alias areas_new_game command_new_game
  199.   def command_new_game
  200.     $monster_area = Monster_Area.new
  201.     areas_new_game
  202.   end
  203.  
  204.  
  205. end
  206.  
  207. class Scene_Map
  208.  
  209.   def update
  210.     # Loop
  211.     loop do
  212.       # Update map, interpreter, and player order
  213.       # (this update order is important for when conditions are fulfilled
  214.       # to run any event, and the player isn't provided the opportunity to
  215.       # move in an instant)
  216.       $game_map.update
  217.       $game_system.map_interpreter.update
  218.       $game_player.update
  219.       # Update system (timer), screen
  220.       $game_system.update
  221.       $game_screen.update
  222.       # Abort loop if player isn't place moving
  223.       unless $game_temp.player_transferring
  224.         break
  225.       end
  226.       # Run place move
  227.       transfer_player
  228.       # Abort loop if transition processing
  229.       if $game_temp.transition_processing
  230.         break
  231.       end
  232.     end
  233.     # Update sprite set
  234.     @spriteset.update
  235.     # Update message window
  236.     @message_window.update
  237.     # If game over
  238.     if $game_temp.gameover
  239.       # Switch to game over screen
  240.       $scene = Scene_Gameover.new
  241.       return
  242.     end
  243.     # If returning to title screen
  244.     if $game_temp.to_title
  245.       # Change to title screen
  246.       $scene = Scene_Title.new
  247.       return
  248.     end
  249.     # If transition processing
  250.     if $game_temp.transition_processing
  251.       # Clear transition processing flag
  252.       $game_temp.transition_processing = false
  253.       # Execute transition
  254.       if $game_temp.transition_name == ""
  255.         Graphics.transition(20)
  256.       else
  257.         Graphics.transition(40, "Graphics/Transitions/" +
  258.           $game_temp.transition_name)
  259.       end
  260.     end
  261.     # If showing message window
  262.     if $game_temp.message_window_showing
  263.       return
  264.     end
  265.    
  266.     #=======================================================
  267.     # my edit
  268.     if $game_player.encounter_count == 0
  269.       unless $game_system.map_interpreter.running? or
  270.              $game_system.encounter_disabled
  271.         #
  272.         troop_id = $monster_area.get_troop
  273.         if troop_id != nil
  274.           # Set battle calling flag
  275.           $game_temp.battle_calling = true
  276.           $game_temp.battle_troop_id = troop_id
  277.           $game_temp.battle_can_escape = true
  278.           $game_temp.battle_can_lose = false
  279.           $game_temp.battle_proc = nil
  280.         end
  281.       end
  282.     end
  283.    #=======================================================
  284.     # If B button was pressed
  285.     if Input.trigger?(Input::B)
  286.       # If event is running, or menu is not forbidden
  287.       unless $game_system.map_interpreter.running? or
  288.              $game_system.menu_disabled
  289.         # Set menu calling flag or beep flag
  290.         $game_temp.menu_calling = true
  291.         $game_temp.menu_beep = true
  292.       end
  293.     end
  294.     # If debug mode is ON and F9 key was pressed
  295.     if $DEBUG and Input.press?(Input::F9)
  296.       # Set debug calling flag
  297.       $game_temp.debug_calling = true
  298.     end
  299.     # If player is not moving
  300.     unless $game_player.moving?
  301.       # Run calling of each screen
  302.       if $game_temp.battle_calling
  303.         call_battle
  304.       elsif $game_temp.shop_calling
  305.         call_shop
  306.       elsif $game_temp.name_calling
  307.         call_name
  308.       elsif $game_temp.menu_calling
  309.         call_menu
  310.       elsif $game_temp.save_calling
  311.         call_save
  312.       elsif $game_temp.debug_calling
  313.         call_debug
  314.       end
  315.     end
  316.   end
  317.  
  318. end
  319.  
  320. #================================================
  321. #  * Encounters
  322. #================================================
  323. module Encounters
  324.   module_function
  325.  
  326.   def get_troop_id(name)
  327.     for id in (1...$data_troops.length)
  328.       troop = $data_troops[id]
  329.       return id if troop.name == name
  330.     end
  331.     return nil
  332.   end
  333.  
  334.   def set(hash)
  335.     hash.each {|tag,troop|
  336.       for name in troop
  337.         # get id
  338.         id = get_troop_id(name)
  339.         # if such troop doesn't exist
  340.         next if id.nil?
  341.         # else
  342.         $monster_area.add(tag,id)
  343.       end
  344.      }
  345.   end
  346.  
  347. end
Expand to see the code.


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

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=71675