////////////::////////////////////////////////////////////////////
//:: Default On Heartbeat
//:: NW_C2_DEFAULT1
//:: Copyright (c) 2001 Bioware Corp.
//::////////////////////////////////////////////////////
/*
    This script will have people perform default
    animations.
*/
//::///////////////////////////////////////////////
//:: Created By: Preston Watamaniuk
//:: Created On: Nov 23, 2001
//:://///////////////////////////////////////////////

// makes the mob sneak if it should be
void CheckSneak(object oMob=OBJECT_SELF);

#include "NW_I0_GENERIC"
#include "jw_privates_inc"
#include "prc_inc_spells"

void main()
{
    // * if not runnning normal or better Ai then exit for performance reasons
    if (GetAILevel() == AI_LEVEL_VERY_LOW) return;
	
	ExecuteScript("prc_npc_hb", OBJECT_SELF);

    if(GetSpawnInCondition(NW_FLAG_FAST_BUFF_ENEMY))
    {
        // This will return TRUE if an enemy was within 40.0 m
        // and we buffed ourselves up instantly to respond --
        // simulates a spellcaster with protections enabled
        // already.
        if(TalentAdvancedBuff(40.0))
        {
            // This is a one-shot deal
            SetSpawnInCondition(NW_FLAG_FAST_BUFF_ENEMY, FALSE);

            // This return means we skip sending the user-defined
            // heartbeat signal in this one case.
            return;
        }
    }

    if(PRCGetHasEffect(EFFECT_TYPE_SLEEP))
    {
        // If we're asleep and this is the result of sleeping
        // at night, apply the floating 'z's visual effect
        // every so often

        if(GetSpawnInCondition(NW_FLAG_SLEEPING_AT_NIGHT))
        {
            effect eVis = EffectVisualEffect(VFX_IMP_SLEEP);
            if(d10() > 6)
            {
                ApplyEffectToObject(DURATION_TYPE_INSTANT, eVis, OBJECT_SELF);
            }
        }
    }
	
int nTalent=FALSE;


object oTarget=GetTarget();
if ((GetIsDead(oTarget))||(!GetIsEnemy(oTarget)))
{
 SetTarget(OBJECT_INVALID);
 oTarget=OBJECT_INVALID;
}


// NB, note D8 check below also applies
if ((GetArea(oTarget)!=GetArea(OBJECT_SELF))&&(d4()==1))
{
 SetTarget(OBJECT_INVALID);
 oTarget=OBJECT_INVALID;
}

int nCoward=FALSE;
if ((GetLevelByClass(CLASS_TYPE_COMMONER)>=1)||(GetLocalInt(OBJECT_SELF,"coward")==TRUE))
{
nCoward=TRUE;
}

// puts the NPC into spot mode if it is near its target but cannot see it
/// or takes it out if it is already near its target and has failed to see it

if ((!GetObjectSeen(oTarget))&&(GetIsObjectValid(oTarget))&&(GetDistanceToObject(oTarget)<12.0)&&(!GetIsFighting(OBJECT_SELF)))
{
    if ((!PRCGetHasEffect(EFFECT_TYPE_TRUESEEING))&&(!PRCGetHasEffect(EFFECT_TYPE_SEEINVISIBLE)))


    {
    nTalent=TalentSeeInvisible();
    }

    if ((!GetDetectMode(OBJECT_SELF))&&(!GetIsFighting(OBJECT_SELF))&&(nTalent==FALSE))
    {

    ActionUseSkill(SKILL_SPOT,OBJECT_SELF);
    SetTarget(OBJECT_INVALID);
    oTarget=OBJECT_INVALID;
    }

}


if (!GetIsObjectValid(oTarget))
{
oTarget=GetNearestCreature(CREATURE_TYPE_REPUTATION,REPUTATION_TYPE_ENEMY,OBJECT_SELF,1,CREATURE_TYPE_PERCEPTION,PERCEPTION_SEEN_AND_HEARD);

    if (!GetIsObjectValid(oTarget))
    {
    oTarget=GetNearestCreature(CREATURE_TYPE_REPUTATION,REPUTATION_TYPE_ENEMY,OBJECT_SELF,1,CREATURE_TYPE_PERCEPTION,PERCEPTION_SEEN_AND_NOT_HEARD);
    }
    if (!GetIsObjectValid(oTarget))
    {
    oTarget=GetNearestCreature(CREATURE_TYPE_REPUTATION,REPUTATION_TYPE_ENEMY,OBJECT_SELF,1,CREATURE_TYPE_PERCEPTION,PERCEPTION_HEARD_AND_NOT_SEEN);
    }
   if (GetIsObjectValid(oTarget))
   {
   SetTarget(oTarget);
   SpeakString("NW_ATTACK_MY_TARGET", TALKVOLUME_SILENT_TALK);
   }

}





if(GetSpawnInCondition(NW_FLAG_FAST_BUFF_ENEMY))
{
if(TalentAdvancedBuff(40.0))
{
SetSpawnInCondition(NW_FLAG_FAST_BUFF_ENEMY, FALSE);
return;
}
}









if(!GetIsInCombat(OBJECT_SELF)&&(GetIsObjectValid(oTarget))&&(nCoward==FALSE))
       {
        if (GetObjectSeen(oTarget)||GetObjectHeard(oTarget))
            {
              if ((GetStealthMode(OBJECT_SELF)!=TRUE)&&(d3()==1))
              {
               PlayVoiceChat(VOICE_CHAT_ATTACK);
              }
              if (!GetIsFighting(OBJECT_SELF))
              {
              DetermineCombatRound();
              }
             //Shout Attack my target, only works with the On Spawn In setup
             SpeakString("NW_ATTACK_MY_TARGET", TALKVOLUME_SILENT_TALK);
            }
         else
            {
             if ((GetHitDice(OBJECT_SELF)<4)||(d8()==1))
                 {
                  SetTarget(OBJECT_INVALID);
                  oTarget=OBJECT_INVALID;
                 }
                  else
                  {
                   ActionMoveToObject(oTarget,TRUE,4.0);
                  }
              }
         }




else
if((!GetIsInCombat())&&(GetSpawnInCondition(NW_FLAG_DAY_NIGHT_POSTING))&&(!GetIsObjectValid(oTarget)))
    {

        // turn off spot mode if in spot mode

        if (GetDetectMode(OBJECT_SELF))
        {
         ActionUseSkill(SKILL_SPOT,OBJECT_SELF);
        }

        int nDay = FALSE;
        if(GetIsDay() || GetIsDawn())
        {
            nDay = TRUE;
        }
        if(GetLocalInt(OBJECT_SELF, "NW_GENERIC_DAY_NIGHT") != nDay)
        {
            if(nDay == TRUE)
            {
                SetLocalInt(OBJECT_SELF, "NW_GENERIC_DAY_NIGHT", TRUE);
            }
            else
            {
                SetLocalInt(OBJECT_SELF, "NW_GENERIC_DAY_NIGHT", FALSE);
            }
            WalkWayPoints();
        }
    }

else
if((!GetIsInCombat())&&(!PRCGetHasEffect(EFFECT_TYPE_SLEEP))&&(!GetIsObjectValid(oTarget))&& (!IsInConversation(OBJECT_SELF)))
    {
      // make the mob sneak at this point if it should be
       CheckSneak(OBJECT_SELF);

      // check if it has any waypoints to walk
      if(GetIsPostOrWalking())
         {
          ///SpeakString("I should be wwp");
          WalkWayPoints();
         }
      else
      /// no waypoints so do something else
      if (GetisCustomEncounterCreature())
      {
       // custom encounter creatures check where they are and go home if needed
        if ((GetDistanceBetweenLocations(GetLocation(OBJECT_SELF),GetStartLocation())>7.0)||(GetAreaFromLocation(GetStartLocation())!=GetArea(OBJECT_SELF)))
             {
              ActionMoveToLocation(GetStartLocation());
             }
       // It is at home already so . .
       else
            {
             // check if we need to rest
             if (!MonsterRest())
                 { // We dont need to rest, so just do our animations
                 PlayMobileAmbientAnimations();
                 }
              }
       // End custom encounter creature stuff
      }
      else
      // Now we have non custom mobs - they just do immobile animations if needed
      if (GetSpawnInCondition(NW_FLAG_IMMOBILE_AMBIENT_ANIMATIONS))
      {
        //SpeakString("I have failed to wwp");
        PlayImmobileAmbientAnimations();
      }

  }




// Send the user-defined event signal if specified
if(GetSpawnInCondition(NW_FLAG_HEARTBEAT_EVENT))
    {
        SignalEvent(OBJECT_SELF, EventUserDefined(EVENT_HEARTBEAT));
    }

}

void CheckSneak(object oMob=OBJECT_SELF)
{
if ((GetChallengeRating(OBJECT_SELF)>3.0)&&(GetLevelByClass(CLASS_TYPE_ROGUE,OBJECT_SELF)>=1)&&(GetStandardFactionReputation(STANDARD_FACTION_DEFENDER,OBJECT_SELF)<40))
      {

           if (!GetStealthMode(OBJECT_SELF)==STEALTH_MODE_ACTIVATED)
           {
           ActionUseSkill(SKILL_HIDE,OBJECT_SELF);
           //SpeakString("stealthing from hb");
           return;
           }
           else
           {
            return;
           }


      }
      else
      {
       return;
      }

}