#==============================================================================
# Encounter Area Restriction
# Version: 1.1
# Author: Modern Algebra (rmrk.net)
# Date: February 5, 2008
#------------------------------------------------------------------------------
# Instructions:
# See inside the editable region at line 48 for instructions on setting up
# the database.
# You can also add a new region at any time in the game by using this code
# in a call script:
#
# $game_system.set_area(x, y, width, height, monster troop array, map_id, name)
#
# In order to make it fit, it may be easier to first assign each value to a
# variable, like so:
#
# x = starting x coordinate for area
# y = starting y coordinate for area
# w = width of area
# h = height of area
# t = [1st troop ID, 2nd Troop ID, ...] for all troops in the area
# m = map id to set the new area in
# n = 'name' of area
# $game_system.set_area (x,y,w,h,t,m,n)
#
# If you ever want to disable or enable an area (for instance, if the player
# comes back to the map after a major event and you no longer want him to
# fight the troops held in an old array), then you can use these codes:
#
# $game_system.area(name, map ID).active = true
# $game_system.area(name, map ID).active = true
#
# If you do not specify map ID, then it will be assumed to be the map you
# are currently located on.
#==============================================================================
# *** Encounter_Regions
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Stores all pertinent data regarding encounters
#==============================================================================
module Encounter_Regions
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# * Regions
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def self.areas
@areas = []
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# * Editable Region
#----------------------------------------------------------------------
# Set initial areas here. Set them up like this:
#
# self.set_area (x, y, width, height, monster troop array, map_id, name)
#
# 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.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
self.set_area (2, 1, 26, 13, [2,3,4,5,6,7], 2, 'First Area')
self.set_area (1, 15, 33, 20,[2,3,4], 2, 'Second Area')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# * End Editable Region
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
return @areas
end
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# * Set Area
# Adds an Encounter_Area object to the areas array.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def self.set_area (x, y, width, height, array, map_id, name = '')
# Initialize array if this is the first map for which there is an area
@areas[map_id] = [] if @areas[map_id].nil?
id = @areas[map_id].size
area = Encounter_Area.new (x, y, width, height, array, map_id, name)
@areas[map_id].push (area)
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
attr_accessor :active
#----------------------------------------------------------------------
# * Initialize
# Adds a Game_Area object to the array.
#----------------------------------------------------------------------
def initialize (x, y, width, height, array, map_id, name)
@rect = Rect.new (x, y, width, height)
@monster_troops = array
@name = name
@map_id = map_id
@active = true
end
#----------------------------------------------------------------------
# * Within?
# Checks if the position specified is within the area
#----------------------------------------------------------------------
def within? (x, y)
return x.between? (@rect.x, @rect.x + @rect.width - 1) &&
y.between? (@rect.y, @rect.y + @rect.height - 1)
end
#----------------------------------------------------------------------
# * Equals?
# Determine if two Encounter_Area objects are equal
#----------------------------------------------------------------------
def == (other)
return other.map_id == @map_id && other.rect == @rect
end
end
end
#==============================================================================
# ** Game_System
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Summary of Changes:
# aliased methods - initialize
# new methods - set_area, update_regions
# new instance variables - regions
#==============================================================================
class Game_System
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# * Public Instance Variables
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
attr_reader :regions # Contains area data
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# * Object Initialization
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
alias ma_encounter_areas_init initialize
def initialize
ma_encounter_areas_init
@regions = []
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_Regions::Encounter_Area.new (x, y, width, height, array, map_id, name)
# If first area in the map, initialize array
@regions[map_id] = [] if @regions[map_id].nil?
@regions[map_id].push (area) unless @regions[map_id].include? (area)
end
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# * Update Areas
# Adds an Encounter_Area object to the areas array.
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def update_regions
# For all areas in the database
Encounter_Regions.areas.each {|regions|
next if regions.nil?
regions.each {|area|
# Initialize the array if this is the first area to be set to that map
@regions[area.map_id] = [] if @regions[area.map_id].nil?
# Add the area to the regions if it does not already exist
@regions[area.map_id].push (area) unless @regions.include? (area)
}
}
end
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# * Retrieve Area by name and map_id
# map_id : the ID of the map
# name : the name of the area you want
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def area (name = '', map_id = $game_map.map_id)
@regions[map_id].each {|i| return i if i.name == name}
return false
end
end
#==============================================================================
# ** Game_Map
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Summary of Changes:
# aliased methods - encounter_list
#==============================================================================
class Game_Map
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# * Encounter List
# Returns the encounter list, with modifications based on level
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
alias ma_encounter_areas_encounter_list encounter_list
def encounter_list
# Run original method
encounters = ma_encounter_areas_encounter_list
return encounters if $game_system.regions[@map_id].nil?
# Add monsters from each area the player is within
$game_system.regions[@map_id].each { |area|
next unless area.active
# Add all unique elements from the area to the encounter_list
encounters |= area.monster_troops if area.within? ($game_player.x, $game_player.y)
}
return encounters
end
end
#======================================================================
# ** Scene_Title
#------------------------------------------------------------------------------
# Summary of Changes:
# aliased method - main
#======================================================================
class Scene_Title
alias ma_encounter_regions_main_update main
def main
# Run original method
ma_encounter_regions_main_update
# Merge new database entries with the game areas
$game_system.update_regions
end
end