neonblack

Event Chase V1.0c

Jul 23rd, 2012
245
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ###--------------------------------------------------------------------------###
  2. #  Event Chase script                                                          #
  3. #  Version 1.0c                                                                #
  4. #                                                                              #
  5. #      Credits:                                                                #
  6. #  Original code by: Neonblack                                                 #
  7. #  Modified by: Archeia, Yami, Diedrupo                                        #
  8. #                                                                              #
  9. #  This work is licensed under the Creative Commons Attribution-NonCommercial  #
  10. #  3.0 Unported License. To view a copy of this license, visit                 #
  11. #  http://creativecommons.org/licenses/by-nc/3.0/.                             #
  12. #  Permissions beyond the scope of this license are available at               #
  13. #  http://cphouseset.wordpress.com/liscense-and-terms-of-use/.                 #
  14. #                                                                              #
  15. #      Contact:                                                                #
  16. #  NeonBlack - neonblack23@live.com (e-mail) or "neonblack23" on skype         #
  17. ###--------------------------------------------------------------------------###
  18.  
  19. ###--------------------------------------------------------------------------###
  20. #      Revision information:                                                   #
  21. #  V1.0 - 7.23.2012                                                            #
  22. #   Wrote and debugged main script                                             #
  23. ###--------------------------------------------------------------------------###
  24.  
  25. ###--------------------------------------------------------------------------###
  26. #      Compatibility:                                                          #
  27. #  Alias       - Game_Event: setup_page_settings, update_self_movement,        #
  28. #                            update, lock                                      #
  29. #                Game_Map: update                                              #
  30. #                DataManager: load_database                                    #
  31. #  New Methods - Game_Party: average_party_level, cloak_check                  #
  32. #                Game_Map: stealth_field, stealth_herb?                        #
  33. #                Game_Event: get_chase_control, flee_player?,                  #
  34. #                            level_over_flee, variable_over_flee,              #
  35. #                            check_line_of_sight, cp_passable?, sighted?,      #
  36. #                            do_spotted, stop_spotted, exit_region?,           #
  37. #                            check_region_spot, check_both_ev_dir              #
  38. #                Game_Interpreter: whistle, stop_chase, cloaker                #
  39. #                RPG::EquipItem: check_cloak                                   #
  40. #                DataManager: make_item_cloaks                                 #
  41. ###--------------------------------------------------------------------------###
  42.  
  43. ###--------------------------------------------------------------------------###
  44. #      Instructions:                                                           #
  45. #  Place this script in the "Materials" section of the scripts above main.     #
  46. #  This script works by adding tags to comment boxes in pages of events.       #
  47. #  Only the tags on the current page will be used.  The following tags may be  #
  48. #  used:                                                                       #
  49. #                                                                              #
  50. #   sight<x> - This is required to use any of the other tags or to cause the   #
  51. #              event to chase the player at all.  Replace "x" with the events  #
  52. #              range of sight.  The event uses line of sight and will not be   #
  53. #              able to see through objects.  Note that the event will chase    #
  54. #              followers as well as the player.                                #
  55. #   balloon<x> - The balloon to pop up above the head of the event when it     #
  56. #                starts to chase the player.  Replace "x" with the number of   #
  57. #                the balloon to use.                                           #
  58. #   chase<x> - The number of frames the event chases the player before         #
  59. #              returning to it's default state.  Replace "x" with a number.    #
  60. #              Remember that 60 frames = 1 second.                             #
  61. #   speed<x> - The speed the event chases the player at.  Replace "x" with a   #
  62. #              number.                                                         #
  63. #   switch<x> - While chasing the player, this switch is turned on.  When the  #
  64. #               event stops chasing the player, this switch is turned off.     #
  65. #               Replace "x" with the switch number.                            #
  66. #   region<x> - The player must be in a certain region for the event to chase  #
  67. #               it.  Replace "x" with a region ID.                             #
  68. #   flee level<x> - The average party level at which the event starts to flee  #
  69. #                   rather than chase the player.  Replace "x" with the        #
  70. #                   average level.  When the party's average level is higher,  #
  71. #                   the event will flee.                                       #
  72. #   flee variable<x:y> - Set a variable to cause the event to flee.  Set "x"   #
  73. #                        to a variable number and "y" to a value.  While the   #
  74. #                        variable is greater than the value, the event will    #
  75. #                        flee from the player.                                 #
  76. #   flee switch<x> - This switch is turned on while the event is fleeing the   #
  77. #                    player.  Replace "x" with the switch number.              #
  78. #   refresh chase - Normally the timer for chasing will count down while the   #
  79. #                   event is chasing the player and will not reset until it    #
  80. #                   has reached 0.  With this, the timer will reset as long    #
  81. #                   as the event keeps the player in it's line of sight.       #
  82. #                                                                              #
  83. ###-----                                                                -----###
  84. #      Additional Uses:                                                        #
  85. #  In addition to the event tags, there are two other options you can do with  #
  86. #  this script.  You can create a cloaking item that will prevent events from  #
  87. #  chasing you while it is equipped and you can whistle to attract all events  #
  88. #  that can chase on the map.                                                  #
  89. #                                                                              #
  90. #   [cloak] - Place this tag exactly as it is (with the brackets) in the       #
  91. #             notebox of an item and while the item is equipped events will    #
  92. #             not chase you when you enter their line of sight.                #
  93. #                                                                              #
  94. #   whistle - Use this script call in an event to cause all events on the      #
  95. #             current map to begin chasing you.  You can create a whistle      #
  96. #             skill or item by placing a script call with this in a common     #
  97. #             event and using the event outside of battle.                     #
  98. #   stop_chase - The exact opposite of whistle.  This causes all events to     #
  99. #                stop chasing the player.                                      #
  100. #   cloaker(x) - Use this script call in an event to cause the player to       #
  101. #                become invisible to events for a certain number of frames.    #
  102. #                Replace "x" with the number of frames to be cloaked for.      #
  103. #                Remember that 60 frames = 1 second.                           #
  104. ###--------------------------------------------------------------------------###
  105.  
  106. module CP    # Do not edit
  107. module CHASE # these lines.
  108.  
  109. ###--------------------------------------------------------------------------###
  110. #      Config:                                                                 #
  111. # This is the variable that stores information for the direction of the event  #
  112. # and the player when the event is activated.  Normally it will be set to 0,   #
  113. # however, if the player is facing the back of the event, it will be set to 1  #
  114. # and if the event is facing the back of the player, it will be set to -1.     #
  115. # Also, if the player approaches an event from the side a value of 2 is set    #
  116. # while if an event approaches the player from the side a value of -2 is set.  #
  117. VARIABLE = 31 # Default = 31                                                   #
  118. ###--------------------------------------------------------------------------###
  119.  
  120. ###--------------------------------------------------------------------------###
  121. #  The following lines are the actual core code of the script.  While you are  #
  122. #  certainly invited to look, modifying it may result in undesirable results.  #
  123. #  Modify at your own risk!                                                    #
  124. ###--------------------------------------------------------------------------###
  125.  
  126.  
  127. end
  128. end
  129.  
  130.  
  131. $imported = {} if $imported == nil
  132. $imported["CP_CHASE"] = 1.0
  133.  
  134. module CP
  135. module CHASE  ## Sets up the REGEXPs.
  136.  
  137. BUBBLE = /BALLOON\<(\d+)\>/i
  138. SWITCH = /SWITCH\<(\d+)\>/i
  139. SIGHT = /SIGHT\<(\d+)\>/i
  140. CHASE = /CHASE\<(\d+)\>/i
  141. FLEE = /FLEE[ ]LEVEL\<(\d+)\>/i
  142. FLEESW = /FLEE[ ]SWITCH\<(\d+)\>/i
  143. FLEEVA = /FLEE[ ]VARIABLE\<(\d+):(\d+)\>/i
  144. REFRESH = /REFRESH[ ]CHASE/i
  145. SPEED = /SPEED\<(\d+)\>/i
  146. REGION = /REGION\<(\d+)\>/i
  147. SFX = /SFX\<([\w\d\s]+):(\d+):(\d+)\>/i
  148. CLOAK = /\[CLOAK\]/i
  149.  
  150. end
  151. end
  152.  
  153. class Game_Party < Game_Unit
  154.   def average_party_level  ## Checks the party's average level.
  155.     return 0 if members.size == 0
  156.     lvls = 0
  157.     members.each do |actor|
  158.       lvls += actor.level
  159.     end
  160.     lvls = lvls.to_f / members.size.to_f
  161.     return lvls
  162.   end
  163.  
  164.   def cloak_check  ## Checks all items for the cloak state.
  165.     return true if members.size == 0
  166.     cloaked = false
  167.     members.each do |actor|
  168.       actor.equips.each do |equ|
  169.         next if equ.nil?
  170.         cloaked = true if equ.cloak
  171.       end
  172.     end
  173.     return cloaked
  174.   end
  175. end
  176.  
  177. class Game_Map
  178.   alias cp_stealth_update update unless $@
  179.   def update(*args)
  180.     cp_stealth_update(*args)
  181.     return if @stealth_herb.nil?
  182.     @stealth_herb -= 1
  183.   end
  184.  
  185.   def stealth_field(val = 300)
  186.     @stealth_herb = val
  187.   end
  188.  
  189.   def stealth_herb?
  190.     return false if @stealth_herb.nil?
  191.     return true if @stealth_herb > 0
  192.     return false
  193.   end
  194. end
  195.  
  196. class Game_Event < Game_Character
  197.   attr_reader :sight
  198.  
  199.   alias cp_chase_setup_page setup_page_settings unless $@
  200.   def setup_page_settings
  201.     stop_spotted
  202.     cp_chase_setup_page
  203.     get_chase_control  ## Sets up event chasing.
  204.   end
  205.  
  206.   def get_chase_control  ## Sets up all the event chasing variables.
  207.     @bubble = nil; @switch = nil; @chase = nil; @flee = nil; @cspeed = nil
  208.     @flee_switch = nil; @flee_variable = nil; @sfx = nil; @region_check = nil
  209.     @refresh_chase = false
  210.     @sight = 0 if @sight.nil?
  211.     return if (@list.nil? || @list.empty?)
  212.     @list.each do |line|  ## Looks for comments with the lines.
  213.       next unless (line.code == 108 || line.code == 408)
  214.       case line.parameters[0]
  215.       when CP::CHASE::BUBBLE
  216.         @bubble = $1.to_i
  217.       when CP::CHASE::SWITCH
  218.         @switch = $1.to_i
  219.       when CP::CHASE::SIGHT
  220.         @sight = $1.to_i
  221.       when CP::CHASE::CHASE
  222.         @chase = $1.to_i
  223.       when CP::CHASE::FLEE
  224.         @flee = $1.to_i
  225.       when CP::CHASE::FLEESW
  226.         @flee_switch = $1.to_i
  227.       when CP::CHASE::FLEEVA
  228.         @flee_variable = []
  229.         @flee_variable[0] = $1.to_i
  230.         @flee_variable[1] = $2.to_i
  231.       when CP::CHASE::REFRESH
  232.         @refresh_chase = true
  233.       when CP::CHASE::REGION
  234.         @region_check = $1.to_i
  235.       when CP::CHASE::SPEED
  236.         @cspeed = $1.to_i
  237.       end
  238.     end
  239.   end
  240.  
  241.   alias cp_chase_self_movement update_self_movement unless $@
  242.   def update_self_movement  ## Changes movement if the player has been spotted.
  243.     @spotted = false if @spotted.nil?
  244.     @spotted = false if @erased # Yami
  245.     if @spotted
  246.       if flee_player?
  247.         move_away_from_player
  248.       else
  249.         move_toward_player
  250.       end
  251.     else
  252.       cp_chase_self_movement
  253.     end
  254.   end
  255.  
  256.   def flee_player?  ## Checks if the event is meant to flee the player.
  257.     return true if level_over_flee
  258.     return true if variable_over_flee
  259.     return false
  260.   end
  261.  
  262.   def level_over_flee  ## Checks for the flee level.
  263.     return false if @flee.nil?
  264.     return true if $game_party.average_party_level > @flee
  265.     return false
  266.   end
  267.  
  268.   def variable_over_flee  ## Checks for the flee variable.
  269.     return false if @flee_variable.nil?
  270.     return false if @flee_variable.empty?
  271.     return true if $game_variables[@flee_variables[0]] > @flee_variables[1]
  272.     return false
  273.   end
  274.  
  275.   alias cp_chase_update update unless $@
  276.   def update  ## Updates the chase timer.
  277.     cp_chase_update
  278.     return stop_spotted if @erased # Yami
  279.     @chtimer = 0 if @chtimer.nil?
  280.     @chtimer -= 1 if (@chtimer > 0 && !@chase.nil?)
  281.     stop_spotted if (@chtimer <= 0 && !@chase.nil?)
  282.     check_line_of_sight  ## Also checks line of sight.
  283.     exit_region?
  284.   end
  285.  
  286.   def exit_region?
  287.     @spotted = false if @spotted.nil?
  288.     return unless @spotted
  289.     stop_spotted if !check_region_spot
  290.   end
  291.  
  292.   def check_line_of_sight  ## Checks the event's line of sight.
  293.     return if (@sight.nil? || @sight == 0)
  294.     for i in 0...@sight  ## Number of blocks to check.
  295.       xc = @x
  296.       yc = @y
  297.       case @direction  ## Alters the checked block based on direction.
  298.       when 2; yc += i
  299.       when 4; xc -= i
  300.       when 6; xc += i
  301.       when 8; yc -= i
  302.       end
  303.       break unless cp_passable?(xc, yc, @direction)  ## Checks for obstacles.
  304.       do_spotted if sighted?(xc, yc, @direction)  ## Finds the player.
  305.     end
  306.   end
  307.  
  308.   def cp_passable?(x, y, d)  ## Obstacle check.  Events do not count.
  309.     x2 = $game_map.round_x_with_direction(x, d)
  310.     y2 = $game_map.round_y_with_direction(y, d)
  311.     return false unless $game_map.valid?(x2, y2)
  312.     return false unless map_passable?(x, y, d)
  313.     return false unless map_passable?(x2, y2, reverse_dir(d))
  314.     return true
  315.   end
  316.  
  317.   def sighted?(x, y, d)  ## Checks for the player.
  318.     x2 = $game_map.round_x_with_direction(x, d)
  319.     y2 = $game_map.round_y_with_direction(y, d)
  320.     return check_region_spot if collide_with_characters?(x2, y2)
  321.     return false
  322.   end
  323.  
  324.   def check_region_spot
  325.     return true if @region_check.nil?
  326.     return true if $game_player.region_id == @region_check
  327.     return false
  328.   end
  329.  
  330.   def do_spotted(dummy = true, ovrd = false)
  331.     unless ovrd  ## Override for whistle.
  332.       return if (@spotted && !@refresh_chase)
  333.       return if $game_party.cloak_check  ## Return if cloaked.
  334.       return if $game_map.stealth_herb?
  335.     end
  336.     @balloon_id = @bubble unless (@bubble.nil? || @spotted)  ## Make balloon.
  337.     @spotted = true  ## Set spotted.
  338.     @old_speed = @move_speed if @old_speed.nil?  ## Alter the speed.
  339.     @old_freq = @move_frequency if @old_freq.nil?  ## Alter the frequency.
  340.     @move_speed = @cspeed unless @cspeed.nil?
  341.     @move_frequency = 5
  342.     @chtimer = 1
  343.     @chtimer = @chase unless @chase.nil?  ## Set the timer.
  344.     $game_switches[@switch] = true unless @switch.nil?  ## Set switches.
  345.     $game_switches[@flee_switch] = true unless @flee_switch.nil?
  346.   end
  347.  
  348.   def stop_spotted
  349.     @spotted = false  ## Turn off the spotted.
  350.     @move_speed = @old_speed unless @old_speed.nil?  ## Reset speed.
  351.     @move_frequency = @old_freq unless @old_freq.nil?  ## Reset frequency.
  352.     @old_speed = nil
  353.     @old_freq = nil
  354.     @chtimer = 0  ## Reset timer.
  355.     $game_switches[@switch] = false unless @switch.nil?  ## Reset switches.
  356.     $game_switches[@flee_switch] = false unless @flee_switch.nil?
  357.   end
  358.  
  359.   alias cp_chase_locki lock unless $@
  360.   def lock
  361.     check_both_ev_dir
  362.     cp_chase_locki
  363.   end
  364.  
  365.   def check_both_ev_dir
  366.     sx = distance_x_from($game_player.x)
  367.     sy = distance_y_from($game_player.y)
  368.     if sx.abs > sy.abs
  369.       res = sx > 0 ? 4 : 6
  370.       ops = sx > 0 ? 6 : 4
  371.     elsif sy != 0
  372.       res = sy > 0 ? 8 : 2
  373.       ops = sy > 0 ? 2 : 8
  374.     else
  375.       res = 0; ops = 0
  376.     end
  377.     var = CP::CHASE::VARIABLE
  378.     $game_variables[var] = 0
  379.     $game_variables[var] = -1 if (@direction == res &&
  380.                                   $game_player.direction == res)
  381.     $game_variables[var] = -2 if (@direction == res &&
  382.                                   $game_player.direction != res &&
  383.                                   $game_player.direction != ops)
  384.     $game_variables[var] = 1 if (@direction == ops &&
  385.                                  $game_player.direction == ops)
  386.     $game_variables[var] = 2 if (@direction != ops &&
  387.                                  @direction != res &&
  388.                                  $game_player.direction == ops)
  389.   end
  390. end
  391.  
  392. class Game_Interpreter
  393.   def whistle  ## Makes all valid events chase.
  394.     $game_map.events.each do |i, evnt|
  395.       next unless evnt.is_a?(Game_Event)
  396.       next if evnt.sight.nil?
  397.       next if evnt.sight == 0
  398.       evnt.do_spotted(false, true)
  399.     end
  400.   end
  401.  
  402.   def stop_chase
  403.     $game_map.events.each do |i, evnt|
  404.       next unless evnt.is_a?(Game_Event)
  405.       next if evnt.sight.nil?
  406.       next if evnt.sight == 0
  407.       evnt.stop_spotted
  408.     end
  409.   end
  410.  
  411.   def cloaker(val = 300)
  412.     $game_map.stealth_field(val)
  413.   end
  414. end
  415.  
  416. class RPG::EquipItem < RPG::BaseItem
  417.   attr_reader :cloak
  418.  
  419.   def check_cloak  ## Checks if an item is a cloaking device.
  420.     return if @cloaker; @cloaker = true
  421.     @cloak = false
  422.     self.note.split(/[\r\n]+/).each do |line|
  423.       case line
  424.       when CP::CHASE::CLOAK
  425.         @cloak = true
  426.       end
  427.     end
  428.   end
  429. end
  430.  
  431. module DataManager
  432.   class << self
  433.    
  434.   alias cp_chase_load_database load_database unless $@
  435.   def load_database
  436.     cp_chase_load_database
  437.     make_item_cloaks
  438.   end
  439.  
  440.   def make_item_cloaks
  441.     groups = [$data_weapons, $data_armors]
  442.     for group in groups
  443.       for obj in group
  444.         next if obj == nil
  445.         obj.check_cloak if obj.is_a?(RPG::EquipItem)
  446.       end
  447.     end
  448.   end
  449.  
  450.   end
  451. end
  452.  
  453.  
  454. ###--------------------------------------------------------------------------###
  455. #  End of script.                                                              #
  456. ###--------------------------------------------------------------------------###
RAW Paste Data