Chaos Project

RPG Maker => RPG Maker Scripts => RMXP Script Database => Topic started by: Heretic86 on October 01, 2013, 07:56:59 am

Title: [XP][VX][VXA] Heretic's Auto State Switches
Post by: Heretic86 on October 01, 2013, 07:56:59 am
Heretic's Auto State Switches
Authors: Heretic, LittleDrago
Version: 1.03
Type: Automatic Game Switch Controller
Key Term: Environment Add-on



Introduction

This Script will automagically Enable and Disable a specified Game Switch when a Party Member has a specified State, such as a Spell Effect, or certain type of Armor equipped.


Features




Screenshots

No Screenshots


Demo
XP - Download Demo (http://www.775.net/~heretic/downloads/rmxp/AutoStateSwitches.exe)


Script

Place anywhere above Main and below Default Scripts.
Spoiler: ShowHide
#==============================================================================
#
#      HERETIC's AUTO STATE SWITCHES [XP/VX/VXA]
#      Version 1.03
#      VX/VXA additions by LiTTleDRAgo
#      Wednesday, October 2nd, 2013
#
#==============================================================================
#
#  ***  YOU MUST EDIT THE CONFIG BEFORE USING THIS SCRIPT  ***
#
#  This Script will allow Game Switches to be changed Automatically when a
#  State is applied to ANY Member of the Party.
#
#  --- CHANGE LOG  ---
#
#  - 1.01 - Little Drago made compatible with VX and Ace
#
#  - 1.02 - Added Error Checking to allow for Knockout States to change
#    Switches when MOVABLE is set to require the Actor be Alive.
#    So Knockout States can change State Switches if your ID is a Knockout
#    State in the Config.
#
#  - 1.03 - Added code to check Armor Changing by Event.  Aliased command_319
#    Fixing silly mistakes, and lots of stuff for compatability with VX/Ace.
#
#  ***  IMPORTANT  ***
#
#  Once you set a Game Switch ID, do not try to alter that Switch in any
#  other way.  The Switch is controlled Automatically by the Script and
#  is not checked frequently.  The Script only checks  when a State is Added
#  or Removed, or when the Game Party is Refreshed to have minimal impact
#  on compatability and performance.
#
#  When any Actor in the Party has a specific State, the corresponding Game
#  Switch will be Enabled.  If none of the Actors in the Party have a specified
#  State, the corresponding Game Switch is set to False / Off.
#
#  This is all done with State IDs and Switch IDs.  These might be a big
#  confusing, but the Database and Editor do just fine at telling you
#  what the ID's are.  So dont use Names.  I also didnt put in any
#  Error Checking so if you mess up your Config, you'll have to figure
#  it out for yourself.  The Defaults in the Script correspond to a Demo
#  so you MUST CHANGE the Values.  I tried to pick High Numbers to not
#  cause any conflits in your Game.
#
#  ---  USES  ---
#
#  This is Useful for altering the Behavior of NPCs.  You might use this
#  to have Townsfolk say Different Things.  If you have Enemy NPC Events
#  this is useful for altering their Behavior by adding additional Pages
#  to those Enemy Events.  Game Switches are used specifically because
#  of the Effects they have on Events and Pages.
#
#  ---  OPTIONS  ---
#
#  Enabled: In case you want or need to disable the effects of this script.
#  change with $game_system.state_switches.enabled = true / false
#
#  Movable: If you want to allow Dead Actors to affect the Game Switches
#  change with $game_system.state_switches.movable = true / false
#
#  List: Alters the List of State IDs and Switch IDs.  Not commonly needed.
#  change with $game_system.state_switches.list = [ [state_id, switch_id] ]
#
#
#==============================================================================

#==============================================================================
#    ***  CONFIG   ***   (Default Values)
#==============================================================================
class State_Switches_Config
 # Change the List around HERE as needed.  Defaults, can be changed later.
 LIST = [
          # [State ID, Game Switch ID]
          # Put Commas between each [],[] except at end
          [1, 20],  # State 1 Knockout enabled Game Switch for KO in Party
          [17, 10], # State 17 Stinky enables Game Switch 10 for Stinky Armor
          [18, 11], # State 18 Smell Good Enables Game Switch 11 Smell Good
          [19, 12], # State 19 Invis enables Game Switch 11 for Invisible
                    # Either Armor or by Spell, Spell Wears off after 10 sec.
        ]
 
 # If Actors MUST BE ALIVE for Effects
 MOVABLE = true
end

#==============================================================================
# ** State Switches for Game_System Object
#
#    These can ALL be Edited on the fly with $game_system.state_switches
#    - Enabled: Whether to Change Switches or not
#    - List: List of State ID's and Corresponding Game Switch IDs
#    - Movable: If Actor MUST BE ALIVE for Effect to be recognized
#==============================================================================
class State_Switches
 #--------------------------------------------------------------------------
 # * Public Instance Variables
 #--------------------------------------------------------------------------
 attr_accessor   :enabled     # Enable to allow Auto Changing Game Switches
 attr_accessor   :movable     # Require Actors to be Alive to Change
 attr_accessor   :list        # List of State and Switches to Change  
 #--------------------------------------------------------------------------
 # * Object Initialization - Uses Config for Default Values
 #--------------------------------------------------------------------------
 def initialize
   # DO NOT EDIT HERE
   @enabled = true
   @movable = State_Switches_Config::MOVABLE
 end
 #--------------------------------------------------------------------------
 # * List of State and Switches to Change
 #--------------------------------------------------------------------------
 def list
   @list || State_Switches_Config::LIST
 end
end
               
#==============================================================================
# ** Game_System
#==============================================================================

class Game_System
 #--------------------------------------------------------------------------
 # * Public Instance Variables
 #--------------------------------------------------------------------------
 attr_writer   :state_switches  # Enable to allow Auto Changing Switces
 #--------------------------------------------------------------------------
 # * Game System Initialization
 #   - Add @state_switches as Object for Settings
 #   - Access and change with $game_system.state_switches.property =
 #--------------------------------------------------------------------------
 def state_switches
   # Object holds State Switches
   @state_switches ||= State_Switches.new
 end
end

#==============================================================================
# ** Game_Party
#==============================================================================
class Game_Party
 #--------------------------------------------------------------------------
 # * Alias Listing
 #--------------------------------------------------------------------------
 unless method_defined?(:auto_state_switches_ssm)
   method_defined?(:members) || (alias_method :members,            :actors)
    method_defined?(:refresh) && (alias_method :auto_state_refresh, :refresh)
   alias_method :auto_state_switches_ssm,  :setup_starting_members
 end
 #--------------------------------------------------------------------------
 # * Initial Party Setup
 #--------------------------------------------------------------------------
 def setup_starting_members(*args)
   # Call Original or Other Aliases
   auto_state_switches_ssm(*args)
   # Check the Original Armors for Switches
   check_auto_state_switches
 end  
 #--------------------------------------------------------------------------
 # * Check Auto State Switches
 #--------------------------------------------------------------------------
 def check_auto_state_switches
   # If Auto Changing Switches is Enabled
   if $game_system.state_switches.enabled
     # Check for State ID for Switch Disabling
     for state_switch in $game_system.state_switches.list
       # Get actor state result
       actor_has_state = result_auto_state_switches(state_switch)
       # Execute all auto switches
       execute_auto_state_switches(state_switch, actor_has_state)
     end
   end    
 end
 #--------------------------------------------------------------------------
 # * Result Auto State Switches
 #--------------------------------------------------------------------------
 def result_auto_state_switches(state_switch)
   # Get Values
   state, switch = *state_switch
   # Get Config
   require_movable = $game_system.state_switches.movable
   # Result of each Actors having this State
   actor_has_state = members.compact.any? do |actor|
     # If somehow states id is not an integer
     states = actor.states.collect {|s| s.is_a?(Integer) ? s : s.id }
     # If Actor not Dead and Actor has this State or
     # State checking for is Dead
     states.include?(state) and (actor.is_state_knockout?(state) or
         not require_movable or (require_movable and actor.movable?))
   end
   # Returns the result    
   return actor_has_state    
 end
 #--------------------------------------------------------------------------
 # * Execute Auto State Switches
 #--------------------------------------------------------------------------
 def execute_auto_state_switches(state_switch, actor_has_state)
   # Get Values
   state, switch = *state_switch
   # if switch is Integer and State of Switch not same as Actors having State
   if switch.is_a?(Integer) and actor_has_state != $game_switches[switch]
     # Change the Auto Game Switches to match Value
     $game_switches[switch] = actor_has_state
     # Refresh the Map and all Events accordingly
     $game_map.need_refresh = true
   end
 end
 #--------------------------------------------------------------------------
 # * Refresh Party - Needed for when a Save Game Loads
 # Only RMXP has $game_party.refresh method
 #--------------------------------------------------------------------------
 if method_defined?(:refresh)  
   def refresh(*args)
     # Call Original or Other Aliases
     auto_state_refresh(*args)
     # Check for Switch Changes
     $game_party.check_auto_state_switches
   end
 end
end

#==============================================================================
# ** Game_Map
#==============================================================================
class Game_Map
 #--------------------------------------------------------------------------
 # * Alias Listing
 #--------------------------------------------------------------------------
 unless method_defined?(:auto_state_setup)
   alias_method :auto_state_setup, :setup
 end
 #--------------------------------------------------------------------------
 # * Setup Map - Needed for when a Save Game Loads
 #--------------------------------------------------------------------------
 def setup(*args)
   # Call Original or Other Aliases
   auto_state_setup(*args)
   # Check for Switch Changes
   $game_party.check_auto_state_switches
 end
end

#==============================================================================
# ** Game_Battler
#==============================================================================
class Game_Battler
 #--------------------------------------------------------------------------
 # * Alias Listing
 #--------------------------------------------------------------------------
 unless method_defined?(:auto_state_switches_add_state)
   alias_method :auto_state_switches_add_state,    :add_state
   alias_method :auto_state_switches_remove_state, :remove_state
 end
#--------------------------------------------------------------------------
 # * State Knockout - True if State has Knockout Properties
 #   -  Works with XP, VX, and VX Ace
 #
 #--------------------------------------------------------------------------  
 def is_state_knockout?(state_id)
   # If Window_ActorCommand class is exist (VX or VXA)
   death_state = defined?(Window_ActorCommand) ? 1 : nil
   # If method death_state_id is exist
   death_state = death_state_id if self.respond_to?(:death_state_id)
   # If state id is death state
   if [death_state].flatten.include?(state_id)
     # True to State has Knockout Properties
     return true
   # If state id is not death state and death state is not nil
   elsif death_state != nil
     # False if not RMXP
     return false
   end
   # Get the State Information from Data Table
   state = $data_states[state_id]
   # If State Restriction Can't Move, Cant Get EXP and Regard as HP 0
   if state.restriction == 4 and state.cant_get_exp and state.zero_hp
     # True to State has Knockout Properties
     return true
   end
 end  
 #--------------------------------------------------------------------------
 # * Add State
 #     state_id : state ID
 #     force    : forcefully added flag (used to deal with auto state)
 #--------------------------------------------------------------------------
 def add_state(*args)
   # Call Original or Other Aliases
   auto_state_switches_add_state(*args)
   # Get scene class
   scene = defined?(SceneManager) ? SceneManager.scene : $scene
   # If scene is not Scene_Title (to avoid stack error)
   unless scene.nil? or scene.is_a?(Scene_Title)
     # Check for State Self Switch Changes if Game_Actor
     $game_party.check_auto_state_switches if self.is_a?(Game_Actor)
   end
 end
 #--------------------------------------------------------------------------
 # * Remove State
 #     state_id : state ID
 #     force    : forcefully removed flag (used to deal with auto state)
 #--------------------------------------------------------------------------
 def remove_state(*args)
   # Call Original or Other Aliases
   auto_state_switches_remove_state(*args)
   # If Game Actor
   if self.is_a?(Game_Actor)
     # Check for State Self Switch Changes
     $game_party.check_auto_state_switches
   end
 end
end

VX    = defined?(Window_ActorCommand)
Klass_Name = VX ? Game_Interpreter : Interpreter
#==============================================================================
# ** Interpreter for XP - Game Interpreter for VX / Ace
#==============================================================================
class Klass_Name
 #--------------------------------------------------------------------------
 # * Alias Listing
 #--------------------------------------------------------------------------
 unless method_defined?(:auto_state_switches_command_319)
   alias_method :auto_state_switches_command_319, :command_319
 end
 #--------------------------------------------------------------------------
 # * Change Equipment
 #--------------------------------------------------------------------------  
 def command_319
   # Call Original or Other Aliases
   result = auto_state_switches_command_319
   # Check for State Self Switch Changes
   $game_party.check_auto_state_switches
   # for Interpreters
   return result
 end
end



Instructions

YOU MUST CONFIGURE THIS SCRIPT BEFORE USING

Edit the List of State ID's and Game Switch ID's

List = [ [25,114],[26,115] ]

The List is an Array of Arrays.  Each Array is a STATE ID and GAME SWITCH to change.  So in the example provided, 25 is the State ID and 114 is the Game Switch.  When ANY Member of your Party has a State of 25, the Game Switch will be set to On.  When NO Members of your Party have a State of 25, the Game Switch is Off.  

If you really want to, you can use One State ID to change Multiple Switches.

I also strongly recommend labeling your Game Switches as "(Auto) whatever" so you do not come back and try to edit that Game Switch with other Commands later.  Just leave the Auto Switches alone once you assign one of them to this Script.


Compatibility

May not work on the Atari 2600


Credits and Thanks




Author's Notes

This is just a really efficient way of Automating Game Switches as opposed using Parallel or Common Events to alter Game Switches.  Where this script really shines is in the Power of Event Pages.  Both in Map Events and Battle Events.  No changes need to be made to the States themselves for this Scritp to function properly.  This script is designed to work as a Standalone Script that compliments a Collection that I am also working on, including Super Event Sensor (not yet released, working on Demo).  This script is very simple powerful tool for making your game much more Dynamic.  It works very well with Spells as Spells can cause States to be imposed.  The states dont even have to do anything.  For example, just label it Invisible to prevent a Player from being approached by an Enemy NPC.  That would require an additional Event Page for an Enemy Event NPC.  It is also very useful in Battle for checking an Actors State during a Battle.  Oh, main actor is Knocked Out?  Time to event out some drama.  States were also chosen over Elements because Armors can induce an Auto State.

---  BUGS  ---

Most of the Bugs will be Your Own.

No offense.  This is just to tell you of what Pitfalls to avoid.  You may find that after editing the Config, the Settings you are trying to use are NOT working.  As you develop your game, you'll find you need to make changes to your Database for things like Adding New Items, and States, etc.  If you come across times where you find that changing your Database and editing the Config is not working as expected in your game, it is NOT a bug, it is a feature.  The script was set up so you can change your State Switches on the Fly.  What is causing your most common appearance of a Bug is that you've loaded a Saved Game where the New Settings are NOT APPLIED.  Starting a New Game should cause your State IDs and Switches to work as expected.  I did it this way because there may be times you want to alter the List of State IDs and Switch IDs as a story progresses.

If you want to check what ID's are being shown due to possible bugs, do this.  The Text in the Script Commands is too short due to Word Wrap so I'll fix that.  (Edit: Fixed, had something else on my brain, put in the wrong variable names)

s=$game_system.state_switches
print s.list

To Change the List (IE Save Game or suspected Bug):

s=$game_system.state_switches
new = [ [old_state_id, old_switch_id],[new_state_id, new_switch_id]  ]
s.list = new

Replace the Old and New text there with just your IDs so it should look more like this:

new = [ [12,23],[34,45],[56,78] ]

Just letting you know about Pitfalls before you get caught in one.  You'll probably need this during Development of your Game, but probably not use it for the Game Player.  I can not predict that you will or will not, thus, I allowed you the ability to change on the fly, even if it does cause a bit more work, it shouldnt be too much.

---

Edit: Using Little Drago's revision for VX and VX Ace compatability since I have neither.
Title: Re: [XP] Heretic's Auto State Switches
Post by: LiTTleDRAgo on October 02, 2013, 07:30:57 am
it's a nice script, btw I have edited it so it would compatible with VX or VXA (and also to prevent corrupted old savegames)

http://pastebin.com/q7iXT2xq
Title: Re: [XP][VX][VXA] Heretic's Auto State Switches
Post by: Heretic86 on October 02, 2013, 09:12:43 am
That works since I think everyone knows I dont have VX or Ace.  I missed that save bug.
Title: Re: [XP][VX][VXA] Heretic's Auto State Switches
Post by: Heretic86 on October 03, 2013, 03:17:04 am
Updated to version 1.02

XP Demo Released.  I dont have VX or Ace so someone else that feels like doing a demo.  The Demo will get the point across.

Changed it around to check if the State ID in the List is a Knockout State.  This allows Knockout States to also change Game Switches while the MOVABLE Option is Enabled.  Normally, when an actor is Knocked Out (not Movable), the script excludes them allowing to enable the Game Switch.  Say Basil has an Armor of Switchiness.  If Basil is Knocked Out and he is the only Party Member that has such an Armor, then that Game Switch will be Disabled because Basil is Dead.  I had to update to 1.02 because Knockout was excluded because the actor was dead, which was a bug that is now fixed, with Little Drago's help for VX and Ace version compatability.

Smell ya later!  (You'll understand that once you download the demo)

---

Updated to 1.03 - Fixed Initial Equipment not being checked.