// v2.0.0 -
//::////////////////////////////////////////////////////////////////////////////
//:: FileName:  hark_d_track_to
//::////////////////////////////////////////////////////////////////////////////
/*
   The Henchman will attempt to find tracks of creatures on the ground and will
  begin following the tracks to the closest creature.
*/
//::////////////////////////////////////////////////////////////////////////////
#include "hark_i0_generic"
//::////////////////////////////////////////////////////////////////////////////
void main()
{
  // Determine if tracking is enabled in this area.
  object oMyArea  = GetArea(OBJECT_SELF);
  object oTrackDC = GetNearestObjectByTag(HS_TRACK_DC);
  int nTrackDC    = GetWillSavingThrow(oTrackDC);
  if (!GetIsObjectValid(oTrackDC) || nTrackDC <= 0 || GetArea(oTrackDC) != oMyArea)
    {
     SpeakString(TRACK_NOPRINTS);
     return;
    }

  // Set facing for the GetDireciton function and play some animation.
  object oMaster = GetRealMaster();
  //SetFacing(GetFacing(oMaster));
  ActionPlayAnimation(ANIMATION_LOOPING_LOOK_FAR, 1.0, 3.0);

  // Apply a penalty for bad weather.
  int nWeather = GetWeather(oMyArea);
  if (nWeather == WEATHER_RAIN || nWeather == WEATHER_SNOW)
    { nTrackDC += 2; }
  else
    { nTrackDC -= 2; }

  // Apply a bonus for natural settings, penalty otherwise.
  if (GetIsAreaNatural(oMyArea) == AREA_NATURAL)
    { nTrackDC -= 2; }
  else
    { nTrackDC += 2; }

  // Determine if any tracks are present.
  float fDis;
  int nAdj;
  int nCnt = 1;
  int nFoundTrack = 0;
  int nValidCreature = 0;
  int nCheck = d20() + GetLevelByClass(CLASS_TYPE_RANGER);
  object oCritter = GetNearestCreature(CREATURE_TYPE_IS_ALIVE, TRUE, OBJECT_SELF, nCnt);
  while (GetIsObjectValid(oCritter) && GetArea(oCritter) == oMyArea && nCnt < 11)
    {
     nValidCreature++;
     fDis = GetDistanceToObject(oCritter);
     nAdj = nTrackDC + FloatToInt(fDis / 10.0);
     if (nCheck >= nAdj && !GetHasFeat(FEAT_TRACKLESS_STEP, oCritter) &&
        !GetIsObjectValid(GetRealMaster(oCritter)) && oCritter != oMaster)
       {
        // Tracks found so break loop and start following.
        nFoundTrack++;
        SetAssociateState(NW_ASC_IS_BUSY);
        SetLocalObject(OBJECT_SELF, HS_TRACK_TARGET, oCritter);
        DelayCommand(3.1, ExecuteScript(sTrackingScript, OBJECT_SELF));
        break;
       }
     nCnt++;
     oCritter = GetNearestCreature(CREATURE_TYPE_IS_ALIVE, TRUE, OBJECT_SELF, nCnt);
    }

  // If tracking failed, give some feedback.
  if (nValidCreature > 0 && nFoundTrack == 0)
    { SpeakString(TRACK_FAILED); }
  else if (nValidCreature == 0)
    { SpeakString(TRACK_NOPRINTS); }
}
//::////////////////////////////////////////////////////////////////////////////
