Advertisement
Iavra

[Ace] Auto States

Jun 4th, 2015
814
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 13.18 KB | None | 0 0
  1. #==============================================================================
  2. # Iavra Auto States 1.00
  3. # -----------------------------------------------------------------------------
  4. # Description:
  5. # Introduces new notetags that allow actors/equips/states to automatically add
  6. # and remove states based on current hp/mp/tp.
  7. # -----------------------------------------------------------------------------
  8. # Prerequisites:
  9. # None
  10. # -----------------------------------------------------------------------------
  11. # How to Use:
  12. # Add the following tag to the notebox of an actor, enemy, equippable item or state:
  13. #
  14. # <state (id) (param) (min)% (max)%>
  15. #
  16. # (id)    id of the state, that should automatically be added/removed.
  17. # (param) one of hp/mp/tp.
  18. # (min)   minimum percentage of the parameter.
  19. # (max)   maximum percentage of the parameter.
  20. #
  21. # Example:
  22. # <state 10 hp 0% 100%>
  23. #
  24. # The order of min and max doesn't actually matter, since they will get sorted.
  25. #
  26. # You can define multiple restrictions for a state by adding this tag:
  27. #
  28. # <state (id)>
  29. #   <(param) (min)% (max)%>
  30. #   <(param) (min)% (max)%>
  31. #   ...
  32. # </state>
  33. #
  34. # Example:
  35. # <state 10>
  36. #   <hp 100% 100%>
  37. #   <mp 0% 0%>
  38. # </state>
  39. #
  40. # Note that all restrictions are additive, so if one of them returns true, the
  41. # state will be added. Having multiple restrictions on a state is currently not
  42. # supported.
  43. #
  44. # During updates, the script will check the restrictions for each actor, its
  45. # equipped items, learned skills and states, as well as for each enemy and its
  46. # states and test if any restriction returns true:
  47. # - If at least one restriction is true and the state is addable to the actor,
  48. #   the state is added and its id is pushed to a list.
  49. # - If there is no valid restriction for a previously added state, it will be
  50. #   removed.
  51. #
  52. # In addition, whenever a state is removed from the actor/enemy, its id is also
  53. # removed from the list. The list is there to prevent the script from removing
  54. # states that were added by other means than the script itself.
  55. # -----------------------------------------------------------------------------
  56. # Terms of Use:
  57. # Free to use for both commercial and non-commercial games. Please give credit.
  58. # -----------------------------------------------------------------------------
  59. # Credits:
  60. # Iavra
  61. # -----------------------------------------------------------------------------
  62. # Changelog:
  63. # - 0.01: First version.
  64. # - 0.02: Changed the script to only affect combat. Now updates at the start and
  65. #         end of every turn as well as at the start and end of battle.
  66. # - 0.03: Fixed a possible error when either max mp or max tp are 0.
  67. # - 0.04: Added the option to configure what methods should update states.
  68. # - 0.05: Fixed a bug, where the script wouldn't correctly remove states if the
  69. #         state id wasn't present in the restrictions anymore.
  70. # - 0.06: Added the option to remove all automatically added states at end of
  71. #         battle.
  72. # - 0.07: If a state is added by other means than the script, its id will now be
  73. #         removed from the list. If the restriction is still true, the id will
  74. #         be re-added with the next update.
  75. # - 0.08: Added support for learned (passive) skills.
  76. # - 1.00: Release version.
  77. #==============================================================================
  78.  
  79. ($imported ||= {})[:iavra_auto_states] = true
  80.  
  81. #==============================================================================
  82. # ▼ IAVRA::ANIMATE
  83. #==============================================================================
  84.  
  85. module IAVRA
  86.     module STATES
  87.        
  88.         #==========================================================================
  89.         # ■ ■ ■ ■ ■ CONFIGURATION ■ ■ ■ ■ ■
  90.         #==========================================================================
  91.        
  92.         #==========================================================================
  93.         # Percentage to return, when maximum mp/tp is 0.
  94.         #==========================================================================
  95.        
  96.         ZERO_MAX = 1.0
  97.        
  98.         #==========================================================================
  99.         # If set to true, this will remove all automatically added states at end of
  100.         # battle. Note that states won't automatically be updated out of combat.
  101.         #==========================================================================
  102.        
  103.         REMOVE_STATES_AFTER_BATTLE = true
  104.        
  105.         #==========================================================================
  106.         # The methods of BattleManager, during which states should be updated.
  107.         #==========================================================================
  108.        
  109.         UPDATE = [
  110.             :battle_start,
  111.             :battle_end,
  112.             :turn_start,
  113.             :turn_end
  114.         ]
  115.        
  116.         #==========================================================================
  117.         # ■ ■ ■ ■ ■ CONFIGURATION ■ ■ ■ ■ ■
  118.         #==========================================================================
  119.        
  120.         #==========================================================================
  121.         # Allowed parameters, which also act as method names to get their current
  122.         # values, mapped to the method names of their maximum values.
  123.         #==========================================================================
  124.        
  125.         PARAMS = {
  126.             :hp => :mhp,
  127.             :mp => :mmp,
  128.             :tp => :max_tp
  129.         }
  130.        
  131.         #==========================================================================
  132.         # Regexes to parse our notetags. Multi-restriction notetags have to be
  133.         # parsed twice, since groups can't be captured multiple times.
  134.         #==========================================================================
  135.        
  136.         REGEX_SINGLE = /<[ ]*state[ ]+(\d+)[ ]+(\w+)[ ]+(\d+)%[ ]+(\d+)%[ ]*>/i
  137.         REGEX_MULTI_OUTER = /<[ ]*state[ ]+(\d+)[ ]*>\s*((?:<[ ]*\w+[ ]+\d+%[ ]+\d+%[ ]*>\s*)+)<[ ]*\/state[ ]*>/i
  138.         REGEX_MULTI_INNER = /<[ ]*(\w+)[ ]+(\d+)%[ ]+(\d+)%[ ]*>/i
  139.        
  140.         #==========================================================================
  141.         # Checks the restrictions for every actor and enemy, as well as its equips,
  142.         # learned skills (both only for actors) and states and adds/removes states
  143.         # accordingly. Maintains a list for every battler to only remove states,
  144.         # that have been added.
  145.         #
  146.         # Works in 2 phases:
  147.         # - Checks all restrictions for the battler and adds states, if they are
  148.         #   also addable. Tracks the added state ids in a list.
  149.         # - Checks all states inside the list and removes the state, if there is
  150.         #   no valid restriction for it.
  151.         #==========================================================================
  152.        
  153.         def self.update
  154.             battlers = []
  155.             battlers += $game_party.members if $game_party
  156.             battlers += $game_troop.members if $game_troop.in_battle
  157.             battlers.each do |battler|
  158.                 restrictions = battler.iavra_states
  159.                 restrictions.keys.each do |state|
  160.                     if(test(battler, state, restrictions) && battler.state_addable?(state))
  161.                         battler.add_state(state, true)
  162.                     end
  163.                 end
  164.                 battler.iavra_states_list.dup.keys.each do |state|
  165.                     battler.remove_state(state) unless test(battler, state, restrictions)
  166.                 end
  167.             end
  168.         end
  169.        
  170.         #==========================================================================
  171.         # Removes all automatically added states.
  172.         #==========================================================================
  173.        
  174.         def self.cleanup
  175.             ($game_party.members + $game_troop.members).each do |battler|
  176.                 battler.iavra_states_list.dup.keys.each{|state| battler.remove_state(state)}
  177.             end
  178.         end
  179.        
  180.         #==========================================================================
  181.         # Checks if the given state should be added to the given battler.
  182.         #==========================================================================
  183.        
  184.         def self.test(battler, state, restrictions)
  185.             return false if (data_array = restrictions[state]).nil?
  186.             data_array.map{|data|
  187.                 cur = battler.send(data[0]).to_f
  188.                 max = battler.send(PARAMS[data[0]]).to_f
  189.                 (max <= 0 ? ZERO_MAX : (cur / max)).between?(data[1], data[2])
  190.             }.any?
  191.         end
  192.        
  193.     end
  194. end
  195.  
  196. #==============================================================================
  197. # ▼ BattleManager
  198. #==============================================================================
  199.  
  200. module BattleManager
  201.  
  202.     class << self
  203.        
  204.         #==========================================================================
  205.         # Call update to automatically add/remove states.
  206.         #==========================================================================
  207.        
  208.         IAVRA::STATES::UPDATE.each do |method|
  209.             eval %Q{
  210.                 alias :iavra_states_#{method} :#{method}
  211.                 def #{method}(*args)
  212.                     iavra_states_#{method}(*args)
  213.                     IAVRA::STATES.update
  214.                 end
  215.             }
  216.         end
  217.        
  218.         #==========================================================================
  219.         # Optionally removes all added state at end of battle.
  220.         #==========================================================================
  221.        
  222.         alias :iavra_states_battle_end2 :battle_end
  223.         def battle_end(*args)
  224.             iavra_states_battle_end2(*args)
  225.             IAVRA::STATES.cleanup if IAVRA::STATES::REMOVE_STATES_AFTER_BATTLE
  226.         end
  227.        
  228.     end
  229.    
  230. end
  231.  
  232. #==============================================================================
  233. # ▼ RPG::BaseItem
  234. #==============================================================================
  235.  
  236. class RPG::BaseItem
  237.    
  238.     #============================================================================
  239.     # Returns our notetags and parses them the first time this is called.
  240.     #============================================================================
  241.    
  242.     def iavra_states
  243.         iavra_states_parse_tags if @iavra_states.nil?
  244.         @iavra_states
  245.     end
  246.    
  247.     #============================================================================
  248.     # Parses all notetags and stores them in a hash looking like this:
  249.     # {
  250.     #   <state_id> = [
  251.     #     [<param>, <min>, <max>],
  252.     #     ...
  253.     #   ]
  254.     # }
  255.     #============================================================================
  256.    
  257.     def iavra_states_parse_tags
  258.         @iavra_states = {}
  259.         @note.scan(IAVRA::STATES::REGEX_SINGLE).each do |tag|
  260.             next unless IAVRA::STATES::PARAMS.include?(param = tag[1].downcase.to_sym)
  261.             (@iavra_states[tag[0].to_i] ||= []) << [param, *[tag[2].to_f / 100, tag[3].to_f / 100].sort]
  262.         end
  263.         @note.scan(IAVRA::STATES::REGEX_MULTI_OUTER).each do |outer|
  264.             outer[1].scan(IAVRA::STATES::REGEX_MULTI_INNER).each do |tag|
  265.                 next unless IAVRA::STATES::PARAMS.include?(param = tag[0].downcase.to_sym)
  266.                 (@iavra_states[outer[0].to_i] ||= []) << [param, *[tag[1].to_f / 100, tag[2].to_f / 100].sort]
  267.             end
  268.         end
  269.     end
  270.    
  271. end
  272.  
  273. #==============================================================================
  274. # ▼ Game_Battler
  275. #==============================================================================
  276.  
  277. class Game_Battler < Game_BattlerBase
  278.    
  279.     alias :iavra_states_add_state :add_state
  280.     alias :iavra_states_remove_state :remove_state
  281.     alias :iavra_states_on_battle_end :on_battle_end
  282.    
  283.     #============================================================================
  284.     # A list, which keeps track of states, that have been added by the script.
  285.     #============================================================================
  286.    
  287.     def iavra_states_list
  288.         @iavra_states_list ||= {}
  289.     end
  290.    
  291.     #============================================================================
  292.     # Adding a state automatically will push its id to the list. If the state is
  293.     # added by other means, its id will be removed.
  294.     #============================================================================
  295.    
  296.     def add_state(state_id, automatically = false)
  297.         iavra_states_add_state(state_id)
  298.         if(automatically)
  299.             iavra_states_list[state_id] = true
  300.         else
  301.             iavra_states_list.delete(state_id)
  302.         end
  303.     end
  304.    
  305.     #============================================================================
  306.     # Removing a state also removes its id from the list.
  307.     #============================================================================
  308.    
  309.     def remove_state(state_id)
  310.         iavra_states_remove_state(state_id)
  311.         iavra_states_list.delete(state_id)
  312.     end
  313.    
  314. end
  315.  
  316. #==============================================================================
  317. # ▼ Game_Actor
  318. #==============================================================================
  319.  
  320. class Game_Actor < Game_Battler
  321.    
  322.     #============================================================================
  323.     # Collects the restrictions from the actor, its equips, skills and states.
  324.     #============================================================================
  325.    
  326.     def iavra_states
  327.         ([actor] + equips + states + skills).compact.map{|item|
  328.             item.iavra_states
  329.         }.inject{|sum, item| sum.merge(item) {|k, h1, h2| h1 + h2}}
  330.     end
  331.    
  332. end
  333.  
  334. #==============================================================================
  335. # ▼ Game_Enemy
  336. #==============================================================================
  337.  
  338. class Game_Enemy < Game_Battler
  339.    
  340.     #============================================================================
  341.     # Collects the restrictions from the enemy and its states.
  342.     #============================================================================
  343.    
  344.     def iavra_states
  345.         ([enemy] + states).map{|item|
  346.             item.iavra_states
  347.         }.inject{|sum, item| sum.merge(item) {|k, h1, h2| h1 + h2}}
  348.     end
  349.    
  350. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement