Advertisement
eshrasic

Era Module

Aug 10th, 2015
1,056
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 16.54 KB | None | 0 0
  1.    
  2.  
  3.     #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
  4.     # * Era Module
  5.     #   Author: Eshra
  6.     #   Compatibility: Rpg Maker VX Ace
  7.     #   Released: vUnfinished posted 9 March 2013
  8.     #   Last Updated:   10 August 2015
  9.     #   Dependencies: ** no dependencies **
  10.     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  11.     #   Provides helper and utility methods.
  12.     #   Standardizes loading notetags from the db so that it's not iterated
  13.     #   through multiple times when getting notes data.
  14.     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  15.     # Update Log:
  16.     # 9  Aug. 2015 - Added nil check in any_comment_match?, added meths to CERA
  17.     # 7  Aug. 2015 - Added any_comment_match?
  18.     # 18 Jan. 2013 - Moved set_revs method from Era to TBM, added core save obj.
  19.     # 10 Jan. 2013 - Rounding and tbs revival settings
  20.     # 30 Dec. 2012 - Notetag Loading standardization
  21.     # 20 Nov. 2012 - added valid_comments
  22.     # 18 Nov. 2012 - added new_event
  23.     #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
  24.      
  25.     ($imported ||= {})["Era Module"] = 0.2
  26.     module Era
  27.      
  28.       module LoadDB
  29.         # Turn loading notetags on/off
  30.         # No scripts modify the default off constants as of 18 Jan. 2013
  31.        
  32.         LOAD_CLASSES = false
  33.         LOAD_SKILLS = true
  34.         LOAD_ITEMS = true
  35.         LOAD_WEAPONS = true
  36.         LOAD_ARMORS = true
  37.         LOAD_ENEMIES = true
  38.         LOAD_TROOPS = false
  39.         LOAD_STATES = true
  40.         LOAD_ANIMATIONS = false
  41.         LOAD_TILESETS = false
  42.         LOAD_COMMON_EVENTS = false
  43.         LOAD_MAPINFOS = false
  44.         LOAD_ACTORS = true
  45.       end # LoadDB
  46.      
  47.       module RE
  48.         WinNL = /[\r\n]+/
  49.         Num = /\A(?:-|)\d+(?:\.|)(?:\d+|)\Z/ # Regex representing a number
  50.       end # RE
  51.      
  52.       #============================================================================
  53.       # ** Era::Event
  54.       # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  55.       #      valid_comments(id) => array of comments for event with id = to param
  56.       #           events active page, => nil if no active pages.
  57.       #      new_event(spawn_map_id, opts = {}) => spawns an event on the map,
  58.       #           requires reproduce events.
  59.       #      round(float, power of ten) => rounds the float to arg power of ten
  60.       #
  61.       #============================================================================
  62.       module Event
  63.         #--------------------------------------------------------------------------
  64.         # * New Event
  65.         # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  66.         #     Places a new event on the map using the "Reproduce Events" Script.
  67.         #--------------------------------------------------------------------------
  68.         def self.new_event(spawn_map_id, opts = {})
  69.           return unless $imported["Ra Event Spawn"] >= 0.2
  70.          
  71.           player = $game_player
  72.           options = {
  73.             :x => player.x, :y => player.y, :persist => false, :init_dir => -1,
  74.             :opts => {}, :name => nil, :id => 0
  75.           }.merge(opts)
  76.        
  77.           persist,x,y,id = options[:persist], options[:x], options[:y], options[:id]
  78.           init_dir, opts, name = options[:init_dir], options[:opts], options[:name]
  79.           EventSpawn.spawn_event(spawn_map_id, id, x, y, persist, init_dir,
  80.             opts, name)
  81.         end
  82.         #--------------------------------------------------------------------------
  83.         # * Valid Comments
  84.         # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  85.         #     Returns the comments for the event with param: id on its active page
  86.         #     => nil if no page conditions are met or no comments are found.
  87.         #--------------------------------------------------------------------------
  88.         def self.valid_comments(id)
  89.           return if (ev = $game_map.events[id]).nil? || !(page = ev.find_proper_page)
  90.           page.list.inject([]){ |a,c| c.code == 108 ? a.push(c.parameters[0]) : a }
  91.         end
  92.        
  93.         #--------------------------------------------------------------------------
  94.         # * Any Comment Match
  95.         # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  96.         #   Returns true if any comment on the events active page matches regex
  97.         #     id    -> The event to look up
  98.         #     regex -> The regular expression to match
  99.         #--------------------------------------------------------------------------
  100.         def self.any_comment_match?(id, regex)
  101.           comments = valid_comments(id)
  102.           match = false
  103.           (comments||=[]).each do |str|
  104.             match = match || ((str =~ regex) == 0)
  105.           end
  106.           match
  107.         end
  108.       end # Event
  109.      
  110.       #______________________
  111.       # Convienience methods
  112.      
  113.       #--------------------------------------------------------------------------
  114.       # * Round a float to the nearest place specified as a power of ten.
  115.       #--------------------------------------------------------------------------
  116.       def self.round(f, pten)
  117.         (f*pten).round.to_f / pten
  118.       end
  119.      
  120.     end # Era
  121.      
  122.     #==============================================================================
  123.     # ** DataManager
  124.     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  125.     #       Note-tag helper methods. Alias eval_note_era to evaluate notes for
  126.     #   the entire database. Determine type by the regex the note matches.
  127.     #==============================================================================
  128.     module DataManager
  129.       class <<self
  130.         alias load_db_era load_database
  131.         alias load_game_era load_game
  132.       end
  133.       def self.load_database
  134.         load_db_era
  135.        
  136.         if !$BTEST
  137.           load_classes_era if Era::LoadDB::LOAD_CLASSES
  138.           load_actors_era if Era::LoadDB::LOAD_ACTORS
  139.           load_enemies_era if Era::LoadDB::LOAD_ENEMIES
  140.           load_troops_era if Era::LoadDB::LOAD_TROOPS
  141.           load_animations_era if Era::LoadDB::LOAD_ANIMATIONS
  142.           load_tilesets_era if Era::LoadDB::LOAD_TILESETS
  143.           load_common_events_era if Era::LoadDB::LOAD_COMMON_EVENTS
  144.           load_map_infos_era if Era::LoadDB::LOAD_MAPINFOS
  145.           load_skills_era if Era::LoadDB::LOAD_SKILLS
  146.           load_items_era if Era::LoadDB::LOAD_ITEMS
  147.           load_weapons_era if Era::LoadDB::LOAD_WEAPONS
  148.           load_armors_era if Era::LoadDB::LOAD_ARMORS
  149.           load_states_era if Era::LoadDB::LOAD_STATES
  150.         end
  151.       end
  152.      
  153.       #--------------------------------------------------------------------------
  154.       # * Alias, load_game
  155.       #--------------------------------------------------------------------------
  156.       def self.load_game(index)
  157.         success = load_game_era(index)
  158.         $game_system.init_era if $game_system # init core obj. if just installed
  159.         success
  160.       end
  161.      
  162.       #----------------------------------------------------------------------------
  163.       # * Load Notetags
  164.       # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  165.       # Methods to perform processing on each item in the database. The benefit is
  166.       # that now if two scripts I wrote needed to for example access all of the
  167.       # armors, there would only be one iteration over $data_armors instead of
  168.       # two. The speed up would only be noticable for large databases using several
  169.       # scripts I've written.
  170.       #----------------------------------------------------------------------------
  171.      
  172.       def self.load_classes_era; $data_classes.each{|c| each_class_era(c)}; end
  173.       #----------------------------------------------------------------------------
  174.       # * Processing for each class in the database
  175.       #----------------------------------------------------------------------------
  176.       def self.each_class_era(param)
  177.         return if param.nil?
  178.         param.note.split(Era::RE::WinNL).each{ |line| eval_note_era(param, line)}
  179.       end
  180.      
  181.       def self.load_actors_era; $data_actors.each{|a| each_actor_era(a)}; end
  182.       #----------------------------------------------------------------------------
  183.       # * Processing for each actor
  184.       #----------------------------------------------------------------------------
  185.       def self.each_actor_era(param)
  186.         return if param.nil?
  187.         param.note.split(Era::RE::WinNL).each{ |line| eval_note_era(param, line)}
  188.       end
  189.      
  190.       def self.load_enemies_era; $data_enemies.each{|e| each_enemy_era(e)}; end
  191.       #----------------------------------------------------------------------------
  192.       # * Processing for each enemy
  193.       #----------------------------------------------------------------------------
  194.       def self.each_enemy_era(param)
  195.         return if param.nil?
  196.         param.note.split(Era::RE::WinNL).each{ |line| eval_note_era(param, line)}
  197.       end
  198.      
  199.       def self.load_troops_era; $data_troops.each{|t| each_troop_era(t)}; end
  200.       #----------------------------------------------------------------------------
  201.       # * Processing for each troop
  202.       #----------------------------------------------------------------------------
  203.       def self.each_troop_era(param)
  204.         param.note.split(Era::RE::WinNL).each{ |line| eval_note_era(param, line)}
  205.       end
  206.      
  207.       def self.load_animations_era;$data_animations.each{|a|each_animation_era(a)};end
  208.       #----------------------------------------------------------------------------
  209.       # * Processing for each animation
  210.       #----------------------------------------------------------------------------
  211.       def self.each_animation_era(param)
  212.         return if param.nil?
  213.         param.note.split(Era::RE::WinNL).each{ |line| eval_note_era(param, line)}
  214.       end
  215.      
  216.       def self.load_tilesets_era; $data_tilesets.each{|t|each_tileset_era(t)}; end
  217.       #----------------------------------------------------------------------------
  218.       # * Processing for each tileset
  219.       #----------------------------------------------------------------------------
  220.       def self.each_tileset_era(param)
  221.         return if param.nil?
  222.         param.note.split(Era::RE::WinNL).each{ |line| eval_note_era(param, line)}
  223.       end
  224.      
  225.       def self.load_common_events_era
  226.         $data_common_events.each{|c| each_common_event_era(c) }
  227.       end
  228.       #----------------------------------------------------------------------------
  229.       # * Processing for each common event
  230.       #----------------------------------------------------------------------------
  231.       def self.each_common_event_era(param)
  232.         return if param.nil?
  233.         param.note.split(Era::RE::WinNL).each{ |line| eval_note_era(param, line)}
  234.       end
  235.      
  236.       def self.load_map_infos_era; $data_mapinfos.each{|m| each_map_info_era(m)};end
  237.       #----------------------------------------------------------------------------
  238.       # * Processing for each map info
  239.       #----------------------------------------------------------------------------
  240.       def self.each_map_info_era(param)
  241.         return if param.nil?
  242.         param.note.split(Era::RE::WinNL).each{ |line| eval_note_era(param, line)}
  243.       end
  244.        
  245.       def self.load_skills_era; $data_skills.each{|s| each_skill_era(s)}; end
  246.       #----------------------------------------------------------------------------
  247.       # * Processing for each skill
  248.       #----------------------------------------------------------------------------
  249.       def self.each_skill_era(arg)
  250.         arg.note.split(Era::RE::WinNL).each{ |line| eval_note_era(arg, line)} if arg
  251.       end
  252.        
  253.       def self.load_items_era; $data_items.each{|i| each_item_era(i)}; end
  254.       #----------------------------------------------------------------------------
  255.       # * Processing for each item
  256.       #----------------------------------------------------------------------------
  257.       def self.each_item_era(param)
  258.         return if param.nil?
  259.         param.note.split(Era::RE::WinNL).each{ |line| eval_note_era(param, line)}
  260.       end
  261.        
  262.       def self.load_weapons_era; $data_weapons.each{|w| each_weapon_era(w)}; end
  263.       #----------------------------------------------------------------------------
  264.       # * Processing for each weapon
  265.       #----------------------------------------------------------------------------
  266.       def self.each_weapon_era(param)
  267.         return if param.nil?
  268.         param.note.split(Era::RE::WinNL).each{ |line| eval_note_era(param, line)}
  269.       end
  270.      
  271.       def self.load_armors_era; $data_armors.each{|a| each_armor_era(a)}; end
  272.       #----------------------------------------------------------------------------
  273.       # * Processing for each armor
  274.       #----------------------------------------------------------------------------
  275.       def self.each_armor_era(param)
  276.         return if param.nil?
  277.         param.note.split(Era::RE::WinNL).each{ |line| eval_note_era(param, line)}
  278.       end
  279.      
  280.       def self.load_states_era; $data_states.each{|s| each_state_era(s)}; end
  281.       #----------------------------------------------------------------------------
  282.       # * Processing for each state
  283.       #----------------------------------------------------------------------------
  284.       def self.each_state_era(param)
  285.         return if param.nil?
  286.         param.note.split(Era::RE::WinNL).each{ |line| eval_note_era(param, line)}
  287.       end
  288.       #----------------------------------------------------------------------------
  289.       # * Only one method to evaluate notetags
  290.       #     assumes type of data can be inferred based on matching regex
  291.       #----------------------------------------------------------------------------
  292.       def self.eval_note_era(data, line)
  293.       end
  294.     end # DataManager
  295.      
  296.     #==============================================================================
  297.     # ** Game_System
  298.     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  299.     #==============================================================================
  300.     class Game_System
  301.       #--------------------------------------------------------------------------
  302.       # * Alias, initialize
  303.       #--------------------------------------------------------------------------
  304.             alias initialize_cera initialize
  305.             def initialize
  306.                     initialize_cera
  307.                     init_era(:force => true)
  308.             end
  309.             #--------------------------------------------------------------------------
  310.             # * Init Core Era object
  311.             #--------------------------------------------------------------------------
  312.             def init_era(opts = {})
  313.               options = {:force => false, :opts => {}}.merge(opts)
  314.               opts2 = options[:opts]
  315.               options[:force] ? @era_save = CEra.new(opts2): @era_save ||= CEra.new(opts2)
  316.             end
  317.       #--------------------------------------------------------------------------
  318.       # * Get Core object
  319.       #--------------------------------------------------------------------------
  320.       def era_obj; @era_save; end
  321.     end # Game_System
  322.      
  323.     #==============================================================================
  324.     # ** CEra
  325.     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  326.     #   Allows easier and more organized inclusion in save data. Less namespace
  327.     #     pollution etc.
  328.     #==============================================================================
  329.     class CEra
  330.       #--------------------------------------------------------------------------
  331.       # * Init
  332.       #--------------------------------------------------------------------------
  333.             def initialize(opts = {})
  334.               init_public_mems(opts)
  335.               init_private_mems(opts)
  336.             end
  337.            
  338.             def init_public_mems(opts = {})
  339.             end
  340.            
  341.             def init_private_mems(opts = {})
  342.             end
  343.     end # CEra
  344.    
  345.     #==============================================================================
  346.     # ** Game_Event
  347.     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  348.     #     Adds:
  349.     #       attr_readers for @event and @list
  350.     #==============================================================================
  351.     class Game_Event < Game_Character; attr_reader :event, :list; end # Game_Event
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement