// v2.1.0 - Modified the HenchGetLockedOrTrappedObject function slightly.
// v2.0.0 -
//::////////////////////////////////////////////////////////////////////////////
//:: FileName:  hark_i0_auto
//::////////////////////////////////////////////////////////////////////////////
/*
   This file contains functions used in the default On* scripts for henchman
  actions during noncombat. This includes dealing with traps, locks, items,
  and containers.
*/
//::////////////////////////////////////////////////////////////////////////////
#include "hark_i0_equip"
//::////////////////////////////////////////////////////////////////////////////


// * FUNCTION PROTOTYPES


void ClearForceOptions();

void OpenLock(object oLock);

void ForceTrap(object oTrap);

int GetIAmNotDoingAnything();

int HenchCheckArea(int nClearActions = FALSE);

// Returns the closest locked or trapped object to oMaster. (Up to 10 objects)
object HenchGetLockedOrTrappedObject(object oMaster);

// This will clear the caller's actions, state and force options.
void HenchResetHenchmenState();


// * FUNCTION DEFINITIONS


//::////////////////////////////////////////////////////////////////////////////
void ClearForceOptions()
{
  DeleteLocalObject(OBJECT_SELF, HS_MASTER_LOCK);
  DeleteLocalInt(OBJECT_SELF, HS_FORCE_TRAP);
}
//::////////////////////////////////////////////////////////////////////////////
void OpenLock(object oLock)
{
  if (GetIsObjectValid(oLock))
    {
     SetLocalObject(OBJECT_SELF, HS_MASTER_LOCK, oLock);
     ExecuteScript(sAutoScript, OBJECT_SELF);
    }
}
//::////////////////////////////////////////////////////////////////////////////
void ForceTrap(object oTrap)
{
  if (GetIsObjectValid(oTrap))
    {
     SetLocalObject(OBJECT_SELF, HS_MASTER_LOCK, oTrap);
     SetLocalInt(OBJECT_SELF, HS_FORCE_TRAP, TRUE);
     ExecuteScript(sAutoScript, OBJECT_SELF);
    }
}
//::////////////////////////////////////////////////////////////////////////////
int GetIAmNotDoingAnything()
{
  int nAction = GetCurrentAction(OBJECT_SELF);
  return (!IsInConversation(OBJECT_SELF) &&
          !GetIsObjectValid(GetAttemptedAttackTarget()) &&
          !GetIsObjectValid(GetAttemptedSpellTarget()) &&
           nAction != ACTION_REST && nAction != ACTION_DISABLETRAP &&
           nAction != ACTION_OPENLOCK && nAction != ACTION_USEOBJECT &&
           nAction != ACTION_RECOVERTRAP && nAction != ACTION_EXAMINETRAP &&
           nAction != ACTION_PICKUPITEM && nAction != ACTION_HEAL &&
           nAction != ACTION_TAUNT);
}
//::////////////////////////////////////////////////////////////////////////////
int HenchCheckArea(int nClearActions = FALSE)
{
  // Only execute if we have something to do.
  if (!GetAssociateState(NW_ASC_MODE_STAND_GROUND) &&
      !GetLocalInt(OBJECT_SELF, "DoNotAttack") &&
       GetIAmNotDoingAnything() &&
     ((GetHasSkill(SKILL_DISABLE_TRAP) &&
      !GetLocalInt(OBJECT_SELF, "NoDisarmTraps")) ||
       GetLocalInt(OBJECT_SELF, "AutoOpenLocks") ||
       GetLocalInt(OBJECT_SELF, "AutoPickup") ||
       GetLocalInt(OBJECT_SELF, "AutoOpenChest") ||
       GetIsObjectValid(GetLocalObject(OBJECT_SELF, HS_MASTER_LOCK))))
    {
     if (!GetLocalInt(OBJECT_SELF, "tk_doing_action"))
       {
        ExecuteScript(sAutoScript, OBJECT_SELF);
        return GetLocalInt(OBJECT_SELF, "tk_action_result");
       }
     else
       {
        ActionDoCommand(ExecuteScript(sAutoScript, OBJECT_SELF));
        return TRUE;
       }
    }
  else
    {
     ClearForceOptions();
    }
  return FALSE;
}
//::////////////////////////////////////////////////////////////////////////////
object HenchGetLockedOrTrappedObject(object oMaster)
{
  int nCnt = 1;
  int bValid = TRUE;
  object oLastObject = GetNearestObjectToLocation(OBJECT_TYPE_DOOR | OBJECT_TYPE_PLACEABLE, GetLocation(oMaster), nCnt);
  while (GetIsObjectValid(oLastObject) && bValid)
    {
     if (GetLocked(oLastObject) || (GetIsTrapped(oLastObject) &&
        (GetTrapDetectedBy(oLastObject, OBJECT_SELF) ||
         GetTrapDetectedBy(oLastObject, oMaster))))
       { return oLastObject; }
     nCnt++;
     if (nCnt == 10) { bValid = FALSE; }
     oLastObject = GetNearestObjectToLocation(OBJECT_TYPE_DOOR | OBJECT_TYPE_PLACEABLE, GetLocation(oMaster), nCnt);
    }
  return OBJECT_INVALID;
}
//::////////////////////////////////////////////////////////////////////////////
void HenchResetHenchmenState()
{
  WrapCommandable(TRUE);
  DeleteLocalObject(OBJECT_SELF, "NW_GENERIC_DOOR_TO_BASH");
  DeleteLocalInt(OBJECT_SELF, "NW_GENERIC_DOOR_TO_BASH_HP");
  SetAssociateState(NW_ASC_IS_BUSY, FALSE);
  ClearForceOptions();
  ClearActions(CLEAR_I0_AUTO_HenchResetHenchmenState);
}
//::////////////////////////////////////////////////////////////////////////////
//void main(){}
