States : Critical Condition

States : Critical Condition
Version: 3.5
By: Kain Nobel

Introduction

With this system, you can control states that should be added or removed automatically when an actor or enemy reaches 1/4 HP. A basic example would be; when [Battler]'s HP is critical, give [Battler] this [State].

To put it in more lamens terms, lets say Basil has reached HP critical (his health is yellow instead of white), you can set the script to automatically give him something like "Str+" state, or something else like "Fatiqued" state. It us up to your imagination what kind of states you want to give actors or enemies when they're in critical condition, and you can specify multiple status ailments to add or remove if you'd like.

Another thing to note, states added through critical condition are handled special from states added through otherwise normal means. In other words, if [Battler] gets [State] through HP critical and not normal means, the state won't be removable until his HP is back to normal again since it was added under this circumstance. The only exception to this is if state is supposed to be removed when battle ends (and you've set RemoveStatesBattle true).

Lastly, yes this does comply with stats that are supposed to be removed after X turns and all that jazz, but the states will still remain until after HP is back to normal again. Basically, only when HP is critical will these states be un-removable, otherwise if your HP isn't critical they're now removable by thier natural means.

Features

  • You can set states to add to battlers when their HP is critical
  • You can also set states to remove when HP is critical
  • "Critical condition" states can't be removed while HP is still critical

Script

States : Critical Condition Show
  1. #===============================================================================
  2. # ** States : Critical Condition
  3. #===============================================================================
  4.  
  5. #-------------------------------------------------------------------------------
  6. # * SDK Log
  7. #-------------------------------------------------------------------------------
  8. if Object.const_defined?(:SDK)
  9.   SDK.log('States.CriticalCondition', 'Kain Nobel ©', 3.5, '2009.09.09')
  10. end
  11.  
  12. #===============================================================================
  13. # ** CriticalConditionStates
  14. #===============================================================================
  15.  
  16. module CriticalConditionStates
  17.   #-----------------------------------------------------------------------------
  18.   # * IDs of states to add to actor when their HP is critical
  19.   #     {actor_id => state_id, ...}                   # Add single state
  20.   #     {actor_id => [state_id, ...], ...}            # Add list of states
  21.   #     {actor_id => -state_id, ...}                  # Remove single state
  22.   #     {actor_id => [-state_id, ...], ...}           # Remove list of states
  23.   #-----------------------------------------------------------------------------
  24.   Actor = {
  25.   1 => 1,
  26.   2 => [1, -2]
  27.   }
  28.   Actor.default = []
  29.   #-----------------------------------------------------------------------------
  30.   # * IDs of states to add to enemy when their HP is critical
  31.   #     {actor_id => state_id, ...}                   # Add single state
  32.   #     {actor_id => [state_id, ...], ...}            # Add list of states
  33.   #     {actor_id => -state_id, ...}                  # Remove single state
  34.   #     {actor_id => [-state_id, ...], ...}           # Remove list of states
  35.   #-----------------------------------------------------------------------------
  36.   Enemy = {
  37.   }
  38.   Enemy.default = []
  39.   #-----------------------------------------------------------------------------
  40.   # * Allow removal of states that are supposed to be removed when battle ends?
  41.   #-----------------------------------------------------------------------------
  42.   RemoveStatesBattle = true
  43. end
  44.  
  45. #===============================================================================
  46. # ** Game_Battler
  47. #===============================================================================
  48.  
  49. class Game_Battler
  50.   #-----------------------------------------------------------------------------
  51.   # * Public Instance Variables
  52.   #-----------------------------------------------------------------------------
  53.   attr_accessor :critical_states_plus   # When HP critical these states added
  54.   attr_accessor :critical_states_minus  # When HP critical these states removed
  55.   attr_reader   :critical_states_array  # States added through HP critical
  56.   #-----------------------------------------------------------------------------
  57.   # * Alias Listings
  58.   #-----------------------------------------------------------------------------
  59.   alias_method :criticalstates_gmbattler_initialize,  :initialize
  60.   alias_method :criticalstates_gmbattler_hpset,       :hp=
  61.   alias_method :criticalstates_gmbattler_addstate,    :add_state
  62.   alias_method :criticalstates_gmbattler_remstate,    :remove_state
  63.   alias_method :criticalstates_gmbattler_remstbtl,    :remove_states_battle
  64.   #-----------------------------------------------------------------------------
  65.   # * Object Initialization
  66.   #-----------------------------------------------------------------------------
  67.   def initialize(*args)
  68.     # The usual
  69.     criticalstates_gmbattler_initialize(*args)
  70.     # Get states to add when HP critical
  71.     @critical_states_plus   = self.get_critical_states_plus
  72.     # Get states to remove when HP critical
  73.     @critical_states_minus  = self.get_critical_states_minus
  74.     # Get reserve array for critical states
  75.     @critical_states_array  = Array.new
  76.     # Default critical state modding flag to false
  77.     @modding_critical_state = false
  78.   end
  79.   #-----------------------------------------------------------------------------
  80.   # * HP = (HP)
  81.   #-----------------------------------------------------------------------------
  82.   def hp=(hp)
  83.     # Get HP before changes
  84.     last_hp = self.hp
  85.     # Set HP as usual
  86.     criticalstates_gmbattler_hpset(hp)    
  87.     # Set flag to notify that states activity is HP critical related
  88.     @modding_critical_state = true
  89.     # If HP is less than 1/4 and wasn't already less than 1/4
  90.     if (self.hp <= self.maxhp / 4) && (last_hp > self.maxhp / 4)
  91.       # Remove all critical states that are to be removed
  92.       self.critical_states_minus.each {|state_id| self.remove_state(state_id)}
  93.       # Add all critical states that are to be added
  94.       self.critical_states_plus.each {|state_id| self.add_state(state_id)}
  95.     # Else if HP more than critical HP
  96.     else
  97.       # For each state to be added due to being critical
  98.       @critical_states_plus.each do |state_id|
  99.         # Delete state from critical states array
  100.         @critical_states_array.delete(state_id)
  101.         # Skip state if waiting for automatic removal
  102.         next if @states_turn[state_id] > 0
  103.         # Remove state
  104.         self.remove_state(state_id)
  105.       end
  106.     end
  107.     # Default critical state activity flag back to false
  108.     @modding_critical_state = false
  109.   end
  110.   #-----------------------------------------------------------------------------
  111.   # * Add State
  112.   #-----------------------------------------------------------------------------
  113.   def add_state(state_id)
  114.     # If state added because HP critical
  115.     if @modding_critical_state
  116.       # Unless critical states array already includes this state
  117.       unless @critical_states_array.include?(state_id)
  118.         # Add this state to the array
  119.         @critical_states_array << state_id
  120.       end
  121.     # Else if adding state normally
  122.     else
  123.       # Delete this state from critical state array
  124.       @critical_states_array.delete(state_id)
  125.     end
  126.     # The usual
  127.     criticalstates_gmbattler_addstate(state_id)
  128.   end
  129.   #-----------------------------------------------------------------------------
  130.   # * Remove State
  131.   #-----------------------------------------------------------------------------
  132.   def remove_state(state_id)
  133.     # If state was removed because HP critical
  134.     if @modding_critical_state
  135.       # Delete this state from critical states array
  136.       @critical_states_array.delete(state_id)
  137.     end
  138.     # End method and disallow removal of state if added through HP critical
  139.     return if @critical_states_array.include?(state_id)
  140.     # The usual
  141.     criticalstates_gmbattler_remstate(state_id)
  142.   end
  143.   #-----------------------------------------------------------------------------
  144.   # * Get Critical States Plus
  145.   #-----------------------------------------------------------------------------
  146.   def get_critical_states_plus(const)
  147.     # Create empty array
  148.     arr = Array.new
  149.     # If setting for battler is a positive integer
  150.     if const[id].is_a?(Integer) && const[id] > 0
  151.       # Add state ID to array
  152.       arr << const[id]
  153.     # Else if setting is an array of state IDs
  154.     elsif const[id].is_a?(Array)
  155.       # Add each positive state ID to plus array
  156.       const[id].each {|state_id| arr << state_id if state_id > 0}
  157.     end
  158.     # Return all state IDs to be added
  159.     arr
  160.   end
  161.   #-----------------------------------------------------------------------------
  162.   # * Get Critical States Minus
  163.   #-----------------------------------------------------------------------------
  164.   def get_critical_states_minus(const)
  165.     # Create empty array
  166.     arr = Array.new
  167.     # If setting for battler is a negative integer
  168.     if const[id].is_a?(Integer) && const[id] < 0
  169.       # Convert state ID to positive number, add to array
  170.       arr << -const[id]
  171.     # Else if setting is an array of state IDs
  172.     elsif const[id].is_a?(Array)
  173.       # For each negative state ID, conver to positive add to array
  174.       const[id].each {|state_id| arr << -state_id if state_id < 0}
  175.     end
  176.     # Return all state IDs to be removed
  177.     arr
  178.   end
  179.   #-----------------------------------------------------------------------------
  180.   # * Remove States Battle
  181.   #-----------------------------------------------------------------------------
  182.   def remove_states_battle
  183.     # Set flag to true to allow natural end-of-battle state removal
  184.     @modding_critical_state = true
  185.     # The usual
  186.     criticalstates_gmbattler_remstbtl
  187.     # Default critical state activity flag back to false
  188.     @modding_critical_state = false
  189.   # This method is only alias defined if RemoveStatesBattle flag is true
  190.   end if CriticalConditionStates::RemoveStatesBattle
  191. end
  192.  
  193. #===============================================================================
  194. # ** Game_Actor
  195. #===============================================================================
  196.  
  197. class Game_Actor < Game_Battler
  198.   #-----------------------------------------------------------------------------
  199.   # * Get Critical States Plus
  200.   #-----------------------------------------------------------------------------
  201.   def get_critical_states_plus
  202.     # Super call with proper constant
  203.     super(CriticalConditionStates::Actor)
  204.   end
  205.   #-----------------------------------------------------------------------------
  206.   # * Get Critical States Minus
  207.   #-----------------------------------------------------------------------------
  208.   def get_critical_states_minus
  209.     # Super call with proper constant
  210.     super(CriticalConditionStates::Actor)
  211.   end
  212. end
  213.  
  214. #===============================================================================
  215. # ** Game_Enemy
  216. #===============================================================================
  217.  
  218. class Game_Enemy < Game_Battler
  219.   #-----------------------------------------------------------------------------
  220.   # * Get Critical States Plus
  221.   #-----------------------------------------------------------------------------
  222.   def get_critical_states_plus
  223.     # Super call with proper constant
  224.     super(CriticalConditionStates::Enemy)
  225.   end
  226.   #-----------------------------------------------------------------------------
  227.   # * Get Critical States Minus
  228.   #-----------------------------------------------------------------------------
  229.   def get_critical_states_minus
  230.     # Super call with proper constant
  231.     super(CriticalConditionStates::Enemy)
  232.   end
  233. end
  234.  
  235. #===============================================================================
  236. # ** Scene_Battle
  237. #===============================================================================
  238.  
  239. class Scene_Battle
  240.   #-----------------------------------------------------------------------------
  241.   # * Alias Listings
  242.   #-----------------------------------------------------------------------------
  243.   alias_method :criticalstates_snbattle_update, :update
  244.   #-----------------------------------------------------------------------------
  245.   # * Update
  246.   #-----------------------------------------------------------------------------
  247.   def update
  248.     # The usual
  249.     criticalstates_snbattle_update
  250.     # unless critical condition states have already been triggered
  251.     unless @_critical_condition_states_triggered
  252.       # For each actor, set its HP to its HP just to trigger critical states
  253.       $game_party.actors.each  {|a| a.hp = a.hp}
  254.       # For each enemy, set its HP to its HP just to trigger critical states
  255.       $game_troop.enemies.each {|e| e.hp = e.hp}
  256.       # Set flag to prevent repeat of this process
  257.       @_critical_condition_states_triggered = true
  258.     end
  259.   end
  260. end
Expand to see the code.


Instructions

Place Above Main (Below SDK if Using)

FAQ

Example syntax is already shown in the setup section of the script, but for the sanity of those that might be confused by this feel free to read the following if you're confused. Please note that all the examples are generic examples from the XP/VX default databases. Also, please note that the settings for Actor and Enemy follow the same exact concept.

If you get too confused, please feel free to ask questions and I'll help you out the best I can.

Adding single state to plus set Show
With this example, [Aluxes] will gain [Poison] when his HP reaches 1/4

  1. Actors = {
  2. 1 => 3
  3. }
  4.  
  5. # 1 is Aluxes' database ID
  6. # 3 is State's database ID
Expand to see the code.

Adding single state to minus set Show
With this example, [Aluxes] will lose [Stunned] when his HP reaches 1/4

  1. Actor = {
  2. 1 => -2
  3. }
  4. # 1 is Aluxes' database ID
  5. # 2 is State's database ID
  6. # With the value being -2, that means to remove this state
Expand to see the code.

Handling multiple states Show
I advise you to read the first two FAQ spoilers, since this is the same thing only you're putting the states into an array instead of pointing to a single state ID. Anyways...

With this example, [Aluxes] will gain [Poison] and lose [Stunned] when his HP reaches 1/4

  1. Actor = {
  2. 1 => [3, -2]
  3. }
  4.  
  5. # 1 is Aluxes' database ID
  6. # 3 means add Poison
  7. # -2 means remove Stunned
Expand to see the code.


Compatibility

Should be compatible with anything, works with or without SDK; didn't test VX but I believe it should work too, thats why I'm putting an XP/VX tag on it. I don't have VX on this machine though, so if you have any problems with the VX version let me know and I'll port it over to my other computer and fix it up.

This script aliases the following methods... Show
Game_Battler.initialize
Game_Battler.hp=
Game_Battler.add_state
Game_Battler.remove_state
Game_Battler.remove_states_battle


Author's Notes

It took longer to comment the script and write this topic than to write the actual script itself, so I would like some questions, comments or bug reports if you happen to be checking it out, I'm pretty sure that I got it down pretty good though =P

Terms and Conditions

Another "Free to use in commercial and non-commercial games" thingy, please credit and stuff :thumb:
Fixed small bug with state turns, replace what you have with this version (you don't have to, its not a big deal). Basically, if actor's HP is modified and no longer critical it'll remove 'critical states' which are supposed to wait X turns.

Feedback 'n stuff would be nice =P
Wait this can be very useful for positive skills that kick in when your in a critical state. Wait would this be possible if an accessory of passive skill that would grant this state when your in critical condition.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=67369