Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #==============================================================================
- # Iavra Auto States 1.00
- # -----------------------------------------------------------------------------
- # Description:
- # Introduces new notetags that allow actors/equips/states to automatically add
- # and remove states based on current hp/mp/tp.
- # -----------------------------------------------------------------------------
- # Prerequisites:
- # None
- # -----------------------------------------------------------------------------
- # How to Use:
- # Add the following tag to the notebox of an actor, enemy, equippable item or state:
- #
- # <state (id) (param) (min)% (max)%>
- #
- # (id) id of the state, that should automatically be added/removed.
- # (param) one of hp/mp/tp.
- # (min) minimum percentage of the parameter.
- # (max) maximum percentage of the parameter.
- #
- # Example:
- # <state 10 hp 0% 100%>
- #
- # The order of min and max doesn't actually matter, since they will get sorted.
- #
- # You can define multiple restrictions for a state by adding this tag:
- #
- # <state (id)>
- # <(param) (min)% (max)%>
- # <(param) (min)% (max)%>
- # ...
- # </state>
- #
- # Example:
- # <state 10>
- # <hp 100% 100%>
- # <mp 0% 0%>
- # </state>
- #
- # Note that all restrictions are additive, so if one of them returns true, the
- # state will be added. Having multiple restrictions on a state is currently not
- # supported.
- #
- # During updates, the script will check the restrictions for each actor, its
- # equipped items, learned skills and states, as well as for each enemy and its
- # states and test if any restriction returns true:
- # - If at least one restriction is true and the state is addable to the actor,
- # the state is added and its id is pushed to a list.
- # - If there is no valid restriction for a previously added state, it will be
- # removed.
- #
- # In addition, whenever a state is removed from the actor/enemy, its id is also
- # removed from the list. The list is there to prevent the script from removing
- # states that were added by other means than the script itself.
- # -----------------------------------------------------------------------------
- # Terms of Use:
- # Free to use for both commercial and non-commercial games. Please give credit.
- # -----------------------------------------------------------------------------
- # Credits:
- # Iavra
- # -----------------------------------------------------------------------------
- # Changelog:
- # - 0.01: First version.
- # - 0.02: Changed the script to only affect combat. Now updates at the start and
- # end of every turn as well as at the start and end of battle.
- # - 0.03: Fixed a possible error when either max mp or max tp are 0.
- # - 0.04: Added the option to configure what methods should update states.
- # - 0.05: Fixed a bug, where the script wouldn't correctly remove states if the
- # state id wasn't present in the restrictions anymore.
- # - 0.06: Added the option to remove all automatically added states at end of
- # battle.
- # - 0.07: If a state is added by other means than the script, its id will now be
- # removed from the list. If the restriction is still true, the id will
- # be re-added with the next update.
- # - 0.08: Added support for learned (passive) skills.
- # - 1.00: Release version.
- #==============================================================================
- ($imported ||= {})[:iavra_auto_states] = true
- #==============================================================================
- # ▼ IAVRA::ANIMATE
- #==============================================================================
- module IAVRA
- module STATES
- #==========================================================================
- # ■ ■ ■ ■ ■ CONFIGURATION ■ ■ ■ ■ ■
- #==========================================================================
- #==========================================================================
- # Percentage to return, when maximum mp/tp is 0.
- #==========================================================================
- ZERO_MAX = 1.0
- #==========================================================================
- # If set to true, this will remove all automatically added states at end of
- # battle. Note that states won't automatically be updated out of combat.
- #==========================================================================
- REMOVE_STATES_AFTER_BATTLE = true
- #==========================================================================
- # The methods of BattleManager, during which states should be updated.
- #==========================================================================
- UPDATE = [
- :battle_start,
- :battle_end,
- :turn_start,
- :turn_end
- ]
- #==========================================================================
- # ■ ■ ■ ■ ■ CONFIGURATION ■ ■ ■ ■ ■
- #==========================================================================
- #==========================================================================
- # Allowed parameters, which also act as method names to get their current
- # values, mapped to the method names of their maximum values.
- #==========================================================================
- PARAMS = {
- :hp => :mhp,
- :mp => :mmp,
- :tp => :max_tp
- }
- #==========================================================================
- # Regexes to parse our notetags. Multi-restriction notetags have to be
- # parsed twice, since groups can't be captured multiple times.
- #==========================================================================
- REGEX_SINGLE = /<[ ]*state[ ]+(\d+)[ ]+(\w+)[ ]+(\d+)%[ ]+(\d+)%[ ]*>/i
- REGEX_MULTI_OUTER = /<[ ]*state[ ]+(\d+)[ ]*>\s*((?:<[ ]*\w+[ ]+\d+%[ ]+\d+%[ ]*>\s*)+)<[ ]*\/state[ ]*>/i
- REGEX_MULTI_INNER = /<[ ]*(\w+)[ ]+(\d+)%[ ]+(\d+)%[ ]*>/i
- #==========================================================================
- # Checks the restrictions for every actor and enemy, as well as its equips,
- # learned skills (both only for actors) and states and adds/removes states
- # accordingly. Maintains a list for every battler to only remove states,
- # that have been added.
- #
- # Works in 2 phases:
- # - Checks all restrictions for the battler and adds states, if they are
- # also addable. Tracks the added state ids in a list.
- # - Checks all states inside the list and removes the state, if there is
- # no valid restriction for it.
- #==========================================================================
- def self.update
- battlers = []
- battlers += $game_party.members if $game_party
- battlers += $game_troop.members if $game_troop.in_battle
- battlers.each do |battler|
- restrictions = battler.iavra_states
- restrictions.keys.each do |state|
- if(test(battler, state, restrictions) && battler.state_addable?(state))
- battler.add_state(state, true)
- end
- end
- battler.iavra_states_list.dup.keys.each do |state|
- battler.remove_state(state) unless test(battler, state, restrictions)
- end
- end
- end
- #==========================================================================
- # Removes all automatically added states.
- #==========================================================================
- def self.cleanup
- ($game_party.members + $game_troop.members).each do |battler|
- battler.iavra_states_list.dup.keys.each{|state| battler.remove_state(state)}
- end
- end
- #==========================================================================
- # Checks if the given state should be added to the given battler.
- #==========================================================================
- def self.test(battler, state, restrictions)
- return false if (data_array = restrictions[state]).nil?
- data_array.map{|data|
- cur = battler.send(data[0]).to_f
- max = battler.send(PARAMS[data[0]]).to_f
- (max <= 0 ? ZERO_MAX : (cur / max)).between?(data[1], data[2])
- }.any?
- end
- end
- end
- #==============================================================================
- # ▼ BattleManager
- #==============================================================================
- module BattleManager
- class << self
- #==========================================================================
- # Call update to automatically add/remove states.
- #==========================================================================
- IAVRA::STATES::UPDATE.each do |method|
- eval %Q{
- alias :iavra_states_#{method} :#{method}
- def #{method}(*args)
- iavra_states_#{method}(*args)
- IAVRA::STATES.update
- end
- }
- end
- #==========================================================================
- # Optionally removes all added state at end of battle.
- #==========================================================================
- alias :iavra_states_battle_end2 :battle_end
- def battle_end(*args)
- iavra_states_battle_end2(*args)
- IAVRA::STATES.cleanup if IAVRA::STATES::REMOVE_STATES_AFTER_BATTLE
- end
- end
- end
- #==============================================================================
- # ▼ RPG::BaseItem
- #==============================================================================
- class RPG::BaseItem
- #============================================================================
- # Returns our notetags and parses them the first time this is called.
- #============================================================================
- def iavra_states
- iavra_states_parse_tags if @iavra_states.nil?
- @iavra_states
- end
- #============================================================================
- # Parses all notetags and stores them in a hash looking like this:
- # {
- # <state_id> = [
- # [<param>, <min>, <max>],
- # ...
- # ]
- # }
- #============================================================================
- def iavra_states_parse_tags
- @iavra_states = {}
- @note.scan(IAVRA::STATES::REGEX_SINGLE).each do |tag|
- next unless IAVRA::STATES::PARAMS.include?(param = tag[1].downcase.to_sym)
- (@iavra_states[tag[0].to_i] ||= []) << [param, *[tag[2].to_f / 100, tag[3].to_f / 100].sort]
- end
- @note.scan(IAVRA::STATES::REGEX_MULTI_OUTER).each do |outer|
- outer[1].scan(IAVRA::STATES::REGEX_MULTI_INNER).each do |tag|
- next unless IAVRA::STATES::PARAMS.include?(param = tag[0].downcase.to_sym)
- (@iavra_states[outer[0].to_i] ||= []) << [param, *[tag[1].to_f / 100, tag[2].to_f / 100].sort]
- end
- end
- end
- end
- #==============================================================================
- # ▼ Game_Battler
- #==============================================================================
- class Game_Battler < Game_BattlerBase
- alias :iavra_states_add_state :add_state
- alias :iavra_states_remove_state :remove_state
- alias :iavra_states_on_battle_end :on_battle_end
- #============================================================================
- # A list, which keeps track of states, that have been added by the script.
- #============================================================================
- def iavra_states_list
- @iavra_states_list ||= {}
- end
- #============================================================================
- # Adding a state automatically will push its id to the list. If the state is
- # added by other means, its id will be removed.
- #============================================================================
- def add_state(state_id, automatically = false)
- iavra_states_add_state(state_id)
- if(automatically)
- iavra_states_list[state_id] = true
- else
- iavra_states_list.delete(state_id)
- end
- end
- #============================================================================
- # Removing a state also removes its id from the list.
- #============================================================================
- def remove_state(state_id)
- iavra_states_remove_state(state_id)
- iavra_states_list.delete(state_id)
- end
- end
- #==============================================================================
- # ▼ Game_Actor
- #==============================================================================
- class Game_Actor < Game_Battler
- #============================================================================
- # Collects the restrictions from the actor, its equips, skills and states.
- #============================================================================
- def iavra_states
- ([actor] + equips + states + skills).compact.map{|item|
- item.iavra_states
- }.inject{|sum, item| sum.merge(item) {|k, h1, h2| h1 + h2}}
- end
- end
- #==============================================================================
- # ▼ Game_Enemy
- #==============================================================================
- class Game_Enemy < Game_Battler
- #============================================================================
- # Collects the restrictions from the enemy and its states.
- #============================================================================
- def iavra_states
- ([enemy] + states).map{|item|
- item.iavra_states
- }.inject{|sum, item| sum.merge(item) {|k, h1, h2| h1 + h2}}
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement