Advertisement
Guest User

Untitled

a guest
Oct 29th, 2013
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.12 KB | None | 0 0
  1. =begin
  2. #===============================================================================
  3.  Title: Stacked Events
  4.  Author: Tsukihime
  5.  Date: Jun 2, 2013
  6. --------------------------------------------------------------------------------
  7.  ** Change log
  8.  Jun 2, 2013
  9.    - Fixed bug where multiple stack events were not created correctly
  10.  Apr 7, 2013
  11.    - added support for multiple stack assignment
  12.    - initial release
  13. --------------------------------------------------------------------------------  
  14.  ** Terms of Use
  15.  * Free to use in commercial/non-commercial projects
  16.  * No real support. The script is provided as-is
  17.  * Will do bug fixes, but no compatibility patches
  18.  * Features may be requested but no guarantees, especially if it is non-trivial
  19.  * Credits to Tsukihime in your project
  20.  * Preserve this header
  21. --------------------------------------------------------------------------------
  22.  ** Description
  23.  
  24.  This script allows you to indirectly stack events by using a single event
  25.  to store event pages for multiple events. The game then sorts the pages
  26.  and then splits it up into multiple events that are stacked on top of each
  27.  other.
  28.  
  29. --------------------------------------------------------------------------------
  30.  ** Installation
  31.  
  32.  Place this script below Materials and above Main
  33.  
  34. --------------------------------------------------------------------------------
  35.  ** Usage
  36.  
  37.  To create a stacked event, you need to do two things
  38.  
  39.  1. The name of the event must have the word [stack]
  40.  2. Each page must indicate which event in the stack they belong to.
  41.  
  42.  For each page, create a comment of the form
  43.  
  44.    <stack: x>
  45.    
  46.  Where x is some value used to distinguish between different events in the
  47.  stack. The value you choose is not important as long as you are consistent.
  48.  The pages will be grouped based on these stack ID's. You can use numbers
  49.  or letters, or even words.
  50.  
  51.  A single page can be added to multiple stack events, rather than duplicating
  52.  the same page again and again for each event in the stack. Simply add multiple
  53.  ID's to the comment and they will be added appropriately, separating each ID
  54.  by a space.
  55.  
  56.    <stack: 1 2 3>
  57.  
  58.  This will add the page to stacks 1, 2, and 3.
  59. #===============================================================================
  60. =end
  61. $imported = {} if $imported.nil?
  62. $imported["TH_StackedEvents"] = true
  63. #===============================================================================
  64. # ** Configuration
  65. #===============================================================================
  66. module TH
  67.   module Stacked_Events
  68.    
  69.     Stack_Name = /\[stack\]/i
  70.     Stack_Regex = /<stack: (.*)>/i
  71.   end
  72. end
  73. #===============================================================================
  74. # ** Rest of script
  75. #===============================================================================
  76. module RPG
  77.   class Event
  78.    
  79.     def stack_event?
  80.       return @is_stack_event unless @is_stack_event.nil?
  81.       parse_event_name_stack_event
  82.     end
  83.    
  84.     def parse_event_name_stack_event
  85.       res = self.name.match(TH::Stacked_Events::Stack_Name)
  86.       @is_stack_event = !res.nil?
  87.     end
  88.    
  89.     #---------------------------------------------------------------------------
  90.     # Split the pages into separate stacks based on the stack ID, then return it
  91.     #---------------------------------------------------------------------------
  92.     def split_stack
  93.       new_pages = {}
  94.       @pages.each do |page|
  95.         page.list.each do |cmd|
  96.           if cmd.code == 108 && cmd.parameters[0] =~ TH::Stacked_Events::Stack_Regex
  97.             stack_ids = $1.split
  98.             stack_ids.each do |id|
  99.               new_pages[id] ||= []
  100.               new_pages[id] << page
  101.             end
  102.             next
  103.           end
  104.         end
  105.       end
  106.       return new_pages
  107.     end
  108.   end
  109.  
  110.   class Map
  111.     def split_stacked_events
  112.       new_events = {}
  113.       temp_events = @events.clone
  114.       temp_events.each do |id, event|
  115.         next unless event.stack_event?
  116.         page_stacks = event.split_stack
  117.         stack_events = create_stack_events(event, page_stacks)
  118.         @events.merge!(stack_events)
  119.       end
  120.     end
  121.    
  122.     #---------------------------------------------------------------------------
  123.     # Takes a set of page stacks and put them into their own events
  124.     # The first stack will be the original event ID, but the rest will have
  125.     # their own, uniquely assigned event ID's
  126.     #---------------------------------------------------------------------------
  127.     def create_stack_events(event, page_stacks)
  128.       new_events = {}
  129.       # first event
  130.       first_stack = page_stacks.shift
  131.       ev = RPG::Event.new(event.x, event.y)
  132.       ev.id = event.id
  133.       ev.pages = first_stack[1]
  134.       new_events[ev.id] = ev
  135.      
  136.       max_id = @events.keys.max
  137.       # rest of the events      
  138.       page_stacks.each do |stack_id, pages|
  139.         max_id += 1
  140.         ev = RPG::Event.new(event.x, event.y)
  141.         ev.id = max_id
  142.         ev.pages = pages
  143.         new_events[max_id] = ev
  144.       end
  145.       return new_events
  146.     end
  147.   end
  148. end
  149.  
  150. class Game_Map
  151.  
  152.   alias :th_stacked_events_setup_events :setup_events
  153.   def setup_events
  154.     @map.split_stacked_events
  155.     th_stacked_events_setup_events
  156.   end
  157. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement