Area Support for RPG maker XP

This script implements Areas (or Regions) in RPG maker XP, so they work the same way as in more modern RPG maker versions.
They work in parallel with terrain tags without changing anything on them, and the way to check them via script is also very similar.

To set them, execute your game in Debug mode and press F8. The option of placing the areas directly along the map using the mouse will be activated. To change the area index, press Q / W (or Page Down / Page Up) and use the directional arrows to scroll across the map. The changes will be saved in real time.

Spoiler: Side notes
If you're using the Grid-Free Doodads script I made some years ago, it has Area Support already integrated.

If you are using another Mouse script in your game, you can remove the Glitchfinder Key and Mouse and use the one you are already using (the methods in the Setup Mode script will have to be adapted).

Author: Wecoc
First Release: September 2016
Last Version: October 2018

Credits
  • Wecoc (Area implementation)
  • Glitchfinder (Keyboard & Mouse modules)

Terms of use
- Please give credits to everyone on the list above.
- You can repost this code.
- You can edit this code freely, and distribute the edits as well.
- This code can be used on commercial games.

Spoiler: Screen
area_screen.png

Instructions
Download the demo and copy the four scripts on your game: Key Input, Mouse, Area Base and Area Setup.
There's not much to it apart from that.

You can define areas that are always passable or non-passable on the start of Area Base, and you can change the debug button (F8) on Area Setup.

Demo: Area Support 1.1.zip

Spoiler: Script calls
Check the area on a map coordinate
$game_map.area(X, Y)

Set the area of a map coordinate by event
$game_map.set_area(X, Y, Area ID)

Get the current area of the player or an event
$game_player.area
$game_map.events[
Event ID].area

This script also has some script addons available. I'll include a few below.

Spoiler: Addon - Event Area Restrictions
This little script allows to make some areas passable or non-passable for certain events or the player.

Ruby:
#==============================================================================
# ** Event Area Restrictions
#==============================================================================

class Game_Character
 
  attr_accessor :passable_areas
  attr_accessor :no_passable_areas
 
  alias areas_pass_ini initialize unless $@
  def initialize(*args)
    areas_pass_ini(*args)
    @passable_areas = {}
    @no_passable_areas = {}
    @passable_areas.default = false
    @no_passable_areas.default = false
  end
 
  def set_passable(area, value=true)
    if value == true
      @passable_areas[area] = true
      @no_passable_areas[area] = false
    else
      @passable_areas[area] = false
    end
    return true
  end
 
  def set_no_passable(area, value=true)
    if value == true
      @no_passable_areas[area] = true
      @passable_areas[area] = false
    else
      @no_passable_areas[area] = false
    end
    return true
  end
 
  def clear_passable(area)
    set_passable(area, false)
  end
 
  def clear_no_passable(area)
    set_no_passable(area, false)
  end
 
  alias areas_pass? passable? unless $@
  def passable?(x, y, d)
    new_x = x + (d == 6 ? 1 : d == 4 ? -1 : 0)
    new_y = y + (d == 2 ? 1 : d == 8 ? -1 : 0)
    return false if @no_passable_areas[self.area] == true
    return false if @no_passable_areas[$game_map.area(new_x, new_y)] == true
    return false unless $game_map.valid?(new_x, new_y)
    return true if @passable_areas[$game_map.area(new_x, new_y)] == true
    areas_pass?(x, y, d)
  end
end

For example:
$game_map.events[Event ID].set_passable(Area ID)
$game_player.set_passable(
Area ID)

You can use set_passable, set_no_passable, clear_passable and clear_no_passable.

Spoiler: Addon - Area Helper
This little script allows to define some descriptions on certain areas that will display when setting them, so you won't have to remember which IDs you had to use.

Ruby:
#==============================================================================
# ** [XP] Area Helper
#------------------------------------------------------------------------------
#  Author: Wecoc
#==============================================================================

module Setup
  AREA_HELPER = {1 => "Example", 10 => "Passable", 11 => "No passable"}
  AREA_HELPER.default = ""
end

class Scene_Map
  alias area_helper_call_setup call_setup unless $@
  def call_setup
    @helper_window = Window_Base.new(160, 0, 480, 64)
    @helper_window.back_opacity = 160
    width = @helper_window.width - 32
    height = @helper_window.height - 32
    @helper_window.contents = Bitmap.new(width, height)
    @helper_window.visible = false
    area_helper_call_setup
  end
 
  alias area_helper_refresh_area refresh_area unless $@
  def refresh_area
    area_helper_refresh_area
    @helper_window.contents.clear
    text = Setup::AREA_HELPER[@area_id]
    @helper_window.contents.draw_text(4, 0, 480 - 32, 32, text)
  end
 
  alias area_helper_update_setup update_setup unless $@
  def update_setup
    area_helper_update_setup
    @helper_window.update
    @helper_window.visible = @area_window.visible
  end
 
  alias area_helper_exit_setup exit_setup unless $@
  def exit_setup
    area_helper_exit_setup
    @helper_window.dispose
  end
end
Sorry about this, but I bump this very old topic with a tutorial on how to place events on the map randomly, but by Areas using this script. I'll comment briefly another way to do that as well.

Sometimes it can be somewhat annoying that when exiting a map and entering later, the positions of events that move randomly are reset each time. The most common solution is to use a script like Game Map Event Position or Remember Events Position, so that the positions of the events are saved when exiting the map, and when re-entering they remain the same.

But today I come to talk about a different alternative; scripting to place certain events at random positions along the map. Assigning a random position to an interactive event does not mean placing it at random X and Y on the map. The position must have two basic characteristics; that is passable for the event and that the character can access it.

Here are the two ways explained with more detail.

Spoiler: Pathfinding
You can make sure the event is accessible using a simple pathfinding script from the player on the entering position to the event.

So on a possible range of X, Y across the map we:
1- Set X and Y randomly
2- Check that coordinate is passable
3- Use pathfinding to make sure there's a possible path from the event to the player.
4- If those conditions are applied move the event to that position, if not repeat from step 1.

Ruby:
#==============================================================================
# ** [XP] Wecoc's Rand Event Position (Pathfinding)
#------------------------------------------------------------------------------
#  Author: Wecoc (no credits required)
#==============================================================================

class Interpreter
  #===========================================================================
  # * Basic Pathfinding Formula
  #---------------------------------------------------------------------------
  # event - RPG::Event
  # tx    - Target X
  # ty    - Target Y
  #===========================================================================
  def find_path(event, tx, ty)
    x, y = event.x, event.y
    path = []
    max_elements = $game_map.width * $game_map.height
    coords = Table.new($game_map.width, $game_map.height)
    coords[x, y] = 1
    n = 0
    loop do
      n += 1
      return [] if n >= max_elements
      dx = tx - x
      dy = ty - y
      if dx.abs > dy.abs
        if dx > 0
          r = (dy > 0) ? [6, 2] : [6, 8]
        else
          r = (dy > 0) ? [4, 2] : [4, 8]
        end
      else
        if dy > 0
          r = (dx > 0) ? [2, 6] : [2, 4]
        else
          r = (dx > 0) ? [8, 6] : [8, 4]
        end
      end
      r << 10 - r[1]
      r << 10 - r[0]
      nx = nil ; ny = nil
      for d in r
        new_x = x + (d == 6 ? 1 : 0) - (d == 4 ? 1 : 0)
        new_y = y + (d == 2 ? 1 : 0) - (d == 8 ? 1 : 0)
        next if !$game_map.passable?(new_x, new_y, d)
        next if coords[new_x, new_y] != 0
        nx = new_x ; ny = new_y
        break
      end
      return [] if nx == nil or ny == nil
      x = nx ; y = ny
      coords[x, y] = 1
      path << [x, y]
      break if x == tx and y == ty
    end
    return path
  end
  #===========================================================================
  # * Teleport Event to a new position
  #---------------------------------------------------------------------------
  # rand_event_position(event[, sx, sy, width, height, px, py])
  #---------------------------------------------------------------------------
  # event  - RPG::Event // Event ID
  # sx     - Range X [Default: 0]
  # sy     - Range Y [Default: 0]
  # width  - Range WIDTH  [Default: Map WIDTH]
  # height - Range HEIGHT [Default: Map HEIGHT]
  # px     - Pathfinding Target X [Default: Player X]
  # py     - Pathfinding Target Y [Default: Player Y]
  #===========================================================================
  def rand_event_position(*args)
    event = args[0]
    event = $game_map.events[args[0]] if event.is_a?(Integer)
    sx = args[1] ; sx = 0 if sx == nil
    sy = args[2] ; sy = 0 if sy == nil
    width  = args[3] ; width  = $game_map.width  if width == nil
    height = args[4] ; height = $game_map.height if height == nil
    px = args[5] ; px = $game_player.x if px == nil
    py = args[6] ; py = $game_player.y if py == nil
    n = 0
    loop do
      n += 1
      next if n > (width * height)
      new_x = sx + rand(width)
      new_y = sy + rand(height)
      next if !event.passable?(new_x, new_y, 0)
      event.moveto(new_x, new_y)
      if self.respond_to?(:find_path)
        path = find_path(event, px, py)
        next if path.size == 0
      end
      break
    end
    return true
  end
