// uber_hb for the mages that guard the uber items
#include "NW_I0_GENERIC"
#include "antistuck_h"
void fnRest()
{
  SetLocalInt(OBJECT_SELF,"nRest",TRUE);
  DelayCommand(HoursToSeconds(8),fnRest());
} // rest


void main()
{
    object oMe=OBJECT_SELF;
    object oItem=GetLocalObject(oMe,"oItem");
    object oTarget=GetItemPossessor(oItem);
    object oArea;
    string sTag;
    float fDist=GetDistanceBetween(oMe,oItem);
    int nRest=GetLocalInt(oMe,"nRest");
    int nFirstTime=GetLocalInt(oMe,"nFirstTime");
    effect eVFX=EffectVisualEffect(VFX_FNF_IMPLOSION);
    object oWP;
    int nR;
    if (oItem==OBJECT_INVALID) DelayCommand(2.0,DestroyObject(oMe));
    if (nFirstTime!=TRUE)
    {
       DelayCommand(HoursToSeconds(8),fnRest());
       SetLocalInt(oMe,"nFirstTime",TRUE);
    }
    if (GetIsInCombat(oMe)==FALSE)
    { // not fighting
      if (nRest==TRUE&&oTarget==OBJECT_INVALID&&GetArea(oMe)==GetArea(oItem)&&fDist<20.0)
      { // okay to rest maybe
        oTarget=GetNearestCreature(CREATURE_TYPE_PERCEPTION,PERCEPTION_SEEN,oMe,1,CREATURE_TYPE_PLAYER_CHAR,PLAYER_CHAR_IS_PC);
        if (oTarget==OBJECT_INVALID)
        { // okay to rest
          AssignCommand(oMe,ClearAllActions());
          AssignCommand(oMe,ActionRest());
          DeleteLocalInt(oMe,"nRest");
        } // okay to rest
      } // okay to rest maybe
      else if (oTarget!=OBJECT_INVALID)
      { // someone has the item
        oArea=GetArea(oTarget);
        sTag=GetTag(oArea);
        fDist=GetDistanceBetween(oMe,oTarget);
        if (sTag=="ThePlaneofInBetween")
        { // target died... get item back
          sTag=GetResRef(oItem);
          DestroyObject(oItem);
          oItem=CreateObject(OBJECT_TYPE_ITEM,sTag,GetLocation(oMe));
          SetLocalObject(oMe,"oItem",oItem);
        } // target died... get item back
        else if (GetArea(oMe)!=GetArea(oTarget))
        { // target is in another area - teleport
          oWP=GetNearestObject(OBJECT_TYPE_WAYPOINT,oTarget,d4());
          if (oWP==OBJECT_INVALID) oWP=GetNearestObject(OBJECT_TYPE_WAYPOINT,oTarget,1);
          ApplyEffectAtLocation(DURATION_TYPE_INSTANT,eVFX,GetLocation(oMe));
          if (oWP!=OBJECT_INVALID)
          { // teleport to nearby waypoint
            ApplyEffectAtLocation(DURATION_TYPE_INSTANT,eVFX,GetLocation(oWP));
            AssignCommand(oMe,ClearAllActions());
            AssignCommand(oMe,JumpToObject(oWP));
          } // teleport to nearby waypoint
          else
          { // teleport to player
            ApplyEffectAtLocation(DURATION_TYPE_INSTANT,eVFX,GetLocation(oTarget));
            AssignCommand(oMe,ClearAllActions());
            AssignCommand(oMe,JumpToLocation(GetLocation(oTarget)));
          } // teleport to player
        } // target is in another area - teleport
        else if (fDist>10.0&&GetObjectSeen(oTarget)==FALSE&&GetObjectHeard(oTarget)==FALSE)
        { // not seen or heard and more than 10 meters away
          AssignCommand(oMe,ClearAllActions());
          AssignCommand(oMe,ASActionMoveToObject(oItem,TRUE,1.0));
        } // not seen or heard and more than 10 meters away
        else
        { // engage them in combat
          DetermineCombatRound();
        } // engage them in combat
      } // someone has the item
      else if (GetArea(oMe)!=GetArea(oItem))
      { // item is in another area
        oWP=GetNearestObject(OBJECT_TYPE_WAYPOINT,oItem,d4());
          if (oWP==OBJECT_INVALID) oWP=GetNearestObject(OBJECT_TYPE_WAYPOINT,oItem,1);
          ApplyEffectAtLocation(DURATION_TYPE_INSTANT,eVFX,GetLocation(oMe));
          if (oWP!=OBJECT_INVALID)
          { // teleport to nearby waypoint
            ApplyEffectAtLocation(DURATION_TYPE_INSTANT,eVFX,GetLocation(oWP));
            AssignCommand(oMe,ClearAllActions());
            AssignCommand(oMe,JumpToObject(oWP));
          } // teleport to nearby waypoint
          else
          { // teleport to item
            ApplyEffectAtLocation(DURATION_TYPE_INSTANT,eVFX,GetLocation(oItem));
            AssignCommand(oMe,ClearAllActions());
            AssignCommand(oMe,JumpToLocation(GetLocation(oItem)));
          } // teleport to item
      } // item is in another area
      else if (fDist>4.9)
      { // not close to item
        AssignCommand(oMe,ClearAllActions());
        AssignCommand(oMe,ASActionMoveToObject(oItem,TRUE,1.0));
      } // not close to item
      else
      { // ambient animations
        if (GetNearestCreature(CREATURE_TYPE_PLAYER_CHAR,PLAYER_CHAR_IS_PC,oMe,1)!=OBJECT_INVALID)
        { // player present - animate
          nR=d4();
          if (nR==1) AssignCommand(oMe,ActionRandomWalk());
          else if (nR==2) AssignCommand(oMe,ActionPlayAnimation(ANIMATION_FIREFORGET_DRINK,1.0,6.0));
          else if (nR==3) AssignCommand(oMe,ActionPlayAnimation(ANIMATION_LOOPING_SIT_CROSS,1.0,6.0));
          else if (nR==4) AssignCommand(oMe,ActionPlayAnimation(ANIMATION_LOOPING_LOOK_FAR,1.0,6.0));
        } // player present - animate
      } // ambient animations
    } // not fighting
    if ((GetArea(oItem)==GetArea(oMe)||fDist>30.0)&&oTarget==OBJECT_INVALID)
    { // stop fighting and go back to the item
      AssignCommand(oMe,ClearAllActions(TRUE));
      AssignCommand(oMe,ASActionMoveToObject(oItem,TRUE,1.0));
    } // stop fighting and go back to the item
}