08-25-2007, 01:00 PM
(This post was last modified: 06-20-2017, 06:50 AM by DerVVulfman.)
Encounter Range Script
Version: 2.5b
Author: modern algebra
Date: August 25, 2007
Version: 2.5b
Author: modern algebra
Date: August 25, 2007
This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given. If you are the owner of the thread, please contact administration.
No support is given. If you are the owner of the thread, please contact administration.
Version History
- Version 2.5b: Just modified the way areas are saved to save space. Also removed the use of any global variables
- Version 2.5 : Allow the user to define areas within a map, and set distinct monsters for each area
- Version 2.0 : Rewrote the script for better compatibility, optimization, as well making it easier to implement
- Version 1.1 : Minor bug fixes
- Version 1.0 : Original Script
Planned Future Versions
- Version 3.0: Make monster troops level with the party so that they are always at the same difficulty
Description
This script allows the game maker to give monster troops levels, and then based on those levels, it determines what enemy troops can be attacked by the player at his current level. That is kind of confusing, so I will try to explain a little better by example. The maker can set up a map with possible enemies being Angels, Cherubims, and Seraphims. You can enter this map at any time, and so you want the encounters to reflect what level the hero party is. You don't want the party attacking Seraphims when they are level 1, because they will be crushed, and you don't want them to fight Angels when they are level 99, because it's too easy. Thus, you just set up in the script what levels the enemy troops are, and with this script you will only be able to fight monster troopss that are within level range of your heroes. You can also set encounter areas. What this means is you can easily set it so that different areas of the map have different enemies. This is especially useful for world maps and the like.
Features
- Allows you to set the levels for each monster troop in the database
- Only allows the player to fight monster troops within his level range
- Allows you to set what the range is for every map
- Allows you to set encounter areas, and only in those areas can specified monsters attack you.
- Allows you to control whether level effects encounters for each map
Instructions
See Script for instructions. They are located in the header and in the Editable Region (where you set up troop levels)
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
Credit
- modern algebra
Support
I will fix all bugs that arise with the script, as well as make it compatible with other scripts if any compatibility issues arise. I will provide support on RMRK, and anywhere else I post this, but I will most swiftly respond if you post your question on RMRK.
Known Compatibility Issues
It may be incompatible with other scripts which restrict encounters, since it overwrites the encounter_list method of Game_Map. It's easy to make compatible though, and so if you like, just post the other script and I will do it for you.
Author's Notes
This script is posted at RMRK, RMRevolution, RM Memories, and Creation Asylum. If you see it anywhere else, please notify me. If you want to post it somewhere else, just ask :)