Help - Search - Members - Calendar
Full Version: Counter Attack Script
RPG RPG Revolution Forums > Scripting > Script Development and Support > Script Requests
Deividdo
Hello,

I am looking for a basic RPG Maker XP Counter Attack script. I'm not hoping for anything fancy, but simply to have a character counter with a normal attack after being attacked by an enemy. I have looked around and haven't found any functional scripts for XP, which I find surprising since I thought this would be a rather common request.

My current project is using the Tankentai system, but if I had a basic counter attack that worked with the default system I believe I could make that compatible.

Thank you.
Jens of Zanicuud
I could build it, but I need some additional info.

1. Counterattack is triggered by an equipped item? Or a status? Or maybe there's a character who counterattacks by default?
2. Enemies should counterattack too?

Jens
Jens of Zanicuud
Nevermind, here's the script.
I think it's not compatible with Tankentai, but I hope you can rearrange it...

CODE
#==============================================================================
#=#============================================================================
# #** Jens of Zanicuud counterattack script v 1.0
# #   -free use, just credit
# #   -free customization, however, posting on RRR the modified version is
# #    mandatory
# #   -you can find me on www.rpgrevoultion.com
#=#============================================================================
#==============================================================================

#==============================================================================
# ** Constants
#==============================================================================

COUNTERATTACK_ENEMY_ID   = []   #id of enemies counter attacking by default
COUNTERATTACK_WEAPON_ID  = []   #id of weapons triggering counters
COUNTERATTACK_ARMOR_ID   = []   #id of armors triggering counters
COUNTERATTACK_STATUS_ID  = []   #id of status triggering counters
COUNTERATTACK_HERO_ID    = []   #id of heroes counter attacking by default

#==============================================================================
# ** Game_BattleAction
#==============================================================================
class Game_BattleAction
  attr_accessor :counter
  #--------------------------------------------------------------------------
  # * aliases
  #--------------------------------------------------------------------------
  alias joz3_counterattack_initialize initialize
  alias joz3_counterattack_clear      clear
  #--------------------------------------------------------------------------
  # * Initialize
  #--------------------------------------------------------------------------
  def initialize
    joz3_counterattack_initialize
    @counter = false
  end
  #--------------------------------------------------------------------------
  # * Clear
  #--------------------------------------------------------------------------
  def clear
    joz3_counterattack_clear
    @counter = false
  end
end

#==============================================================================
# ** Game_Battler
#==============================================================================
class Game_Battler
  #--------------------------------------------------------------------------
  # * Store_Action
  #--------------------------------------------------------------------------
  def store_action
    @backup_action = self.current_action
  end
  #--------------------------------------------------------------------------
  # * Restore_Action
  #--------------------------------------------------------------------------
  def restore_action
    if @backup_action != nil
    @current_action = @backup_action
    end
  end
  #--------------------------------------------------------------------------
  # * Prepare Counterattack
  #--------------------------------------------------------------------------
  def prepare_counterattack(index)
    store_action
    @current_action.kind         = 0
    @current_action.basic        = 0
    @current_action.target_index = index
    @current_action.counter      = true
  end
  #--------------------------------------------------------------------------
  # * Can_Counterattack?
  #--------------------------------------------------------------------------
  def can_counterattack?
    #hp check
    if self.hp0?
      return false
    end
    #input check
    if !self.inputable?
      return false
    end
    #status check
    for id in COUNTERATTACK_STATUS_ID
    if self.state?(id)
      return true
    end
    end
    return false
  end
end

#==============================================================================
# ** Game_Actor
#==============================================================================
class Game_Actor < Game_Battler
  #--------------------------------------------------------------------------
  # * Can_Counterattack?
  #--------------------------------------------------------------------------
  def can_counterattack?
    #hero check
    if COUNTERATTACK_HERO_ID.include?(self.id)
      return true
    end
    #weapon check
    if COUNTERATTACK_WEAPON_ID.include?(@weapon_id)
      return true
    end
    #armor check
    if COUNTERATTACK_ARMOR_ID.include?(@armor1_id) or
       COUNTERATTACK_ARMOR_ID.include?(@armor2_id) or
       COUNTERATTACK_ARMOR_ID.include?(@armor3_id) or
       COUNTERATTACK_ARMOR_ID.include?(@armor4_id)
      return true
    end
    super
  end
end

#==============================================================================
# ** Game_Enemy
#==============================================================================
class Game_Enemy < Game_Battler
  #--------------------------------------------------------------------------
  # * Can_Counterattack?
  #--------------------------------------------------------------------------
  def can_counterattack?
    #id check
    if COUNTERATTACK_ENEMY_ID.include?(self.id)
      return true
    end
    super
  end
end

#==============================================================================
# ** Scene_Battle (part 4)
#------------------------------------------------------------------------------
#  This class performs battle screen processing.
#==============================================================================
class Scene_Battle

  #--------------------------------------------------------------------------
  # * Frame Update (main phase step 3 : animation for action performer)
  #--------------------------------------------------------------------------
  def update_phase4_step3
    # Animation for action performer (if ID is 0, then white flash)
    if @active_battler.current_action.counter
      @help_window.set_text(@active_battler.name + ": Counter Attack!", 1)
    end
    if @animation1_id == 0
      @active_battler.white_flash = true
    else
      @active_battler.animation_id = @animation1_id
      @active_battler.animation_hit = true
    end
    # Shift to step 4
    @phase4_step = 4
  end
  #--------------------------------------------------------------------------
  # * Frame Update (main phase step 4 : animation for target)
  #--------------------------------------------------------------------------
  def update_phase4_step4
    # Animation for target
    for target in @target_battlers
      target.animation_id = @animation2_id
      target.animation_hit = (target.damage != "Miss")
    end
    if @active_battler.current_action.kind == 0 and
      @active_battler.current_action.basic == 0 and
      @active_battler.current_action.counter == false
      target = @target_battlers[0]
      if target.can_counterattack?
        target.prepare_counterattack(@active_battler.index)
        @counterattack_flag = true
      end
    end
    # Animation has at least 8 frames, regardless of its length
    @wait_count = 8
    # Shift to step 5
    @phase4_step = 5
  end
  #--------------------------------------------------------------------------
  # * Frame Update (main phase step 5 : damage display)
  #--------------------------------------------------------------------------
  def update_phase4_step5
    # Hide help window
    @help_window.visible = false
    # Refresh status window
    @status_window.refresh
    # Display damage
    for target in @target_battlers
      if target.damage != nil
        target.damage_pop = true
      end
    end
    # Shift to step 6
    @phase4_step = 6
  end
  #--------------------------------------------------------------------------
  # * Frame Update (main phase step 6 : refresh)
  #--------------------------------------------------------------------------
  def update_phase4_step6
    # Clear battler being forced into action
    if @counterattack_flag
     counter_update
     return
    end
    if @active_battler.current_action.counter
     @active_battler.restore_action
     @active_battler.current_action.counter = false
    end
    $game_temp.forcing_battler = nil
    # If common event ID is valid
    if @common_event_id > 0
      # Set up event
      common_event = $data_common_events[@common_event_id]
      $game_system.battle_interpreter.setup(common_event.list, 0)
    end
    # Shift to step 1
    @phase4_step = 1
  end
  #--------------------------------------------------------------------------
  # * Counter Update
  #--------------------------------------------------------------------------
  def counter_update
    @counterattack_flag = false
    # Initialize animation ID and common event ID
    @animation1_id = 0
    @animation2_id = 0
    @common_event_id = 0
    # Shift from head of actionless battlers
    @active_battler = @target_battlers[0]
    # If already removed from battle
    # Natural removal of states
    @active_battler.remove_states_auto
    # Refresh status window
    @status_window.refresh
    # Shift to step 2
    @phase4_step = 2
  end
end


HOW TO USE:
Just set the constants...

e.g.

COUNTERATTACK_ENEMY_ID = [1,3,7] #id of enemies counter attacking by default
COUNTERATTACK_WEAPON_ID = [2,12] #id of weapons triggering counters
COUNTERATTACK_ARMOR_ID = [10,14] #id of armors triggering counters
COUNTERATTACK_STATUS_ID = [1,6] #id of status triggering counters
COUNTERATTACK_HERO_ID = [9] #id of heroes counter attacking by default

Ask for troubleshooting anytime.

Jens
Deividdo
Thank you very much, Jens! ^_^ This works wonderfully and is just what I wanted.

As expected, it wasn't compatible with Atoa's XP Tankentai. So I added a portion of your code to the following Tankentai method to correct this allowing counterattacking to work.

CODE
  alias update_phase4_step6_n01 update_phase4_step6
  def update_phase4_step6
    #======================================================================
    #This is the code that was added for the Counter Attack.
    if @active_battler.current_action.counter
      @help_window.set_text(@active_battler.name + ": Counter Attack!", 1)
    end
    if @active_battler.current_action.kind == 0 and
      @active_battler.current_action.basic == 0 and
      @active_battler.current_action.counter == false
      target = @target_battlers[0]
      if target.can_counterattack?
        target.prepare_counterattack(@active_battler.index)
        @counterattack_flag = true
      end
    end    
    #======================================================================
    update_phase4_step6_n01
    @active_battler.active = false if @active_battler != nil
  end


My project is using the CTB extension, but I had to remove that in order to get counterattacking to work with the normal Tankentai. Once I figure out how to fix that I'll post that too for anyone who might be curious.
Wonderjosh
Hopefully the topic's not too old to comment on, but I found that if a character is confused and attacks another character, the attacked character retaliates against an enemy rather than the attacking character. Any way to adjust this? Maybe just so the attacked party member doesn't retaliate against the attacking party member.
Night_Runner
This script should make it so party member's cannot counterattack other party members

CODE
#==============================================================================
#=#============================================================================
# #**� Jens of Zanicuud counterattack script v 1.0
# #   -free use, just credit
# #   -free customization, however, posting on RRR the modified version is
# #    mandatory
# #   -you can find me on www.rpgrevoultion.com
#=#============================================================================
#==============================================================================

#==============================================================================
# ** Constants
#==============================================================================

COUNTERATTACK_ENEMY_ID   = []   #id of enemies counter attacking by default
COUNTERATTACK_WEAPON_ID  = []   #id of weapons triggering counters
COUNTERATTACK_ARMOR_ID   = []   #id of armors triggering counters
COUNTERATTACK_STATUS_ID  = []   #id of status triggering counters
COUNTERATTACK_HERO_ID    = []   #id of heroes counter attacking by default

#==============================================================================
# ** Game_BattleAction
#==============================================================================
class Game_BattleAction
  attr_accessor :counter
  #--------------------------------------------------------------------------
  # * aliases
  #--------------------------------------------------------------------------
  alias joz3_counterattack_initialize initialize
  alias joz3_counterattack_clear      clear
  #--------------------------------------------------------------------------
  # * Initialize
  #--------------------------------------------------------------------------
  def initialize
    joz3_counterattack_initialize
    @counter = false
  end
  #--------------------------------------------------------------------------
  # * Clear
  #--------------------------------------------------------------------------
  def clear
    joz3_counterattack_clear
    @counter = false
  end
end

#==============================================================================
# ** Game_Battler
#==============================================================================
class Game_Battler
  #--------------------------------------------------------------------------
  # * Store_Action
  #--------------------------------------------------------------------------
  def store_action
    @backup_action = self.current_action
  end
  #--------------------------------------------------------------------------
  # * Restore_Action
  #--------------------------------------------------------------------------
  def restore_action
    if @backup_action != nil
    @current_action = @backup_action
    end
  end
  #--------------------------------------------------------------------------
  # * Prepare Counterattack
  #--------------------------------------------------------------------------
  def prepare_counterattack(index)
    store_action
    @current_action.kind         = 0
    @current_action.basic        = 0
    @current_action.target_index = index
    @current_action.counter      = true
  end
  #--------------------------------------------------------------------------
  # * Can_Counterattack?
  #--------------------------------------------------------------------------
  def can_counterattack?
    #hp check
    if self.hp0?
      return false
    end
    #input check
    if !self.inputable?
      return false
    end
    #status check
    for id in COUNTERATTACK_STATUS_ID
    if self.state?(id)
      return true
    end
    end
    return false
  end
end

#==============================================================================
# ** Game_Actor
#==============================================================================
class Game_Actor < Game_Battler
  #--------------------------------------------------------------------------
  # * Can_Counterattack?
  #--------------------------------------------------------------------------
  def can_counterattack?
    #hero check
    if COUNTERATTACK_HERO_ID.include?(self.id)
      return true
    end
    #weapon check
    if COUNTERATTACK_WEAPON_ID.include?(@weapon_id)
      return true
    end
    #armor check
    if COUNTERATTACK_ARMOR_ID.include?(@armor1_id) or
       COUNTERATTACK_ARMOR_ID.include?(@armor2_id) or
       COUNTERATTACK_ARMOR_ID.include?(@armor3_id) or
       COUNTERATTACK_ARMOR_ID.include?(@armor4_id)
      return true
    end
    super
  end
end

#==============================================================================
# ** Game_Enemy
#==============================================================================
class Game_Enemy < Game_Battler
  #--------------------------------------------------------------------------
  # * Can_Counterattack?
  #--------------------------------------------------------------------------
  def can_counterattack?
    #id check
    if COUNTERATTACK_ENEMY_ID.include?(self.id)
      return true
    end
    super
  end
end

#==============================================================================
# ** Scene_Battle (part 4)
#------------------------------------------------------------------------------
#  This class performs battle screen processing.
#==============================================================================
class Scene_Battle

  #--------------------------------------------------------------------------
  # * Frame Update (main phase step 3 : animation for action performer)
  #--------------------------------------------------------------------------
  def update_phase4_step3
    # Animation for action performer (if ID is 0, then white flash)
    if @active_battler.current_action.counter
      @help_window.set_text(@active_battler.name + ": Counter Attack!", 1)
    end
    if @animation1_id == 0
      @active_battler.white_flash = true
    else
      @active_battler.animation_id = @animation1_id
      @active_battler.animation_hit = true
    end
    # Shift to step 4
    @phase4_step = 4
  end
  #--------------------------------------------------------------------------
  # * Frame Update (main phase step 4 : animation for target)
  #--------------------------------------------------------------------------
  def update_phase4_step4
    # Animation for target
    for target in @target_battlers
      target.animation_id = @animation2_id
      target.animation_hit = (target.damage != "Miss")
    end
    if @active_battler.current_action.kind == 0 and
      @active_battler.current_action.basic == 0 and
      @active_battler.current_action.counter == false
      target = @target_battlers[0]
      if target.can_counterattack?
        if not (@active_battler.is_a?(Game_Actor) and target.is_a?(Game_Actor))
          target.prepare_counterattack(@active_battler.index)
          @counterattack_flag = true
        end
      end
    end
    # Animation has at least 8 frames, regardless of its length
    @wait_count = 8
    # Shift to step 5
    @phase4_step = 5
  end
  #--------------------------------------------------------------------------
  # * Frame Update (main phase step 5 : damage display)
  #--------------------------------------------------------------------------
  def update_phase4_step5
    # Hide help window
    @help_window.visible = false
    # Refresh status window
    @status_window.refresh
    # Display damage
    for target in @target_battlers
      if target.damage != nil
        target.damage_pop = true
      end
    end
    # Shift to step 6
    @phase4_step = 6
  end
  #--------------------------------------------------------------------------
  # * Frame Update (main phase step 6 : refresh)
  #--------------------------------------------------------------------------
  def update_phase4_step6
    # Clear battler being forced into action
    if @counterattack_flag
     counter_update
     return
    end
    if @active_battler.current_action.counter
     @active_battler.restore_action
     @active_battler.current_action.counter = false
    end
    $game_temp.forcing_battler = nil
    # If common event ID is valid
    if @common_event_id > 0
      # Set up event
      common_event = $data_common_events[@common_event_id]
      $game_system.battle_interpreter.setup(common_event.list, 0)
    end
    # Shift to step 1
    @phase4_step = 1
  end
  #--------------------------------------------------------------------------
  # * Counter Update
  #--------------------------------------------------------------------------
  def counter_update
    @counterattack_flag = false
    # Initialize animation ID and common event ID
    @animation1_id = 0
    @animation2_id = 0
    @common_event_id = 0
    # Shift from head of actionless battlers
    @active_battler = @target_battlers[0]
    # If already removed from battle
    # Natural removal of states
    @active_battler.remove_states_auto
    # Refresh status window
    @status_window.refresh
    # Shift to step 2
    @phase4_step = 2
  end
end
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2013 Invision Power Services, Inc.