Zetu

Aggression System

May 24th, 2011
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 9.95 KB | None | 0 0
  1.                             #======================#
  2.                             #  Z-Systems by: Zetu  #
  3. #===========================#======================#===========================#
  4. #                     *  *  *  Aggro System v1.01  *  *  *                     #
  5. #=#==========================================================================#=#
  6.   # OVERWRITES                                                               #
  7.   #   Game_Enemy::make_action                                                #
  8.   #   Game_Interpretor::command_339                                          #
  9.   # SEMI-OVERWRITES                                                          #
  10.   #   Game_BattleAction::decide_random_target                                #
  11.   #--------------------------------------------------------------------------#
  12.   # Introduction:  What is Aggro?                                            #
  13.   #   Aggro (or Aggression) is a statistical value that helps determine the  #
  14.   #   percent chance an enemy will attack a specific actor.                  #
  15.   # What is this good for?                                                   #
  16.   #   In many RPGs, there are strong characters that can take a hit, also    #
  17.   #   called "Tanks", and weaker ones that cannot take much damage.  By      #
  18.   #   manipulating these statistical values, you can control which actors    #
  19.   #   will take damage, and which ones will not.                             #
  20.   # How to increase Aggro?                                                   #
  21.   #   In this system, Aggro can be gained/lost by Attacks, Skills, and by    #
  22.   #   guarding.  The default values are defined by the module below.  Inside #
  23.   #   note tags of skills, you may add tags to change its default amount.    #
  24.   #                                                                          #
  25.   # <aggro: X>                                                               #
  26.   #   Increases aggro by a rating of X.  Keep this value between             #
  27.   #   0.0 and 1 OR 0 and 100.  Does not perfectly increase aggro by the      #
  28.   #   amount given.                                                          #
  29.   # <pro aggro: X>                                                           #
  30.   #   This sets aggro to an amount, reducing all other aggro to match.  Only #
  31.   #   use in hard aggro skills, such as a provoke (gain all aggro) or fade   #
  32.   #   (remove all aggro).  This tag is still in testing and not guarenteed   #
  33.   #   to be bug free.                                                        #
  34.   #--------------------------------------------------------------------------#
  35.   # Version History:                                                         #
  36.   #  v1.01 ALPHA (Please Report all bugs related to this script)             #
  37.   #--------------------------------------------------------------------------#
  38.   #           * * *  FOR SCRIPTERS WANTING TO CREATE A HUD!  * * *           #
  39.   # The aggro values are stored in $scene.aggro and is a two dimensional     #
  40.   # array.  @aggro[enemy.index][actor.index] returns the percent value of    #
  41.   # aggro, and will be between 0.0 and 1.0.                                  #
  42.   #==========================================================================#
  43.  
  44. module Z_Systems
  45.   module Aggro
  46.     NORMAL_ATTACK   =  0.06
  47.     SKILL_DEFAULT   =  0.06
  48.     GUARD           = -0.06
  49.   end
  50. end
  51. #========#======================#====#================================#========#
  52. #--------#                      #----# DO NOT EDIT PAST THIS POINT!!! #--------#
  53. #--------# End of Customization #----# Editing will cause death by    #--------#
  54. #--------#                      #----# brain asplosions.              #--------#
  55. #========#======================#====#================================#========#
  56.  
  57. class Array; def sum; inject( nil ) { |sum,x| sum ? sum+x : x }; end; end
  58.  
  59. class Scene_Battle < Scene_Base
  60.  
  61.   alias zs_aggro_start start
  62.   def start
  63.     zs_aggro_start
  64.     @aggro = []
  65.     for e in 0...$game_troop.members.size
  66.         array = []
  67.         for actor in $game_party.members
  68.           array.push(1.0/$game_party.members.size)
  69.         end
  70.         @aggro[e] = array
  71.       end
  72.   end
  73.  
  74.   def aggro
  75.     return @aggro
  76.   end
  77.  
  78.   def normalize_aggro
  79.     for e in 0...$game_troop.members.size
  80.         value = @aggro[e].sum
  81.         for a in 0...$game_party.members.size
  82.           @aggro[e][a] /= value
  83.         end
  84.       end
  85.   end
  86.  
  87.   def inc_aggro(e, a, value)
  88.     return if $game_party.members.size <= 1
  89.     @aggro[e][a] += value
  90.       @aggro[e][a] = [[@aggro[e][a], 0.0].max, 1.0].min
  91.     return if @aggro[e][a] == 1.0
  92.     div = (@aggro[e].sum - @aggro[e][a])/(1.0-@aggro[e][a])
  93.     for aindex in 0...$game_party.members.size
  94.       unless a == aindex
  95.         @aggro[e][aindex] /= div
  96.       end
  97.     end
  98.   end
  99.  
  100.   def set_aggro(e, a, value)
  101.     return if $game_party.members.size <= 1
  102.     @aggro[e][a] = value
  103.       @aggro[e][a] = [[@aggro[e][a], 0.0].max, 1.0].min
  104.     if @aggro[e][a] == 1.0
  105.       for aindex in 0...$game_party.members.size
  106.         unless a == aindex
  107.           @aggro[e][aindex] = 0
  108.         end
  109.       end
  110.     else
  111.       div = (@aggro[e].sum - @aggro[e][a])/(1.0-@aggro[e][a])
  112.       for aindex in 0...$game_party.members.size
  113.         unless a == aindex
  114.           @aggro[e][aindex] /= div
  115.         end
  116.       end
  117.     end
  118.   end
  119.  
  120.   alias zs_aggro_execute_action_attack execute_action_attack
  121.   def execute_action_attack
  122.     zs_aggro_execute_action_attack
  123.     return if @active_battler.action.make_targets.size == 0
  124.     return if @active_battler.action.make_targets[0].actor?
  125.     for target in @active_battler.action.make_targets
  126.       inc_aggro(target.index, @active_battler.index, Z_Systems::Aggro::NORMAL_ATTACK)
  127.     end
  128.       normalize_aggro
  129.   end
  130.  
  131.   alias zs_aggro_execute_action_skill execute_action_skill
  132.   def execute_action_skill
  133.     zs_aggro_execute_action_skill
  134.     return if @active_battler.action.make_targets[0].actor?
  135.     value = Z_Systems::Aggro::SKILL_DEFAULT
  136.       for line in @active_battler.action.skill.note.split(//)
  137.       case line
  138.         when /<aggro:[ ](.*)>/i
  139.         value = $1.to_f
  140.         if value > 1.0
  141.             value /= 100.0
  142.           end
  143.         for target in @active_battler.action.make_targets
  144.           inc_aggro(target.index, @active_battler.index, value)
  145.         end
  146.         return
  147.       when /<pro[ ][_]aggro:[ ](.*)>/i
  148.         value = $1.to_f
  149.         if value > 1.0
  150.           value /= 100.0
  151.         end
  152.         for target in @active_battler.action.make_targets
  153.           set_aggro(target.index, @active_battler.index, value)
  154.         end
  155.         return
  156.       end
  157.       end
  158.     for target in @active_battler.action.make_targets
  159.       inc_aggro(target.index, @active_battler.index, value)
  160.     end
  161.   end
  162.  
  163.   alias zs_aggro_execute_action_guard execute_action_guard
  164.   def execute_action_guard
  165.     zs_aggro_execute_action_guard
  166.     for enemy in $game_troop.members
  167.       inc_aggro(enemy.index, @active_battler.index, Z_Systems::Aggro::GUARD)
  168.     end
  169.     normalize_aggro
  170.   end
  171.  
  172. end
  173.  
  174. class Game_Unit
  175.  
  176.   def random_target_aggro(enemy)
  177.     $scene.normalize_aggro
  178.     rref = rand(101).to_f/100.0
  179. #~     print $scene.aggro[enemy.index].inspect
  180.     for a in 0...$game_party.members.size
  181.       if rref <= $scene.aggro[enemy.index][0..a].sum and !$game_party.members[a].dead?
  182. #~         print $scene.aggro[enemy.index][0..a].inspect
  183.         return $game_party.members[a]
  184.       end
  185.     end
  186.     return random_target_aggro(enemy)
  187.   end
  188.  
  189. end
  190.  
  191. class Game_BattleAction
  192.  
  193.   alias zs_aggro_decide_random_target decide_random_target
  194.   def decide_random_target(attacker = nil)                       #SEMI-OVERWRITE
  195.     if !friends_unit.members[0].actor? or attacker != nil
  196.       if for_friend?
  197.         target = friends_unit.random_target
  198.       elsif for_dead_friend?
  199.         target = friends_unit.random_dead_target
  200.       else
  201.         target = opponents_unit.random_target_aggro(attacker)
  202.       end
  203.       if target == nil
  204.         clear
  205.       else
  206.         @target_index = target.index
  207.       end
  208.     else
  209.       zs_aggro_decide_random_target
  210.     end
  211.   end
  212.  
  213. end
  214.  
  215. class Game_Enemy < Game_Battler
  216.  
  217.   def make_action
  218.     @action.clear
  219.     return unless movable?
  220.     available_actions = []
  221.     rating_max = 0
  222.     for action in enemy.actions
  223.       next unless conditions_met?(action)
  224.       if action.kind == 1
  225.         next unless skill_can_use?($data_skills[action.skill_id])
  226.       end
  227.       available_actions.push(action)
  228.       rating_max = [rating_max, action.rating].max
  229.     end
  230.     ratings_total = 0
  231.     rating_zero = rating_max - 3
  232.     for action in available_actions
  233.       next if action.rating <= rating_zero
  234.       ratings_total += action.rating - rating_zero
  235.     end
  236.     return if ratings_total == 0
  237.     value = rand(ratings_total)
  238.     for action in available_actions
  239.       next if action.rating <= rating_zero
  240.       if value < action.rating - rating_zero
  241.         @action.kind = action.kind
  242.         @action.basic = action.basic
  243.         @action.skill_id = action.skill_id
  244.         @action.decide_random_target(self)
  245.         return
  246.       else
  247.         value -= action.rating - rating_zero
  248.       end
  249.     end
  250.   end
  251.  
  252. end
  253.  
  254. class Game_Interpretor
  255.  
  256.   def command_339
  257.     iterate_battler(@params[0], @params[1]) do |battler|
  258.       next unless battler.exist?
  259.       battler.action.kind = @params[2]
  260.       if battler.action.kind == 0
  261.         battler.action.basic = @params[3]
  262.       else
  263.         battler.action.skill_id = @params[3]
  264.       end
  265.       if @params[4] == -2                   # Last target
  266.         battler.action.decide_last_target
  267.       elsif @params[4] == -1                # Random
  268.         battler.action.decide_random_target(battler)
  269.       elsif @params[4] >= 0                 # Index designation
  270.         battler.action.target_index = @params[4]
  271.       end
  272.       battler.action.forcing = true
  273.       $game_troop.forcing_battler = battler
  274.       @index += 1
  275.       return false
  276.     end
  277.     return true
  278.   end
  279.  
  280. end
Add Comment
Please, Sign In to add comment