//::///////////////////////////////////////////////
//:: Default On Damaged
//:: NW_C2_DEFAULT6
//:: Copyright (c) 2001 Bioware Corp.
//:://////////////////////////////////////////////
/*
    If already fighting then ignore, else determine
    combat round
*/
//:://////////////////////////////////////////////
//:: Created By: Preston Watamaniuk
//:: Created On: Oct 16, 2001
//:://////////////////////////////////////////////

#include "NW_I0_GENERIC"

int IsItemMetal(object oItem)
{
  int nReturnVal=0;
  int type=GetBaseItemType(oItem);
  if((type<6)||type==9||type==10||type==12||type==13||type==17||type==18
     ||type==22||type==27||type==32||type==33||type==35||type==37||type==28
     ||type==40||type==41||type==42||type==47||type==51||type==52||type==53
     ||type==57||type==59||type==60||type==63||type==65||type==76||type==7
     ||type==19||type==20||type==21||type==28||type==31||type==44||type==45
     ||type==46||type==56||type==62||type==78||type==25||type==55||type==58)
  {
    nReturnVal=2;// Mostly metal
  }
  return nReturnVal;
}

void main()
{
  if(!GetFleeToExit())
  {
    if(!GetSpawnInCondition(NW_FLAG_SET_WARNINGS))
    {
      if (GetDistanceToObject(GetLastDamager()) < 4.0)
      {
        object oPC = GetLastDamager();
        if (GetItemInSlot(INVENTORY_SLOT_RIGHTHAND, oPC) != OBJECT_INVALID)
        {
          object oWeapon = GetItemInSlot(INVENTORY_SLOT_RIGHTHAND, oPC);
          if (!GetWeaponRanged(oWeapon) && (IsItemMetal(oWeapon)>0))
          {

      //Determine AC Bonus, Enhance Bonus, To Hit Bonus, & Misc. Properties of oWeapon
      int iBonus = 0;
      int iEnhance = 0;
      int iToHit = 0;
      int iProp = 0;
      // Sums across all known ITEM_PROPERTY_ constants
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ABILITY_BONUS) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_AC_BONUS) == TRUE)
       {
       iProp+=1;
       iBonus=3;
       }
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_AC_BONUS_VS_ALIGNMENT_GROUP) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_AC_BONUS_VS_DAMAGE_TYPE) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_AC_BONUS_VS_RACIAL_GROUP) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_AC_BONUS_VS_SPECIFIC_ALIGNMENT) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ATTACK_BONUS) == TRUE)
       {
       iProp+=1;
       iToHit=3;
       }
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ATTACK_BONUS_VS_ALIGNMENT_GROUP) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ATTACK_BONUS_VS_RACIAL_GROUP) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ATTACK_BONUS_VS_SPECIFIC_ALIGNMENT) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_BASE_ITEM_WEIGHT_REDUCTION) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_BONUS_FEAT) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_BONUS_SPELL_SLOT_OF_LEVEL_N) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_CAST_SPELL) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_DAMAGE_BONUS) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_DAMAGE_BONUS_VS_ALIGNMENT_GROUP) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_DAMAGE_BONUS_VS_RACIAL_GROUP) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_DAMAGE_BONUS_VS_SPECIFIC_ALIGNMENT) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_DAMAGE_RESISTANCE) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_DAMAGE_VULNERABILITY) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_DARKVISION) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ENHANCEMENT_BONUS) == TRUE)
       {
       iProp+=1;
       iEnhance=3;
       }
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ENHANCEMENT_BONUS_VS_ALIGNMENT_GROUP) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ENHANCEMENT_BONUS_VS_RACIAL_GROUP) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ENHANCEMENT_BONUS_VS_SPECIFIC_ALIGNEMENT) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_EXTRA_MELEE_DAMAGE_TYPE) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_EXTRA_RANGED_DAMAGE_TYPE) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_FREEDOM_OF_MOVEMENT) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_HASTE) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_HOLY_AVENGER) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_IMMUNITY_DAMAGE_TYPE) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_IMMUNITY_MISCELLANEOUS) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_IMMUNITY_SPECIFIC_SPELL) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_IMMUNITY_SPELL_SCHOOL) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_IMMUNITY_SPELLS_BY_LEVEL) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_IMPROVED_EVASION) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_KEEN) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_LIGHT) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_MASSIVE_CRITICALS) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_MIGHTY) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_MIND_BLANK) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ON_HIT_PROPERTIES) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ONHITCASTSPELL) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_POISON) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_REGENERATION) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_REGENERATION_VAMPIRIC) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_SAVING_THROW_BONUS) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_SAVING_THROW_BONUS_SPECIFIC) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_SKILL_BONUS) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_SPELL_RESISTANCE) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_TRUE_SEEING) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_UNLIMITED_AMMUNITION) == TRUE) iProp+=1;

    //Apply Splash Damage if Damager is in H2H Combat
    if ((GetDistanceToObject(oPC) < 4.5)&&(IsItemMetal(oWeapon)))
    {
            // Determine Saving Throw for oWeapon
            string sWeapon = GetName(oWeapon);
            if (ReflexSave(oPC,20-iProp-iBonus-iEnhance-iToHit))//saved
            {
              if (GetIsPC(oPC))
              {
                FloatingTextStringOnCreature("The "+sWeapon+" resists the rust effects.", oPC, TRUE);
                //SendMessageToPC(oPC,"Your "+sWeapon+" resists the rust effects.");
              }
            }
            else //didn't save
            {
              if (GetIsPC(oPC))
              {
                FloatingTextStringOnCreature("The rust monster has destroyed the "+sWeapon+"!", oPC, TRUE);
                //SendMessageToPC(oPC,"Your "+sWeapon+" has impacted the monster and is destroyed!");
              }
              DestroyObject(oWeapon);
              ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_FNF_GAS_EXPLOSION_NATURE),oPC);
            }
          }
        }
        else
        {
          if (GetItemInSlot(INVENTORY_SLOT_LEFTHAND, oPC) != OBJECT_INVALID)
          {
            object oWeapon = GetItemInSlot(INVENTORY_SLOT_LEFTHAND, oPC);
            if (IsItemMetal(oWeapon)>0)
            {

      //Reset AC Bonus, Enhance Bonus, To Hit Bonus, and Misc. Properties of oWeapon
      int iBonus = 0;
      int iEnhance = 0;
      int iToHit = 0;
      int iProp = 0;
      // Sums across all known ITEM_PROPERTY_ constants
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ABILITY_BONUS) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_AC_BONUS) == TRUE)
       {
       iProp+=1;
       iBonus=3;
       }
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_AC_BONUS_VS_ALIGNMENT_GROUP) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_AC_BONUS_VS_DAMAGE_TYPE) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_AC_BONUS_VS_RACIAL_GROUP) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_AC_BONUS_VS_SPECIFIC_ALIGNMENT) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ATTACK_BONUS) == TRUE)
       {
       iProp+=1;
       iToHit=3;
       }
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ATTACK_BONUS_VS_ALIGNMENT_GROUP) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ATTACK_BONUS_VS_RACIAL_GROUP) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ATTACK_BONUS_VS_SPECIFIC_ALIGNMENT) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_BASE_ITEM_WEIGHT_REDUCTION) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_BONUS_FEAT) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_BONUS_SPELL_SLOT_OF_LEVEL_N) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_CAST_SPELL) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_DAMAGE_BONUS) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_DAMAGE_BONUS_VS_ALIGNMENT_GROUP) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_DAMAGE_BONUS_VS_RACIAL_GROUP) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_DAMAGE_BONUS_VS_SPECIFIC_ALIGNMENT) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_DAMAGE_RESISTANCE) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_DAMAGE_VULNERABILITY) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_DARKVISION) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ENHANCEMENT_BONUS) == TRUE)
       {
       iProp+=1;
       iEnhance=3;
       }
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ENHANCEMENT_BONUS_VS_ALIGNMENT_GROUP) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ENHANCEMENT_BONUS_VS_RACIAL_GROUP) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ENHANCEMENT_BONUS_VS_SPECIFIC_ALIGNEMENT) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_EXTRA_MELEE_DAMAGE_TYPE) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_EXTRA_RANGED_DAMAGE_TYPE) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_FREEDOM_OF_MOVEMENT) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_HASTE) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_HOLY_AVENGER) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_IMMUNITY_DAMAGE_TYPE) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_IMMUNITY_MISCELLANEOUS) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_IMMUNITY_SPECIFIC_SPELL) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_IMMUNITY_SPELL_SCHOOL) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_IMMUNITY_SPELLS_BY_LEVEL) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_IMPROVED_EVASION) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_KEEN) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_LIGHT) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_MASSIVE_CRITICALS) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_MIGHTY) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_MIND_BLANK) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ON_HIT_PROPERTIES) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_ONHITCASTSPELL) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_POISON) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_REGENERATION) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_REGENERATION_VAMPIRIC) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_SAVING_THROW_BONUS) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_SAVING_THROW_BONUS_SPECIFIC) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_SKILL_BONUS) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_SPELL_RESISTANCE) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_TRUE_SEEING) == TRUE) iProp+=1;
      if (GetItemHasItemProperty(oWeapon, ITEM_PROPERTY_UNLIMITED_AMMUNITION) == TRUE) iProp+=1;

              string sWeapon = GetName(oWeapon);
              if (ReflexSave(oPC,20-iProp-iBonus-iEnhance-iToHit))//saved
              {
                if (GetIsPC(oPC))
                {
                  FloatingTextStringOnCreature("The "+sWeapon+" resists the rust effects.", oPC, TRUE);
                  //SendMessageToPC(oPC,"The "+sWeapon+" resists the rust effects.");
                }
              }
              else //didn't save
              {
                if (GetIsPC(oPC))
                {
                  FloatingTextStringOnCreature("The rust monster has destroyed the "+sWeapon+"!", oPC, TRUE);
                  //SendMessageToPC(oPC,"Your "+sWeapon+" has impacted the monster and is destroyed!");
                }
                DestroyObject(oWeapon);
                ApplyEffectToObject(DURATION_TYPE_INSTANT,EffectVisualEffect(VFX_FNF_GAS_EXPLOSION_NATURE),oPC);
              }
            }
          }
        }
        if(!GetIsObjectValid(GetAttemptedAttackTarget()) && !GetIsObjectValid(GetAttemptedSpellTarget()))
        {
          if(GetBehaviorState(NW_FLAG_BEHAVIOR_SPECIAL))
          {
            DetermineSpecialBehavior(GetLastDamager());
          }
          else if(GetIsObjectValid(GetLastDamager()))
          {
            DetermineCombatRound();
            if(!GetIsFighting(OBJECT_SELF))
            {
              object oTarget = GetLastDamager();
              if(!GetObjectSeen(oTarget) && GetArea(OBJECT_SELF) == GetArea(oTarget))
              {
                ActionMoveToLocation(GetLocation(oTarget), TRUE);
                ActionDoCommand(DetermineCombatRound());
              }
            }
          }
        }
        else if (!GetIsObjectValid(GetAttemptedSpellTarget()))
        {
          object oTarget = GetAttackTarget();
          if(!GetIsObjectValid(oTarget))
          {
            oTarget = GetAttemptedAttackTarget();
          }
          object oAttacker = GetLastHostileActor();
          if (GetIsObjectValid(oAttacker) && oTarget != oAttacker && GetIsEnemy(oAttacker) &&
             (GetTotalDamageDealt() > (GetMaxHitPoints(OBJECT_SELF) / 4) ||
             (GetHitDice(oAttacker) - 2) > GetHitDice(oTarget) ) )
          {
            DetermineCombatRound(oAttacker);
          }
        }
      }
    }
  }
  if(GetSpawnInCondition(NW_FLAG_DAMAGED_EVENT))
  {
    SignalEvent(OBJECT_SELF, EventUserDefined(1006));
  }
 }
}
