Chaos Project

RPG Maker => RPG Maker Scripts => RMXP Script Database => Topic started by: Heretic86 on April 29, 2015, 08:03:08 pm

Title: [XP] Heretic's Restrict Bush Passage
Post by: Heretic86 on April 29, 2015, 08:03:08 pm
Restrict Bush Passage XP
Authors: Heretic
Version: 1.0
Type: Custom Movement System
Key Term: Custom Movement System



Introduction

This script is similar in nature to Restrict Tile Passages, but is used for setting constraints by Bush Tags.  You can set a Character to ONLY or NEVER move on a Bush Tile.  It also contains a number of very important features to animating Heretic's Vehicles XP script.  It allows you to turn on Bush Effects, which are also useful for simulating Water, and animating the Bush Depth, even when there is no Bush Tile to stand on.


Features




Screenshots

Spoiler: ShowHide
(http://downloads.chaos-project.com/heretic86/images/RestrictBushPassage.jpg)

The NPCs in this screenshot on the Bush / Grass tiles move Randomly and are constrained to only move on the Bush Tiles, or prohibited from moving on them.  Also notice that you can see the Butterflies since their Bush Effects have been disabled.  I cant show a Bush Animation, but it is also there.



Demo

http://downloads.chaos-project.com/heretic86/MP/ModularPassable.exe

NOTE: The Demo contains a number of Modular Passable scripts.


Script

Place below Modular Passable  (http://forum.chaos-project.com/index.php/topic,14986.0.html) (Required)
Place below Collision Optimizer (Optional)
Place below Loop Maps (Optional)

Spoiler: ShowHide
#===============================================================================
#
#           HERETIC'S RESTRICT BUSH PASSAGE [XP]
#           Version 1.0
#           Tuesday, October 21st, 2014
#
#===============================================================================
#
# ---  Requirements and Installation  ---
#
# *** REQUIRES HERETICS MODULAR PASSABLE SCRIPT ***
#
# Place below Heretic's Modular Passable Script and above Main.
#
#
# This was an EXAMPLE SCRIPT of how to write Scripts that will be compatible
# with any other scripts dependant on Modular Passable.  Although it is
# intended only as an Example, you may have some use for this script.
#
# The Real Purpose of this script is to show that MANY scripts that all need
# to alter Passable in some way can be compatible without the need to replace
# the main passable methods themselves.  This script ONLY uses Aliases and
# does not replace any definitions.  It does add a few new ones.
#
#
# ---  Overview  ---
#
# This script will allow you to restrict passage based on Bush Tiles and
# allows total control over Bush Depth with Comments on each Event Page.
#
# Place the following Comments withing the first 10 or so Lines on
# each Event Page you wish to control passage based on Bush.
#
# \bush_only_move         - Event can ONLY move around on Bush Tiles
# \bush_no_move           - Event will NOT move on Bush Tiles
# \bush_off               - Prevents Bush Effects while on Bush Tiles
# \bush_force             - Forces Bush Effects even if not on Bush Tile
# \bush_anime             - Animates Bush Depth for simulating Water
# \bush_anime_smooth      - Smooth End when Bush Anime changed to Off
# \bush_same_rate         - Moving does not increase Bush Anime Speeds
# \bush_anime_max[N]      - Animation Bush Depth Max from the Default Depth
# \bush_anime_speed[N]    - Overrides Move Speed for Bush Animation
# \bush_depth_override[N] - Uses specified N value for Bush Depth instead
#                           of Default of 12.  Useful for small animals.
#
# NOTE: \bush_force WILL cause Events with Tileset Graphics (Event Tiles)
#       to also have a Bush Effect applied
#
#
# ---  Animations  ---
#
# I put in a couple things to allow Bush Depth to Animate.  Since the Bush
# Effect is merely a Transparency, I found some other uses for it, namely
# simulating a Character Floating in Water.  When this script is combined
# with my Boat / Vehicle script, you can make a Character float up and
# down with Comments made available by the Boat script while the Bush Anime
# causes a Transparency Effect, can create the# appearance that the Water
# is partially Transparent.  The result is that the Character looks like
# they are bobbing up and down in Water.
#
# ---  Script Calls  ---
#
# There is only one Script Call in this Script:
#
# - bush_depth_to(depth, duration, init_value)
#
# Use it on any Character and it will cause a Bush Depth Override Transition
# to take place.  The Bush Effect is merely a Transparency at the bottom
# of a Character's Sprite, so bush can be useful in other ways.  Another
# script I wrote for Boats and Vehicles has a lot to do with Water, so
# this script can be useful for using Bush Effects to simulate Water Depth.
# You can use bush_depth_to(20, 5, 0) to simulate jumping in water and
# allow a character to 'swim around'.
#
#===============================================================================

# Initialized Value for Bush Animations, Override with \bush_anime_max[N]
DEFAULT_BUSH_MAX = 2
# Initial Value for Bush Depth Override, Override this with a Comment or Arg
DEFUALT_BUSH_OVERRIDE_DEPTH = 10


# Check for Modular Passable Script - REQUIRED - DO NOT EDIT
unless $Modular_Passable
 print "Fatal Error: Heretics Restrict Bush Passage script\n",
       "requires Heretics Modular Passable Script!\n\n",
       "Modular Passable is Not Available or is below this script.\n",
       "Modular Passable MUST be above this script.\n\n",
       "The Game will now Exit"
 exit
end

#==============================================================================
# ** Game_Character - Class
#==============================================================================
class Game_Character
 #--------------------------------------------------------------------------
 # * Public Instance Variables - Game_Character
 #--------------------------------------------------------------------------
 attr_accessor     :bush_only_move         # NPCs only move on Bush Tiles
 attr_accessor     :bush_no_move           # Prevent NPCs on Bush Tiles
 attr_accessor     :bush_depth_override    # Overrides Bush Depth
 attr_accessor     :bush_force             # Forces a Bush Depth
 attr_accessor     :bush_anime             # Animates Bush Depth to Max
 attr_accessor     :bush_anime_speed       # Overrides Move Speed
 attr_accessor     :bush_same_rate         # Moving is Ignored for Anime
 attr_accessor     :bush_anime_smooth      # Smooth End to Bush Anime
 attr_accessor     :bush_anime_max         # Max Depth to Animate
 attr_accessor     :bush_anime_count       # Internal Bush Anime Counter
 attr_accessor     :bush_offset_count      # Offsets the Bush Depth for Anime
 attr_accessor     :bush_off               # Prevents Bush Effects
 attr_accessor     :bush_depth_target      # Transition a Bush Depth
 attr_accessor     :bush_duration          # Frames for Bush Depth Transition
 #--------------------------------------------------------------------------
 # * Object Initialization - Game_Character
 #  - Add Values before calling Original or other Aliases before Update
 #--------------------------------------------------------------------------
 alias bush_depth_animate_initialize initialize unless $@
 def initialize
   # Bush Depth Animation
   @bush_anime = false
   # Bush Animation Counter for Bush Depth Animation
   @bush_anime_count = 0
   # Overrides Move Speed for updating Bush Animations
   @bush_anime_speed = nil
   # Causes Movement to be ignored and Bush Animations occur at Same Rate
   @bush_same_rate = nil
   # Causes a Smooth End to Bush Animations when changing Bush Anime to false
   @bush_anime_smooth = nil
   # Bush Depth Target used to change Bush Depth during a Bush Transition
   @bush_depth_target = nil
   # Bush Duration is how many Frames it takes to adjust Bush Depth Target
   @bush_duration = 0
   # Bush Offse Count adjusts existing Bush Depths during an Animation Cycle
   @bush_offset_count = 0
   # Number of Pixels to Animate Up, then Down, so twice this value
   @bush_anime_max = DEFAULT_BUSH_MAX
   # Call Original or other Aliases
   bush_depth_animate_initialize
 end
 #--------------------------------------------------------------------------
 # * Bush Depth Adjust - Game_Character
 #       bush_depth : Default Thicket Depth (Bush)
 #  - Adjusts Bush Depth Values for Bush Depth Animation
 #  - Allows \bush_force to cause Bush Transparency Effects to simulate
 #    a Character floating in Water
 #--------------------------------------------------------------------------
 def bush_offset_adjust(bush_depth)
   # Shorthand for Bush Offset Count and Bush Anime Max
   bc = @bush_offset_count
   bm = @bush_anime_max
   # If Bush Count is less than Bush Anime Max * 2
   if bc < bm * 2
     # Determine if Adjust Up or Down
     return (bc < bm ? bc : bm*2 - bc) + bush_depth
   else
     # Determine if Subtract Values for Up or Down
     return (bc < bm * 3 ? bc - bm*2 : bm*4 - bc) * -1 + bush_depth
   end
 end
 #--------------------------------------------------------------------------
 # * Get Thicket Depth (Bush) - Game_Character
 #  - Allows overriding normal Bush Depth values with
 #    \bush_off, \bush_force, and \bush_depth_override[N] Comments
 #--------------------------------------------------------------------------
 alias rbp_bush_depth bush_depth unless $@
 def bush_depth
   # If Bush Effects are turned Off
   return 0 if @bush_off
   # If Self Conditions for not displaying a Thicket Depth
   if (@tile_id == 0 or @bush_force) and
       not @always_on_top and @jump_count == 0
     # If Bush Depth Override is set
     if @bush_depth_override
       # If Bush Effect is Forced \bush_force or actual Bush Tile
       if @bush_force or $game_map.bush?(@x, @y)
         # Return the Adjusted Specified Bush Depth
         return bush_offset_adjust(@bush_depth_override)
       end
     end
     # Return a Default Value from Config for Bush Forced
     return bush_offset_adjust(DEFUALT_BUSH_OVERRIDE_DEPTH) if @bush_force
   end
   # Adjust and Return Original or other Aliases
   return bush_offset_adjust(rbp_bush_depth)
 end
 #--------------------------------------------------------------------------
 # * Bush Depth To - Game_Character
 #       bush_depth_target : New Bush Depth Override
 #       bush_duration     : Number of Frames for Transition if set
 #       bush_depth_init   : Initial Value of Bush Depth Override if set
 #  - Executes a Transition of adjustment to Bush Depth over Duration
 #  - Bush Depth Override is Initialized at 0 if it doesn't exist
 #  - Useful for "Sinking" in Water when combined with Bush Force to
 #    force Bush Effects on Non Bush Tiles and 'Float Base To'
 #    from my Vehicle script due to Water Effects for Boats which
 #    allows a "Sinking" Position of character without altering
 #    collisions with Character.
 #--------------------------------------------------------------------------
 def bush_depth_to(bush_depth_target, bush_duration = 0, bush_depth_init = nil)
   # If No Duration
   if bush_duration == 0
     # Just set the Bush Depth Override with no Transition
     @bush_depth_override = bush_depth_target
     return
   end
   # If Bush Depth Override isnt a Number or Arg used for Initial Value
   if @bush_depth_override.nil? or bush_depth_init != nil
     # Set a Default Bush Depth Override value for Math called later
     if not bush_depth_init.nil?
       # A value was passed so use value passed in argument
       @bush_depth_override = bush_depth_init
     else
       # Use the Default from Config
       @bush_depth_override = DEFUALT_BUSH_OVERRIDE_DEPTH
     end
   end
   # The Bush Depth Override Target when finished
   @bush_depth_target = bush_depth_target
   # The Number of Frames to take to adjust Bush Target
   @bush_duration = bush_duration
 end
 #--------------------------------------------------------------------------
 # * Update Move (Frame) - Game_Character
 #  - Increases the Bush Count more quickly when Moving than when Stopped
 #  - Rate can be consistent with Stop Increment if a user applies either
 #    a \bush_same_rate Comment or sets @bush_same_rate in a Script
 #--------------------------------------------------------------------------
 alias bush_anime_update_move update_move unless $@
 def update_move
   # Call Original or other Aliases
   bush_anime_update_move
   # If Bush Animation is ON for Transparency
   if @bush_duration == 0 and (@bush_anime or (@bush_smooth and
      ( (not @bush_depth_override.nil? and @bush_depth_override != 0) or
      @bush_anime_count > 0)))
     # Increase Bush Count by 1.5 unless \bush_same_rate Override Comment
     @bush_anime_count += (@bush_same_rate) ? 1 : 1.5
   end
 end  
 #--------------------------------------------------------------------------
 # * Update Stop (Frame) - Game_Character
 #--------------------------------------------------------------------------
 alias bush_anime_update_stop update_stop unless $@
 def update_stop
   # Call Original or other Aliases
   bush_anime_update_stop
   # If Float Animation is ON
   if @bush_duration == 0 and @bush_anime or (@bush_smooth and
       ( (not @bush_depth_override.nil? and @bush_depth_override != 0) or
       @bush_anime_count > 0))
     # Increase Bush Count by 1 (Default Rate)
     @bush_anime_count += 1
   end      
 end  
 #--------------------------------------------------------------------------
 # * Bush Depth Animate - Game_Character
 #  - Animates Bush Depth Transitions and Animations
 #--------------------------------------------------------------------------
 def bush_depth_animate
   # If a Bush Depth To with a Duration has been called
   if @bush_duration > 0
     # Check for a Nil Bush Depth Override value and set to 0
     @bush_depth_override = 0 if @bush_depth_override.nil?
     # Shorthand
     t = @bush_depth_target.to_f
     d = @bush_duration
     # Adjust the Bush Depth Override over Duration Frames
     @bush_depth_override = (@bush_depth_override * (d - 1) + t) / d
   end
   # If Character has a Float Animation or Float Smooth Animation
   if @bush_duration == 0 and @bush_anime or
      (@bush_smooth and @bush_offset_count > 0)
     # Check for a "Smooth" End to Bush Anime in the middle of the Anime Cycle
     if @bush_smooth and not @bush_anime and
        @bush_offset_count == 2 * @bush_anime_max
       # Clear the Internal Bush Counters while Offset is Centered
       @bush_anime_count = 0
       @bush_offset_count = 0
     end
     # If Bush Anime Speed has been set for Character \bush_anime_speed[N]
     if @bush_anime_speed
       # If animation count exceeds maximum value
       # * Maximum value is Bush Anime Speed * 2 taken from basic value 18
       if @bush_anime_count > 18 - @bush_anime_speed * 2
         # Update Bush Offset Value
         @bush_offset_count += 1
         # If Bush Offset equals Bush Anime Max * 4 for Four Animation Cycles
         if @bush_offset_count == @bush_anime_max * 4
           # Clear Bush Offset Counter
           @bush_offset_count = 0
         end
        # Clear Bush Anime Counter
        @bush_anime_count = 0
       end
     # Use Move Speed for updating Bush Animations if Flags are set
     else
       # If animation count exceeds maximum value
       # * Maximum value is Bush Anime Speed * 2 taken from basic value 18
       if @bush_anime_count > 18 - @move_speed * 2
         # Update Bush Offset Value
         @bush_offset_count += 1
         # If Bush Offset equals Bush Anime Max * 4 for Four Animation Cycles
         if @bush_offset_count == @bush_anime_max * 4
           # Clear Bush Offset Counter
           @bush_offset_count = 0
         end
        # Clear Bush Anime Counter
        @bush_anime_count = 0
       end
     end
   end
   # If Bush Depth To has not completed
   if @bush_duration > 0
     # Decrease the Duration Cycle to End
     @bush_duration -= 1
     # End the 'Bush Depth To' Transition
     @bush_depth_override = @bush_depth_target if @bush_duration == 0        
   end    
 end
 #--------------------------------------------------------------------------
 # * Update - Game_Character
 #  - Alias of Main Update method for Game_Character
 #  - Updates Bush Depth Transitions if any are available
 #--------------------------------------------------------------------------
 alias bush_depth_update update
 def update
   # Animates any Bush Depth Animations or Transitions
   bush_depth_animate
   # Call Original or other Aliases
   bush_depth_update
 end  
 #--------------------------------------------------------------------------
 # * Other Passable? - Game_Character
 #  - This check is performed prior to Event Iteration for Performance
 #  - Notice how this method maintains Class Stucture
 #  - Must return True to allow for Passage
 #     x     : x-coordinate
 #     y     : y-coordinate
 #     d     : direction (0,2,4,6,8)
 #             * 0 = Determines if all directions are impassable (for jumping)
 #     new_x : Target X Coordinate
 #     new_y : Target Y Coordinate
 #     result : true or false passed as an arg by Aliases  
 #--------------------------------------------------------------------------
 alias bush_other_passable? other_passable? unless $@
 def other_passable?(x, y, d, new_x, new_y, result = nil)
   # If nothing else has set a Result and not Player
   if self != $game_player
     # If Bush settings consistent with Map Bush Tile
     if (@bush_only_move and not $game_map.bush?(new_x, new_y)) or
        (@bush_no_move and $game_map.bush?(new_x, new_y))
       # Impassable - Bush Settings prohibit Passage (Other Checks IGNORED)
       return false
     end
   end
   # Call Original or other Aliases with any Results
   bush_other_passable?(x, y, d, new_x, new_y, result)
 end    
end

#==============================================================================
# ** Scene_Load
#==============================================================================
class Scene_Load < Scene_File
 #--------------------------------------------------------------------------
 # * Read Save Data - Scene_Load
 #     file : file object for reading (opened)
 #  - Checks for Uninitialized Values that Crash Savegames on Installation
 #--------------------------------------------------------------------------
 alias bush_rbp_read_save_data read_save_data unless $@
 def read_save_data(file)
   # Call Original or other Aliases
   bush_rbp_read_save_data(file)
   # Check the Player for Nil values that require Numbers
   $game_player.bush_duration = 0 if $game_player.bush_duration.nil?
   $game_player.bush_anime_count = 0 if $game_player.bush_anime_count.nil?
   $game_player.bush_offset_count = 0 if $game_player.bush_offset_count.nil?
   # If Bush Anime Max is Nil, it has to be a Number so use Default
   if $game_player.bush_anime_max.nil?
     # Assign Default Value from Config
     $game_player.bush_anime_max = DEFAULT_BUSH_MAX
   end
   # Check All Map Events
   for event in $game_map.events.values
     # These can be set to 0 as they are handled internally
     event.bush_duration = 0 if event.bush_duration.nil?
     event.bush_anime_count = 0 if event.bush_anime_count.nil?
     event.bush_offset_count = 0 if event.bush_offset_count.nil?
     # Assign Default Value from Config
     event.bush_anime_max = DEFAULT_BUSH_MAX if event.bush_anime_max.nil?
   end
 end
end

#==============================================================================
# ** Game_Event - Class
#==============================================================================
class Game_Event < Game_Character
 #--------------------------------------------------------------------------
 # * Reset Page Comment Config - Game_Event
 #  - Resets Instance Variables
 #  - Called in Event::Refresh on a Page Change
 #  - Maintains Class Structure of Modular Passable
 #  - You can Alias this in Game_Event, not Game_Character
 #--------------------------------------------------------------------------
 alias bush_reset_page_comment_config reset_page_comment_config unless $@
 def reset_page_comment_config
   # Reset to set again by Comment Conditions
   @bush_only_move = nil
   @bush_no_move = nil
   @bush_off = nil
   @bush_force = nil
   @bush_depth_target = nil
   @bush_duration = 0
   @bush_depth_override = nil
   # These will cause a Bush Depth Transition to repeat Infintely
   @bush_anime = nil
   @bush_anime_speed = nil
   @bush_same_rate = nil
   @bush_anime_smooth = nil    
   @bush_anime_count = 0
   @bush_offset_count = 0
   @bush_anime_max = DEFAULT_BUSH_MAX
   # Runs Original or Other Aliases of this method
   bush_reset_page_comment_config
 end
 #--------------------------------------------------------------------------
 # * Bush Check Comment Page Config - Game_Event
 #     comment : the Comment to be checked to adjust Page Properties
 #     count   : integer of which List of Event Commands is checked
 #  - Checks for \bush_only_move, \bush_no_move, and \bush_off Comments
 #  - Saves Number found in \bush_depth_override[N] Comments
 #  - Called in Event Refresh on a Page Change  
 #  - Returns count when a Comment is found to prevent checks by other Aliases
 #    since Comment found is specific to this script
 #
 #  Note: When aliasing, please return the value of count, especially
 #        if your script adjusted this value.
 #--------------------------------------------------------------------------
 alias bush_check_page_comment_config check_page_comment_config unless $@
 def check_page_comment_config(comment, count)
   # Looks for "\bush_only_tiles" in the Comments with Regular Expressions
   comment.gsub(/^\\bush_only_move\z/i){@bush_only_move = true; return count}
   # Looks for "\bush_no_move" in the Comments
   comment.gsub(/^\\bush_no_move\z/i){@bush_no_move = true; return count}
   # Looks for "\bush_depth_override[N] Comments (to Integer)
   comment.gsub(/^\\bush_depth_override\[([-0-9.]+)\]\z/i){
     @bush_depth_override = $1.to_f if $1.to_f.is_a?(Numeric); return count;}
   # Looks for "\bush_off" in the Comments to disable Bush Effects
   comment.gsub(/^\\bush_off\z/i){@bush_off = true; return count}
   # Looks for "\bush_force" in the Comments to Force Bush Effects anywhere
   comment.gsub(/^\\bush_force\z/i){@bush_force = true; return count}
   # Looks for "\bush_anime" in the Comments to Animate Bush Depth
   comment.gsub(/^\\bush_anime\z/i){@bush_anime = true; return count}
   # Looks for "\bush_anime_speed[N] Comments (to Integer)
   comment.gsub(/^\\bush_anime_speed\[([-0-9.]+)\]\z/i){
     @bush_anime_speed = $1.to_f if $1.to_f.is_a?(Numeric); return count;}
   # Looks for "\bush_anime_smooth" in the Comments to Animate Bush Depth
   comment.gsub(/^\\bush_anime_smooth\z/i){@bush_anime_smooth = true;
     return count}
   # Looks for "\bush_same_rate" in the Comments to Animate Bush Depth
   comment.gsub(/^\\bush_same_rate\z/i){@bush_same_rate = true;
     return count}
   # Looks for "\bush_anime_max[N] Comments (to Integer)
   comment.gsub(/^\\bush_anime_max\[([0-9]+)\]\z/i){
     @bush_anime_max = $1.to_i if $1.to_i.is_a?(Numeric); return count;}    
   # Return counter for other Aliases when no Comments are found
   return bush_check_page_comment_config(comment, count)
 end
end



Instructions

This script is fairly straight forward to use.  Just add a Comment to an Event:

\bush_only_move         - Event can ONLY move around on Bush Tiles
\bush_no_move           - Event will NOT move on Bush Tiles
\bush_off               - Prevents Bush Effects while on Bush Tiles
\bush_force             - Forces Bush Effects even if not on Bush Tile
\bush_anime             - Animates Bush Depth for simulating Water
\bush_anime_smooth      - Smooth End when Bush Anime changed to Off
\bush_same_rate         - Moving does not increase Bush Anime Speeds
\bush_anime_max[N]      - Animation Bush Depth Max from the Default Depth
\bush_anime_speed[N]    - Overrides Move Speed for Bush Animation
\bush_depth_override[N] - Uses specified N value for Bush Depth instead
                          of Default of 12.  Useful for small animals.

There is one Script Call: bush_depth_to(new_depth, duration, init_value = nil)

These properties can be applied to the Player as well by accessing the corresponding property of each comment.  Thus an Event Comment of \bush_off would have a property of @bush_off = true / false.


Compatibility

Not compatible with Lawnmowers, Weedwhackers, or Hedge Trimmers.


Credits and Thanks




Author's Notes

Restricting Passages is most useful for NPCs or Events that you want to move Randomly and retain some level of control over where they move.  The rest of the Effects are most useful when combined with Heretic's Vehicles XP as Bush and Water are interchangable.