Advertisement
blucalm

Era Utilities/Helpers

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