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.

Modern Algebra's Encounter Range Script

Okay awhile ago I found this script on a blog somewhere (Honestly, I can't recall it was a random Google search) and have since tried to get it to work so that my game's party doesn't get wiped out on the world map when they first begin, but then aren't bored with how easy it all is later on in the game.  The only problem is I can't figure out how the script works.  So I come and ask if anyone here knows a way or knows of a different script that is possibly easier to understand/implement.

Script:

Code:
#==============================================================================
# Encounter Range Script 
# Version: 2.5b
# Author: Modern Algebra
# Date: August 25, 2007
#------------------------------------------------------------------------------
#  Instructions:
#  Set up the regular map encounter list with all of the global (appear all over
#  the map, regardless of area). Then you can set up areas in one of two ways:
#  1) Upon initialization. If you read down further in the Editable Region of  
#     the Data_Areas class, detailed instructions on how to do this are there. 
#  2) In the map. You can add an area to the database very easily. This is 
#     useful for two reasons. One, you can look at the map that you are setting
#     areas for, and two, you can set new areas once an event in the game occurs
#     (like a volcano or something erupting). All you need to do is use the 
#     Script event command and enter this code:
#
#       range = $game_system.encounter_restrictions
#       range.set_area (x, y, width, height, troops array)
#
#     Unlike setting it to the database, you do not need to set a map id unless
#     you want the area to be set in a map different from the one you are in. 
#     For more details on what that stuff means, look below in the Editable 
#     Region of the Data_Areas class.
#------------------------------------------------------------------------------
#  Compatibility:
#  It ought to be compatible with any script that does not modify the 
#  encounter_list method of Game_Map (and I can't think of any script that would
#  want to besides one like this). It will not initialize for old savefiles, 
#  and this WILL cause problems. You must initialize this manually for old 
#  savefiles.
#==============================================================================

class Encounter_Restrictions
  #------------------------------------------------------------------------
  # * Public Instance Variables
  #------------------------------------------------------------------------
  attr_reader   :troop_levels  # User-edited array containing troop levels
  attr_reader   :areas         # Contains area data
  attr_accessor :level_boolean # Turns on and off the level range function
  attr_accessor :min_level     # The lower limit of the encounter range
  attr_accessor :max_level     # The upper limit of the encounter range
  #------------------------------------------------------------------------
  # * Object Initialization
  #------------------------------------------------------------------------
  def initialize
    #----------------------------------------------------------------------
    # * Editable Region
    #----------------------------------------------------------------------
    #  Each number in @troop_levels corresponds to a troop in the database,
    #  and it represents the level of that troop. Thus, if you want the 1st
    #  and 2nd troops to be a level 2 battle, then in those slots, you would 
    #  put a 2, and if you wanted the 3rd troop in the database to be a level 
    #  10 fight, then in the 3rd slot you would put a 10. The array would look
    #  like this: 
    #
    #     @troop_levels = [2,2,10,etc...]
    #  
    #  You MUST give each troop in the database a level, at least all the ones
    #  you intend to have as random encounters at any point in the game
    #
    #  As for @min_level and @max_level, these values define the range of 
    #  encounters, by level. For instance, if @min_level = -5, then the party
    #  can't encounter any troops that are more than 5 levels below the party
    #  average. If @max_level = 10, then the party can't encounter any troops 
    #  that are more than 10 levels above the party average.
    #  To change these values in-game (in order to make certain maps harder
    #  or easier), just use these codes:
    #
    #     $game_system.encounter_restrictions.min_level = x 
    #     $game_system.encounter_restrictions.max_level = x 
    #
    #  Where x is the value you are changing it to.
    #
    #  You can also turn the levelling feature on and off at will, using 
    #  this code:
    #
    #  $game_system.encounter_restrictions.level_boolean = true (to turn it on)
    #  $game_system.encounter_restrictions.level_boolean = false (to turn it off) 
    #-----------------------------------------------------------------------
    @troop_levels = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,
                     23,24,25,26,27,28,29,30,31,32,15,40,10,15,10,15]
    @min_level = -5
    @max_level = 10
    @level_boolean = true
    #-----------------------------------------------------------------------
    # * END Editable Region
    #-----------------------------------------------------------------------
    @troop_levels.unshift (nil)
    @areas = []
  end
  #-------------------------------------------------------------------------
  # * Set Area
  #     Adds an Encounter_Area object to the areas array.
  #-------------------------------------------------------------------------
  def set_area (x, y, width, height, array, name, map_id = $game_map.map_id)
    area = Encounter_Area.new
    area.set_area (x, y, width, height, array, name, map_id)
    if @areas[map_id] == nil
      @areas[map_id] = []
    end
    @areas[map_id].push (area)
  end
  #-------------------------------------------------------------------------
  # * Update Areas
  #     Adds an Encounter_Area object to the areas array.
  #-------------------------------------------------------------------------
  def update_areas 
    # Set variable to the database object
    data_areas = Data_Areas.new
    # For all areas in the database
    for area in data_areas.areas
      # If this is the first area to be set to that map
      if @areas[area.map_id] == nil
        # Initialize the array
        @areas[area.map_id] = []
      end
      # Set a test variable
      check = false
      # For all areas in the map of that database
      for area2 in @areas[area.map_id]
        # Check if the area is already contained within the @areas array
        check = area2.equals? (area)
        # If so, break the loop
        if check == true
          break
        end
      end
      # If the area is not in the @areas array
      if check == false
        # Add the area
        @areas[area.map_id].push (area)
      end
    end
  end
end 
#==============================================================================
# ** Areas Database
#------------------------------------------------------------------------------
#  The object which represents an area
#==============================================================================

class Data_Areas
  #------------------------------------------------------------------------
  # * Public Instance Variables
  #------------------------------------------------------------------------
  attr_reader    :areas
  #------------------------------------------------------------------------
  # * Object Initialization
  #------------------------------------------------------------------------
  def initialize
    @areas = []
    #----------------------------------------------------------------------
    # * Editable Region
    #----------------------------------------------------------------------
    # Set initial areas here. Set them up like this:
    #
    #    set_area (x, y, width, height, monster troop array, name, map_id)
    #
    # In the 1st example area below, we have defined an area at x = 10, y = 6, 
    # width = 4, height = 5, in the 1st map with monsters 2, 3, and 8 appearing
    # in the area. Essentially, this means that on the first map, you can 
    # encounter monster troops 2, 3 and 8 if the player coordinates are within
    # (10-13, 6-10). The name is 'Lies'. If you do not want to give the area
    # a name, then merely type in nil.
    #
    # You can set as many areas as you like for as many maps as you like.
    #-----------------------------------------------------------------------
    set_area (3,4,4,7,[1,33,34],'Graveyard',2)
    set_area (13,4,4,7,[37,38],'Farm',2)
    #----------------------------------------------------------------------
    # * End Editable Region
    #----------------------------------------------------------------------
  end
  #-------------------------------------------------------------------------
  # * Set Area
  #     Adds an Encounter_Area object to the areas array.
  #-------------------------------------------------------------------------
  def set_area (x, y, width, height, array, name, map_id)
    area = Encounter_Area.new
    area.set_area (x, y, width, height, array, name, map_id)
    @areas.push (area)
  end
end

#==============================================================================
# ** Encounter Area 
#------------------------------------------------------------------------------
#  The object which represents an area
#==============================================================================

