/*//////////////////////////////////////////////////////////////////////////////
 Script Name: 0e_player_target
 Programmer: Philos
////////////////////////////////////////////////////////////////////////////////
 OnPlayerTarget event script
    Used to allow player targeting while passing any module player targeting
    script through to work as intended.

    We Use a string variable upon the player using the targeting mode to define the
    action of the target.
    AI_TARGET_MODE is the constant used.
    AI_TARGET_ASSOCIATE is the associate that triggered the target mode.

    AI_TARGET_MODE_ON defines if the player is in target mode for a henchman instead of the PC.
/*//////////////////////////////////////////////////////////////////////////////
#include "0i_player_target"
void main()
{
    object oPC = GetLastPlayerToSelectTarget();
    // Get any plugin target scripts and run it instead of this one.
    string sPluginTargetScript = GetLocalString(oPC, AI_PLUGIN_TARGET_SCRIPT);
    if(sPluginTargetScript != "")
    {
        DeleteLocalString(oPC, AI_PLUGIN_TARGET_SCRIPT);
        ExecuteScript(sPluginTargetScript, oPC);
        // Remove the plugin script as it must be set each time the plugin uses the target event.
    }
    else
    {
        // Get the targeting mode data
        object oTarget = GetTargetingModeSelectedObject();
        vector vTarget = GetTargetingModeSelectedPosition();
        location lLocation = Location(GetArea(oPC), vTarget, GetFacing(oPC));
        object oAssociate = GetLocalObject(oPC, AI_TARGET_ASSOCIATE);
        string sTargetMode = GetLocalString(oPC, AI_TARGET_MODE);
        DeleteLocalString(oPC, AI_TARGET_MODE);
        // ********************* Exiting Target Actions ************************
        // If the user manually exited targeting mode without selecting a target, return
        if(!GetIsObjectValid(oTarget) && vTarget == Vector())
        {
            if(sTargetMode == "ASSOCIATE_ACTION_ALL")
            {
                ai_SendMessages("You have exited selecting an action for the party.", AI_COLOR_YELLOW, oPC);
                ai_RemoveAllActionMode(oPC);
                return;
            }
            else if(sTargetMode == "ASSOCIATE_ACTION")
            {
                ai_SendMessages("You have exited selecting an action for " + GetName(oAssociate) + ".", AI_COLOR_YELLOW, oPC);
                // Clean up any PC AI being turned on as well as variables.
                DeleteLocalObject(oPC, AI_TARGET_ASSOCIATE);
                DeleteLocalInt(oPC, AI_TARGET_MODE_ON);
                DeleteLocalObject(oPC, AI_TARGET_MODE_ASSOCIATE);
                // Make sure the camera goes back to the player since we are leaving henchmen control.
                if(GetLocalObject(oPC, "AI_CAMERA_ON_ASSOCIATE") != OBJECT_INVALID)
                {
                    DeleteLocalObject(oPC, "AI_CAMERA_ON_ASSOCIATE");
                    AttachCamera(oPC, oPC);
                    if(!GetLocalInt(GetModule(), AI_USING_PRC)) ai_TurnOff(oPC, oPC, "pc");
                }
                ai_SetAIMode(oAssociate, AI_MODE_COMMANDED, FALSE);
                if(ai_GetAIMode(oPC, AI_MODE_ACTION_GHOST) && !ai_GetAIMode(oPC, AI_MODE_GHOST) &&
                   GetLocalInt(oAssociate, sGhostModeVarname))
                {
                    ai_RemoveASpecificEffect(oAssociate, EFFECT_TYPE_CUTSCENEGHOST);
                    DeleteLocalInt(oAssociate, sGhostModeVarname);
                }
                ExecuteScript("nw_ch_ac1", oAssociate);
                return;
            }
            else if(sTargetMode == "ASSOCIATE_GET_TRAP")
            {
                ai_SendMessages(GetName(oAssociate) + " has exited selecing a trap!", AI_COLOR_YELLOW, oPC);
                if(GetLocalInt(oPC, AI_TARGET_MODE_ON)) ai_EnterAssociateTargetMode(oPC, GetLocalObject(oPC, AI_TARGET_MODE_ASSOCIATE));
                return;
            }
            else if(sTargetMode == "ASSOCIATE_PLACE_TRAP")
            {
                ai_SendMessages(GetName(oAssociate) + " has exited placing the trap!", AI_COLOR_YELLOW, oPC);
                if(GetLocalInt(oPC, AI_TARGET_MODE_ON)) ai_EnterAssociateTargetMode(oPC, GetLocalObject(oPC, AI_TARGET_MODE_ASSOCIATE));
                return;
            }
            else if(sTargetMode == "DM_SELECT_CAMERA_VIEW")
            {
                AttachCamera(oPC, oPC);
                ai_SendMessages(GetName(oPC) + " has defaulted camera view back to the player!", AI_COLOR_YELLOW, oPC);
                return;
            }
            // If these actions are canceled and we are in target mode with a henchmen
            // then turn target mode back on for that henchmen.
            else if(sTargetMode == "ASSOCIATE_USE_ITEM" ||
                    sTargetMode == "ASSOCIATE_USE_FEAT" ||
                    sTargetMode == "ASSOCIATE_CAST_SPELL" ||
                    sTargetMode == "ASSOCIATE_FOLLOW_TARGET")
            {
                if(GetLocalInt(oPC, AI_TARGET_MODE_ON)) ai_EnterAssociateTargetMode(oPC, GetLocalObject(oPC, AI_TARGET_MODE_ASSOCIATE));
                return;
            }
        }
        // ************************* Targeted Actions **************************
        else
        {
            // This action makes an associates move to vTarget.
            if(sTargetMode == "ASSOCIATE_ACTION_ALL")
            {
                ai_ActionAllAssociates(oPC, oTarget, lLocation);
                SetLocalString(oPC, AI_TARGET_MODE, "ASSOCIATE_ACTION_ALL");
                EnterTargetingMode(oPC, OBJECT_TYPE_ALL, MOUSECURSOR_ACTION, MOUSECURSOR_NOWALK);
                return;
            }
            else if(sTargetMode == "ASSOCIATE_ACTION")
            {
                AssignCommand(oAssociate, ai_ActionAssociate(oPC, oTarget, lLocation));
                return;
            }
            else if(sTargetMode == "ASSOCIATE_FOLLOW_TARGET")
            {
                ai_SelectFollowTarget(oPC, oAssociate, oTarget);
                return;
            }
            else if(sTargetMode == "ASSOCIATE_GET_TRAP")
            {
                ai_SelectTrap(oPC, oAssociate, oTarget);
                return;
            }
            else if(sTargetMode == "ASSOCIATE_PLACE_TRAP")
            {
                AssignCommand(oAssociate, ai_PlaceTrap(oPC, lLocation));
                return;
            }
            else if(sTargetMode == "ASSOCIATE_USE_ITEM")
            {
                if(oTarget == GetArea(oPC)) oTarget = OBJECT_INVALID;
                ai_UseWidgetItem(oPC, oAssociate, oTarget, lLocation);
                DelayCommand(6.0, ai_UpdateAssociateWidget(oPC, oAssociate));
                if(GetLocalInt(oPC, AI_TARGET_MODE_ON)) ai_EnterAssociateTargetMode(oPC, GetLocalObject(oPC, AI_TARGET_MODE_ASSOCIATE));
                return;
            }
            else if(sTargetMode == "ASSOCIATE_USE_FEAT")
            {
                if(oTarget == GetArea(oPC)) oTarget = OBJECT_INVALID;
                ai_UseWidgetFeat(oPC, oAssociate, oTarget, lLocation);
                DelayCommand(6.0, ai_UpdateAssociateWidget(oPC, oAssociate));
                if(GetLocalInt(oPC, AI_TARGET_MODE_ON)) ai_EnterAssociateTargetMode(oPC, GetLocalObject(oPC, AI_TARGET_MODE_ASSOCIATE));
                return;
            }
            else if(sTargetMode == "ASSOCIATE_CAST_SPELL")
            {
                if(oTarget == GetArea(oPC)) oTarget = OBJECT_INVALID;
                ai_CastWidgetSpell(oPC, oAssociate, oTarget, lLocation);
                DelayCommand(6.0, ai_UpdateAssociateWidget(oPC, oAssociate));
                if(GetLocalInt(oPC, AI_TARGET_MODE_ON)) ai_EnterAssociateTargetMode(oPC, GetLocalObject(oPC, AI_TARGET_MODE_ASSOCIATE));
                return;
            }
            else if(sTargetMode == "DM_SELECT_CAMERA_VIEW")
            {
                AttachCamera(oPC, oTarget);
                ai_SendMessages(GetName(oPC) + " has changed the camera view to " + GetName(oTarget) + ".", AI_COLOR_YELLOW, oPC);
                return;
            }
            else if(sTargetMode == "DM_SELECT_OPEN_INVENTORY")
            {
                if(LineOfSightObject(oPC, oTarget))
                {
                    OpenInventory(oTarget, oPC);
                    ai_SendMessages("You have opened the inventory of "+ GetName(oTarget) + ".", AI_COLOR_YELLOW, oPC);
                }
                else ai_SendMessages(GetName(oTarget) + " is not in your line of sight!", AI_COLOR_YELLOW, oPC);
                return;
            }
            else if(GetStringLeft(sTargetMode, 15) == "DM_SELECT_GROUP")
            {
                ai_AddToGroup(oPC, oTarget, sTargetMode);
                return;
            }
            else if(GetStringLeft(sTargetMode, 15) == "DM_ACTION_GROUP")
            {
                ai_DMAction(oPC, oTarget, lLocation, sTargetMode);
                return;
            }
        }
        // Get saved module player target script and execute it for pass through compatibility.
        ExecuteScript(GetLocalString(GetModule(), AI_MODULE_TARGET_EVENT));
    }
}
