// HCR v3.2.0 - Refactored code. Fixed GetFirstObjectInShape bug.
//::////////////////////////////////////////////////////////////////////////////
//:: FileName:  HC_Pal_DetEvil
//::////////////////////////////////////////////////////////////////////////////
/*
   Modified to work per PHB by Archaegeo, Wraithdrit, and Velmar.
*/
//::////////////////////////////////////////////////////////////////////////////
//:: Created By:
//:: Created On:
//::////////////////////////////////////////////////////////////////////////////
#include "HC_Inc"
#include "HC_Inc_TimeCheck"
#include "HC_Text_DetEvil"
//::////////////////////////////////////////////////////////////////////////////
void main()
{
  // Declare major variables.
  string sEvilMsg;
  object oTarget;
  int nPow;
  int nCtEvil = 0;
  int nMxEvil = 0;
  int nSSB = SecondsSinceBegin();
  int nLastDet = GetLocalInt(OBJECT_SELF, "DETECTEVILTIME");
  int nLastCnt = GetLocalInt(OBJECT_SELF, "DETECTEVILROUNDS");
  location lMyLoc  = GetLocation(OBJECT_SELF);
  location lTarget = GetItemActivatedTargetLocation();
  vector vMyVector = GetPositionFromLocation(lMyLoc);
  effect eVis  = EffectVisualEffect(VFX_COM_SPECIAL_RED_WHITE);

  if (!nLastCnt || !nLastDet || (nSSB > (nLastDet + 12)))
    {
     oTarget = GetFirstObjectInShape(SHAPE_CONE, 18.3, lTarget, TRUE, OBJECT_TYPE_CREATURE | OBJECT_TYPE_DOOR | OBJECT_TYPE_PLACEABLE, vMyVector);
     while (GetIsObjectValid(oTarget))
       {
        if (GetObjectType(oTarget) == OBJECT_TYPE_CREATURE)
          {
           if (GetAlignmentGoodEvil(oTarget) == ALIGNMENT_EVIL)
              nCtEvil++;
          }
        else if (GetLocalInt(oTarget, "EVILPOWER"))
          { nCtEvil++; }

        oTarget = GetNextObjectInShape(SHAPE_CONE, 18.3, lTarget, TRUE,  OBJECT_TYPE_CREATURE | OBJECT_TYPE_DOOR | OBJECT_TYPE_PLACEABLE, vMyVector);
       }

     if (nCtEvil > 0)
       { SendMessageToPC(OBJECT_SELF, DETEVIL); }

     nLastCnt++;
     SetLocalInt(OBJECT_SELF, "DETECTEVILROUNDS", nLastCnt);
    }
  else if (nLastCnt == 1)
    {
     oTarget = GetFirstObjectInShape(SHAPE_CONE, 18.3, lTarget, TRUE, OBJECT_TYPE_CREATURE | OBJECT_TYPE_DOOR | OBJECT_TYPE_PLACEABLE, vMyVector);
     while (GetIsObjectValid(oTarget))
       {
        if (GetObjectType(oTarget) == OBJECT_TYPE_CREATURE)
          {
           if (GetAlignmentGoodEvil(oTarget) == ALIGNMENT_EVIL)
             {
              nCtEvil++;

              nPow = abs(GetHitDice(oTarget) / 5);
              if ((GetLevelByClass(CLASS_TYPE_OUTSIDER, oTarget) > 0) ||
                  (GetLevelByClass(CLASS_TYPE_CLERIC, oTarget) > 0))
                { nPow = GetHitDice(oTarget); }
              else if ((GetLevelByClass(CLASS_TYPE_UNDEAD, oTarget) > 0) ||
                       (GetLevelByClass(CLASS_TYPE_ELEMENTAL, oTarget) > 0))
                { nPow = abs(GetHitDice(oTarget) / 2); }

              if (nPow > nMxEvil)
                { nMxEvil = nPow; }
             }
          }
        else
          {
           nPow = GetLocalInt(oTarget, "EVILPOWER");
           if (nPow > 0)
             {
              nCtEvil++;

              if (nPow > nMxEvil)
                { nMxEvil = nPow; }
             }
          }

        oTarget = GetNextObjectInShape(SHAPE_CONE, 18.3, lTarget, TRUE,  OBJECT_TYPE_CREATURE | OBJECT_TYPE_DOOR | OBJECT_TYPE_PLACEABLE, vMyVector);
       }

     if (nCtEvil > 0)
       {
        sEvilMsg = IntToString(nCtEvil) + SENSE;
        if (nMxEvil < 2)
          { sEvilMsg += FAINT; }
        else if (nMxEvil < 5)
          { sEvilMsg += MOD; }
        else if (nMxEvil < 11)
          { sEvilMsg += STRONG; }
        else
          { sEvilMsg += OVERPWR; }

        SendMessageToPC(OBJECT_SELF, sEvilMsg);

        if (nMxEvil > (GetHitDice(OBJECT_SELF)*2) && (nMxEvil > 10))
          {
           SendMessageToPC(OBJECT_SELF, STUNNED);
           DeleteLocalInt(OBJECT_SELF, "DETECTEVILROUNDS");
           effect eStun = EffectStunned();
           ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eStun, OBJECT_SELF, 6.0);
           return;
          }
       }

     nLastCnt++;
     SetLocalInt(OBJECT_SELF, "DETECTEVILROUNDS", nLastCnt);
    }
  else if (nLastCnt == 2)
    {
     oTarget = GetFirstObjectInShape(SHAPE_CONE, 18.3, lTarget, TRUE, OBJECT_TYPE_CREATURE | OBJECT_TYPE_DOOR | OBJECT_TYPE_PLACEABLE, vMyVector);
     while (GetIsObjectValid(oTarget))
       {
        if (GetObjectType(oTarget) == OBJECT_TYPE_CREATURE)
          {
           if (GetAlignmentGoodEvil(oTarget) == ALIGNMENT_EVIL)
             {
              ApplyEffectToObject(DURATION_TYPE_TEMPORARY, eVis, oTarget, 3.0);

              nPow = abs(GetHitDice(oTarget) / 5);
              if ((GetLevelByClass(CLASS_TYPE_OUTSIDER, oTarget) > 0) ||
                  (GetLevelByClass(CLASS_TYPE_CLERIC, oTarget) > 0))
                { nPow = GetHitDice(oTarget); }
              else if ((GetLevelByClass(CLASS_TYPE_UNDEAD, oTarget) > 0) ||
                       (GetLevelByClass(CLASS_TYPE_ELEMENTAL, oTarget) > 0))
                { nPow = abs(GetHitDice(oTarget) / 2); }

              sEvilMsg = "";
              if (nPow < 2)
                { sEvilMsg += FAINTLY; }
              else if (nPow < 5)
                { sEvilMsg += MODLY; }
              else if (nPow < 11)
                { sEvilMsg += STRONGLY; }
              else
                { sEvilMsg += OVERPWRLY; }

              SendMessageToPC(OBJECT_SELF, GetName(oTarget) + IS + sEvilMsg + EVIL);
             }
          }
        else
          {
           nPow = GetLocalInt(oTarget, "EVILPOWER");
           if (nPow > 0)
             {
              sEvilMsg = "";
              if (nPow < 2)
                { sEvilMsg += FAINTLY; }
              else if (nPow < 5)
                { sEvilMsg += MODLY; }
              else if (nPow < 11)
                { sEvilMsg += STRONGLY; }
              else
                { sEvilMsg += OVERPWRLY; }

              SendMessageToPC(OBJECT_SELF, GetName(oTarget) + IS + sEvilMsg + EVIL);
             }
          }
        oTarget = GetNextObjectInShape(SHAPE_CONE, 18.3, lTarget, TRUE, OBJECT_TYPE_CREATURE | OBJECT_TYPE_DOOR | OBJECT_TYPE_PLACEABLE, vMyVector);
       }
    }

  SetLocalInt(OBJECT_SELF, "DETECTEVILTIME", nSSB);
}
//::////////////////////////////////////////////////////////////////////////////
