Archeia

Event Chase v2.0

May 22nd, 2014
572
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##-----------------------------------------------------------------------------
  2. #  Event Chase v2.0
  3. #  Created by Neon Black
  4. #  v2.0 - 2.11.2014 - Script updated for public release
  5. #  v1.0 - 7.23.2012 - Main script completed
  6. #  For both commercial and non-commercial use as long as credit is given to
  7. #  Neon Black and any additional authors.  Licensed under Creative Commons
  8. #  CC BY 4.0 - http://creativecommons.org/licenses/by/4.0/
  9. ##-----------------------------------------------------------------------------
  10.  
  11. ##-----------------------------------------------------------------------------
  12. #      Instructions:
  13. #  Place this script in the "Materials" section of the scripts above main.
  14. #  This script works by adding tags to comment boxes in pages of events.  Only
  15. #  the tags on the current page will be used.  The following tags may be used:
  16. #
  17. #  sight<5>
  18. #   - This tag is required to use any of the other tags because without it the
  19. #     event will not chase the player at all.  Select a range for the line of
  20. #     sight.  The event will the "see" the player as  long as the player is
  21. #     no more than that many tiles away from the event.  It is worth noting
  22. #     that events cannot see through objects that are not passable.
  23. #  balloon<1>
  24. #   - This balloon will pop up above the event's head when the player is
  25. #     spotted.  0 defines the first balloon in the sheet.
  26. #  chase<300>
  27. #   - The number of frames the event will chase the player before returning to
  28. #     an inactive state.  Remember that 60 frames = 1 second by default.
  29. #  speed<3>
  30. #   - The speed at which the event will chase the player.  This will only take
  31. #     effect while the event is giving chase and will not directly override the
  32. #     default speed.
  33. #  switch<40>
  34. #   - Activates a switch while the event is chasing the player.  When the event
  35. #     returns to an inactive state the switch is turned off.
  36. #  region<10>
  37. #   - The region the player must be in for the event to give chase.  This tag
  38. #     may appear more than once.
  39. #  flee level<5>
  40. #   - The average party level at which the event will flee from the player
  41. #     rather than chasing them.  When the party's average level is greater than
  42. #     this value, the event will flee.
  43. #  flee variable<20:5>
  44. #   - Causes the event to flee from the player while a variable is greater than
  45. #     a specific value.  The first number is the variable to check while the
  46. #     second number is the required value.
  47. #  flee switch<6>
  48. #   - This switch activates while the event is currently fleeing from the
  49. #     player.
  50. #  refresh chase
  51. #   - This tag is a little different.  By default, an event's timer will count
  52. #     down even while the event is chasing the player.  While this tag is
  53. #     present, the event will continue to check if the player is in the line
  54. #     of sight even while chasing the player, causing the timer to reset.
  55. #
  56. ##------
  57. #      Additional Uses:
  58. #  In addition to the event tags, there are two other options you can do with
  59. #  this script.  You can create a cloaking item that will prevent events from
  60. #  chasing you while it is equipped and you can whistle to attract all events
  61. #  that can chase on the map.
  62. #
  63. #  [cloak]
  64. #   - When this tag is in the notebox of an item, the item will cause the
  65. #     player to become invisible to events while the item is equipped to any
  66. #     party member.
  67. #  whistle
  68. #   - This command is a SCRIPT CALL.  Use this from an event to cause all
  69. #     events on the map to chase the player.
  70. #  stop_chase
  71. #   - This command is a SCRIPT CALL.  This is the opposite of "whistle" in
  72. #     that it causes all events on the map to stop chasing the player.
  73. #  cloaker(600)
  74. #   - This command is a SCRIPT CALL.  This command causes the player to become
  75. #     invisible to events with a line of sight for a certain number of frames.
  76. #     Remember that 60 frames = 1 second by default.
  77. ###--------------------------------------------------------------------------###
  78.  
  79. module CP    # Do not edit
  80. module CHASE # these lines.
  81.  
  82. ##-----------------------------------------------------------------------------
  83. #      Config:
  84. #  This is the variable that stores information for the direction of the event
  85. #  and the player when the event is activated.  Normally it will be set to 0,
  86. #  however, if the player is facing the back of the event, it will be set to 1
  87. #  and if the event is facing the back of the player, it will be set to -1.
  88. #  Also, if the player approaches an event from the side a value of 2 is set
  89. #  while if an event approaches the player from the side a value of -2 is set.
  90. VARIABLE = 2995
  91. ##-----------------------------------------------------------------------------
  92.  
  93. ##-----------------------------------------------------------------------------
  94. #  The following lines are the actual core code of the script.  While you are
  95. #  certainly invited to look, modifying it may result in undesirable results.
  96. #  Modify at your own risk!
  97. ##-----------------------------------------------------------------------------
  98.  
  99.  
  100. end
  101. end
  102.  
  103.  
  104. $imported = {} if $imported == nil
  105. $imported["CP_CHASE"] = 2.0
  106.  
  107. module CP
  108. module CHASE  ## Sets up the REGEXPs.
  109.  
  110. BUBBLE = /BALLOON\<(\d+)\>/i
  111. SWITCH = /SWITCH\<(\d+)\>/i
  112. SIGHT = /SIGHT\<(\d+)\>/i
  113. CHASE = /CHASE\<(\d+)\>/i
  114. FLEE = /FLEE[ ]LEVEL\<(\d+)\>/i
  115. FLEESW = /FLEE[ ]SWITCH\<(\d+)\>/i
  116. FLEEVA = /FLEE[ ]VARIABLE\<(\d+):(\d+)\>/i
  117. REFRESH = /REFRESH[ ]CHASE/i
  118. SPEED = /SPEED\<(\d+)\>/i
  119. REGION = /REGION\<(\d+)\>/i
  120. SFX = /SFX\<(.+):(\d+):(\d+)\>/i
  121. CLOAK = /\[CLOAK\]/i
  122.  
  123. end
  124. end
  125.  
  126. class Game_Party < Game_Unit
  127.   def average_party_level  ## Checks the party's average level.
  128.     return 0 if members.empty?
  129.     value = members.inject(0) { |r, m| r += m.level }
  130.     (value.to_f / members.size).ceil
  131.   end
  132.  
  133.   def cloak_check  ## Checks all items for the cloak state.
  134.     return true if members.empty?
  135.     members.each do |actor|
  136.       actor.equips.each do |equ|
  137.         next if equ.nil? || !equ.cloak
  138.         return true
  139.       end
  140.     end
  141.     return false
  142.   end
  143. end
  144.  
  145. class Game_Map
  146.   alias cp_stealth_update update
  147.   def update(*args)
  148.     cp_stealth_update(*args)
  149.     @stealth_herb -= 1 if @stealth_herb
  150.   end
  151.  
  152.   def events_start_chase  ## Makes all valid events chase.
  153.     @events.each do |i, evnt|
  154.       next unless evnt.is_a?(Game_Event)
  155.       next if evnt.sight.nil? || evnt.sight <= 0
  156.       evnt.do_spotted(false, true)
  157.     end
  158.   end
  159.  
  160.   def events_stop_chase
  161.     @events.each do |i, evnt|
  162.       next unless evnt.is_a?(Game_Event)
  163.       next if evnt.sight.nil? || evnt.sight <= 0
  164.       evnt.stop_spotted
  165.     end
  166.   end
  167.  
  168.   def stealth_field(val = 300)
  169.     @stealth_herb = val
  170.   end
  171.  
  172.   def stealth_herb?
  173.     @stealth_herb = 0 unless @stealth_herb
  174.     return @stealth_herb > 0
  175.   end
  176. end
  177.  
  178. class Game_Event < Game_Character
  179.   attr_reader :sight
  180.  
  181.   alias cp_chase_setup_page setup_page_settings
  182.   def setup_page_settings
  183.     stop_spotted
  184.     cp_chase_setup_page
  185.     get_chase_control  ## Sets up event chasing.
  186.   end
  187.  
  188.   def get_chase_control  ## Sets up all the event chasing variables.
  189.     @bubble = nil; @switch = nil; @chase = nil; @flee = 999999; @cspeed = nil
  190.     @flee_switch = nil; @flee_variable = nil; @csfx = nil; @region_check = []
  191.     @refresh_chase = false
  192.     @sight = 0 if @sight.nil?
  193.     return if @list.nil? || @list.empty?
  194.     @list.each do |line|  ## Looks for comments with the lines.
  195.       next unless (line.code == 108 || line.code == 408)
  196.       case line.parameters[0]
  197.       when CP::CHASE::BUBBLE
  198.         @bubble = $1.to_i
  199.       when CP::CHASE::SWITCH
  200.         @switch = $1.to_i
  201.       when CP::CHASE::SIGHT
  202.         @sight = $1.to_i
  203.       when CP::CHASE::CHASE
  204.         @chase = $1.to_i
  205.       when CP::CHASE::FLEE
  206.         @flee = $1.to_i
  207.       when CP::CHASE::FLEESW
  208.         @flee_switch = $1.to_i
  209.       when CP::CHASE::FLEEVA
  210.         @flee_variable = []
  211.         @flee_variable[0] = $1.to_i
  212.         @flee_variable[1] = $2.to_i
  213.       when CP::CHASE::REFRESH
  214.         @refresh_chase = true
  215.       when CP::CHASE::REGION
  216.         @region_check.push($1.to_i)
  217.       when CP::CHASE::SPEED
  218.         @cspeed = $1.to_i
  219.       when CP::CHASE::SFX
  220.         @csfx = RPG::SE.new($1.to_s, $2.to_i, $3.to_i)
  221.       end
  222.     end
  223.   end
  224.  
  225.   alias cp_chase_self_movement update_self_movement
  226.   def update_self_movement  ## Changes movement if the player has been spotted.
  227.     @spotted = false if @spotted.nil? || @erased
  228.     if @spotted
  229.       flee_player? ? move_away_from_player : move_toward_player
  230.     else
  231.       cp_chase_self_movement
  232.     end
  233.   end
  234.  
  235.   def flee_player?  ## Checks if the event is meant to flee the player.
  236.     return level_over_flee || variable_over_flee
  237.   end
  238.  
  239.   def level_over_flee  ## Checks for the flee level.
  240.     return $game_party.average_party_level > @flee
  241.   end
  242.  
  243.   def variable_over_flee  ## Checks for the flee variable.
  244.     return false if @flee_variable.nil? || @flee_variable.empty?
  245.     return $game_variables[@flee_variables[0]] > @flee_variables[1]
  246.   end
  247.  
  248.   alias cp_chase_update update
  249.   def update  ## Updates the chase timer.
  250.     cp_chase_update
  251.     return stop_spotted if @erased
  252.     @chtimer = 0 if @chtimer.nil?
  253.     @chtimer -= 1 if @chtimer > 0 && !@chase.nil?
  254.     stop_spotted if @chtimer <= 0 && !@chase.nil?
  255.     check_line_of_sight  ## Also checks line of sight.
  256.     exit_region?
  257.   end
  258.  
  259.   def exit_region?
  260.     @spotted = false if @spotted.nil?
  261.     return unless @spotted
  262.     stop_spotted if !check_region_spot
  263.   end
  264.  
  265.   def check_line_of_sight  ## Checks the event's line of sight.
  266.     return if @sight.nil? || @sight <= 0
  267.     @sight.times do |i|  ## Number of blocks to check.
  268.       xc = @x
  269.       yc = @y
  270.       case @direction  ## Alters the checked block based on direction.
  271.       when 2; yc += i
  272.       when 4; xc -= i
  273.       when 6; xc += i
  274.       when 8; yc -= i
  275.       end
  276.       break unless cp_passable?(xc, yc, @direction)  ## Checks for obstacles.
  277.       do_spotted if sighted?(xc, yc, @direction)  ## Finds the player.
  278.     end
  279.   end
  280.  
  281.   def cp_passable?(x, y, d)  ## Obstacle check.  Events do not count.
  282.     x2 = $game_map.round_x_with_direction(x, d)
  283.     y2 = $game_map.round_y_with_direction(y, d)
  284.     return false unless $game_map.valid?(x2, y2)
  285.     return false unless map_passable?(x, y, d)
  286.     return false unless map_passable?(x2, y2, reverse_dir(d))
  287.     return true
  288.   end
  289.  
  290.   def sighted?(x, y, d)  ## Checks for the player.
  291.     x2 = $game_map.round_x_with_direction(x, d)
  292.     y2 = $game_map.round_y_with_direction(y, d)
  293.     return check_region_spot if collide_with_characters?(x2, y2)
  294.     return false
  295.   end
  296.  
  297.   def check_region_spot
  298.     return true if @region_check.empty?
  299.     return @region_check.include?($game_player.region_id)
  300.   end
  301.  
  302.   def do_spotted(dummy = true, ovrd = false)
  303.     unless ovrd  ## Override for whistle.
  304.       return if @spotted && !@refresh_chase
  305.       return if $game_party.cloak_check || $game_map.stealth_herb?
  306.     end
  307.     @balloon_id = @bubble unless @bubble.nil? || @spotted  ## Make balloon.
  308.     @spotted = true  ## Set spotted.
  309.     @old_speed = @move_speed if @old_speed.nil?  ## Alter the speed.
  310.     @old_freq = @move_frequency if @old_freq.nil?  ## Alter the frequency.
  311.     @move_speed = @cspeed if @cspeed
  312.     @move_frequency = 5
  313.     @chtimer = 1
  314.     @chtimer = @chase if @chase  ## Set the timer.
  315.     @csfx.play if @csfx
  316.     $game_switches[@switch] = true unless @switch.nil?  ## Set switches.
  317.     $game_switches[@flee_switch] = true unless @flee_switch.nil?
  318.   end
  319.  
  320.   def stop_spotted
  321.     @spotted = false  ## Turn off the spotted.
  322.     @move_speed = @old_speed unless @old_speed.nil?  ## Reset speed.
  323.     @move_frequency = @old_freq unless @old_freq.nil?  ## Reset frequency.
  324.     @old_speed = nil
  325.     @old_freq = nil
  326.     @chtimer = 0  ## Reset timer.
  327.     $game_switches[@switch] = false unless @switch.nil?  ## Reset switches.
  328.     $game_switches[@flee_switch] = false unless @flee_switch.nil?
  329.   end
  330.  
  331.   alias cp_chase_locki lock
  332.   def lock
  333.     check_both_ev_dir
  334.     cp_chase_locki
  335.   end
  336.  
  337.   def check_both_ev_dir
  338.     sx = distance_x_from($game_player.x)
  339.     sy = distance_y_from($game_player.y)
  340.     if sx.abs > sy.abs
  341.       res = sx > 0 ? 4 : 6
  342.       ops = sx > 0 ? 6 : 4
  343.     elsif sy != 0
  344.       res = sy > 0 ? 8 : 2
  345.       ops = sy > 0 ? 2 : 8
  346.     else
  347.       res = 0; ops = 0
  348.     end
  349.     var = CP::CHASE::VARIABLE
  350.     $game_variables[var] = 0
  351.     $game_variables[var] = -1 if @direction == res &&
  352.                                  $game_player.direction == res
  353.     $game_variables[var] = -2 if @direction == res &&
  354.                                  $game_player.direction != res &&
  355.                                  $game_player.direction != ops
  356.     $game_variables[var] = 1 if @direction == ops &&
  357.                                 $game_player.direction == ops
  358.     $game_variables[var] = 2 if @direction != ops &&
  359.                                 @direction != res &&
  360.                                 $game_player.direction == ops
  361.   end
  362. end
  363.  
  364. class Game_Interpreter
  365.   def whistle  ## Makes all valid events chase.
  366.     $game_map.events_start_chase
  367.   end
  368.  
  369.   def stop_chase
  370.     $game_map.events_stop_chase
  371.   end
  372.  
  373.   def cloaker(val = 300)
  374.     $game_map.stealth_field(val)
  375.   end
  376. end
  377.  
  378. class RPG::EquipItem < RPG::BaseItem
  379.   def cloak
  380.     check_cloak if @cloak.nil?
  381.     return @cloak
  382.   end
  383.  
  384.   def check_cloak  ## Checks if an item is a cloaking device.
  385.     @cloak = false
  386.     self.note.split(/[\r\n]+/).each do |line|
  387.       case line
  388.       when CP::CHASE::CLOAK
  389.         @cloak = true
  390.       end
  391.     end
  392.   end
  393. end
  394.  
  395.  
  396. ##-----------------------------------------------------------------------------
  397. #  End of script.
  398. ##-----------------------------------------------------------------------------
RAW Paste Data