////////////////////////////////////////////////////////////////////////////////
//
//  Olander's AI
//  nw_c2_default4
//  by Don Anderson
//  dandersonru@msn.com
//
//  OnConversation
//  Original script by Bioware
//
////////////////////////////////////////////////////////////////////////////////

#include "oai_inc_ai"

void main()
{
    // * if petrified, jump out
    if (GetHasEffect(EFFECT_TYPE_PETRIFY, OBJECT_SELF) == TRUE) return;

    // * If dead, exit directly.
    if (GetIsDead(OBJECT_SELF) == TRUE) return;
	
    int nMatch = GetListenPatternNumber();
    object oShouter = GetLastSpeaker();

    //DMFI CODE ADDITIONS BEGIN HERE
    if(GetIsPC(oShouter)) ExecuteScript("dmfi_voice_exe", OBJECT_SELF);

    if(nMatch == -1 && GetIsPC(oShouter) &&(GetLocalInt(GetModule(), "dmfi_AllMute") || GetLocalInt(OBJECT_SELF, "dmfi_Mute")))
    {
      SendMessageToAllDMs(GetName(oShouter) + " is trying to speak to a muted NPC, " + GetName(OBJECT_SELF) + ", in area " + GetName(GetArea(OBJECT_SELF)));
      SendMessageToPC(oShouter, "This NPC is muted. A DM will be here shortly.");
      return;
    }
    //DMFI CODE ADDITIONS END HERE

    if (nMatch == -1)
    {
      // Not a match -- start an ordinary conversation
      if (GetCommandable(OBJECT_SELF))
      {
        ClearActions(CLEAR_NW_C2_DEFAULT4_29);
        BeginConversation();
      }
      else
      // * July 31 2004
      // * If only charmed then allow conversation
      // * so you can have a better chance of convincing
      // * people of lowering prices
      if (GetHasEffect(EFFECT_TYPE_CHARMED) == TRUE)
      {
        ClearActions(CLEAR_NW_C2_DEFAULT4_29);
        BeginConversation();
      }
    }
    // Respond to shouts from friendly non-PCs only
    else if (GetIsObjectValid(oShouter)
               && !GetIsPC(oShouter)
               && GetIsFriend(oShouter))
    {
        object oIntruder = OBJECT_INVALID;

        // Determine the intruder if any
        if(nMatch == 4)
        {
          oIntruder = GetLocalObject(oShouter, "NW_BLOCKER_INTRUDER");
        }
        else if(nMatch == 3)
        {
          if(GetStatusCondition(OAI_I_CAN_RAISE_DEAD)) SetLocalObject(OBJECT_SELF, "OAI_RAISE_TARGET", oShouter);
        }
        else if (nMatch == 5)
        {
          oIntruder = GetLastHostileActor(oShouter);
          if(!GetIsObjectValid(oIntruder))
          {
            oIntruder = GetAttemptedAttackTarget();
            if(!GetIsObjectValid(oIntruder))
            {
              oIntruder = GetAttemptedSpellTarget();
              if(!GetIsObjectValid(oIntruder))
              {
                oIntruder = OBJECT_INVALID;
              }
            }
          }
        }
        else if (nMatch == 5000)
        {
          SetLocalInt(OBJECT_SELF,"HEARD_THE_CALL",1);
          FOF_Execute();
          return;
        }
        else if (nMatch == 5001)
        {
          object oTarget = GetLastHostileActor(oShouter);
          int nCheck = GetLocalInt(OBJECT_SELF,"RETREATED");
          if (nCheck != 1)
          {
            ClearAllActions();
            OAI_DetermineCombatRound(oTarget);
            return;
          }
        }

        // Actually respond to the shout
        RespondToShout(oShouter, nMatch, oIntruder);
    }

    // Send the user-defined event if appropriate
    if(GetSpawnInCondition(NW_FLAG_ON_DIALOGUE_EVENT))
    {
      SignalEvent(OBJECT_SELF, EventUserDefined(1004));
    }
}