Advertisement
Zetu

Aggression System

May 14th, 2011
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 9.89 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[0].actor?
  124.     for target in @active_battler.action.make_targets
  125.       inc_aggro(target.index, @active_battler.index, Z_Systems::Aggro::NORMAL_ATTACK)
  126.     end
  127.       normalize_aggro
  128.   end
  129.  
  130.   alias zs_aggro_execute_action_skill execute_action_skill
  131.   def execute_action_skill
  132.     zs_aggro_execute_action_skill
  133.     return if @active_battler.action.make_targets[0].actor?
  134.     value = Z_Systems::Aggro::SKILL_DEFAULT
  135.       for line in @active_battler.action.skill.note.split(//)
  136.       case line
  137.         when /<aggro:[ ](.*)>/i
  138.         value = $1.to_f
  139.         if value > 1.0
  140.             value /= 100.0
  141.           end
  142.         for target in @active_battler.action.make_targets
  143.           inc_aggro(target.index, @active_battler.index, value)
  144.         end
  145.         return
  146.       when /<pro[ ][_]aggro:[ ](.*)>/i
  147.         value = $1.to_f
  148.         if value > 1.0
  149.           value /= 100.0
  150.         end
  151.         for target in @active_battler.action.make_targets
  152.           set_aggro(target.index, @active_battler.index, value)
  153.         end
  154.         return
  155.       end
  156.       end
  157.     for target in @active_battler.action.make_targets
  158.       inc_aggro(target.index, @active_battler.index, value)
  159.     end
  160.   end
  161.  
  162.   alias zs_aggro_execute_action_guard execute_action_guard
  163.   def execute_action_guard
  164.     zs_aggro_execute_action_guard
  165.     for enemy in $game_troop.members
  166.       inc_aggro(enemy.index, @active_battler.index, Z_Systems::Aggro::GUARD)
  167.     end
  168.     normalize_aggro
  169.   end
  170.  
  171. end
  172.  
  173. class Game_Unit
  174.  
  175.   def random_target_aggro(enemy)
  176.     $scene.normalize_aggro
  177.     rref = rand(101).to_f/100.0
  178. #~     print $scene.aggro[enemy.index].inspect
  179.     for a in 0...$game_party.members.size
  180.       if rref <= $scene.aggro[enemy.index][0..a].sum and !$game_party.members[a].dead?
  181. #~         print $scene.aggro[enemy.index][0..a].inspect
  182.         return $game_party.members[a]
  183.       end
  184.     end
  185.     return random_target_aggro(enemy)
  186.   end
  187.  
  188. end
  189.  
  190. class Game_BattleAction
  191.  
  192.   alias zs_aggro_decide_random_target decide_random_target
  193.   def decide_random_target(attacker = nil)                       #SEMI-OVERWRITE
  194.     if !friends_unit.members[0].actor? or attacker != nil
  195.       if for_friend?
  196.         target = friends_unit.random_target
  197.       elsif for_dead_friend?
  198.         target = friends_unit.random_dead_target
  199.       else
  200.         target = opponents_unit.random_target_aggro(attacker)
  201.       end
  202.       if target == nil
  203.         clear
  204.       else
  205.         @target_index = target.index
  206.       end
  207.     else
  208.       zs_aggro_decide_random_target
  209.     end
  210.   end
  211.  
  212. end
  213.  
  214. class Game_Enemy < Game_Battler
  215.  
  216.   def make_action
  217.     @action.clear
  218.     return unless movable?
  219.     available_actions = []
  220.     rating_max = 0
  221.     for action in enemy.actions
  222.       next unless conditions_met?(action)
  223.       if action.kind == 1
  224.         next unless skill_can_use?($data_skills[action.skill_id])
  225.       end
  226.       available_actions.push(action)
  227.       rating_max = [rating_max, action.rating].max
  228.     end
  229.     ratings_total = 0
  230.     rating_zero = rating_max - 3
  231.     for action in available_actions
  232.       next if action.rating <= rating_zero
  233.       ratings_total += action.rating - rating_zero
  234.     end
  235.     return if ratings_total == 0
  236.     value = rand(ratings_total)
  237.     for action in available_actions
  238.       next if action.rating <= rating_zero
  239.       if value < action.rating - rating_zero
  240.         @action.kind = action.kind
  241.         @action.basic = action.basic
  242.         @action.skill_id = action.skill_id
  243.         @action.decide_random_target(self)
  244.         return
  245.       else
  246.         value -= action.rating - rating_zero
  247.       end
  248.     end
  249.   end
  250.  
  251. end
  252.  
  253. class Game_Interpretor
  254.  
  255.   def command_339
  256.     iterate_battler(@params[0], @params[1]) do |battler|
  257.       next unless battler.exist?
  258.       battler.action.kind = @params[2]
  259.       if battler.action.kind == 0
  260.         battler.action.basic = @params[3]
  261.       else
  262.         battler.action.skill_id = @params[3]
  263.       end
  264.       if @params[4] == -2                   # Last target
  265.         battler.action.decide_last_target
  266.       elsif @params[4] == -1                # Random
  267.         battler.action.decide_random_target(battler)
  268.       elsif @params[4] >= 0                 # Index designation
  269.         battler.action.target_index = @params[4]
  270.       end
  271.       battler.action.forcing = true
  272.       $game_troop.forcing_battler = battler
  273.       @index += 1
  274.       return false
  275.     end
  276.     return true
  277.   end
  278.  
  279. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement