dsiver144

Gambit Database Organizer

Jun 7th, 2014
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 66.79 KB | None | 0 0
  1. #==============================================================================
  2. # GAMBIT DATABASE ORGANIZER
  3. #------------------------------------------------------------------------------
  4. # WRITTEN BY: Gambit
  5. #------------------------------------------------------------------------------
  6. # VERSION: 1.50
  7. #------------------------------------------------------------------------------
  8. # CHANGELOG:
  9. #  v 1.50 - June 6, 2014 > Added support for variables
  10. #                        > Added support for self switches
  11. #  v 1.15 - June 5, 2014 > Significant performance improvements
  12. #                        > Added support for switches
  13. #  v 1.10 - June 4, 2014 > Added Database Organizer scene
  14. #  v 1.00 - June 4, 2014 > Initial release
  15. #------------------------------------------------------------------------------
  16. # DESCRIPTION AND FEATURES:
  17. #   This script allows a user to modify the order of objects in most databases
  18. #   while preserving all game references to the original objects. In the project
  19. #   editor, if you attempt to swap two Items by cutting and pasting, all
  20. #   references to the items would be reversed. The references are tied to the ID
  21. #   instead of the object itself. This script allows you to manipulate the
  22. #   databases while preserving all references to the original objects.
  23. #
  24. #   A message will appear when the tasks you have specified are completed
  25. #   successfully. CLOSE AND REOPEN THE PROJECT for the changes to finalize and
  26. #   appear in the editor. If you save the project after running the script but
  27. #   before closing and reopening it, the Organizer tasks will be REVERTED.
  28. #
  29. #   The script will print out a note to the console as each task is completed.
  30. #   If you encounter an error and would like to revert all the tasks that were
  31. #   performed prior to the error, just save your project. Again, saving the
  32. #   project after running the script will revert the changes. To finalize the
  33. #   changes, you must close the project after running the script then reopen it.
  34. #
  35. #  Features:
  36. #   - Swap, move, insert, and delete database objects without ruining all of the
  37. #     references in your game
  38. #   - Update and redirect object references
  39. #   - User-friendly Database Organizer scene to facilitate organization
  40. #   - Optional task queue is available which will automatically perform the
  41. #     specified tasks in the order they are provided
  42. #   - Most objects are supported:
  43. #     - Actors
  44. #     - Animations
  45. #     - Armors
  46. #     - Classes
  47. #     - Common Events
  48. #     - Enemies
  49. #     - Items
  50. #     - Self Switches
  51. #     - Skills
  52. #     - States
  53. #     - Switches
  54. #     - Tilesets
  55. #     - Troops
  56. #     - Variables
  57. #     - Weapons
  58. #------------------------------------------------------------------------------
  59. # REQUIRES:
  60. # - None
  61. #------------------------------------------------------------------------------
  62. # INSTRUCTIONS AND SETUP:
  63. #  Place this script below Materials and above Main Process.
  64. #
  65. #  Setup for this script is below. Please read explanations in comments.
  66. #------------------------------------------------------------------------------
  67. # Terms of Use:
  68. # - Credit if used
  69. # - Free for use in non-commercial projects
  70. # - Please do not repost elsewhere, convert, or translate this script without
  71. #   permission.
  72. # - If seeking to use in commercial projects (projects that are generating money
  73. #   in any way, including advertisements), please contact me via PM at:
  74. #   http://www.rpgmakervxace.net/user/14194-gambit/
  75. # - When using third-party scripts, the terms and conditions of those scripts
  76. #   also apply. Please be aware of them prior to using.
  77. # - Not for use in any project involving any form of nudity, commercial or not
  78. #==============================================================================
  79.  
  80. module Gambit
  81.   module DatabaseOrganizer
  82.    
  83.     # Determines whether Database Organizer will run when your game starts.
  84.     Enabled = true
  85.    
  86.     # Determines whether to use the Database Organizer scene.
  87.     # If set to true, you do not need to set up Tasks below.
  88.     UseGUI = true
  89.    
  90.     # Only needed if UseGUI is set to false.
  91.     # This allows you to queue tasks if you prefer creating them manually and
  92.     # bypassing the Database Organizer scene.
  93.     #       -------------------------------------------------------------
  94.     # Tasks that you want to perform. The tasks are performed in the order
  95.     # listed, one after the other. So feel free to chain tasks by using the IDs
  96.     # that would result from the previous tasks.
  97.     #       -------------------------------------------------------------
  98.     # Each entry should have this format:
  99.     # [task type, object type, old ID, new ID(, map ID)],
  100.     #       -------------------------------------------------------------
  101.     # task type =
  102.     #   :swap   = swaps old ID object with new ID object and updates the
  103.     #             references to both.
  104.     #   :move   = moves old ID object to new ID, overwriting object that was
  105.     #             previously at new ID. References to old ID will be changed to
  106.     #             new ID so references to both the old and new IDs will point at
  107.     #             new ID. The old ID will be a blank object.
  108.     #   :insert = inserts old ID object at new ID. If old ID is less than new
  109.     #             ID, all objects in between are shifted up one. If old ID is
  110.     #             greater than new ID, all objects in between are shifted down
  111.     #             one. All references are updated accordingly.
  112.     #   :delete = deletes old ID object and shifts the IDs of all the following
  113.     #             objects up one. All references are updated accordingly.
  114.     #             References to the old ID will be redirected to the last ID.
  115.     #             The last ID will be a blank object. A new ID is not needed.
  116.     #   :update = updates and redirects old ID references to new ID.
  117.     #
  118.     # object type = :actor, :animation, :armor, :class, :common_event, :enemy,
  119.     #               :item, :self_switch, :skill, :state, :switch, :tileset,
  120.     #               :troop, :variable, :weapon
  121.     # old ID      = original ID
  122.     # new ID      = target ID (not needed for :delete task type)
  123.     #       -------------------------------------------------------------
  124.     # For Self Switches only:
  125.     # map ID      = map ID
  126.     #
  127.     # Only the :swap and :update task types can be used.
  128.     # Use strings for the IDs ('A', 'B', 'C', 'D').
  129.     #       -------------------------------------------------------------
  130.     Tasks = [
  131.       [:swap,   :actor,   5,  6],
  132.       [:move,   :class,  10,  4],
  133.       [:insert, :weapon, 23, 34],
  134.       [:delete, :skill,   7    ],
  135.       [:update, :armor,  10, 12],
  136.     ]
  137.    
  138.   end
  139. end
  140.  
  141. ###############################################################################
  142. ###############################################################################
  143. ###############################################################################
  144. ###############################################################################
  145. ### END OF SETTING UP ###### ONLY EDIT BELOW IF YOU KNOW WHAT YOU ARE DOING ###
  146. ###############################################################################
  147. ###############################################################################
  148. ###############################################################################
  149. ###############################################################################
  150.  
  151.  
  152. ($imported ||={})["GambitDatabaseOrganizer"] = true
  153.  
  154. #==============================================================================
  155. # ** DataManager
  156. #------------------------------------------------------------------------------
  157. #  This module manages the database and game objects. Almost all of the
  158. # global variables used by the game are initialized by this module.
  159. #==============================================================================
  160.  
  161. module DataManager
  162.   class <<self
  163.     alias gam_dborganizer_load_normal_database load_normal_database
  164.   end
  165.   #--------------------------------------------------------------------------
  166.   # * Load Normal Database
  167.   #--------------------------------------------------------------------------
  168.   def self.load_normal_database
  169.     gam_dborganizer_load_normal_database
  170.     if Gambit::DatabaseOrganizer::Enabled && !Gambit::DatabaseOrganizer::UseGUI
  171.       Gambit::DatabaseOrganizer::Tasks.each_with_index do |task, index|
  172.         organize_database(task, index)
  173.       end
  174.       database_organizer_done
  175.     end
  176.   end
  177.   #--------------------------------------------------------------------------
  178.   # * Perform Database Organizer Tasks
  179.   #--------------------------------------------------------------------------
  180.   def self.organize_database(task, index = nil)
  181.     task_type, type, old_id, new_id, map_id = task
  182.     case task_type
  183.     when :swap
  184.       separator = '↔'
  185.       database_swap(*task.drop(1))
  186.       update_references(*task)
  187.     when :move
  188.       separator = '→'
  189.       database_move(*task.drop(1))
  190.       update_references(*task)
  191.     when :insert
  192.       separator = '↓'
  193.       database_insert(*task.drop(1))
  194.       update_references(*task)
  195.     when :delete
  196.       new_id = determine_type(type)[0].drop(1).size
  197.       task.size == 3 ? task << new_id : task[-1] = new_id
  198.       database_delete(type, old_id)
  199.       update_references(*task)
  200.     when :update
  201.       separator = '↑'
  202.       update_references(*task)
  203.     else
  204.       error = true
  205.       puts "XTask #{index + 1}: Skipped - #{task_type}: invalid task type"
  206.     end unless type == :self_switch
  207.     if type == :self_switch
  208.       case task_type
  209.       when :swap
  210.         separator = '↔'
  211.         update_references(*task)
  212.       when :update
  213.         separator = '→'
  214.         update_references(*task)
  215.       else
  216.         error = true
  217.         puts "XTask #{index + 1}: Skipped - #{task_type}: invalid task type " +
  218.           'for self switch object type'
  219.       end
  220.     else
  221.       save_data(*determine_type(type, true)) unless error
  222.     end
  223.     if task_type == :delete
  224.       puts "✓Task #{index + 1}: Completed - #{task_type}: #{type} #{old_id}"
  225.     elsif !error
  226.       puts "✓Task #{index + 1}: Completed - #{task_type}: #{type} #{old_id} " +
  227.         "#{separator} #{new_id}"
  228.     end
  229.   end
  230.   #--------------------------------------------------------------------------
  231.   # * Present Database Organizer Completion Message
  232.   #--------------------------------------------------------------------------
  233.   def self.database_organizer_done
  234.     message = "Database organization has been performed successfully! Close and
  235.               reopen your project for the changes to show in the database. You
  236.               can now disable the Gambit Database Organizer script or change
  237.               the tasks to prevent them from being repeated."
  238.     title   = "Gambit Database Organizer: Successful"
  239.     message.gsub!(/\s+/," ").gsub!(/! C/,"!\n\nC").gsub!(/. Y/,".\n\nY")
  240.     win32 = Win32API.new("user32", "MessageBox", ['i', 'p', 'p', 'i'], 'i')
  241.     win32.call(0, message, title, 64)
  242.     exit
  243.   end
  244.   #--------------------------------------------------------------------------
  245.   # * Delete Database Object
  246.   #--------------------------------------------------------------------------
  247.   def self.database_delete(type, old_id)
  248.     array = determine_type(type)[0]
  249.     array.delete_at(old_id)
  250.     array << array[-1].class.new
  251.     return if type == :switch || type == :variable
  252.     array[old_id..array.size - 1].each_with_index do |object, index|
  253.       object.id = index + old_id
  254.     end
  255.   end
  256.   #--------------------------------------------------------------------------
  257.   # * Insert Database Object
  258.   #--------------------------------------------------------------------------
  259.   def self.database_insert(type, old_id, new_id)
  260.     array = determine_type(type)[0]
  261.     array.insert(new_id, array.delete_at(old_id))
  262.     return if type == :switch || type == :variable
  263.     array[[old_id, new_id].min..[old_id, new_id].max].each_with_index do
  264.       |object, index|
  265.       object.id = index + [old_id, new_id].min
  266.     end
  267.   end
  268.   #--------------------------------------------------------------------------
  269.   # * Move Database Object
  270.   #--------------------------------------------------------------------------
  271.   def self.database_move(type, old_id, new_id)
  272.     return if old_id == new_id
  273.     array = determine_type(type)[0]
  274.     array[new_id] = array[old_id]
  275.     array[new_id].id = new_id unless type == :switch || type == :variable
  276.     array[old_id] = array[old_id].class.new unless type == :self_switch
  277.     return if type == :switch || type == :variable
  278.     array[old_id].id = old_id
  279.   end
  280.   #--------------------------------------------------------------------------
  281.   # * Swap Database Objects
  282.   #--------------------------------------------------------------------------
  283.   def self.database_swap(type, old_id, new_id)
  284.     array = determine_type(type)[0]
  285.     array.swap!(old_id, new_id)
  286.     return if type == :switch || type == :variable
  287.     array[old_id].id = old_id
  288.     array[new_id].id = new_id
  289.   end
  290.   #--------------------------------------------------------------------------
  291.   # * Get Data and File Name of Type
  292.   #--------------------------------------------------------------------------
  293.   def self.determine_type(type, save = false)
  294.     case type
  295.     when :actor
  296.       array = $data_actors
  297.       file = "Data/Actors.rvdata2"
  298.     when :animation
  299.       array = $data_animations
  300.       file = "Data/Animations.rvdata2"
  301.     when :armor
  302.       array = $data_armors
  303.       file = "Data/Armors.rvdata2"
  304.     when :class
  305.       array = $data_classes
  306.       file = "Data/Classes.rvdata2"
  307.     when :common_event
  308.       array = $data_common_events
  309.       file = "Data/CommonEvents.rvdata2"
  310.     when :enemy
  311.       array = $data_enemies
  312.       file = "Data/Enemies.rvdata2"
  313.     when :item
  314.       array = $data_items
  315.       file = "Data/Items.rvdata2"
  316.     when :self_switch
  317.       array = [*'A'..'D']
  318.       file = false
  319.     when :skill
  320.       array = $data_skills
  321.       file = "Data/Skills.rvdata2"
  322.     when :state
  323.       array = $data_states
  324.       file = "Data/States.rvdata2"
  325.     when :switch
  326.       array = save ? $data_system : $data_system.switches
  327.       file = "Data/System.rvdata2"
  328.     when :tileset
  329.       array = $data_tilesets
  330.       file = "Data/Tilesets.rvdata2"
  331.     when :troop
  332.       array = $data_troops
  333.       file = "Data/Troops.rvdata2"
  334.     when :variable
  335.       array = save ? $data_system : $data_system.variables
  336.       file = "Data/System.rvdata2"
  337.     when :weapon
  338.       array = $data_weapons
  339.       file = "Data/Weapons.rvdata2"
  340.     end
  341.     [array, file]
  342.   end
  343.   #--------------------------------------------------------------------------
  344.   # * Update References to Object IDs and Save Data
  345.   #--------------------------------------------------------------------------
  346.   def self.update_references(*args)
  347.     type = args[1]
  348.     #------------------------------------------------------------------------
  349.     # * Modify Map References to Self Switches
  350.     #------------------------------------------------------------------------
  351.     if type == :self_switch
  352.       file = sprintf("Data/Map%03d.rvdata2", args[-1])
  353.       map = load_data(file)
  354.       map.events.values.each do |event|
  355.         event.pages.each do |page|
  356.           page.condition.self_switch_ch.dborg!(*args)
  357.           change_event_references(*args, page)
  358.         end
  359.       end
  360.       save_data(map, file)
  361.       return
  362.     end
  363.     #------------------------------------------------------------------------
  364.     # * Modify Common Event References to IDs
  365.     #------------------------------------------------------------------------
  366.     $data_common_events.drop(1).each do |page|
  367.       page.switch_id = page.switch_id.dborg(*args) if type == :switch
  368.       change_event_references(*args, page)
  369.     end
  370.     save_data($data_common_events, "Data/CommonEvents.rvdata2")
  371.     #------------------------------------------------------------------------
  372.     # * Modify Map References to IDs
  373.     #------------------------------------------------------------------------
  374.     $data_mapinfos.keys.each do |map_id|
  375.       file = sprintf("Data/Map%03d.rvdata2", map_id)
  376.       map = load_data(file)
  377.       map.tileset_id = map.tileset_id.dborg(*args) if type == :tileset
  378.       map.encounter_list.each do |encounter|
  379.         encounter.troop_id = encounter.troop_id.dborg(*args)
  380.       end if type == :troop
  381.       map.events.values.each do |event|
  382.         event.pages.each do |page|
  383.           case type
  384.           when :actor
  385.             page.condition.actor_id = page.condition.actor_id.dborg(*args)
  386.           when :item
  387.             page.condition.item_id = page.condition.item_id.dborg(*args)
  388.           when :switch
  389.             page.condition.switch1_id = page.condition.switch1_id.dborg(*args)
  390.             page.condition.switch2_id = page.condition.switch2_id.dborg(*args)
  391.             page.move_route.list.each do |move_command|
  392.               next unless move_command.code == 27 || move_command.code == 28
  393.               move_command.parameters[0] =
  394.                 move_command.parameters[0].dborg(*args)
  395.             end
  396.           when :variable
  397.             page.condition.variable_id = page.condition.variable_id.dborg(*args)
  398.           end
  399.           change_event_references(*args, page)
  400.         end
  401.       end
  402.       save_data(map, file)
  403.     end
  404.     #------------------------------------------------------------------------
  405.     # * Modify Troop References to IDs
  406.     #------------------------------------------------------------------------
  407.     $data_troops.drop(1).each do |troop|
  408.       troop.members.each do |member|
  409.         member.enemy_id = member.enemy_id.dborg(*args)
  410.       end if type == :enemy
  411.       troop.pages.each do |page|
  412.         page.condition.actor_id = page.condition.actor_id.dborg(*args) if
  413.           type == :actor
  414.         page.condition.switch_id = page.condition.switch_id.dborg(*args) if
  415.           type == :switch
  416.         change_event_references(*args, page)
  417.       end
  418.     end
  419.     save_data($data_troops, "Data/Troops.rvdata2")
  420.     #------------------------------------------------------------------------
  421.     # * Modify System References to Actor IDs
  422.     #------------------------------------------------------------------------
  423.     if type == :actor
  424.       $data_system.party_members.map! {|id| id.dborg(*args) }
  425.       $data_system.test_battlers.each do |battler|
  426.         battler.actor_id = battler.actor_id.dborg(*args)
  427.       end
  428.       save_data($data_system, "Data/System.rvdata2")
  429.     end
  430.     #------------------------------------------------------------------------
  431.     # * Modify Item, Skill, and Weapon References to Animation IDs
  432.     #------------------------------------------------------------------------
  433.     if type == :animation
  434.       ($data_skills | $data_items | $data_weapons).compact.each do |object|
  435.         object.animation_id = object.animation_id.dborg(*args)
  436.       end
  437.       save_data($data_skills,  "Data/Skills.rvdata2")
  438.       save_data($data_items,   "Data/Items.rvdata2")
  439.       save_data($data_weapons, "Data/Weapons.rvdata2")
  440.     end
  441.     #------------------------------------------------------------------------
  442.     # * Modify Actor References to Class IDs
  443.     #------------------------------------------------------------------------
  444.     if type == :class
  445.       $data_actors.drop(1).each do |actor|
  446.         actor.class_id = actor.class_id.dborg(*args)
  447.       end
  448.       save_data($data_actors, "Data/Actors.rvdata2")
  449.     end
  450.     #------------------------------------------------------------------------
  451.     # * Modify Class References to Skill IDs
  452.     #------------------------------------------------------------------------
  453.     if type == :skill
  454.       $data_classes.drop(1).each do |class_object|
  455.         class_object.learnings.each do |learning|
  456.           learning.skill_id = learning.skill_id.dborg(*args)
  457.         end
  458.       end
  459.     end
  460.     #------------------------------------------------------------------------
  461.     # * Modify Skill and Item References to Variable IDs
  462.     #------------------------------------------------------------------------
  463.     if type == :variable
  464.       ($data_skills | $data_items).compact.map {|obj| obj.damage.formula }.
  465.         each do |formula|
  466.         formula.gsub!(/v\[(\d+)\]/) { "v[#{$1.to_i.dborg(*args)}]" }
  467.       end
  468.       save_data($data_skills, "Data/Skills.rvdata2")
  469.       save_data($data_items,  "Data/Items.rvdata2")
  470.     end
  471.     #------------------------------------------------------------------------
  472.     # * Modify Enemy References to Armor, Item, and Weapon IDs
  473.     #------------------------------------------------------------------------
  474.     if [:armor, :item, :weapon].include?(type)
  475.       $data_enemies.drop(1).each do |enemy|
  476.         enemy.drop_items.each do |item|
  477.           case type
  478.           when :armor
  479.             next unless item.kind == 3
  480.             item.data_id = item.data_id.dborg(*args)
  481.           when :item
  482.             next unless item.kind == 1
  483.             item.data_id = item.data_id.dborg(*args)
  484.           when :weapon
  485.             next unless item.kind == 2
  486.             item.data_id = item.data_id.dborg(*args)
  487.           end
  488.         end
  489.       end
  490.       save_data($data_enemies, "Data/Enemies.rvdata2")
  491.     end
  492.     #------------------------------------------------------------------------
  493.     # * Modify Actor and System References to Armor and Weapon IDs
  494.     #------------------------------------------------------------------------
  495.     if type == :armor || type == :weapon
  496.       $data_actors.drop(1).each do |actor|
  497.         case type
  498.         when :armor
  499.           [*2..4].each do |index|
  500.             actor.equips[index] = actor.equips[index].dborg(*args)
  501.           end
  502.           actor.equips[1] = actor.equips[1].dborg(*args) if (actor.features |
  503.             $data_classes[actor.class_id].features).none? {|feature|
  504.             feature.code == 55 }
  505.         when :weapon
  506.           actor.equips[0] = actor.equips[0].dborg(*args)
  507.           actor.equips[1] = actor.equips[1].dborg(*args) if (actor.features |
  508.             $data_classes[actor.class_id].features).any? {|feature|
  509.             feature.code == 55 }
  510.         end
  511.       end
  512.       save_data($data_actors, "Data/Actors.rvdata2")
  513.       $data_system.test_battlers.each do |battler|
  514.         case type
  515.         when :armor
  516.           [*2..4].each do |index|
  517.             battler.equips[index] = battler.equips[index].dborg(*args)
  518.           end
  519.           battler.equips[1] = battler.equips[1].dborg(*args) if
  520.             ($data_actors[battler.actor_id].features |
  521.             $data_classes[$data_actors[battler.actor_id].class_id].features).
  522.             none? {|feature| feature.code == 55 }
  523.         when :weapon
  524.           battler.equips[0] = battler.equips[0].dborg(*args)
  525.           battler.equips[1] = battler.equips[1].dborg(*args) if
  526.             ($data_actors[battler.actor_id].features |
  527.             $data_classes[$data_actors[battler.actor_id].class_id].features).
  528.             any? {|feature| feature.code == 55 }
  529.         end
  530.       end
  531.       save_data($data_system, "Data/System.rvdata2")
  532.     end
  533.     #------------------------------------------------------------------------
  534.     # * Modify Effect References to Common Event, Skill, and State IDs
  535.     #------------------------------------------------------------------------
  536.     if [:common_event, :skill, :state].include?(type)
  537.       ($data_skills | $data_items).compact.map {|obj| obj.effects }.flatten.
  538.         each do |effect|
  539.         case type
  540.         when :common_event
  541.           next unless effect.code == 44
  542.           effect.data_id = effect.data_id.dborg(*args)
  543.         when :skill
  544.           next unless effect.code == 43
  545.           effect.data_id = effect.data_id.dborg(*args)
  546.         when :state
  547.           next unless effect.code == 21 || effect.code == 22
  548.           effect.data_id = effect.data_id.dborg(*args)
  549.         end
  550.       end
  551.       save_data($data_skills, "Data/Skills.rvdata2")
  552.       save_data($data_items,  "Data/Items.rvdata2")
  553.     end
  554.     #------------------------------------------------------------------------
  555.     # * Modify Feature References to Skill and State IDs
  556.     #------------------------------------------------------------------------
  557.     if type == :skill || type == :state
  558.       ($data_actors | $data_classes | $data_weapons | $data_armors |
  559.         $data_enemies | $data_states).compact.map {|obj| obj.features }.flatten.
  560.         each do |feature|
  561.         case type
  562.         when :skill
  563.           next unless feature.code == 43 || feature.code == 44
  564.           feature.data_id = feature.data_id.dborg(*args)
  565.         when :state
  566.           next unless [13, 14, 32].include?(feature.code)
  567.           feature.data_id = feature.data_id.dborg(*args)
  568.         end
  569.       end
  570.       save_data($data_actors,  "Data/Actors.rvdata2")
  571.       save_data($data_classes, "Data/Classes.rvdata2")
  572.       save_data($data_weapons, "Data/Weapons.rvdata2")
  573.       save_data($data_armors,  "Data/Armors.rvdata2")
  574.       save_data($data_enemies, "Data/Enemies.rvdata2")
  575.       save_data($data_states,  "Data/States.rvdata2")
  576.     end
  577.     #------------------------------------------------------------------------
  578.     # * Modify Enemy References to Skill, State, and Switch IDs
  579.     #------------------------------------------------------------------------
  580.     if [:skill, :state, :switch].include?(type)
  581.       $data_enemies.drop(1).each do |enemy|
  582.         enemy.actions.each do |action|
  583.           case type
  584.           when :skill
  585.             action.skill_id = action.skill_id.dborg(*args)
  586.           when :state
  587.             next unless action.condition_type == 4
  588.             action.condition_param1 = action.condition_param1.dborg(*args)
  589.           when :switch
  590.             next unless action.condition_type == 6
  591.             action.condition_param1 = action.condition_param1.dborg(*args)
  592.           end
  593.         end
  594.       end
  595.       save_data($data_enemies, "Data/Enemies.rvdata2")
  596.     end
  597.   end
  598.   #--------------------------------------------------------------------------
  599.   # * Determine Type to Modify in Event Commands
  600.   #--------------------------------------------------------------------------
  601.   def self.change_event_references(*args)
  602.     case args[1]
  603.     when :actor
  604.       change_event_actors(*args)
  605.     when :animation
  606.       change_event_animations(*args)
  607.     when :armor
  608.       change_event_armors(*args)
  609.     when :class
  610.       change_event_classes(*args)
  611.     when :common_event
  612.       change_event_common_events(*args)
  613.     when :enemy
  614.       change_event_enemies(*args)
  615.     when :item
  616.       change_event_items(*args)
  617.     when :self_switch
  618.       change_event_self_switches(*args)
  619.     when :skill
  620.       change_event_skills(*args)
  621.     when :switch
  622.       change_event_switches(*args)
  623.     when :state
  624.       change_event_states(*args)
  625.     when :tileset
  626.       change_event_tilesets(*args)
  627.     when :troop
  628.       change_event_troops(*args)
  629.     when :variable
  630.       change_event_variables(*args)
  631.     when :weapon
  632.       change_event_weapons(*args)
  633.     end
  634.   end
  635.   #--------------------------------------------------------------------------
  636.   # * Modify Actor IDs in Event Commands
  637.   #--------------------------------------------------------------------------
  638.   def self.change_event_actors(*args)
  639.     page = args.pop
  640.     page.list.each do |command|
  641.       case command.code
  642.       when 111
  643.         next unless command.parameters[0] == 4
  644.         command.parameters[1] = command.parameters[1].dborg(*args)
  645.       when 122
  646.         next unless command.parameters[3..4] == [3, 3]
  647.         command.parameters[5] = command.parameters[5].dborg(*args)
  648.       when 129, 303, 319..322, 324
  649.         command.parameters[0] = command.parameters[0].dborg(*args)
  650.       when 311..318
  651.         next unless command.parameters[0] == 0
  652.         command.parameters[1] = command.parameters[1].dborg(*args)
  653.       when 339
  654.         next unless command.parameters[0] == 1
  655.         command.parameters[1] = command.parameters[1].dborg(*args)
  656.       when 401, 405
  657.         command.parameters[0].gsub!(/\\(N)\[(\d+)\]/i) {
  658.           "\\#{$1}[#{$2.to_i.dborg(*args)}]" }
  659.       end
  660.     end
  661.   end
  662.   #--------------------------------------------------------------------------
  663.   # * Modify Animation IDs in Event Commands
  664.   #--------------------------------------------------------------------------
  665.   def self.change_event_animations(*args)
  666.     page = args.pop
  667.     page.list.each do |command|
  668.       next unless command.code == 212 || command.code == 337
  669.       command.parameters[1] = command.parameters[1].dborg(*args)
  670.     end
  671.   end
  672.   #--------------------------------------------------------------------------
  673.   # * Modify Armor IDs in Event Commands
  674.   #--------------------------------------------------------------------------
  675.   def self.change_event_armors(*args)
  676.     page = args.pop
  677.     page.list.each do |command|
  678.       case command.code
  679.       when 111
  680.         next unless command.parameters.values_at(*[0, 2]) == [4, 5] ||
  681.           command.parameters[0] == 10
  682.         if command.parameters[0] == 10
  683.           command.parameters[1] = command.parameters[1].dborg(*args)
  684.         else
  685.           command.parameters[3] = command.parameters[3].dborg(*args)
  686.         end
  687.       when 122
  688.         next unless command.parameters[3..4] == [3, 2]
  689.         command.parameters[5] = command.parameters[5].dborg(*args)
  690.       when 128
  691.         command.parameters[0] = command.parameters[0].dborg(*args)
  692.       when 302, 605
  693.         next unless command.parameters[0] == 2
  694.         command.parameters[1] = command.parameters[1].dborg(*args)
  695.       when 319
  696.         next unless command.parameters[1] > 0
  697.         command.parameters[2] = command.parameters[2].dborg(*args)
  698.       end
  699.     end
  700.   end
  701.   #--------------------------------------------------------------------------
  702.   # * Modify Class IDs in Event Commands
  703.   #--------------------------------------------------------------------------
  704.   def self.change_event_classes(*args)
  705.     page = args.pop
  706.     page.list.each do |command|
  707.       case command.code
  708.       when 111
  709.         next unless command.parameters.values_at(*[0, 2]) == [4, 2]
  710.         command.parameters[3] = command.parameters[3].dborg(*args)
  711.       when 321
  712.         command.parameters[1] = command.parameters[1].dborg(*args)
  713.       end
  714.     end
  715.   end
  716.   #--------------------------------------------------------------------------
  717.   # * Modify Common Event IDs in Event Commands
  718.   #--------------------------------------------------------------------------
  719.   def self.change_event_common_events(*args)
  720.     page = args.pop
  721.     page.list.each do |command|
  722.       next unless command.code == 117
  723.       command.parameters[0] = command.parameters[0].dborg(*args)
  724.     end
  725.   end
  726.   #--------------------------------------------------------------------------
  727.   # * Modify Enemy IDs in Event Commands
  728.   #--------------------------------------------------------------------------
  729.   def self.change_event_enemies(*args)
  730.     page = args.pop
  731.     page.list.each do |command|
  732.       next unless command.code == 336
  733.       command.parameters[1] = command.parameters[1].dborg(*args)
  734.     end
  735.   end
  736.   #--------------------------------------------------------------------------
  737.   # * Modify Item IDs in Event Commands
  738.   #--------------------------------------------------------------------------
  739.   def self.change_event_items(*args)
  740.     page = args.pop
  741.     page.list.each do |command|
  742.       case command.code
  743.       when 111
  744.         next unless command.parameters[0] == 8
  745.         command.parameters[1] = command.parameters[1].dborg(*args)
  746.       when 122
  747.         next unless command.parameters[3..4] == [3, 0]
  748.         command.parameters[5] = command.parameters[5].dborg(*args)
  749.       when 126
  750.         command.parameters[0] = command.parameters[0].dborg(*args)
  751.       when 302, 605
  752.         next unless command.parameters[0] == 0
  753.         command.parameters[1] = command.parameters[1].dborg(*args)
  754.       end
  755.     end
  756.   end
  757.   #--------------------------------------------------------------------------
  758.   # * Modify Self Switches in Event Commands
  759.   #--------------------------------------------------------------------------
  760.   def self.change_event_self_switches(*args)
  761.     page = args.pop
  762.     page.list.each do |command|
  763.       case command.code
  764.       when 111
  765.         next unless command.parameters[0] == 2
  766.         command.parameters[1].dborg!(*args)
  767.       when 123
  768.         command.parameters[0].dborg!(*args)
  769.       end
  770.     end
  771.   end
  772.   #--------------------------------------------------------------------------
  773.   # * Modify Skill IDs in Event Commands
  774.   #--------------------------------------------------------------------------
  775.   def self.change_event_skills(*args)
  776.     page = args.pop
  777.     page.list.each do |command|
  778.       case command.code
  779.       when 111
  780.         next unless command.parameters.values_at(*[0, 2]) == [4, 3]
  781.         command.parameters[3] = command.parameters[3].dborg(*args)
  782.       when 318
  783.         command.parameters[3] = command.parameters[3].dborg(*args)
  784.       when 339
  785.         command.parameters[2] = command.parameters[2].dborg(*args)
  786.       end
  787.     end
  788.   end
  789.   #--------------------------------------------------------------------------
  790.   # * Modify Switch IDs in Event Commands
  791.   #--------------------------------------------------------------------------
  792.   def self.change_event_switches(*args)
  793.     page = args.pop
  794.     page.list.each do |command|
  795.       case command.code
  796.       when 111
  797.         next unless command.parameters[0] == 0
  798.         command.parameters[1] = command.parameters[1].dborg(*args)
  799.       when 121
  800.         command.parameters[0] = command.parameters[0].dborg(*args)
  801.         command.parameters[1] = command.parameters[1].dborg(*args)
  802.       when 205
  803.         command.parameters[1].list.each do |move_command|
  804.           next unless move_command.code == 27 || move_command.code == 28
  805.           move_command.parameters[0] = move_command.parameters[0].dborg(*args)
  806.         end
  807.       end
  808.     end
  809.   end
  810.   #--------------------------------------------------------------------------
  811.   # * Modify State IDs in Event Commands
  812.   #--------------------------------------------------------------------------
  813.   def self.change_event_states(*args)
  814.     page = args.pop
  815.     page.list.each do |command|
  816.       case command.code
  817.       when 111
  818.         next unless command.parameters.values_at(*[0, 2]) == [4, 6] ||
  819.           command.parameters.values_at(*[0, 2]) == [5, 1]
  820.         command.parameters[3] = command.parameters[3].dborg(*args)
  821.       when 313
  822.         command.parameters[3] = command.parameters[3].dborg(*args)
  823.       when 333
  824.         command.parameters[2] = command.parameters[2].dborg(*args)
  825.       end
  826.     end
  827.   end
  828.   #--------------------------------------------------------------------------
  829.   # * Modify Tileset IDs in Event Commands
  830.   #--------------------------------------------------------------------------
  831.   def self.change_event_tilesets(*args)
  832.     page = args.pop
  833.     page.list.each do |command|
  834.       next unless command.code == 282
  835.       command.parameters[0] = command.parameters[0].dborg(*args)
  836.     end
  837.   end
  838.   #--------------------------------------------------------------------------
  839.   # * Modify Troop IDs in Event Commands
  840.   #--------------------------------------------------------------------------
  841.   def self.change_event_troops(*args)
  842.     page = args.pop
  843.     page.list.each do |command|
  844.       next unless command.code == 301 && command.parameters[0] == 0
  845.       command.parameters[1] = command.parameters[1].dborg(*args)
  846.     end
  847.   end
  848.   #--------------------------------------------------------------------------
  849.   # * Modify Variable IDs in Event Commands
  850.   #--------------------------------------------------------------------------
  851.   def self.change_event_variables(*args)
  852.     page = args.pop
  853.     page.list.each do |command|
  854.       case command.code
  855.       when 103, 104
  856.         command.parameters[0] = command.parameters[0].dborg(*args)
  857.       when 111
  858.         next unless command.parameters[0] == 1
  859.         command.parameters[1] = command.parameters[1].dborg(*args)
  860.         next unless command.parameters[2] == 1
  861.         command.parameters[3] = command.parameters[3].dborg(*args)
  862.       when 122
  863.         command.parameters[0] = command.parameters[0].dborg(*args)
  864.         command.parameters[1] = command.parameters[1].dborg(*args)
  865.         next unless command.parameters[3] == 1
  866.         command.parameters[4] = command.parameters[4].dborg(*args)
  867.       when 125
  868.         next unless command.parameters[1] == 1
  869.         command.parameters[2] = command.parameters[2].dborg(*args)
  870.       when 126..128, 331, 332
  871.         next unless command.parameters[2] == 1
  872.         command.parameters[3] = command.parameters[3].dborg(*args)
  873.       when 201
  874.         next unless command.parameters[0] == 1
  875.         command.parameters[1] = command.parameters[1].dborg(*args)
  876.         command.parameters[2] = command.parameters[2].dborg(*args)
  877.         command.parameters[3] = command.parameters[3].dborg(*args)
  878.       when 202
  879.         next unless command.parameters[1] == 1
  880.         command.parameters[2] = command.parameters[2].dborg(*args)
  881.         command.parameters[3] = command.parameters[3].dborg(*args)
  882.         command.parameters[4] = command.parameters[4].dborg(*args)
  883.       when 203
  884.         next unless command.parameters[1] == 1
  885.         command.parameters[2] = command.parameters[2].dborg(*args)
  886.         command.parameters[3] = command.parameters[3].dborg(*args)
  887.       when 231, 232
  888.         next unless command.parameters[3] == 1
  889.         command.parameters[4] = command.parameters[4].dborg(*args)
  890.         command.parameters[5] = command.parameters[5].dborg(*args)
  891.       when 285
  892.         command.parameters[0] = command.parameters[0].dborg(*args)
  893.         next unless command.parameters[2] == 1
  894.         command.parameters[3] = command.parameters[3].dborg(*args)
  895.         command.parameters[4] = command.parameters[4].dborg(*args)
  896.       when 301, 313, 314, 318
  897.         next unless command.parameters[0] == 1
  898.         command.parameters[1] = command.parameters[1].dborg(*args)
  899.       when 311, 312, 315, 316
  900.         command.parameters[1] = command.parameters[1].dborg(*args) if
  901.           command.parameters[0] == 1
  902.         command.parameters[4] = command.parameters[4].dborg(*args) if
  903.           command.parameters[3] == 1
  904.       when 317
  905.         command.parameters[1] = command.parameters[1].dborg(*args) if
  906.           command.parameters[0] == 1
  907.         command.parameters[5] = command.parameters[5].dborg(*args) if
  908.           command.parameters[4] == 1
  909.       when 401, 405
  910.         command.parameters[0].gsub!(/\\(V)\[(\d+)\]/i) {
  911.           "\\#{$1}[#{$2.to_i.dborg(*args)}]" }
  912.       end
  913.     end
  914.   end
  915.   #--------------------------------------------------------------------------
  916.   # * Modify Weapon IDs in Event Commands
  917.   #--------------------------------------------------------------------------
  918.   def self.change_event_weapons(*args)
  919.     page = args.pop
  920.     page.list.each do |command|
  921.       case command.code
  922.       when 111
  923.         next unless command.parameters.values_at(*[0, 2]) == [4, 4] ||
  924.           command.parameters[0] == 9
  925.         if command.parameters[0] == 9
  926.           command.parameters[1] = command.parameters[1].dborg(*args)
  927.         else
  928.           command.parameters[3] = command.parameters[3].dborg(*args)
  929.         end
  930.       when 122
  931.         next unless command.parameters[3..4] == [3, 1]
  932.         command.parameters[5] = command.parameters[5].dborg(*args)
  933.       when 127
  934.         command.parameters[0] = command.parameters[0].dborg(*args)
  935.       when 302, 605
  936.         next unless command.parameters[0] == 1
  937.         command.parameters[1] = command.parameters[1].dborg(*args)
  938.       when 319
  939.         next unless command.parameters[1] == 0
  940.         command.parameters[2] = command.parameters[2].dborg(*args)
  941.       end
  942.     end
  943.   end
  944. end
  945.  
  946. #==============================================================================
  947. # ** SceneManager
  948. #------------------------------------------------------------------------------
  949. #  This module manages scene transitions. For example, it can handle
  950. # hierarchical structures such as calling the item screen from the main menu
  951. # or returning from the item screen to the main menu.
  952. #==============================================================================
  953.  
  954. module SceneManager
  955.   class <<self
  956.     alias gam_dborganizer_first_scene_class first_scene_class
  957.   end
  958.   #--------------------------------------------------------------------------
  959.   # * Get First Scene Class
  960.   #--------------------------------------------------------------------------
  961.   def self.first_scene_class
  962.     Gambit::DatabaseOrganizer::Enabled && !$BTEST ? Scene_DatabaseOrganizer :
  963.       gam_dborganizer_first_scene_class
  964.   end
  965. end
  966.  
  967. #==============================================================================
  968. # ** Window_DatabaseControls
  969. #------------------------------------------------------------------------------
  970. #  This window shows the control explanations on the database organizer screen.
  971. #==============================================================================
  972.  
  973. class Window_DatabaseControls < Window_Base
  974.   #--------------------------------------------------------------------------
  975.   # * Set Category Controls Text
  976.   #--------------------------------------------------------------------------
  977.   def set_category
  978.     contents.clear
  979.     change_color(system_color)
  980.     draw_text(0, 0, contents_width, line_height, 'Select a database to')
  981.     draw_text(0, line_height, contents_width, line_height,
  982.       'organize.')
  983.   end
  984.   #--------------------------------------------------------------------------
  985.   # * Set Database Controls Text
  986.   #--------------------------------------------------------------------------
  987.   def set_database(old_id)
  988.     contents.clear
  989.     cw = contents_width
  990.     change_color(system_color)
  991.     draw_text(0, line_height * 2, cw / 5 * 3, line_height, "Select #{old_id}:",
  992.       2)
  993.     draw_text(0, line_height * 3, cw / 5 * 3, line_height, "Delete #{old_id}:",
  994.       2)
  995.     draw_text(0, contents_height - line_height, cw / 2, line_height, 'Cancel:',
  996.       2)
  997.     change_color(normal_color)
  998.     draw_text(cw / 5 * 3, line_height * 2, cw, line_height, ' Z / Enter')
  999.     draw_text(cw / 5 * 3, line_height * 3, cw, line_height, ' D')
  1000.     draw_text(cw / 2, contents_height - line_height, cw / 2, line_height,
  1001.       ' X / Esc')
  1002.   end
  1003.   #--------------------------------------------------------------------------
  1004.   # * Set Pending Database Controls Text
  1005.   #--------------------------------------------------------------------------
  1006.   def set_pending(old_id, new_id)
  1007.     contents.clear
  1008.     cw = contents_width
  1009.     change_color(system_color)
  1010.     draw_text(0, line_height * 2, cw / 5 * 3, line_height,
  1011.       "Swap with #{new_id}:", 2)
  1012.     draw_text(0, line_height * 3, cw / 5 * 3, line_height,
  1013.       "Move to #{new_id}:", 2)
  1014.     draw_text(0, line_height * 4, cw / 5 * 3, line_height,
  1015.       "Insert at #{new_id}:", 2)
  1016.     draw_text(0, line_height * 5, cw / 5 * 3, line_height,
  1017.       "Update to #{new_id}:", 2)
  1018.     draw_text(0, contents_height - line_height, cw / 2, line_height, 'Cancel:',
  1019.       2)
  1020.     change_color(normal_color)
  1021.     draw_text(0, line_height * 0, cw, line_height, "Original ID: #{old_id}", 1)
  1022.     draw_text(cw / 5 * 3, line_height * 2, cw, line_height, ' Z / Enter')
  1023.     draw_text(cw / 5 * 3, line_height * 3, cw, line_height, ' S')
  1024.     draw_text(cw / 5 * 3, line_height * 4, cw, line_height, ' Shift')
  1025.     draw_text(cw / 5 * 3, line_height * 5, cw, line_height, ' A')
  1026.     draw_text(cw / 2, contents_height - line_height, cw / 2, line_height,
  1027.       ' X / Esc')
  1028.   end
  1029.   #--------------------------------------------------------------------------
  1030.   # * Set Map Database Controls Text
  1031.   #--------------------------------------------------------------------------
  1032.   def set_map
  1033.     contents.clear
  1034.     change_color(system_color)
  1035.     draw_text(0, 0, contents_width, line_height, 'Select a map to organize.')
  1036.   end
  1037.   #--------------------------------------------------------------------------
  1038.   # * Set Self Switch Database Controls Text
  1039.   #--------------------------------------------------------------------------
  1040.   def set_self_switch_database(map, old_id)
  1041.     contents.clear
  1042.     cw = contents_width
  1043.     change_color(system_color)
  1044.     draw_text(0, line_height * 2, cw / 5 * 3, line_height, "Select #{old_id}:",
  1045.       2)
  1046.     draw_text(0, contents_height - line_height, cw / 2, line_height, 'Cancel:',
  1047.       2)
  1048.     change_color(normal_color)
  1049.     draw_text(0, line_height * 0, cw, line_height, "Map: #{map}", 1)
  1050.     draw_text(cw / 5 * 3, line_height * 2, cw, line_height, ' Z / Enter')
  1051.     draw_text(cw / 2, contents_height - line_height, cw / 2, line_height,
  1052.       ' X / Esc')
  1053.   end
  1054.   #--------------------------------------------------------------------------
  1055.   # * Set Pending Self Switch Database Controls Text
  1056.   #--------------------------------------------------------------------------
  1057.   def set_pending_self_switch_database(map, old_id, new_id)
  1058.     contents.clear
  1059.     cw = contents_width
  1060.     change_color(system_color)
  1061.     draw_text(0, line_height * 3, cw / 5 * 3, line_height,
  1062.       "Swap with #{new_id}:", 2)
  1063.     draw_text(0, line_height * 4, cw / 5 * 3, line_height,
  1064.       "Update to #{new_id}:", 2)
  1065.     draw_text(0, contents_height - line_height, cw / 2, line_height, 'Cancel:',
  1066.       2)
  1067.     change_color(normal_color)
  1068.     draw_text(0, line_height * 0, cw, line_height, "Map: #{map}", 1)
  1069.     draw_text(0, line_height * 1, cw, line_height, "Original Switch: #{old_id}",
  1070.       1)
  1071.     draw_text(cw / 5 * 3, line_height * 3, cw, line_height, ' Z / Enter')
  1072.     draw_text(cw / 5 * 3, line_height * 4, cw, line_height, ' A')
  1073.     draw_text(cw / 2, contents_height - line_height, cw / 2, line_height,
  1074.       ' X / Esc')
  1075.   end
  1076. end
  1077.  
  1078. #==============================================================================
  1079. # ** Window_DatabaseList
  1080. #------------------------------------------------------------------------------
  1081. #  This window displays a list of database objects on the database organizer
  1082. # screen.
  1083. #==============================================================================
  1084.  
  1085. class Window_DatabaseList < Window_Selectable
  1086.   #--------------------------------------------------------------------------
  1087.   # * Public Instance Variables
  1088.   #--------------------------------------------------------------------------
  1089.   attr_reader   :pending_index            # Pending position
  1090.   attr_accessor :map                      # selected map id
  1091.   #--------------------------------------------------------------------------
  1092.   # * Object Initialization
  1093.   #--------------------------------------------------------------------------
  1094.   def initialize(x, y, width, height)
  1095.     super
  1096.     @category = :none
  1097.     @map = false
  1098.     @pending_index = -1
  1099.   end
  1100.   #--------------------------------------------------------------------------
  1101.   # * Set Category
  1102.   #--------------------------------------------------------------------------
  1103.   def category=(category)
  1104.     return if @category == category
  1105.     @category = category
  1106.     refresh unless @category == :none
  1107.     self.oy = 0
  1108.   end
  1109.   #--------------------------------------------------------------------------
  1110.   # * Set Controls Window
  1111.   #--------------------------------------------------------------------------
  1112.   def controls_window=(controls_window)
  1113.     @controls_window = controls_window
  1114.     call_update_help
  1115.   end
  1116.   #--------------------------------------------------------------------------
  1117.   # * Set Pending Position
  1118.   #--------------------------------------------------------------------------
  1119.   def pending_index=(index)
  1120.     last_pending_index = @pending_index
  1121.     @pending_index = index
  1122.     redraw_item(@pending_index)
  1123.     redraw_item(last_pending_index)
  1124.   end
  1125.   #--------------------------------------------------------------------------
  1126.   # * Get Number of Items
  1127.   #--------------------------------------------------------------------------
  1128.   def item_max
  1129.     @data ? @data.size : 1
  1130.   end
  1131.   #--------------------------------------------------------------------------
  1132.   # * Get Item Height
  1133.   #--------------------------------------------------------------------------
  1134.   def item_height
  1135.     @category == :actor ? 34 : line_height
  1136.   end
  1137.   #--------------------------------------------------------------------------
  1138.   # * Get Item
  1139.   #--------------------------------------------------------------------------
  1140.   def item
  1141.     @data && index >= 0 ? @data[index] : nil
  1142.   end
  1143.   #--------------------------------------------------------------------------
  1144.   # * Create Item List
  1145.   #--------------------------------------------------------------------------
  1146.   def make_item_list
  1147.     if @category == :self_switch && @map
  1148.       @data = DataManager::determine_type(@category)[0]
  1149.     elsif @category == :self_switch
  1150.       @data = $data_mapinfos.map do |map_id, info|
  1151.         map = RPG::BaseItem.new
  1152.         map.id = map_id
  1153.         map.name = info.name
  1154.         map
  1155.       end
  1156.     else
  1157.       @data = DataManager::determine_type(@category)[0].drop(1)
  1158.     end
  1159.   end
  1160.   #--------------------------------------------------------------------------
  1161.   # * Draw Item
  1162.   #--------------------------------------------------------------------------
  1163.   def draw_item(index)
  1164.     item = @data[index]
  1165.     if item
  1166.       rect = item_rect(index)
  1167.       rect.x += 2
  1168.       rect.width -= 4
  1169.       draw_item_background(index)
  1170.       draw_item_id(rect, item, index)
  1171.       draw_item_name(item, rect.x + 38, rect.y, rect.height)
  1172.     end
  1173.   end
  1174.   #--------------------------------------------------------------------------
  1175.   # * Draw Background for Item
  1176.   #--------------------------------------------------------------------------
  1177.   def draw_item_background(index)
  1178.     if index == @pending_index
  1179.       contents.fill_rect(item_rect(index), pending_color)
  1180.     end
  1181.   end
  1182.   #--------------------------------------------------------------------------
  1183.   # * Draw ID of Item
  1184.   #--------------------------------------------------------------------------
  1185.   def draw_item_id(rect, item, index)
  1186.     return if @map
  1187.     if @category == :switch || @category == :variable
  1188.       draw_text(rect, sprintf("%03d:", index + 1))
  1189.     else
  1190.       draw_text(rect, sprintf("%03d:", item.id))
  1191.     end
  1192.   end
  1193.   #--------------------------------------------------------------------------
  1194.   # * Draw Item Name
  1195.   #--------------------------------------------------------------------------
  1196.   def draw_item_name(item, x, y, height)
  1197.     return unless item
  1198.     case @category
  1199.     when :actor
  1200.       draw_actor_graphic(item, x + 12, y + 33)
  1201.       offset = 26
  1202.     when :skill, :item, :weapon, :armor, :state
  1203.       draw_icon(item.icon_index, x, y)
  1204.       offset = 24
  1205.     else
  1206.       offset = 0
  1207.     end
  1208.     width = contents_width - x - offset
  1209.     if @category == :switch || @category == :variable || @map
  1210.       draw_text(x + offset, y, width, height, item)
  1211.     else
  1212.       draw_text(x + offset, y, width, height, item.name)
  1213.     end
  1214.   end
  1215.   #--------------------------------------------------------------------------
  1216.   # * Update Help Text
  1217.   #--------------------------------------------------------------------------
  1218.   def update_help
  1219.     @help_window.set_item(item) if item.respond_to?(:description)
  1220.     if @map && @pending_index >= 0
  1221.       @controls_window.set_pending_self_switch_database(@map.name,
  1222.         @data[@pending_index], item)
  1223.     elsif @pending_index >= 0
  1224.       @controls_window.set_pending(@pending_index + 1, index + 1)
  1225.     elsif @category == :self_switch && !map
  1226.       @controls_window.set_map
  1227.     elsif @map
  1228.       @controls_window.set_self_switch_database(@map.name, item)
  1229.     else
  1230.       @controls_window.set_database(index + 1)
  1231.     end
  1232.   end
  1233.   #--------------------------------------------------------------------------
  1234.   # * Redraw Item
  1235.   #--------------------------------------------------------------------------
  1236.   def redraw_item(index)
  1237.     @data[index] = DataManager::determine_type(@category)[0].drop(1)[index] if
  1238.       @category != :self_switch
  1239.     super
  1240.   end
  1241.   #--------------------------------------------------------------------------
  1242.   # * Redraw Range of Items
  1243.   #--------------------------------------------------------------------------
  1244.   def refresh_range(first, last)
  1245.     for number in [*[first, last].min..[first, last].max]
  1246.       redraw_item(number)
  1247.     end
  1248.   end
  1249.   #--------------------------------------------------------------------------
  1250.   # * Refresh
  1251.   #--------------------------------------------------------------------------
  1252.   def refresh
  1253.     make_item_list
  1254.     create_contents
  1255.     draw_all_items
  1256.   end
  1257.   #--------------------------------------------------------------------------
  1258.   # * Handling Processing for OK and Cancel Etc.
  1259.   #--------------------------------------------------------------------------
  1260.   def process_handling
  1261.     super
  1262.     return unless open? && active
  1263.     return process_delete if Input.trigger?(:Z) && @category != :self_switch
  1264.     return unless @pending_index >= 0
  1265.     return process_move   if Input.trigger?(:Y) && @category != :self_switch
  1266.     return process_insert if Input.trigger?(:A) && @category != :self_switch
  1267.     return process_update if Input.trigger?(:X)
  1268.   end
  1269.   #--------------------------------------------------------------------------
  1270.   # * Processing When OK Button Is Pressed
  1271.   #--------------------------------------------------------------------------
  1272.   def process_ok(task_type = :ok)
  1273.     Sound.play_ok
  1274.     Input.update
  1275.     deactivate
  1276.     call_handler(task_type)
  1277.   end
  1278.   #--------------------------------------------------------------------------
  1279.   # * Processing When Move Button Is Pressed
  1280.   #--------------------------------------------------------------------------
  1281.   def process_move
  1282.     process_ok(:move)
  1283.   end
  1284.   #--------------------------------------------------------------------------
  1285.   # * Processing When Insert Button Is Pressed
  1286.   #--------------------------------------------------------------------------
  1287.   def process_insert
  1288.     process_ok(:insert)
  1289.   end
  1290.   #--------------------------------------------------------------------------
  1291.   # * Processing When Delete Button Is Pressed
  1292.   #--------------------------------------------------------------------------
  1293.   def process_delete
  1294.     @pending_index >= 0 ? process_cancel : process_ok(:delete)
  1295.   end
  1296.   #--------------------------------------------------------------------------
  1297.   # * Processing When Update Button Is Pressed
  1298.   #--------------------------------------------------------------------------
  1299.   def process_update
  1300.     process_ok(:update)
  1301.   end
  1302. end
  1303.  
  1304. #==============================================================================
  1305. # ** Window_DatabaseCategory
  1306. #------------------------------------------------------------------------------
  1307. #  This window is for selecting a category from a list of databases on the
  1308. # database organizer screen.
  1309. #==============================================================================
  1310.  
  1311. class Window_DatabaseCategory < Window_Command
  1312.   #--------------------------------------------------------------------------
  1313.   # * Public Instance Variables
  1314.   #--------------------------------------------------------------------------
  1315.   attr_reader   :database_window
  1316.   #--------------------------------------------------------------------------
  1317.   # * Object Initialization
  1318.   #--------------------------------------------------------------------------
  1319.   def initialize
  1320.     super(0, 0)
  1321.   end
  1322.   #--------------------------------------------------------------------------
  1323.   # * Get Window Width
  1324.   #--------------------------------------------------------------------------
  1325.   def window_width
  1326.     Graphics.width
  1327.   end
  1328.   #--------------------------------------------------------------------------
  1329.   # * Get Number of Lines to Show
  1330.   #--------------------------------------------------------------------------
  1331.   def visible_line_number
  1332.     3
  1333.   end
  1334.   #--------------------------------------------------------------------------
  1335.   # * Get Digit Count
  1336.   #--------------------------------------------------------------------------
  1337.   def col_max
  1338.     return 5
  1339.   end
  1340.   #--------------------------------------------------------------------------
  1341.   # * Get Spacing for Items Arranged Side by Side
  1342.   #--------------------------------------------------------------------------
  1343.   def spacing
  1344.     return 0
  1345.   end
  1346.   #--------------------------------------------------------------------------
  1347.   # * Get Rectangle for Drawing Items (for Text)
  1348.   #--------------------------------------------------------------------------
  1349.   def item_rect_for_text(index)
  1350.     item_rect(index)
  1351.   end
  1352.   #--------------------------------------------------------------------------
  1353.   # * Frame Update
  1354.   #--------------------------------------------------------------------------
  1355.   def update
  1356.     super
  1357.     @database_window.category = current_symbol if @database_window
  1358.   end
  1359.   #--------------------------------------------------------------------------
  1360.   # * Create Command List
  1361.   #--------------------------------------------------------------------------
  1362.   def make_command_list
  1363.     add_command('Actors',        :actor)
  1364.     add_command('Classes',       :class)
  1365.     add_command('Skills',        :skill)
  1366.     add_command('Items',         :item)
  1367.     add_command('Weapons',       :weapon)
  1368.     add_command('Armors',        :armor)
  1369.     add_command('Enemies',       :enemy)
  1370.     add_command('Troops',        :troop)
  1371.     add_command('States',        :state)
  1372.     add_command('Animations',    :animation)
  1373.     add_command('Tilesets',      :tileset)
  1374.     add_command('Common Events', :common_event)
  1375.     add_command('Switches',      :switch)
  1376.     add_command('Variables',     :variable)
  1377.     add_command('Self Switches', :self_switch)
  1378.   end
  1379.   #--------------------------------------------------------------------------
  1380.   # * Get Alignment
  1381.   #--------------------------------------------------------------------------
  1382.   def alignment
  1383.     return 1
  1384.   end
  1385.   #--------------------------------------------------------------------------
  1386.   # * Set Database Window
  1387.   #--------------------------------------------------------------------------
  1388.   def database_window=(database_window)
  1389.     @database_window = database_window
  1390.     update
  1391.   end
  1392. end
  1393.  
  1394. #==============================================================================
  1395. # ** Scene_DatabaseOrganizer
  1396. #------------------------------------------------------------------------------
  1397. #  This class performs the database organizer screen processing.
  1398. #==============================================================================
  1399.  
  1400. class Scene_DatabaseOrganizer < Scene_Base
  1401.   #--------------------------------------------------------------------------
  1402.   # * Start Processing
  1403.   #--------------------------------------------------------------------------
  1404.   def start
  1405.     Graphics.resize_screen(640, 480)
  1406.     super
  1407.     @task_count = 0
  1408.     create_help_window
  1409.     create_category_window
  1410.     create_database_window
  1411.     create_controls_window
  1412.   end
  1413.   #--------------------------------------------------------------------------
  1414.   # * Create Help Window
  1415.   #--------------------------------------------------------------------------
  1416.   def create_help_window
  1417.     @help_window = Window_Help.new
  1418.     @help_window.y = Graphics.height - @help_window.height
  1419.     @help_window.viewport = @viewport
  1420.   end
  1421.   #--------------------------------------------------------------------------
  1422.   # * Create Category Window
  1423.   #--------------------------------------------------------------------------
  1424.   def create_category_window
  1425.     @category_window = Window_DatabaseCategory.new
  1426.     @category_window.viewport = @viewport
  1427.     @category_window.help_window = @help_window
  1428.     @category_window.set_handler(:ok,     method(:on_category_ok))
  1429.     @category_window.set_handler(:cancel, method(:on_category_cancel))
  1430.   end
  1431.   #--------------------------------------------------------------------------
  1432.   # * Create Database Window
  1433.   #--------------------------------------------------------------------------
  1434.   def create_database_window
  1435.     wy = @category_window.height
  1436.     wh = Graphics.height - wy - @help_window.height
  1437.     @database_window = Window_DatabaseList.new(0, wy, Graphics.width / 2, wh)
  1438.     @database_window.viewport = @viewport
  1439.     @database_window.help_window = @help_window
  1440.     @database_window.set_handler(:ok,     method(:on_database_ok))
  1441.     @database_window.set_handler(:move,   method(:on_database_move))
  1442.     @database_window.set_handler(:insert, method(:on_database_insert))
  1443.     @database_window.set_handler(:delete, method(:on_database_delete))
  1444.     @database_window.set_handler(:update, method(:on_database_update))
  1445.     @database_window.set_handler(:cancel, method(:on_database_cancel))
  1446.     @category_window.database_window = @database_window
  1447.   end
  1448.   #--------------------------------------------------------------------------
  1449.   # * Create Controls Window
  1450.   #--------------------------------------------------------------------------
  1451.   def create_controls_window
  1452.     wx = @database_window.width
  1453.     wy = @category_window.height
  1454.     wh = @database_window.height
  1455.     @controls_window = Window_DatabaseControls.new(wx, wy, wx, wh)
  1456.     @controls_window.viewport = @viewport
  1457.     @controls_window.set_category
  1458.     @database_window.controls_window = @controls_window
  1459.   end
  1460.   #--------------------------------------------------------------------------
  1461.   # * Category [OK]
  1462.   #--------------------------------------------------------------------------
  1463.   def on_category_ok
  1464.     @database_window.activate
  1465.     @database_window.select(0)
  1466.   end
  1467.   #--------------------------------------------------------------------------
  1468.   # * Category [Cancel]
  1469.   #--------------------------------------------------------------------------
  1470.   def on_category_cancel
  1471.     DataManager::database_organizer_done
  1472.   end
  1473.   #--------------------------------------------------------------------------
  1474.   # * Database [OK]
  1475.   #--------------------------------------------------------------------------
  1476.   def on_database_ok(task_type = :swap)
  1477.     if @database_window.pending_index >= 0
  1478.       DataManager::organize_database(task(task_type), @task_count)
  1479.       @task_count += 1
  1480.       @database_window.refresh_range(@database_window.pending_index,
  1481.         @database_window.index) if task_type == :insert
  1482.       @database_window.pending_index = -1
  1483.       @database_window.redraw_item(@database_window.index) unless
  1484.         @category_window.current_symbol == :self_switch
  1485.     elsif task_type == :delete
  1486.       DataManager::organize_database(task(task_type), @task_count)
  1487.       @task_count += 1
  1488.       @database_window.refresh_range(@database_window.index,
  1489.         @database_window.item_max - 1)
  1490.     elsif @category_window.current_symbol == :self_switch &&
  1491.       !@database_window.map
  1492.       @database_window.map = @database_window.item
  1493.       @database_window.select(0)
  1494.       @database_window.refresh
  1495.     else
  1496.       @database_window.pending_index = @database_window.index
  1497.     end
  1498.     @database_window.activate
  1499.   end
  1500.   #--------------------------------------------------------------------------
  1501.   # * Database [Move]
  1502.   #--------------------------------------------------------------------------
  1503.   def on_database_move
  1504.     on_database_ok(:move)
  1505.   end
  1506.   #--------------------------------------------------------------------------
  1507.   # * Database [Insert]
  1508.   #--------------------------------------------------------------------------
  1509.   def on_database_insert
  1510.     on_database_ok(:insert)
  1511.   end
  1512.   #--------------------------------------------------------------------------
  1513.   # * Database [Delete]
  1514.   #--------------------------------------------------------------------------
  1515.   def on_database_delete
  1516.     on_database_ok(:delete)
  1517.   end
  1518.   #--------------------------------------------------------------------------
  1519.   # * Database [Update]
  1520.   #--------------------------------------------------------------------------
  1521.   def on_database_update
  1522.     on_database_ok(:update)
  1523.   end
  1524.   #--------------------------------------------------------------------------
  1525.   # * Database [Cancel]
  1526.   #--------------------------------------------------------------------------
  1527.   def on_database_cancel
  1528.     if @database_window.pending_index >= 0
  1529.       @database_window.pending_index = -1
  1530.       @database_window.activate
  1531.     elsif @database_window.map
  1532.       @database_window.select(@database_window.map.id - 1)
  1533.       @database_window.map = false
  1534.       @database_window.refresh
  1535.       @database_window.activate
  1536.     else
  1537.       @controls_window.set_category
  1538.       @database_window.unselect
  1539.       @category_window.activate
  1540.     end
  1541.   end
  1542.   #--------------------------------------------------------------------------
  1543.   # * Get Task Array
  1544.   #--------------------------------------------------------------------------
  1545.   def task(task_type)
  1546.     if task_type == :delete
  1547.       task = [task_type, @category_window.current_symbol,
  1548.         @database_window.index + 1]
  1549.     elsif @category_window.current_symbol == :self_switch
  1550.       array = DataManager::determine_type(:self_switch)[0]
  1551.       task = [task_type, @category_window.current_symbol,
  1552.         array[@database_window.pending_index], array[@database_window.index],
  1553.         @database_window.map.id]
  1554.     else
  1555.       task = [task_type, @category_window.current_symbol,
  1556.         @database_window.pending_index + 1, @database_window.index + 1]
  1557.     end
  1558.   end
  1559. end
  1560.  
  1561. class Array
  1562.   #--------------------------------------------------------------------------
  1563.   # * Swap Two Elements by Indices
  1564.   #--------------------------------------------------------------------------
  1565.   def swap!(a, b)
  1566.     self[a], self[b] = self[b], self[a]
  1567.     self
  1568.   end
  1569. end
  1570.  
  1571. class Integer
  1572.   #--------------------------------------------------------------------------
  1573.   # * Determine Database Organizer Reference ID
  1574.   #--------------------------------------------------------------------------
  1575.   def dborg(task_type, type, old_id, new_id)
  1576.     value = self
  1577.     case task_type
  1578.     when :swap
  1579.       if self == old_id
  1580.         value = new_id
  1581.       elsif self == new_id
  1582.         value = old_id
  1583.       end
  1584.     when :move, :update
  1585.       value = new_id if self == old_id
  1586.     when :insert
  1587.       unless self < [old_id, new_id].min || self > [old_id, new_id].max
  1588.         if self == old_id
  1589.           value = new_id
  1590.         elsif old_id < new_id
  1591.           value -= 1
  1592.         elsif old_id > new_id
  1593.           value += 1
  1594.         end
  1595.       end
  1596.     when :delete
  1597.       unless self < old_id
  1598.         if self == old_id
  1599.           value = new_id
  1600.         else
  1601.           value -= 1
  1602.         end
  1603.       end
  1604.     end
  1605.     value
  1606.   end
  1607. end
  1608.  
  1609. class String
  1610.   #--------------------------------------------------------------------------
  1611.   # * Determine Database Organizer Reference Self Switch
  1612.   #--------------------------------------------------------------------------
  1613.   def dborg!(task_type, type, old_id, new_id, map_id)
  1614.     value = self
  1615.     case task_type
  1616.     when :swap
  1617.       if self == old_id
  1618.         value = new_id
  1619.       elsif self == new_id
  1620.         value = old_id
  1621.       end
  1622.     when :update
  1623.       value = new_id if self == old_id
  1624.     end
  1625.     self.replace(value)
  1626.   end
  1627. end
Advertisement
Add Comment
Please, Sign In to add comment