class Encounter_Area
  #------------------------------------------------------------------------
  # * Public Instance Variables
  #------------------------------------------------------------------------
  attr_reader    :map_id
  attr_reader    :rect
  attr_reader    :monster_troops # The monsters within the troop
  attr_reader    :name
  #-------------------------------------------------------------------------
  # * Set Area
  #     Adds a Game_Area object to the array.
  #-------------------------------------------------------------------------
  def set_area (x, y, width, height, array, name, map_id = $game_map.map_id)
    @rect = Rect.new (x, y, width, height)
    @monster_troops = array
    @name = name
    @map_id = map_id
  end
  #-------------------------------------------------------------------------
  # * Within?
  #     Checks if the position specified is within the area
  #-------------------------------------------------------------------------
  def within? (x, y)
    if x.between? (@rect.x, @rect.x + @rect.width - 1) && y.between? (@rect.y, @rect.y + @rect.height - 1)
      return true
    else
      return false
    end
  end
  #-------------------------------------------------------------------------
  # * Equals?
  #     Determine if two Encounter_Area objects are equal
  #-------------------------------------------------------------------------
  def equals? (other)
    if other.rect == @rect && other.map_id == @map_id
      return true
    else
      return false
    end
  end
end

#==============================================================================
# ** Game_Map
#==============================================================================

class Game_Map
  #-------------------------------------------------------------------------
  # * Get Area Encounter List
  #       Returns the encounter list, with modifications based on area
  #-------------------------------------------------------------------------
  def get_area_encounters
    encounter_list = @map.encounter_list.dup
    range = $game_system.encounter_restrictions
    # Add monsters from each area the player is within
    for area in range.areas[@map_id] # Areas
      # If the player is, in fact, within the area
      if area.within? ($game_player.x, $game_player.y)
        # Add all unique elements from the area to the encounter_list 
        encounter_list |= area.monster_troops
      end
    end
    return encounter_list
  end
  #-------------------------------------------------------------------------
  # * Encounter List
  #       Returns the encounter list, with modifications based on level
  #-------------------------------------------------------------------------
  def encounter_list
    range = $game_system.encounter_restrictions
    true_encounter_list = get_area_encounters
    # If level range is being taken into account
    if range.level_boolean == true
      encounter_list = []
      # Calculate Party Level Average
      average_level = 0
      for actor in $game_party.actors
        average_level += actor.level
      end
      average_level /= $game_party.actors.size
      # Test average agains all troops in the list
      for i in 0...true_encounter_list.size
        troop_level = range.troop_levels[true_encounter_list[i]]
        # Set Range
        minimum = [average_level + range.min_level,1].max
        maximum = [average_level + range.max_level,99].min
        # If actor level within range
        if troop_level.between? (minimum, maximum)
          # Add to the returning array
          encounter_list.push (true_encounter_list[i])
        end
      end
    else
      # Return the full encounter list, taking into account areas
      encounter_list = true_encounter_list
    end
    return encounter_list
  end
end

#==============================================================================
# ** Scene_Title
#------------------------------------------------------------------------------
#  Aliases command_new_game method to initialize $game_system.encounter_restrictions
#  and aliases main method to initialize $data_areas
#==============================================================================

class Scene_Title
  alias encounter_restrictions_update main
  def main
    # Run original method
    encounter_restrictions_update
    # Merge the database areas with the game areas
    $game_system.encounter_restrictions.update_areas 
  end
end

#==============================================================================
# ** Game_System (Encounter Restrictions)
#------------------------------------------------------------------------------
# Adds an instance variable for access to the Script
#==============================================================================

class Game_System
  #------------------------------------------------------------------------
  # * Public Instance Variables
  #------------------------------------------------------------------------
  attr_reader   :encounter_restrictions
  #------------------------------------------------------------------------
  # * Object Initialization
  #------------------------------------------------------------------------
  alias encounter_restrictions_initialize initialize
  def initialize
    encounter_restrictions_initialize
    @encounter_restrictions = Encounter_Restrictions.new
  end
end

Edit:  If I used the wrong forum section I'm sorry.  I wasn't sure if this could be considered a request or help ... sorry, if I am wrong then I will willing accept any repercussion of this action.
 

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