end

To call this use:
rand_event_position(Event ID)

You can also define the range, thus limiting the events to squared areas:
rand_event_position(Event ID, X, Y, Width, Height)

You can also make the target be something else than the player:
rand_event_position(Event ID, X, Y, Width, Height, Target X, Target Y)

You simply use this on a single Parallel Process with each event you want to place, and then use Erase Event Temporarily. That's it!

Spoiler: Areas
This is the more precise way; generate them inside specific areas.
Right below the Area Support script, you will need this little Addon:

Ruby:
#==============================================================================
# ** [XP] Wecoc's Rand Event Position (Area)
#------------------------------------------------------------------------------
#  Author: Wecoc (no credits required)
#==============================================================================

class Interpreter
  #===========================================================================
  # * Teleport Event to a new position
  #---------------------------------------------------------------------------
  # rand_event_area(event, area[, sx, sy, width, height])
  #---------------------------------------------------------------------------
  # event  - RPG::Event // Event ID
  # area   - Area ID
  # sx     - Range X [Default: 0]
  # sy     - Range Y [Default: 0]
  # width  - Range WIDTH  [Default: Map WIDTH]
  # height - Range HEIGHT [Default: Map HEIGHT]
  #===========================================================================
  def rand_event_area(event, area, *args)
    event = $game_map.events[event] if event.is_a?(Integer)
    sx = args[0] ; sx = 0 if sx == nil
    sy = args[1] ; sy = 0 if sy == nil
    width  = args[2] ; width  = $game_map.width  if width == nil
    height = args[3] ; height = $game_map.height if height == nil
    n = 0
    loop do
      n += 1
      next if n > (width * height)
      new_x = sx + rand(width)
      new_y = sy + rand(height)
      next if !($game_map.area(new_x, new_y) == area)
      next if !event.passable?(new_x, new_y, 0)
      event.moveto(new_x, new_y)
      break
    end
    return true
  end
end

The callers are:
rand_event_area(Event ID, Area ID)

You can also limit it onto a squared region apart from the Area tag:
rand_event_area(Event ID, Area ID, X, Y, Width, Height)

You simply use this on a single Parallel Process with each event you want to place, and then use Erase Event Temporarily. That's it! I personally recommend this solution when possible.

Do you want to use this to define random battle encounters on the map by invisible events, but aren't sure how to do that? Then check this.

I hope this helps someone :awink:

Original thread: https://forums.rpgmakerweb.com/threads/area-support-for-rpg-maker-xp.139673/