Advertisement
Loque

Random event position

Jun 18th, 2014
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 9.25 KB | None | 0 0
  1. =begin
  2. #===============================================================================
  3.  Title: Random Event Positions
  4.  Author: Hime
  5.  Date: Apr 6, 2014
  6. --------------------------------------------------------------------------------
  7.  ** Change log
  8.  Apr 6, 2014
  9.    - fixed bug where "page" randomization was not working
  10.  Aug 12, 2013
  11.    - fixed regex
  12.    - added "start" randomization type.
  13.  Aug 9, 2013
  14.    - added support for variable region ID's
  15.  Jul 20, 2013
  16.    - fixed issue where events could overlap each other
  17.  May 3, 2013
  18.    - fixed issue where game crashed if no page selected
  19.  Apr 28, 2013
  20.    - added option for determining when the event position is randomized
  21.  Apr 25, 2013
  22.    - added global disable switch
  23.    - fixed bug where event was assigned to a region that was not on the map
  24.    - initial release
  25. --------------------------------------------------------------------------------  
  26.  ** Terms of Use
  27.  * Free to use in non-commercial projects
  28.  * Contact me for commercial use
  29.  * No real support. The script is provided as-is
  30.  * Will do bug fixes, but no compatibility patches
  31.  * Features may be requested but no guarantees, especially if it is non-trivial
  32.  * Credits to Hime Works in your project
  33.  * Preserve this header
  34. --------------------------------------------------------------------------------
  35.  ** Description
  36.  
  37.  This script allows you to randomize an event's position when you enter the
  38.  map. Regions are used to designate the tiles where an event can appear.
  39.  
  40. --------------------------------------------------------------------------------
  41.  ** Installation
  42.  
  43.  Place this script below Materials and above Main
  44.  
  45. --------------------------------------------------------------------------------
  46.  ** Usage
  47.  
  48.  To designate an event page where the event's position is randomized, create
  49.  a comment and write
  50.  
  51.    <random position region: x type>
  52.    
  53.  For some region ID x
  54.  The region ID can be a fixed number such as 1 or 2, or it can be a reference
  55.  to a game variable such as v[2] which will take the value of variable 2.
  56.  
  57.  The type determines when position randomization occurs
  58.  
  59.    start - only once in the game: the first time the event is loaded
  60.    init - whenever the event is created (eg: map loading)
  61.    page - whenever you change to that page
  62.  
  63.  If that page is active when the map is loaded, then the event will be randomly
  64.  moved to a tile in that region.
  65.  
  66.  This is applied to events on a per-page basis, so you may need to add the
  67.  comment to multiple pages.
  68.  
  69.  In the configuration there is a switch that will allow you to disable
  70.  random event positioning if the switch is ON.
  71.  
  72.  Note that the "start" type does not memorize the event's location, so you will
  73.  need a different script for that.
  74.  
  75. #===============================================================================
  76. =end
  77. $imported = {} if $imported.nil?
  78. $imported["TH_RandomEventPositions"] = true
  79. #===============================================================================
  80. # ** Configuration
  81. #===============================================================================
  82. module TH
  83.   module Random_Event_Positions
  84.    
  85.     # Global switch that determines whether positions should be randomized
  86.     # If it's ON, then positions will not be randomized
  87.     Disable_Switch = 928
  88.    
  89.     # type of randomization by default
  90.     Default_Type = :init
  91.    
  92.     Regex = /<random[-_ ]position[-_ ]region:\s*(v\[\d+\]|\d+)\s*(\w+)?>/i
  93.   end
  94. end
  95. #===============================================================================
  96. # ** Rest of Script
  97. #===============================================================================
  98. module RPG
  99.   class Event::Page
  100.    
  101.     #---------------------------------------------------------------------------
  102.     # Returns true if event's position should be randomized on this page.
  103.     # True only if random position region > 0
  104.     #---------------------------------------------------------------------------
  105.     def random_position?
  106.       return @is_random_position unless @is_random_position.nil?
  107.       load_notetag_random_event_positions
  108.       return @is_random_position
  109.     end
  110.    
  111.     #---------------------------------------------------------------------------
  112.     # Determines when the position randomization will occur
  113.     #---------------------------------------------------------------------------
  114.     def random_position_type
  115.       return @random_position_type unless @random_position_type.nil?
  116.       load_notetag_random_event_positions
  117.       return @random_position_type
  118.     end
  119.    
  120.     #---------------------------------------------------------------------------
  121.     # Returns the random position region for this event page
  122.     #---------------------------------------------------------------------------
  123.     def random_position_region
  124.       return eval_random_position_region unless @random_position_region.nil?
  125.       load_notetag_random_event_positions
  126.       return eval_random_position_region
  127.     end
  128.    
  129.     def eval_random_position_region(v=$game_variables, s=$game_switches)
  130.       eval(@random_position_region)
  131.     end
  132.    
  133.     #---------------------------------------------------------------------------
  134.     # Parse event page commands looking for the required comment
  135.     #---------------------------------------------------------------------------
  136.     def load_notetag_random_event_positions
  137.       @is_random_position = false
  138.       @random_position_region = "0"
  139.       @random_position_type = TH::Random_Event_Positions::Default_Type
  140.       @list.each do |cmd|
  141.         if cmd.code == 108 && cmd.parameters[0] =~ TH::Random_Event_Positions::Regex
  142.           @random_position_region = $1
  143.           @random_position_type = $2.to_sym unless $2.nil?
  144.           @is_random_position = @random_position_region != 0
  145.           break
  146.         end
  147.       end
  148.     end
  149.   end
  150. end
  151.  
  152. class Game_System
  153.  
  154.   alias :th_random_event_positions :initialize
  155.   def initialize
  156.     th_random_event_positions
  157.     @random_position_event = {}
  158.   end
  159.  
  160.   def random_position_event
  161.     @random_position_events ||= {}
  162.   end
  163. end
  164.  
  165. class Game_Map
  166.  
  167.   attr_reader :region_tile_mapping
  168.  
  169.   #-----------------------------------------------------------------------------
  170.   # Setup the region tile mapping before creating events
  171.   #-----------------------------------------------------------------------------
  172.   alias :th_random_event_positions_setup_events :setup_events
  173.   def setup_events
  174.     setup_region_tile_mapping
  175.     th_random_event_positions_setup_events
  176.   end
  177.  
  178.   #-----------------------------------------------------------------------------
  179.   # Sets up a hash, where the keys are region ID's and values are arrays of
  180.   # positions stored as [x, y]. This is to cache the tiles based on their
  181.   # regions. Assumes regions do not change after the map is loaded.
  182.   #-----------------------------------------------------------------------------
  183.   def setup_region_tile_mapping
  184.     @region_tile_mapping = {}
  185.     (0..63).each {|i| @region_tile_mapping[i] = []}
  186.     for x in 0..data.xsize
  187.       for y in 0..data.ysize
  188.         @region_tile_mapping[region_id(x, y)] << [x,y]
  189.       end
  190.     end
  191.   end
  192. end
  193.  
  194. class Game_Event < Game_Character
  195.  
  196.   #-----------------------------------------------------------------------------
  197.   # Move the event to a new position if necessary
  198.   #-----------------------------------------------------------------------------
  199.   alias :th_random_event_positions_initialize :initialize
  200.   def initialize(map_id, event)
  201.     th_random_event_positions_initialize(map_id, event)
  202.     randomize_position
  203.   end
  204.  
  205.   #-----------------------------------------------------------------------------
  206.   # Picks a random tile based on the event's random position region.
  207.   # Seed is randomized so that events with the same position region don't
  208.   # all appear on the same tiles
  209.   #-----------------------------------------------------------------------------
  210.   def randomize_position
  211.     return if @page.nil?
  212.     return if $game_switches[TH::Random_Event_Positions::Disable_Switch] || !@page.random_position?
  213.     # if randomize type is ":start", check if it's already been visited
  214.     # else, check if randomize type is ":init"
  215.     if @page.random_position_type == :start
  216.       return if $game_system.random_position_event[[@map_id, @id]]
  217.     elsif @page.random_position_type == :init
  218.       return if @position_randomized
  219.     end
  220.     @position_randomized = true
  221.    
  222.     srand
  223.     $game_system.random_position_event[[@map_id, @id]] = true
  224.     begin
  225.       pos = get_random_position
  226.       return unless pos
  227.     end while !$game_map.events_xy(pos[0], pos[1]).empty?
  228.    
  229.     moveto(pos[0], pos[1])
  230.   end
  231.  
  232.   #-----------------------------------------------------------------------------
  233.   # Pick a random tile and remove it from the list of available positions
  234.   #-----------------------------------------------------------------------------
  235.   def get_random_position
  236.     arr = $game_map.region_tile_mapping[@page.random_position_region]
  237.     return arr.delete_at(rand(arr.length))
  238.   end
  239.  
  240.   alias :th_random_event_positions_setup_page_settings :setup_page_settings
  241.   def setup_page_settings
  242.     th_random_event_positions_setup_page_settings
  243.     randomize_position if @page.random_position_type == :page
  244.   end
  245. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement