Advertisement
TheSixth

Custom Switches by Sixth

Sep 2nd, 2015
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 19.89 KB | None | 0 0
  1. #===============================================================================
  2. # * [ACE] Custom Switch Storage
  3. #===============================================================================
  4. # * Made by: Sixth (www.rpgmakervxace.net, www.forums.rpgmakerweb.com)
  5. # * Version: 1.2
  6. # * Updated: 29/04/2015
  7. # * Requires: -------
  8. #-------------------------------------------------------------------------------
  9. # * < Change Log >
  10. #-------------------------------------------------------------------------------
  11. # * Version 1.0 (15/01/2015)
  12. #   - Initial release.
  13. # * Version 1.1 (17/01/2015)
  14. #   - Fixed switch initialization.
  15. #     Switches didn't reset until the game was shut down and restarted,
  16. #     regardless if the player quited the current game and started a new one.
  17. #     This is now fixed.
  18. # * Version 1.2 (29/04/2015)
  19. #   - Fixed the event comments checking. Whoops!
  20. #-------------------------------------------------------------------------------
  21. # * < Description >
  22. #-------------------------------------------------------------------------------
  23. # * By default, you have 5000 switches available to set up and use in your game,
  24. #   and you can control them only one by one with a "Control Switches" event
  25. #   command for each of them. This gets more complicated when you want to make a
  26. #   switch toggle event, where the switch will either be turned ON or OFF
  27. #   depending on the current state of the switch. Also, you can only assign 2 of
  28. #   these switches for a page condition, and you can't make a page condition
  29. #   which says "if switch 32 is turned OFF", for example.
  30. #   While 5000 switches sounds more than enough, the available operations for
  31. #   them are pretty limited without making long and repetetive, sometimes even
  32. #   complicated condition checks.
  33. #
  34. # * With this script, you can:
  35. #   - Create infinite amount of custom switches for your game.
  36. #   - Name them however you want, so you will never mix them.
  37. #   - Arrange them in a clean and organized way in the script.
  38. #   - Assign them into a group.
  39. #   - Turn them ON/OFF or even toggle them with simple script calls.
  40. #   - Turn groups ON/OFF or even toggle them with simple script calls.
  41. #   - Assign custom switch requirements for your events with unlimited switches.
  42. #   - Shorten your custom switch requirements by simply assigning a group
  43. #     requirement, or even more group requirements for an event.
  44. #   - Save countless lines on your event pages.
  45. #-------------------------------------------------------------------------------
  46. # * < Event Comments >
  47. #-------------------------------------------------------------------------------
  48. # * To make custom switch requirements for your events, you need to use comment
  49. #   commands on their pages. When I refer to the name of your custom switches
  50. #   and groups, here is what I mean:
  51. #
  52. #   - In the script, a switch looks like this:
  53. #
  54. #       :custom1 => false, or :whatever32 => true, etc,
  55. #
  56. #     Notice the ":" sign before the actual name of the switch!
  57. #     When you make a custom switch requirement for your event page, you will
  58. #     need to use the name of the switches or groups in it without that ":" sign!
  59. #     So, the correct names from the above examples are:
  60. #
  61. #       custom1, whatever32
  62. #
  63. #     Now you know what I mean when I write
  64. #     "the name of your custom switch/group" below in the detailed explanation.
  65. #
  66. #   - There will be another commonly used word here. That one goes by "value".
  67. #     The value can be either true or false.
  68. #     If you use true, the page won't activate unless the assigned switch/group
  69. #     is turned ON.
  70. #     If you use false, the page won't activate unless the assigned switch/group
  71. #     is turned OFF.
  72. #
  73. #     Keep this in mind when setting up custom switch requirements!
  74. #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  75. # * To make a single switch requirement for an event, make a comment command
  76. #   which looks like this:
  77. #
  78. #     <single_sw_req: name, value>
  79. #
  80. #   name = The name of your custom switch.
  81. #   value = The state required for the switch. Can be either true or false.
  82. #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  83. # * To make a single group switch requirement for an event, make a comment
  84. #   command which looks like this:
  85. #          
  86. #     <group_sw_req: name, value>
  87. #
  88. #   name = The name of your group.
  89. #   value = The state required for the group. Can be either true or false.
  90. #      
  91. #   NOTE:
  92. #   If you use a single group requirement for an event page, that means that all
  93. #   switches assigned to that group must have the value defined in the comment.
  94. #   So, if you use true for the value, all of the switches from the group must
  95. #   be turned ON before the event activates!
  96. #   Same thing applies if you use false, but than, obviously, all of the switches
  97. #   from the group must be turned OFF before the event activates.
  98. #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  99. # * To make a fully customizable switch/group requirement for an event, make a
  100. #   comment command which looks like this:
  101. #
  102. #     <custom_sw_req: name value, name value, name value ... >
  103. #
  104. #   name = The name of a switch or group. Both can be used.
  105. #   value = The state required for the switch/group. Can be either true or false.
  106. #
  107. #   All of the rules from the other switch requirement comments applies here too!
  108. #  
  109. #   The beauty of this last type of custom switch requirement is that you can
  110. #   assign mixed switch requirements for your event page.
  111. #   You can use single switch names or group names too.
  112. #   You can also use true for one requirement and false for another one, so you
  113. #   can create complicated requirements for your pages without having to make
  114. #   dozens of (often nested) conditional branches on your event page.
  115. #   You can also use unlimited number of switch requirements!
  116. #   Don't forget to use groups to shorten your comments for the requirements!
  117. #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  118. # * Some examples:
  119. #   (NOTE: The examples are using the sample switches and groups set up in the
  120. #          script initially to present some exact practical examples.)
  121. #
  122. #     <single_sw_req: test1, false>
  123. #   The event will not start until the switch named :test1 is turned OFF.
  124. #
  125. #     <group_sw_req: grupy2, true>
  126. #   The event will not start until all of the switches are turned ON from the
  127. #   group named :grupy2.
  128. #
  129. #     <custom_sw_req: test3 true, test4 false, grupy5 true>
  130. #   The event will not start until:
  131. #   - The switch named :test3 is turned ON.
  132. #   - The switch named :test4 is trurned OFF.
  133. #   - All of the switches from :grupy5 are turned ON.
  134. #-------------------------------------------------------------------------------
  135. # * < Script Calls >
  136. #-------------------------------------------------------------------------------
  137. # * The rules for the script calls are the same as for the custom page
  138. #   requirements mentioned above!
  139. #
  140. #   There is a little difference thought:
  141. #   You will need to use the entire symbol for the name of the switches and
  142. #   groups in the script calls, meaning you will need that ":" sign at the start!
  143. #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  144. # * To turn ON/OFF a custom switch, you can use the following script call:
  145. #
  146. #     set_custom_sw(:name,value)
  147. #
  148. #   :name = The name of your custom switch.
  149. #   value = Can be either true or false.
  150. #           If true, the switch will be turned ON.
  151. #           If false, the switch will be turned OFF.
  152. #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  153. # * To turn ON/OFF a custom group, you can use the following script call:
  154. #
  155. #     set_custom_group_sw(:name,value)
  156. #
  157. #   :name = The name of your group.
  158. #   value = Can be either true or false.
  159. #           If true, all of the switches from the group will be turned ON.
  160. #           If false, all of the switches from the group will be turned OFF.
  161. #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  162. # * To toggle ON/OFF a custom switch, you can use the following script call:
  163. #
  164. #     toggle_custom_sw(:name)
  165. #
  166. #   :name = The name of your custom switch.
  167. #
  168. #   Depending on the current state of the switch, this script call will either
  169. #   turn ON or turn OFF the switch. If the switch is turned ON, it will be turned
  170. #   OFF, and if the switch is turned OFF, it will be turned ON.
  171. #
  172. #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  173. # * To toggle ON/OFF a custom group, you can use the following script call:
  174. #
  175. #     toggle_custom_group_sw(:name)
  176. #
  177. #   :name = The name of your group.
  178. #
  179. #   Depending on the current states of the switches in the group, this script
  180. #   call will either turn them ON or turn them OFF.
  181. #   If a switch from the group is turned ON, after the script call it will be
  182. #   turned OFF, and if a switch is turned OFF, it will be turned ON.
  183. #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  184. # * To check the state of a custom switch, you can use the following script call
  185. #   in a conditional branch (using the script method on the last page):
  186. #
  187. #     check_custom_sw(:name,value)
  188. #
  189. #   :name = The name of your custom switch.
  190. #   value = The state of the switch you want to check for.
  191. #           If you use true, and if the checked switch is turned ON, this
  192. #           conditional check will return true.
  193. #           If you use false, and if the checked switch is turned OFF, this
  194. #           conditional check will return true.
  195. #           In all other cases, the check returns false.
  196. #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  197. # * To check the state of a custom group, you can use the following script call
  198. #   in a conditional branch (using the script method on the last page):
  199. #
  200. #     check_custom_group_sw(:name,value)
  201. #
  202. #   :name = The name of your custom group.
  203. #   value = The state of the group you want to check for.
  204. #           If you use true, and if all of the switches from the checked group
  205. #           are turned ON, the conditional check will return true.
  206. #           If you use false, and if all of the switches from the checked group
  207. #           are turned OFF, the conditional check will return true.
  208. #           In all other cases, the check returns false.
  209. #- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  210. # * Some examples:
  211. #   (NOTE: The examples are using the sample switches and groups set up in the
  212. #          script initially to present some exact practical examples.)
  213. #
  214. #     set_custom_sw(:test3,true)
  215. #   Turns the switch named :test3 ON.
  216. #    
  217. #     set_custom_group_sw(:grupy1,false)
  218. #   Turns all of the switches from the group named :grupy1 OFF.
  219. #
  220. #     toggle_custom_sw(:test4)
  221. #   Toggles the switch named :test4.
  222. #   If it was turned ON, it will be turned OFF.
  223. #   If it was turned OFF, it will be turned ON.
  224. #
  225. #     toggle_custom_group_sw(:grupy3)
  226. #   Toggles all of the switches from the group named :grupy3.
  227. #   All of the switches turned ON from the group will be turned OFF.
  228. #   All of the switches turned OFF from the group will be turned ON.
  229. #
  230. #     check_custom_sw(:test2,true)
  231. #   Checks if the switch named :test2 is turned ON or not.
  232. #   If it is, it returns true, if it is not, it returns false.
  233. #   Use it in a conditional branch with the script method!
  234. #
  235. #     check_custom_group_sw(:grupy4,false)
  236. #   Checks if the switches from the group named :grupy4 are turned OFF or not.
  237. #   If all of them are, it returns true, if not all of them, it returns false.
  238. #   Use it in a conditional branch with the script method!
  239. #-------------------------------------------------------------------------------
  240. # * < Installation >
  241. #-------------------------------------------------------------------------------
  242. # * Place this script below Materials but above Main!
  243. #-------------------------------------------------------------------------------
  244. # * < Compatibility Info >
  245. #-------------------------------------------------------------------------------
  246. # * No known incompatibilities.
  247. #-------------------------------------------------------------------------------
  248. # * < Known Issues >
  249. #-------------------------------------------------------------------------------
  250. # * No known issues.
  251. #-------------------------------------------------------------------------------
  252. # * < Terms of Use >
  253. #-------------------------------------------------------------------------------
  254. # * Free to use for whatever purposes you want.
  255. # * Credit me (Sixth) in your game, pretty please! :P
  256. # * Posting modified versions of this script is allowed as long as you notice me
  257. #   about it with a link to it!
  258. #===============================================================================
  259. $imported = {} if $imported.nil?
  260. $imported["SixthCustomSwitches"] = true
  261. #===============================================================================
  262. # Settings:
  263. #===============================================================================
  264. module Sixth_Custom_Switch
  265.   #-----------------------------------------------------------------------------
  266.   # Custom Switch Settings:
  267.   #-----------------------------------------------------------------------------
  268.   # This is the place where you can set your custom switches up.
  269.   # You can use any name for them, but there is one rule:
  270.   # You must use a ":" sign at the start of every switch name!
  271.   # You can make unlimited amounts of switches.
  272.   # Remember to separate them with commas at the end of each line or after each
  273.   # switch setup!
  274.   #
  275.   # Switch setup format:
  276.   #   :name => value,
  277.   # The :name is the name of the switch.
  278.   # You can not make the same name for a switch and for a group!
  279.   # The value is the state of the switch.
  280.   # If it is set to true, the switch will be turned ON at game startup.
  281.   # If it is set to false, the switch will be turned OFF at game startup.
  282.   #-----------------------------------------------------------------------------
  283.   Single_Switches = { # <-- No touchy-touchy!
  284.     :bomb_puzzle => false,
  285.     :arrow_puzzle => false,
  286.    
  287.     :test1 => false,
  288.     :test2 => false,
  289.     :test3 => false,
  290.     :test4 => false,
  291.   } # <-- No touchy-touchy!
  292.  
  293.   #-----------------------------------------------------------------------------
  294.   # Custom Group Settings:
  295.   #-----------------------------------------------------------------------------
  296.   # This is the place where you can set your groups up.
  297.   # Groups are used to make batch operations and page requirements with your
  298.   # custom switches.
  299.   # Any switch assigned to the group will be affected by the group script calls.
  300.   # All of them will be checked for the page requirements when using groups.
  301.   # Remember to separate them with commas at the end of each line or after each
  302.   # group setup!
  303.   #
  304.   # Group setup format:
  305.   #   :name => [:switch1, :switch2, switch3, ..., switchn],
  306.   # The :name is the name of the group, same format as for the switches.
  307.   # You can use any name for your groups as long as they don't have the same name
  308.   # as your switches. Keep this in mind!
  309.   # The :switchx is the name of the switch assigned to the group.
  310.   # You can have unlimited amount of switches assigned to your groups.
  311.   #-----------------------------------------------------------------------------
  312.   Group_Switches = { # <-- No touchy-touchy!
  313.     :grupy1 => [:test1,:test2],
  314.     :grupy2 => [:test2,:test3],
  315.     :grupy3 => [:test1,:test2,:test3],
  316.     :grupy4 => [:test1,:test2,:test3,:test4],
  317.     :grupy5 => [:test3,:test4],
  318.   } # <-- No touchy-touchy!
  319.  
  320. end # <-- No touchy-touchy!
  321. #===============================================================================
  322. # End of Settings! Editing anything below may lead to... you know it, right?
  323. #===============================================================================
  324.  
  325. # Managing data (initialize/save/load).
  326. module DataManager
  327.  
  328.   class << self
  329.     alias sixth_cw_create_objects        create_game_objects
  330.     alias sixth_cw_save_contents         make_save_contents
  331.     alias sixth_cw_extract_contents      extract_save_contents
  332.   end
  333.  
  334.   def self.create_game_objects
  335.     sixth_cw_create_objects
  336.     $custom_sw = Game_Custom_Switches.new
  337.   end
  338.  
  339.   def self.make_save_contents
  340.     contents = sixth_cw_save_contents
  341.     contents[:custom_switches] = $custom_sw
  342.     contents
  343.   end
  344.  
  345.   def self.extract_save_contents(contents)
  346.     sixth_cw_extract_contents(contents)
  347.     $custom_sw = contents[:custom_switches]
  348.   end
  349.  
  350. end
  351.  
  352. # Initializing all custom switches.
  353. class Game_Custom_Switches
  354.  
  355.   attr_accessor :id
  356.  
  357.   def initialize
  358.     @id = {}
  359.     init_custom_switches
  360.   end
  361.    
  362.   def init_custom_switches
  363.     Sixth_Custom_Switch::Single_Switches.each do |switch,value|
  364.       @id[switch] = value
  365.     end
  366.   end
  367.    
  368. end
  369.  
  370. class Game_Event < Game_Character
  371.  
  372.   # Adding custom switch page requirements.
  373.   alias sixth_custom_switches1223 conditions_met?
  374.   def conditions_met?(page)
  375.     return false if get_them_custom_switch_req(page) == false
  376.     sixth_custom_switches1223(page)
  377.   end
  378.  
  379.   # Checking for custom switch page requirements.
  380.   def get_them_custom_switch_req(page)
  381.     return true if page.list.nil? || page.list.size <= 0
  382.     page.list.each do |cmd|
  383.       next unless cmd.code == 108 || cmd.code == 408
  384.       if cmd.parameters[0] =~ /<single_sw_req: (.*), (.*)>/i
  385.         data = $2 == "true" ? true : false
  386.         return false if check_for_single_sw_req($1.to_sym,data) == false
  387.       elsif cmd.parameters[0] =~ /<group_sw_req: (.*), (.*)>/i
  388.         data = $2 == "true" ? true : false
  389.         return false if check_for_group_sw_req($1.to_sym,data) == false
  390.       elsif cmd.parameters[0] =~ /<custom_sw_req: (.*[ ].*(?:\s*,\s*.*[ ].*)*)>/i
  391.         $1.split(", ").each do |info|
  392.           info =~ /(.*) (.*)/i
  393.           data = $2 == "true" ? true : false
  394.           if $custom_sw.id.include?($1.to_sym)
  395.             return false if check_for_single_sw_req($1.to_sym,data) == false
  396.           else
  397.             return false if check_for_group_sw_req($1.to_sym,data) == false
  398.           end
  399.         end
  400.       end
  401.     end
  402.     return true  
  403.   end
  404.  
  405.   # Checking for single switch page requirements.
  406.   def check_for_single_sw_req(sym,data)
  407.     return false if $custom_sw.id[sym] != data
  408.     return true
  409.   end
  410.  
  411.   # Checking for group page requirements.
  412.   def check_for_group_sw_req(sym,data)
  413.     Sixth_Custom_Switch::Group_Switches[sym].each do |switch|
  414.       return false if $custom_sw.id[switch] != data
  415.     end
  416.     return true
  417.   end
  418.    
  419. end
  420.  
  421. class Game_Interpreter
  422.    
  423.   # Setting a single switch.
  424.   def set_custom_sw(sym,data)
  425.     $custom_sw.id[sym] = data
  426.     $game_map.need_refresh = true
  427.   end
  428.  
  429.   # Checking for a single swicth.
  430.   def check_custom_sw(sym,data)
  431.     return true if $custom_sw.id[sym] == data
  432.     return false
  433.   end
  434.  
  435.   # Setting a group.
  436.   def set_custom_group_sw(sym,data)
  437.     Sixth_Custom_Switch::Group_Switches[sym].each do |switch|
  438.       $custom_sw.id[switch] = data
  439.     end
  440.     $game_map.need_refresh = true
  441.   end
  442.  
  443.   # Checking for a group.
  444.   def check_custom_group_sw(sym,data)
  445.     Sixth_Custom_Switch::Group_Switches[sym].each do |switch|
  446.       return false if $custom_sw.id[switch] != data
  447.     end
  448.     return true
  449.   end
  450.  
  451.   # Toggle single switch.
  452.   def toggle_custom_sw(sym)
  453.     $custom_sw.id[sym] = !$custom_sw.id[sym]
  454.     $game_map.need_refresh = true
  455.   end
  456.  
  457.   # Toggle a group.
  458.   def toggle_custom_group_sw(sym)
  459.     Sixth_Custom_Switch::Group_Switches[sym].each do |switch|
  460.       $custom_sw.id[switch] = !$custom_sw.id[switch]
  461.     end
  462.     $game_map.need_refresh = true
  463.   end
  464.  
  465. end
  466. #==============================================================================
  467. # !!END OF SCRIPT - OHH, NOES!!
  468. #==============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement