#include "macros/btlcmd.inc"


_000:
    Call BATTLE_SUBSCRIPT_PUSH_ATTACKER_AND_DEFENDER

_001:
    TryPursuit _238
    UpdateVar OPCODE_SET, BTLVAR_POWER_MULTI, 20
    CalcCrit 
    CalcDamage 
    ApplyTypeEffectiveness 
    UpdateVar OPCODE_FLAG_OFF, BTLVAR_BATTLE_CTX_STATUS, SYSCTL_SKIP_ATTACK_MESSAGE
    UpdateVar OPCODE_FLAG_OFF, BTLVAR_BATTLE_CTX_STATUS, SYSCTL_PLAYED_MOVE_ANIMATION
    PrintAttackMessage 
    Wait 
    CompareVarToValue OPCODE_FLAG_SET, BTLVAR_MOVE_STATUS_FLAGS, MOVE_STATUS_DID_NOT_HIT, _154
    PlayMoveAnimation BTLSCR_ATTACKER
    Wait 
    CheckSubstitute BTLSCR_DEFENDER, _108
    UpdateVarFromVar OPCODE_SET, BTLVAR_SCRIPT_TEMP, BTLVAR_DAMAGE
    UpdateVar OPCODE_MUL, BTLVAR_SCRIPT_TEMP, -1
    CompareMonDataToVar OPCODE_LTE, BTLSCR_DEFENDER, BATTLEMON_CUR_HP, BTLVAR_SCRIPT_TEMP, _051
    UpdateVarFromVar OPCODE_SET, BTLVAR_ATTACKER_SHELL_BELL_DAMAGE_DEALT, BTLVAR_DAMAGE
    GoTo _060

_051:
    UpdateMonDataFromVar OPCODE_GET, BTLSCR_DEFENDER, BATTLEMON_CUR_HP, BTLVAR_ATTACKER_SHELL_BELL_DAMAGE_DEALT
    UpdateVar OPCODE_MUL, BTLVAR_ATTACKER_SHELL_BELL_DAMAGE_DEALT, -1

_060:
    UpdateVarFromVar OPCODE_SET, BTLVAR_HP_CALC_TEMP, BTLVAR_DAMAGE
    UpdateVarFromVar OPCODE_SET, BTLVAR_DEFENDER_PHYSICAL_DAMAGE_TAKEN, BTLVAR_DAMAGE
    UpdateVarFromVar OPCODE_SET, BTLVAR_MSG_BATTLER_TEMP, BTLVAR_DEFENDER
    CheckHoldOnWith1HP BTLSCR_MSG_TEMP
    Call BATTLE_SUBSCRIPT_UPDATE_HP
    Call BATTLE_SUBSCRIPT_CRITICAL_HIT
    Call BATTLE_SUBSCRIPT_MOVE_FOLLOWUP_MESSAGE
    CompareMonDataToValue OPCODE_EQU, BTLSCR_DEFENDER, BATTLEMON_CUR_HP, 0, _166
    TriggerAbilityOnHit _090
    CallFromVar BTLVAR_SCRIPT_TEMP

_090:
    TriggerHeldItemOnHit _094
    CallFromVar BTLVAR_SCRIPT_TEMP

_094:
    UpdateVar OPCODE_FLAG_ON, BTLVAR_BATTLE_CTX_STATUS, SYSCTL_MOVE_HIT
    TriggerHeldItemOnPivotMove _102
    CallFromVar BTLVAR_SCRIPT_TEMP

_102:
    UpdateVar OPCODE_FLAG_OFF, BTLVAR_BATTLE_CTX_STATUS, SYSCTL_MOVE_HIT
    GoTo _158

_108:
    UpdateVarFromVar OPCODE_SET, BTLVAR_SCRIPT_TEMP, BTLVAR_DAMAGE
    UpdateVar OPCODE_MUL, BTLVAR_SCRIPT_TEMP, -1
    CompareMonDataToVar OPCODE_LTE, BTLSCR_DEFENDER, BATTLEMON_SUBSTITUTE_HP, BTLVAR_SCRIPT_TEMP, _128
    UpdateVarFromVar OPCODE_SET, BTLVAR_ATTACKER_SHELL_BELL_DAMAGE_DEALT, BTLVAR_DAMAGE
    GoTo _142

_128:
    UpdateMonData OPCODE_FLAG_OFF, BTLSCR_DEFENDER, BATTLEMON_VOLATILE_STATUS, VOLATILE_CONDITION_SUBSTITUTE
    UpdateMonDataFromVar OPCODE_GET, BTLSCR_DEFENDER, BATTLEMON_SUBSTITUTE_HP, BTLVAR_ATTACKER_SHELL_BELL_DAMAGE_DEALT
    UpdateVar OPCODE_MUL, BTLVAR_ATTACKER_SHELL_BELL_DAMAGE_DEALT, -1

_142:
    UpdateVarFromVar OPCODE_SET, BTLVAR_MSG_BATTLER_TEMP, BTLVAR_DEFENDER
    Call BATTLE_SUBSCRIPT_HIT_SUBSTITUTE
    Call BATTLE_SUBSCRIPT_CRITICAL_HIT
    Call BATTLE_SUBSCRIPT_MOVE_FOLLOWUP_MESSAGE
    GoTo _158

_154:
    WaitButtonABTime 15
    Call BATTLE_SUBSCRIPT_MISSED

_158:
    Call BATTLE_SUBSCRIPT_POP_ATTACKER_AND_DEFENDER
    UpdateVarFromVar OPCODE_GET, BTLVAR_MOVE_TEMP, BTLVAR_CURRENT_MOVE
    GoTo _001

_166:
    Call BATTLE_SUBSCRIPT_FAINT_CHECK_DESTINY_BOND
    TriggerAbilityOnHit _172
    CallFromVar BTLVAR_SCRIPT_TEMP

_172:
    TriggerHeldItemOnHit _176
    CallFromVar BTLVAR_SCRIPT_TEMP

_176:
    UpdateVar OPCODE_FLAG_ON, BTLVAR_BATTLE_CTX_STATUS, SYSCTL_MOVE_HIT
    TriggerHeldItemOnPivotMove _184
    CallFromVar BTLVAR_SCRIPT_TEMP

_184:
    UpdateVar OPCODE_FLAG_OFF, BTLVAR_BATTLE_CTX_STATUS, SYSCTL_MOVE_HIT
    UpdateVarFromVar OPCODE_SET, BTLVAR_SCRIPT_TEMP, BTLVAR_FAINTED_MON
    UpdateVar OPCODE_SET, BTLVAR_FAINTED_MON, BATTLER_PLAYER_1
    UpdateVarFromVar OPCODE_SET, BTLVAR_CALC_TEMP, BTLVAR_BATTLE_CTX_STATUS_2
    UpdateVar OPCODE_RIGHT_SHIFT, BTLVAR_CALC_TEMP, 0x0000001C
    UpdateVar OPCODE_FLAG_OFF, BTLVAR_BATTLE_CTX_STATUS_2, SYSCTL_PAYOUT_EXP

_208:
    CompareVarToValue OPCODE_FLAG_NOT, BTLVAR_CALC_TEMP, 0x00000001, _215
    Call BATTLE_SUBSCRIPT_GRANT_EXP

_215:
    UpdateVar OPCODE_ADD, BTLVAR_FAINTED_MON, BATTLER_ENEMY_1
    UpdateVar OPCODE_RIGHT_SHIFT, BTLVAR_CALC_TEMP, 0x00000001
    CompareVarToValue OPCODE_NEQ, BTLVAR_CALC_TEMP, 0x00000000, _208
    // BUG: Acid Rain (see docs/bugs_and_glitches.md)
    UpdateVarFromVar OPCODE_SUB_TO_ZERO, BTLVAR_FIELD_CONDITIONS, BTLVAR_SCRIPT_TEMP
    Call BATTLE_SUBSCRIPT_POP_ATTACKER_AND_DEFENDER
    UpdateVarFromVar OPCODE_GET, BTLVAR_MOVE_TEMP, BTLVAR_CURRENT_MOVE

_238:
    End 
