Guest User

Preemptive-Surprise Event Battle

a guest
Jun 4th, 2015
393
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #==============================================================================
  2. # ** Preemptive-Surprise Event Battle
  3. # by: Jeneeus Guruman
  4. #------------------------------------------------------------------------------
  5. #     This script allows to make evented encounters have preemptive and
  6. #   surprise encounters.
  7. #
  8. #   How to use:
  9. #
  10. #     * Plug-n-play
  11. #     * Place this below default and most non-aliased scripts.
  12. #     * Not compatible and has problems with the following:
  13. #       - Any 8-directional movement scripts
  14. #       - Any pixel movement scripts
  15. #     * Not recommended if followers are shown.
  16. #     * "Encounter Half" and "Encounter None" party abilities do not work for
  17. #     evented encounters but "Raise Preemptive" and "Cancel Surprise" does work.
  18. #
  19. #==============================================================================
  20.  
  21. module Jene
  22.   #--------------------------------------------------------------------------
  23.   # * PREEMPTIVE_SIDE_CHANCE
  24.   #     The percent chance of having a preemtive encounter occured when
  25.   #   going to the evented enemy's side. Putting any negative number will use
  26.   #   the default formula for preemptive attack.
  27.   #--------------------------------------------------------------------------
  28.   PREEMPTIVE_SIDE_CHANCE = -1
  29.   #--------------------------------------------------------------------------
  30.   # * SURPRISE_SIDE_CHANCE
  31.   #     The percent chance of having a surprised encounter occured when enemies
  32.   #   are going to the character's side. Putting any negative number will use
  33.   #   the default formula for surprise attack.
  34.   #--------------------------------------------------------------------------
  35.   SURPRISE_SIDE_CHANCE = -1
  36.   #--------------------------------------------------------------------------
  37.   # * PREEMPTIVE_SURPRISE_SWITCH
  38.   #     The switch that will enable to use the preemptive-surprise function.
  39.   #   Making the value 0 will make it always enabled. Making the value negative
  40.   #   will disable it instead when turning on.
  41.   #--------------------------------------------------------------------------
  42.   PREEMPTIVE_SURPRISE_SWITCH = 0
  43.   #--------------------------------------------------------------------------
  44.   # * PREEMPTIVE_SURPRISE_AFFIX
  45.   #     The affix name of the event that will enable to use the
  46.   #   preemptive-surprise function.
  47.   #--------------------------------------------------------------------------
  48.   PREEMPTIVE_SURPRISE_AFFIX = "[enemy]"
  49.   #--------------------------------------------------------------------------
  50.   # * PREEMPTIVE_SURPRISE_TANKENTAI
  51.   #     Always enable this if you are using the Tankentai Ace script and
  52.   #   always disable this if otherwise. Not doing so will cause problems.
  53.   #--------------------------------------------------------------------------
  54.   PREEMPTIVE_SURPRISE_TANKENTAI = false
  55. end
  56.  
  57. #----------------------------------------------------------------------------
  58. # * Do not edit anything below here. Or else, evil errors will be released
  59. # from the other dimension of evil.
  60. #----------------------------------------------------------------------------
  61.  
  62. #==============================================================================
  63. # ** BattleManager
  64. #------------------------------------------------------------------------------
  65. #  This module manages battle progress.
  66. #==============================================================================
  67.  
  68. module BattleManager
  69.   class <<self
  70.     alias jene_pseb_on_encounter on_encounter
  71.     alias jene_pseb_battle_end battle_end
  72.   end
  73.   #--------------------------------------------------------------------------
  74.   # * Processing at Encounter Time
  75.   #--------------------------------------------------------------------------
  76.   def self.on_encounter
  77.     if Jene::PREEMPTIVE_SURPRISE_SWITCH > 0
  78.       psbe_enable = $game_switches[Jene::PREEMPTIVE_SURPRISE_SWITCH]
  79.     elsif Jene::PREEMPTIVE_SURPRISE_SWITCH < 0
  80.       psbe_enable = !$game_switches[-Jene::PREEMPTIVE_SURPRISE_SWITCH]
  81.     else
  82.       psbe_enable = true
  83.     end
  84.     if psbe_enable
  85.       @preemptive = $game_temp.ps_num > 0
  86.       @surprise = $game_temp.ps_num < 0
  87.     else
  88.       jene_pseb_on_encounter
  89.     end
  90.     if Jene::PREEMPTIVE_SURPRISE_TANKENTAI
  91.       $sv_camera.mirror = @surprise if N03::BACK_ATTACK
  92.     end
  93.   end
  94.   #--------------------------------------------------------------------------
  95.   # * End Battle
  96.   #     result : Result (0: Win 1: Escape 2: Lose)
  97.   #--------------------------------------------------------------------------
  98.   def self.battle_end(result)
  99.     $game_temp.ps_num = 0
  100.     jene_pseb_battle_end(result)
  101.   end
  102. end
  103.  
  104. #==============================================================================
  105. # ** Game_Temp
  106. #------------------------------------------------------------------------------
  107. #  This class handles temporary data that is not included with save data.
  108. # The instance of this class is referenced by $game_temp.
  109. #==============================================================================
  110.  
  111. class Game_Temp
  112.   #--------------------------------------------------------------------------
  113.   # * Public Instance Variables
  114.   #--------------------------------------------------------------------------
  115.   attr_accessor :ps_num                 # Number indicator for encounters
  116.   #--------------------------------------------------------------------------
  117.   # * Object Initialization
  118.   #--------------------------------------------------------------------------
  119.   alias jene_pseb_temp_initialize initialize
  120.   def initialize
  121.     jene_pseb_temp_initialize
  122.     @ps_num = 0
  123.   end
  124. end
  125.  
  126. #==============================================================================
  127. # ** Game_CharacterBase
  128. #------------------------------------------------------------------------------
  129. #  This base class handles characters. It retains basic information, such as
  130. # coordinates and graphics, shared by all characters.
  131. #==============================================================================
  132.  
  133. class Game_CharacterBase
  134.   #--------------------------------------------------------------------------
  135.   # * Public Instance Variables
  136.   #--------------------------------------------------------------------------
  137.   attr_reader   :prev_direction           # Previous Direction
  138.   #--------------------------------------------------------------------------
  139.   # * Initialize Public Member Variables
  140.   #--------------------------------------------------------------------------
  141.   alias jene_pseb_init_public_members init_public_members
  142.   def init_public_members
  143.     jene_pseb_init_public_members
  144.     @prev_direction = 2
  145.   end
  146.   #--------------------------------------------------------------------------
  147.   # * Change Direction to Designated Direction
  148.   #     d : Direction (2,4,6,8)
  149.   #--------------------------------------------------------------------------
  150.   def set_direction(d)
  151.     @prev_direction = @direction if !@direction_fix && @direction != 0
  152.     @direction = d if !@direction_fix && d != 0
  153.     @stop_count = 0
  154.   end
  155. end
  156.  
  157. #==============================================================================
  158. # ** Game_Event
  159. #------------------------------------------------------------------------------
  160. #  This class handles events. Functions include event page switching via
  161. # condition determinants and running parallel process events. Used within the
  162. # Game_Map class.
  163. #==============================================================================
  164.  
  165. class Game_Event < Game_Character
  166.   #--------------------------------------------------------------------------
  167.   # * Public Instance Variables
  168.   #--------------------------------------------------------------------------
  169.   attr_reader   :event                    # Event
  170.  
  171.   #--------------------------------------------------------------------------
  172.   # alias method: update_self_movement
  173.   #--------------------------------------------------------------------------
  174.   alias jene_pseb_update_self_movement update_self_movement
  175.   def update_self_movement
  176.     return if $game_map.interpreter.running? && @event.name.include?(Jene::PREEMPTIVE_SURPRISE_AFFIX)
  177.     jene_pseb_update_self_movement
  178.   end
  179. end
  180.  
  181. #==============================================================================
  182. # ** Game_Follower
  183. #------------------------------------------------------------------------------
  184. #  This class handles followers. A follower is an allied character, other than
  185. # the front character, displayed in the party. It is referenced within the
  186. # Game_Followers class.
  187. #==============================================================================
  188.  
  189. class Game_Follower < Game_Character
  190.   #--------------------------------------------------------------------------
  191.   # * Object Initialization
  192.   #--------------------------------------------------------------------------
  193.   alias jene_pseb_follower_initialize initialize
  194.   def initialize(member_index, preceding_character)
  195.     jene_pseb_follower_initialize(member_index, preceding_character)
  196.     #@priority_type = 0
  197.   end
  198. end
  199.  
  200. #==============================================================================
  201. # ** Game_Interpreter
  202. #------------------------------------------------------------------------------
  203. #  An interpreter for executing event commands. This class is used within the
  204. # Game_Map, Game_Troop, and Game_Event classes.
  205. #==============================================================================
  206.  
  207. class Game_Interpreter
  208.   #--------------------------------------------------------------------------
  209.   # * Battle Processing
  210.   #--------------------------------------------------------------------------
  211.   alias jene_pseb_command_301 command_301
  212.   def command_301
  213.     if Jene::PREEMPTIVE_SURPRISE_SWITCH > 0
  214.       psbe_enable = $game_switches[Jene::PREEMPTIVE_SURPRISE_SWITCH]
  215.     elsif Jene::PREEMPTIVE_SURPRISE_SWITCH < 0
  216.       psbe_enable = !$game_switches[-Jene::PREEMPTIVE_SURPRISE_SWITCH]
  217.     else
  218.       psbe_enable = true
  219.     end
  220.     if psbe_enable
  221.       return if $game_party.in_battle
  222.       if @params[0] == 0                      # Direct designation
  223.         troop_id = @params[1]
  224.       elsif @params[0] == 1                   # Designation with variables
  225.         troop_id = $game_variables[@params[1]]
  226.       else                                    # Map-designated troop
  227.         troop_id = $game_player.make_encounter_troop_id
  228.       end
  229.       if $data_troops[troop_id]
  230.         preemptive_or_surprise
  231.         BattleManager.setup(troop_id, @params[2], @params[3])
  232.         BattleManager.event_proc = Proc.new {|n| @branch[@indent] = n }
  233.         BattleManager.on_encounter
  234.         $game_player.make_encounter_count
  235.         SceneManager.call(Scene_Battle)
  236.       end
  237.       Fiber.yield
  238.     else
  239.       jene_pseb_command_301
  240.     end
  241.   end
  242.   #--------------------------------------------------------------------------
  243.   # * Battle Processing
  244.   #--------------------------------------------------------------------------
  245.   def preemptive_or_surprise
  246.     enemy = get_character(0)
  247.     player = get_character(-1)
  248.     return unless enemy.event.name.include?(Jene::PREEMPTIVE_SURPRISE_AFFIX)
  249.     if player.direction == enemy.prev_direction
  250.       case player.direction
  251.       when 2
  252.         if player.y < enemy.y; $game_temp.ps_num = 1; end
  253.       when 4
  254.         if player.x > enemy.x; $game_temp.ps_num = 1; end
  255.       when 6
  256.         if player.x < enemy.x; $game_temp.ps_num = 1; end
  257.       when 8
  258.         if player.y > enemy.y; $game_temp.ps_num = 1; end
  259.       end
  260.     end
  261.     if player.direction == enemy.direction && !$game_party.cancel_surprise?
  262.       case enemy.direction
  263.       when 2
  264.         if enemy.y < player.y; $game_temp.ps_num = -1; end
  265.       when 4
  266.         if enemy.x > player.x; $game_temp.ps_num = -1; end
  267.       when 6
  268.         if enemy.x < player.x; $game_temp.ps_num = -1; end
  269.       when 8
  270.         if enemy.y > player.y; $game_temp.ps_num = -1; end
  271.       end
  272.     end
  273.     if 10 - player.direction != enemy.prev_direction
  274.       if Jene::PREEMPTIVE_SIDE_CHANCE < 0
  275.         chance = rand < $game_party.rate_preemptive($game_troop.agi)
  276.       else
  277.         chance = rand < Jene::PREEMPTIVE_SIDE_CHANCE / 100.to_f
  278.         chance *= 4 if $game_party.raise_preemptive?
  279.       end
  280.       case player.direction
  281.       when 2
  282.         if player.y < enemy.y && chance; $game_temp.ps_num = 1; end
  283.       when 4
  284.         if player.x > enemy.x && chance; $game_temp.ps_num = 1; end
  285.       when 6
  286.         if player.x < enemy.x && chance; $game_temp.ps_num = 1; end
  287.       when 8
  288.         if player.y > enemy.y && chance; $game_temp.ps_num = 1; end
  289.       end
  290.     end
  291.     if 10 - enemy.direction != player.direction && !$game_party.cancel_surprise?
  292.       if Jene::SURPRISE_SIDE_CHANCE < 0
  293.         chance = rand < $game_party.rate_surprise($game_troop.agi)
  294.       else
  295.         chance = rand < Jene::SURPRISE_SIDE_CHANCE / 100.to_f
  296.       end
  297.       case enemy.direction
  298.       when 2
  299.         if player.y > enemy.y && chance; $game_temp.ps_num = -1; end
  300.         return
  301.       when 4
  302.         if player.x < enemy.x && chance; $game_temp.ps_num = -1; end
  303.         return
  304.       when 6
  305.         if player.x > enemy.x && chance; $game_temp.ps_num = -1; end
  306.         return
  307.       when 8
  308.         if player.y < enemy.y && chance; $game_temp.ps_num = -1; end
  309.         return
  310.       end
  311.     end
  312.   end
  313. end
RAW Paste Data