Advertisement
Solistra

SES Switches and Variables

Jul 16th, 2013
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 25.67 KB | None | 0 0
  1. #═╦═════════════════════════════════════════════════════════════════════════════
  2. # ║ § Switches and Variables (1.0) by Solistra           [License: CC BY-SA 3.0]
  3. # ║                                                             <RMVX Ace>
  4. #═╬═════════════════════════════════════════════════════════════════════════════
  5. # ║ § Change Log
  6. #─╫─────────────────────────────────────────────────────────────────────────────
  7. # ║ v1.0 (July 16th, 2013) - Initial release
  8. # ║
  9. #═╬═════════════════════════════════════════════════════════════════════════════
  10. # ║ § Summary
  11. #─╫─────────────────────────────────────────────────────────────────────────────
  12. # ║ This script allows you to access and use switches, variables, and event
  13. # ║ self-switches in a more flexible manner than that used by RMVX Ace by
  14. # ║ default. Most notably, you can access these elements by name rather than
  15. # ║ by numerical index only -- which is particularly useful with event self-
  16. # ║ switches. This script also allows you to specify default starting values
  17. # ║ for switches, variables, and event self-switches.
  18. # ║
  19. #═╬═════════════════════════════════════════════════════════════════════════════
  20. # ║ § Script Calls
  21. #─╫─────────────────────────────────────────────────────────────────────────────
  22. # ║ Switches, variables, and event self-switches may be accessed normally when
  23. # ║ using this script if desired. For instance, these are still acceptable:
  24. # ║   $game_switches[5]
  25. # ║   $game_self_switches[[22, 5, "C"]]
  26. # ║
  27. # ║ In addition to this, however, any of these elements may be accessed by the
  28. # ║ names that the user has designated for them in the editor like so:
  29. # ║   $game_switches["Switch name"]
  30. # ║   $game_variables["Variable name"]
  31. # ║   $game_self_switches["Map name", "Event name", "A"]
  32. # ║
  33. # ║ In the case of event self-switches, you may optionally use map and event
  34. # ║ names or not. The script call $game_self_switches["Map name", 5, "A"] is
  35. # ║ still perfectly valid to access the "A" switch of event ID 5 on "Map name".
  36. # ║
  37. # ║ This script also allows you to toggle switch and event self-switch values:
  38. # ║   $game_switches.toggle!("Switch name or ID")
  39. # ║   $game_self_switches.toggle!("Map name", "Event name", "A")
  40. # ║
  41. # ║ For scripters, you may now also add actions to be evaluated whenever an
  42. # ║ assignment is performed on switches, variables, or event self-switches.
  43. # ║ Adding an action to be evaluated is simple:
  44. # ║   $game_switches.actions.push('puts "#{index} assigned to #{value}"')
  45. # ║
  46. # ║ Actions are strings which are evaluated within the context of the instance.
  47. # ║ For more detailed information, consult the documentation of this script.
  48. # ║
  49. #═╬═════════════════════════════════════════════════════════════════════════════
  50. # ║ § Caveats
  51. #─╫─────────────────────────────────────────────────────────────────────────────
  52. # ║ Accessing elements by name presents a problem if there is more than one
  53. # ║ switch, variable, map, or event with the same name. In this case, the first
  54. # ║ name found is the one used to access its associated element.
  55. # ║
  56. # ║ Also, while the previous form of indexing event self-switches still works,
  57. # ║ it has actually been changed. Before, you had to pass an array -- now, you
  58. # ║ are encouraged not to. Example:
  59. # ║   $game_self_switches[[1, 1, 'A']] = true # Previous form.
  60. # ║   $game_self_switches[1, 1, 'A']   = true # New form.
  61. # ║
  62. #═╬═════════════════════════════════════════════════════════════════════════════
  63. # ║ ▼ module SES
  64. #═╩═════════════════════════════════════════════════════════════════════════════
  65. module SES
  66.   module Switches
  67.     #═╦═════════════════════════════════════════════════════════════════════════
  68.     # ║ α BEGIN CONFIGURATION
  69.     #═╩═════════════════════════════════════════════════════════════════════════
  70.     # An array of switch indices which should be enabled by default whenever the
  71.     # game is started. Switch indices may be given by name or literal index.
  72.     # Example:
  73.     #   INITIALIZE = [ 1, 5, 'Switch name', 99 ]
  74.     INITIALIZE = []
  75.     #═╦═════════════════════════════════════════════════════════════════════════
  76.     # ║ Ω END CONFIGURATION
  77.     #═╩═════════════════════════════════════════════════════════════════════════
  78.   end
  79.  
  80.   module Variables
  81.     #═╦═════════════════════════════════════════════════════════════════════════
  82.     # ║ α BEGIN CONFIGURATION
  83.     #═╩═════════════════════════════════════════════════════════════════════════
  84.     # A hash of variables where indices are keys and initial values are the hash
  85.     # values. Variable indices may be given by name or literal index.
  86.     # Example:
  87.     #    INITIALIZE = {
  88.     #      'Variable name' => 25, # Don't forget the comma.
  89.     #      1 => 10
  90.     #    }
  91.     INITIALIZE = {
  92.      
  93.     }
  94.     #═╦═════════════════════════════════════════════════════════════════════════
  95.     # ║ Ω END CONFIGURATION
  96.     #═╩═════════════════════════════════════════════════════════════════════════
  97.   end
  98.  
  99.   module SelfSwitches
  100.     #═╦═════════════════════════════════════════════════════════════════════════
  101.     # ║ α BEGIN CONFIGURATION
  102.     #═╩═════════════════════════════════════════════════════════════════════════
  103.     # An array of self switches where elements are arrays referencing the event
  104.     # and the desired self switch. The map and event indices of the array may be
  105.     # given by name or literal index. The order of elements in the array is
  106.     # fixed, and must always be given as [ map, event, self_switch ].
  107.     # Example:
  108.     #    INITIALIZE = [
  109.     #      [ 'Map Name', 'Event Name', 'A' ], # Don't forget the comma.
  110.     #      [ 1, 5, 'D' ]
  111.     #    ]
  112.     INITIALIZE = [
  113.      
  114.     ]
  115.     #═╦═════════════════════════════════════════════════════════════════════════
  116.     # ║ Ω END CONFIGURATION
  117.     #═╩═════════════════════════════════════════════════════════════════════════
  118.   end
  119.   #═╦═══════════════════════════════════════════════════════════════════════════
  120.   # ║ ▼ module SES::DataWrapper
  121.   #═╩═══════════════════════════════════════════════════════════════════════════
  122.   # This module provides a simple wrapper around the data structure represented
  123.   # by the @data instance variable. Note: this module is intended to be mixed in
  124.   # to user-created classes rather than used on its own.
  125.   module DataWrapper
  126.     include Enumerable
  127.    
  128.     # Classes mixing in this module are expected to provide a data structure to
  129.     # wrap around which is represented by the @data instance variable. Arrays
  130.     # are recommended.
  131.     attr_reader :data
  132.    
  133.     # Classes mixing in this module are expected to provide an @actions instance
  134.     # variable corresponding to an array of actions to be carried out by the
  135.     # class on assignment. Actions must be written as strings to be passed to
  136.     # Kernel.instance_eval.
  137.     attr_reader :actions
  138.    
  139.     # Provides a default action to be used by classes mixing in this module.
  140.     # This action will cause the map to refresh if an instance of $game_map is
  141.     # defined.
  142.     def self.default_action
  143.       '$game_map.need_refresh = true if $game_map'
  144.     end
  145.    
  146.     # Returns inspection of the base data.
  147.     def inspect
  148.       @data.inspect
  149.     end
  150.     alias :to_s :inspect
  151.    
  152.     # Returns the value stored in the base data structure's given index.
  153.     def [](index)
  154.       @data[obtain(index)]
  155.     end
  156.    
  157.     # Assigns the given value to the given index, then calls the on_assignment
  158.     # method to carry out actions post-assignment.
  159.     def []=(index, value)
  160.       @data[obtain(index)] = value
  161.       on_assignment(index, value)
  162.     end
  163.    
  164.     # Returns the underlying base data structure as an array when an array
  165.     # representation is requested.
  166.     def to_ary
  167.       @data.to_ary rescue @data.to_a
  168.     end
  169.     alias :to_a :to_ary
  170.    
  171.     # Facilitates Enumerable functionality for the wrapper. This method allows
  172.     # the use of many others that should be familiar to Ruby scripters (such as
  173.     # each_with_index, collect, reduce, and so on). Returns the underlying data
  174.     # structure if no block is given.
  175.     def each
  176.       block_given? ? @data.each { |element| yield element } : @data
  177.     end
  178.    
  179.     # Evaluates all actions for the wrapper after an assignment has been
  180.     # performed. The @actions instance variable will be initialized to a default
  181.     # array if it has not already been assigned.
  182.     def on_assignment(index, value)
  183.       (@actions ||= [ self.default_action ]).each do |action|
  184.         instance_eval(action)
  185.       end
  186.     end
  187.    
  188.     # Method intended to facilitate finding an index for the base data class by
  189.     # name. Any class mixing in this module is expected to redefine this method
  190.     # for specific use cases.
  191.     def obtain(index)
  192.       index
  193.     end
  194.   end
  195.   #═╦═══════════════════════════════════════════════════════════════════════════
  196.   # ║ ▲ module SES::DataWrapper
  197.   #═╩═══════════════════════════════════════════════════════════════════════════
  198. end
  199. #═╦═════════════════════════════════════════════════════════════════════════════
  200. # ║ ▲ module SES
  201. #═╩═════════════════════════════════════════════════════════════════════════════
  202.             ($imported ||= {})['SES - Switches and Variables'] = 1.0
  203. #═╦═════════════════════════════════════════════════════════════════════════════
  204. # ║ ▼ class Game_Switches
  205. #═╩═════════════════════════════════════════════════════════════════════════════
  206. class Game_Switches
  207.   include SES::DataWrapper
  208.  
  209.   def initialize
  210.     # The base data array. The first element is not used by RPG Maker VX Ace.
  211.     @data    = [ nil ]
  212.    
  213.     # An array of actions to be carried out in sequence when the on_assignment
  214.     # method is called on the wrapper.
  215.     @actions = [ SES::DataWrapper.default_action ]
  216.    
  217.     # Initialization of switches specified in the SES::Switches module. All
  218.     # given switches are initialized to a true value.
  219.     SES::Switches::INITIALIZE.each { |index| self[index] = true }
  220.   end
  221.  
  222.   # Redefines inspection of the wrapper class to the wrapper's representation as
  223.   # an array. For the unaltered data array, use $game_switches.data.inspect.
  224.   def inspect
  225.     to_ary.inspect
  226.   end
  227.   alias :to_s :inspect
  228.  
  229.   # Returns an array where all elements of the base data array have been given
  230.   # explicit true, false, or nil values to reflect the intended use of the
  231.   # class. Use $game_switches.data to obtain the unaltered array.
  232.   def to_ary
  233.     @data.map { |element| element ? true : false unless element.nil? }
  234.   end
  235.   alias :to_a :to_ary
  236.  
  237.   # Redefinition of the Game_Switches class' original [] method to allow the
  238.   # ability to reference indices by name as well as their actual index. Also
  239.   # explicitly returns only true or false values rather than the actual contents
  240.   # of the base data array (to access actual contents, use the data method).
  241.   def [](index)
  242.     @data[obtain(index)] ? true : false
  243.   end
  244.  
  245.   # Redefinition of the Game_Switches class' original []= method to allow the
  246.   # ability to reference indices by name as well as their actual index. Values
  247.   # passed to this method are automatically converted to true or false. Also
  248.   # calls the on_assignment method to carry out actions post-assignment.
  249.   def []=(index, value)
  250.     index = obtain(index)
  251.     @data[index] = value ? true : false
  252.     on_assignment(index, value)
  253.   end
  254.  
  255.   # Toggles the switch at the given index. Note: toggled values will always be
  256.   # "true" or "false," not necessarily the return value of a unary ! operation
  257.   # on the element contained by the index due to the class' []= method.
  258.   def toggle!(index)
  259.     index = obtain(index)
  260.     self[index] = begin !@data[index] rescue !self[index] end
  261.   end
  262.  
  263.   # Attempts to find the location of the given index by name. Returns the given
  264.   # index if the name cannot be found.
  265.   def obtain(index)
  266.     $data_system.switches.index(index) || index
  267.   end
  268. end
  269. #═╦═════════════════════════════════════════════════════════════════════════════
  270. # ║ ▲ class Game_Switches
  271. #─╫─────────────────────────────────────────────────────────────────────────────
  272. # ║ ▼ class Game_Variables
  273. #═╩═════════════════════════════════════════════════════════════════════════════
  274. class Game_Variables
  275.   include SES::DataWrapper
  276.  
  277.   def initialize
  278.     # The base data array. The first element is not used by RPG Maker VX Ace.
  279.     @data    = [ nil ]
  280.    
  281.     # An array of actions to be carried out in sequence when the on_assignment
  282.     # method is called on the wrapper.
  283.     @actions = [ SES::DataWrapper.default_action ]
  284.    
  285.     # Initialization of variables defined in the SES::Variables module.
  286.     SES::Variables::INITIALIZE.each { |index, value| self[index] = value }
  287.   end
  288.  
  289.   # Redefinition of the Game_Variables class' original [] method to allow the
  290.   # ability to reference indices by name as well as their actual index. Also
  291.   # returns a 0 value if the value at the index is false or nil.
  292.   def [](index)
  293.     @data[obtain(index)] || 0
  294.   end
  295.  
  296.   # Redefinition of the Game_Variables class' original []= method to allow the
  297.   # ability to reference indices by name as well as their actual index. Also
  298.   # calls the on_assignment method to carry out actions post-assignment.
  299.   def []=(index, value)
  300.     index = obtain(index)
  301.     @data[index] = value
  302.     on_assignment(index, value)
  303.   end
  304.  
  305.   # Attempts to find the location of the given index by name. Returns the given
  306.   # index if the name cannot be found.
  307.   def obtain(index)
  308.     $data_system.variables.index(index) || index
  309.   end
  310. end
  311. #═╦═════════════════════════════════════════════════════════════════════════════
  312. # ║ ▲ class Game_Variables
  313. #─╫─────────────────────────────────────────────────────────────────────────────
  314. # ║ ▼ class Game_SelfSwitches
  315. #═╩═════════════════════════════════════════════════════════════════════════════
  316. class Game_SelfSwitches
  317.   include SES::DataWrapper
  318.  
  319.   def initialize
  320.     # The base data hash.
  321.     @data    = {}
  322.    
  323.     # An array of actions to be carried out in sequence when the on_assignment
  324.     # method is called on the wrapper.
  325.     @actions = [ SES::DataWrapper.default_action ]
  326.    
  327.     # Initialization of switches specified in the SES::SelfSwitches module. All
  328.     # given switches are initialized to a true value.
  329.     SES::SelfSwitches::INITIALIZE.each { |index| self[index] = true }
  330.   end
  331.  
  332.   # Redefines inspection of the wrapper class so that all values are represented
  333.   # by true, false, or nil values to reflect the intended use of the class. To
  334.   # get the unaltered hash, use $game_self_switches.data.inspect.
  335.   def inspect
  336.     to_hash.inspect
  337.   end
  338.   alias :to_s :inspect
  339.  
  340.   # Returns a hash where all values of the base hash have been given explicit
  341.   # true, false, or nil values to reflect the intended use of the class. Use
  342.   # $game_self_switches.data to obtain the unaltered hash.
  343.   def to_hash
  344.     @data.each_with_object({}) do |(key, value), hash|
  345.       hash[key] = value ? true : false unless value.nil?
  346.     end
  347.   end
  348.  
  349.   # Redefinition of the Game_SelfSwitches class' original [] method to allow the
  350.   # ability to reference indices by name as well as their actual index. Also
  351.   # explicitly returns only true or false values rather than the actual contents
  352.   # of the base data array (to access actual contents, use the data method).
  353.   def [](map, event = nil, switch = nil)
  354.     @data[obtain(parse_index(map, event, switch))] ? true : false
  355.   end
  356.  
  357.   # Redefinition of the Game_SelfSwitches class' original []= method to allow
  358.   # the ability to reference indices by name as well as their actual index.
  359.   # Values passed to this method are automatically converted to true or false.
  360.   # Also calls the on_assignment method to carry out actions post-assignment.
  361.   def []=(map, event = nil, switch = nil, value)
  362.     index = parse_index(map, event, switch)
  363.     @data[obtain(index)] = value ? true : false
  364.     on_assignment(index, value)
  365.   end
  366.  
  367.   # Toggles the switch at the given index. Note: toggled values will always be
  368.   # "true" or "false," not necessarily the return value of a unary ! operation
  369.   # on the element contained by the index due to the class' []= method.
  370.   def toggle!(map, event = nil, switch = nil)
  371.     index = obtain(parse_index(map, event, switch))
  372.     self[index] = begin !@data[index] rescue !self[index] end
  373.   end
  374.  
  375.   # This method parses the information given to it into an index usable by this
  376.   # class' obtain method. This method is what enables multiple index formats for
  377.   # the wrapper. Note: this is a private method.
  378.   def parse_index(map_id, event_id, switch_id)
  379.     # If event_id is nil, we assume that map_id contains an array to parse.
  380.     # Otherwise, we build it.
  381.     index = (event_id ? [map_id, event_id] : map_id)
  382.     index.push(switch_id) if switch_id
  383.     index.unshift($game_map.map_id) if index.length == 2
  384.     index
  385.   end
  386.  
  387.   # Attempts to find the names for given map and event indices present in the
  388.   # index if the elements representing the map and event resemble Strings. This
  389.   # method tries to perform substitution for these elements and returns an array
  390.   # with the actual values used by the underlying data hash.
  391.   def obtain(index)
  392.     if index[0].respond_to?(:to_str)
  393.       $data_mapinfos.each { |id, m| index[0] = id if m.name == index[0].to_str }
  394.     end
  395.     if index[1].respond_to?(:to_str)
  396.       # Get the RPG::Map instance to use for accessing RPG::Event instances.
  397.       # Using $game_map does not provide events with a name method, which is
  398.       # required for this method to work properly.
  399.       if !$game_map || $game_map.map_id != index[0]
  400.         load_data(sprintf('Data/Map%03d.rvdata2', index[0]))
  401.       else
  402.         $game_map.map
  403.       end.events.each { |id, e| index[1] = id if e.name == index[1].to_s }
  404.     end
  405.     index
  406.   end
  407.  
  408.   private :parse_index
  409. end
  410. #═╦═════════════════════════════════════════════════════════════════════════════
  411. # ║ ▲ class Game_SelfSwitches
  412. #─╫─────────────────────────────────────────────────────────────────────────────
  413. # ║ ▼ class Game_Map
  414. #═╩═════════════════════════════════════════════════════════════════════════════
  415. class Game_Map
  416.   # Allows accessing the RPG::Map instance used by Game_Map directly.
  417.   attr_reader :map
  418. end
  419. #═╦═════════════════════════════════════════════════════════════════════════════
  420. # ║ ▲ class Game_Map
  421. #═╩═════════════════════════════════════════════════════════════════════════════
  422. # Removing the sort and sort_by methods for the default VX Ace switch, variable,
  423. # and self switch data structures -- these structures are inherently in order.
  424. [Game_Switches, Game_Variables, Game_SelfSwitches].each do |structure|
  425.   [:sort, :sort_by].each { |method| structure.send(:undef_method, method) }
  426. end
  427. #═╦══════════════════════════════════════════════════════════════════════════╦═#
  428. # ║                              ▼ END OF FILE ▼                             ║ #
  429. #═╩══════════════════════════════════════════════════════════════════════════╩═#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement